Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/358.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 Can';我不明白为什么真/假是';行不通_Python_Python 3.x - Fatal编程技术网

Python Can';我不明白为什么真/假是';行不通

Python Can';我不明白为什么真/假是';行不通,python,python-3.x,Python,Python 3.x,玩家取下灯泡后,“灯泡”被附加到清单中,灯泡变量被设置为True。我已经在代码中添加了print(light\u bulb),事实上,一旦取下灯泡,它就被设置为True 我不明白为什么第9行会运行,即使在末尾有一个而没有灯泡:。代码运行的第二次、第三次或第四次,因为灯泡设置为True;这将计算为而不是True或而不是False。这意味着第14行应该运行而不是第9行,对吗 inventory = ["null"] def lol(): choice1 = input(">")

玩家取下灯泡后,“灯泡”被附加到清单中,灯泡变量被设置为
True
。我已经在代码中添加了
print(light\u bulb)
,事实上,一旦取下灯泡,它就被设置为
True

我不明白为什么第9行会运行,即使在末尾有一个
而没有灯泡:
。代码运行的第二次、第三次或第四次,因为
灯泡
设置为
True
;这将计算为
而不是True
而不是False
。这意味着第14行应该运行而不是第9行,对吗

inventory = ["null"]

def lol():
    choice1 = input(">")
    light_bulb = False
    if "bulb" in inventory:
        light_bulb = True 

    if "bulb" in choice1.lower() or "unscrew" in choice1.lower() or "lightbulb" in choice1.lower() or "light" in choice1.lower() and not light_bulb:
        inventory.append("bulb")
        print("You try to fix the light bulb but it comes off. You decide to keep it.")
        alley()

    elif "bulb" in choice1.lower() or "unscrew" in choice1.lower() or "lightbulb" in choice1.lower() and light_bulb:
        print ("You already have a light bulb!")
        alley()
    else:
        dead("You fumble around in the darkness and accidentally kill yourself.")

操作符的绑定比
更紧密。所以你写的相当于:

  if "bulb" in choice1.lower() or "unscrew" in choice1.lower() or "lightbulb" in choice1.lower() or ("light" in choice1.lower() and not light_bulb):
你想要的是:

  if ("bulb" in choice1.lower() or "unscrew" in choice1.lower() or "lightbulb" in choice1.lower() or "light" in choice1.lower()) and not light_bulb:

要得到它,需要括号。

这个问题与计算
表达式的顺序有关。
运算符的优先级高于其前面的
表达式,因此首先对其求值。我猜你想让它最后评估。尝试添加括号,以明确顺序(这也允许您将行换行到更合理的长度):


请注意,可能不需要检查字符串
“lightbulb”
,因为您还需要分别检查其子字符串
“light”
“bulb”
(并且
中的
操作符不尊重单词边界).

你没有在这么长的条件下设置
light\u bulb=True
。我不明白。我正在设置
light\u bulb=True
这里:
如果清单中的“bulb”:light\u bulb=True
而不是灯泡
只是检查玩家是否有灯泡。结果取决于你的输入。你输入的是什么。此外,为了整洁起见,请将小写转换一次,并且“灯泡”的测试是多余的。如何进行转换一次?选择1=输入(“>”)。小写
if ("bulb" in choice1.lower() or "unscrew" in choice1.lower() or
    "lightbulb" in choice1.lower() or "light" in choice1.lower()) and not light_bulb: