im trying to write code in python that basically its so that in the terminal you input whether you want to signup or log in if you already signed up i cant really figure out how to make it so that the signup input gets stored in a dictionary and then later when you try to enter the username and password it makes sure its the same one from the signup feature thanks in advance
so far ive tried
accounts = {"user":"password", "user2":"password2"}
login_or_signup = input("Login or signup? ")
if login_or_signup.upper() == 'LOGIN':
username = input("Enter your username: ")
if username in list(accounts.keys()):
password = input("Enter your password: ")
if password in list(accounts.values()):
print("Logged in successfully.")
else:
print("Account credentials do not match.")
else:
print("Account not found.")
elif login_or_signup.upper() == "SIGNUP":
username = input("Enter your username: ")
password = input("Enter your password: ")
accounts.update({user,password})
but im getting an error
CodePudding user response:
You have a typo in your last line, it should be username
instead of user
. Also :
instead of ,
accounts.update({username: password})
That will make your program not fail in both options.
However, first, you are not checking properly the password, you need to grab the user and check if the password corresponds to the user in your dictionary.
Additionally, your program will finish right after you set up your new signup. You can add a while True
loop at the top and break
accordingly in the condition you would like to do so (example)
CodePudding user response:
You should check if input password, matches the input username.
accounts = {"user": "password", "user2": "password2"}
login_or_signup = input("Login or signup? ")
while True:
if login_or_signup.upper() == 'LOGIN':
username = input("Enter your username: ")
if username in accounts:
password = input("Enter your password: ")
if accounts[username] == password:
print("Logged in successfully.")
else:
print("Account credentials do not match.")
else:
print("Account not found.")
elif login_or_signup.upper() == "SIGNUP":
username = input("Enter your username: ")
password = input("Enter your password: ")
accounts[username] = password