2013-05-15 121 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所以从我的例子来看,你想要得到的预期输出是什么? –

相关问题