Converting types with int() and float()

Tip: Choose Save as PDF in the print dialog to download. Use A4 paper size.

Converting types with int() and float()

Name:
Date:
Class:

Quick Questions

Shade in the A or B circle for each question.

1. A student types 5 when the program runs age = input(). What type is age?
A A string (text): '5'
B An integer: 5
2. Which line lets you add 1 to the number the user types?
A score = input(int('Score: '))
B score = int(input('Score: '))
3. Which function should you use for a price like 4.50?
A int()
B float()
4. What does this code do? age = input('Age: ') then print(age + 1)
A It causes an error because you can't add a number to a string
B It prints the age plus one
5. Which line prints: You are 13 years old (age is the integer 13)?
A print('You are ' + str(age) + ' years old')
B print('You are ' + age + ' years old')

Match the Pairs

Draw a line connecting each item on the left to its match on the right.

Left columnRight column
1age = int(input("Age: ")) AAsks the user to type their age, then turns the typed text into a whole number
2price = float("4.50") Btotal = int(input()) + 5
3Use str() to turn a number into text so it can be joined to other text with + CTurns the text "4.50" into a decimal number
4Convert typed text to a whole number before adding to it Dprint("Score: " + str(score))
5age = input("Age: ") print(age + 1) EYou cannot join a number to text with +. Change the number to text using str(13).
6print("You are " + 13 + " years old") Finput() gives text, not a number. Adding 1 to text causes an error. Wrap input() in int() first.

Definitions

int(input())

Fill in the Blanks

Complete each line of code by filling in the missing parts.

1.
age = ___(input("Age: ")) print(age + 1)
2.
price = _____(input("Price: ")) print(price * 2)
3.
age = 12 print("You are " + ___(age) + " years old")
4.
total = int(_____("Number: ")) * 2 print(total)
5.
num = int(input("Number: ")) print(___ * 2)

Spot the Error

Shade in the letter of the line that contains an error.

Question 1
A age = input("Age: ")
B next_age = age + 1
C print("Next year: " + str(next_age))
Question 2
A name = input("Name: ")
B score = str(input("Score: "))
C total = score + 10
D print(name, total)
Question 3
A print("Shopping")
B price = int(input("Price: "))
C total = price * 3
D print("Total:", total)
Question 4
A n = 5
B msg = "Pets: " + n
C print(msg)
Question 5
A a = int(input("First: "))
B b = int(input("Second: "))
C total = a + b
D print("Sum is " + total)

Answer Key

Converting types with int() and float()
Section A: Quick Questions
Q1
A — A string (text): '5'
Q2
B — score = int(input('Score: '))
Q3
B — float()
Q4
A — It causes an error because you can't add a number to a string
Q5
A — print('You are ' + str(age) + ' years old')
Section B: Match the Pairs
Q1
age = int(input("Age: ")) → Asks the user to type their age, then turns the typed text into a whole number
Q2
price = float("4.50") → Turns the text "4.50" into a decimal number
Q3
Use str() to turn a number into text so it can be joined to other text with + → print("Score: " + str(score))
Q4
Convert typed text to a whole number before adding to it → total = int(input()) + 5
Q5
age = input("Age: ") print(age + 1) → input() gives text, not a number. Adding 1 to text causes an error. Wrap input() in int() first.
Q6
print("You are " + 13 + " years old") → You cannot join a number to text with +. Change the number to text using str(13).
Section C: Definitions
Q1
int(input()) — input() gives text, so int() wraps it to turn the typed text into a whole number you can calculate with. Use float() for decimals. str() turns a number back into text.
Section D: Fill in the Blanks
Q1
age = int(input("Age: ")) print(age + 1)
Q2
price = float(input("Price: ")) print(price * 2)
Q3
age = 12 print("You are " + str(age) + " years old")
Q4
total = int(input("Number: ")) * 2 print(total)
Q5
num = int(input("Number: ")) print(num * 2)
Section E: Spot the Error
Q1
A — input() gives back text, so age + 1 would crash. Wrap it in int(): age = int(input("Age: ")).
Q2
B — str() keeps the value as text, so score + 10 would crash. Use int() to turn the typed text into a whole number.
Q3
B — A price like 2.50 has a decimal point, and int() can't convert text like that. Use float() for decimal numbers.
Q4
B — You can't join a string and an int with +. Turn the number into text first: str(n).
Q5
D — total is a number, so it can't be joined to text with +. Use str(total) to turn it into text.