Python Coding Exercise #50: Working with Numbers
Part of the series Python Practice Exercise — part 50
Before you start: Know input() and int() · Know while True and break · Know if, elif and else · Know f-strings
The question
This exercise is worth four points, and it is built in steps. Each part adds one more result to the same program.
Pre-task
Please write a program which asks the user for integer numbers. The program should keep asking for numbers until the user types in zero.
Please type in integer numbers. Type in 0 to finish.
Number: 5
Number: 22
Number: 9
Number: -2
Number: 0
Part 1: Count
After reading in the numbers the program should print out how many numbers were typed in. The zero at the end should not be included in the count.
You will need a new variable here to keep track of the numbers typed in.
Numbers typed in 4
Part 2: Sum
The program should also print out the sum of all the numbers typed in. The zero at the end should not be included in the calculation.
Numbers typed in 4
The sum of the numbers is 34
Part 3: Mean
The program should also print out the mean of the numbers. The zero at the end should not be included in the calculation. You may assume the user will always type in at least one valid non-zero number.
Numbers typed in 4
The sum of the numbers is 34
The mean of the numbers is 8.5
Part 4: Positives and negatives
The program should also print out statistics on how many of the numbers were positive and how many were negative. The zero at the end should not be included in the calculation.
Numbers typed in 4
The sum of the numbers is 34
The mean of the numbers is 8.5
Positive numbers 3
Negative numbers 1
Before coding: what to track
The program has to read the numbers in a loop, and use them after the loop. Each result needs something to be remembered while the loop runs:
| Result | What it needs | When it is calculated |
|---|---|---|
| Count | A counter that goes up by 1 for every number | In the loop |
| Sum | A total that grows by each number | In the loop |
| Mean | The sum divided by the count | After the loop |
| Positives | A counter that goes up for every number above zero | In the loop |
| Negatives | A counter that goes up for every number below zero | In the loop |
The mean doesn’t need its own variable in the loop, because it can be worked out from two things that are already tracked: the sum and the count.
The most important detail is the zero. It ends the loop, but it is not one of the numbers, so it must not be counted or added. To make sure of that, the if number == 0: break check has to come before any counting.
My Solution
numbers = []
positive_int = 0
negative_int = 0
print("Please type in integer numbers. Type in 0 to finish.")
while True:
number = int(input("Number: "))
if number == 0:
break
numbers.append(number)
if number > 0:
positive_int += 1
elif number < 0:
negative_int += 1
print(f"Numbers typed in {len(numbers)}")
print(f"The sum of the numbers is {sum(numbers)}")
print(f"The mean of the numbers is {sum(numbers)/len(numbers)}")
print(f"Positive numbers {positive_int}")
print(f"Negative numbers {negative_int}")
How My Solution Works
1. An empty list for storing values
numbers = [] creates an empty list. A list is a variable that holds many values, one after another. Inside the loop, numbers.append(number) adds the new number to the end of the list. After the sample, numbers is [5, 22, 9, -2].
Learn more about list manipulation in my blog post on lists, tuples, sets and dictionaries.
2. Counters for positive and negative integers
positive_int and negative_int both start at 0. In each round, the number is checked: if number > 0 adds 1 to positive_int, and elif number < 0 adds 1 to negative_int.
3. The if statement
if number == 0: break comes before numbers.append(number). So when the user types 0, the loop ends immediately, and the zero never reaches the list or the counters. (For while True and break, see Exercise #43.)
4. The results come from the list after the loop
Everything that needs the whole set of numbers is worked out after the loop, using two built-in functions on the list:
len(numbers)gives how many numbers there are, which is the count. (len()was introduced in Exercise #30.)sum(numbers)adds all the numbers together.
The mean is sum(numbers)/len(numbers), which is the sum divided by the count: 34 / 4 = 8.5. The positives and negatives are printed from their counters.
Following the sample
| Round | The user types | numbers after | positive_int | negative_int |
|---|---|---|---|---|
| 1 | 5 | [5] | 1 | 0 |
| 2 | 22 | [5, 22] | 2 | 0 |
| 3 | 9 | [5, 22, 9] | 3 | 0 |
| 4 | -2 | [5, 22, 9, -2] | 3 | 1 |
| 5 | 0 | unchanged | 3 | 1 |
After the loop: the count is 4, the sum is 5 + 22 + 9 − 2 = 34, the mean is 34 / 4 = 8.5, and there are 3 positive and 1 negative numbers.
University Solution
print("Please type in integer numbers. Type in 0 to finish.")
numbers = 0
sum = 0
positives = 0
while True:
number = int(input("Number: "))
if number == 0:
break
numbers += 1
sum += number
if number>0:
positives += 1
print("Numbers typed in", numbers)
print("The sum of the numbers is", sum)
print("The mean of the numbers is", sum/numbers)
print("Positive numbers", positives)
print("Negative numbers", numbers-positives)
The University Solution gets the same results without a list. Instead, it keeps three plain numbers that it updates in the loop:
numbersis the count. It goes up by 1 for each number typed. In My Solution, the count islen(numbers).sumis a running total. It adds each new number to itself withsum += number, so after the sample it is 34. In My Solution, the total issum(numbers).positivesis a counter for numbers above zero, as in mine.- The negatives are not counted at all. They are worked out at the end:
numbers - positives. Since the zero was never counted, every number typed is either positive or negative, so the negatives are what is left after taking out the positives. For the sample, that is 4 − 3 = 1.
It also prints with commas inside print(), for example print("Numbers typed in", numbers), which puts a space between the text and the value, so the output is identical to the f-strings in My Solution. (See Exercise #05 for how commas and spaces work in print().)
List or counters?
| My Solution | University Solution | |
|---|---|---|
| Keeps the numbers | Yes, in a list | No, only the count and the total |
| Count | len(numbers) | numbers += 1 in the loop |
| Sum | sum(numbers) | sum += number in the loop |
| Negatives | Own counter | numbers - positives |
Both programs printed identical output for every set of numbers tested, including the sample, only positive numbers, only negative numbers, and numbers that add up to zero. The list version keeps all the numbers, so it is easier to do more with them later, such as finding the largest. The counters version only keeps what the exercise asks for.
Things to notice
The mean is always a float. The / operator gives a decimal result, even when it divides evenly. So three numbers adding up to 9 give 3.0, not 3.
The mean fails if the first number is zero. If the user types 0 straight away, the count is 0, and Python can’t divide by 0. Both programs stop with a ZeroDivisionError. The exercise says you may assume at least one non-zero number is typed, so it doesn’t need to be handled here.
The University Solution uses sum as a variable name. sum is also the name of Python’s built-in function that adds up a list, which My Solution uses. Once a variable is called sum, the name no longer means the function. Calling sum([1, 2]) afterwards gives TypeError: 'int' object is not callable. It works in this program because the function is never needed, but it’s safer to use another name, such as total. The same name is used again in Exercise #53.
elif number < 0 could be a plain else. The zero has already been dealt with by the break, so a number that isn’t above zero must be below it. I wrote elif number < 0 to make the meaning clear.
Key takeaways
- A loop can collect several results at once: a counter, a total, and more.
- Check for the stop value first, and
breakbefore any counting, so the stop value isn’t included. - A list can hold all the numbers, and
len()andsum()give the count and the total. - A counter and a running total can replace a list when only the count and the total are needed.
- The mean is the sum divided by the count, and it is worked out after the loop.
- If every number is either positive or negative, the negatives are the count minus the positives.
- Don’t use the name of a built-in function, like
sum, for your own variable.
✦ This is part of my Python Practice Exercise series, where I work through exercises while learning Python and explain each answer in plain language. ✦
Practice Source: This exercise is taken from the Python Programming MOOC 2026 by the University of Helsinki. I’m working through the exercises as part of my Python learning journey and documenting my answers and explanations here.