0
What's wrong with my fibonacci code?
num = int(input()) def fibonacci(n): #complete the recursive function if n<=1: return 1 #exit condition else: return fibonacci(n-1) + fibonacci(n-2) for number in range(num): print(fibonacci(num)) It is printing the sum of all of the numbers in the sequence when the Python Core lesson wants all of the numbers for given input printed.
2 Respuestas
+ 2
for number in range(num):
print(fibonacci(number))
0
Thanks Rodwynnejones, I see I was printing the wrong thing. I needed to add a couple lines also. Here's what worked:
num = int(input())
def fibonacci(n):
#complete the recursive function
if n == 0:
return 0
elif n == 1 or n == 2:
return 1
else:
return fibonacci(n-1) + fibonacci(n-2)
for number in range(num):
print(fibonacci(number))