Your tracker says Over budget: False. A person reading the report would rather see a plain message, and a warning only when one is needed. This lesson makes the program choose which lines to run.
The concept
Here is your tracker as the last lesson left it. Run it:
Now run this. The total is over the budget:
Three lines printed. Now the same code with a bigger budget:
Only Report finished printed this time. The two indented lines were skipped.
The line if total > budget: is an if statement. Python works out the comparison. When it is True, Python runs the indented lines under it. When it is False, Python skips them and carries on from the first line that is not indented.
Those indented lines are called a block. Indentation is how Python knows where the block starts and stops. This is your first block, and every block in Python works the same way.
Often you want one thing or the other. Run this:
else: gives the if a second block. Exactly one of the two blocks runs: the if block when the condition is True, the else block when it is False.
The syntax
if total > budget:
print("Status: over budget")
print("Cut back before the month ends.")
else:
print("Status: within budget")if, then a condition, then a colon.- The block underneath is indented by four spaces. Every line in it must have the same indentation.
else:sits at the same indentation as itsif, and takes no condition. It ends with a colon too.- The block ends at the first line that goes back to the indentation of the
if.
In this sandbox, press Enter at the end of a line that ends in a colon, and the editor indents the next line for you. Press Shift+Tab to step back out of a block. Most code editors behave the same way.
A minimal working example
total = 12500
budget = 15000
if total > budget:
print("Status: over budget")
else:
print("Status: within budget")
print("Report finished")Output: Status: within budget, then Report finished. The last line is not indented, so it runs whichever way the choice went.
Common mistakes
You forget the colon. Run this:
SyntaxError: expected ':'Every line that opens a block ends with a colon: if, else, and the others you will meet in this part. Python names the missing character.
You forget to indent the block. Run this:
IndentationError: expected an indented block after 'if' statement on line 3The if line promised a block, and the next line was not indented. Add four spaces in front of it.
You use one equals sign in the condition. Run this:
SyntaxError: invalid syntax. Maybe you meant '==' or ':=' instead of '='?A condition must be a question. = stores a value, so it is not one. Python's first suggestion is the one you want: ==. You can ignore := for now.
Your indentation is uneven. If one line in a block has four spaces and the next has two, Python stops with IndentationError: unindent does not match any outer indentation level. Give every line in a block the same indentation.
You give else a condition. else total < budget: gives SyntaxError: expected ':'. else never takes a condition: it covers every case the if did not. The next lesson shows how to check a second condition.
Try it yourself
You will replace the bare True or False with a message your tracker chooses.
-
Run the starting code below. Confirm the last line is
Over budget:followed byTrueorFalse. -
Delete the
over_budget = total > budgetline and theOver budgetprint at the end. Under theBudgetprint, add this block, indented as shown:if total > budget: print("Status: over budget") print("Cut back before the month ends.") else: print("Status: within budget")Run it. Confirm the last line is
Status: within budgetif your total is below your budget. -
Test the other path: change your budget to
10000and Run it. Confirm the report ends with the two over-budget lines instead. Then put your budget back. -
Break it on purpose: delete the four spaces in front of
print("Status: within budget"). Run it. Confirm the console shows anIndentationErrorthat says it expected an indented block after the'else'statement. -
Fix it: put the four spaces back. Run it. Confirm your status line returns.
Your tracker now decides what to say. It has only two answers, though: over or within. A total ₦100 under budget gets the same calm message as a total ₦10,000 under. The next lesson adds a third answer in between.