4、 网站要求用户输入用户名和密码进行注册。编写程序以检查用户输入的密码的有效性。若不符合要求,则给出提示,提醒密码不满足哪个条件;若符合要求,则提示注册成功。
以下是检查密码的标准:
l [a-z]之间至少有1个字母
l [0-9]之间至少有1个数字
l [A-Z]之间至少有一个字母
l [$#@]中至少有1个字符
l 最短交易密码长度:6
l 交易密码的最大长度:12
def main():
# Prompt the user to enter a password
password = input("Enter a password: ")
# Check the password against the standards
has_lowercase = False
has_uppercase = False
has_digit = False
has_special = False
length = len(password)
for c in password:
if c.islower():
has_lowercase = True
elif c.isupper():
has_uppercase = True
elif c.isdigit():
has_digit = True
elif c in ['$', '#', '@']:
has_special = True
# Print a message based on the password's validity
if not has_lowercase:
print("The password must contain at least one lowercase letter.")
elif not has_uppercase:
print("The password must contain at least one uppercase letter.")
elif not has_digit:
print("The password must contain at least one digit.")
elif not has_special:
print("The password must contain at least one special character ($, #, or @).")
elif length < 6:
print("The password must be at least 6 characters long.")
elif length > 12:
print("The password must be no more than 12 characters long.")
else:
print("The password is valid.")
if __name__ == '__main__':
main()