Builder tools

Build a Small Internal Calculator with Claude Code and Check Its Edge Cases

Cihan's view: Give Claude Code a bounded calculator brief, require tests for normal and edge cases, inspect the diff, and check the arithmetic independently before using the tool.

Choose another work task →

What you will make

You will make a small local Python calculator for a fictional service quote. It takes quantity, unit price, and discount percentage, then writes a quote total. The exercise includes tests for zero quantity, a zero discount, and an invalid discount. It does not connect to a database, send a quote, or touch production files.

This is a documentation-based guide with an illustrative example. Claude Code documentation describes a terminal tool that can read a codebase, edit files, run commands, and work across several surfaces. No Claude Code run or generated result is claimed here.

Before you start

You need a disposable project folder, Python, a terminal, and Claude Code installed and available as claude. Checkpoint the folder before you begin. Do not place credentials, customer data, or production files in it.

Create quote-input.txt:

A fictional quote calculator should accept:
- quantity: a whole number greater than or equal to 0
- unit price: a number greater than or equal to 0
- discount percentage: a number from 0 to 100

Formula:
subtotal = quantity * unit price
discount = subtotal * discount percentage / 100
total = subtotal - discount

Example check:
quantity 4, unit price 125, discount 10 produces subtotal 500, discount 50, total 450.

Three steps

1. Ask for a plan before code

From the disposable folder, run:

claude

Paste this request:

Read quote-input.txt and plan a small local Python calculator.

Requirements:
- Create calculator.py and test_calculator.py only.
- Accept quantity, unit price, and discount percentage as command-line inputs.
- Reject negative quantity and unit price.
- Reject discount values below 0 or above 100.
- Use decimal-safe money handling rather than binary floating point for the final total.
- Print subtotal, discount, and total with two decimal places.
- Add tests for the example, zero quantity, zero discount, and invalid discount.
- Do not use the network, credentials, external packages, or destructive commands.
- Do not create or edit files yet. Return the proposed files, validation rules, and test cases.

Review the plan. It should mention the formula, validation rules, files, and test cases before any edit.

2. Request the bounded implementation

If the plan is correct, paste:

Implement only the approved calculator.py and test_calculator.py plan.

Before editing, show the exact files you will create. After editing:
- run the tests,
- show the final files,
- explain how each test maps to a requirement,
- do not add packages or change other files.
Stop if a requirement is ambiguous.

Read the diff. The calculator should not accept a negative price, silently clamp an invalid discount, or round the subtotal before calculating the discount.

3. Check the arithmetic yourself

Use the sample values from quote-input.txt and calculate the expected result independently. Then ask Claude Code:

Review calculator.py against quote-input.txt without editing it.

Check:
- 4 at 125 with a 10 percent discount equals 450.00 total.
- zero quantity produces a zero total.
- zero discount leaves the subtotal unchanged.
- discounts below 0 and above 100 fail clearly.
- the source input file is unchanged.

Return a pass or fail for each check and quote the relevant code line. Do not claim a check passed if you did not inspect it.

The output from this step is still an assistant review. Treat it as a prompt for your own inspection, not as independent proof.

Acceptance checks

The artifact passes when only the two approved files exist, the tests cover the four named cases, invalid inputs fail instead of being corrected silently, the example total is 450.00, and the source input remains unchanged. Before adapting it for real work, add tests for your own rounding and currency rules.

Common failures

  • If claude is not found, check the installation and PATH using the official setup instructions. Do not paste a token into a shell command.
  • If the plan proposes extra files or packages, stop and restate the file boundary.
  • If tests pass while the formula is wrong, compare the code with the written equation and hand-check the sample. Passing tests only proves the cases that were written.

Use a disposable folder and fictional values. A calculator that passes a toy example is not financial approval software.

For a file-checking workflow with a different output, see Use Claude Code to check JSON exports before you share them. Get one practical AI workflow each week through The Weekly Verdict.

Sources

About Cihan

Creator and operator focused on practical AI for business professionals. Background and editorial approach →