The station sends hundreds of short status messages every day. Raven wants an alert whenever a message mentions low supplies. Rocket starts reading them one by one.
"Water ok. Air ok. Fuel low on deck 3." He rubs his eyes. "There are so many!"
Raven asks, "What do you notice about the messages we care about? They all have the same little word inside." Nova blinks. "Would you like a hint? Python can check if one string is hiding inside another."
in checks whether one string is inside another. It gives True or False.
in cares about capitals. "Fuel" and "fuel" are different.
log = "fuel low on deck 3"
print("low" in log)
print("high" in log)
print("Fuel" in log)
if "deck" in log:
print("Deck alert!") | The screen shows |
|---|
| True |
| False |
| False |
| Deck alert! |
| Check | Does Python say True? |
|---|---|
| "air" in "air ok" | ? |
| "ok" in "Book" | ? |
| "OK" in "air ok" | ? |
| "tape" in "tap" | ? |
msgs = ["water ok", "fuel low", "air ok", "tape low"]
for m in msgs:
if "low" in m:
print("Check:", m) | The screen shows |
|---|
| Check: fuel low |
| Check: tape low |
msgs = ["Hatch open", "hatch closed", "Lamp on", "Hatch closed"]
count = 0
for m in msgs:
if "Hatch" in m:
count = count + 1
print(count) msgs = ["air ok", "Air low", "water low", "lamp ok"]
for m in msgs:
if "low" in m:
print("Alert:", m)
else:
print("Fine:", m) | Check | Does Python say True? |
|---|---|
| "star" in "starlight" | ? |
| "light" in "Starlight" | ? |
| "Star" in "starlight" | ? |
| "rats" in "star" | ? |
Great searching! Tomorrow you will mix slices, upper and loops to make crew tags.