bhylu.com

Advertisements

Python Challenges

Combine validation, a pure calculation, and a readable report in one small booking exercise. Keep the solution understandable before optimising it.

Understand the idea

Given two bookings, calculate each cost and the grand total. Require a positive integer seat count; remember that bool is an int subclass if the boundary must exclude it. The worked solution focuses on the calculation after validated data is supplied. Add validation and edge tests as your next step.

Where to try it

Open this example with Try in playground. The code loads as editable text and does not run until you select Run. The first run downloads Python. Each run starts fresh; temporary files created by a script are not files on your PC.

Your task

Combine validation, a pure calculation, and a readable report in one small booking exercise. Keep the solution understandable before optimising it.

Show the worked solution
Python

bookings = [("Mira", 2, 1200), ("Dev", 3, 900)]
totals = [(name, seats * price) for name, seats, price in bookings]
for name, total in totals:
    print(name, total)
print("Grand total", sum(total for _, total in totals))

Check the result

The costs are 2400 and 2700 cents, for a grand total of 5100.

Test one more case

Add a validation function and demonstrate that invalid input cannot produce a misleading partial report.