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

Monday

A program that asks
// Programs that ask questions
⏱ about 15 min

Monday: A program that asks

New pilots arrive at Beacon Station every week. Rocket wants the welcome screen to greet each one by name. "But I do not know their names before they arrive," he says.

Raven watches a pilot tap the screen. "What do you notice? The pilot could tell the program their name."

Nova makes a blinking cursor appear in the air. "Would you like a hint? Python can ask a question and wait for an answer."

input asks and waits

input shows a question, called a prompt. Then the program waits.

When the user types an answer and presses Enter, input gives back what they typed.

Store the answer in a variable so you can use it.

name = input("Pilot name? ")
print("Welcome aboard,", name)

Here is one run of the program. The pilot typed Kai.

The screen showsThe user types
Pilot name?Kai
Welcome aboard, Kai
  • The prompt ends with a space, so the answer does not touch the question mark.
  • Every run can be different. It depends on what the user types.
  • To trace a program with input, you need to know the answer the user gave.
place = input("Where to? ")
print("Course set for", place)
The user types Ring Station. What does the last line on the screen say?
WHY THIS EXERCISETracing with a given answer is how you test a program that asks questions.
Statement about inputTrue or false?
input shows a prompt on the screen.?
The program waits until the user presses Enter.?
input knows the answer before the user types it.?
You should store the answer in a variable.?
WHY THIS EXERCISEinput is how a program listens. These rules explain how it listens.
WHICH LINE?
  • Read the question.
  • Tap your answer.
Which line asks for a gate and stores the answer?
Try it
Play program with a grown-up.
You say a prompt, like Favorite planet? They answer. You say the welcome line.

Practice lab: two questions

ship = input("Ship name? ")
gate = input("Gate? ")
print(ship, "leaves from gate", gate)
The screen showsThe user types
Ship name?Drift
Gate?6
Drift leaves from gate 6
How many prompts show in one run?
What does ship hold after line 1 in the run above?
The user types Arrow, then 3. What is the last line?
INPUT CHECK
  • Read the question.
  • Tap your answer.
What is the prompt in gate = input("Gate? ")?
When does line 2 run?
Why does each prompt end with a space?
StatementTrue or false?
Each run can show a different output.?
The answer is stored in the variable on the left.?
The user types the prompt.?
A program can ask more than one question.?
WHY THIS EXERCISEThe program shows the prompt. The user types the answer.
The user types Nova, then 2. What word comes first on the last line?
WHY THIS EXERCISEship holds the first answer, so it comes first.

Your programs can listen now! Tomorrow you will find out what kind of value input gives back.