Python Practice › List Programs › Program 99
Round every number and sum
Write a Python program to round every number in a list and print the sum of the rounded numbers multiplied by the list length.
Input
[22.4, 4.0, -16.22, -9.1, 11.0, -12.22, 14.2, -5.2, 17.5]
Output
[22, 4, -16, -9, 11, -12, 14, -5, 18] 243
Approach
- Round each value with
round(), add them, multiply by the length.
Solution
numbers = [22.4, 4.0, -16.22, -9.1, 11.0, -12.22, 14.2, -5.2, 17.5]rounded = [round(n) for n in numbers]print(rounded)print(sum(rounded) * len(numbers))Output
[22, 4, -16, -9, 11, -12, 14, -5, 18] 243
Line by line
Line 2
round(17.5) is 18 and round(-12.22) is -12; ties go to the even number.
Note
The sum of the rounded numbers is 27; multiplied by the 9 items it is 243.
Learn the concept
New to this topic? Read the lesson, then come back.