我正在构建一个程序,该程序将检查用户的密码是小于 6 个字符还是大于 10 个字符,并继续检查它是否强。如果不超过 6 个字符或少于字符,它将继续要求用户输入一个少于 10 个字符和超过 6 个字符的密码。尝试使用 while 循环但没有奏效。提前致谢。
if password.isalpha(): # If python detects the input is all letters, it will-
message = "password weak – contains only letters" # Choose this message and present it on prompt from line 35
elif password.isnumeric(): # If python detects the input is not all alphabetical letters, it proceeds to this method-
message = "password weak – contains only numbers" # and chooses this message and presents it on prompt from line 35
else:
message = "password strong" # If the password input is a mixture of letters & numbers, it will print this message-
print(message) # and will be prompted to show for this line of code.
答案 0 :(得分:1)
试试这个逻辑。 Flag = 1 表示密码强。
MIN_PASSWORD_LENGTH = 6
MAX_PASSWORD_LENGTH = 10
password = input("Please enter your password: ") # User will input their password here, proceeding to the if structure
flag=0
while flag==0:
if len(password) < MIN_PASSWORD_LENGTH:
password = input("Please enter your password again:(Min Length=6) ")
continue
if len(password) > MAX_PASSWORD_LENGTH:
password = input("Please enter your password again:(Max Length=10) ")
continue
else:
flag=1
答案 1 :(得分:0)
试试这个:
while len(str(password)) < MIN_PASSWORD_LENGTH and len(str(password)) > MAX_PASSWORD_LENGTH:
if password.isalpha():
message = "password weak – contains only letters"
elif password.isnumeric():
message = "password weak – contains only numbers"
else:
message = "password strong"
break
print(message)
答案 2 :(得分:0)
MIN_PASSWORD_LENGTH = 6
MAX_PASSWORD_LENGTH = 10
while True:
password = input("Please enter your password: ")
if len(password) < MIN_PASSWORD_LENGTH or len(password) > MAX_PASSWORD_LENGTH:
print("Password should have more than 6 but less 10 characters")
continue
elif password.isalpha():
print ("password weak – contains only letters")
continue
elif password.isnumeric():
print ("password weak – contains only numbers")
continue
else:
print("password strong")
break
答案 3 :(得分:0)
MIN_PASSWORD_LENGTH = 6
MAX_PASSWORD_LENGTH = 10
def password_func():
password = input("Please enter your password: ")
if len(password) < MIN_PASSWORD_LENGTH or len(password) > MAX_PASSWORD_LENGTH:
print("Password should have more than 6 but less 10 characters")
return password_func()
elif password.isalpha():
print ("password weak – contains only letters")
return password_func()
elif password.isnumeric():
print ("password weak – contains only numbers")
return password_func()
else:
print("password strong")
return "string"
password_func()
或者简单地while循环
MIN_PASSWORD_LENGTH = 6
MAX_PASSWORD_LENGTH = 10
while True:
password = input("Please enter your password: ")
if len(password) < MIN_PASSWORD_LENGTH or len(password) > MAX_PASSWORD_LENGTH:
print("Password should have more than 6 but less 10 characters")
elif password.isalpha():
print ("password weak – contains only letters")
elif password.isnumeric():
print ("password weak – contains only numbers")
else:
print("password strong")
break
两者都给我输出如下
Please enter your password: tester
password weak – contains only letters
Please enter your password: test
Password should have more than 6 but less 10 characters
Please enter your password: tester1
password strong