Introduction:
When we want to ask the user for some information during a program’s execution, we use the input()
function in Python. This function allows us to prompt the user to type something, which the program can store in a variable and use later. For example, if you’re building a game that asks for the player's name or a quiz that requires an answer, you’ll need to get user input.
One key thing to remember is that whatever the user types into the program using input()
is stored as a string. A string is just a fancy word for text. Even if the user enters numbers, they are stored as text until we convert them into another data type, like an integer or a float.
Here’s a quick example of using input()
to get a user's name:
name = input("What is your name? ") print("Hello, " + name + "!")
When you run this code, the program will pause, wait for the user to type something, and then store it in the variable name. Once the user presses Enter, the program will print a personalised greeting.
Predict:
Let’s start by looking at a simple program that uses input()
to get a user’s favourite colour. Can you predict what the output will be if the user types "blue"?
favourite_colour = input("What is your favourite colour? ") print("Your favourite colour is " + favourite_colour)
Task: Before moving on, take a moment to think about what this program does. What do you think will happen if the user types "blue"?
Run:
Now it’s your turn to run a similar program. The code below will ask for the user's age and then print a message back to the user.
Instructions: Click "Run" in the IDE above and enter your age. What happens after you enter your age? Notice how the program responds to your input.
Investigate:
Let’s take this a bit further. In the following program, we’ll ask for both the user’s first name and their favourite hobby, then display a personalised message. Can you see how the two pieces of input are being combined?
Instructions: Run this code in the IDE. What happens when you enter your name and hobby? Try entering different values to see how the program changes the output. Pay attention to how the input()
function stores everything as a string, even if you enter numbers.
Modify:
Before we dive into fixing the code, let's first talk about the problem and introduce a new concept: casting.
When you use input()
to get information from the user, Python treats whatever they type as a string. You can think of a string as text stored in a special box. Even if the user types a number, like 150, Python stores it as if it were the word "150," not the number 150.
Now, you might wonder: what's the difference? Well, imagine trying to do maths with words. If you try to calculate with the word "150," Python gets confused because words don’t work for mathematical operations.
That’s where casting comes in. Casting means converting one type of data into another. In this case, we want to convert the string (text) into an integer (a whole number), so we can do calculations with it.
Think of it like this: casting is like taking the contents of the box (the string) and changing what kind of box it's in. By using the int()
function, we’re telling Python to take the number inside the string and place it inside a box that’s meant for numbers, so we can do maths with it.
Here’s how you convert the user’s input from a string to an integer:
height = input('What is your height in cm? ') height = int(height) # This converts the string into an integer print("Your height in cm is", height)
By doing this, Python knows that height is now a number, and you can perform calculations with it.
Now, let’s fix the code! The program below asks for the user’s height but tries to do maths with a string. Your task is to modify it so that the input is converted into a number (integer) using int()
, or if the height could be in decimals, use float()
to handle decimal numbers.
Hint: You'll need to convert the input using int()
or float()
before trying to do any calculations. Can you fix the code so that it calculates the height in metres correctly?