2017-09-05 52 views
0

我查找了类似的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评论)建议实现同样的事情

+0

那么,你希望发生时,'dict'不包含的关键是什么?这是错误的来源... –

+0

你应该尝试使用'userinfo_dict.get(username,'not found')'来解决这个问题 – PRMoureu

回答

0

你可以检查的替代或更有效的方法,无论是用户名和密码都在字典和它们的键 - 值对,使用下列:

#Check that both username and password in dictionary 
if username in userinfo_dict.keys() and password in userinfo_dict.values(): 
    if userinfo_dict[username] == password: 
     accessgranted = True 
else: 
    print('Access Denied') #Print if either username or password not in dictionary 

keys()方法在词典而返回键的列表方法返回字典中值的列表。

+3

这是非常低效的。最好使用'.get'方法,或者只使用'try-except'。请注意,在检查密码是否在'userinfo_dict.values()'中没有真正的意义。而在Python2中,这使得你的算法是线性的而不是恒定时间的。 –

+0

我需要它在Python 3. juanpa - 如果你有一个更有效的方法,你能否请它张贴它作为答案。 Thankyou亨利,这工作....所以只是等待任何事情或将接受它。 – MissComputing

+1

'如果userinfo_dict和userinfo_dict中的用户名[username] == password:...' – ekhumoro

0

我同意以上所述。问题在于获取不存在的密钥。试试看之一:

另外,代码需要一个比特重构例如为:

  • 通过检查为False的 '是' A;
  • accessgranted ==真的应该是赋值,而不是比较;
  • 循环的逻辑也必须改变。

见下文:

username = input("Enter username:") 
password = input("Enter password:") 

access_granted = False 
while access_granted is False: 
    if userinfo_dict.get(username) == password: 
     # loggedin() 
     access_granted = True 
    else: 
     print("Sorry, wrong credentials") 
     break 
# main() 
+2

呃,这种惯用的方法是简单地检查'while not access_granted:'。 –

0

几乎总是使用dictionary.get(key)代替dictionary[key]。前者对于不存在键的情况是安全的(比如在这种情况下),而后者会抛出错误。

if userinfo_dict.get(username) == password: # returns None is key doesn't exist 
    loggedin() 
    accessGranted=True 
else: 
    break 
+0

其实 - 我试着如果用户名userinfo_dict.keys()和userinfo_dict [用户名] ==密码:它的工作。当您使用正确的用户名和密码进行尝试时,使用您的建议答案会出现错误。不会downvote - 但你可能想检查 – MissComputing

+0

你可以发布你运行的错误/例子吗?似乎无法在本地重新创建 – James

0

什么错误是告诉你的是,你输入用户名值“EE”,但是没有指定的用户“EE”(即无键值对用钥匙“ ee“)。这是尝试获取不存在密钥的值时的预期结果。

正确的蟒蛇成语用于测试的一个关键的存在:

if user_name in userinfo_dict: 
2

的问题,因为很多人已经指出的那样,你正试图获得一个不存在的键的值。

一个简单的解决方法是仅当username是现有密钥时才检查userinfo_dict[username] == password

username = input("Enter username:") 
password = input("Enter password:") 

access_granted = False 
while access_granted is False: 
    if username in userinfo_dict.keys() and userinfo_dict[username] == password: 
     loggedin() 
     access_granted = True 
    else: 
     break 
print("Sorry, wrong credentials") 
main() 

编辑:access_granted标志是没用的,你可以做到这:

username = input("Enter username:") 
password = input("Enter password:") 

if username in userinfo_dict.keys() and userinfo_dict[username] == password: 
    loggedin() 
else: 
    print("Sorry, wrong credentials") 
+0

真的不需要这里的国旗吗? ... – MissComputing

+1

@MissComputing不是。我把它留在那里,因为我不知道你在程序的其余部分如何使用它。 – Marco

+1

@MissComputing你可以保留标志并在while循环中放置用户名和密码输入,如果你希望用户能够多次尝试登录,可以在else块中打印语句。 – Henry