Dragoonknight Dragoonknight - 1 year ago 94
Python Question

Python: Syntax Error at Cloud9? Is this a bug or did I do something wrong?

I believe I have found a bug with the Cloud9 IDE, as I get a syntax error in the following code:

for x in optionMenu:
print x[0], x[1]
action = raw_input ("Please select an action: ")
if action == "1":
direction = directionMenu()
if direction == "East":
validAction = True
print "You raise the portcullis and enter the palace."
print "You can't go that way!"
elif action == "2":
elif action == "3":
if searched_yet1 == False:
skill_pass = skillCheck(1, character.search)
if skill_pass == True:
print "The double portcullis seems moveable with some effort."
searched_yet1 = True
print "You fail to find anything worthy of note. "
searched_yet1 = True
print "You have already attempted that action!"
elif action == "4":
if listened_yet1 == False:
skill_pass = skillCheck(5, character.listen)
if skill_pass == True:
print "Sounds coming from deep in the palace can be heard every few minutes."
listened_yet1 = True

print "You fail to hear anything worth of note. "
listened_yet1 = True
print "You have already attempted that action!"

The syntax error occurs at
"elif action == "4":
. AmI doing something wrong or have I found a bug with the Cloud9 IDE? I have tried adjusting the spacing. Is there an error with the above print statement?

EDIT: Version is Python 2.7.6, error is

File "/home/ubuntu/workspace/dungeonMap.py", line 63
elif action == "4":
SyntaxError: invalid syntax

Answer Source

As I examine your code as posted here, the line elif action == 4: is preceded by 4 spaces then 2 tabs. Mixing spaces and tabs in Python is a very bad idea. I also see that some lines, such as the preceding one, use only spaces for indentation.

Replace those two tabs, as well as any others, with spaces, and configure your IDE to use only spaces when indenting. See if that solves the problem.

After looking more closely, I now see the direct problem. I believe that Python treats a tab as 8 spaces, no matter how it appears in your editor. Given that, your line two lines above your problem line is an else: but is indented to conclude the if action == "1": line rather than the if searched_yet1 == False: line that you intended. Python then sees your elif action == 4: line as an elif without a corresponding prior if.

Again, replacing all those tabs with spaces and then getting the indentation to look right will solve that problem and others.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download