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

Function argument unpacking in Python.

Example:

def tips_func(x, y, z):
  print(x, y, z)
  
tuple_val = (2, 0, 2)
dict_val = {'x': 3, 'y': 2, 'z': 1}
tips_func(*tuple_val)
tips_func(**dict_val)

Output:

2 0 2
3 2 1