Python program to read numbers find the total average

 

Python program to read numbers repeatedly find the total, an average of numbers

Develop a program that repeatedly reads numbers until the user enters “done”. Once “done” is entered, print out the total, count, and average of the numbers. If the user enters anything other than a number, detect their mistake using try and except and print an error message and skip to the next number.

Video Tutorial

Source Code to read numbers display the total and average

sum = 0
cnt = 0
while True:
    inp = input("Enter the number or done: ")
    if (inp == 'done'):
        break
    try:
        inp = int(inp)
        cnt = cnt + 1
        sum = sum + inp
    except:
        print ("Enter the numeric value")
        continue
        
avg = sum / cnt

print ("Total is:", sum)
print ("Th eCount is:", cnt)
print ("Average is:", avg)

Output:

Case 1:

Enter the number or done: 10

Enter the number or done: 20

Enter the number or done: hit

Enter the numeric value

Enter the number or done: 30

Enter the number or done: done

Total is: 60

The count is: 3

Average is: 20.0

Case 2:

Enter the number or done: 5

Enter the number or done: 10

Enter the number or done: 15

Enter the number or done: Mahesh

Enter the numeric value

Enter the number or done: 20

Enter the number or done: done

Total is: 50

The count is: 4

Average is: 12.5

Summary:

This tutorial discusses how to write a Python program to read numbers repeatedly find the total, an average of numbers. If you like the tutorial share it with your friends. Like the Facebook page for regular updates and YouTube channel for video tutorials.

Leave a Comment

Your email address will not be published. Required fields are marked *

Welcome to VTUPulse.com


Computer Graphics and Image Processing Mini Projects -> Click Here

Download Final Year Project -> Click Here

This will close in 12 seconds