Originally Posted by Mekire
Use the # button in the tool bar for code tags. After pasting your code, highlight it and hit that button.
Anyway, this is of course trivial if you use a class with an identical method to using a global, except with a class attribute instead.
With just a function however it is a bit tricky. This seems to do the trick:
python Code:
def fib(x,num_calls=0):
"""
assumes x an integer > 0
returns fibonacci of x
"""
if x == 0 or x == 1:
return 1,num_calls+1
else:
a,num_calls = fib(x-1,num_calls)
b,num_calls = fib(x-2,num_calls)
return a+b,num_calls+1
if __name__ == "__main__":
x = 5
print("Fib({}) = {}\nCalls = {}".format(x,*fib(x)))
-Mek