On your 16th birthday, would a ticket machine still give you the child fare?
A ticket machine at a Galway station has three fares in this example: child, adult and senior. You enter your age on your sixteenth birthday. Which fare does the machine choose, and how can you tell from the code?
You type a whole number, such as fifteen. The input function reads it as text, and the int function returns the number fifteen. The assignment stores that number in age. Then print displays it.
Watch what happens when you ask whether age is less than sixteen. At fourteen, the answer is True. At nineteen, it's False. A comparison like this gives one of two Boolean values: True or False.
Now put sixteen into the same comparison. Python asks whether sixteen is less than sixteen. What Boolean value does that comparison give? Pick an answer before you see the fare rule.
In pseudocode, read the age first. If it’s under sixteen, set the child fare to four euro. Otherwise, if it’s under sixty-six, set the adult fare to eight euro. The remaining ages get the senior fare of five euro. Finally, output the chosen fare.
The Python function takes an age. The if, elif and else headers line up, and each indented assignment belongs to its own branch. Return lines up with if, so it runs after the chosen branch. The last two lines read the age, call the function and print the fare it returns.
The function receives sixteen. Sixteen is less than sixteen gives False, so skip the if block. Sixteen is less than sixty-six gives True, so set fare to eight and skip else. The function returns eight. The final line prints eight, so the ticket costs eight euro.
This time you enter sixty-six. Check whether sixty-six is less than sixteen, then whether it’s less than sixty-six. Use those results to decide which branch runs and what the function returns. Pick the output you’d see.
If you indent return inside else, it runs only in that branch. At sixteen, elif sets fare to eight, but the function reaches its end without returning a fare, so print displays None. Line return up with if and the function returns the chosen fare, so print displays eight.