A finding from Qubix University
A Python decision that quietly loses £2.43
Reading a shop receipt with int() instead of float() removes £2.43 from an £18.70 basket, and raises no error at all.
| What the till recorded | £18.70 | |
|---|---|---|
| Computed with float() | £18.70 | matches |
| Computed with int() | £16.27 | short by £2.43 |
| The weighed line | 0.347 kg × £7.00 = £2.43 | the entire difference |
Every value read from a file arrives as text, so a quantity has to be converted before it can be multiplied. That conversion is a choice, and one of the two options is wrong here.
Three of these four lines are sold by the unit, and int() handles them perfectly. The fourth is sold by weight. int(float("0.347")) returns 0, so 0.347 kg of it becomes nothing, the line costs nothing, and the basket is short by exactly that line.
Nothing raises. The till says £18.70 and the program says £16.27, and the only way to notice is to check.
Python 3.14: int("0.347") raises, and int(float("0.347")) returns 0.
Taught in Chapter 6 · Values, names and types.
Qubix University is a data-science course being written in the open. Volume 0 is a draft: it has not been through final review, and nothing in it is accredited. The finding above is arithmetic, and stands on its own.