gpt4 book ai didi

python - 列表中的字典

转载 作者:太空宇宙 更新时间:2023-11-04 09:32:59 25 4
gpt4 key购买 nike

假设我有这个列表:

a=[ {"user":"John","password":"123"} , {"user":"Mike","password":"12345"} ,{"user":"Peter","password":"qwerty"}]

user=input("insert your username")
password=input("insert your password")

现在我想知道输入的用户名和密码是否在前面的列表中,我该如何解决这个问题?

如果我现在想要有 3 种情况:一种是用户和密码匹配,第二种是用户名正确但密码不正确,最后一种是用户名不存在。

if {'user':user,'password':password} in a:
print("okay")
else:
if {'user':user} in a and {'password':password} not in a:
print("user okay, but incorrect pass")
else:
print("No username")

这种类型的代码行不通,对吧?那么我该如何解决第二步(在第一步之后)?

最佳答案

使用:

if {'user':user,'password':password} in a:
# it is already in there
else:
# it isn't in there

编辑

使用:

if {'user':user,'password':password} in a:
# it is already in there
elif any(user == i['user'] and password != i['password'] for i in a):
# user name is correct, but password isn't
else:
# it isn't in there

关于python - 列表中的字典,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54976229/

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