简体   繁体   English

Python中的Elif语法错误

[英]Elif syntax error in Python

This is my code for a if/elif/else conditional for a text-based adventure game I'm working on in Python. 这是我使用Python开发的基于文本的冒险游戏的if / elif / else条件代码。 The goal of this section is to give the player options on what to do, but it says there is something wrong with the syntax of all of my "elif" statements. 本部分的目的是为玩家提供执行操作的选项,但它表示我所有“ 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()

I always get this error: 我总是收到此错误:

elif command.lower() in ["east", "e"]:
   ^
SyntaxError: invalid syntax

Edit: although this is something that's also wrong, it's not the cause of his SyntaxError 编辑:虽然这也是错误的东西,但这不是他的SyntaxError的原因

There must be something in a code block. 代码块中一定有东西 Because of your commenting out some lines: 由于您注释了以下几行:

elif command.lower() in ["south", "s"]:

    #forest()

elif command.lower() in ["west", "w"]:

There is no code in the block after the first elif:, so you get a syntax error when the next elif: is found instead. 在第一个elif:之后的代码块中没有代码,因此当找到下一个elif:时会出现语法错误。

Add a pass statement in the blocks you have commented out: 在您已注释掉的块中添加pass语句:

elif command.lower() in ["south", "s"]:
    #forest()
    pass
elif command.lower() in ["west", "w"]:

pass does nothing, but it can form a block in cases like this. pass不会执行任何操作,但是在这种情况下会形成障碍。

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

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