Question:
The function is_positive should yield True if the supplied number is positive; otherwise, it gives None. Can you fill in the blanks accordingly?
def is_positive(number):
if _____ :
return _____
Answer:
def is_positive(number):
if (number > 0):
return True
else:
return "None"
---------------------------------------------------------------------------------
Code Test and Sample Output:
print(is_positive(6))
>> True
print(is_positive(-7))
>> None
----------------------------------------------------------------------------------
Explanation:
This segment of code is composed in Python.
To explain it further, let's break down the content of each line;
Line 1: Creates a function identified as is_positive which takes in a variable number.i.e
def is_positive(number):
Line 2: Evaluates if the given number is positive; a number qualifies as positive if it exceeds zero. i.e
if (number > 0):
Line 3: Produces a boolean result of True when the input number is positive. i.e
return True
Line 4: Initiates the else block to be executed in case the input number isn’t positive. i.e
else:
Line 5: Returns the string "None" when the number is not positive. i.e
return "None"
When combined, this results in;
===============================
def is_positive(number):
if (number > 0):
return True
else:
return "None"
================================
An instance test for the code provided an argument of 6 and -7, resulting in True and None respectively. i.e
print(is_positive(6)) = True
print(is_positive(-7)) = None