'''如何编写Python程序以检查用户输入的密码的有效性。 允许用户尝试3次,第4次退出程序(Google是您的朋友:))。
Validation :
•At least 1 letter between [a-z] and ta least one letter between [A-Z].
•At least 1 number between [0-9].
•At least 1 character from [$#@].
•Minimum length 6 characters.
•Maximum length 16 characters.
'''
答案 0 :(得分:0)
将这个问题分解为更简单的部分可能会很好。
len()
检查字符串的长度。'a' in some_string
检查字符串中是否存在字母或符号。re.search()
来查看字符串中是否包含任意范围的字符。 将它们全部组合在一起,您将得到如下内容:
import re
def is_valid(i):
is_between_6_16_chars = 6 <= len(i) <= 16
has_symbols = "$" in i or "#" in i or "$" in i
has_numbers = re.search('[0-9]', i) is not None
has_uppercase_letters = re.search('[a-z]', i) is not None
has_lowercase_letters = re.search('[A-Z]', i) is not None
return is_between_6_16_chars and has_symbols and has_numbers and has_uppercase_letters and has_lowercase_letters