Combine two conditions — quick review
Here are two signs invented for this lesson.
When A and B are each True or False: A and B is True only if both are True. A or B is True if at least one is True. not A is True if A is False, and False if A is True.
Without parentheses, comparisons group first, then not, then and, then or. Python works out the left side of and or or first, and works out the right side only when the left side has not settled the answer.
Write the rule in words and underline each joining word: and, or, not.
Write each part as a complete condition. A part is a comparison with its own left side, operator and right side, such as
height >= 120, or a name that already holdsTrueorFalse, such ashas_ticket.Where the rule negates a part, write
notin front of that part, such asnot has_coat.Join the parts with
andwhere both must hold and withorwhere one is enough.If the rule mixes
andwithor, put parentheses around the part the rule treats as one group, as in(member or total > 50) and coupon.Fill in a small table with one row for each case of the parts, True or False, and work out the whole condition for every row.
Each side of and or or must be an expression that stands on its own. English lets you say "older than 12 and younger than 18". In Python, < 18 is not an expression, so age > 12 and < 18 stops with a SyntaxError. Write age > 12 and age < 18.
The slip that raises no error is reply == "y" or "yes". Python reads it as (reply == "y") or "yes". The right side is the text "yes" on its own, and Python counts any text that is not empty as true. The condition therefore passes for every reply. Write reply == "y" or reply == "yes".
When and and or share a line, and groups more tightly. member or total > 50 and coupon means member or (total > 50 and coupon). If you mean the other grouping, write the parentheses.