Technoob
Technoob

Reputation: 59

Bool issues. And, or, and or, or not

I'm trying to run a code using bool but I'm having issues with getting the code to work. Basically I have to use the code to decide if it's safe to drive after meeting certain criteria. The battery has to be charger(True), I have to have a car(True), I cannot be drunk(False), I must have enough gas to get where I'm going, and if it's night time I must have working head lights. So far I've manged to get all the way through to the gas part but I cant seem to figure out the and, or, and not, or not statements when it comes to driving at night or day with working and non-working head lights.

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.")

Upvotes: 1

Views: 60

Answers (1)

Prune
Prune

Reputation: 77847

Your problem is in the final clause:

and (nighttime and not headlights_out)

This says that you can drive only at night with your headlights on. What you need to say is that if it is nighttime, the headlights must be on. The logical equivalent easiest to code is "It must be daytime, or my headlights must be on":

and (not nighttime or not headlights_out):

That should get you on the road ...

Upvotes: 1

Related Questions