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

Thursday

Choices from answers
// Programs that ask questions
⏱ about 15 min

Thursday: Choices from answers

Before every launch, someone must check the hatch. Rocket's program asks, "Hatch sealed?" When Kai types YES in capitals, the program says to stop.

"But I said yes!" Kai says. Raven grins. "What do you notice? We fixed this same problem in week five."

Rocket thinks hard. Nova gives a tiny nod. "Would you like a hint? Make the answer small letters before you check it."

if with input

An if can check what the user typed.

Use lower so YES, Yes and yes all match.

answer = input("Hatch sealed? ")
if answer.lower() == "yes":
    print("Safe to launch")
else:
    print("Stop! Check the hatch")
The screen showsThe user types
Hatch sealed?YES
Safe to launch
The user typesDoes the program say Safe to launch?
Yes?
no?
yes please?
yES?
WHY THIS EXERCISETesting many possible answers is how you find a program's limits.

Numbers and elif

fuel = int(input("Fuel units? "))
if fuel >= 100:
    print("Full tank")
elif fuel >= 40:
    print("Enough for one trip")
else:
    print("Refuel first")
The screen showsThe user types
Fuel units?55
Enough for one trip
TRACE IT
  • Read the question.
  • Tap your answer.
The user types 100. What shows?
The user types 39. What shows?
The user types 40. What shows?
Try it
Ask a grown-up to type pretend fuel numbers on paper.
You be the program. Say the message for each one.

Practice lab: free pods

pods = int(input("Pods free? "))
if pods == 0:
    print("Station full")
elif pods < 3:
    print("Few pods left")
else:
    print("Plenty of room")
The user types 0. What shows?
The user types 2. What shows?
The user types 3. What shows?
PATH CHECK
  • Read the question.
  • Tap your answer.
Which answers say Few pods left?
Why use int on the answer?
Which answer is the edge case for pods < 3?
The user typesDoes it say Plenty of room?
5?
3?
2?
0?
WHY THIS EXERCISE3 is not 0 and not less than 3, so the else runs.
The check is answer.lower() == "go". The user types GO. Does it match? Type yes or no.
WHY THIS EXERCISElower turns GO into go, so the check is True.

Great choices! Tomorrow you will build a whole launch checklist program.

← Wednesday