简体   繁体   English

while 循环在 email 密码检查器(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)

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.该脚本的目的是询问用户他们的 email 和密码,如果不正确,则使用 while 循环它应该不断询问用户的详细信息,直到用户提供正确的 email 和密码。

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}")

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM