python初学者,验证密码是否符合要求问题,在写验证这部分时不会了
"""
Please enter a valid password
Your password must be between 5 and 15 characters, and contain:
1 or more uppercase characters
1 or more lowercase characters
1 or more numbers and 1 or more special characters: !@#$%^&*()_-=+`~,./'[]<>?{}|\
"""
MIN_LENGTH = 5
MAX_LENGTH = 15
SPECIAL_CHARS_REQUIRED = False
SPECIAL_CHARACTERS = "!@#$%^&*()_-=+`~,./'[]<>?{}|\\"
def main():
"""Program to get and check a user's password."""
print("Please enter a valid password")
print("Your password must be between", MIN_LENGTH, "and", MAX_LENGTH,
"characters, and contain:")
print("\t1 or more uppercase characters")
print("\t1 or more lowercase characters")
print("\t1 or more numbers")
if SPECIAL_CHARS_REQUIRED:
print("\tand 1 or more special characters: ", SPECIAL_CHARACTERS)
password = input("> ")
while not is_valid_password(password):
print("Invalid password!")
password = input("> ")
print("Your {}-character password is valid: {}".format(len(password),
password))
def is_valid_password(password):
"""Determine if the provided password is valid."""
# TODO: if length is wrong, return False
count_lower = 0
count_upper = 0
count_digit = 0
count_special = 0
for char in password:
# TODO: count each kind of character (use str methods like isdigit)
pass
# TODO: if any of the 'normal' counts are zero, return False
# TODO: if special characters are required, then check the count of those
# and return False if it's zero
# if we get here (without returning False), then the password must be valid
return True
main()
上面在验证密码函数部分,原题已经给了注释,我这部分卡住了,写不出来,麻烦看一下,给一个完整补充好的代码,如果能有详细一点的注释就更好啦,谢谢