Stephen Kairys
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_han d
File "hello_world.py", line 15
elif (restaurant == "abcde"):
^
SyntaxError: invalid syntax
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
M:\MongoDB University\Lecture 1\bottle_framework_url_han
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**"
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Is the error on
elif (restaurant == "abcde"):
or on
elif (restaurant not in myrestaurants): # trying something extra...
elif (restaurant == "abcde"):
or on
elif (restaurant not in myrestaurants): # trying something extra...
ASKER
ASKER
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
FWIW, when I tried an assignment statement at the same indent level, I did NOT get the error.
Thanks.
File "hello_world.py", line 16
elif (restaurant == "McDonalds"):
^
SyntaxError: invalid syntax
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.
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.
ASKER
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!
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
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
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
ASKER
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!
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.
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.
ASKER
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thank you. That seems to work
Will review later to see which of your posts will be included in my ACCEPT MULTIPLE SOLUTIONS. :)
Will review later to see which of your posts will be included in my ACCEPT MULTIPLE SOLUTIONS. :)
ASKER
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
@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
ASKER