A function that returns True or False inside an if

Tip: Choose Save as PDF in the print dialog to download. Use A4 paper size.

A function that returns True or False inside an if

Name:
Date:
Class:

Quick Questions

Shade in the A or B circle for each question.

1. Which function body correctly checks if a number is even and returns a bool?
A def is_even(n): return n % 2 == 0
B def is_even(n): return n % 2 = 0
2. Which if line calls a yes/no function directly in the condition?
A if is_even(n):
B if is_even(n) == True or False:
3. What does is_even(8) give back?
A True
B 8
4. Which name is best for a function that answers a yes/no question about a number?
A even_number_stuff
B is_even
5. n is 7. What happens with this code? if is_even(n): print('Even')
A Even is printed
B Nothing is printed

Match the Pairs

Draw a line connecting each item on the left to its match on the right.

Left columnRight column
1return n % 2 == 0 ACalls the function inside the if condition; the indented code runs only when it returns True
2if is_even(n): BThe brackets and argument are missing, so the function is not called. is_even is just the function itself, not its True/False answer.
3A function that returns a bool can be called directly in an if condition, with no extra variable CThe function prints the result instead of returning it, so it gives back None and the if never sees True.
4A good name for a yes/no function starts with is_ so the if line reads like a question DGives back True when n is even and False when it is odd, so the function can be used as a yes/no check
5if is_even == True: print("Even") Eif is_adult(age): print("Welcome")
6def is_even(n): print(n % 2 == 0) Fdef is_even(n): return n % 2 == 0

Definitions

if is_even(n):

Fill in the Blanks

Complete each line of code by filling in the missing parts.

1.
___ is_even(n): return n % 2 == 0
2.
if _______(n): print("n is even")
3.
__ is_even(n): print("Even")
4.
while ___ is_even(n): n = n + 1
5.
age = 20 if ________(age): print("Welcome")

Spot the Error

Shade in the letter of the line that contains an error.

Question 1
A def is_even(n)
B return n % 2 == 0
C num = 8
D if is_even(num):
E print("Even")
Question 2
A def is_even(n):
B return n % 2 == 0
C n = 6
D if is_even[n]:
E print("Even")
Question 3
A def is_adult(age):
B return age >= 18
C if is_adult:
D print("Adult")
Question 4
A def is_even(n):
B result = n % 2 == 0
C print(result)
D if is_even(10):
E print("Even")
Question 5
A def is_even(n):
B return n % 2 = 0
C for i in range(1, 5):
D if is_even(i):
E print(i)

Answer Key

A function that returns True or False inside an if
Section A: Quick Questions
Q1
A — def is_even(n): return n % 2 == 0
Q2
A — if is_even(n):
Q3
A — True
Q4
B — is_even
Q5
B — Nothing is printed
Section B: Match the Pairs
Q1
return n % 2 == 0 → Gives back True when n is even and False when it is odd, so the function can be used as a yes/no check
Q2
if is_even(n): → Calls the function inside the if condition; the indented code runs only when it returns True
Q3
A function that returns a bool can be called directly in an if condition, with no extra variable → if is_adult(age): print("Welcome")
Q4
A good name for a yes/no function starts with is_ so the if line reads like a question → def is_even(n): return n % 2 == 0
Q5
if is_even == True: print("Even") → The brackets and argument are missing, so the function is not called. is_even is just the function itself, not its True/False answer.
Q6
def is_even(n): print(n % 2 == 0) → The function prints the result instead of returning it, so it gives back None and the if never sees True.
Section C: Definitions
Q1
if is_even(n): — Calls a function that returns True or False right inside an if condition. The indented code runs only when the function returns True, so no extra variable is needed.
Section D: Fill in the Blanks
Q1
def is_even(n): return n % 2 == 0
Q2
if is_even(n): print("n is even")
Q3
if is_even(n): print("Even")
Q4
while not is_even(n): n = n + 1
Q5
age = 20 if is_adult(age): print("Welcome")
Section E: Spot the Error
Q1
A — A function definition line must end with a colon. It should be def is_even(n):
Q2
D — A function is called with round brackets, not square brackets. It should be if is_even(n):
Q3
C — The function is never called because the brackets and the argument are missing. It should be if is_adult(20):
Q4
C — The function prints the answer but gives nothing back, so the if gets None and never runs. It should be return result.
Q5
B — A single = assigns a value. To compare, use ==. It should be return n % 2 == 0