educative.io

What have I done wrong?

Hey everyone, I tried to write my own code for the fibonacci exercise. Comparing it to the solution, it looks pretty similar, but mine doesn’t work. Can someone please have a look and see what mistakes I have made in my code? Thanks!

Edit: The indents aren’t showing up, but they are the same as in the solution.

def fib(n):
if n<=0:
return -1
fib1==0
fib2==1
fibn==0
count==3
if n==1:
return fib1
if n==2:
return fib2
while count<=n:
fibn==fib1+fib2
fib1==fib2
fib2==fibn
count+=1
return fibn

Hello Brian,

There are syntax errors in your code. Other than that it works fine. You seem to have confused the == and = operators.

To assign a value to an element we use the assignment operator = in python. For example, if you want to assign 0 to the variable fib1, you will have to do it like this: fib1 = 0

== is a relational operator used to compare two values. For example, if you want to compare if count is less than or equal to n, then you will have to do it like this: count <= n

To make the code work just replace == with = in the following lines:

  1. Change fib1==0 to fib1 = 0
  2. Change fib2==1 to fib2 = 1
  3. Change fibn==0 to fibn = 0
  4. Change count==3 to count = 1
  5. Change fibn==fib1+fib2 to fibn = fib1+fib2
  6. Change fib1==fib2 to fib1 = fib2
  7. Change fib2==fibn to fib2 = fibn
1 Like