Fibonacci code question | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Fibonacci code question

I used this code to solve the fibonacci task in Python core num = int(input()) def fibonacci(n): #complete the recursive function if n <= 1: return n else: return fibonacci(n-1) + fibonacci(n-2) for i in range(num): print(fibonacci(i)) Can someone explain why the whole output sequense turns negative (-1, -1,...) If n <= 1 is n<=0 instead? Was stuck on this for a while.

13th Dec 2021, 8:43 PM
Mauriz Brunell
2 Answers
+ 2
Just look at Fibonacci(n-2). When you have if n<=0 then for sure you will have -1 with n=1
13th Dec 2021, 8:52 PM
Alexey Kopyshev
Alexey Kopyshev - avatar
0
Thank you! I see it now.
14th Dec 2021, 7:30 AM
Mauriz Brunell