Sum of ten numbers. Given 10 numbers as input, read them and print their sum using as few variables as possible.
|
# Initialize the sum to 0 total_sum = 0 # Read 10 numbers from input and add them to the total sum for _ in range(10): number = int(input()) total_sum += number # Print the total sum print(total_sum) |
- Initialize the Sum:
- A variable
total_sum
is initialized to 0. This will be used to store the sum of the 10 numbers.
- Reading and Adding Numbers:
for _ in range(10):
number = int(input())
total_sum += number
- A
for
loop runs 10 times, indicated by range(10)
.
- Each iteration reads a number from the user using
input()
, converts it to an integer with int()
, and stores it in the variable number
.
- The value of
number
is then added to total_sum
.
- Printing the Total Sum:
- Finally, the
print()
function outputs the value of total_sum
.