159k views
2 votes
Write a function (funception) that takes in another function func_a and a number start and returns a function (func_b) that will have one parameter to take in the stop value. func_b should take the following into consideration the following in order: Takes in the stop value. If the value of start is less than 0, it should exit the function. If the value of start is greater than stop, apply func_a on start and return the result. If not, apply func_a on all the numbers from start (inclusive) up to stop (exclusive) and return the product.

1 Answer

0 votes

Answer:

The code for this question can be defined as follows:

def funception(func_a, start):#define a method funception that accept two parameter

def func_b(stop): #define a method func_b that accept one parameter

if start < 0:#define if block that checks start parameter value less then 0

return #Use return keyword

if start > stop:#define if block that checks start parameter value greater then stop parameter value

return func_a(start)#use return keyword to call func_a method

else:#define else block

product = 1#define a variable product and assign a value 1

for i in range(start, stop):#define for loop to calculate the value

product= product * func_a(i)#multiply the method value in product variable

return product#return prod value

return func_b#return func_b value

def func_a(num):#define a method func_a that accept a num variable as a parameter

return num + 1#use return keyword by increment num value by 1

Step-by-step explanation:

In the above python program code, a method "funception" is defined that accepts two parameters that are "func_a and start", in which "func_a" is a method name, that accepts a "num" variable in its parameter and returns its one increment value.

  • Inside the method, another method that is "func_b" is defined that accepts a "start" variable in its parameter and uses it if block to check "start and stop" values.
  • If the start value is less then 0 it will exit the program. If the start value is less then stop value so, it will return the "func_a" value.
  • In the else part, a "product" variable is defined that multiplies the method "func_a" value and returns its value.
User Mobie
by
5.1k points