design, write and debug programs that accomplish specific goals, including controlling or simulating physical systems; solve problems by decomposing them into smaller parts.

use logical reasoning to explain how some simple algorithms work and to detect and correct errors in algorithms and programs

4.2b

LO: To be able to cast variables.

We have looked at getting input from the user and using our guidelines to write readable programs for users.


If we remember 4.2a - To know how to use and change a variable then we know that variables come in different flavours - ints and strings: int = integers (whole numbers) and strings (letters, words, phrases and sentences) surrounded by quotes (" "). Here these differences become very important because the input() function will return the input as a string. Let's look at what this means for us:

When using this code we will have to remember that the function input() will return the value of a string to the variable number. From what we know of 4.2a we can't do calculations on strings, therefore we will have to do something to the variable if we want to do mathematical operations on it, for that we will have to introduce the process of casting!


Casting can be seen as the same process that 'casting' has in making films - an actor is cast into a role - they will perform in a certain way.


Casting in programming is a way of turning one variable type into another, with some restrictions...

#turn a string into an int
#assign it to a new variable if the input is 'number'
new_number = int(number)

#the variable has to be a number, '1' turns into 1
#the variable has to be a number, you can't turn 'one' into 1

			

The function int() takes in a string as an argument and the returns the value as an int which is stored/remembered in the variable new_number. With this new variable you are able to perform mathematical operations on it (you couldn't if it was a string).

Let's look at this in a simple example:

#Adding 10

#Introduce the program - tell the user what the profram does
print("This program will add ten to the number you input.")

#Explain how to use the program - use a question as a string argument 
#We will also need to assign the input to a variable to store (remember) what the user types in as a response to the question
number = input("Enter a number: ") #add a colon and space for clarity

#cast the input (string) to an int
new_number = int(number)

#Hide the algorithim from the user - we don't need to print this out for the user to see
#Present the answer in a readable way
print(new_number + 10)

			

If we did not cast the variable number as an int we would encounter an error message - remember you can't add a number to a word.


Let's add a question to our greeting program:

#Greeting program 3

#Introduce the program - tell the user what the program does
print("This program will ask your name and age and then say hello to you.")

#Explain how to use the program - use a question as a string argument 
#We will also need to assign the input to a variable to store (remember) what the user types in as a response to the question
name = input("What is your name? ") #add a space for clarity
age = input("What is your age: ") #add a colon and space for clarity

#Hide the algorithim from the user - don't print intermediate stages
#cast the variable 'age' from a string to an int
new_age = int(age)

#Present the answer in a readable way - we can separate the variable (name) with the rest of the responses (strings) by using a comma
print("Hello", name, "you are", new_age, "years old," "have a nice day") #the comma will add a space automatically between the string and the variable

			

Code Examples

Resources