简体   繁体   中英

How to just use while() to print a message in case of an error?

I am working on an exercise and I tried using while() to print a message when the input is other than present in the dictionary.

*Create a static dictionary with a number of users and with the following values: First name Last name Email address Password Ask the user for: 5. Email address 6. Password

Loop (for()) through the dictionary and if (if()) the user is found print the following: 7. Hello, first name last name you have successfully logged in 8. Notify the user if the password and email address are wrong 9. Additional challenge: if you want the program to keep asking for a username and password when the combination is wrong, you will need a while() loop.*

# key and value
user_details = {
    1: {"first name":"carla","last name":"smith","email address":"carla11@gmail.com","password":"welcome1carla"},
    2: {"first name":"sandra","last name":"silva","email address":"sandra26@live.com","password":"welcome226sandra"},
    3: {"first name":"val","last name":"santos","email address":"val55@outlook.com","password":"332valwelcome"},
    4: {"first name":"suely","last name":"costa","email address":"suely41@yahoo.com","password":"suely8923"},
    5: {"first name":"sammy","last name":"vida","email address":"sammy32@hotmail.com","password":"vida323welcome"},
}

# ask user email address
user_email = raw_input("What is your email? ")
user_password = raw_input("What is your password? ")


#loop
for id,info in user_details.items():
    first_name = info["first name"]
    last_name = info["last name"]
    email = info["email address"]
    password_code = info["password"]
    
    if user_email + user_password == email + password_code:
        print("Hello, "+first_name+" "+last_name+ ", you have successfully logged in")
    while user_email + user_password != email + password_code:
        print("ERROR: the password and email address are wrong")
        print("Please try again")

Now the while print message keeps running non-stop. How can I print it just once, as requested in the exercise?

Thanks a lot!

You are not using the while to the right position. You are actually printing that the credentials are invalid indefinitly at the first invalid credential you find. Since you're stuck in the while loop you don't event look to the other credentials.

The way of doing it is :

  1. Is there a successfull loggin attempt ? (No for the first iteration since there isn't a loggin attempt yet)
  2. Supply credential
  3. Test the credentials against the possible values
# key and value
user_details = {
    1: {"first name":"carla","last name":"smith","email address":"carla11@gmail.com","password":"welcome1carla"},
    2: {"first name":"sandra","last name":"silva","email address":"sandra26@live.com","password":"welcome226sandra"},
    3: {"first name":"val","last name":"santos","email address":"val55@outlook.com","password":"332valwelcome"},
    4: {"first name":"suely","last name":"costa","email address":"suely41@yahoo.com","password":"suely8923"},
    5: {"first name":"sammy","last name":"vida","email address":"sammy32@hotmail.com","password":"vida323welcome"},
}

logged_in = False

#1. Is there a successfull loggin attempt ?
while not logged_in:
    
    #2. Supply credential
    user_email = raw_input("What is your email? ")
    user_password = raw_input("What is your password? ")


    #3. Test the credentials against the possible values 
    for id,info in user_details.items():
        first_name = info["first name"]
        last_name = info["last name"]
        email = info["email address"]
        password_code = info["password"]
    
        if user_email + user_password == email + password_code:
            print("Hello, "+first_name+" "+last_name+ ", you have successfully logged in")
            logged_in = True
        else:
            print("ERROR: the password and email address are wrong")
            print("Please try again")

I think it's enough to add asking for input in the while loop after the prints so the values for password and email change and the possibility appears for them to match:

# ask user email address
user_email = raw_input("What is your email? ")
user_password = raw_input("What is your password? ")


#loop
for id,info in user_details.items():
    first_name = info["first name"]
    last_name = info["last name"]
    email = info["email address"]
    password_code = info["password"]
    
    if user_email + user_password == email + password_code:
        print("Hello, "+first_name+" "+last_name+ ", you have successfully logged in")
    while user_email + user_password != email + password_code:
        print("ERROR: the password and email address are wrong")
        print("Please try again")
        user_email = raw_input("What is your email? ")
        user_password = raw_input("What is your password? ")

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.

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