String, Int Differentiation and Looping in Python

The following code should be what you want.

count = 0
total = 0

while True:
    x = raw_input('Enter number: ')
    if(x.lower() == "done"):
        break
    else:
        try:
            x=int(x)
            total = total + x
            count = count + 1
            average = total / count
        except:
            print("That is not an integer. Please try again.")

print total, count, average

or in Python 3

count = 0
total = 0

while True:
    x = input('Enter number: ')
    if(x.lower() == "done"):
        break
    else:
        try:
            x=int(x)
            total = total + x
            count = count + 1
            average = total / count
        except:
            print("That is not an integer. Please try again.")

print(total, count, average)

Leave a Comment