+ 1

Fibonnacci python need help

# can anyone explain me why ** return n ** num = int(input()) def fibonacci(n): if n <=1: return n else: return fibonacci(n-1)+fibonacci(n-2) print(fibonacci(num))

10th Jan 2022, 5:15 PM
SẊm Lâm Bảo Quý
SẊm Lâm Bảo Quý - avatar
3 Answers
+ 3
The function actually always returns 0 or 1, and the result is the total sum, it is very difficult for me to explain this, but I hope this will help you : (ah, your case would be the first code, remove the triple double quotes to test) https://code.sololearn.com/cNjnzUF2yWHu/?ref=app
10th Jan 2022, 5:22 PM
CGM
CGM - avatar
+ 1
return n will return 1 or 0 in your function, and it's the condition that stop the recursive fonction from calling itself. You have fibonacci(n-1)+fibonacci(n-1) inside the function so it will stop after getting 0 or 1 and that's the result you want to add to the calculated values.
10th Jan 2022, 5:28 PM
YoPycode
+ 1
Thank you, I got it
10th Jan 2022, 5:29 PM
SẊm Lâm Bảo Quý
SẊm Lâm Bảo Quý - avatar