← All coding practice

Coding Practice · Beginner · October 21, 2026 · 5 min read

Python Coding Exercise #52: Powers of Two and Powers of Base n

Part of the series Python Practice Exercise — part 52

Before you start: Know input() and int() · Know while loops with a condition · Know comparison operators

These two exercises are from the section “Loops with conditions” in Part 3 of the course. The second one is the first one with a small change, so I put them in one post.

The question

Powers of two

Please write a program which asks the user to type in an upper limit. The program then prints out numbers so that each subsequent number is the previous one doubled, starting from the number 1. That is, the program prints out powers of two in order.

The execution of the program finishes when the next number to be printed would be greater than the limit set by the user. No numbers greater than the limit should be printed.

Upper limit: 8
1
2
4
8
Upper limit: 20
1
2
4
8
16
Upper limit: 100
1
2
4
8
16
32
64

Please don’t use the value True as the condition of your while loop in this exercise!

What are powers of two? The first power of two is the number 1. The next one is 1 times 2, which is 2. The next is 2 times 2, which is 4. The next is 4 times 2, which is 8, and so forth. Each power in the sequence is multiplied by two to produce the next one.

Powers of base n

Please change the program from the previous exercise so that the user gets to input also the base which is multiplied (in the previous program the base was always 2).

Upper limit: 27
Base: 3
1
3
9
27
Upper limit: 1234567
Base: 10
1
10
100
1000
10000
100000
1000000

Please don’t use the value True as the condition of your while loop in this exercise!

Before coding: the pattern

In Exercise #51, the loops added a fixed amount each round (+1 or +2). Here, each number is the previous one multiplied by 2. It is the same loop, with * instead of +:

RoundNumber printedNext number (× 2)
112
224
348
4816
51632

The loop needs the same three things as before: a starting value (1), a condition (the number is not above the limit) and a change (multiply by 2).

My Solution: powers of two

number = int(input("Upper limit: "))
starting_number = 1
while starting_number <= number:
    print(starting_number)
    starting_number = starting_number * 2

How My Solution Works

1. Two variables

number holds the upper limit the user typed. starting_number holds the power that is about to be printed. It starts at 1, because the first power of two is 1.

2. The condition protects the limit

while starting_number <= number: is checked before each print. So a number above the limit is never printed. The exercise says that execution finishes “when the next number to be printed would be greater than the limit”, and this condition does that. I used <= and not <, because a power that is exactly equal to the limit should be printed. With a limit of 8, the output includes 8.

3. The change multiplies

starting_number = starting_number * 2 doubles the number after it is printed.

Following an upper limit of 20

Roundstarting_number at the check<= 20?Python printsNext value
11Yes12
22Yes24
34Yes48
48Yes816
516Yes1632
—32Nothe loop stops

So 32 is calculated, but never printed. For an upper limit of 100, the output ends at 64, because the next value, 128, is above the limit.

My Solution: powers of base n

number = int(input("Upper limit: "))
base = int(input("Base: "))
starting_number = 1
while starting_number <= number:
    print(starting_number)
    starting_number = starting_number * base

What changed

There are only two changes from the first program:

  • A new input: base = int(input("Base: ")) asks the user for the number to multiply by.
  • * 2 became * base: the fixed 2 is replaced by the variable.

Everything else stays the same. The program for powers of two is now just a special case of this one, with the base set to 2. Typing a base of 2 gives the same output as the first program, for every upper limit from 0 to 5000.

With a limit of 27 and a base of 3, the numbers are 1, 3, 9 and 27, and the next one, 81, is above the limit. With a limit of 1234567 and a base of 10, the output stops at 1000000, because the next one, 10000000, is above the limit.

Things to notice

The limit and the order of the questions. The program asks for the upper limit first and the base second, as in the sample, so the order of the two input() lines matters.

The limit is included. Both programs print a power that is exactly equal to the limit. For a limit of 8 with a base of 2, the last number is 8. For a limit of 27 with a base of 3, it is 27.

The first number is always 1. The output starts with 1 whatever the base is. If the upper limit is 0 or lower, the condition 1 <= 0 is false at the start, and nothing is printed.

A base of 1 or 0 never ends. With a base of 1, starting_number stays at 1 forever, so the loop prints 1 again and again. With a base of 0, it prints 1, and then 0 forever. The exercise expects a base of 2 or more, so it doesn’t need to be handled here. If it happens while testing, press Ctrl + C to stop the program.

Key takeaways

  • A loop can change its variable by multiplying, not only by adding.
  • The condition is checked before each print, so a number above the limit is never printed.
  • <= includes a number equal to the limit.
  • Replacing a fixed value (2) with a variable (base) makes a program more general. The first program becomes a special case of the second.
  • A multiplier of 1 or 0 doesn’t make the number grow, so the loop never ends.

✦ 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.

Tagged: python · loops · while-loop · powers · multiplication