[英]I am having troubles with Python functions, can someone help me?
Goal: I am trying to create a program that can take in 10 numbers then spit out the 10 largest numbers. 目标:我正在尝试创建一个程序,该程序可以输入10个数字,然后吐出10个最大的数字。
I need all integers able to be inserted, then for the program to find the odds and see which of them are the biggest. 我需要所有能够插入的整数,然后程序才能找到几率,并查看其中最大的几率。
#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)
What you need to fix: 您需要解决的问题:
Check whether the length of the list numbers
exceeds 9 or not. 检查列表
numbers
的长度是否超过9。 You can get the length of a list by using len( ) function. 您可以使用len()函数获取列表的长度。 So, it should be :
while len(numbers) < 9:
. 因此,应为:
while len(numbers) < 9:
You didn't append the first input to the list numbers
. 您没有将第一个输入添加到列表
numbers
。
The way you find the highest odd
doesn't work. 您
find the highest odd
的方法不起作用。 Check out the modification. 检查修改。
To sum up, the code should be : 综上所述,代码应为:
#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)
Sample : 样品:
>>> 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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.