Python max and min
Asked Answered
F

5

8

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.")
Fink answered 15/12, 2011 at 19:21 Comment(0)
R
18

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.

Rachellerachis answered 15/12, 2011 at 19:26 Comment(2)
Completely true, and it should be fixed. It was that way in the question, however, and I'm a firm believer in providing answer code that deviates as little as possible from question code.Rachellerachis
It was my pleasure. Just so you know (because you're new here), if you choose an answer, it is customary to "accept" it - that is, the little check mark on the left side of the answer. See the section in the faq or this post on meta for more details.Rachellerachis
P
9

That's cause each time you pass one integer argument to max and min and python doesn't know what to do with it.

Ether pass at least two arguments:

least_number = min(number1, number2,...,numbern)

or an iterable:

least_number = min([number1, number2, ...,numbern])

Here's the doc

Pervious answered 15/12, 2011 at 19:24 Comment(1)
How would I pass multiple numbers to max and min? Thanks for the explanation as to why it's not working.Fink
R
1

You need to change number to an list of numbers. E.g.,

print "Welcome to The Number Input Program."

numbers = []
number = int(raw_input("Please enter a number: "))

while (number != -1):
    numbers.append(number)
    number = int(raw_input("Please enter a number: "))

high = max(numbers)
low = min(numbers)

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.")
Reciprocal answered 15/12, 2011 at 19:27 Comment(0)
M
0

As mentioned by another answer, min and max can also take multiple arguments. To omit the list, you can

print "Welcome to The Number Input Program."

number = int(raw_input("Please enter a number: "))
high = low = number


while (number != int(-1)):
    number = int(raw_input("Please enter a number: "))
    high = max(high, number)
    low = min(low, 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.")
Maduro answered 18/7, 2017 at 7:38 Comment(0)
D
0
num = ''
active = True

largest = 0
smallest = 0
while active:
    num = input("Please enter a number")
    if num == 'done': 
        active = False
        break
    else:
        try:
            num = int(num)

            if largest < num:
                largest = num
            if smallest == 0 or smallest > num:
                smallest = num

        except ValueError:
            print("Please enter a valid number")

print("Largest no. is " + str(largest))
print("Smallest no. is " + str(smallest))
Donoho answered 10/5, 2020 at 18:34 Comment(3)
This program will accept any count of numbers entered by user. It also checks for if the input string is a number, otherwise throws error.Donoho
Code dumps without any explanation are rarely helpful. Stack Overflow is about learning, not providing snippets to blindly copy and paste. Please edit your question and explain how it works better than what the OP provided.Glidewell
int object is not iterable. There are 2 ways to overcome the problem. First either define a list as mentioned in the solutions earlier. The second option is compare the number entered by the user with the Max and Min variables defined while iterating in the While loop.Donoho

© 2022 - 2024 — McMap. All rights reserved.