A Worked Python Program from Input to Result

Calculate the cost of three orders from compact text records and use the result to practice tracking intermediate state and validation.

Source
Anbloq
Language
English
Created
Updated
Chapters
4
Reading time
2 min
In this article4 chapters
  1. Read the records
  2. Check the representation and assumptions
  3. Walk through conversion and accumulation as separate operations
  4. Build tests for the cases that share a happy result

Read the records

Each row contains a quantity and a unit price as integers separated by a comma. The total is the sum of the individual products. With rows 2,7, 1,5 and 3,4, the products are fourteen, five, and twelve. A running accumulator visits totals fourteen, nineteen, and thirty-one.

Check the representation and assumptions

rows = ["2,7", "1,5", "3,4"]
total = 0
for row in rows:
    quantity_text, price_text = row.split(",")
    total += int(quantity_text) * int(price_text)
print(total)

The example expects exactly two valid fields per row. A row with three fields fails during unpacking. A row of 2,six unpacks and then fails during conversion. A testable set of inputs covers successful data and those two failure stages independently.[1][2]

Walk through conversion and accumulation as separate operations

For the row "2,7", splitting yields the strings "2" and "7". Converting creates integer values two and seven. Only then does the product become fourteen. The next row contributes five and the last twelve. Read the code in that order and write the total after each row: fourteen, nineteen, thirty-one. Retaining only the total is sufficient for the required result. A different interface asking for subtotals would keep or emit each line's product as well. See accumulator invariants.

That invariant lets you check the algorithm with more than one example. Before reading a row, the accumulator equals the sum of the products for completed rows. A successful iteration adds exactly one product. At completion, all rows have contributed once. With no input rows, a zero total follows directly from initialization if the interface permits empty input.

Build tests for the cases that share a happy result

Try a record "0,7" under two policies: zero quantity allowed and positive quantities required. The first expects no added cost, while the second expects a domain error. The conversion alone cannot distinguish those policies. Try another record containing an extra comma. It fails before checking the numeric contract. See CSV with embedded commas when the row format grows to support names containing separators.

To use this routine in a larger program, return the total from a calculation and let the caller decide where to display it. A test can then assert the total independently of output formatting, and another can check the full command behavior.

References

  1. Python Software Foundation, Python Language Reference: Execution Model ↩
  2. Python Software Foundation, Python Language Reference: Expressions ↩