temp >= 25?1temp = int(input("Temp? "))2if temp >= 25:3 print("T-shirt")4elif temp >= 15:5 print("Jumper")6else:7 print("Coat")8print("Bye!")if temp = 25:SyntaxError181818JumperBye!30T-shirt
Code card: wear.py; the highlighted line is the one running, faded lines are skippedCondition: the comparison Python works out and its answer, True or FalseTerminal: where input() asks and print() writesMemory box: the number stored under the name tempAn if asks a yes-or-no question, a condition, and runs its indented block only when the answer is True. wear.py picks what to wear from a temperature. Each step lights the parts it is about. A token hops from the terminal into the memory box for temp, from temp to the condition sign, which shows the comparison and its answer, and from a line to the terminal. Lines in every block the run skips fade back, so the path stays visible. Typed 18, the run prints Jumper; typed 30, it prints T-shirt. In the last step line 2 becomes if temp = 25: and Python refuses to run the file.

Get a value

  1. Line 1 asks Temp? and waits. You type 18. input() gives back the text '18', int() turns it into the number 18, and = stores it in temp. Now the program has to make a decision.

if, elif, else

A new answer

One slip

Try it yourself

;; step 1/10 · line 1: temp = int(input("Temp? "))
>>> text = input("Temp? ")
Temp? 18
>>> text
'18'
>>> temp = int(text)
>>> temp
18
>>> temp + 1        # a number: maths works
19

Python asks the questions from the top, runs the block of the first one that is True and skips every other block of the chain; the questions below it are not even worked out. The figure clears the terminal between the two runs; the $ is the terminal's own prompt. On macOS and Linux the command may be python3. In the real error message the ... is the full path to the file, and the figure wraps its long last line to fit the screen.