2015-05-06 116 views
2

似乎無法讓此循環工作,它保持循環回到二進制數字輸入。我希望它回到菜單選擇。對於noob問題抱歉,我是python和編程新手。整數輸入驗證Python的

import sys 
loop = 0 
menu_Select = 0 
for menu_Select in range(1,100): 
    #Display user options to the screen 
    print('*** Menu ***') 
    print('1. Convert to binary') 

    userMenu = input('What would you like to do [1,2,3,4]? ') 
    if userMenu != '1' and userMenu != '2' and userMenu != '3' and userMenu != '4': 
     print("Please enter either 1, 2, 3, or 4.") 

    elif userMenu == '4': 
     print('Goodbye.') 
     sys.exit(0) 

    elif userMenu == '1': 
     #Decimal to Binary convertion code 
     print('\n') 
     while loop < 1: 
      while True: 
       try: 
        user_Number = (int(input('Please enter number: '))) 
       except ValueError: 
        print('wrong') 
       else: 
        binary_num = [] 

        while (user_Number > 0): 
         if user_Number % 2 != 0: 
          binary_num.append(1) 
         elif user_Number % 2 == 0: 
          binary_num.append(0) 
         user_Number = user_Number // 2 
        binary_num.reverse() 
        binary_display = ''.join(str(k) for k in binary_num) 
        print('Binary number: ',binary_display) 
      loop += 1 
+2

你什麼時候期待'while True:'循環結束? – Matthias

+0

顯示二進制數後 – girthquake

+0

我不允許使用break語句 – girthquake

回答

2

使用input()實際上會將用戶鍵入的內容轉換爲int,如果可以的話。所以看看會發生什麼:

>>> input("= ") 
= 12 
12 

返回12,而不是'12'。對於輸入給我'12'我需要手動包裝在引號。

>>> input("= ") 
= '12' 
'12' 

而是使用raw_input()來讓Python讀取用戶鍵入的任何內容作爲字符串。

>>> raw_input("= ") 
= 12 
'12' 

另外,正如其他人所說的,您正在使用while循環錯誤。如果你想繼續詢問用戶輸入,直到你得到一個有效的數字,最好用相關的條件包裝少量的代碼。

即。讓循環只在沒有有效數字的情況下運行,並且只包含發生輸入的行。

 user_Number = None 
     while user_Number is None: 
      try: 
       user_Number = (int(raw_input('Please enter number: '))) 
      except ValueError: 
       print('wrong') 
     binary_num = [] 

     while (user_Number > 0): 
      if user_Number % 2 != 0: 
       binary_num.append(1) 
      elif user_Number % 2 == 0: 
       binary_num.append(0) 
       user_Number = user_Number // 2 
     binary_num.reverse() 
     binary_display = ''.join(str(k) for k in binary_num) 
     print('Binary number: ',binary_display) 
0

變化:

if userMenu != '1' and userMenu != '2' and userMenu != '3' and userMenu != '4': 

要:

if userMenu != 1 and userMenu != 2 and userMenu != 3 and userMenu != 4: 

並更新你的if語句,看看他們是廉政的,而不是字符串。這將工作在python 2.7上,不確定python 3.

+0

代碼看起來像Python 3,所以你的答案是錯誤的。如果你仔細閱讀這個問題,你會發現這不是問題。 – Matthias

+0

Gotcha。我將他的代碼放入我的2.7版本並運行它,它在第一個問題上循環,從未達到if語句。但我現在看到,他需要擺脫他的'while True'循環。 – Alex

+0

@Alex謝謝! – girthquake

1

你可以在True循環之前引入布爾變量done = False,並將此循環更改爲while not done。打印二進制數後,將done設置爲True

elif userMenu == '1': 
    #Decimal to Binary convertion code 
    print('\n') 
    done = False 
    while not done: 
     try: 
      user_Number = (int(input('Please enter number: '))) 
     except ValueError: 
      print('wrong') 
     else: 
      binary_num = [] 
      while (user_Number > 0): 
       if user_Number % 2 != 0: 
        binary_num.append(1) 
       elif user_Number % 2 == 0: 
        binary_num.append(0) 
       user_Number = user_Number // 2 
      binary_num.reverse() 
      binary_display = ''.join(str(k) for k in binary_num) 
      print('Binary number: ',binary_display) 
      done = True