Python中的Elif语法错误

2024-04-20 08:12:04 发布

您现在位置: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()

我总是得到这样的错误:

^{pr2}$

Tags: ofthetoinyouiflowerelse
1条回答
网友
1楼 · 发布于 2024-04-20 08:12:04

编辑:虽然这也是错误的,但这并不是他语法错误的原因

代码块中一定有一些东西。因为你评论了几行:

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

    #forest()

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

块中在第一个elif:之后没有代码,因此当找到下一个elif:时,会出现语法错误。在

在已注释掉的块中添加pass语句:

^{pr2}$

pass什么都不做,但在这种情况下它可以形成块。在

相关问题 更多 >