Logic Gates
The ancient vault requires two keys to open. Or perhaps a master key? Hoppy needs to combine conditions.
Python uses and, or, and
not to combine logic.
Logic Spells
has_key = True
knows_password = True
is_admin = False
# AND: Both must be true
if has_key and knows_password:
print("Access Granted")
# OR: Both must be true
if has_key or is_admin:
print("Can Enter")
Your Mission
1
Inventory
Set has_wand to True and
has_magic to False.
2
Strict Requirement
Write an if statement that checks if you have both the
wand and magic. Print "Cast Spell".
3
Flexible Requirement
Write another if (separate from the first) that checks if you
have either the wand or magic. Print
"Can Practice".
Suggested SolutionExpandCollapse
ExpandCollapse
Solution:
Here is the logic:
has_wand = True
has_magic = False
if has_wand and has_magic:
print("Cast Spell")
if has_wand or has_magic:
print("Can Practice")Advanced TipsWant more? Click to expandClick to collapse
You can chain them! But be careful with order. Parentheses ()
help clarify.
if (has_wand and has_magic) or is_admin:
print("Super Access")Loading...
Terminal
Terminal
Ready to run...