2012-12-05 117 views
0

這是我當前的代碼:多個輸出要限制

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至少小寫用戶名「只有一次。非常感謝你

回答

2

你壓痕熄滅:

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.") 

因此,對於每個角色,要打印錯誤文本。你想要做的,而不是什麼是移動的支票上的數字外循環

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.") 

所以,你已經算的大寫字符(中環)之後,你評估結果並打印錯誤文本一次。

這同樣適用於小寫字母和數字檢查。

Btw。你可以爲不同的for循環使用相同的變量。所以你不需要使用cde,你可以繼續使用c。另外,你應該考慮合併你的循環。在他們三個人,你遍歷用戶名中的字符,這樣你就可以在同一時間做的一切:

numupper, numlower, numdigit = 0, 0, 0 

for c in username: 
    if c.isupper(): 
     numupper += 1 
    if c.islower(): 
     numlower +=1 
    if e.isdigit(): 
     numdigit += 1 

# and now check numupper, numlower and numdigit 

此外,你可以看到,我移除了,如果條件== True。通常情況下,最好不要這樣做,因爲if已經檢查表達式是否等於true。所以它基本上是多餘的。

最後,你可以做一些不同的檢查。例如,您可以通過將字符串轉換爲相同的字符並將其與原始文本進行比較來檢查小寫字母和大寫字符。所以username.lower() == username意味着它裏面沒有大寫字母;類似地,username.upper() == username表示不存在小寫字符。

+0

用戶名:-d用戶名:-2345對不起,用戶名至少需要8個字符。對不起,您的名字只能包含字母數字字符您在此用戶名中沒有大寫字母。這個用戶名中沒有小寫字母。此用戶名中至少有一位數字。感謝您的輸入<----感謝您現在的工作,但是您是否知道爲什麼該程序不會在用戶名第一次被提問時起作用,它在我第二次輸入類似「craig」之類的東西時起作用該程序要求輸入用戶名 – user1762229

1

「字符計數檢查」行,如if numupper > 0:,位於for-each-character-in-username循環中。把它們放在循環之後,而不是放在它之內,以達到預期的效果。

例如,

for c in username: 
     if c.isupper() == True: 
      numupper += 1 
# Now we're outside the loop, because we've de-dented the lines below 
if numupper > 0: 
    print ("You have at least 1 uppercase in this username.") 
else: 
    print ("You have no uppercase in this username.") 
+0

用戶名:-d 用戶名:-2345 對不起,用戶名至少需要8個字符。 對不起,您的名字只能包含字母數字字符 此用戶名中沒有大寫字母。 此用戶名中沒有小寫字母。 此用戶名中至少有一位數字。 感謝您的輸入<----感謝您現在的工作,但是您是否知道爲什麼該程序不會在用戶名首次被問到時就起飛,它在我進入類似「craig」該程序要求輸入用戶名 – user1762229

0

如果你不需要大寫/小寫/數字的計數,你可以跳出for循環。就像這樣:

for d in username: 
     if d.islower(): 
      print ("You have at least 1 lowercase in this username.") 
      break 
    else: 
     # This executes only if the for loop doesn't end because of a break 
     print ("You have no lowercase in this username.") 
1

使這一變化:

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.") 

你想在循環之外print語句。

我可能會寫這樣的:

hasUpper = any([c.isupper() for c in username]) 
if hasUpper: 
    print ("You have at least 1 uppercase in this username.") 
else: 
    print ("You have no uppercase in this username.") 
0

可以大大簡化你的代碼,如果你刪除try...except塊,並與map()小號取代你的循環:

import string 

while True: 
    errors = [] 
    username = input("Username: ") 

    if len(username) < 8: 
     errors.append("The username must be at least 8 characters long.") 

    if len(map(string.isupper, username)) == 0: 
     errors.append("You have no uppercase in this username.") 

    if len(map(string.isupper, username)) == 0: 
     errors.append("You have no lowercase in this username.") 

    if len(map(string.isdigit, username)) == 0: 
     errors.append("You have no digits in this username.") 

    if not username.isalnum(): 
     errors.append("Your name can only contain alpha numeric characters.") 

    if not errors: 
     print("Thank you for your input") 
     break 
    else: 
     for error in errors: 
      print(error) 

     print("Sorry, not valid. Try again.")