What am I doing wrong????
The Fibonacci sequence begins with 0 and then 1 follows. All subsequent values are the sum of the previous two, ex: 0, 1, 1, 2, 3, 5, 8, 13. Complete the fibonacci() function, which has an index, n (starting at 0), as a parameter and returns the nth value in the sequence. Any negative index values should return -1.
My code:
def fibonacci(n):
a = 0
b = 1
c = 1 # Define the result variable here
if (n < 0): # Specifically return 1 for any entry of a negative number
return -1
print("1 ", end="") # Starting number is "1"
for i in range(1,n):
print(c, " ", end="") # Start printing Fibonacci values beginning with "0 + 1"
c = a + b
a = b
b = c
print(c, " ") # Print final value
return c
if __name__ == '__main__':
start_num = int(input("Enter a number: "))
print(f'fibonacci({start_num}) is {fibonacci(start_num)}')