+ 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))

6th Mar 2021, 11:21 PM
Yami Francø
Yami Francø - avatar
5 Answers
0
You aren't using the for loop on the print funtion
6th Mar 2021, 11:25 PM
LordHill
LordHill - avatar
0
wait so that is necessary?i mean the for loop
6th Mar 2021, 11:26 PM
Yami Francø
Yami Francø - avatar
0
Not at all, that is just a variable, that can be anything.. but your code doesn't have a for loop
7th Mar 2021, 12:20 AM
LordHill
LordHill - avatar
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..
7th Mar 2021, 7:23 AM
Jayakrishna 🇮🇳
0
Ohhh thank you very much for the explain
7th Mar 2021, 7:58 PM
Yami Francø
Yami Francø - avatar