Launch day checks have three steps: hatch sealed, fuel loaded, crew seated. Right now Rocket asks each one out loud and keeps score on his fingers.
"Let's build it!" he says. "The program can ask, count and decide." Raven nods. "What do you notice about the three checks? They are a list."
Nova lines up three glowing checkboxes. "Would you like a hint? A loop can ask a question for each item in a list."
checks = ["hatch sealed", "fuel loaded", "crew seated"]
ready = 0
for c in checks:
answer = input(c + "? ")
if answer.lower() == "yes":
ready = ready + 1
print(ready, "of", len(checks), "ready")
if ready == len(checks):
print("Cleared for launch!")
else:
print("Hold the launch") | The screen shows | The user types |
|---|---|
| hatch sealed? | yes |
| fuel loaded? | no |
| crew seated? | yes |
| 2 of 3 ready | |
| Hold the launch |
items = ["helmet", "gloves", "boots"]
missing = []
for i in items:
answer = input("Have " + i + "? ")
if answer.lower() != "yes":
missing.append(i)
print("Missing:", len(missing))
if len(missing) > 0:
print("Get:", ", ".join(missing)) | The screen shows | The user types |
|---|---|
| Have helmet? | yes |
| Have gloves? | no |
| Have boots? | yes |
| Missing: 1 | |
| Get: gloves |
| Statement | True or false? |
|---|---|
| The loop asks one question for each item. | ? |
| A YES answer adds the item to missing. | ? |
| missing starts empty. | ? |
| Adding "visor" to items needs a new input line. | ? |
Week nine is done! Your programs can ask, decide and answer. Next week, you will package code into functions that give back answers.