我查找了类似的stackoverflow问题,但没有一个是非常像这样的。 很简单,我有下面的代码,试图查找字典的用户名和相应的密码。如果它们匹配,则访问授权并转到登录功能,否则访问被拒绝。Python中的关键错误的字典搜索结果
已经设置了类似下面,它完美的作品时的凭据是正确的,但让他们错了,导致关键错误:
代码
username=input("Enter username:")
password=input("Enter password:")
accessgranted=False
while accessgranted==False:
if userinfo_dict[username]==password:
loggedin()
accessgranted==True
else:
break
print("Sorry, wrong credentials")
main()
错误
if userinfo_dict[username]==password:
KeyError: 'ee'
该文件很简单:
user1,pass1
user2,pass2
可能有人请 一)正确和错误 b评论)建议实现同样的事情
那么,你希望发生时,'dict'不包含的关键是什么?这是错误的来源... –
你应该尝试使用'userinfo_dict.get(username,'not found')'来解决这个问题 – PRMoureu