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

2024-04-18 12:18:38 发布

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

注意:我之前做过breakhold 3=0。你知道吗


Tags: orandthetopathinyouif
2条回答

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

http://inform7.com

http://tads.org

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

这类布尔表达式存在问题:

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是否在“yes”值列表中:

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

更简单,但:

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

另外,请记住,在发布的代码中,有几个地方有不正确的布尔表达式。你知道吗

相关问题 更多 >