← Teacher guide

Numbers into text

Students learn why a number cannot be joined onto text with + and fix it with str.

Console bridge: text in, text out · Text in, text out · 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 5 min · Mix it up 3 min · Make 8 min

Running the lesson

  1. Predict: show the failing program and vote. Many students expect it to work, and that is the point. Then ask what + was designed to join.
  2. Run: read the friendly error together, then fix it with str.
  3. Investigate: score + score and str(score) + str(score) look alike but give 10 and 55. This prepares for typed numbers next lesson.
  4. Make: students choose their own receipt. Ask each to point to every str in their program.

In class

On the slides at these points. Students can do the whole lesson on their own; these are for the room. In the deck, the Run, Modify and Make slides have the live playground in them (the tutorial link is the backup), and the notes above are in each slide's speaker notes.

The hard part (on purpose)

Remembering which pieces are numbers. Ask which piece has no quotes and works out to a number.

When students get stuck

What you will seeA question to askThe fix
An error that mentions joining a number onto text.Which piece of the line is a number?Put str( ) around that piece only.
str( ) on its own line does nothing.Where does the text copy go?str makes a copy. Use it where the number is joined onto text.

Answers to the checks

  1. Predict: What happens when you run it?
    It stops with an error on line 2. + joins text to text. score holds a number, and Python will not guess how to join a number onto text.
  2. Trace the lines: Fill in what each line shows.
    Score: 6, 10, 55
    score + score adds two numbers: 10. str(score) + str(score) joins two pieces of text: 55.
  3. Spot the bug: This should show Lives: 3, but stops with an error. Click the line that still has the problem.
    line 3
    str(lives) on its own line makes a text copy and throws it away. lives is still a number. Put the str right where it is joined: "Lives: " + str(lives).
  4. Mix it up: The person types Maria. What does the program print?
    Maria has 5 letters.
    len(name) is 5, str(count) is the text 5, and the three pieces join into one line.

Solutions

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

Modify

name = input("What is your name? ")
print("Hello, " + name)
print(name + " has " + str(len(name)) + " letters.")

Make

cups = 3
price = 4
total = cups * price
print(str(cups) + " cups cost $" + str(total))

Stretch and support

Stretch: Print a receipt with a count, a price each, a total and a tip.

Support: Give the line with the numbers joined and ask which pieces need str.

Exit ticket

score = 5. Which prints Score: 5, and why: A) print("Score: " + score) B) print("Score: " + str(score))?

B. + will not join text and a number. str makes the number into text.

What each step is for

Where it leads

Next lesson: what input gives back is text, even digits, so adding two typed numbers needs a conversion the other way.