我有Python功能的麻烦,有人可以帮助我吗?

时间:2013-05-15 21:47:05

标签: python python-2.7

目标:我正在尝试创建一个程序,可以接收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)

1 个答案:

答案 0 :(得分:4)

您需要修复的内容:

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

  2. 您没有将第一个输入附加到列表numbers

  3. find the highest odd无效的方式。查看修改。

  4. 总结一下,代码应为:

    #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