Before you keep reading...
Runestone Academy can only continue if we get support from individuals like you. As a student you are well aware of the high cost of textbooks. Our mission is to provide great books to you for free, but we ask that you consider a $10 donation, more if you can or less if $10 is a burden.
Before you keep reading...
Making great stuff takes time and $$. If you appreciate the book you are reading now and want to keep quality materials free for other students please consider a donation to Runestone Academy. We ask that you consider a $10 donation, but if you can give more thats great, if $10 is too much for your budget we would be happy with whatever you can afford as a show of support.
7.8. Boolean Functions¶
We have already seen that boolean values result from the evaluation of boolean expressions. Since the result of any
expression evaluation can be returned by a function (using the
functions can return boolean values. This turns out to be a very convenient way to hide the details of complicated tests. For example:
The name of this function is
isDivisible. It is common to give boolean
functions names that sound like yes/no questions.
False to indicate whether the
x is or is not
We can make the function more concise by taking advantage of the fact that the
condition of the
if statement is itself a boolean expression. We can return
it directly, avoiding the
if statement altogether:
def isDivisible(x, y): return x % y == 0
Boolean functions are often used in conditional statements:
if isDivisible(x, y): ... # do something ... else: ... # do something else ...
It might be tempting to write something like
if isDivisible(x, y) == True:
but the extra comparison is redundant. You only need an
== expression if you are comparing some other type than boolean. (
isDivisible(x, y) == False can also be made more concise as
not isDivisible(x, y)). The following example shows the
isDivisible function at work. Notice how
descriptive the code is when we move the testing details into a boolean function. Try it
with a few other actual parameters to see what is printed.
Here is the same program in codelens. When we evaluate the
if statement in the main part of the program, the evaluation of
the boolean expression causes a call to the
isDivisible function. This is very easy to see in codelens.
Check your understanding
7.8.1. More Unit Testing¶
When we write unit tests, we should also consider output equivalence classes that result in significantly different results.
isDivisible function can return either
False. These two different outputs give us two equivalence classes. We then choose inputs that should give each of the different results. It is important to have at least one test for each output equivalence class.
Extend the program …
Starting on line 7, write two unit tests (that should pass), one for each output equivalence class.
This workspace is provided for your convenience. You can use this activecode window to try out anything you like.