我的程序运行正常,但不会用密码返回所有错误,只会返回一个问题。
示例: 密码为ASD123(问题少于10个字符,没有符号)。程序仅返回“密码少于10个字符”
passwordisokk = True
def passwordisOK():
while True:
passwordisokk = input("Please enter a password so we can validate:")
if len(passwordisokk) < 10:
print(" Your password should be 10 characters,please enter more characters")
passwordisokk = False
print(passwordisokk)
elif re.search("[0-9]",passwordisokk) is None:
print("Your password needs a number,please enter one")
passwordisokk = False
print(passwordisokk)
elif re.search("[A-Z]",passwordisokk) is None:
print(" Your password needs a capital letter,please enter one")
passwordisokk = False
print(passwordisokk)
elif re.search("[$,#,%,&,*]",passwordisokk) is None:
print(" You password needs one of these symbols:$,#,%,&,*. Please enter one")
passwordisokk = False
print(passwordisokk)
elif re.search("[ ]",passwordisokk):
passwordisokk = False
print("No spaces when entering your password please")
print(passwordisokk)
else:
passwordisokk = True
print(passwordisokk)
break
passwordisOK()
答案 0 :(得分:3)
只需将elif
和else
更改为if
语句。
import re
passwordisokk = True
def checkPasswordisOK():
while True:
password = input("Please enter a password so we can validate:")
if len(password) < 10:
print(" Your password should be 10 characters,please enter more characters")
passwordisokk = False
print(passwordisokk)
if re.search("[0-9]",password) is None:
print("Your password needs a number,please enter one")
passwordisokk = False
print(passwordisokk)
if re.search("[A-Z]",password) is None:
print(" Your password needs a capital letter,please enter one")
passwordisokk = False
print(passwordisokk)
if re.search("[$,#,%,&,*]",password) is None:
print(" You password needs one of these symbols:$,#,%,&,*. Please enter one")
passwordisokk = False
print(passwordisokk)
if re.search("[ ]",password):
passwordisokk = False
print("No spaces when entering your password please")
print(passwordisokk)
if not passwordisokk:
passwordisokk = True
print(passwordisokk)
break
checkPasswordisOK()