Hello guys I need help.What`s wrong with my code | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Hello guys I need help.What`s wrong with my code

It prints out the fibonacci sequence fir a user specified number. num = int(input()) def fibonacci(n): #complete the recursive function if n<=0: return 0 else: x,y=1,0 for i in range (n): x,y=y,x+y print(x) fibonacci(num-1)

20th Nov 2021, 10:34 AM
Ochola Mark Joshua
Ochola Mark Joshua - avatar
2 Answers
+ 3
Ochola Mark Joshua The point of the exercise is to use recursion to resolve the problem, so using a for loop within the function defeats the purpose of the exercise. Have a look at the following. You will see how to add num to num-1 in a recursive fashion. Your base case was good, but your for loop bypassed it. num = int(input()) def fibonacci(n): #complete the recursive function if n<=0: return 0 else: return n + fibonacci(n-1) print(fibonacci(num))
20th Nov 2021, 11:00 AM
Rik Wittkopp
Rik Wittkopp - avatar
+ 3
fibonacci(num)
20th Nov 2021, 10:50 AM
Simba
Simba - avatar