I am working on a Python lesson about functions. I created a simple square root function:
def sqrt(x):
return x ** 0.5
print(sqrt(9))
Now, I wanted to create a function which can call sqrt (as a parameter) twice:
def add_function(func, x):
return((func, x) + (func, x))
print(add_function(sqrt, 9))
However, this gets a syntax error. In my mind, the add_function
should return the sqrt
function with the argument 9 added to the same.
I am looking for a some enlightenment.
I suspect this is what you want: use the func variable to call whatever function is passed in.
def sqrt(x):
return x ** 0.5
def add_function(func, x):
return func(x) + func(x)
print(sqrt(9))
print(add_function(sqrt, 9))
Output:
3.0
6.0