2017-09-05 49 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