2017-02-20 90 views
1

我正在學習Python,我正在嘗試爲一個更大的項目製作一個簡單的登錄程序。相同的字符串不匹配?

此代碼:

def _login():#Login Function 
     username = input("Username:\n") 
     password = input("Please enter your password:\n") 
     with open("logins", "r") as log: 
      for line in log: 
      compound = str(username + password) 
      a = line 
      b = str(re.sub(':', '', a)) 
      if a == compound: 
       print("Success") 
_login() searches through a text file where **user:pass** are on individual lines displayed as such in the bold text. 

這就需要你輸入用戶名和密碼的字符串,concats並做同樣的與用戶名/傳文件,免除了「」,這是過濾通過正則表達式。

問題:

將合併的字符串,所述用戶輸入(VAR 化合物),並從文本文件中的過濾字符串(VAR 一個)不返回真時即使它們都等於用戶「UserPass」

我該如何解決這個問題?

Cheers

+0

您是否嘗試過打印每個字符串的'repr'?我想你會發現,就像口譯員告訴你的那樣,他們並不平等。 – TigerhawkT3

+0

啊廢話,我沒有輸入()...:P – Kris

+0

我非常感謝你哈哈!文件中的密碼包含「\ n」的實例! – Kris

回答

0

看起來你已經找到了答案。 但我想說,你最好使用'b'來比較而不是'a'。 因爲某種原因你應該刪除':'。 :)

+0

.........該死的它是repl.it ... Chromebook和Betas!使可讀性差。謝謝大聲笑 – Kris