Python Forum

Full Version: Indentation Error With If Else Statement
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hello all

I was hoping someone could help with the following issue.

I keep getting an indentation error when using an If else statement, my code is as follows:-

t=0

while t < len(Data_Frame_Weekly_Tools_Report):
    
    data_frame_current = Data_Frame_Weekly_Tools_Report
    
    Data_Temp = Data_Frame_Weekly_Tools_Report.iloc[t,0]
    
    b=0
    
    while b < len(data_frame_current):
        
        if Data_Temp == data_frame_current.iloc[b,0]:
            #do nothing
        
        else:
            data_frame_current.drop(index=[b,0])
            
        b=b+1
        
    t=t+1
The error i get is:-

Error:
else: ^ IndentationError: expected an indented block
I know the code looks unorthodox but i understand it but i just dont understand why the hell i keep getting this error????

Can anyone help?

Thank you.
# do nothing is not a statement. It is a comment. Python needs a statement after the if. Use pass
Gribouillis

That worked a charm.

Thank you.
This is one of those times when the Python error message isn't all that useful. I think this should be flagged as a syntax error, a floating else statement without a corresponding if or for. As in:
        if Data_Temp == data_frame_current.iloc[b,0]:
            else:
                data_frame_current.drop(index=[b,0])
After all, "else" is the first statement following "if". But I guess indentation is either more important or is checked first.