Your print_line function prints, and that is all. Its result goes to the screen, where the rest of your program cannot use it. This lesson writes a function that hands its answer back, so you can store it and use it more than once.
The concept
Here is your tracker as the last lesson left it. Run it:
Look at your expense loop. The counting is tangled into the printing, and it only ever counts against 3,000. Now run this:
count_over contains the counting pattern from the accumulation lesson. Its last line is new: return found. return ends the function and hands the value of found back to the line that called it.
So count_over(expenses, 3000) becomes the number 2, and large = count_over(expenses, 3000) stores it. The second call counts against 1,000 instead, and gives back 4. One function, two questions, two answers your program can keep.
Now the wall. This trips up almost everyone. Run this:
Two lines appeared: 2, then Large spends: None. The function printed its count, so 2 reached the screen. But it returned nothing, so nothing came back to be stored. A function that ends without return gives back None, Python's value for nothing. That None is what large holds.
Printing shows a value to a person. Returning gives a value to your program. When the rest of your program needs the result, use return.
The syntax
def count_over(amounts, limit):
found = 0
for amount in amounts:
if amount > limit:
found = found + 1
return foundreturn, then the value to hand back.- It sits inside the function's block. Here it is indented four spaces, level with the
for, so it runs once, after the loop has finished. returnends the function. Any lines after it in the block never run.
The call works like a value:
large = count_over(expenses, 3000)Names created inside a function, like found, belong to that function. They exist while it runs, and are gone once it returns. The value comes out through return, and nowhere else.
A minimal working example
def count_over(amounts, limit):
found = 0
for amount in amounts:
if amount > limit:
found = found + 1
return found
expenses = [1500, 2500, 3500, 800, 4200]
large = count_over(expenses, 3000)
print(f"Expenses over ₦3,000: {large}")Output: Expenses over ₦3,000: 2.
Common mistakes
You print instead of returning. The demo above shows one symptom: None where a value should be. Here is the other one:
TypeError: unsupported operand type(s) for +: 'NoneType' and 'int'double(1500) printed 3000, then gave back None. Python cannot add 100 to nothing. NoneType is the type of None. When you see it in an error, look for a function that prints its result instead of returning it.
You use a name from inside the function. Run this:
NameError: name 'found' is not defined. Did you mean: 'round'?Ignore the suggestion: round is a built-in function with a similar spelling, and it has nothing to do with this. The function ran, and returned 2, but nothing stored it. found disappeared when the function ended. Store the returned value in a name of your own: large = count_over(...).
You write return outside a function. It gives SyntaxError: 'return' outside function. There is nothing to hand the value back to. Check the indentation: return must be inside a def block.
Try it yourself
You will replace the counter inside your loop with a function, and use it twice.
-
Run the starting code below. Note the number on your
Expenses over ₦3,000line. -
Under your
print_linefunction and its two blank lines, addcount_over, followed by two blank lines:def count_over(amounts, limit): found = 0 for amount in amounts: if amount > limit: found = found + 1 return found -
Take the counting out of your expense loop. Delete the
large_spends = 0line above thefor, and the two-lineifinside it. The loop should only print each amount again. -
Under your
left = budget - totalline, addmid_spends = count_over(expenses, 1000)andlarge_spends = count_over(expenses, 3000). Above yourExpenses over ₦3,000line, addprint(f"Expenses over ₦1,000: {mid_spends}"). Run it. Confirm the ₦3,000 line shows the same number as in step 1, and the new ₦1,000 line shows how many amounts are over 1,000. With the amounts given, that is4. -
Break it on purpose: in
count_over, changereturn foundtoprint(found). Run it. Confirm two stray numbers appear at the very top of the output, and bothExpenses overlines now sayNone. -
Fix it: change it back to
return found. Run it. Confirm both lines show numbers again.
Your tracker now asks the same question with two different limits, using one function. That completes Part 2. The checkpoint next shows what your program can now decide, and what it still cannot describe.