2017-04-05 77 views
-2

我正在研究一個密碼檢查器,檢查字符串是否是有效的密碼。我必須檢查是否至少有八個字符,必須只包含字母和數字,最後兩個字符必須是數字。str.isdigit()似乎沒有在python中工作

password.isdigit()以外,這一切似乎工作。有時密碼出來是有效的,有時它不會。有什麼建議麼?

# Gets the users password 
password = input('Enter a string for password: ') 
# Splices the last two characters of the password 
lastTwo = password[-2:] 

# Checks the password if it is less than 8 characters 
while len(password) < 8: 
    print('The password you entered is too short.') 
    print() 
    password = input('Enter a string for password: ') 

    # Checks the password if it is composed of letters and numbers 
    while password.isalnum() == False: 
     print('Your password has special characters not allowed.') 
     print() 
     password = input('Enter a string for password: ') 

    # Checks the spice to verify they are digits 
    while lastTwo.isdigit() == False: 
     print('Your last two characters of your password must be digits.') 
     print() 
     password = input('Enter a string for password: ') 

print('Your password is valid.') 
+1

什麼是一些投入不像你期望的那樣行事? –

+0

「任何建議」 - 尋找真正的問題。在*你的*代碼和/或在你的理解。 –

+1

您應該在最後一個循環之前拼接最後兩個字符的密碼。否則它將包含先前密碼的最後2個字符 – kuro

回答

2

有與你提供的代碼問題了一把。特別是,您只能檢查後續規則while len(password) < 8。如果你給它一個長度爲10的密碼,規則從不被檢查。此外,你不更新lastTwo隨着每一個新的密碼嘗試解決這一問題將與if...elif..elif...else...包裹在一個整體while語句來代替你的幾個while聲明

的一種方法,具體如下:

# Gets the users password 
password = input('Enter a string for password: ') 

while True: 
    # Checks the password if it is less than 8 characters 
    if len(password) < 8: 
     print('The password you entered is too short.') 
    # Checks the password if it is composed of letters and numbers 
    elif not password.isalnum(): 
     print('Your password has special characters not allowed.') 
    # Checks the spice to verify they are digits 
    elif not password[:-2].isdigit(): 
     print('Your last two characters of your password must be digits.') 
    else: 
     # we only get here when all rules are True 
     break 

    print() 
    password = input('Enter a string for password: ') 

print('Your password is valid.') 

這應該按照你的意圖工作。但是,雖然我們在這,它爲什麼不告訴用戶規則他們的密碼已損壞?從UI的角度來看,它有助於讓用戶瞭解情況。

如果我們存儲旁邊的相關規則是否被滿足的信息消息,我們可以快速計算出所有這一切都被打破,像這樣的規則:

valid_password = False 

while not valid_password: 
    # Get a password 
    password = input('\nEnter a string for password: ') 
    # applies all checks 
    checks = { 
     '- end in two digits': password[-2].isdigit(), 
     '- not contain any special characters': password.isalnum(), 
     '- be over 8 characters long': len(password) > 8 
    } 
    # if all values in the dictionary are true, the password is valid. 
    if all(checks.values()): 
     valid_password = True 
    # otherwise, return the rules violated 
    else: 
     print('This password is not valid. Passwords must:\n{}'.format(
      '\n'.join([k for k, v in checks.items() if not v]))) 

print('Your password is valid.') 
+0

雖然我同意你的看法。我實際上沒有回答這個(相當可憐的)問題,是嗎? – SiHa

+0

@SiHa你絕對正確 - 編輯添加更直接的「修復」 – asongtoruin

+0

謝謝。這確實更有意義而且更加簡潔。我仍然對python和編碼這個新事物感興趣,所以我很感激這個幫助。 – Drock33

0

你永遠不會在你的while循環中更新你的lastTwo的值。因此,想象一下,如果用戶第一次輸入密碼abc123。那麼lastTwo將計算爲23

現在您的代碼會發現密碼太短,並提示用戶輸入新密碼。假設他輸入abcdefgh。這現在通過你的第一次和第二次檢查。但請注意,lastTwo仍然是23,因此您的第三張支票將錯誤地通過。

你應該這樣重新計算,只要你接受一個新的密碼,或直接檢查這樣lastTwo的價值:

while (password[-2:]).isdigit() == False:

+0

是的現在您已經提到它非常有意義我有點失望,我沒有意識到這一點。謝謝 – Drock33