Python中的Elif语法错误
这是我在用Python制作的一个文字冒险游戏中的if/elif/else条件代码。这个部分的目的是给玩家提供选择,让他们决定接下来要做什么,但系统提示我所有的"elif"语句的语法有问题。
if command.lower() in ["north", "n"]:
#With invitation
if "invitation" in items:
castle()
#Without 'Invitation'
else:
print "As you walk towards the gate, a guard approaches you and pushes you away. \"Sorry, pal,\" says the guard,\"You can't get in the castle without a written invitation from the king\""
town_center()
elif command.lower() in ["east", "e"]:
#market()
elif command.lower() in ["south", "s"]:
#forest()
elif command.lower() in ["west", "w"]:
#village()
elif command.lower() in ["inventory", "i"]:
print items
town_center()
elif command.lower() in ["look", "l"]:
print "\nYou are standing in the center of the rural town of Grifiinhorn, a quaint rural town in the province of Bedrin. To the north, you see a giant castle sitting on top of a hill. To the east is the marketplace, and to the west is the village. To the south is exit gates of the town which faces the Dark Forest.\n"
town_center()
else:
town_center()
我总是收到这个错误信息:
elif command.lower() in ["east", "e"]:
^
SyntaxError: invalid syntax
1 个回答
3
编辑:虽然这也是一个错误,但这并不是导致他出现语法错误的原因。
在代码块里必须有一些东西。因为你注释掉了一些行:
elif command.lower() in ["south", "s"]:
#forest()
elif command.lower() in ["west", "w"]:
在第一个elif:之后的代码块里没有任何代码,所以当下一个elif:被找到时,就会出现语法错误。
在你注释掉的代码块里加一个pass
语句:
elif command.lower() in ["south", "s"]:
#forest()
pass
elif command.lower() in ["west", "w"]:
pass
什么都不做,但在这种情况下,它可以形成一个代码块。