Your tracker knows how much of your budget is left. This lesson asks a question about the future. If you saved that amount every month, how many months would it take to reach a savings goal?
The concept
Here is your tracker as the last lesson left it. Run it:
Now run this:
Four passes, then it stopped. This is a while loop. Before every pass, Python checks the condition: is saved still under 10,000? While the answer is True, the block runs again. Each pass adds 2,500 to saved and one to months. After the fourth pass, saved is 10,000, the condition is False, and the loop ends.
Nobody told the loop to run four times. The number of passes was the answer to the question. That is when a while loop beats a for loop. A for loop runs once per item in a list. A while loop runs until something changes.
The condition is checked before the first pass too. If it is already False, the block never runs:
Now the important one. Run this, and wait:
The block adds to months, but nothing in it changes saved. So saved < 10000 stays True, and the loop has no reason to stop. A program like this runs until someone stops it.
After 5 seconds, this sandbox stops it for you. You see a traceback that points at the line inside the loop where it was running, and the error KeyboardInterrupt. On your own computer, a program like this would run until you pressed Ctrl+C in the terminal. Python then reports the same KeyboardInterrupt, because Ctrl+C is the keyboard interrupting the program.
The syntax
while saved < savings_goal:
saved = saved + left
months = months + 1while, a condition, and a colon. The block is indented four spaces.- Python checks the condition before every pass, including the first.
- Something in the block must eventually make the condition
False. Here,savedgrows on every pass, so it will reach the goal.
That last rule is the one to check every time you write a while loop. Find the name in the condition, then find the line in the block that changes it.
A minimal working example
left = 2500
savings_goal = 50000
saved = 0
months = 0
while saved < savings_goal:
saved = saved + left
months = months + 1
print(f"Saving ₦{left:,} a month reaches ₦{savings_goal:,} in {months} months.")Output: Saving ₦2,500 a month reaches ₦50,000 in 20 months.
Common mistakes
Nothing in the block changes the condition. The demo above shows it: no error, no output, then a stop after 5 seconds with KeyboardInterrupt. When a program seems to hang, look for a while loop, then check that its block changes the name in its condition.
The block changes the condition, but never enough. If left is 0, then saved = saved + left runs on every pass and saved never grows. The loop never ends, even though the code looks right. Your exercise breaks this way on purpose, and then guards against it.
You forget the colon or the indentation. The same rules as if and for: SyntaxError: expected ':', or an IndentationError that mentions the 'while' statement.
Try it yourself
You will add a savings forecast to your tracker, see it hang, then protect it.
-
Run the starting code below. Note how much is left in your status line.
-
Under your
budgetline, addsavings_goal = 50000. At the very end of the file, with no indentation, add:saved = 0 months = 0 while saved < savings_goal: saved = saved + left months = months + 1 print(f"Saving ₦{left:,} a month reaches ₦{savings_goal:,} in {months} months.")Run it. Confirm the last line gives a number of months. With the amounts given, it is 20.
-
Break it on purpose: change your budget so it equals your total exactly. With the amounts given, that is
budget = 12500. Nowleftis0. Run it and wait. Confirm that after about 5 seconds the console showsKeyboardInterrupt, pointing at a line inside your loop, and a note that the program was stopped. -
Guard against it. Select the six lines you added in step 2 and press Tab to indent them all. Above them, with no indentation, add
if left > 0:. Below them, with no indentation, add:else: print("Nothing left to save this month.")Run it, with your budget still equal to your total. Confirm the report now ends with
Nothing left to save this month. -
Put your budget back. Run it. Confirm the months line returns.
Your tracker can now look ahead. It also has four lines that repeat almost word for word. Total spent, Biggest spend, Smallest spend and Budget all print a label and an amount the same way. The next lesson packages that pattern into a function of your own.