Reputation: 11
I am creating a game which begins with a log in feature
The user can 'Log in (A) or Create an account(B)'
My issue is if the user logs in with a username which doesn't exist I get a KeyError: "(whatever username they typed)"
Expected Result: I would like the code to output 'User doesn't exist' if this happens.
The problem can be reproduced with this code and typing 'A' to log in and entering a random username which doesn't exist.
users = {} # Currently empty list called Users. Stores all log ins
global status
status = ""
def LogIn():#Function is called Log In. Can be called at any time.
status = input("Log in (A) or Create an account(B) - Enter 'A' or 'B' ") # asks for log in information
status = status.upper()
if status == "A":
oldUser() #Already has an account
elif status == "B":
newUser() #Would like to make a new account
return status #Moves on to the function named status
def newUser(): # Creating an account.
CreateUsername = input("Create username: ") #Enter a what your username is
if CreateUsername in users: # check if login name exists in the list
print ("\n Username already exists!\n")
else:
createPassw = input("Create password: ")
users[CreateUsername] = createPassw # add login and password
print("\nUser created!\n")
def oldUser():
username = input("Enter username: ")
passw = input("Enter password: ")
# check if user exists and login matches password
if passw == users[username]:
print ("Login successful!\n")
print('Game begins')
else:
print ("\nUser doesn't exist or wrong password!\n")
while status != "q":
status = LogIn()
Extra Info: more context of how the log in feature works.
Upvotes: 0
Views: 350
Reputation: 11
Fixed by altering
def oldUser():
username = input("Enter username: ")
passw = input("Enter password: ")
# check if user exists and login matches password
if username not in users:
print ("Username doesn't exist")
print('Game begins')
elif passw == users[username]:
print('Log In successful')
else:
print ("\nUser doesn't exist or wrong password!\n")
while status != "q":
status = LogIn()
Upvotes: 0
Reputation: 35
Look into using python's try
and except
. These exist to help us handle errors when they arise while customizing the way you handle those errors. So for your specific question, give this a shot:
def oldUser():
username = input("Enter username: ")
passw = input("Enter password: ")
# check if user exists and login matches password
try:
if passw == users[username]:
print ("Login successful!\n")
print('Game begins')
except:
print ("\nUser doesn't exist or wrong password!\n")
Upvotes: 0
Reputation: 81
You're getting an error because you're trying to access the users
dictionary with a key that isn't in the dictionary. The key you're using is the username of the user. Therefore, when there isn't a user in the dictionary with that username you receive the KeyError
An alternative to using try
and except
would be to restructure your dictionary to be an array of user dictionaries, with each user dictionary containing the keys username
and password
Upvotes: 1