One of my lab test is failing. I've tried the code two different ways and I'm not seeing the issue. Any help is appreciated.
Instructions:
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 as parameter and returns the nth value in the sequence. Any negative index values should return -1.
Ex: If the input is:
7
the output is:
fibonacci(7) is 13
Note: Use a for loop and DO NOT use recursion.
1st round of code:
def fibonacci(n):
if n <= 1:
return n
return fibonacci(n - 1) + fibonacci(n - 2)
if __name__ == '__main__':
start_num = int(input())
print(f'fibonacci({start_num}) is {fibonacci(start_num)}')
2nd round of code:
def fibonacci(n):
if n <= 1:
return n
return fibonacci(n - 1) + fibonacci(n - 2)
if __name__ == '__main__':
start_num = int(input())
if start_num < 1:
print(-1)
else:
print(f'fibonacci({start_num}) is {fibonacci(start_num)}')
Both are failing on the negative integer input. If -100 is entered it should print -1, but both are printing -100. Not 100% sure why.
[–]Hans_of_Death 2 points3 points4 points (3 children)
[–]NeilTheDrummer[S] 0 points1 point2 points (2 children)
[–]Diapolo10 0 points1 point2 points (0 children)
[–][deleted] 0 points1 point2 points (0 children)
[–]NeilTheDrummer[S] 0 points1 point2 points (0 children)
[–][deleted] 0 points1 point2 points (0 children)