Calling a looping input into a max/min builtin?

This is the place for queries that don't fit in any of the other categories.

Calling a looping input into a max/min builtin?

Postby Tyreejackson » Wed Jun 12, 2013 9:07 pm

Hi there,

I am trying to write a function that uses a while loop. The program must find the average, maximum and minimum of the numbers that the user inputs. Then I need to call the function from another module. I have managed to get the average to work, but I cannot use the max() and min() functions correctly it seems.

The error message I get is:

Traceback (most recent call last):
File "C:\Users\Scott\workspace\woooo\herro.py", line 6, in <module>
total, average, smallest = maggiespizza.sla(n)
File "C:\Users\Scott\workspace\woooo\maggiespizza.py", line 15, in sla
smallest = min (x)
TypeError: 'int' object is not iterable


Here is my code:
Code: Select all
def sla (n):
    FIRST_N = n
    total = 0
    while n > 0:
        x = int(input("Enter a value: "))
        total += x
        n = n - 1

    average = total / FIRST_N
    smallest = min (x)
   
    return total, average, smallest
Tyreejackson
 
Posts: 7
Joined: Wed Jun 12, 2013 3:56 am

Re: Calling a looping input into a max/min builtin?

Postby Yoriz » Wed Jun 12, 2013 9:33 pm

Min & Max take more then one item to find which of those items is the smallest and largest, you are passing only a single item.
The following code stores the entered values into a list which those function can then work with.
Code: Select all
def sla(count):
    values = []
    while count > 0:
        values.append(int(input("Enter a value: ")))
        count -= 1

    total = sum(values)
    average = total / len(values)
    smallest = min(values)
    largest = max(values)

    return total, average, smallest, largest
New Users, Read This
Join the #python-forum IRC channel on irc.freenode.net!
Spam topic disapproval technician
Windows7, Python 2.7.4., WxPython 2.9.5.0., some Python 3.3
User avatar
Yoriz
 
Posts: 565
Joined: Fri Feb 08, 2013 1:35 am
Location: UK

Re: Calling a looping input into a max/min builtin?

Postby Tyreejackson » Wed Jun 12, 2013 9:59 pm

Yoriz wrote:Min & Max take more then one item to find which of those items is the smallest and largest, you are passing only a single item.
The following code stores the entered values into a list which those function can then work with.
Code: Select all
def sla(count):
    values = []
    while count > 0:
        values.append(int(input("Enter a value: ")))
        count -= 1

    total = sum(values)
    average = total / len(values)
    smallest = min(values)
    largest = max(values)

    return total, average, smallest, largest


Thanks Yoriz, this helped a lot. I hadn't known about this append builtin yet. I will remember this for the future, as it seems like I could use this often. Thanks again!
Tyreejackson
 
Posts: 7
Joined: Wed Jun 12, 2013 3:56 am


Return to General Coding Help

Who is online

Users browsing this forum: No registered users and 8 guests

cron