+ 1

Why my code does not work?

Here is my code https://code.sololearn.com/cGgzHqn6YYkh/?ref=app I can't understand what's my mistake....

16th Sep 2021, 7:10 PM
Kind_Cat
Kind_Cat - avatar
19 Answers
17th Sep 2021, 9:30 PM
Christian Duah Marfo
Christian Duah Marfo - avatar
+ 3
Kind_Cat Maybe this adjustment to your code might help to explain Add up the nums and compare to result # Created by Kind_Cat num = int(input()) print(num,'\n') def fibonacci(n): if n == 0: print(0) return 0 elif n == 1: print(1) return 1 #print (fibonacci(n-1)+fibonacci(n-2)) return fibonacci(n-1)+fibonacci(n-2) print(fibonacci(num))
16th Sep 2021, 10:08 PM
Rik Wittkopp
Rik Wittkopp - avatar
+ 3
Christian Duah Marfo well done, now can be easily redone in a recursive way ☺️
17th Sep 2021, 11:21 PM
Solo
Solo - avatar
+ 2
I updated my previous answer for more clarification.
17th Sep 2021, 1:03 AM
Arsalan [Inactive]
Arsalan [Inactive] - avatar
+ 2
Try this, it can be helpful def fibonacci(n): if n == 0: return 0 elif n == 1: return 1 else: return fibonacci(n-1)+fibonacci(n-2) num = int(input()) for i in range(num): print(fibonacci(i))
17th Sep 2021, 3:27 PM
Kittu
Kittu - avatar
+ 1
fibonacci(n) will keep calling it with same value . Maybe you meant to do n-1 and n-2
16th Sep 2021, 7:11 PM
Abhay
Abhay - avatar
+ 1
You're Russian too!
16th Sep 2021, 8:29 PM
Kind_Cat
Kind_Cat - avatar
+ 1
This can not be done with a recursive pure function. recursive: https://code.sololearn.com/czbSIG9KoN7H/?ref=app pure: https://code.sololearn.com/cK02jcB2GUjL/?ref=app edited for clarification : This function has to print a sequence of numbers, not just one number. In a recursive function with two recursion at one call, like this, the function may be executed twice for one input. So there has to be an out-of-function variable that determines if one output has already been printed. for example, f(6) calls f(5) and f(4); And f(5) calls f(4) separately. So if the function prints somthing for f(4), it prints it twice. One solution is that our function have one output and be called multiple times. but it's no longer pure for our purpose.
16th Sep 2021, 10:31 PM
Arsalan [Inactive]
Arsalan [Inactive] - avatar
0
Abhay i try it too. But it doesn't work
16th Sep 2021, 7:12 PM
Kind_Cat
Kind_Cat - avatar
0
And I make it in that code now. But it still doesn't work
16th Sep 2021, 7:14 PM
Kind_Cat
Kind_Cat - avatar
0
Agree, it's hard to understand the process of recursion versus iteration ☺️ My suggestion: "1. Create a fibonacci function using a while loop. 2. Create any simple recursive function, for example, print 1,2,3". Post your solutions here Then together we will try to write a recursive function fibonacci 😉
16th Sep 2021, 8:24 PM
Solo
Solo - avatar
0
Maybe, it will be easier... But I want to understand it
16th Sep 2021, 8:25 PM
Kind_Cat
Kind_Cat - avatar
0
So you will understand after passing this path ☺️
16th Sep 2021, 8:28 PM
Solo
Solo - avatar
0
But if you get a straight answer, you will never learn how to create a recursive function correctly.
16th Sep 2021, 8:33 PM
Solo
Solo - avatar
0
Да! ☺️
16th Sep 2021, 8:33 PM
Solo
Solo - avatar
0
Revision chapters to understand
18th Sep 2021, 3:03 PM
Aman Rawat
0
I read all comments now. But nothing I see here help me
18th Sep 2021, 10:08 PM
Kind_Cat
Kind_Cat - avatar
- 1
num = int(input()) def fibonacci(n): if n == 0: print(0) #you should not use print here return 0 elif n == 1: print(1) # no print required return 1 else: # this else must be required , otherwise it will run with every iteration and become endless loop return fibonacci(n-1)+fibonacci(n-2) print(fibonacci(num)) here is edited code https://code.sololearn.com/cD8Nmc10y4mH/?ref=app
18th Sep 2021, 6:48 PM
Kothooru Narendar
Kothooru Narendar - avatar