2016-11-09 70 views
0

我的代碼如下:如果語句錯誤:ValueError異常:無效的字面INT()基數爲10:「」和亂序INT()的

allnums = [] 
odds = [] 
evens = [] 

number = 0 

while True: 
    number = input("Enter a number or type 'done':") 

    if number.lower() == 'done': 
     print("=============================") 
     print("Your even numbers are", str(evens)) 
     print("Your odds numbers are", str(odds)) 
     print("=============================") 
     print("Your smallest number is '"+str(allnums[0])+"'") 
     break 
    if int(number) % 2 == 0: 
     evens.append(number) 
     evens.sort() 
     allnums.append(number) 
     allnums.sort()  
    if int(number) % 2 != 0: 
     odds.append(number) 
     odds.sort() 
     allnums.append(number) 
     allnums.sort() 
    else: 
     print("Invalid input") 

我試圖創建一個程序,讀取數字列表並確定最低值,同時還提供偶數和奇數的列表。我遇到兩個問題與我的代碼,其中最小的數字是不是經常正確,例如:

Enter a number or type 'done':33 
Enter a number or type 'done':4 
Invalid input 
Enter a number or type 'done':6 
Invalid input 
Enter a number or type 'done':4 
Invalid input 
Enter a number or type 'done':6 
Invalid input 
Enter a number or type 'done':7 
Enter a number or type 'done':44 
Invalid input 
Enter a number or type 'done':88 
Invalid input 
Enter a number or type 'done':done 
============================= 
Your even numbers are ['4', '4', '44', '6', '6', '88'] 
Your odds numbers are ['33', '7'] 
============================= 
Your smallest number is '33' 

我也得到了以下錯誤,當我使用只是一個空格(」「)作爲一個答案,這我想編寫一個Print(「無效輸入」)響應的東西,是不是數字或完成,但結果總是在:

Traceback (most recent call last): 
    File "XXXX", line 17, in <module> 
    if int(number) % 2 == 0: 
ValueError: invalid literal for int() with base 10: ' ' 

我知道這大多與事件的順序做,但什麼我錯過了嗎?

回答

0
allnums = [] 
odds = [] 
evens = [] 

number = 0 

while True: 
    try: 
     number = input("Enter a number or type 'done':") 

     if number.lower() == 'done': 
      print("=============================") 
      print("Your even numbers are", str(sorted(evens))) 
      print("Your odds numbers are", str(sorted(odds))) 
      print("=============================") 
      print("Your smallest number is '"+str(sorted(allnums)[0])+"'") 
      break 
     elif int(number) % 2 == 0: 
      evens.append(number) 
      #evens.sort() 
      allnums.append(number) 
      #allnums.sort()  
     elif int(number) % 2 != 0: 
      odds.append(number) 
      #odds.sort() 
      allnums.append(number) 
      #allnums.sort() 
     pass 

    except: 
     print("Invalid input") 
    pass 

你確定,這是我的輸出

[email protected]:~/Desktop/d$ python3 code.py 
Enter a number or type 'done':12 
Enter a number or type 'done': 
Invalid input 
Enter a number or type 'done': 
Invalid input 
Enter a number or type 'done':qwe 
Invalid input 
Enter a number or type 'done': 
Invalid input 
Enter a number or type 'done':wqew 
Invalid input 
Enter a number or type 'done':done 
============================= 
Your even numbers are ['12'] 
Your odds numbers are [] 
============================= 
Your smallest number is '12' 
+0

仍返回相同 '回溯(最近最後一次通話): 文件 「XXXX」,第17行,在 如果INT(數字)%2 == 0: ValueError異常:無效的字面INT()與基地10:''' – maaier

+0

你確定,我測試了它,給了我完美的結果..等待粘貼輸出 –

+0

啊,我沒有通行證。現在導致明顯的無效響應。真棒:) – maaier

0

錯誤消息很明顯:number包含一個空格,無法將其解析爲整數。如果你想標記這樣的輸入無效,你必須在之前測試試圖將其轉換爲整數(或捕獲這樣做的錯誤)。

+0

你會怎麼建議添加/更改?我有點傻眼,因爲我嘗試添加不同的測試人員,但收到相同的結果。 – maaier

0

你有兩個問題:

  1. 需要檢查的空間你想從它那裏得到一些之前。

  2. 然後要附加數字輸入作爲串到你的列表中,這將錯誤的排序結果結束,因爲這將排序string而不是number


allnums = [] 
odds = [] 
evens = [] 

number = 0 

    if number.lower() == 'done': 
    evens.sort() 
    odds.sort() 
    allnums.sort() 
    print("=============================") 
    print("Your even numbers are", str(evens)) 
    print("Your odds numbers are", str(odds)) 
    print("=============================") 
    print("Your smallest number is '"+str(allnums[0])+"'") 
    break 

if number.isspace(): 
    print("Invalid input") 
else: 
    number = int(number) 
    if int(number) % 2 == 0: 
     evens.append(number) 
     allnums.append(number) 
    elif int(number) % 2 != 0: 
     odds.append(number) 
     allnums.append(number) 
0
allnums = [] 
odds = [] 
evens = [] 

number = 0 

while True: 
    number = input("Enter a number or type 'done':") 

    if number.lower() == 'done': 
     print("=============================") 
     print("Your even numbers are", str(evens)) 
     print("Your odds numbers are", str(odds)) 
     print("=============================") 
     print("Your smallest number is '" + sorted(allnums)[0] + "'") 
     break 
    if not number.isnumeric(): 
     print("Invalid input") 
     continue 
    if int(number) % 2 == 0: 
     evens.append(number) 
     evens.sort() 
     allnums.append(number) 
     allnums.sort() 
    if int(number) % 2 != 0: 
     odds.append(number) 
     odds.sort() 
     allnums.append(number) 
     allnums.sort() 
相關問題