Python 如何确保用户输入的密码与注册到该密码的用户名匹配?

Python 如何确保用户输入的密码与注册到该密码的用户名匹配?,python,python-3.x,Python,Python 3.x,我很好奇如何制作一个用户注册的示例程序(我知道如何做这一部分),但是当用户注册并且用户名被添加到一个列表或字典中时,他们不得不这么做,它不能是另一个用户的密码,因为如果我做了“if x in x:”,因为它将允许另一个用户的密码访问另一个用户。我只想让正确的数据匹配 #Example users = ["bob", "joe"] passwords = ["example1", "example2"] ex1 = input("What is your username?") if ex1

我很好奇如何制作一个用户注册的示例程序(我知道如何做这一部分),但是当用户注册并且用户名被添加到一个列表或字典中时,他们不得不这么做,它不能是另一个用户的密码,因为如果我做了“if x in x:”,因为它将允许另一个用户的密码访问另一个用户。我只想让正确的数据匹配

#Example

users = ["bob", "joe"]
passwords = ["example1", "example2"]

ex1 = input("What is your username?")
if ex1 in users:
    ex2 = input("enter your password")
if ex2 in passwords:
    print("access granted")
#With this code any password will work with any user,
#I want the registered user's password to have to be the password they registered with

您需要使用字典,这样您就可以将密码链接到用户

users = ["bob", "joe"]
passwords = ["example1", "example2"]

# d = {'bob' : 'example1', 'joe' : 'example2'}
# An example how to zip your lists into a dictionary
d = dict(zip(users, passwords))

ex1 = input("What is your username?")
if ex1 in users:
    ex2 = input("enter your password")

# How to use your dict to check if the password matches the user
if ex2 == d[ex1]:
    print("access granted")
使用字典:

users = {"bob": "example1", "joe": "example2"}
ex1 = raw_input("What is your username?: ")
if ex1 in users:
    ex2 = raw_input("enter your password: ")
if ex2 == users.get(ex1):
    print("access granted")

使用字典将用户名映射到他们的密码,
d=dict(zip(users,passwords))
,然后访问密码,比如
d['bob']
您不知道密码,而只是一个密码。获取匹配用户的密码,然后进行检查。您是否阅读了注释和其他答案?你读过Python3标签了吗?你知道
.keys()
调用是多余的吗?