The most common answer is:
total = 0
number_of_numbers = int(input(“How many numbers would you like to sum? “))
Repeat the code the number of times indicated by the user
for i in range(number_of_numbers):
next = int(input(“Enter a number: “))
total = total + next
print(“Sum: ” + str(total))
To ensure the code runs correctly and adheres to Python syntax, including using standard ASCII quotation marks and the proper use of the print function in Python 3, here’s the revised version of your code:
"""
This program first asks the user how many numbers they want to total. It then
asks them for that many numbers and reports the total of the numbers.
"""
total = 0
number_of_numbers = int(input("How many numbers would you like to sum? "))
# Repeat the code the number of times indicated by the user
for i in range(number_of_numbers):
next_number = int(input("Enter a number: ")) # 'next' is a built-in function, so using 'next_number' or another variable name is preferable
total = total + next_number
print("Sum: " + str(total))
This program will:
- Prompt the user to enter the number of numbers they wish to sum.
- Use a
for
loop to repeatedly ask the user to enter a number, adding each entered number to thetotal
. - Print the total sum after all the numbers have been entered and summed.