First time here? Checkout the FAQ!
x
0 votes
484 views
asked in Python by (116k points)  
Please also check if the fibo(7) = 8
  

2 Answers

0 votes
answered by (116k points)  
selected by
 
Best answer

The correct answer is here:

#function
def fibonacci(n):
    if (n <= 0):
      return ("number should be positive")
    elif (n == 1): 
      return(0)
    elif (n == 2):
      return(1)
    else:
      sum = fibonacci(n-1) + fibonacci(n-2)
      return (sum)

#print result   
print(fibonacci(7))
0 votes
answered by (140 points)  
#function
def fibonacci(n):
if n == 1 or n == 2:
return 1
else:
sum = n + fibonacci(n-1)
return sum

#print result
print(fibonacci(10))

https://repl.it/@soulwolf233/fibonacci-sequence

commented by (116k points)  
You should correct your code as follows:

sum = fibonacci(n-1) + fibonacci(n-2)
...