new to lists on python

Posted by user1762229 on Stack Overflow See other posts from Stack Overflow or by user1762229
Published on 2012-12-09T22:59:23Z Indexed on 2012/12/09 23:04 UTC
Read the original article Hit count: 156

Filed under:
|

This is my current code:

while True:
        try:
            mylist = [0] * 7
            for x in range(7):
                    sales = float(input("Sales for day:"))
                    mylist[x] = sales
                    if  sales < 0:
                        print ("Sorry,invalid. Try again.")
        except:
            print ("Sorry, invalid. Try again.")
        else:
            break

print (mylist)

best = max(sales)
worst = min(sales)

print ("Your best day had", best, "in sales.")
print ("Your worst day had", worst, "in sales.")

When I run it I get this:

Sales for day:-5
Sorry,invalid. Try again.
Sales for day:-6
Sorry,invalid. Try again.
Sales for day:-7
Sorry,invalid. Try again.
Sales for day:-8
Sorry,invalid. Try again.
Sales for day:-9
Sorry,invalid. Try again.
Sales for day:-2
Sorry,invalid. Try again.
Sales for day:-5
Sorry,invalid. Try again.
[-5.0, -6.0, -7.0, -8.0, -9.0, -2.0, -5.0]
Traceback (most recent call last):
  File "C:/Users/Si Hong/Desktop/HuangSiHong_assign9_part.py", line 45, in <module>
    best = max(sales)
TypeError: 'float' object is not iterable

I am not quite sure how to code it so that, the lists do NOT take in negative values, because I only want values 0 or greater.

I am not sure how to solve the TypeError issue so that the min and max values will print as in my code

My last issue is, if I want to find the average value of the seven inputs that an user puts in, how should I go about this in pulling the values out of the lists

Thank you so much

© Stack Overflow or respective owner

Related posts about python

Related posts about list