← Console bridge: text in, text out

Text into numbers

What input gives back is always text, so turn it into a number with int before calculating with it.

Every app that asks for an age, a quantity or a PIN has to turn what you typed into a number before it can calculate.

Last lesson you turned a number into text. Typed answers go the other way: they arrive as text, even when the person types digits. To calculate with them, the program turns the text into a number.

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
a = input("First number: ")
b = input("Second number: ")
print(a + b)

The program asks for two numbers and prints a + b. What will it print?

You run it and type 5, then 5. What does it print?

RunRun it and compare the result with your prediction.

Run

Run it with 5 and 5. Then change the first two lines to a = int(input("First number: ")) and b = int(input("Second number: ")), and run it again.

Line What it does Why
input("First number: ") Gives back the typed answer as text Typed digits are still text, "5" not 5
int(...) Turns text made of digits into a whole number Now + adds instead of joining
print(a + b) Shows the total Both are numbers, so there is nothing to convert back to text
lesson.py
a = input("First number: ")
b = input("Second number: ")
print(a + b)
Try it yourselfYour edits are saved in this browser.
InvestigateTrace how each line of code connects to what you see.

Investigate

lesson.py
a = input("First number: ")
b = input("Second number: ")
print(a + b)
print(int(a) + int(b))
print(a + " and " + b)
print(int(a) * 2)

The same two answers, different lines.

A ticket machine with a strange total.

You type 5 for the first answer and 3 for the second. Fill in what each line shows.

lineshows
print(a + b)53
print(int(a) + int(b))
print(a + " and " + b)
print(int(a) * 2)

A ticket costs $12. You type 3, and it prints That will be $333333333333. Click the line that causes it.

ModifyChange the code on purpose and check the result.

Modify

The program asks for an age and says what it will be next year. Change it so it also says what the age will be in ten years.

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

Add a line under the first print, using age + 10. Remember str for the number.

Also print the age in ten years.

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

    InvestigateTrace how each line of code connects to what you see.

    Mix it up

    This mixes the last three lessons: ask and store, text into a number, a number back into text.

    lesson.py
    name = input("What is your name? ")
    age = int(input("How old are you? "))
    print(name + " will be " + str(age + 1) + " next year.")
    
    The person types Sam, then 11. What does the program print?

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

    Make

    Tickets to the school play cost $12 each. Write a ticket machine that asks how many tickets and prints the total, such as That will be $36. Test it with 3. (Typing a word will stop it for now. Two lessons from now it learns to cope.)

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

    tickets = int(input("How many tickets? ")) turns the answer into a number as it is stored.

    Another hint

    Work out the total, then use str() to join it onto the text.

    Ask how many tickets, turn the answer into a number, and print the total cost.

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

      For teachers

      Students learn that input gives back text, and turn it into a number with int. About 30 minutes.

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

      Students will be able to

      Running the lesson

      1. Predict: the 55 answer surprises many students. Let them argue before running. Ask the 55 voters to explain, then run it.
      2. Run: run it with 5 and 5, then fix it with int. Ask what changed in the output and why.
      3. Investigate: the trace shows the same two answers giving 53 and 8. Ask students to say which lines work with text and which with numbers.
      4. Make: students use their own price. Ask them to test with a few quantities.

      The hard part (on purpose)

      Believing the typed 5 is already a number. Ask whether quotes are around it.

      When students get stuck

      Stretch and support

      Stretch: Ask for two numbers and print their sum, difference and product.

      Support: Give the program with int missing and ask the student where it goes.

      Where it leads

      Next lesson: what happens when someone types a word instead of a number, and how a program checks first.

      What each step is for

      Now you can
      Next lesson5. Check firstAsk the typed text whether it is all digits with isdigit, and use an if to choose, so a typo gets an answer instead of a crash.