在python中,为什么我的简单测试基础游戏的这段(相当混乱)代码不起作用



这是我现在正在进行的一个简单的基于文本的冒险的一部分(强调部分)。出于某种原因,每次我运行这个并在任何一点上说"不"时,它仍然会继续执行并执行"是"的代码。也可以随意建议任何清理此代码的方法。我使用互联网自学,对python非常陌生,所以请不要讨厌:)。

if path1 == "1":
while breakhold3 == 0:
    if path1 == "1":
        poi1 = raw_input ("There is a strange artificial hole in the ground. It is surrounded by yellow bricks and vines cover them. Would you like to investigate?")
        if poi1 == "Yes" or "yes" or "ye" or "Ye" or "Y" or "y":
            print("You crouch down to look in the hole. When you look in, you find that it goes so deep all you can see is blackness. There is a large rock next to the hole.")
            if item == "1":
                poic1 = raw_input ("You get out the rope and attatch it to the rock. You throw it down the hole and it is at least 2 seconds before you hear it hit the bottom. Would you like to descend the rope?")
                if poic1 == "Yes" or "yes" or "Ye" or "ye" or "Y" or "y":
                    print ("You slowly descend down the rope. It takes about a minute before you get to the bottom. It is dark here, and you begin to feel around the room with your hands.") 
                    from random import randrange
                    numberboy = randrange(7,10)
                    if numberboy == 7:
                        print ("You unluckily fall into a pit!")
                        health -= 1
                        if health == 0:
                            print ("You drop to your knees and lie, filled with pain in the pit. You drop to the floor. Your quest is over.")
                        print ("Your health has fallen to " + str(health) + ". ")
                    if numberboy in (8,9):
                        print ("You could have fallen into a pit but you luckily didn't!")
                    print ("You find a path leading of to another room.")
                    print ("You walk down the path and find a light illuminating an elvish sword!")
                    print ("You walk out of an escape path and find youself coming out of a secret entrance at the clearing.")
                    breakhold3 += 1
                    break
                if poic1 == "No" or "no" or "n" or "N":
                    print ("You decide not to descend down the rope. You continue down the path ahead.")
                    breakhold3 += 1
        if poi1 == "no" or "No" or "n" or "N":
            print ("You decide not to investigate. You continue down the path ahead.")
            breakhold3 += 1
print ("Check")
import time

注意:我早些时候突破了3=0。

问题在于这种类型的布尔表达式:

if poi1 == "Yes" or "yes" or "ye" or "Ye" or "Y" or "y":

这就是你想要做的:

if poi1 == "Yes" or poi1 == "yes" or poi1 == "ye" or poi1 == "Ye" or poi1 == "Y" or poi1 == "y":

问题是这些字符串本身的求值结果为True

更好的解决方案可能是检查poi1是否在"是"值列表中:

if poi1.lower() in ["yes", "ye", "y"]:

更简单,但:

if poi1.lower().startswith("y"):

此外,请记住,在您发布的代码中,有几个地方存在不正确的布尔表达式。

或者,尝试一种特别适合文本冒险的语言:

http://inform7.com

http://tads.org

更多信息,请访问http://www.ifwiki.org

最新更新