Three operators combine booleans.
and is True only when both sides are True:
print(5 > 3 and 2 > 1)
print(5 > 3 and 2 > 9)
Output:
True
False
or is True when at least one side is True:
print(5 > 9 or 2 > 1)
print(5 > 9 or 2 > 9)
Output:
True
False
not flips a boolean:
print(not 5 > 9)
Output:
True
Combined conditions read directly in an if or a return:
def can_vote(age, is_citizen):
return age >= 18 and is_citizen
An amusement park ride admits a rider who is at least 140 cm tall and at least 10 years old. A rider accompanied by an adult is admitted at any height, as long as they are at least 10.
can_ride with three parameters: height, age, and with_adult.True when the rider is admitted under the rules above, otherwise False.can_ride(150, 12, False) returns True. can_ride(120, 12, False) returns False. can_ride(120, 12, True) returns True.
Run your code to see the output, then press Submit.
import unittest
class TestCanRide(unittest.TestCase):
def test_tall_enough_and_old_enough(self):
self.assertIs(can_ride(150, 12, False), True)
def test_too_short_without_an_adult(self):
self.assertIs(can_ride(120, 12, False), False)
def test_too_short_but_with_an_adult(self):
self.assertIs(can_ride(120, 12, True), True)
def test_too_young_even_with_an_adult(self):
self.assertIs(can_ride(150, 8, True), False)
def test_exactly_at_both_limits(self):
self.assertIs(can_ride(140, 10, False), True)
def can_ride(height, age, with_adult):
return age >= 10 and (height >= 140 or with_adult)
print(can_ride(150, 12, False))
print(can_ride(120, 12, False))
print(can_ride(120, 12, True))
Sign in to join the discussion.
No comments yet. Be the first to ask a question.
Press Run to execute your code, or Submit to test and complete this problem.