Recently I have been learning Python after years of putting it off, and I am having a really fun time with it. However after I spent a few hours making something that I thought was pretty cool, I decided to share it to one of the Discord servers that I am in. And then from there on everyone just kind of mocked me, and my code even got posted on the r/badcode subreddit where they mocked me even more. All I want to know is if I actually did something really stupid and if so how can I fix it, or if they are all just being mean because I am new to the language.

While learning python I kept stumbling across a ton of "Python projects for beginners" sites, and I took a lot of interest in them, as they were fun to make and it was cool to see the result. What I did with this was take two of the projects and combine them into one just for a little challenge for myself.

The two that I combined were; Making a register/login form for the console, and a rock paper scissors game against the computer. A silly idea that is impractical but was just a little side project for fun.

### The code:

The register snippet:

import random

def register():
file = open("accountfile.txt","a")
file.write(" ")
file.write("\n")
file.close()
print("You are now logged in...")
else:
print("You aren't logged in!")


def login():
print("Correct credentials!")
return True
print("Incorrect credentials.")
return False

required_input1 = "register"
user_input = input("What would you like to do?: ")
while user_input == "commands":
print("My commands are: register, login and commands, all commands are case sensitive!")
user_input = input("What would you like to do?: ")
if user_input == required_input1:
register()
elif user_input == required_input2:


The rock/paper/scissors snippet:

def game():
place = "test"
user_wins = 0
computer_wins = 0
draw = 0

options = ["rock", "paper", "scissors"]
while True:
user_input = input("Type Rock/Paper/Scissors or Q to quit: ").lower()
if user_input == "q":
print("Goodbye!")
quit()
elif user_input not in options:
continue

random_number = random.randint(0, 2)
# rock : 0, paper: 1, scissors: 2
computer_pick = options[random_number]
print("Computer picked", computer_pick + ".")

if user_input == computer_pick:
print('Draw!')
draw += 1
elif user_input == "rock" and computer_pick == "scissors":
print("You won!")
user_wins += 1
elif user_input == "paper" and computer_pick == "rock":
print("You won!")
user_wins += 1
elif user_input == "scissors" and computer_pick == "scissors":
print("You won!")
user_wins += 1
else:
print("Computer wins")
computer_wins += 1

if computer_wins == 3:
print("The computer won that game, better luck next time!")
quit()
elif user_wins == 3:
print("You won the game, nice job!")
quit()


### Summary

As I stated previously, all the code functions how I want it to. However because of the mockery I am receiving I am assuming that there is a much easier way of executing this.
Let me know what you guys think, and thank you for your time!

• Welcome to Code Review! Please verify that the code you posted is indented as intended, especially for the rock-paper-scissors snippet. May 6 at 18:54
• Are you sure you want a Python-2.x review? This looks more like Python-3.x code. May 6 at 19:26
• I think it's mean-spirited for someone to have posted this on /r/badcode. Since you describe yourself as a beginner, it's not like you're expected to know better; and it's not great but it isn't a disaster either. May 6 at 19:27

Recently I have been learning Python after years of putting it off, and I am having a really fun time with it.

Keep it up!

In register, your file needs to be protected with a with so that - if anything bad happens (an exception) after the file is open, closure is still guaranteed.

Consider checking whether the username already exists when the user attempts to register.

Your database format for accounts is a problem. If a person has spaces in their username or password, your current code will not work. There are many ways to fix this. One simple way is to base64-encode your fields; spaces will not affect them.

Never input() to get a password. Use getpass instead, which is more secure because it does not echo characters that are input.

I assume that you login() during your register so that the user can re-enter their credentials, to make sure no typos were made. But in your implementation, the damage is already done: if they did make a typo, you've already saved it to a file. You should make an attempt at temporarily verifying those credentials before they're saved to the file.

options should be a tuple rather than a list since you're not changing it.

Don't quit() where you do so; instead return from a function that includes this code. That way, in the future if you want to include the ability for a player to play again, it won't be as difficult.

I assume that you need to log in before you play the game. You have not shown this.

Some of your commands are case-sensitive and others aren't. Why? Might as well make them all case-insensitive.

## Suggested

import random
from getpass import getpass
from base64 import b64encode, b64decode
from typing import Iterator

def load_accounts(filename: str = 'accountfile.txt') -> Iterator[tuple[str, str]]:
with open(filename) as file:
for line in file:
b64decode(word).decode()
for word in line.split()
)

def login_with_accounts(accounts: dict[str, str]) -> bool:
print("Correct credentials!")
return True
print("Incorrect credentials.")
return False

)

def register() -> None:
try:
except FileNotFoundError:
accounts = {}

while True:

continue

break

with open("accountfile.txt", "ba") as file:
file.write(b' ')
file.write(b'\n')

def authenticate() -> None:
while True:
user_input = input("What would you like to do?: ").lower()
if user_input == 'register':
register()
break
break

def game() -> None:
user_wins = 0
computer_wins = 0
draw = 0

options = ("rock", "paper", "scissors")
while True:
user_input = input("Type Rock/Paper/Scissors or Q to quit: ").lower()
if user_input == "q":
print("Goodbye!")
return
if user_input not in options:
continue

computer_pick = random.choice(options)
print("Computer picked", computer_pick + ".")

if user_input == computer_pick:
print('Draw!')
draw += 1
elif (
user_input == "rock" and computer_pick == "scissors" or
user_input == "paper" and computer_pick == "rock" or
user_input == "scissors" and computer_pick == "scissors"
):
print("You won!")
user_wins += 1
else:
print("Computer wins")
computer_wins += 1

if computer_wins >= 3:
print("The computer won that game, better luck next time!")
return
elif user_wins >= 3:
print("You won the game, nice job!")
return

if __name__ == '__main__':
authenticate()
game()

• Your reasons for using with are good, but turning load_accounts into a generator negates them. If an exception happens in code using the generator, after the generator is started but before it is exhausted, it will be left dangling, unclosed until garbage collection or interpreter exit, meaning so will the file. In both cases, it is fully consumed by a dict() call, but why complicate beginner code with a potentially unsafe generator construct when load_accounts() could directly return a dict? May 7 at 15:48
• @AJNeufeld "Why complicate beginner code" is valid criticism, but your mental model of a context manager is incorrect. Try out this demo gist. May 7 at 15:56
• Specifically, the mechanism by which the generator stops and the context manager is closed is GeneratorExit. May 7 at 16:02
• Your gist demonstrates a GeneratorExit is sent to the generator (due to being garbage collected possibly as late as at interpreter exit), but does not demonstrate how soon it happens which was my concern. My concern was a heavily workload might delay garbage collection. Playing around, I see as long as only local variables are used, the interpreter does seem to ensure the generator is closed expeditiously - when the last local variable holding a reference to the variable goes out of scope. It took intentionally doing something stupid to substantially delay generator exit. May 9 at 17:35
• Dangling circular references. Dict A with a reference to Dict B which held a reference to Dict A and the generator. No other references to either dictionaries. Garbage collector has to mark-and-sweep to get rid of them. Kept busy with sum(sqrt(range(1,10_000_000)). As I said, intentionally doing something stupid, that should never normally occur. Using with inside a generator is not unsafe, as I feared. May 9 at 17:49