Lesson 3 of 8 · 12 min

Challenge: The kombi conductor's fare calculator

Kuda is a conductor on a kombi from Harare to Chitungwiza. Families climb on together and ask, "How much for all of us?" He wants his phone to answer instantly.

The fare is $1, but not everyone pays the same.

Your task

Write kombi_fare(ages). It takes a list of the passengers' ages and returns the total the group pays, in dollars.

Passenger Age Pays
Baby under 3 free
Child 3 to 11 half fare, $0.50
Adult 12 to 59 full fare, $1
Pensioner 60 and over half fare, $0.50

Group discount: if 6 or more passengers in the group pay something, the whole group gets $1 off the total. Babies ride free, so they do not count towards the 6.

kombi_fare([30, 41, 18])                 # 3
kombi_fare([35, 33, 7, 1])               # 2.5   (two adults, one child, a baby)
kombi_fare([20, 20, 20, 20, 20, 20])     # 5     (6 adults: 6 - 1)
kombi_fare([20, 20, 20, 20, 20, 1])      # 5     (only 5 pay, no discount)
kombi_fare([])                           # 0

Rules

  • Ages are whole numbers, 0 or more.
  • Return a number, not text: 2.5, not "$2.50".
  • An empty list means nobody is travelling: return 0.

Tips

  • Read the table from top to bottom as an if / elif / else chain. Watch the edges: is a 3-year-old free or half price?
  • Keep two variables as you loop: the total so far, and how many passengers have paid.
  • Apply the discount once, after the loop, not inside it.
main.py
def kombi_fare(ages):
    # TODO: add up the fare for each passenger, then apply the group discount
    return 0


print(kombi_fare([35, 33, 7, 1]))  # should print 2.5

Python 3.14 runs right here in your browser. The first run downloads about 10 MB; after that it is instant.

Discussion

Stuck? Ask here. Be kind, and share hints rather than full answers.

Loading the discussion…