This is the expected output:
Output for fib(5) =>
Iteration 0: 1
Iteration 1: 1
Iteration 2: 2
Iteration 3: 3
Iteration 4: 5
Iteration 5: 8
This was my output
def Iteration(xn):
if xn <= 0:
print("Iteration(0):1")
return 1
else:
print(f'Iteration({xn}):{xn -1 xn - 2}')
return Iteration
Iteration(5)
CodePudding user response:
This stanza:
return Iteration
Returns a function (the function Iteration
), not a value. You want to instead evaluate the Iteration
function:
return Iteration(xn - 1)
This will return the value calculated by the iterative step.
CodePudding user response:
Instead of returning the function, you need to call the function
Replace
return Iteration
with
return Iteration(xn-1) Iteration(xn-2)
And change your if statements to
if xn == 0:
return 0
elif xn == 1:
return 1
This should fix your problem
So full code:
def Iteration(xn):
if xn == 0:
return 0
elif xn == 1:
return 1
else:
return Iteration(xn - 1) Iteration(xn - 2)