while 循环在电子邮件密码检查程序中不起作用 (python)
while loop not working in email password checker (python)
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)
此脚本的目的是询问用户他们的电子邮件和密码,如果不正确,则使用 while 循环它应该不断询问用户的详细信息,直到用户提供正确的电子邮件和密码。
即使我第一次输入正确 email/password 也没有用。
更正代码
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}")
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)
此脚本的目的是询问用户他们的电子邮件和密码,如果不正确,则使用 while 循环它应该不断询问用户的详细信息,直到用户提供正确的电子邮件和密码。
即使我第一次输入正确 email/password 也没有用。
更正代码
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}")