这是我当前的代码:
while True:
try:
username = input("Username: ")
if len(username) < 8:
print ("Sorry, the username must be at least 8 characters long.")
if username.isalnum() == False:
print ("Sorry, your name can only contain alpha numeric characters")
numupper = 0
for c in username:
if c.isupper() == True:
numupper += 1
if numupper > 0:
print ("You have at least 1 uppercase in this username.")
else:
print ("You have no uppercase in this username.")
numlower = 0
for d in username:
if d.islower() == True:
numlower +=1
if numlower > 0:
print ("You have at least 1 lowercase in this username.")
else:
print ("You have no lowercase in this username.")
numdigit = 0
for e in username:
if e.isdigit() == True:
numdigit += 1
if numdigit > 0:
print ("You have at least one digit in this username.")
else:
print("You have no digits in this username.")
else:
print("Please try again")
continue
except:
print ("Sorry, not valid. Try again.")
else:
print ("Thank you for your input")
break
当我使用我的主程序运行这个定义时:
import uservalidation
username = input("Username: ")
result, reason = uservalidation.valid_username(username)
if not(result):
print (reason)
我明白了(取决于我输入了多少个字母):
Username: craig
Sorry, the username must be at least 8 characters long.
You have no uppercase in this username.
You have no uppercase in this username.
You have no uppercase in this username.
You have no uppercase in this username.
You have no uppercase in this username.
You have at least 1 lowercase in this username.
You have at least 1 lowercase in this username.
You have at least 1 lowercase in this username.
You have at least 1 lowercase in this username.
You have at least 1 lowercase in this username.
You have no digits in this username.
You have no digits in this username.
You have no digits in this username.
You have no digits in this username.
You have no digits in this username.
Please try again
如何更改我的代码,使其仅显示诸如“您在此用户名中至少有小写字母”之类的语句一次。非常感谢