Use Python to find average of some numbers

user1718826 picture user1718826 · Oct 11, 2012 · Viewed 17.7k times · Source

I am using Python to do a question like keep asking user to enter a number or not. If not,the program should calculate the average of these numbers. It's seems like my program cannot escape from the second while loop, and "except" is still wrong. Here is my program:

count =0
total=0
ask=input ("Do you want to enter a number? (Y/N)")
while ask=="Y":
    numbers=float(input("Enter number"))
    count= count+1
    total=total+numbers
    con_ask=input ("Do you want to continue entering a number? (Y/N)")
    if con_ask=="Y":
        numbers=float(input("Enter number"))
        count=count+1
        total=total+numbers
    elif con_ask=="N":
        print ("The average of", count, "numbers is", total/count)
except :
    print ("Zero Division Occured. Average cannot be calculated")

Answer

iMom0 picture iMom0 · Oct 11, 2012

My version of computing average number:

count = 0
total = 0
ask = raw_input("Do you want to enter a number? (Y/N)")
try:
    while ask == "Y":
        numbers = float(raw_input("Enter number"))
        count = count + 1
        total = total + numbers
        con_ask = raw_input("Do you want to continue entering a number? (Y/N)")
        if con_ask == "Y":
            continue
        elif con_ask == "N":
            print "The average of", count, "numbers is", total / count
            break
except:
    print "Zero Division Occured. Average cannot be calculated"