← Teacher guide

Text into numbers

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

Console bridge: text in, text out · Numbers from text · about 30 minutes · Open the student lesson

Classroom resources

Load these into Waypoints Prepare: choose Load URL and paste the copied address, or download the file and import it. In Prepare, the worksheet's Teacher edition shows the answers; the student copy hides them.

Students will be able to

Timing

Predict and Run 8 min · Investigate 8 min · Modify 4 min · Mix it up 3 min · Make 7 min

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

What you will seeA question to askThe fix
The total is a long repeated string of digits.What does input give back, a number or text?Put int( ) around the input line so the answer is stored as a number.
An error when adding a number to text in the print line.Which piece of the line is a number?Put str( ) around that piece, as in the last lesson.

Answers to the checks

  1. Predict: You run it and type 5, then 5. What does it print?
    55. Both answers are text, "5" and "5", and + joins text, so the result is "55".
  2. Trace the lines: You type 5 for the first answer and 3 for the second. Fill in what each line shows.
    8, 5 and 3, 10
    With a and b as text, a + b joins them: 53. int(a) + int(b) adds the numbers: 8. A number is only needed when the program calculates.
  3. Spot the bug: A ticket costs $12. You type 3, and it prints That will be $333333333333. Click the line that causes it.
    line 2
    tickets holds the text "3", and text times 12 repeats it 12 times. Turn it into a number first: int(tickets) * 12.
  4. Mix it up: The person types Sam, then 11. What does the program print?
    Sam will be 12 next year.
    name stays text. int makes age the number 11, age + 1 is 12, and str makes the 12 into text so it joins.

Solutions

One possible solution for each task. Other correct programs are fine.

Modify

age = int(input("How old are you? "))
print("Next year you will be " + str(age + 1) + ".")
print("In ten years you will be " + str(age + 10) + ".")

Make

tickets = int(input("How many tickets? "))
print("That will be $" + str(tickets * 12) + ".")

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.

Exit ticket

a = input("?") and the person types 7. What is a + a, and what is int(a) + int(a)?

a + a is 77 (text joined). int(a) + int(a) is 14.

What each step is for

Where it leads

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