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
-
\$\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\$pacmaninbw– pacmaninbw ♦2020年02月24日 21:54:10 +00:00Commented Feb 24, 2020 at 21:54
-
1\$\begingroup\$ @pacmaninbw OP rephrased the question since your comment. Looks okay...ish now, I'd say. \$\endgroup\$AlexV– AlexV2020年02月25日 14:57:28 +00:00Commented Feb 25, 2020 at 14:57
-
1\$\begingroup\$ @AlexV VTC retracted \$\endgroup\$pacmaninbw– pacmaninbw ♦2020年02月25日 15:12:50 +00:00Commented Feb 25, 2020 at 15:12
-
\$\begingroup\$ @pacmaninbw what is a 'VTC'? \$\endgroup\$Yonlif– Yonlif2020年02月25日 17:09:51 +00:00Commented 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\$Mast– Mast ♦2020年02月26日 07:45:48 +00:00Commented Feb 26, 2020 at 7:45
1 Answer 1
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 theglobal
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 awhile 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 thelogin
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