+ 1
help please
Could someone explain me please why my code doesnt do the fibonacci sequence well and the other yes. My code: n=int(input()) def fibonacci(n): if n<2: return n else: #fn=fn-1 + fn-2 return fibonacci(n-1)+fibonacci(n-2) print(fibonacci(n)) Other: n=int(input()) def fibonacci(n): if n<2: return n else: #fn=fn-1 + fn-2 return fibonacci(n-1)+fibonacci(n-2) for x in range(n): print(fibonacci(x))
5 Answers
0
You aren't using the for loop on the print funtion
0
wait so that is necessary?i mean the for loop
0
Not at all, that is just a variable, that can be anything.. but your code doesn't have a for loop
0
In code, you are finding n'th number in the Fibonacci sequence . Not total sequence.
In the 2nd code, also doing same but for 0 to n-1 times , by loop.
For printing sequence, no need of recursive calls, use just a loop in function (as in the way you commented..).
Hope it helps..
0
Ohhh thank you very much for the explain