简体   繁体   中英

How do I use a if statement to access values in a dictionary based on the corresponding key?

I thought I could use a dictionary to store key/value pairs for user's names and passwords. How do I use a if statement to access values in a dictionary based on the corresponding key?

users_data_base = dict(Mike = "Speedy203", Ken = "Justify30", Marvin = "Pop_Jay")
while True:
    current_user = input("Enter User Name: ")
    if current_user not  in users_data_base:
        print("User name not valid")
    else:
        
        print("Welcome " + current_user)
        break
while True:
    pw_entry = input("Please enter password: ")
    if pw_entry is ([current_user]):
        print("Incorrect password entry")
        print("Try again")
    else:
        print("Successful entry")
        break

You are missing the dictionary's name in the second if :

if pw_entry == user_data_base[current_user]:
    # Here ----^

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