Objective
Develop a Python program that prompts the user for an undetermined amount of numbers (until 0 is entered) and displays their sum, as follows:
Number? 5
Total = 5
Number? 10
Total = 15
Number? -2
Total = 13
Number? 0
Finished
Example Python Exercise
Show Python Code
# Initialize the total sum variable
total = 0
# Use a while loop to prompt the user for numbers until 0 is entered
while True:
num = float(input("Number? "))
# Check if the entered number is 0
if num == 0:
print("Finished")
break
# Add the entered number to the total sum
total += num
# Display the current total sum
print(f"Total = {total}")
Output
Number? 5
Total = 5
Number? 10
Total = 15
Number? -2
Total = 13
Number? 0
Finished
Share this Python Exercise