The idea
Tony’s door may open only when two facts hold: the reader has a key and the code is correct. Write down the facts separately before combining them. == compares values, != means not equal, and < and > compare order. and requires both conditions; or accepts either; not reverses a truth value. You can trace these small questions without guessing what the whole program does.
A tiny experiment
has_key = True
code = "407"
if has_key and code == "407":
print("Enter the workshop.")
else:
print("Check the evidence.")Reveal the expected output
Enter the workshop.
Read it one step at a time
- The two variables describe the situation before the decision.
- The condition asks two questions. Both are true, so Python runs the first block.
- The else block is the alternative. It is aligned with if, not with the indented print.
Make one change
Change only has_key to False. Then restore it and change only the code. What happens each time?
Check your thinking
Either change makes the combined condition false, so both experiments print Check the evidence.
Keep following the trail
- Activity 05: Which note matches?
- Activity 08: Choose the allowed commands
- Activity 11: Build your own choice
Sources & further reading
Companion notes for Coding Adventure by James Colby. Historical claims link to their sources; the examples are written for this learning trail.