← Back to course
Code Crew G4 / Week 09 / Friday
5/6
Week 09 Β· Input, process, output

Friday

The launch checklist
// Programs that ask questions
⏱ about 15 min

Friday: The launch checklist

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."

Loop, ask, count, decide

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 showsThe user types
hatch sealed?yes
fuel loaded?no
crew seated?yes
2 of 3 ready
Hold the launch
  • c + "? " joins the check name and a question mark to make each prompt.
  • The counter ready goes up by one for each yes.
  • After the loop, the program compares ready to the number of checks.
The user types yes, yes, yes. How many checks are ready?
What is the last line then?
The user types Yes, YES, nope. How many checks are ready?
WHY THIS EXERCISElower makes the checklist friendly to however people type.
ADD A CHECK
  • Read the question.
  • Tap your answer.
The crew adds "lights on" to the checks list. What else must change for the program to still work?
At the computer
This is never required.
If a grown-up has set up Python from python.org, type the checklist into IDLE.
Run it a few times with different answers.

Practice lab: the space walk kit

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 showsThe user types
Have helmet?yes
Have gloves?no
Have boots?yes
Missing: 1
Get: gloves
The answers are no, no, yes. What number shows after Missing:?
For that run, which item shows first after Get:?
The answers are yes, yes, yes. How many lines show after the prompts?
KIT CHECK
  • Read the question.
  • Tap your answer.
What does != mean?
What is the first prompt?
The answers are no, no, no. What shows after Get:?
StatementTrue 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.?
WHY THIS EXERCISELists and loops let a program grow without new lines of code.
The answers are Yes, nah, YES. How many items are missing?
WHY THIS EXERCISEOnly nah is not yes, so one item is missing.
Try it
Read the kit program out loud to a grown-up.
They answer each prompt.
You keep the missing list on paper.

Week nine is done! Your programs can ask, decide and answer. Next week, you will package code into functions that give back answers.

← Thursday