+ 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))
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
+ 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.
+ 1
Thank you, I got it