Scores, prices, countdowns and receipts all mix words with numbers. Python will not guess how to join them, so you tell it.
What you should already know: Print text · Show text on the screen with print, and join pieces of text into one line with +. Name a value · Give a number a name, then let the program work out the rest. Last lesson the program joined text it was given. Now it will join a number onto text, such as Score: 5.
How this lesson works: Predict, Run, Investigate, Modify, Make Predict : Read the code and say what it will do before you run anything. A wrong guess is fine: it shows you what to look for.Run : Run it and compare the result with your prediction.Investigate : Trace how each line of code connects to what you see.Modify : Change the code on purpose and check the result.Make : Build something new using what you have learned. Expect a few tries; that is normal.
? Explain Predict Run Investigate Modify Mix it up Make
Explain it to me Close str turns a number into text so + can join it · Sound on for the voiceover Voice: Emma George Heart + joins text to text, and adds a number to a number, but will not mix them. To join a number onto text, turn it into text first: str(score) gives back a text copy, such as "5". The number itself stays a number.
Calculations can go inside: str(cups * price).
Make your prediction first. Help opens once you've locked it in.
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 Copy
score = 5
print ("Score: " + score)
You know + joins text. What do you think happens when one of the pieces is a number?
RunRun it and compare the result with your prediction.
Run Run it and read the message. Then change line 2 to print("Score: " + str(score)) and run it again.
Line
What it does
Why
score = 5
Stores the number 5
No quotes, so it is a number, not text
"Score: " + score
Fails
+ joins text to text, or adds number to number. It will not mix them
str(score)
Gives back the text "5"
str makes a text copy of the number. score itself stays a number
lesson.py Copy
score = 5
print ("Score: " + score)
Try it yourself Your edits are saved in this browser. Full screen
InvestigateTrace how each line of code connects to what you see.
Investigate lesson.py Copy
score = 5
print ("Score: " + str (score))
print ("Score: " + str (score + 1 ))
print (score + score)
print (str (score) + str (score))
1 · Trace the lines 2 · Spot the bug
Adding numbers and joining text look the same, but they are different.
The str is there, but not in the right place.
Fill in what each line shows.
Check table
This should show Lives: 3, but stops with an error. Click the line that still has the problem.
lives = 3str(lives)print("Lives: " + lives)
ModifyChange the code on purpose and check the result.
Modify Change the program so it also prints how many letters the name has.
Try it yourself Your edits are saved in this browser. Full screen
Need a hint? Before a hint: what kind of stuck is this? Try a strategy first
len(name) gives the number of letters. It is a number, so it needs str.
Print how many letters the name has, such as Ada has 3 letters.
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 Mix of the last two lessons: the answer is stored, a number is worked out from it, and the number is turned into text.
lesson.py Copy
name = input ("What is your name? " )
count = len (name)
print (name + " has " + str (count) + " letters." )
MakeBuild something new using what you have learned. Expect a few tries; that is normal.
Make A café sells cups of hot chocolate. Write a receipt: store the number of cups and the price, work out the total, and print a line such as 3 cups cost $12.
Try it yourself Your edits are saved in this browser. Full screen
Build up to it Need a hint? Before a hint: what kind of stuck is this? Try a strategy first
Work out the total first: total = cups * price. Then turn it into text in the print line.
Another hint Each number joined onto text needs its own str().
Print a receipt line that joins a count and a calculated total with text.
Run your program. This ticks when your code does all of the above.
Lesson done. You've finished every step you need.
Now you can Say why "Score: " + 5 fails. Use str to join a number onto text. Build a line from text, a name and a calculated number. Next lesson 4. Text into numbers What input gives back is always text, so turn it into a number with int before calculating with it.
For teachers Students learn why a number cannot be joined onto text with + and fix it with str. About 30 minutes.
Open the teacher guide : answers, solutions, slides and a worksheet.
Students will be able to I can say why "Score: " + 5 fails. I can use str to join a number onto text. I can build a line from text, a name and a calculated number. Running the lesson 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. Run: read the friendly error together, then fix it with str. Investigate: score + score and str(score) + str(score) look alike but give 10 and 55. This prepares for typed numbers next lesson. Make: students choose their own receipt. Ask each to point to every str in their program. 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 An error that mentions joining a number onto text. Ask: Which piece of the line is a number? Fix: Put str( ) around that piece only.str( ) on its own line does nothing. Ask: Where does the text copy go? Fix: str makes a copy. Use it where the number is joined onto text.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.
Where it leads Next lesson: what input gives back is text, even digits, so adding two typed numbers needs a conversion the other way.
What each step is for Investigate: str makes a text copy of a number. The copy joins with +; the original is unchanged.Modify: Anything that works out to a number can go inside str, including a calculation.Mix it up: Ask, store, calculate, convert, print: each move does one job.