Year 7 · Y7.3 Introduction to Python · Lesson 2

Stretch tasks Input and data types

Task 1

Quiz Challenge

A quiz game against four computer players, then short and long written answers.

  1. Quiz Blitz12 questions against the clock
  2. Quick Fire4 short answers
  3. Big Think2 long answers

About 20 minutes

Task 2

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

Task 3

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

  1. What type of data does input() always give you?

    • A whole number
    • A decimal number
    • True or False
    • Text, a string
  2. 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
  3. Which line gets a whole number from the user?

    • age = int(input("Age? "))
    • age = input("Age? ")
    • age = str(input("Age? "))
    • age = print("Age? ")
  4. Which data type would store 3.14?

    • int
    • str
    • bool
    • float
  5. What data type is True?

    • str
    • int
    • float
    • bool
  6. What is printed?

    print("3" + "4")
    • 7
    • 3 4
    • An error
    • 34
  7. What is printed?

    score = 15
    print("You scored " + str(score))
    • You scored score
    • An error
    • You scored str(score)
    • You scored 15
  8. "7" and 7 are the same to Python.

    • True
    • False
  9. 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))
  10. What is int("7") + 1?

  11. Put these lines in order to ask for a name and greet the user.

    Number these 1 to 3.

    • greeting = "Hello " + name
    • print(greeting)
    • name = input("Name? ")
  12. Which error do you get from int("hello")?

    • SyntaxError
    • IndentationError
    • ValueError
    • NameError

Quiz Challenge, round 2: Quick Fire

  1. Explain why age + 1 causes an error after age = input("Age? "). [2 marks]

  2. How can you make input give you a whole number? [2 marks]

  3. Give three data types, with an example of each. [3 marks]

  4. What is the difference between "7" and 7? [2 marks]

Quiz Challenge, round 3: Big Think

  1. score holds the number 15. Explain two ways to print You scored 15, and why "You scored " + score fails. [4 marks]

  2. 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

  1. The user types 12 into input(). What does Python store?

    • The number 12
    • The text "12"
  2. Which converts "7" into the number 7?

    • str("7")
    • int("7")
  3. Which data type stores 1.5?

    • float
    • int
  4. What is printed?

    print(2 + 2)
    • 4
    • 22
  5. What data type is the value False?

    • bool
    • str
  6. Which joins text and a number without an error?

    • "Age: " + str(12)
    • "Age: " + 12
  7. The user types ten instead of 10 into int(input()). What happens?

    • A ValueError
    • It stores 10
  8. score holds a number. Which line works?

    • print("Score: " + score)
    • print("Score:", score)

Memory Match: match each code to its meaning

CodeMeaning
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 bool6 Always gives back text
Answers

Round 1: Quiz Blitz

  1. Text, a string input always gives text, even if the user types a number.
  2. A TypeError: you cannot add 1 to text input gives the text "7", and Python cannot add the number 1 to text.
  3. age = int(input("Age? ")) int() converts the text from input into a whole number.
  4. float A float holds numbers with decimals.
  5. bool True and False are Boolean values, type bool.
  6. 34 Both are text, so + joins them together.
  7. You scored 15 str turns 15 into the text "15", which can be joined to the other text.
  8. False "7" is text, a string. 7 is a number, an int.
  9. print("You scored", score); print("You scored " + str(score)) Use a comma, or convert the number with str() before joining it with +.
  10. 8 int turns the text "7" into the number 7, and 7 + 1 = 8.
  11. name = input("Name? ") → greeting = "Hello " + name → print(greeting) Get the input, build the message, then print it.
  12. 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 answer

    input 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 answer

    Convert 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 answer

    int, 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 answer

    The 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 answer

    input 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

  1. The text "12" input always gives text, whatever the user types.
  2. int("7") int() makes a whole number. str() makes text.
  3. float An int holds whole numbers only. A float holds numbers with decimals.
  4. 4 Without quotation marks they are numbers, so + adds them.
  5. bool True and False are Booleans. "False" inside quotation marks would be a string.
  6. "Age: " + str(12) Convert the number with str() before joining it with +.
  7. A ValueError Python cannot turn the word ten into a number.
  8. 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