users = {
1: {"first_name": "Tom", "last_name": "Smith", "email": "tom@email.com", "password": "123123"},
2: {"first_name": "Simon", "last_name": "Stevens", "email": "simon@email.com", "password": "password1234"},
3: {"first_name": "Laura", "last_name": "Laurens", "email": "laura@email.com", "password": "laura1234"},
4: {"first_name": "Gabriel", "last_name": "Mitchel", "email": "gab@email.com", "password": "laura12534"}
}
email_input = input("What's your email?")
password_input = input("What's your password?")
for key, info in users.items():
name = info["first_name"]
last_name = info["last_name"]
email = info["email"]
password = info["password"]
while email_input != email and password_input != password:
print("wrong password/email")
email_input = input("What's your email?")
password_input = input("What's your password?")
print("You're logged in",name)
The goal of this script is to ask the user their email and password and if it's incorrect then using a while loop it should keep asking the user for their details until the user provides the correct email and password.
It doesn't work even when I enter correct email/password the first time.
Code with Corrections
users = {
1: {"first_name": "Tom", "last_name": "Smith", "email": "tom@email.com", "password": "123123"},
2: {"first_name": "Simon", "last_name": "Stevens", "email": "simon@email.com", "password": "password1234"},
3: {"first_name": "Laura", "last_name": "Laurens", "email": "laura@email.com", "password": "laura1234"},
4: {"first_name": "Gabriel", "last_name": "Mitchel", "email": "gab@email.com", "password": "laura12534"}
}
found = False
while not found: # loop until match found
# Get login credentials
email_input = input("What's your email?")
password_input = input("What's your password?")
# check each dictionary for email and password
for key, info in users.items():
if email_input == info["email"] and password_input == info["password"]:
# Found match, so get name
name = info["first_name"]
last_name = info["last_name"]
found = True # set flag to terminate while loop
break # found match
else:
print("wrong password/email") # Break not encountered in for loop, so did not find match
print(f"You're logged in {name} {last_name}")
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.