← Console bridge: text in, text out

Ask and store

Ask a question with input, store the typed answer in a name, and use it in what the program says back.

A login box, a quiz question and a chatbot all ask something and then use the answer. The answer has to be stored somewhere first.

Last lesson the program said the same thing every time. Now it will ask, wait for you, and use your answer.

PredictRead the code and say what it will do before you run anything. A wrong guess is fine: it shows you what to look for.

Predict

lesson.py
name = input("What is your name? ")
print("Hello, " + name)

input is new. Read the code. If you type Ada when it asks, what will the second line show?

You run it and type Ada. What does the last line show?

RunRun it and compare the result with your prediction.

Run

Run it, type an answer in the console and press Enter. Run it again with a different answer.

Line What it does Why
name = input("What is your name? ") Shows the question, waits for typing, stores the answer in name The words in the brackets are shown to the person. The answer is what they type
print("Hello, " + name) Joins Hello, onto the stored answer and shows it name has no quotes, so Python uses what it holds

The answer is stored as text, whatever is typed. The program is now different every time it runs.

lesson.py
name = input("What is your name? ")
print("Hello, " + name)
Try it yourselfYour edits are saved in this browser.
InvestigateTrace how each line of code connects to what you see.

Investigate

lesson.py
name = input("What is your name? ")
print("Hello, " + name)

Where does the typed text go?

The question is asked, then the answer disappears.

Fill in what name holds and what the last line shows for each answer.

typedname holdslast line shows
AdaAdaHello, Ada
Sam
42

The program asks, but then stops with a NameError on line 2. Click the line that needs to change.

MakeBuild something new using what you have learned. Expect a few tries; that is normal.

Make

Write a program that asks two questions (for example a name and a favourite animal) and prints one sentence that uses both answers.

Try it yourselfYour edits are saved in this browser.
Need a hint?

Store each answer under its own name: one input line per question.

Another hint

Use both names in the final print, with + between the pieces and spaces inside the quotes.

Ask two questions, store both answers, and print one line that uses both.

    Run your program. This ticks when your code does all of the above.

    For teachers

    Students meet input, store the typed answer in a name, and use it in the reply. About 30 minutes.

    Open the teacher guide: answers, solutions, slides and a worksheet.

    Students will be able to

    Running the lesson

    1. Predict: take a vote before running. Wrong options: the name as a word, the prompt stored in the name, no answer at all. Ask one pair from each option to explain: the question is shown, not stored.
    2. Run: students run it several times with different answers. Ask: what changed each time, and what stayed the same?
    3. Investigate: the bug is a very common first slip. Ask what happened to the typed text.
    4. Make: students choose their own two questions. Circulate and ask each to read the program aloud, line by line.

    The hard part (on purpose)

    Remembering that input needs a name to catch the answer. Ask where the typed text went.

    When students get stuck

    Stretch and support

    Stretch: Ask for three things and print a card with one line per answer.

    Support: Give the first question and ask the student to add the second.

    Where it leads

    Next lesson: the answer is always text, so adding a typed number to a number fails. Joining numbers onto text takes str.

    What each step is for

    Now you can
    Next lesson3. Numbers into textJoin a number onto text by turning it into text first with str.