Debugging Functions
The program below plans a dinner for 10 with a function,
amount_for, meant to scale one ingredient of a 4-serving recipe to any number of servings. This lesson finds the program's errors one at a time and repairs each one.The recipe uses 3 cups of rice, and the program was meant to print how many cups to cook for 10 people. The function was written first, with a print inside it, and tested with the recipe's own 4 servings. The last line is the dinner.
def amount_for(cups, servings):
print(cups * 4 / servings)
amount_for(3, 4)
print("Cook", amount_for(3, 10), "cups of rice")
Output
Press Run to see what this program prints.
Note: This IDE is for experimenting with the code in this lesson. Your changes are not saved, and refreshing the page puts back the original lesson code.
It prints three lines:
3.0
1.2
Cook None cups of rice
Run the program above to see those three lines. As the lesson finds each error, repair it in the program and run it again.
A logic error
The test line printed 3.0, the recipe's own amount, so the function looked right. The dinner call printed 1.2, less rice for 10 servings than for 4.
That 1.2 could come from the arguments or from the formula. To see which, add an output statement at the top of the body,
print("got", cups, servings). The run then prints got 3 4 before the 3.0 and got 3 10 before the 1.2, so the arguments are right and the error is in the formula.Traced with the dinner's input,
cups equal to 3 and servings equal to 10, the formula cups * 4 / servings is 3 * 4 / 10, or 1.2. It should multiply by the servings wanted and divide by the 4 the recipe serves, and it does the reverse. At 4 servings the two formulas are the same expression, 3 * 4 / 4, so the test couldn't tell them apart.The repair is in the body:
print(cups * servings / 4). The test call still prints 3.0, and the dinner call now prints 7.5. The output statement has done its job, so it's removed.Python never reported the backward formula. The program ran to the end and printed a wrong amount.
Printing instead of returning
The third line, still
Cook None cups of rice, comes from the dinner call. Python evaluates each argument of print before print runs, so amount_for(3, 10) runs first. The body prints 7.5, the second line of output, and reaches its end with no return statement. The call's value is therefore None, and print shows it between Cook and cups of rice.The print in the body works like the output statement added earlier: it shows the amount, and it does nothing else. Output statements show values while you look for a bug. They don't change what a function returns, so adding more of them can't repair this one.
The repair is to return the amount,
return cups * servings / 4, and leave the printing to the caller, whose print already puts the number in a sentence. The caller knows what the number is for, so printing belongs there. The program now prints one line:Cook 7.5 cups of rice
Using the returned value
The 3.0 is gone from the output. The test line still calls
amount_for(3, 4), and the body still runs and returns 3.0, but nothing on that line uses the value.The dinner line uses its value by printing it. The test line now shows nothing, so it becomes
print(amount_for(3, 4)), which prints 3.0, and it's deleted once testing is done.Each call runs the body once. The printing version showed it: two calls printed two numbers, 3.0 and 1.2.
Storing a result once
The plan needs one more line. The pantry holds 6 cups of rice, and the program should print how many more cups to buy. One way calls the function on each line:
print("Cook", amount_for(3, 10), "cups of rice")
print("Buy", amount_for(3, 10) - 6, "more cups")Another stores the amount first:
rice = amount_for(3, 10)
print("Cook", rice, "cups of rice")
print("Buy", rice - 6, "more cups")
Both print
Cook 7.5 cups of rice and then Buy 1.5 more cups, so both are correct. The subtraction needs the number itself, which the printing version never handed back. The second program stores it once in rice, then prints it and subtracts from it.The two programs don't do the same work. The first calls
amount_for(3, 10) twice, and each call runs the body, even with the same arguments, since Python doesn't remember the first answer. Here the extra run costs a multiplication and a division. In a function whose body does more, storing the result once saves all of that work on every reuse.