The most common answer is:
Variable to keep track of the running total
total = 0
Ask the user fora value and add to total
for i in range(10):
next = int(input(“Enter a number: “))
total = total + next
Report the result
print “Sum: ” + str(total)
To ensure this program runs correctly and adheres to Python syntax, including using standard ASCII quotation marks and proper use of the print function in Python 3, here’s the revised version of your code:
"""
This program asks the user for ten numbers. It then reports the total of the
ten numbers.
"""
# Variable to keep track of the running total
total = 0
# Ask the user for a value and add to total
for i in range(10):
next_number = int(input("Enter a number: ")) # 'next' is a built-in function, so it's better to use a different variable name like 'next_number'
total = total + next_number
# Report the result
print("Sum: " + str(total))
This program will:
- Initialize a
total
variable to keep track of the sum of the numbers entered by the user. - Use a
for
loop to prompt the user to enter ten numbers, adding each to thetotal
. - Print the total sum after all ten numbers have been entered and summed.