Python: elif syntax error

In this code fragment:, when I invoke the Python interpreter, I get the following error:

M:\MongoDB University\Lecture 1\bottle_framework_url_hand
  File "hello_world.py", line 15
    elif (restaurant == "abcde"):
       ^
SyntaxError: invalid syntax


   
if (restaurant == None or restaurant == ""):
        restaurant="***No restaurant selected***"
    elif (restaurant not in myrestaurants): # trying something extra...
	restaurant = "**Invalid restaurant selected**"

Open in new window


As far as I can tell, my syntax is OK. (e.g. a colon after the elif line and proper indentation. What am I missing?
Thanks,
Steve
LVL 4
Stephen KairysTechnical Writer - ConsultantAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

ozoCommented:
It looks like the elif  indentation does not match the if indentation.
try making them the same
Stephen KairysTechnical Writer - ConsultantAuthor Commented:
What's the surest way to tell the indentation level in Notepad++? Thanks.
Stephen KairysTechnical Writer - ConsultantAuthor Commented:
Follow-up: I checked by doing SHIFT-TAB on both lines and they are only indented one level.  Here's a screenshot:

elif error
CompTIA Cloud+

The CompTIA Cloud+ Basic training course will teach you about cloud concepts and models, data storage, networking, and network infrastructure.

ozoCommented:
Is the error on
    elif (restaurant == "abcde"):
or on
    elif (restaurant not in myrestaurants): # trying something extra...
Stephen KairysTechnical Writer - ConsultantAuthor Commented:
Error on the later example.

Now, I tried starting the whole if/elif thing from scratch and now it' s worse...getting a indent error. It looks fine here.....my indent(s) are the TAB key. Wondering (in NOTEPAD++) if that red line on the left is a clue to what's wrong...Thanks.

Indent error
Stephen KairysTechnical Writer - ConsultantAuthor Commented:
OK, I got rid of my Indent errors....I'm not sure what i had done to create them. So, I went back to an earlier version of the program and am back to the original elif error
  File "hello_world.py", line 16
    elif (restaurant == "McDonalds"):
       ^
SyntaxError: invalid syntax

Open in new window


FWIW, when I tried an assignment statement at the same indent level, I did NOT get the error.

Thanks.
gelonidaCommented:
What editor are you using? Could it be, that you are mixing space characters and tab characters.

On many editors you can display special characters in order to indentify such issues.


Could you copy / paste  the complete function (no screenshot, really the actual code) having the error?

Very probably it will be very easy to identify the error if we have the actual code.
Stephen KairysTechnical Writer - ConsultantAuthor Commented:
Hi Gelonida.

Using NOTEPAD++.

Here's the screenshot.  NOTE: I am leaving on vacation today (apx. 1700 UCT). So, I won't be able to respond to anything posted after that point. No rush though...

Thanks!

gelonidaCommented:
Hi Stephen,

please look very carefully at line 16 and line 17.
You will see, that you see a small orange arrow in the leading white space of each line.

This indicates, that you used:
4 space characters in line 13
4 space characters in line 14
8 space characters in line 15
1 tab character       in line 16
4 space characters and one tab character in line 17

Your editor seems to be configured to a tab width of 4 (tab is displayed as an indentation of 4)
Python however treats a tab character as in indentation of 8 characters.

So what you see with your eyes is following indentation for line 13 to 17
4
4
8
4
8


But what Python sees is following indentation for line 13 to 17
(To see what I mean you can do following in notepad++:
Set settings -> preferences -> Language Menu / Tab settings -> tab size to 8)
4
4
8
8
12
Now the indentation error, that Python reports gets obvious.

The simple rule is to NEVER mix tab and blank characters in the same file.
So please find all tab characters (orange arrows in the leading space) and replace with blank characers.

In order to avoid this issue in the future when using notepad ++ I would recommend following settings:

Set settings -> preferences -> Language Menu / Tab settings -> tab size to 4)
and check following value  settings -> preferences -> Language Menu / Tab settings -> replace by space )

