Search code examples
pythonfunctionsyntax-errorfibonacci

How to solve this else syntax error in python


Following is this code for generating Fibonacci series for n elements, the else which has a comment is the part that is causing an error

def solve(n):
# write your code here,end=
a=[0,1]
b=[]
if n==0:
    b.append(0)
return b    
else:#THE ERROR PART
    for i in range(n-1):
        a.append(a[i+1]+a[i])
    return a

enter image description here


Solution

  • The indent for return needs correction.

    def solve(n):
        # write your code here,end=
        a=[0,1]
        b=[]
        if n==0:
            b.append(0)
            return b    #edit here
        else:#THE ERROR PART -> no more error
            for i in range(n-1):
                a.append(a[i+1]+a[i])
            return a
    
    solve(10)
    

    Output

    [0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55]