gpt4 book ai didi

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

转载 作者:太空狗 更新时间:2023-10-30 02:39:43 26 4
gpt4 key购买 nike

我很好奇如何制作一个用户注册的示例程序(我知道如何做这部分)但是当用户注册并且用户名被添加到列表或字典中时,他们必须这样做另一个用户的密码,因为如果我执行“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")

关于python - 如何确保用户输入的密码与注册到该密码的用户名匹配?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43681377/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com