In fact, Python itself is able to identify files, that use space and tab characters as indentation in an incoherent manner. Unfortunately this is not done by default. for Python 2.
Python 3 however would have reported something like:
TabError: inconsistent use of tabs and spaces in indentation

in Python 2 you can type
python -t yourfile.py
and you would also get an error reported:
./yourfile.py: inconsistent use of tabs and spaces in indentation

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Stephen KairysTechnical Writer - ConsultantAuthor Commented:
Gelonida.,

Thank you for your detailed reply.  Sorry for the late response as I got back from my holiday late last night.

I will look at this later today. What I will say is that I don't recall ever typing leading spaces at the start of those (or any) lines....does that make sense? :)

So for now:
What's the safest way to specify to Python that I want one level of indent, two levels, etc.?
Should I use tabs or spaces?

Thanks again,


Steve
Stephen KairysTechnical Writer - ConsultantAuthor Commented:
Update:
I retyped the entire function. I decided (arbitrarily) to use two spaces for each indent level. Is that method considered best practice - or at least "acceptable practice" :) ?

Thanks!
gelonidaCommented:
Most Python projects I saw use four space characters as indentation and prefer blank characters over tab characters.
Most modern javascript projects seem to use two blanks, so I'd stick with four characters, though anything is acceptable.

What's most important however is to configure your editor such, that it replaces tab characters with blank characters immediately when typing (or alternatively, but I saw that less often to only use tab characters and never leading blank characters). Mixing tabs and spaces is what has to be avoided in any language, where indentation is an essential part of the syntax (e.g. Python / Coffeescript)

It's a pity that you removed the screen shot to which I gave you the answer to (at least I don't see this screen shot anymore) , as this was exactly the one, that would have helped later participants to understand THE actual problem, that you encountered and that others might encounter in the future.

About how to fix the problem with existing code:
With Notepad ++ one of following two strategies is what I normally recommend.

Either you make a regexp search replace of \t with four  blank characters) and look then at the places where indentation looks inconsistent or perhaps safer (but slower): look manually for red 'arrows' in the leading white space of your code (as were visible on your now disappeared screen shot) and replace them with four characters or  the amount of characters that make the code correct at this place.
Stephen KairysTechnical Writer - ConsultantAuthor Commented:
Well that's odd per the screeenshot. I do not recall removing it. Here it is again...does this help?
(At this point, I can't recall which of two screenshots I used, so I'm including both...:) )

Btw, using a fixed # of spaces is working very well for me by hitting the spacebar X times.

Thanks again.

elif error
Indent error
gelonidaCommented:
Hi Stephen. it was none of the above screenshots.

It was the screenshot, that displayed line numbers and where one could see a thin red arrow in the leading white space of a line,

You should not be forced by your editor to have to press the space bar multiple times.
Be nice to your typing fingers ;-)

Just configure your editor to a tab width of 4 characters and configure it to 'expand' tabs to spaces.

For Notepad++ this would be
Set settings -> preferences -> Language Menu / Tab settings -> tab size to 4)
and check following value  settings -> preferences -> Language Menu / Tab settings -> replace by space )
Stephen KairysTechnical Writer - ConsultantAuthor Commented:
You mean this screen? Looks like TAB SETTINGS is below Language Menu, not a submenu of it but looks promising...thanks.
Notepad tab settings
gelonidaCommented:
yes this is the setting to change
Stephen KairysTechnical Writer - ConsultantAuthor Commented:
Thank you. That seems to work

Will review later to see which of your posts will be included in my ACCEPT MULTIPLE SOLUTIONS. :)
Stephen KairysTechnical Writer - ConsultantAuthor Commented:
Thank you both!

@Gelondia  - Note that I did not try the PYTHON -T option with the code sample I had originally referenced.

I'm good to go!

Steve
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Python

From novice to tech pro — start learning today.