简体   繁体   English

如何使用 if 语句根据相应的键访问字典中的值?

[英]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?如何使用 if 语句根据相应的键访问字典中的值?

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您在第二个中缺少字典的名称:

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

暂无
暂无

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

相关问题 如何根据 Pandas 数据框中的元数据字典创建相应的值? - How do I create corresponding values based on the metadata dictionary in pandas data frame? 如何快速访问与特定第二级键相对应的所有值,而不管Python字典中的第一级键如何? - How to quickly access all values corresponding to a specific second level key regardless of first level key in a Python dictionary? 在给定 if 语句的情况下,如何使用循环语句更改字典中的特定值? - How do I use a loop statement to change specific values in a dictionary given an if statement? 如何删除字典中的键值? - How do I remove key values in a dictionary? 我如何 plot 一个 Python 字典键与其对应的字典值? - How do I plot a Python dictionary key against its corresponding dictionary value? 如何在条件语句中使用嵌套在列表中的字典键值对的值? - How do I use the value of a key-value pair of a dictionary nested inside a list in a conditional statement? 将值累积到字典中的相应键? - accumulating values to corresponding key in a dictionary? 选择与键对应的字典值 - selecting dictionary values corresponding to a key 如何将键转换为值并将索引作为字典中的键? - How do I shift key into values and make index as key in dictionary? Python:如何使用字典调用方法(字典中的值)以根据用户输入(字典中的键)在不同的函数中运行? - Python: How to use a dictionary to call methods (values in dictionary) to run based on user input (key in dictionary) in a different function?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM