Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/314.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python正在忽略逃逸变量_Python - Fatal编程技术网

Python正在忽略逃逸变量

Python正在忽略逃逸变量,python,Python,我正在为一款基于文本的游戏制作一个战斗系统,其中一个选择是逃跑。 但是,我想让你只能尝试逃跑一次。我这样做是为了在1次尝试失败后,将fleering_expected变量从false设置为true。然而,python忽略了这一点,让我尝试逃离任意次数 编辑:我做了修复,但结果没有改变。这里是修改 fleeing_attempted = False #the first time the variable is defined def battle(): print("What will

我正在为一款基于文本的游戏制作一个战斗系统,其中一个选择是逃跑。 但是,我想让你只能尝试逃跑一次。我这样做是为了在1次尝试失败后,将fleering_expected变量从false设置为true。然而,python忽略了这一点,让我尝试逃离任意次数

编辑:我做了修复,但结果没有改变。这里是修改

fleeing_attempted = False #the first time the variable is defined

def battle():
    print("What will you do?")
    print("1. attack")
    print("2. flee")
    action = input()
    if action == "1":
        print("test")
    elif action == "2":
        fleeing = randint(1, 100)
        if fleeing in range(1, 50):
            print("You got away!")
        elif fleeing in range(51,100):
            callable(fleeing_attempted)
            print("The enemy caught up!")
            battle()
    elif action == 2 and fleeing_attempted:
        print("You already tried that!")
        battle()
测试它是否正确

您还可以使用

elif action == "2" and fleeing_attempted :
    # rest of code
已清除的代码

def have_battle():
    action = input("What will you do?")
    print("1. Attack")
    print("2.Flee")

    if action == "1":
        print("You choose to attack")
    elif action == "2":
        # do your random check
        fleeing = random.randint(0, 100)
        print("fleeing variable: {0}", fleeing)
        if fleeing in range (1,50):
            print("You choose to flee")

你有一些大问题,但我解决了一些,你应该能够解决

看看下面两行代码:

fleering\u expected=False
如果尝试逃离_==真:
在该
if
语句中,
fleeding\u尝试的
不可能是
True
,因为它在上面一行被设置为
False


没有看到代码的其余部分,很难判断
fleering\u expected=False
应该放在哪里,但它可能应该在初始化过程中的某个地方。

如果fleering\u expected==True:
在任何情况下都不能为True,因为您在上面的行中设置为False。我认为您必须将第一个声明
fleering\u expected=False
移到外部第一个elif的范围。每次。。。还要注意的是,如果你试图逃跑,你可以只做
否则:
重新调整一下你的思维可能是个好主意。Python并没有忽略任何事情,它所做的正是您让它做的。这是电脑最棒的(有时也是令人讨厌的)地方。所以下一次,请一步一步地检查代码,找出事情没有按预期工作的原因。它是一种编程语言,它逐行执行,不忽略任何内容。它确实会忽略您通过检查始终为False的条件隐式告诉它忽略的代码行……即使在我进行了此修复之后,它仍然会忽略它。”elif action==2和fleering\u expected:print(“你已经试过了!”)battle()@rsg33-你把
fleering\u expected=False
放在哪里了?@rsg33-你是怎么输入的?在您的示例中,
2
是一个字符串。发布您的全部代码,以便我们可以帮助您。我刚才在原始帖子中添加了更改。
elif action == "2" and fleeing_attempted :
    # rest of code
def have_battle():
    action = input("What will you do?")
    print("1. Attack")
    print("2.Flee")

    if action == "1":
        print("You choose to attack")
    elif action == "2":
        # do your random check
        fleeing = random.randint(0, 100)
        print("fleeing variable: {0}", fleeing)
        if fleeing in range (1,50):
            print("You choose to flee")