Back to Python
2026-03-285 min read

Example: Sum Numbers Until User Enters Zero (Python Programming)

Learn Example: Sum Numbers Until User Enters Zero (Python Programming) step by step with clear examples and exercises.

Why This Matters

Understanding how to write a program that sums numbers until the user enters zero is essential for mastering fundamental programming concepts. This simple yet powerful example demonstrates the use of loops, variables, and input/output operations in Python. By learning this concept, you can prepare yourself for more complex programming tasks and real-world coding challenges.

Prerequisites

To follow along with this lesson, you should have a basic understanding of Python syntax, variables, and input/output operations. If you're new to Python, we recommend reviewing the following resources before proceeding:

Core Concept

To create a program that sums numbers until the user enters zero, we'll use a while loop to continuously ask for input from the user. The loop will continue as long as the entered number is not zero. Here's an outline of our code:

total = 0
num_entered = 0

while True:
num = int(input("Enter a number (or 0 to stop): "))
total += num
num_entered += 1

if num == 0:
break

print(f"The average of the {num_entered} numbers you entered is {total / num_entered}")

Let's break this code down line by line:

  1. total = 0: We initialize the variable total to zero, as we don't have any numbers yet.
  2. num_entered = 0: We also initialize num_entered to zero, which will keep track of the number of entries made by the user.
  3. while True:: This creates an infinite loop that will run until it is explicitly stopped.
  4. num = int(input("Enter a number (or 0 to stop): ")): We ask the user for input and store their response as an integer. The input function returns a string, so we convert it to an integer using the int() function.
  5. total += num: We add the entered number to our total sum.
  6. num_entered += 1: We increment the count of numbers entered by one.
  7. if num == 0:: This checks if the user has entered zero, in which case we break out of the loop.
  8. break: Exits the loop when the user enters zero.
  9. print(f"The average of the {num_entered} numbers you entered is {total / num_entered}"): Once the user enters zero or the program is stopped for some reason, we print out the final average of the entered numbers.

Worked Example

Now let's try this code out in a Python interpreter:

>>> total = 0
>>> num_entered = 0
>>> while True:
num = int(input("Enter a number (or 0 to stop): "))
total += num
num_entered += 1

if num == 0:
break

Enter a number (or 0 to stop): 5
Enter a number (or 0 to stop): 3
Enter a number (or 0 to stop): 7
Enter a number (or 0 to stop): 2
Enter a number (or 0 to stop): 0
The average of the 4 numbers you entered is 4.5

In this example, we entered the numbers 5, 3, 7, 2, and then 0. The program calculated the sum of these numbers, counted the number of entries, and printed out the final average: 4.5.

Common Mistakes

Incorrect Input Type

One common mistake when writing this program is to forget that the input function returns a string by default. If you don't convert the input to an integer using int(), your program will throw an error when trying to perform arithmetic operations on the entered numbers.

>>> total = 0
>>> num_entered = 0
>>> while True:
num = input("Enter a number (or 0 to stop): ")
total += num
num_entered += 1

if num == '0':
break

Enter a number (or 0 to stop): 5
Traceback (most recent call last):
File "<pyshell#1>", line 6, in <module>
total += num
TypeError: Can't convert 'str' object to int

To fix this issue, make sure to use int(input("...")).

Infinite Loop

Another common mistake is forgetting to stop the infinite loop once the user enters zero. If you don't break out of the loop when the user enters zero, the program will continue asking for input indefinitely.

>>> total = 0
>>> num_entered = 0
>>> while True:
num = int(input("Enter a number (or 0 to stop): "))
total += num
num_entered += 1

if num == 0:
break

Enter a number (or 0 to stop): 5
Enter a number (or 0 to stop): 3
Enter a number (or 0 to stop): 7
Enter a number (or 0 to stop): 2
Enter a number (or 0 to stop): 0
The average of the 4 numbers you entered is 4.5
Enter a number (or 0 to stop): ...

To fix this issue, add a break statement inside the loop that stops it when the user enters zero.

Practice Questions

  1. Write a program that calculates the average of three numbers entered by the user. The program should continue asking for input until all three numbers have been entered.
  2. Modify the previous example so that the user can enter more than one number before entering zero to stop the loop. Print out the sum of all non-zero numbers entered.
  3. Write a program that finds the largest number among those entered by the user. The program should continue asking for input until the user enters zero or a negative number.
  4. (Bonus) Modify the original example so that it also calculates and prints out the sum of all the even numbers entered, as well as the sum of all odd numbers entered.

FAQ

Q: Why do I need to convert the input to an integer?

A: In Python, the input() function always returns a string. To perform arithmetic operations on the entered numbers, we must convert them to integers using the int() function.

Q: What happens if the user enters a non-numeric value (like "apple")?

A: If the user enters a non-numeric value, the program will throw an error when trying to convert it to an integer. To handle this situation, you can add a try-except block around the int(input("...")) line and provide a helpful message for invalid input.

Q: How can I make my program more user-friendly by handling errors and edge cases?

A: To make your program more user-friendly, you can add error handling to catch unexpected inputs and provide helpful messages to the user. You can also add additional features like checking if the entered number is within a valid range or displaying a friendly message when the user enters zero to indicate that the average has been calculated.

Q: How can I improve the efficiency of my program?

A: To improve the efficiency of your program, you can use a list to store the entered numbers instead of calculating the sum and count separately. This will allow you to access the individual numbers more easily and perform operations like finding the largest number or separating even and odd numbers more efficiently. Here's an example using a list:

numbers = []
total_even = 0
total_odd = 0
num_entered = 0

while True:
num = int(input("Enter a number (or 0 to stop): "))
if num == 0:
break
numbers.append(num)
num_entered += 1
if num % 2 == 0:
total_even += num
else:
total_odd += num

average = sum(numbers) / num_entered
print(f"The average of the {num_entered} numbers you entered is {average}")
print(f"The sum of all even numbers is {total_even}")
print(f"The sum of all odd numbers is {total_odd}")
Example: Sum Numbers Until User Enters Zero (Python Programming) | Python | XQA Learn