Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/297.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_Boolean - Fatal编程技术网

Python 布尔问题。和,或,和或,或不

Python 布尔问题。和,或,和或,或不,python,boolean,Python,Boolean,我正在尝试使用bool运行代码,但在使代码正常工作方面遇到了问题。基本上,我必须使用代码来决定在满足某些标准后驾驶是否安全。电池必须是充电器(正确),我必须有一辆车(正确),我不能喝醉(错误),我必须有足够的汽油到达我要去的地方,如果是晚上,我必须有工作头灯。到目前为止,我已经设法把所有的方式,通过气体部分,但我似乎无法理解的和,或,和不是,或不是,或不是声明,当谈到驾驶在夜间或白天与工作和非工作头灯 battery_charged=True got_car=True drunk=False g

我正在尝试使用bool运行代码,但在使代码正常工作方面遇到了问题。基本上,我必须使用代码来决定在满足某些标准后驾驶是否安全。电池必须是充电器(正确),我必须有一辆车(正确),我不能喝醉(错误),我必须有足够的汽油到达我要去的地方,如果是晚上,我必须有工作头灯。到目前为止,我已经设法把所有的方式,通过气体部分,但我似乎无法理解的和,或,和不是,或不是,或不是声明,当谈到驾驶在夜间或白天与工作和非工作头灯

battery_charged=True
got_car=True
drunk=False
gas=2 # (gallons) # gas currently in the tank of the car
distance=100 # miles from home
mpg=35 # miles per gallon expected to be used driving home
nighttime=False
headlights_out=False

can_drive=(battery_charged and got_car and not drunk and \
               (gas*mpg>=distance) and (nighttime and not headlights_out))

if can_drive:
    print("Drive home")

else:
    print("Don't drive home.")

你的问题在最后一条中:

and (nighttime and not headlights_out)
这表示您只能在夜间亮着前照灯驾驶。你需要说的是,如果是夜间,车头灯必须亮着。最容易编码的逻辑等价物是“必须是白天,否则我的前照灯必须亮着”:


这应该会让你上路……

提示:如果是白天,你会在意车头灯吗?
and (not nighttime or not headlights_out):