Login

Please fill in your details to login.





lesson 3.1.3 the problem with numbers

Seriously, 73 is a number!



image

Ever had a computer misunderstand you? Today, we're tackling one of the most common (and frustrating!) bugs new programmers face. We'll learn why Python sometimes thinks the number
10
is a word (🙄), and how to teach it to do maths properly using a trick called 'typecasting'. This is a core skill for any Software Developer!

Learning Outcomes
The Building Blocks (Factual Knowledge)
Recall that
input()
always provides a string data type.
Describe the difference between an integer (a number) and a string (text).
Identify a
TypeError
as a type of runtime error.

The Connections and Theories (Conceptual Knowledge)
Explain why trying to add a string and an integer causes a
TypeError
.
Understand that typecasting is the process of explicitly converting data from one type to another.

The Skills and Methods (Procedural Outcomes)
Apply the
int()
function to typecast a string variable to an integer.
Write a Python program that correctly takes numerical input and uses it in a mathematical calculation.

Digital Skill Focus: Apply keyboard shortcuts (e.g., Ctrl+C, Ctrl+V, Ctrl+S) to efficiently write and save code in a text editor.



Data Types?


Data comes in many shapes and forms. To make it easier for the computer to handle the data in the correct way, we group data into one of five main categories...

image
The five main data types.

Why is this important? Because the
input()
statement ALWAYS returns strings and that can cause problems if, for instance, we need to do maths...

The Problem with Numbers


In our last lesson, we learned how to get information from a user using the
input()
function and store it in a variable. But what happens when we try to do maths with that information? Let's find out!

Run the code by pressing the ▶️ button. Remember to press ENTER after you have entered your age.

🤔 Code Explainer
Line 1: Ask the user for their age and store it as a string in the variable
age
.
Line 2: Calculate their age in 5 years by taking their age and adding 5. Store it in the variable
future_age
.
Line 3: Print a polite message with their 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


This type of error is called a Runtime ErrorA error caused by an unexpected occurance during the operation of a program i.e. division by zero, file not found, incorrect data types etc. because it only occurs under certain circumstances after you run your script.

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

Typecasting


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 - changing a variable's type. 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, try running it again by pressing the ▶️ button.

🤔 Code Explainer
Line 1: Ask the user for their age and store it as a string in the variable
age
.
Line 3: Calculate their age in 5 years by converting age into an integer and adding 5. Store it in the variable
future_age
.
Line 5: Print a polite message with their future age.

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


image
Are you a DRIVER or a NAVIGATOR?

time limit
Task The Broken Calculator and the Rectangle Challenge

Work in pairs. One person is the Driver (hands on the keyboard) and one is the Navigator (reads the instructions, thinks, and guides the Driver). Let's build a program that can add two numbers together. Follow the PRIMMI have no idea what this means steps carefully!

🤔 Code Explainer
Line 1: Print a friendly title
Line 2: Ask for the first number and store it in the variable num1
Line 3: Ask for the second number and store it in the variable num2
Line 4: Calculate the sum of the numbers (or does it?)
Line 5: Print the answer in a friendly format.

image
Predict
As a pair, look at the starter code in the widget.
If the driver enters
5
and then
3
, what do you both think the program will print and why?

Run
Now, the Driver runs the code.
Enter
5
and
3
pressing the Enter key after each number.
What was the result? Was your prediction correct?

image

Investigate
Discuss together: 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 using what we just learned about typecasting?

Modify
The Navigator should guide the Driver. Modify the code to correctly convert both
num1
and
num2
into integers before you add them together. Change Line 4 in the program so that it looks like this...

answer = int(num1) + int(num2)


Run the program again. Does it now correctly calculate 5 + 3 = 8?

Make
Swap roles! The Driver is now the Navigator, and the Navigator is the Driver.
In the empty Python Widget below, create a "Rectangle Area Calculator".
Line 1 should ask the user for the
width
of a rectangle.
Line 2 should ask the user for the
height
of a rectangle.
Line 3 should then calculate the area (area = width * height). Remember to use
int()
to typecast your inputs and use an asterix (*) for multiplication.
Line 4 should should print the answer in a clear sentence, like "The area of the rectangle is 30."

image


Outcome: We have worked as a pair to debug a program by identifying a
TypeError
and applying
int()
to typecast string inputs, and then applied this skill to write a new program that performs a calculation.

Checkpoint

image
Today you have learnt the vital difference between strings and integers, and how to use
int()
to typecast data so you can perform calculations.

Out of Lesson Learning

Last modified: November 21st, 2025
The Computing Café works best in landscape mode.
Rotate your device.
Dismiss Warning