Python Coding Exercise #43: Shall We Continue?
Part of the series Python Practice Exercise — part 43
Before you start: Know input() · Know if statements · Know comparison operators
The question
This program should print out the message “hi” and then ask “Shall we continue?” until the user inputs “no”. Then the program should print out “okay then” and finish. Please have a look at the example below.
hi
Shall we continue? yes
hi
Shall we continue? oui
hi
Shall we continue? jawohl
hi
Shall we continue? no
okay then
What is new here: a loop
Until now, every program in this series ran from the first line to the last line once. This exercise needs the program to repeat some lines, and to decide by itself when to stop. That is what a loop does.
A while loop repeats its indented block again and again, for as long as its condition is true:
while condition:
# these lines repeat
In this exercise, we don’t know how many times the user will answer “yes”, so there is no number to count up to. The program has to keep going until the user types no.
My Solution
while True:
print("hi")
answer = input("Shall we continue? ")
if answer == "no":
break
print("okay then")
How My Solution Works
1. while True: starts a loop that never ends on its own
A while loop keeps going as long as its condition is true. Here the condition is simply True, and True is always true, so the loop would run forever. The only way out is the break inside it.
2. Everything indented is loop code block
The three lines under while True: are indented, so they are the loop’s block, and they repeat every time:
print("hi")shows the message.answer = input("Shall we continue? ")asks the question and stores what the user types.- The
ifchecks the answer.
3. break leaves the loop
When the answer is "no", the if is true and Python runs break. That ends the loop immediately, and Python jumps to the first line after the loop. For any other answer, the if is false, nothing happens, and the loop goes back to the top and prints hi again.
4. print("okay then") is outside the loop
This last line is not indented, so it is not part of the loop. It runs only once, after the loop has ended. If it were indented, the program would print okay then after every answer.
Following the sample
| Step | Python prints | User types | answer == "no"? | What happens |
|---|---|---|---|---|
| 1 | hi, then the question | yes | No | Loop again |
| 2 | hi, then the question | oui | No | Loop again |
| 3 | hi, then the question | jawohl | No | Loop again |
| 4 | hi, then the question | no | Yes | break, leave the loop |
| 5 | okay then | Program finishes |
Notice that hi is printed before the question every time, including the first time. So even if the user types no straight away, the output is:
hi
Shall we continue? no
okay then
University Solution
while True:
print("hi")
answer = input("Shall we continue? ")
if answer == "no":
break
print("okay then")
The University Solution is the same as My Solution. The only difference is the blank line before the last print(), and a blank line doesn’t change anything in Python. It is there only to separate the loop from the line after it, so the code is easier to read.
Things to notice
The answer has to be exactly no. The comparison answer == "no" is case-sensitive and exact, as in Exercise #37. So No, NO and no (with a space at the end) don’t stop the program, and it asks again.
A loop that never ends. while True is useful when you don’t know how many rounds you need, but a loop without a working break runs forever. If that happens while testing, press Ctrl + C in the terminal to stop the program.
Key takeaways
- A
whileloop repeats its indented block for as long as its condition is true. while True:creates a loop that only ends whenbreakruns.breakends the loop at once, and the program continues with the first line after it.- Only the indented lines belong to the loop. An unindented line runs once, after the loop.
- Use a loop when you don’t know in advance how many times something has to repeat.
- The stop word has to be typed exactly, so
Nodoesn’t matchno.
✦ 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.