1
votes

Here is my code for the above tasks for a Fibonacci sequence. I was told that I indented incorrectly, but I still couldn't figure out why it happened. Can anyone have a look for me, please? I'm very grateful for any help. Also, my code worked for task 1 but not for task 2. The error i got is IndentationError: unindent does not match any outer indentation level, which is really frustrating after hours of trying to sort things out.

Task 1 The first 10 numbers of the Fibonacci sequence are: 0, 1, 1, 2, 3, 5, 8, 13, 21, 34 The sequence is generated from the first two numbers (0 and 1), and every subsequent number is the sum of the previous two numbers. What is the largest number in the Fibonacci sequence smaller then 10^ 22?

Task 2 Rewrite your previous Fibonacci code as a function taking as arguments one or two arguments. If there is one argument, print all the Fibonacci numbers up to that number. If there are two arguments, print all the Fibonacci sequence numbers between the two arguments.

x0,x1=0,1
while x1 < 1e22:
    x0,x1=x1,x0+x1
    print x1
#end of task 1

def fibo(xmax,xmin=0):
    x0,x1 = 0,1
    while x0 <= xmax:
        x0,x1=x0,x0+x1
        if x0 >= xmin:
            print x0
print fibo(60,6)
#end of task 2
1
I don't see any indentation errors but there is an infinity loop in your fibo function, since x0 is always 0 and therefore x0 <= xmax is true in each iteration. - tamasgal
What are you using for running this code and were there any indication like line number etc. about this error? - Aseem Bansal

1 Answers

0
votes

Couple problems.

  1. In the first task, you're only supposed to print the number less than 10e22. Unindent the python statement here because you print all the numbers upto that number in your code.

    x0,x1=0,1
    while x1 < 1e22:
        x0,x1=x1,x0+x1
    print x1
    #end of task 1
    
  2. In the second code, you seem to have mixed up the update statements, you reassign x0 to itself.

    def fibo(xmax,xmin=0):
        x0,x1 = 0,1
        while x0 <= xmax:
            x0,x1=x1, x1+x0
            if x0 >= xmin:
                print x0
    fibo(60,6)
    #end of task 2
    

Also, since you don't return anything, you should not do print fibo(60,6) because the implicit return value of None gets printed at the end.