- 1

Python Recursion Help

I’m working on the Fibonacci sequence project in the Python tutorial. I think I understand recursion well enough, but I can’t figure out how to print iterations of the sequence without using a for loop. Is there supposed to be a way to do that? Here’s my code so far: ~~~~~ num = int(input()) def fibonacci(n): #complete the recursive function if n <= 1: return n else: print(fibonacci(n - 1) + fibonacci(n - 2)) fibonacci(num) ~~~~~ This just gives me a bunch of errors. Thanks.

8th Jun 2021, 5:03 PM
Jorian
Jorian - avatar
2 Respostas
+ 3
Jorian , please post your code you have done so far here, so that we can get an idea about it. thanks!
8th Jun 2021, 6:03 PM
Lothar
Lothar - avatar
+ 2
I added it to the post. If I change the last line to a for loop like: for i in range(num): print(fibonacci(i)) And change print in the fibonacci function to return, it works perfectly.
8th Jun 2021, 6:12 PM
Jorian
Jorian - avatar