Boolean logic refers to logic dealing with boolean (True or False) values. For example,
Dogs must have four legs and weigh less than 100 kilograms. (Both conditions must be true)
Cars are cool if they go faster than 200 MPH, or if they are electric. (At least one condition must be true)
As we discussed earlier, the logical operators and and or can be used to perform boolean logic.
The and operator returns True if both of the conditions on either side evaluates to True:
def is_dog(num_legs, weight):
return num_legs == 4 and weight < 100
Let's go over how this function evaluates the parameters num_legs=4 and weight=99:
return 4 == 4 and 99 < 100
return True and True
return True
Let's see what would happen with 3 and 98 instead:
return 3 == 4 and 98 < 100
return False and True
return False
The or operator returns True if at least one of the conditions on either side evaluates to True:
def is_car_cool(speed, is_electric):
return speed > 200 or is_electric
Let's use a non-electric car that can do 250:
return 250 > 200 or False
return True or False
return True
We need a way for our game to track whether a character's attack hits or misses.
Complete the does_attack_hit function. The function should return True if either of the following conditions are met:
attack_roll is not a 1 and the attack roll is greater than or equal to the armor_class, or20Otherwise, it should return False.
Remember that you can define the order of operations using parentheses:
# The college admits students with a high GPA and high SAT score
# or anyone who is just rich
should_admit = (high_gpa and high_sat_score) or (is_rich)