
Code
Is It In There?
in checks whether one string appears inside another.
not in checks the opposite:
note = "Beware the glass bridge"
print("glass" in note) # True
print("Glass" in note) # False – case matters!
print("gold" not in note) # True
Use .lower() first when case shouldn't matter:
"glass" in note.lower().
Checking for None
None means "no value". To check for it, use is None (or
is not None), not ==:
keeper = None
if keeper is None:
print("No keeper yet")
is asks "is this the very same object?" There is only
one None in all of Python. So is None is exact, and it's
the officially recommended style.
Always check for None before calling methods on a value.
None.lower() crashes with an AttributeError.
Assignment
Complete check_crate(label). label is a string, or None
when the crate has no label:
- If
labelisNone, return"unlabelled". - If
"fragile"appears in the label (any capitals), return"handle with care". - Otherwise return
"ok".
Complete check_crate(label). label is a string, or None
when the crate has no label:
- If
labelisNone, return"unlabelled". - If
"fragile"appears in the label (any capitals), return"handle with care". - Otherwise return
"ok".
main.py Python sleeping
Press Run to try your code, then Check to test it.