Year 7 · Y7.3 Introduction to Python · Lesson 2
Stretch tasks Input and data types
Quiz Challenge
A quiz game against four computer players, then short and long written answers.
- Quiz Blitz12 questions against the clock
- Quick Fire4 short answers
- Big Think2 long answers
About 20 minutes
This or That
Pixel the cat asks 8 questions. Each has two answers: pick the right one.
- Two answers to every question
- A fish treat for every right answer
- Streaks win Pixel shades, then a party hat
About 4 minutes
Memory Match
Flip the cards two at a time to match each code card to its meaning card.
- 12 cards: each code has a meaning
- One peek at every card
- Combo points for pairs found in a row
6 pairs, about 4 minutes
The games need JavaScript. Every question is below, with the answers at the end.
Read the lesson: Input and data types
Input and data types: the questions
Quiz Challenge, round 1: Quiz Blitz
What type of data does input() always give you?
- A whole number
- A decimal number
- True or False
- Text, a string
The user types 7. What happens?
age = input("Age? ") print(age + 1)- It prints 71
- It prints 7
- A TypeError: you cannot add 1 to text
- It prints 8
Which line gets a whole number from the user?
age = int(input("Age? "))age = input("Age? ")age = str(input("Age? "))age = print("Age? ")
Which data type would store 3.14?
- int
- str
- bool
- float
What data type is True?
- str
- int
- float
- bool
What is printed?
print("3" + "4")- 7
- 3 4
- An error
- 34
What is printed?
score = 15 print("You scored " + str(score))- You scored score
- An error
- You scored str(score)
- You scored 15
"7" and 7 are the same to Python.
- True
- False
score = 15. Which TWO lines print You scored 15 without an error?
Tick 2.
print("You scored", score)print("You scored " + str(score))print("You scored " + score)print(You scored, score)print("You scored " + int(score))
What is int("7") + 1?
Put these lines in order to ask for a name and greet the user.
Number these 1 to 3.
greeting = "Hello " + nameprint(greeting)name = input("Name? ")
Which error do you get from int("hello")?
- SyntaxError
- IndentationError
- ValueError
- NameError
Quiz Challenge, round 2: Quick Fire
Explain why age + 1 causes an error after age = input("Age? "). [2 marks]
How can you make input give you a whole number? [2 marks]
Give three data types, with an example of each. [3 marks]
What is the difference between "7" and 7? [2 marks]
Quiz Challenge, round 3: Big Think
score holds the number 15. Explain two ways to print You scored 15, and why "You scored " + score fails. [4 marks]
Explain why input always gives text, what problems this causes, and how to avoid them. Use examples. [6 marks]
This or That: tick the right answer
The user types 12 into input(). What does Python store?
- The number 12
- The text "12"
Which converts "7" into the number 7?
str("7")int("7")
Which data type stores 1.5?
- float
- int
What is printed?
print(2 + 2)- 4
- 22
What data type is the value False?
- bool
- str
Which joins text and a number without an error?
"Age: " + str(12)"Age: " + 12
The user types ten instead of 10 into int(input()). What happens?
- A ValueError
- It stores 10
score holds a number. Which line works?
print("Score: " + score)print("Score:", score)
Memory Match: match each code to its meaning
| Code | Meaning |
|---|---|
A int("7") | 1 Turns text into a decimal number |
B float("3.5") | 2 True or False |
C str(15) | 3 Joins two strings to make 34 |
D input() | 4 Turns the text 7 into a whole number |
E "3" + "4" | 5 Turns a number into text |
F bool | 6 Always gives back text |
Answers
Round 1: Quiz Blitz
- Text, a string input always gives text, even if the user types a number.
- A TypeError: you cannot add 1 to text input gives the text "7", and Python cannot add the number 1 to text.
age = int(input("Age? "))int() converts the text from input into a whole number.- float A float holds numbers with decimals.
- bool True and False are Boolean values, type bool.
- 34 Both are text, so + joins them together.
- You scored 15 str turns 15 into the text "15", which can be joined to the other text.
- False "7" is text, a string. 7 is a number, an int.
print("You scored", score);print("You scored " + str(score))Use a comma, or convert the number with str() before joining it with +.- 8 int turns the text "7" into the number 7, and 7 + 1 = 8.
name = input("Name? ")→greeting = "Hello " + name→print(greeting)Get the input, build the message, then print it.- ValueError hello cannot be turned into a whole number, so Python raises a ValueError.
Round 2: Quick Fire
- input always gives text, a string
- You cannot add a number to text
Model answerinput always gives text, a string, even if the user types a number. You cannot add the number 1 to text, so Python gives a TypeError.
- Convert it with int()
- Put int() around the input
Model answerConvert it with int(), wrapped around the input: age = int(input("Age? ")). This turns the text into a whole number.
- int, such as 7
- float, such as 3.14
- str, such as "hello"
- bool, True or False
Model answerint, such as 7. float, such as 3.14. str, such as "hello".
- "7" is text, a string
- 7 is a number that you can do maths with
Model answer"7" is text, a string, so it cannot be added up. 7 is a number, an int, so you can do maths with it.
Round 3: Big Think
- Use a comma inside print
- Convert the number with str() and then join with +
- + joins pieces of text together
- + cannot join text and a number, so it gives a TypeError
- With a comma, Python handles the types for you
Model answerThe first way is a comma: print("You scored", score), which Python handles for you, adding a space. The second way is to convert the number to text with str() and join them with +. "You scored " + score fails because + cannot join text and a number, so Python gives a TypeError.
- input gives a string even when the user types a number
- Adding a number to it gives a TypeError
- An example, such as age + 1
- Convert whole numbers with int()
- Convert decimals with float()
- Converting text such as hello gives a ValueError
- Use str() to join a number to text
Model answerinput always gives a string, even if the user types a number. If the user types 7 and the program does age + 1, Python gives a TypeError, because you cannot add a number to a string. To avoid this, convert the input with int(), such as age = int(input("Age? ")), or with float() for decimals. If the user types something like hello, converting it gives a ValueError. To join a number to text for printing, convert it back with str().
This or That
- The text "12" input always gives text, whatever the user types.
int("7")int() makes a whole number. str() makes text.- float An int holds whole numbers only. A float holds numbers with decimals.
- 4 Without quotation marks they are numbers, so + adds them.
- bool True and False are Booleans. "False" inside quotation marks would be a string.
"Age: " + str(12)Convert the number with str() before joining it with +.- A ValueError Python cannot turn the word ten into a number.
print("Score:", score)A comma works with any type. + needs both sides to be text.
Memory Match
- A 4
int("7"): Turns the text 7 into a whole number - B 1
float("3.5"): Turns text into a decimal number - C 5
str(15): Turns a number into text - D 6
input(): Always gives back text - E 3
"3" + "4": Joins two strings to make 34 - F 2
bool: True or False