Fibonacci on python | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 3

Fibonacci on python

num = int(input()) def fibonacci(n): #complete the recursive function if n <= 1: return 0 else: x, y = 0, 1 for i in range(n): x, y = y, x+y print(x) #print("0") fibonacci(num-1) “”” I ran the code multiple times, and the zero won’t show up can someone help me figure out what I’m missing here. “””

3rd Feb 2021, 5:41 AM
Yeriel Vanderhost
Yeriel Vanderhost - avatar
15 Answers
- 2
Just put the print(x) before the x, y =... By the time you print x is already 1
3rd Feb 2021, 5:46 AM
Angelo
Angelo - avatar
+ 5
I wrote this code: num = int(input()) def fibonacci(n): # code goes here if n == 0: return n elif n == 1: print(n - 1) return (0, 1) else: prev = fibonacci(n - 1) a, b = prev a, b = b, a + b print(a) return (a, b) fibonacci(num)
27th May 2021, 3:29 PM
Evgeniy Smelov
Evgeniy Smelov - avatar
+ 5
num = int(input()) def fibonacci(n): #complete the recursive function if n <= 1: return 0 else: x, y = 1, 0 for i in range(n): x, y = y, x+y print(x) fibonacci(num)
29th May 2021, 12:03 PM
Carlito
Carlito - avatar
+ 3
I wanted my solution to be recursive as asked, and not needing an extra loop for the printing task. Here is my best solution yet. num = int(input()) def fibonacci(n): """ RECURSIVELY calculates and prints the n first numbers of the fibonacci sequence n has to be an integer greater than zero fibonacci(n) returns a tupple with the n'th and the (n+1)'th fibonacci number """ if n > 1: prev = fibonacci(n - 1) # create a new tupple # with the last given fibonacci number # and the calculated next one new = (prev[1], prev[0]+prev[1]) print(new[0]) return(new) else: # print the first fibonacci number and # return the two first fibonacci numbers print(0) return (0, 1) fibonacci(num) # OLD VERSION # needs lots of reruns of f(n) to calculate # and print each value separately # #def fibonacci(n): # def f(n): # if n > 1: # return f(n-1) + f(n-2) # elif n == 1: # return 1 # else: # return 0 # # for i in range(n): # print(f(i))
29th Jan 2022, 11:45 PM
P-Y
+ 2
Yeriel Vanderhost Print x before for loop num = int(input()) def fibonacci(n): #complete the recursive function if n <= 1: return 0 else: x, y = 0, 1 print (x) for i in range(n): x, y = y, x + y print(x) fibonacci(num - 1)
3rd Feb 2021, 5:58 AM
A͢J
A͢J - avatar
+ 2
x,y = 0,1 change it to x,y =1,0 and you are good to go
29th May 2021, 12:00 PM
Carlito
Carlito - avatar
+ 1
Carlito, where is recursion in your code?
29th May 2021, 2:58 PM
Evgeniy Smelov
Evgeniy Smelov - avatar
+ 1
num = int(input()) def fibonacci(n): if n <= 1: return 0 else: x, y = 0, 1 for i in range(n): print(x) x, y = y, x+y fibonacci(num) I used the original commentor code but I change it up so that you could print 0 as initially it was calculating before printing that is why, it doesn't print 0. Thank you Yeriel Vanderhost, otherwise I would be stuck.
27th Oct 2022, 3:04 PM
Piyaporn Srimanta
Piyaporn Srimanta - avatar
0
Alguien me pasa el código que si le haga ejecutado por favor ya vi muchos videos y me sale error Someone passes me the code that if I make it executed please I already saw many videos and I get error
13th Jun 2021, 4:26 PM
Garcia Ever
0
num = int(input()) def fibonacci(n): # code goes here if n == 0: return n elif n == 1: print(n - 1) return (0, 1) else: prev = fibonacci(n - 1) a, b = prev a, b = b, a + b print(a) return (a, b) fibonacci(num)
9th Dec 2021, 9:09 AM
Muhammad Ezzat aqil bin Mohd Ezani
0
num = int(input()) def fibonacci(n): if n <= 1: return n else: return fibonacci(n-1) + fibonacci(n-2) for i in range(num): print(fibonacci(i))
5th Apr 2022, 8:36 AM
Grisha
0
num = int(input()) def fibonacci(n): if n<=1: return 0 else: x,y = 0,1 print(x) for i in range(n-1): x,y = y, x+y print(x) fibonacci(num)
13th Jun 2022, 2:33 AM
Mohamed Riyasudeen
0
num = int(input()) fir=0 sec=1 def fibonacci(n,fir,sec): if n<=1: return n else : sum1=fir+sec print(sum1) fir=sec sec=sum1 return fibonacci(n-1,fir,sec) #complete the recursive function print(fir) print(sec) fibonacci(num-1,fir,sec)
23rd Jul 2022, 7:50 AM
Hasibur Rahman
Hasibur Rahman - avatar
0
Hope it will help you: num = int(input()) def fibonacci(n): #complete the recursive function a = 0 b = 1 if n==0: return print (a) if n==1: print (a) print (b) return else: print(a) print(b) for i in range(2,n): c = a+b a = b b = c print (c) fibonacci(num) Good luck!
16th Jan 2023, 11:13 AM
Albert
- 1
num = int(input()) def loop(n): for i in range(0, n, 1): def fib(i): if i == 0: return 0 elif i == 1: return 1 else: return fib(i-2) + fib(i-1) print(fib(i)) loop(num)
20th Sep 2021, 10:10 PM
Will
Will - avatar