简体   繁体   中英

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. 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.

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

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.

Add a pass statement in the blocks you have commented out:

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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