[英]SyntaxError at Cloud9 IDE? Is this a bug or did I do something wrong?
我相信我在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中發現錯誤?我嘗試調整間距。上述打印語句是否有錯誤?
編輯:版本是Python 2.7.6,錯誤是
File "/home/ubuntu/workspace/dungeonMap.py", line 63
elif action == "4":
^
SyntaxError: invalid syntax
當我檢查此處發布的代碼時,行elif action == 4:
前面是4個空格,然后是2個制表符。 在Python中混合空格和制表符是一個非常糟糕的主意 。 我還看到某些行(例如前一行)僅使用空格進行縮進。
用空格替換這兩個選項卡以及其他所有選項卡,並將IDE配置為縮進時僅使用空格。 看看是否能解決問題。
仔細觀察之后,我現在看到了直接的問題。 我相信,無論標簽在編輯器中如何顯示,Python都會將其視為8個空格。 鑒於此,問題行上方的兩行是else:
但縮進后得出if action == "1":
行,而不是您想要的if searched_yet1 == False:
行。 然后,Python將您的elif action == 4:
行視為沒有相應的if
的elif
。
同樣,用空格替換所有這些選項卡,然后使縮進看起來正確將解決該問題以及其他問題。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.