Python program to print total, count, and average of elements of a list

Python program to print total, count, and average of elements of a list

Problem Definition

Python program to read numbers from the user until the user enters ‘done’ and store the numbers into a list. Once the user enters ‘done’ print out the total, count, and average of elements of a list.

Solution

Step by Step solution to Python program to print total count and average of elements of a list without using the list inbuilt functions

First, read the input from the user using the input statement.

If the input is other than ‘done’, then convert the input to floating-point value using float function.

The input is added to the variable total to get the partial total and increment the value of the count by 1.

If the input is ‘done’ break the loop. Calculate the average of numbers by dividing the total by count.

Finally print the total, count, and average of numbers entered by the user through standard input.

Video Tutorial

total = 0
count = 0
while (True):
    inp = input('Enter a number: ')
    if inp == 'done': 
        break
    value = float(inp)
    total = total + value
    count = count + 1
average = total / count
print("Total is: ", total)
print("The Count is: ", count)
print('Average:', average)

Output

Enter a number: 10

Enter a number: 20

Enter a number: 30

Enter a number: done

Total is: 60.0

The count is: 3

Average: 20.0

Step by Step solution to Python program to print total count and average of elements of a list without using the list inbuilt functions

First, create an empty list named numlist.

Read the input from the user using the input statement.

If the input is other than ‘done’, then convert the input to floating-point value using float function.

The input is appended to numlist using the append method of the list.

If the input is ‘done’ break the loop. Calculate the average of numbers by dividing the sum of list elements by the number of elements in the list. The Sum of elements of the list is calculated using the sum function and length is calculated using len function of the list.

Finally print the total, count, and average of numbers entered by the user through standard input.

numlist = []
while (True):
    inp = input('Enter a number: ')
        if inp == 'done': 
        break        
    value = float(inp)
    numlist.append(value)
    
average = sum(numlist) / len(numlist)
print("Total is: ", sum(numlist))
print("The Count is: ", len(numlist))
print('Average:', average)

Output

Enter a number: 10

Enter a number: 20

Enter a number: 30

Enter a number: done

Total is: 60.0

The count is: 3

Average: 20.0

Summary:

If you like the program please share it with your friends and share your opinion in the comment box. Like the Facebook page for regular updates and YouTube channel for video tutorials.

Leave a Comment

Your email address will not be published.