我刚学习python几个月,我正在尝试编写一个程序来帮助测试密码的特征。我已经很接近我需要的东西了,但是似乎有一行被跳过了,我不知道为什么...这是代码:
def main():
print("Create a password. Password must follow these rules:")
print(" - password must be at least 8 characters long")
print(" - password must have one uppercase AND lowercase letter")
print(" - password must have at least one digit")
isValidPassword()
def isValidPassword():
password = []
password2 = []
print()
print("Enter password:", end="")
pass1 = input("")
print("Re-enter password:", end="")
pass2 = input("")
password.append(pass1)
password2.append(pass2)
if password == password2 and len(password) >= 8 and password.isupper() == False and password.islower() == False and password.isalpha() == False and password.isdigit() == False:
print("Password will work.")
else:
print("Password will not work. Try again.")
isValidPassword()
main()
运行代码时,即使我输入了满足所有要求的密码,if语句下的打印语句(“ Password will work。”)也不会打印。我已经在def isValidPassword()函数之外的另一个文件中运行了if语句,它似乎可以正常工作。
有人对它为什么行不通提供任何见解吗??
答案 0 :(得分:2)
我认为主要问题在于此处的比较:password == password2
,因为您正在测试两个list
对象是否相等。您应该做的是将输入存储为字符串,并测试字符串是否相等。
此代码应可以正常工作:
def isValidPassword():
print("Enter password:", end="")
password = input("")
print("Re-enter password:", end="")
password2 = input("")
if password == password2 and len(password) >= 8 and not password.isupper() and not password.islower() and not password.isalpha() and not password.isdigit():
print("Password will work.")
else:
print("Password will not work. Try again.")
isValidPassword()