Why does a function that prints instead of returns hand back a mysterious None — and how does a function live in memory as an object bound to a name?
Short drills on what this video just taught. Write the code, run the checks, and reveal the answer only if you are stuck.
Here is the specification. Write a function div_by(n, d), where n and d are integers greater than 0, that gives back True when d divides n evenly and False when it does not. Then call div_by(80, 4) and store the value it hands back in divides.
Hint: % gives the remainder of a division, == compares two values, and return (not print) is what hands a value back to the caller.
The stray None in div_by
Write both bodies of div_by, the one printing True/False inside the function and the one returning them, and state that a function with no return gives back None.
The flipped-remainder bug in div_by
Record the two test cases that disagreed with the expected False and True, the wrong condition d % n == 0, and the correction to n % d == 0.
A function object bound to a name
Compare a = 3 binding a variable with def binding the name even to a function object whose body Python leaves unexamined, then bind a, b, c to three call results.
A call dropped into a larger program
Write the top-level loop over the numbers 1 up to 10 that tests if is_even(i) and prints each number labelled even or odd, with no enclosing def.