Python最大和最小 [英] Python max and min
问题描述
我对Python很陌生,令我感到困扰的是我觉得这很简单.我在第8行中一直出现错误.我只希望该程序采用用户输入的数字并打印最大和最小的值,如果它们输入负1,我希望它取消循环.
I'm pretty new to Python, and what makes me mad about my problem is that I feel like it's really simple.I keep getting an error in line 8. I just want this program to take the numbers the user entered and print the largest and smallest, and I want it to cancel the loop if they enter negative 1.
'int' object is not iterable
是错误.
print "Welcome to The Number Input Program."
number = int(raw_input("Please enter a number: "))
while (number != int(-1)):
number = int(raw_input("Please enter a number: "))
high = max(number)
low = min(number)
print "The highest number entered was ", high, ".\n"
print "The lowest number entered was ", low, ".\n"
raw_input("\n\nPress the enter key to exit.")
推荐答案
问题是number
是int
. max
和min
都需要列表(或其他可迭代的事物)-因此,您必须将number
添加到列表中,如下所示:
The problem is that number
is an int
. max
and min
both require lists (or other iterable things) - so instead, you have to add number
to a list like so:
number = int(raw_input("Please enter a number: "))
num_list = []
while (number != int(-1)):
num_list.append(number)
number = int(raw_input("Please enter a number: "))
high = max(num_list)
low = min(num_list)
阅读完jimbob博士的答案后,请注意-我的答案假设您不想在找到high
和low
时要考虑-1.
Just as a note after reading dr jimbob's answer - my answer assumes that you don't want to account for -1 when finding high
and low
.
这篇关于Python最大和最小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!