0

I'm absolutely pulling my hair out over this. The if/elif statement in this function throws a syntax error on the elif line. To me there are no obvious syntax problems.

"elif n == cs_index:"
    ^
SyntaxError: invalid syntax

I tried switching it to a bear "else:" just to see if that would stupidly word and it didn't. I'm sure there's something I don't see.

def higherhighlight(cs_index):
    text.tag_remove("the hello","1.0", END)
    idex = "1.0"
    for n in range(cs_index):
        if n < cs_index:
            idex = text.search("Hello", idex, nocase=1, stopindex=END)
            lastidex = idex+"+"+str(len("hello"))+"c"
            idex = lastidex
        elif n == cs_index:
            idex = text.search("Hello", idex, nocase=1, stopindex=END)
            print idex
            lastidex = idex+"+"+str(len("hello"))+"c"
            print lastidex
            text.tag_remove("hellos", idex, lastidex)
            text.tag_add("the hello", idex, lastidex)
    text.tag_config("the hello", background="dark green")
OneCricketeer
  • 179,855
  • 19
  • 132
  • 245
pabs
  • 3
  • 1
  • 4

2 Answers2

1

After pasting my code into my Spyder IDE and running it, I exhibit no errors (apart from the parentheses missing in the print statements - you should really upgrade to Python 3.5.2!)

Try re-indenting that line by resetting the indentation and adding three tabs. I get similar errors when I copy and paste code that uses spaces for indentation.

Jossie Calderon
  • 1,393
  • 12
  • 21
1

You are mixing tabs and spaces in your code; taking the source of your initial post and pasting it into Sublime Text, then selecting all lines I see this:

source code with spaces and tabs

The grey lines are tabs, the dots are spaces. I have tabs set to expand to every 4th column, you probably have the same setting.

Python, which expands tabs to every 8th column, sees this:

same source with tabs set to 8 spaces

Note how the elif indentation matches the preceding lines, because the tab character following those 4 spaces is expanded to the first 8th column, not a second. This is the cause of the exception you see.

Don't mix tabs and spaces. Preferably, stick to spaces for indentation only; it is what the Python styleguide recommends:

Spaces are the preferred indentation method.

Tabs should be used solely to remain consistent with code that is already indented with tabs.

Configure your editor to use spaces only. You can still use the TAB key, but your editor will use spaces to indent lines when you do.

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343