這是我當前的代碼:多個輸出要限制
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
我怎樣才能改變我的代碼,以便它只會顯示語句,如「你有THI至少小寫用戶名「只有一次。非常感謝你
用戶名:-d用戶名:-2345對不起,用戶名至少需要8個字符。對不起,您的名字只能包含字母數字字符您在此用戶名中沒有大寫字母。這個用戶名中沒有小寫字母。此用戶名中至少有一位數字。感謝您的輸入<----感謝您現在的工作,但是您是否知道爲什麼該程序不會在用戶名第一次被提問時起作用,它在我第二次輸入類似「craig」之類的東西時起作用該程序要求輸入用戶名 – user1762229