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.
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.
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
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?
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.
line
shows
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.")
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.
I can calculate with a typed number and print the result.
Running the lesson
Predict: the 55 answer surprises many students. Let them argue before running. Ask the 55 voters to explain, then run it.
Run: run it with 5 and 5, then fix it with int. Ask what changed in the output and why.
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.
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
The total is a long repeated string of digits. Ask: What does input give back, a number or text? Fix: 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. Ask: Which piece of the line is a number? Fix: Put str( ) around that piece, as in the last lesson.
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
Investigate: Typed text and numbers look the same on screen but behave differently: + joins text and adds numbers.
Modify: Once the text is a number, it works in any calculation.
Mix it up: Two answers, two jobs: one stays text, one becomes a number, and the result goes back to text.
Now you can
Say that input always gives back text.
Turn typed digits into a number with int.
Calculate with a typed number and print the result.