Python traps · Chapter 4 of 16

Float money

Build it in your browser: a real editor and terminal, AI that explains, and tests that tell you when it works.

  • Part: Everyday traps
  • 3 tasks
  • Python
  • In the all labs plan

The first 2 chapters of Python traps are free. This one comes with the all labs plan.

What you build: 3 tasks

Each task has tests. You write the code in the lab's editor and press Check.

  1. Task 1

    repro.py: a paisa short on a ₹225 bill

    Write reproduce() in repro.py. Take a ₹225 bill, apply the DABBA10 coupon with after_coupon from billing.py, work out the GST of the result with gst, and return that GST as a string, with str().

    The check runs your reproduce() twice: beside the original billing.py it must return '10.12' (a paisa short), and beside a fixed billing.py it must return '10.13' (5% of 202.50 is 10.125, and the GST rule rounds half a paisa up). So it must really call after_coupon and gst. No real waiting and no input(): reproduce() must finish within 3 seconds.

  2. Task 2

    billing.py: every paisa where the GST rule puts it

    Fix billing.py so that its money is exact, for amounts given as a Decimal or as whole rupees (an int). after_coupon(price) gives the price with 10% off, to the paisa (after_coupon(225) is 202.50). gst(amount) gives 5% of the amount, rounded to the paisa with halves going up: 202.50 gives 10.13, 184.50 gives 9.23, 123.40 gives 6.17, 300 gives 15.00. invoice_total(bills) gives the exact sum of each bill plus its gst.

    Each result is a Decimal with two places, like Decimal("10.13"); invoice_total of no bills is 0. The check imports a fresh copy of your billing.py, uses its own bills, many of them with exact half paise, and an invoice of 40 bills. Python's round() on a Decimal rounds halves to the even paisa, and a Decimal made from a float keeps the float's error: both still lose paise.

  3. Task 3

    test_billing.py: a test that would have caught it

    Write a pytest file, test_billing.py, that would have caught this bug before the first short GST. The check runs your file four times: beside the original billing.py, where at least one test must fail; beside the fixed billing.py, where every test must pass; beside a wrong fix that uses Decimal but Python's round(), and beside a wrong fix that works in floats and makes a Decimal of the result, where at least one test must fail each time.

    So test GST on exact half paise and compare with Decimal values made from strings: ₹225 with DABBA10 (202.50) has GST 10.13, and ₹205 with DABBA10 (184.50) has GST 9.23. No real waiting: each test must finish within 3 seconds.

What you will learn

  • repro.py: a paisa short on a ₹225 bill
  • billing.py: every paisa where the GST rule puts it
  • test_billing.py: a test that would have caught it

More in Everyday traps

Build "Float money" yourself

DabbaDrop runs on Python, and Kunal, its senior developer, keeps assigning you tickets. Each is a real trap interviews love to ask about. A real editor and terminal in your browser, AI that explains, and a Check that says when it works.

The first 2 chapters of Python traps are free. This one comes with the all labs plan.