while循环中的If语句忽略lis

2024-03-28 15:44:40 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试制作一个基于文本的冒险游戏,我已经开始学习Python了。我计划在我去的时候加上它,并用它来应用我所学的东西。你知道吗

出于某种原因,尽管一切看起来都很好,但当我在块的开头添加while循环时,Python开始忽略我的“否定”列表。我不明白为什么!你知道吗

# 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

Tags: orofthetoyouyourifone
1条回答
网友
1楼 · 发布于 2024-03-28 15:44:40

您在negative列表中的no后面放错了逗号。你知道吗

把它改成

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

在此之前,non将打印您的You must choose! Yes... or no?消息,因为从技术上讲,它们都不在列表中。不过,像nah这样的回答也奏效了。加上那个逗号你就没事了。你知道吗

相关问题 更多 >