Link to home
Start Free TrialLog in
Avatar of Stephen Kairys
Stephen KairysFlag for United States of America

asked on

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
SOLUTION
Avatar of ozo
ozo
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Stephen Kairys

ASKER

What's the surest way to tell the indentation level in Notepad++? Thanks.
Follow-up: I checked by doing SHIFT-TAB on both lines and they are only indented one level.  Here's a screenshot:

User generated image
Is the error on
    elif (restaurant == "abcde"):
or on
    elif (restaurant not in myrestaurants): # trying something extra...
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.

User generated image
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.
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.
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!

ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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
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!
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.
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.

User generated image
User generated image
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
You mean this screen? Looks like TAB SETTINGS is below Language Menu, not a submenu of it but looks promising...thanks.
User generated image
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thank you. That seems to work

Will review later to see which of your posts will be included in my ACCEPT MULTIPLE SOLUTIONS. :)
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