Make a function and test its edges
Separate a reusable calculation from its inputs and test normal, zero and invalid cases.
In this lesson
- It starts with a question. Must a reusable cost calculation contain one fixed quantity forever?
- The idea you will find. A function accepts inputs, returns a result and needs a stated contract. — For this notebook, line_total returns price × quantity. Its caller first checks that price is nonnegative and quantity is a nonnegative whole number. A return value can be used elsewhere; merely displaying a number does not return it.
- How you learn it. By doing: a number line, matching.
- Length. 8 short steps. Progress saves on this phone.
Part of Python market notebook
Follow a fictional market notebook through inputs, totals, data structures and tested functions. Complete every activity inside FijiLearn.