Python · Boolean in Depth
Test OR, AND, NOT
The full text of this chapter, to read or revisit. To practise, do the exercises.
▶ Do the exercisesExercise 1 — AND or OR?
You need to check if your Codémons can train. For that, you need to check if they're both ready to fight. If both conditions are met, you can train them.
codemon_1_ready = True
codemon_2_ready = True
can_train = codemon_1_ready ____ codemon_2_ready
print(can_train)
Options: and or not
Hint : Remember: And is used to check if both conditions are True. Or is used to check if at least one condition is True.
Exercise 2 — AND or OR?
First phase of training is done, for the second phase, you're going to battle someone else, you only need one Codémon to be ready to fight. If at least one of them is ready, you can train them.
codemon_1_ready = True
codemon_2_ready = False
can_train = codemon_1_ready ____ codemon_2_ready
print(can_train)
Options: and or not
Hint : Remember: And is used to check if both conditions are True. Or is used to check if at least one condition is True.
Exercise 3 — Boolean Logic in Action
Your Codémon needs rest if it's either tired OR injured. What operator should you use?
Options
• or
• and
• not
• None of these
Hint : Use OR when at least one condition needs to be true. If the Codémon is tired OR injured, it needs rest.
Exercise 4 — To train or not to train?
Your Codémon can only fight if it's NOT sleeping. Complete the code:
is_sleeping = False
can_fight = ____ is_sleeping
print(can_fight)
Options: not and or !
Hint : NOT operator inverts a boolean value. If is_sleeping is True, then ```not is_sleeping``` is False.
Exercise 5 — Match the Scenario
Match each scenario with the correct boolean operator:
Items to match
• Codémon can evolve if level >= 30 AND has evolution stone
• Codémon needs healing if HP < 20 OR poisoned
• Codémon can enter battle if NOT fainted
• Shop is open if day_time OR emergency
Definitions
• and
• or
• not
• or
Exercise 6 — Complex Boolean Expression
What is the result of:
True and (False or True)
?
Options
• True
• False
• Error
• None
Hint : First evaluate parentheses: (False or True) = True. Then: True and True = True.
Exercise 7 — Complete the Logic
A Codémon can use a special attack if it has enough MP AND it's not paralyzed. Complete the expression:
has_enough_mp, is_paralyzed = True, False
can_use_special = has_enough_mp ____ ____ is_paralyzed
print(can_use_special)
Options: and or not & |
Hint : You can combine and and not operators. Use AND to require both conditions, and NOT to invert is_paralyzed (not paralyzed means can act).
Back to the course