繁体   English   中英

Python中的Elif语法错误

[英]Elif syntax error in Python

这是我使用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

编辑:虽然这也是错误的东西,但这不是他的SyntaxError的原因

代码块中一定有东西 由于您注释了以下几行:

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不会执行任何操作,但是在这种情况下会形成障碍。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM