Search code examples
pythonindentation

How can i identify a problem in Indentation error inside a if, elif loop due to tab or space inside code. Is there a compiler which identifies it?


I am very new to python, so I am having trouble understanding the indentation in loops as no other programs I learnt used it. Isn't it much simpler if we use brackets to define certain blocks inside if and else statements? Anyways below is the screenshot of the code and the error marked in red.

Actual line of CODE:

if Turb_Wake_State_Model == "GLAUERT":
        if a_ax < 0.333:
            a_ax = C_ax/(4*(1-a_ax))
        else:
            a_ax = C_ax/((4*(1-0.25*a_ax*(5-3*a_ax))
            
    elif Turb_Wake_State_Model == "SPERA":
        a_c = 0.2
        if a_ax < a_c:
            a_ax = C_ax/(4*(1-a_ax))
        else:
            a_ax = (0.25*(C_ax - a_c^2)/(1-2*a_c))
    
    else:
         print ("WRONG TURBULENT WAKE STATE MODEL IS SPECIFIED")

The error occurs at the elif loop. I tried all combinations of else and ifs but to no avail. Also tried reading the help "if" statement but it didn't actually describe the indentations. The problem might seem very trivial to some but i am having trouble with the simplest of things here.


Solution

  • I think something like this is what you are looking for:

    if Turb_Wake_State_Model == "GLAUERT":
        if a_ax < 0.333:
            a_ax = C_ax/(4*(1-a_ax))
        else:
            a_ax = C_ax/((4*(1-0.25*a_ax*(5-3*a_ax))
                
    elif Turb_Wake_State_Model == "SPERA":
        a_c = 0.2
        if a_ax < a_c:
            a_ax = C_ax/(4*(1-a_ax))
        else:
            a_ax = (0.25*(C_ax - a_c^2)/(1-2*a_c))
        
    else:
        print ("WRONG TURBULENT WAKE STATE MODEL IS SPECIFIED")
    

    In python your elif and else statements have to be on the same indentation level as the if statement you want them to correspond to. You also had an extra space before the last print() line, python is really picky about spaces. I wrote a small program with the same flow structure as yours to demonstrate:

    # test.py
    def test(teststr1, teststr2):
        if teststr1 == "test1":
            print("test1")
            if teststr2 == "test1":
                print("test1 test1")
            else:
                print("test1 else")
    
        elif teststr1 == "test2":
            print("test2")
            if teststr2 == "test1":
                print("test2 test1")
            else:
                print("test2 else")
    
        else:
            print("else")
    
    
    test("test1", "test1")
    test("test1", "foobar")
    test("test2", "test1")
    test("test2", "foobar")
    test("foobar", "foobar")
    

    With output:

    $ python3 test.py
    test1
    test1 test1
    test1
    test1 else
    test2
    test2 test1
    test2
    test2 else
    else
    

    You may want to look here for examples and references.

    Also I agree brackets make more sense, but python gonna python :P