2013-05-15 125 views
-1

目標:我正在嘗試創建一個可以輸入10個數字然後吐出10個最大數字的程序。我有麻煩的Python功能,有人可以幫助我嗎?

我需要所有能夠插入的整數,然後讓程序找到可能性並查看其中哪些最大。

#Introduction 
print ('Enter 10 odd numbers to see which is the greatest ') 
#The big question 
user_input = raw_input ('Enter a odd number ') 
#Input function that only accepts intergers 
numbers = [] 
while numbers < 11: 
    try: 
     numbers.append(int(raw_input(user_input))) 
     break 
    except ValueError: 
     print 'Invalid number' 
#Function that finds the highest odd and sees if it is odd 
highest_odd = max(user_input) and user_input % 2 != 0 
print 'The largest odd number was' + str(highest_odd) 
+2

如果你想在10個號碼,然後吐出的10大,你可以打印你的輸入。 – Marcin

+0

_「我需要所有能夠插入的整數,然後讓程序找到可能性並查看其中哪些是最大的。這是我的嘗試,但它不起作用。」_什麼部分不起作用?你認爲問題在哪裏? –

回答

4

您需要解決什麼:

  1. 檢查列表numbers長度是否超過9與否。您可以使用len()函數獲取列表的長度。所以,它應該是:while len(numbers) < 9:

  2. 您沒有將第一個輸入追加到列表numbers

  3. 你的方式find the highest odd不起作用。檢查修改。

綜上所述,代碼應該是:

#Introduction 
print ('Enter 10 odd numbers to see which is the greatest ') 

#The big question 
user_input = int(raw_input('Enter an odd number ')) 

#Input that only accepts integers 
numbers = [] 
while len(numbers) < 9: 
    try: 
     numbers.append(user_input) 
     user_input = int(raw_input('Enter an odd number ')) 
    except ValueError: 
     print 'Invalid number' 

#Find the highest odd 
highest_odd = max(i for i in numbers if i % 2) 

print "The largest odd number was " + str(highest_odd) 

樣品:

>>> Enter 10 odd numbers to see which is the greatest 
>>> Enter an odd number 3 
>>> Enter an odd number 5 
>>> Enter an odd number 1 
>>> Enter an odd number 7 
>>> Enter an odd number 6 
>>> Enter an odd number 4 
>>> Enter an odd number 1.3 
Invalid number 

>>> Enter an odd number 9 
>>> Enter an odd number 4 
>>> Enter an odd number 6 
The largest odd number was 9 
+0

也許我沒有澄清,我需要所有能夠插入的整數,然後讓程序找到可能性並查看其中哪些最大。這是我嘗試過的,但它不工作-__- –

+0

@llija所以從我的例子來看,你想要得到的預期輸出是什麼? –

相關問題