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

Python Fibonacci module 7

I have a trouble finishing python core module 7 quiz.so I searched the internet for a solution and got @Miel Amer's code and it works, but I still don't understand how this code works, maybe some of you guys can help me? Thank you #code num = int(input()) def fibonacci(n): if n <= 1: return n else: return fibonacci(n-1) + fibonacci(n-2) for number in range(num): print(fibonacci(number))

13th Jan 2023, 3:54 PM
Tian Tama
Tian Tama - avatar
1 Answer
+ 6
Tian Tama , For example take n=5 Fibonacci series for 5 will be , 0 1 1 2 3 First if condition is checked if n value is 0 or 1 It will return that value In that code change if condition as, If n==0 or n==1: ( since negative values are neglected) If the condition fails it will come to else statement fibonacci (n-1) + fibonacci (n-2)===> fibonacci (4)+ fibonacci (3) Like this fibonacci function is called till the value 4 and 3 above becomes 0 or 1.. Then the returned value is printed using for loop ... Hope it is understandable...!
13th Jan 2023, 4:24 PM
Riya
Riya - avatar