Try it on paper first. A hint, the key idea and the worked solution are here when you need them.
A ticket is free under years, rupees under , rupees under , and rupees for or older. What do ages , , and pay? What goes wrong if the condition age < 60 is written first?
When conditions overlap, put the most special one first.
Have you tried it?
Work it out on paper first. It is fine to get stuck or make a mistake: that is how we learn. When you are ready, compare your working with the solution.
Plan. Check from the youngest age up, with elif.
age < 5 is true. Price 0.else. Price 120.The order. If age < 60 came first, then ages 3 and 10 would also pass it, and they would pay 200. The conditions overlap, so the most special one (age < 5) must come first.
Log in to join the discussion.
Loading…