Exercise 4 of 10 · Conditions
Umbrella or Not
What you will make
A rain report card that reads the forecast for you and signs off with a straight answer — take the umbrella, or leave it at home.
The one new idea: if/else chooses between two different outcomes
Anything that reacts is a program choosing between outcomes: a low battery warning, a free delivery message, a seat belt chime. if and else are how that choice gets written down.
Go straight to the code ↓Asking a question before acting
Everything you have written so far ran straight down the page, every line, every time. That is fine for a banner. It is useless for advice, because advice depends on the situation.
if is how a program asks a question before it acts. You give it something to check, and underneath it a
block of lines that run only when the answer is yes. Follow that with else and a second block, and you
have covered the no as well. Exactly one of the two blocks runs. Never both, never neither.
Two small things do all the work of holding that together:
- The
ifline ends in a colon. The colon means the lines below belong to this question. - Those lines are indented, pushed in by four spaces. The indentation is what marks them as the block. Python reads it the way you read a box on a paper form: what is inside the box is inside.
One equals sign or two
The question itself goes between if and the colon, and it is usually a comparison — two values held up
against each other:
==asks whether two values are the same>=asks whether the left one is at least as big as the right one
That first one catches almost everybody once, because one equals sign and two equals signs are completely different instructions:
=stores.rain_chance = 70puts 70 intorain_chance. It is a command, and it changes something.==compares.rain_chance == 70asks whether what is in there happens to be 70. It is a question, and it changes nothing.
Python will not accept = inside an if, because storing a value is not a question it can answer. It stops
and reports a syntax error before anything at all runs. If you ever see that, count the equals signs first.
A worked example
A cinema till, not a weather report, so the answer to this exercise stays yours to write:
age = 14
adult_from = 18
print("Age ......", age)
if age >= adult_from:
print("Ticket type: adult")
else:
print("Ticket type: child")
print("Enjoy the film.")Running that prints three lines:
Age ...... 14
Ticket type: child
Enjoy the film.14 is not at least 18, so the first block is skipped over entirely and the second one runs. Only one ticket
line ever appears. Notice the last line too: it is not indented, so it sits outside the question and prints
either way. Change age to 30 and the middle line changes while the other two stay put.
Your turn
The editor holds a rain report. Two numbers sit at the top: rain_chance is today's forecast, and
umbrella_mark is the point at which carrying an umbrella is worth the bother.
Run it before changing anything. The card prints, and then tells you to leave the umbrella at home on a day with a seventy percent chance of rain. That is because the test currently reads:
if rain_chance == 0:which asks whether the chance is exactly zero — a question with nothing to do with umbrellas, and one whose answer today is no.
Replace that test with one asking whether rain_chance has reached umbrella_mark: at the mark or above it
counts as take it. Everything else stays exactly as it is.
Then change rain_chance to 20 and run it again. The same program should now tell you to leave the umbrella
at home. That second run is the real proof — it shows the program is reading the numbers rather than
printing a fixed answer.
If something goes wrong
The likeliest slip is the equals sign. Written as if rain_chance = umbrella_mark:, Python stops on the
spot with a syntax error and nothing runs, not even the card above. That is Python saying it expected a
question and got an instruction. Add the second equals sign, or the comparison you actually meant.
Next likeliest is the colon going missing from the end of that line. Same kind of message, different cause: Python ran out of line while still waiting for the colon that opens the block.
If the message mentions indentation, the spaces in front of the print lines have shifted. They are not decoration — they are the only thing telling Python which lines belong to which answer. Put them back, run it again. Nothing here can break.
Write your code
Runs in your browser. Press Run (or Ctrl/Cmd+Enter) and the output is checked for you.
Press Esc then Tab to move keyboard focus out of the code editor.
Output will appear here after you run your code.The runtime is starting in the background. You can type now — it will be ready before you are.
The answer appears here once you have run your code at least once.
Things that often go wrong here
- Writing one equals sign in the test
- One equals sign means store this value, which is an instruction rather than a question. Python stops before running a single line and reports a syntax error on the if line, so not even the card above it prints.
- Leaving the colon off the end of the if line
- The colon is what opens the indented block underneath. Without it Python reaches the end of the line still waiting, and reports a syntax error pointing at that spot.
- Comparing the rain chance with a typed-in 50 instead of umbrella_mark
- It gives the right advice today and the wrong advice later. Change the mark to 80 and the test carries on using the old 50, because that number was typed into the question instead of looked up.
- Changing the indentation of the print lines
- Those four spaces are what put a line inside a branch. Move them and Python either reports that it expected an indented block, or that line escapes the question and prints every time whatever the forecast says.
Longer explanation: read the full lesson. Want a blank editor instead? Open the Python playground.