
Predict
Side Quest: Trace the Gate Rules
To trace code means to follow it one line at a time, like a
finger on a map. With a nested if, answer the outer question
first. Only go inside a branch if its test was true. Skip
everything inside the branches that weren't taken.
A handy trick: write down each call's values, then tick every check as True or False as you go.
The gatekeeper of Lumen follows these rules. Trace all four calls.
def gate(pass_color, cargo):
if pass_color == "gold":
if cargo > 50:
return "inspect"
else:
return "open"
elif pass_color == "silver":
if cargo == 0:
return "open"
else:
return "wait"
else:
return "closed"
print(gate("gold", 50))
print(gate("silver", 3))
print(gate("bronze", 0))
print(gate("Gold", 10))