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.
What I made:
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():
username = input("Please create a username: ")
password = input("Please create a password: ")
file = open("accountfile.txt","a")
file.write(username)
file.write(" ")
file.write(password)
file.write("\n")
file.close()
if login():
print("You are now logged in...")
else:
print("You aren't logged in!")
The login snippet:
def login():
username = input("Please enter your username: ")
password = input("Please enter your password: ")
for line in open("accountfile.txt","r").readlines():
login_info = line.split()
if username == login_info[0] and password == login_info[1]:
print("Correct credentials!")
return True
print("Incorrect credentials.")
return False
required_input1 = "register"
required_input2 = "login"
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:
login()
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!