我的代码应该测试用户输入的密码是否有效。密码有效与否有一些要求:
密码不能包含单词'password',不能以数字123结尾,必须至少包含8个字符和至少2位数字,并且只能包含字母和数字。我在第13行开始遇到错误,在那我设置了要求输入的密码不能包含单词'password'的要求。它说代码无法到达。我究竟做错了什么?
password = str(input("Enter password for verification: "))
def get_digits(password):
return [i for i in password if i.isdigit()]
numbers = ''.join(get_digits(password))
if len(password) < 8:
print(password, "is an invalid password. Password must have at least 8 characters.")
elif len(numbers) < 2:
print(password, "is an invalid password. Password must contain at least 2 digits.")
elif (len(password) == numbers, "password", numbers):
print(password, "is an invalid password. Password must not contain the word 'password'.")
elif (len(password) == password, "123"):
print(password, "is an invalid password. Password must not end with '123'.")
else:
print(password, "is a valid password")
此条件将始终为真:
elif (len(password) == numbers, "password", numbers):
因为您正在测试非空Tuple
的“真实性”,它将类似于(False, "password", 123
)。
我认为您想要:
elif("password" in password):
为了测试字符串"password"
是否在变量password
中。