Python Forum
Indentation Error With If Else Statement
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Indentation Error With If Else Statement
#1
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.
Reply
#2
# do nothing is not a statement. It is a comment. Python needs a statement after the if. Use pass
Reply
#3
Gribouillis

That worked a charm.

Thank you.
Reply
#4
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.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  An unexplainable error in .format statement - but only in a larger piece of code? ToniE 4 656 Sep-05-2023, 12:50 PM
Last Post: ToniE
  Error in Using INPUT statement gunwaba 1 2,017 Jul-03-2022, 10:22 PM
Last Post: deanhystad
  Cryptic Error with import statement Led_Zeppelin 2 2,479 Jan-11-2022, 01:13 PM
Last Post: Led_Zeppelin
  syntax error on return statement l_butler 5 3,029 May-31-2020, 02:26 PM
Last Post: pyzyx3qwerty
  Indentation error... lol ironfelix717 8 3,648 Apr-02-2020, 02:31 PM
Last Post: buran
  Error in continue statement Man_from_India 1 1,523 Feb-02-2020, 02:23 PM
Last Post: Larz60+
  Syntax Error (elif statement) Kanashi 0 3,638 Nov-20-2019, 11:29 PM
Last Post: Kanashi
  Error with simple "or" statement? Mark17 4 2,266 Nov-15-2019, 05:16 PM
Last Post: Mark17
  Getting error message for indentation Shafla 5 2,796 May-07-2019, 08:56 PM
Last Post: Yoriz
  Help please - indentation error bil007 1 2,021 Mar-24-2019, 11:41 AM
Last Post: Yoriz

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020