如果while循环中的语句忽略列表

时间:2015-10-15 00:15:05

标签: python if-statement while-loop

我正在尝试制作基于文本的冒险游戏,而且我在学习Python的几天之后。我计划在我去的时候添加它并使用它来应用我正在学习的东西。

出于某种原因,尽管一切似乎都很好,但Python开始忽略了我的"否定"当我在块的开头添加while循环时列出。我无法弄明白为什么!

# Error message printed if player types gibberish or doesn't pick one of the available responses.
def error():
    print("You must choose one of the options!")

# Allows going back to the beginning of the game should the player lose.
def begin():
    playerName = input("Enter your name: ").upper()
    print("This is a choose your own adventure game. %s, you have a great deal of danger ahead of you. This will require a lot of courage. Are you sure you are ready?" % (playerName))

# List of affirmative sayings the player may type (incl. alternatives to "yes").
affirmative = ["yes",
        "y",
        "yup",
        "yep",
        "sure",
        "okay",
        "rad",
        "yepper",
        "yeppers",
        "alright",
                ]

# List of negative sayings a player may type (incl. alternatives to "no").
negative = ["no"
        "n",
        "nah",
        "nu",
        "nope",
        "negative",
        "negatory",
        "go away",
        ]

begin()

""" If they aren't ready, gives them an epilogue and allows them to restart. If they ARE ready, allows them to continue. If they type gibberish, it makes them choose between one of the two options. """

while True:
    ans = input()
    if ans in negative:
        print("You never begin your journey. You stay at home and lead a completely normal, boring life. As the years go by, you find yourself reflecting more and more often on what could have been. The spectre of your unfulfilled potential haunts you until your death. :( ")
        newAns = input("Press Enter to start over.")
        if newAns == True or newAns == False:
            begin()
            break
    elif ans in affirmative:
        print("You begin your journey into a dark wood. Are there any supplies you'd like to take with you?")
        break
    else:
        print("You must choose! Yes... or no?")
        continue

1 个答案:

答案 0 :(得分:3)

您在no列表中negative之后放错了逗号。

将其更改为

negative = ["no",
            "n",
            "nah",
            "nu",
            "nope",
            "negative",
            "negatory",
            "go away",
            ]

之前,non会打印您的You must choose! Yes... or no?消息,因为这两个消息都不在技术上。但是,nah之类的其他回复也奏效了。添加逗号,你应该没问题。