str.isdigit() 似乎在 python 中不起作用
str.isdigit() doesn't seem to be working in 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.')
您永远不会在 while 循环中更新 lastTwo
的值。想象一下,如果用户首先输入密码 abc123
。那么 lastTwo
将被计算为 23
.
现在您的代码会发现密码太短并提示用户输入新密码。假设他输入abcdefgh
。这现在通过了您的第一次和第二次检查。但是请注意 lastTwo
仍然是 23
,因此您的第三次检查将错误地通过。
因此,每当您接受新密码或直接检查时,您应该重新计算 lastTwo 的值:
while (password[-2:]).isdigit() == False:
您提供的代码存在一些问题。特别是,您只检查后续规则while len(password) < 8
。如果给它一个长度为 10 的密码,则永远不会检查规则。此外,您不会使用每个尝试的新密码更新 lastTwo
解决此问题的一种方法是将几个 while
语句替换为包含在整个 while
语句中的 if...elif..elif...else...
,如下所示:
# 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.')
这应该会如您所愿。但是,当我们这样做时,为什么不告诉用户 every 规则他们的密码已被破解?从 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.')
我正在开发一个密码检查器,用于检查字符串是否为有效密码。我必须检查是否至少有八个字符,必须只包含字母和数字,最后两个字符必须是数字。
到目前为止,除了 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.')
您永远不会在 while 循环中更新 lastTwo
的值。想象一下,如果用户首先输入密码 abc123
。那么 lastTwo
将被计算为 23
.
现在您的代码会发现密码太短并提示用户输入新密码。假设他输入abcdefgh
。这现在通过了您的第一次和第二次检查。但是请注意 lastTwo
仍然是 23
,因此您的第三次检查将错误地通过。
因此,每当您接受新密码或直接检查时,您应该重新计算 lastTwo 的值:
while (password[-2:]).isdigit() == False:
您提供的代码存在一些问题。特别是,您只检查后续规则while len(password) < 8
。如果给它一个长度为 10 的密码,则永远不会检查规则。此外,您不会使用每个尝试的新密码更新 lastTwo
解决此问题的一种方法是将几个 while
语句替换为包含在整个 while
语句中的 if...elif..elif...else...
,如下所示:
# 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.')
这应该会如您所愿。但是,当我们这样做时,为什么不告诉用户 every 规则他们的密码已被破解?从 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.')