我正在尝试将两种形式的验证结合起来检查字符串中的字符序列。苦苦挣扎着添加第二个,不知道我哪里出错了,你能帮忙吗?
def passwordnumbercheck(password):
numberCheck = [];
for i in password:
numberCheck.append(i.isnumeric());
if numberCheck == [False, False, True, False]:
return True;
print ("this is a valid password")
else:
return False;
print ("this is invalid")
def passwordlettercheck(password):
letterCheck = [];
for i in password:
letterCheck.append(i.isalpha());
if letterCheck == [True, True, False, True]:
return True;
passwordnumbercheck (password)
else:
return False;
print("this is not a valid password")
password = input("enter a valid password, it must be a letter, a letter, a number and a letter ")
passwordlettercheck (password)
passwordnumbercheck (password)
答案 0 :(得分:0)
您可以使用re模块。
>>> password = input("enter a valid password, it must be a letter, a letter, a number and a letter ")
>>> password = input("enter a valid password, it must be a letter, a letter, a number and a letter :\n")
enter a valid password, it must be a letter, a letter, a number and a letter :
ll7l
>>> def check(passw):
if re.match(r'^[A-Za-z][A-Za-z]\d[A-Za-z]$', passw):
print('valid password')
else:
print('Invalid password')
>>> check(password)
valid password
>>> password = input("enter a valid password, it must be a letter, a letter, a number and a letter :\n")
enter a valid password, it must be a letter, a letter, a number and a letter :
7u6y
>>> check(password)
Invalid password