SIMULATION
Fix the missing return statement in this function that should return whether a number is even.
def is_even(number):
if number % 2 == 0:
True
else:
False
Suggested Answer: A
Get Premium Foundations of Programming (Python) (E010) Questions
as Interactive Web-Based Practice Test or PDF
Real Exam Format
Detailed Explanations
Instant Access
Get Your Practice Test
==========
Step 1: The expression number % 2 == 0 checks whether the number is even.
Step 2: The original code writes True and False, but it does not return them.
Step 3: A function must use the return statement to send a value back to the caller.
Correct code:
def is_even(number):
if number % 2 == 0:
return True
else:
return False
Simplified correct code:
def is_even(number):
return number % 2 == 0
Example:
print(is_even(4))
print(is_even(7))
Output:
True
False
4 comments · newest first
LE
Leonie
38 minutes ago
I might be confused, but I feel like the function should return the result of the condition check directly.
NA
Nada
5 days ago
I'm not entirely sure, but I think we need to return `True` and `False` directly instead of just writing them.
TR
Tran
10 days ago
Wasn't there a practice question about returning boolean values? I feel like this is similar.
IS
Isabelle
16 days ago
I think I remember that we need to use the `return` keyword to actually send back the value from the function.