您可以将解决方案用作 Martijn 的海报,但您也可以使用if/else Python 构造来调用您的simple 或compound 计算例程
考虑到复合兴趣例程应该采用额外的参数n,即感兴趣的频率计算,因此您可以根据参数长度切换函数调用。
此外,您的驱动程序例程应该接受可变参数以接受这两种函数的参数
>>> def calc(*args):
if len(args) == 3:
return calc_simple(*args)
elif len(args) == 4:
return calc_compund(*args)
else:
raise TypeError("calc takes 3 or 4 arguments ({} given)".format(len(args)))
>>> def calc_compund(*args):
P, r, n, t = args
print "calc_compund"
#Your Calc Goes here
>>> def calc_simple(*args):
P, r, t = args
print "calc_simple"
#Your Calc Goes here
>>> calc(100,10,2,5)
calc_compund
>>> calc(100,10,5)
calc_simple
>>> calc(100,10)
Traceback (most recent call last):
File "<pyshell#108>", line 1, in <module>
calc(100,10)
File "<pyshell#101>", line 7, in calc
raise TypeError("calc takes 3 or 4 arguments ({} given)".format(len(args)))
TypeError: calc takes 3 or 4 arguments (2 given)
>>>