Respuesta :
Question:
The is_positive function should return True if the number received is positive, otherwise it returns None. Can you fill in the gaps to make that happen?
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:
The code above has been written in Python.
Now, let's explain each of the lines of the code;
Line 1: defines a function called is_positive which takes in a parameter number. i.e
def is_positive(number):
Line 2: checks if the number, supplied as parameter to the function, is positive. A number is positive if it is greater than zero. i.e
if (number > 0):
Line 3: returns a boolean value True if the number is positive. i.e
return True
Line 4: defines the beginning of the else block that is executed if the number is not positive. i.e
else:
Line 5: returns a string value "None" if the number is not positive. i.e
return "None"
All of these put together gives;
===============================
def is_positive(number):
if (number > 0):
return True
else:
return "None"
================================
An example test of the code has also been given where the function was been called with an argument value of 6 and -7. The results were True and None respectively. i.e
print(is_positive(6)) = True
print(is_positive(-7)) = None
Following are the python program to check input number is positive or negative:
Program:
def is_positive(n):#defining the method is_positive that takes one variable in parameter
if (n > 0):#defining if block that check input number is positive
return True#return boolean value that is True
else:#else block
return "None"#return string value
n=int(input("Enter number: "))#defining n variable that input a number
print(is_positive(n))#using print method that calls and prints method return value
Output:
please find the attached file.
Program Explanation:
- Defining the method "is_positive" that takes one variable "n" in the parameter.
- Inside the method, if conditional block is defined that checks the input number is positive, if it's true, it will return a boolean value that is "True".
- Otherwise, it will go to the else block, where it will return a string value that is "None".
- Outside the method, the "n" variable is declared to be an input number.
- After the input value, a print method is used that calls the "is_positive" method and prints its return value.
Find out more about the method here:
brainly.com/question/5082157
