我正在开发一个密码检查器,用于检查字符串是否为有效密码。我必须检查是否至少有八个字符,必须只包含字母和数字,最后两个字符必须是数字。
到目前为止,这一切似乎都有效,除了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.')