Build an Nth Fibonacci Number Calculator - Build an Nth Fibonacci Number Calculator

Tell us what’s happening:

On step 3 to building a nth Fibonacci number calculator it says Fibonacci (2) should return 1 and I can’t figure out what’s wrong nor can ai or edge help pls

Your code so far

def fibonacci(n):
    sequence = [0,1]

    if n < 2:
           return sequence[n]


           for i in range(2, n + 1):
            sequence.append(sequence[i-1] + sequence[i -2])

            return sequence[n]

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/151.0.0.0 Safari/537.36 Edg/151.0.0.0

Challenge Information:

Build an Nth Fibonacci Number Calculator - Build an Nth Fibonacci Number Calculator

GitHub Link: freeCodeCamp/curriculum/challenges/english/blocks/lab-nth-fibonacci-number/68de7360e1832ea180577a8a.md at main · freeCodeCamp/freeCodeCamp · GitHub

Welcome to the forum @Dragonmage2025

To help you debug add the following print call after the function definition.

print(fibonacci(2))

Happy coding

how do you expect this to return 1?

the if condition is false and anyway sequence[2] does not have a value

also please double check the positioning of this, note how it is after return sequence[n] and with its same indentation meaning this is never going to be executed