AW Dev Rethought

✂️ Perfection is achieved not when there is nothing more to add, but when there is nothing left to take away - Antoine de Saint-Exupéry

🐍 Python From Zero: 05/13 — Making Decisions 🎟️


Description:

On the day Mira turns 13, the same cinema seat goes from ₹60 to ₹120. A ticket calculator can explain the change with two comparisons. Last lesson we turned a typed answer into a number. This time we’ll let that number choose which line of code runs.


Turn the price board into questions

For this fictional cinema, ages 0–4 enter free, ages 5–12 pay ₹60, and ages 13 and up pay ₹120. Ask about the youngest group first. If someone reaches the second question, you already know they are at least five.

if age < 5:
    price = 0
elif age < 13:
    price = 60
else:
    price = 120

print(f"Ticket: ₹{price}")

The < sign means “strictly less than.” Each comparison gives Python either True or False. A colon starts each block. Indent its assignment by four spaces. Line up elif and else with if. The final print() sits at the left margin, so it runs after any of the three choices.


Trace a birthday

Age age < 5 age < 13 Printed ticket
4 True Skipped ₹0
5 False True ₹60
12 False True ₹60
13 False False ₹120

At age 4, both comparisons would be true in isolation. In this chain, Python takes the first branch and skips the rest. Reversing the order of the two tests would charge a four-year-old ₹60. At exactly 13, the second comparison is false because 13 equals the cutoff. The else block needs no comparison of its own.


Run the calculator

Save ticket_price.py and run python3 ticket_price.py (or py ticket_price.py on Windows). The script reads the answer with input(), strips surrounding spaces and uses int() to convert digit text to an integer. It asks for an age in whole years. An entry like -1 or five ends the program with a message.

Age in years: 13
Ticket: ₹120

Your turn. Add an ₹80 senior ticket starting at 60. Predict what happens at 59 and 60 before running the program. The practice sheet and separate solution are included.


Link copied!

Comments

Add Your Comment

Comment Added!