I have to have run three functions: 1 & 2 can do something and 3 does either 1 or 2 depending on a bool's value. Function 1 & 2 work fine and return what I want when I type function1(10) in the console. Don't need to print, just return. The if clause in function3 seems to work when I put print statements in it, but doesn't return anything.
def function1(n)
result1 = do things to (n)
return result1
def function2(n)
result2 = do other things to (n)
return result2
def function3(n, aboolvariable=True)
if aboolvariable:
function1(n)
else:
function2(n)
CodePudding user response:
The if clause in function3 seems to work when I put print statements in it, but doesn't return anything. but doesn't return anything
Function to be able return something, Simply use return
keyword before the thing you want to return in your example function1
if aboolvariable
True else return function2
.
def function3(n, aboolvariable=True)
return function1(n) if aboolvariable else return function2(n)
Don't forget to invoke the Function function3
by calling it inside print:
print(function3(1)) #which call func1 by default aboolvariable=True
print(function3(1, False)) #call func2 since second Argument is False
CodePudding user response:
In funtion3
you dont return values only calling them, you need to return
those results:
def function3(n, aboolvariable=True)
if aboolvariable:
return function1(n)
else:
return function2(n)
also you can use isinstance(n, bool)
instead of passing aboolvariable
parameter to your function
CodePudding user response:
Right now function3 simply calls func1 or func2 based on your boolean.
In order for func3 to return the result of the selected function you must add a return
statement to it.
tldr: add a return
statement to function3
def function3(n, aboolvariable=True)
if aboolvariable:
return function1(n)
return function2(n)