【发布时间】:2017-08-31 10:53:41
【问题描述】:
我正在开发一个密码检查器,用于检查字符串是否为有效密码。我必须检查是否至少有八个字符,必须只包含字母和数字,最后两个字符必须是数字。
到目前为止,除了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 个字符
-
第一次输入密码后,您永远不会更新
lastTwo- 您需要将其带入while循环