これは私の現在のコードです:
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
「このユーザー名には少なくとも小文字が含まれています」などのステートメントを 1 回だけ表示するようにコードを変更するにはどうすればよいですか。本当にありがとうございます