Please help - get average

Cliff Wells clifford.wells at comcast.net
Wed Oct 27 03:49:52 EDT 2004


On Tue, 2004-10-26 at 12:49 -0500, Borman, Linda E wrote:
> Hi , Im new to Python can you help.
> Trying to create a program that  I can entered  only 10 random
> numbers, and calculate the average of those numbers after executing a
> loop. Using for loop.
>  
>  
> Stumped
> This is what I have so far.
>  
> # using for loop
> number = 10
> while number !=0 :
>     number = input("Enter a number: ")
>     count = number
>     sum =  number
>     for a in range (1):
>         print sum 
>         print "The average is: ", sum / count
>     print "all Done"

Try it something like this:

total = 0.0
count = 0
number = None

while number not in ('q', 'Q'):
    number = raw_input("Enter a number (or Q to quit): ")
    try:
        number = float(number)
    except ValueError:
        continue
    total += number
    count += 1

print "The average is:", total / count


Regards,
Cliff
 
-- 
Cliff Wells <clifford.wells at comcast.net>




More information about the Python-list mailing list