Python max and min

Speedy Bast picture Speedy Bast · Dec 15, 2011 · Viewed 96.7k times · Source

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 is the error.

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.")

Answer

Nate picture Nate · Dec 15, 2011

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)

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.