-1

目标:我正在尝试创建一个可以输入 10 个数字然后吐出 10 个最大数字的程序。

我需要能够插入所有整数,然后让程序找到几率并查看其中哪个是最大的。

#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)
4

1 回答 1

4

您需要解决的问题:

  1. 检查列表的长度numbers是否超过 9。您可以使用len( )函数获取列表的长度。所以,应该是:while len(numbers) < 9:

  2. 您没有将第一个输入附加到 list 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
于 2013-05-15T22:00:45.313 回答