Python Coding Exercise #53: The Sum of Consecutive Numbers
Part of the series Python Practice Exercise — part 53
Before you start: Know input() and int() · Know while loops with a condition · Know f-strings
These are the last two exercises of the section “Loops with conditions” in Part 3 of the course. The second exercise is built on the program from the first, so I put them in one post.
The question
Version 1
Please write a program which asks the user to type in a limit. The program then calculates the sum of consecutive numbers (1 + 2 + 3 + …) until the sum is at least equal to the limit set by the user. The program should function as follows:
Limit: 2
3
Limit: 10
10
Limit: 18
21
If you have trouble understanding how the desired output is calculated, the sample outputs in the next exercise may help. You may assume the number typed in by the user is always equal to 2 or higher.
Version 2
Please write a new version of the program in the previous exercise. In addition to the result it should also print out the calculation performed:
Limit: 2
The consecutive sum: 1 + 2 = 3
Limit: 10
The consecutive sum: 1 + 2 + 3 + 4 = 10
Limit: 18
The consecutive sum: 1 + 2 + 3 + 4 + 5 + 6 = 21
You may assume the number typed in by the user is always equal to 2 or higher.
Before coding: what the program does
The program adds 1, then 2, then 3, and keeps going. After each addition, it asks whether the total has reached the limit. It stops at the first total that is at least the limit, which means equal to the limit or above it.
For a limit of 18, the total grows like this:
| Number added | Total so far | At least 18? |
|---|---|---|
| 1 | 1 | No |
| 2 | 3 | No |
| 3 | 6 | No |
| 4 | 10 | No |
| 5 | 15 | No |
| 6 | 21 | Yes, stop |
The answer is 21, which is the first total that is not below 18. For a limit of 10, the total reaches exactly 10 after adding 4, so the answer is 10.
We don’t know how many numbers will be added, so the program uses a loop with a condition. The condition is: keep adding while the total is still below the limit.
My Solution: version 1
numbers = 0
sum = 0
limit = int(input("Limit: "))
while sum < limit:
numbers = numbers + 1
sum = numbers + sum
print(sum)
How My Solution Works
1. Two variables start at zero
numbersholds the latest number that has been added: 1, then 2, then 3. It starts at 0, because nothing has been added yet.sumis the running total. It also starts at 0.
2. The condition: keep going while the total is below the limit
while sum < limit: is checked before every round. When sum reaches the limit or goes above it, the condition is false and the loop stops. This is why the answer is the first total that is at least the limit.
3. Each round has two steps
numbers = numbers + 1moves to the next number.sum = numbers + sumadds that number to the total.
4. The result is printed after the loop
print(sum) is not indented, so it runs once, when the loop is over.
Following a limit of 10
| Round | sum at the check | sum < 10? | numbers after | sum after |
|---|---|---|---|---|
| 1 | 0 | Yes | 1 | 1 |
| 2 | 1 | Yes | 2 | 3 |
| 3 | 3 | Yes | 3 | 6 |
| 4 | 6 | Yes | 4 | 10 |
| — | 10 | No | the loop stops |
The program prints 10.
My Solution: version 2
numbers = 0
sum = 0
calculation = ""
limit = int(input("Limit: "))
while sum < limit:
numbers = numbers + 1
sum = numbers + sum
if numbers == 1:
calculation += str(numbers)
else:
calculation += " + " + str(numbers)
print(f"The consecutive sum: {calculation} = {sum}")
What changed
The first part is the same as version 1. There are two additions:
- A text variable,
calculation, starts as an empty string. Along with the total, the loop now builds the text of the calculation, piece by piece, as in Exercise #49. - An
if/elseinside the loop decides what to add to the text.
The if / else and the plus signs
The calculation has a plus sign between the numbers, but not before the first one: 1 + 2 + 3, not + 1 + 2 + 3. So:
- The first number (
numbers == 1) is added alone:calculation += str(numbers). - Every other number is added with a plus sign in front:
calculation += " + " + str(numbers).
str(numbers) turns the number into text, because a number can’t be joined to text with +.
Following a limit of 10
| Round | numbers | sum | calculation |
|---|---|---|---|
| 1 | 1 | 1 | 1 |
| 2 | 2 | 3 | 1 + 2 |
| 3 | 3 | 6 | 1 + 2 + 3 |
| 4 | 4 | 10 | 1 + 2 + 3 + 4 |
The last line then prints The consecutive sum: 1 + 2 + 3 + 4 = 10.
University Solution
limit = int(input("Limit: "))
number = 1
sum = 1
numbers = "1"
while sum < limit:
number += 1
sum += number
# note that f-string can also be used like this
numbers += f" + {number}"
print(f"The consecutive sum: {numbers} = {sum}")
The University Solution solves the first-number problem in a different way. Instead of an if / else inside the loop, it prepares the first number before the loop:
number = 1,sum = 1andnumbers = "1"already contain the first step of the calculation.- Because of this, the loop starts from the second number. It does
number += 1, so the first number it adds is 2, and every number the loop adds needs a plus sign. That’s why it can usenumbers += f" + {number}"without anyif. - The f-string
f" + {number}"joins the plus sign and the number in one go, sostr()isn’t needed.
Both programs print identical output for every limit from 1 to 2000, including all the samples. The idea is different from My Solution: the work of the first round is done before the loop, instead of being handled inside it.
The two programs only differ for a limit below 1, which the exercise rules out by promising a limit of 2 or higher. Because the University Solution starts with sum = 1, it prints 1 = 1 even for a limit of 0, while My Solution skips the loop and prints an empty calculation.
Things to notice
The result can be above the limit. The program stops at the first total that is at least the limit. For a limit of 18, no total is exactly 18, so the answer is 21. It is exact only when the limit is a total such as 3, 6, 10, 15 or 21.
sum hides the built-in function. Both programs use sum as a variable name, which is also the name of Python’s built-in function sum(). It works here because the function is never used. As explained in Exercise #50, calling sum() after the name is taken gives TypeError: 'int' object is not callable. A name like total is safer.
Version 2 is version 1 plus a string. The loop and the total are the same. The only addition is building the text, which is a good reminder that a loop can build several things at the same time.
Key takeaways
- A loop with a condition can keep adding until a total reaches a limit:
while sum < limit:. - The loop ends at the first total that is at least the limit, so the result can be above it.
- A running total and a running piece of text can grow together in the same loop.
- When a separator (like
+) goes only between items, handle the first item separately, either with anifinside the loop or by preparing it before the loop. - An f-string can join text and a number without
str(). - Don’t use a built-in function name, 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: These exercises are 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.