Consider the following piece of code:
def integ(fncts, propagate, stpSz):
conditions = propagate.copy()
iterator = 0
for i in fncts:
conditions[iterator] = conditions[iterator] + stpSz * i(0)
iterator+=1
return conditions
Where fncts is an array of functions, like this:
f1 = lambda x: x
f2 = lambda x: 2*x
fncts = (f1, f2)
Problem is, the code above works for length(fncts)>1. However, if there is only one function, it fails. How I can make sure the code can be executed if the user inputs only one function?
I’m assuming you mean the user inputs
fncts = f? You can either require the user to always a sequence, sofncts = [f]orfncts = (f,), or check whethercallable(fncts); tuples and lists are not callable.