我正在研究一個密碼檢查器,檢查字符串是否是有效的密碼。我必須檢查是否至少有八個字符,必須只包含字母和數字,最後兩個字符必須是數字。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.')
什麼是一些投入不像你期望的那樣行事? –
「任何建議」 - 尋找真正的問題。在*你的*代碼和/或在你的理解。 –
您應該在最後一個循環之前拼接最後兩個字符的密碼。否則它將包含先前密碼的最後2個字符 – kuro