Function 编写异常函数
我目前正在一个在线学习平台上学习,我的代码必须通过测试用例(包含在下面) 问题是: 编写一个高阶函数exception\u函数,它将返回一个包含异常的函数。exception_函数应该接受函数f(x)、整数输入和整数输出,并返回另一个函数g(x)。g(x)的输出应该与f(x)相同,只是当x与整数输入相同时,将返回输出 例如,假设我们有一个函数sqrt,它返回参数的平方根。使用new_sqrt=exception_函数(sqrt,7,2),我们得到new_sqrt,其行为类似于sqrt,除了new_sqrt(7),其中返回值2 下面是答案模板Function 编写异常函数,function,exception,higher-order-functions,Function,Exception,Higher Order Functions,我目前正在一个在线学习平台上学习,我的代码必须通过测试用例(包含在下面) 问题是: 编写一个高阶函数exception\u函数,它将返回一个包含异常的函数。exception_函数应该接受函数f(x)、整数输入和整数输出,并返回另一个函数g(x)。g(x)的输出应该与f(x)相同,只是当x与整数输入相同时,将返回输出 例如,假设我们有一个函数sqrt,它返回参数的平方根。使用new_sqrt=exception_函数(sqrt,7,2),我们得到new_sqrt,其行为类似于sqrt,除了new
from math import *
def exception_function(f, rejected_input, new_output):
"""Your code here"""
pass
#################
#DO NOT REMOVE#
#################
new_sqrt = exception_function(sqrt, 7, 2)
测试用例:
新标准(9)-预期答案3
新标准(7)-预期答案2
以下是我不确定的地方。
def exception_function(f, rejected_input, new_output):
def inner_function(x):
if x==rejected_input:
return new_output
else:
return f(x)
return inner_function
new_sqrt = exception_function(sqrt, 7, 2)