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 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 shows | The user types |
|---|---|
| Pilot name? | Kai |
| Welcome aboard, Kai |
place = input("Where to? ")
print("Course set for", place) | Statement about input | True 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. | ? |
ship = input("Ship name? ")
gate = input("Gate? ")
print(ship, "leaves from gate", gate) | The screen shows | The user types |
|---|---|
| Ship name? | Drift |
| Gate? | 6 |
| Drift leaves from gate 6 |
| Statement | True 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. | ? |
Your programs can listen now! Tomorrow you will find out what kind of value input gives back.