CoursePython from Scratch to Intermediate · Module 4: Decisions and Loops · part 29 of 75
Part 29 · Module 4: Decisions and Loops

Lesson 3: while loops, and how to stop them

16 min read·14 Oct 2026
Module 4 · Lesson 3 · About 45 minutes · Needs: Module 4 Lessons 1 and 2 (if, elif, else, and, for loops, range())

The situation

Amira has Ben's loan, L13, in her hand. "He borrowed The War of the Worlds on day 21. How many days late is he today?" Tom adds a worry: he sometimes types nine instead of 9 into the fee program, and it stops with an error.

BEN, L13, SHELF DAYS day 21 borrowed day 35 due day 44 today 9 days to count
Ben's loan was due on day 35 and today is day 44; the 9 red squares are the late days a loop has to count.

Both jobs repeat a step until something becomes true: count one more day until today, ask again until the answer is a number. The while loop is made for that.

The rest of this part opens with an account

Signing up is free and takes one email. Nothing to pay, and nothing renews.