w3resource

Python Exercise: Calculate the sum and average of n integer numbers

Python Conditional: Exercise - 42 with Solution

Write a Python program to calculate the sum and average of n integer numbers (input from the user). Input 0 to finish.

Pictorial Presentation:

Python Exercise: Calculate the sum and average of n integer numbers
Sample Solution:

Python Code:

print("Input some integers to calculate their sum and average. Input 0 to exit.")

count = 0
sum = 0.0
number = 1

while number != 0:
	number = int(input(""))
	sum = sum + number
	count += 1

if count == 0:
	print("Input some numbers")
else:
	print("Average and Sum of the above numbers are: ", sum / (count-1), sum)
	

Sample Output:

Input some integers to calculate their sum and average. Input 0 to exit
.                                                                      
15                                                                     
16                                                                     
12                                                                     
0                                                                      
Average and Sum of the above numbers are:  14.333333333333334 43.0 

Flowchart :

Flowchart: Calculate the sum and average of n integer numbers

Visualize Python code execution:

The following tool visualize what the computer is doing step-by-step as it executes the said program:

Python Code Editor:

Have another way to solve this solution? Contribute your code (and comments) through Disqus.

Previous: Write a Python program to get next day of a given date.
Next: Write a Python program to create the multiplication table (from 1 to 10) of a number.

What is the difficulty level of this exercise?

Test your Python skills with w3resource's quiz



Python: Tips of the Day

Python: Function With Multiple Outputs

If a function is required to return multiple values then:

[Variable] AggregateFunction([Value] for [item] in [collection])