Editor

Does the counting loop stop?

Intermediate · Write the code · from Undecidable Problems: Questions No Program Can Always Answer

From the lesson: Undecidable Problems: Questions No Program Can Always Answer

Some yes-or-no questions can be answered by a program for every possible input, and some cannot be answered by any program at all, no matter how fast the computer or how clever the programmer.

Read the lesson

Write loop_stops(start, step, stop) that returns True if this loop eventually stops and False if it runs forever:

i = start
while i != stop:
    i += step

Think about three cases: step is 0, the loop moves away from stop, and the loop jumps over stop without landing on it.

def loop_stops(start, step, stop):
# Your code here
pass

What Check runs

Your code runs first, then each of these lines, and 2 more that stay hidden. The output must match exactly.

RunsExpected output
loop_stops(0, 1, 10)True
loop_stops(0, 3, 10)False
loop_stops(20, -5, 0)True

More Undecidable Problems exercises

All Python exercises · 50 practice problems