liveBook · Manning

Infinite loop

As explained in Python Workout, Second Edition

An infinite loop is a loop whose condition remains true so that execution continues until an explicit exit changes the control flow.

An infinite loop keeps running because its condition remains true. In Python, while True is the clearest form; execution must reach break, usually after a successful guess or another explicit exit condition. section 2.3

Use an explicit exit path and guard the work performed on each iteration:

  • Put repeated input or processing inside while True. section 2.3
  • Test for the condition that ends the task, then use break to leave the innermost loop. section 2.3 section 5.9
  • Validate or convert input before comparing or storing it; invalid rainfall input uses continue to begin the next iteration. section 2.3 section 5.9
  • Watch transformations that revisit inserted characters, such as adding ub before every vowel, because the loop can process its own output. section 3.11

Guess-until-correct

A guessing program generates an answer with random.randint, then enters while True. Each iteration reads input, converts the returned string with int, compares the result with answer, and uses break when the guess is correct. Without that break, the prompt continues indefinitely. section 2.3

while s = input('Enter thoughts:'):
    print(f'Your thoughts are: {s}')

Compare a condition-controlled while loop with while True: the former exits when input becomes empty, while the latter requires an explicit break. section 2.3

How Python Workout, Second Edition covers it

How deep it goes
Explains while True, break, continue, input conversion, and a transformation-related failure mode, but stops short of loop internals.
Examples
Uses a number-guessing loop, a rainfall dictionary tracker, and a Ubbi Dubbi character transformation.
What it assumes
Assumes while loops, comparisons, Boolean conditions, input, and basic type conversion; reinforces string-to-integer conversion.
What you can do after
Apply it via Python while True, break, and continue to build interactive loops with explicit termination and input validation.
How it connects
Connects infinite loops to while loops, break statements, continue statements, input conversion, and Boolean conditions.

Also explained in

Compare how 3 books explain infinite loop →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Python Workout, Second Edition. The section links point to the parts of the book it draws on.