我相信我发现了Cloud9 IDE的错误,因为我在以下代码中遇到语法错误:
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."
room2(character)
else:
print "You can't go that way!"
elif action == "2":
characterMenu(character)
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
else:
print "You fail to find anything worthy of note. "
searched_yet1 = True
else:
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
else:
print "You fail to hear anything worth of note. "
listened_yet1 = True
else:
print "You have already attempted that action!"
语法错误发生在"elif action == "4":
。我做错了什么或者我发现了Cloud9 IDE的错误?我试过调整间距。上述print语句是否有错误?
编辑:版本是Python 2.7.6,错误是
File "/home/ubuntu/workspace/dungeonMap.py", line 63
elif action == "4":
^
SyntaxError: invalid syntax
答案 0 :(得分:1)
当我检查您在此处发布的代码时,行elif action == 4:
前面有4个空格,然后是2个标签。在Python中混合空格和制表符是一个非常糟糕的主意。我还看到一些行(例如前一行)仅使用空格来缩进。
用空格替换这两个选项卡以及任何其他选项卡,并将IDE配置为在缩进时仅使用空格。看看是否能解决问题。
仔细观察后,我现在看到了直接的问题。我相信Python将标签视为8个空格,无论它在编辑器中出现的方式如何。鉴于此,您问题行上方两行的行是else:
,但缩进为结束if action == "1":
行而不是您想要的if searched_yet1 == False:
行。然后,Python会将您的elif action == 4:
行视为elif
而没有相应的先前if
。
再次,用空格替换所有这些标签然后让缩进看起来正确将解决这个问题和其他问题。