0
Error in Fibonacci code
Here is my code : num = int(input()) def fibonacci(n): if n == 1: return 1 elif n == 2: return 1 else: return (fibonacci(n-1) + fibonacci(n-2)) for n in range(0,num): print(fibonacci(n)) Can you help me? I also tried to use this code in my pc and it works 😅 In the original code I added if n == 0 - - - > return 0 before if n == 1, but it was wrong there too
3 Réponses
+ 1
Use condition if n<=1: instead of n==1.
n-2 cause to 0 in some cases so it won't stop recursion.
It may indentation errors, hope you can remove..
0
Thank you... I added if == 0 and then if <= 1 instead of if == 1 and it works
0
Instead of 2 if blocks , you can just add
if n<=2:
return 1
Works for all n<=2. (n = 0,1,2)
You're welcome...