1
\$\begingroup\$

I'm trying to make a password program that you can login to or make a new user. I want to make this better, so any help would be appreciated, also any suggestions to make this better would be good

import time 
LOGIN = ("Login", "login", "L", "l", "LOGIN") 
CREATE = ("Create", "create", "C", "c", "CREATE", "Create Account", "Create account", "create Account", "create account", "CREATE ACCOUNT") 
x = 0 
newPass = "X" 
newUser = "X" 
validEntry = { 
 "Liam" : "liampass", 
 "Bob" : "bobpass" 
} #All current usernames and passwords stored in this dictionary
print("--==Welcome==--") 
time.sleep(1) 
def choice(): 
 print("Login or Create Account?") 
 choice = input(":>>>") 
 if choice in LOGIN: 
 login() 
 elif choice in CREATE: 
 create() #Choice to login or create
def login(): 
 print(""" 
Please login. Case sensitive.""") 
 time.sleep(0.5) 
 print("Username.") 
 entry = input(":>>>") 
 if entry in validEntry: 
 x = 0 
 while x < 3: 
 print("Input Password.") 
 passentry = input(":>>>") 
 passright = validEntry.get(entry) #Checks dictionary to find password associated with username entered
 if passentry == passright: 
 print("Successful Login!") 
 x += 5 #Correct password, addition code could be added here
 else: 
 print("""Incorrect password, try again 
 """) 
 x += 1 #Incorrect password, allows three attempts 
 else: 
 print("Username not recognised!") 
 login() 
def create(): 
 print("Please choose a username") 
 newUser = str(input(":>>>")) 
 print("Please choose a password for", newUser) 
 newPass = str(input(":>>>")) 
 global newUser, newPass #Globalizes both new password and username for entry to dictionary 
choice() 
validEntry[newUser] = newPass #Adds new username and password to dictionary 
choice() #You can only make one new account then login
asked Feb 24, 2020 at 21:40
\$\endgroup\$
5
  • \$\begingroup\$ Welcome to code review where we review working code and provide suggestions on how that code can be improved. Unfortunately we can't help you write code that isn't written yet, such as adding a new username and password to a file. \$\endgroup\$ Commented Feb 24, 2020 at 21:54
  • 1
    \$\begingroup\$ @pacmaninbw OP rephrased the question since your comment. Looks okay...ish now, I'd say. \$\endgroup\$ Commented Feb 25, 2020 at 14:57
  • 1
    \$\begingroup\$ @AlexV VTC retracted \$\endgroup\$ Commented Feb 25, 2020 at 15:12
  • \$\begingroup\$ @pacmaninbw what is a 'VTC'? \$\endgroup\$ Commented Feb 25, 2020 at 17:09
  • 1
    \$\begingroup\$ @Yonlif Vote to close. Whenever a question is in violation of the site's rules (as per the help center, community members will close the question so it isn't answered by accident. Closed questions should be fixed before they're answered to improve the quality and helpfulness of both the question an answers. \$\endgroup\$ Commented Feb 26, 2020 at 7:45

1 Answer 1

3
\$\begingroup\$

I have some suggestions, some regarding the code and some regarding the logic of it. Generally this is a nice code.
Let's begin, logical suggestions:

  • First of all, the code does not run. SyntaxError: name 'newUser' is used prior to global declaration. You should put the global keyword before declaring the variables.

  • Use the __main__ function. Usually when writing a good python code you do not write logic outside of functions. This will make your code more readable. You can than put the logic in a while True loop and keep accept users forever and not just twice.

Coding suggestions:

  • Magic numbers. The number of time you can accept a wrong password is 3, make it a constant: NUMBER_OF_ERROR = 3.

  • You do not need to declare x outside of the login function.

  • Instead of x += 5 just use break, it makes much more sense.

  • Do not call the login function at the end of login. It creates unnecessary recursion.

That's it.
Good luck and have fun

answered Feb 25, 2020 at 17:26
\$\endgroup\$

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.