Login

Please fill in your details to login.





lesson 3.1.3 the problem with numbers

Ooo - an adventure game!

Introduction for the learner

Learning Outcomes
The Building Blocks (Factual Knowledge)

The Connections and Theories (Conceptual Knowledge)

Digital Skill Focus:

The Skills and Methods (Procedural Outcomes)


The Problem with Numbers


Let's try to write a program that asks for a user's age and tells them how old they will be in 5 years.

age = input("How old are you? ")
future_age = age + 5
print("In 5 years, you will be", future_age)

🤔 Code Explainer
Line 1: Ask for user's age and store in the variable
age
.
Line 2: Calculate the user's age in 5 years time.
Line 3: Print out the user's future age.

When we run this, the program crashes! It gives a
TypeError
, saying you can't add a string and an integer.

TypeError: cannot concatenate 'str' and 'int' objects on line 2


Why? Because the
input()
function always gives us a string, even if the user types in numbers. So, Python sees "13" (text) not 13 (a number). Trying to do "14" + 5 is like trying to do "hello" + 5 - it doesn't make sense!

Typecasting: Changing a Variable's Type


To fix this, we need to convert the data type from a stringA sequence of characters delimited with quotation marks. to an integerA whole, positive or negative number.. This process is called typecastingI have no idea what this means. We can do this using the
int()
function. It takes a string containing numbers and turns it into a proper integer that we can do maths with.

Here's the corrected code:

age = input("How old are you? ")
future_age = int(age) + 5
print("In 5 years, you will be", future_age)

🤔 Code Explainer
Line 2: Notice, we've changed
age
to
int(age)
to convert the string to an integer.

This now works perfectly! Understanding data types and knowing when to typecast is a super important skill for any Software Developer.


time limit
Task 3 A Simple Calculator
Let's build a program that can add two numbers together. Follow the PRIMM steps carefully!

🤔 Code Explainer
Line 1: Print a nice title.
Line 2-3: Ask user for two numbers and store them in variables.
Line 4: Add the numbers together (or does it?).
Line 5: Print out the answer.

1
Predict
Look at the starter code in the widget.
If the user enters 5 and then 3, what do you think the program will print? Why?
2
Run
Now run the code.
Enter
5
and
3
.
What was the result?
Was your prediction correct?
3
Investigate
The program printed "53", not 8! This is because
input()
gives us strings. The
+
symbol concatenated (joined) the strings "5" and "3" instead of adding them as numbers. How can we fix this?
4
Modify
Modify the code to correctly convert both num1 and num2 into integers before you add them together.
Run the program again. Does it now correctly calculate 5 + 3 = 8?
5
Make
In the empty Python Widget below, create a "Rectangle Area Calculator".
It should ask the user for the
width
and
height
of a rectangle.
It should then calculate the
area
(
area = int(width) * int(height)
) using typecastingI have no idea what this means.
Finally, it should print the answer in a clear sentence, like "The area of the rectangle is 30."
Use the Print to PDF button to print out your work and hand it to your teacher.


Outcome: I have written a Python program that takes number inputs from a user, correctly converts them from strings to integers using typecasting, and performs a mathematical calculation with them.

Checkpoint

image
Today you have learnt...


Out of Lesson Learning

Last modified: October 27th, 2025
The Computing Café works best in landscape mode.
Rotate your device.
Dismiss Warning