Year 8 · Y8.3 Python: Loops, Lists and Functions · Lesson 2

Stretch tasks Lists

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 output card.

  • 12 cards: each code has a output
  • 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: Lists

Lists: the questions

Quiz Challenge, round 1: Quiz Blitz

  1. What is printed?

    names = ["Ali", "Bea", "Cal", "Dev"]
    print(names[2])
    • Bea
    • Dev
    • An error
    • Cal
  2. What happens?

    names = ["Ali", "Bea", "Cal", "Dev"]
    print(names[4])
    • It prints Ali
    • It prints nothing
    • An IndexError, because the last index is 3
    • It prints Dev
  3. What is printed?

    scores = [4, 9, 2, 7]
    print(len(scores) + max(scores))

  4. scores.append(5) adds 5 to the start of the list.

    • True
    • False
  5. What is printed?

    nums = [3, 8, 1]
    nums.append(6)
    nums[0] = 10
    print(nums)
    • [3, 8, 1, 6]
    • [10, 3, 8, 1, 6]
    • [6, 8, 1, 10]
    • [10, 8, 1, 6]
  6. What is printed?

    temps = [12, 15, 9, 20]
    print(sum(temps) / len(temps))

  7. Put these lines in order to print the average of a list.

    Number these 1 to 4.

    • total = sum(scores)
    • average = total / len(scores)
    • print(average)
    • scores = [6, 9, 3]
  8. Which TWO lines remove Bea from names = ["Ali", "Bea", "Cal"]?

    Tick 2.

    • del names[1]
    • del names[2]
    • names.remove(1)
    • names.append("Bea")
    • names.remove("Bea")
  9. What is printed on the second line?

    words = ["cat", "horse", "ox"]
    for w in words:
        print(len(w))
    • 3
    • 2
    • horse
    • 5
  10. A list holds 20 items. What is the index of the last one?

  11. sorted(scores) gives a sorted copy and leaves scores itself unchanged.

    • True
    • False
  12. What is printed?

    marks = [55, 72, 38, 90]
    count = 0
    for m in marks:
        if m >= 50:
            count = count + 1
    print(count)
    • 2
    • 4
    • 1
    • 3

Quiz Challenge, round 2: Quick Fire

  1. Explain why a list is better than 30 separate variables for storing a class's scores. [2 marks]

  2. What does scores[3] cause here, and why? [2 marks]

    scores = [8, 3, 5]

  3. Give the line of code that adds Eve to the end of a list called names. [1 mark]

  4. What is printed? Explain your answer. [2 marks]

    data = [5, 2, 9]
    data.append(4)
    print(data[1] + data[3])

Quiz Challenge, round 3: Big Think

  1. Explain what this program does, step by step, and state exactly what it prints. [4 marks]

    temps = [14, 18, 11, 21, 16]
    hot = []
    for t in temps:
        if t > 15:
            hot.append(t)
    print(hot, len(hot))

  2. A teacher keeps her class's test scores in a list. Explain how a program could use the list to give her useful information, with examples of code. [6 marks]

This or That: tick the right answer

  1. What is printed?

    pets = ["cat", "dog", "fish"]
    print(pets[1])
    • dog
    • cat
  2. A list holds 5 items. Which index does not exist?

    • 5
    • 0
  3. What is printed?

    nums = [4, 1, 3]
    nums.append(2)
    print(len(nums))
    • 4
    • 3
  4. Which line changes the second item of scores to 50?

    • scores[1] = 50
    • scores[2] = 50
  5. What is printed?

    scores = [12, 45, 3]
    print(max(scores) - min(scores))
    • 42
    • 33
  6. What is the difference between remove and del?

    • remove takes an index, del takes a value
    • remove takes a value, del takes an index
  7. Which works out the average of a list called scores?

    • max(scores) / len(scores)
    • sum(scores) / len(scores)
  8. Why use one list instead of 30 separate variables?

    • One name holds them all, so a loop can process them
    • Separate variables can only hold text

Memory Match: match each code to its output

CodeOutput
A len([4, 7, 1])1 1
B max([4, 7, 1])2 [1, 4, 7]
C sum([4, 7, 1])3 7
D sorted([4, 7, 1])4 3
E [4, 7, 1][0]5 12
F min([4, 7, 1])6 4
Answers

Round 1: Quiz Blitz

  1. Cal Indexes start at 0: Ali is 0, Bea is 1, Cal is 2.
  2. An IndexError, because the last index is 3 Four items have indexes 0 to 3. There is no index 4, so Python stops with an IndexError.
  3. 13 len is 4 and max is 9, so 4 + 9 = 13.
  4. False append always adds to the end of the list.
  5. [10, 8, 1, 6] append puts 6 on the end, then nums[0] = 10 replaces the first item, 3.
  6. 14.0 56 divided by 4 is 14. Dividing with / always gives a decimal in Python, so it prints 14.0.
  7. scores = [6, 9, 3] → total = sum(scores) → average = total / len(scores) → print(average) Each line needs the one before it: the list, then its total, then the average, then the output.
  8. names.remove("Bea"); del names[1] remove deletes by value; del deletes by index, and Bea is at index 1. remove(1) fails because there is no value 1 in the list.
  9. 5 The loop prints the length of each word: 3, 5, 2. The second line is 5.
  10. 19 Indexes run from 0 to 19, because counting starts at 0.
  11. True sorted returns a new list. scores.sort() is the one that changes the original.
  12. 3 55, 72 and 90 are at least 50. This loop counts the passes.

Round 2: Quick Fire

    • One name holds all of the scores
    • You can loop through them, or use sum, max and len
    • It needs less code, and more scores are easy to add
    Model answer

    A list stores all 30 scores under one name, so you can loop through them or use sum, max and len on them, instead of writing 30 separate lines. It is also easy to add more scores with append.

    • An IndexError: the program crashes
    • Indexes start at 0, so the last index is 2
    Model answer

    It causes an IndexError, because indexes start at 0. The three items are at indexes 0, 1 and 2, so there is no index 3.

    • names.append("Eve")
    Model answer

    names.append("Eve")

    • 6
    • data[1] is 2, and data[3] is the 4 that was appended
    Model answer

    6 is printed. data[1] is 2, and append put 4 on the end at index 3, so 2 + 4 = 6.

Round 3: Big Think

    • It sets up a list of temperatures and an empty list
    • It loops through every temperature
    • Temperatures over 15 are appended to the new list
    • It prints the list [18, 21, 16]
    • and 3, the number of hot days
    Model answer

    The program sets up a list of five temperatures and an empty list called hot. It loops through every temperature, and if one is greater than 15 it appends it to hot. Finally it prints the hot list, [18, 21, 16], followed by its length, 3.

    • The average: sum(scores) / len(scores)
    • The highest and lowest scores, with max and min
    • How many passed, using a loop and an if
    • The scores in order, with sorted
    • New scores added with append
    • Searching for a particular score
    • The number of students, with len
    Model answer

    The program could work out the class average with sum(scores) / len(scores), and find the highest and lowest scores with max(scores) and min(scores). It could count how many students passed by looping through the list and using an if to add 1 to a counter for each score of 50 or more. sorted(scores) would show the scores in order, and len(scores) gives the number of students. New scores can be added with scores.append(), and the program could search the list to find whether anyone got a particular score.

This or That

  1. dog Indexes start at 0, so index 1 is the second item.
  2. 5 The indexes run from 0 to 4. Asking for index 5 causes an error.
  3. 4 append adds one item to the end, so the list now holds 4 items.
  4. scores[1] = 50 The second item is at index 1, because the first is at index 0.
  5. 42 max gives 45 and min gives 3, and 45 take away 3 is 42.
  6. remove takes a value, del takes an index scores.remove(3) removes the value 3. del scores[0] removes whatever is at index 0.
  7. sum(scores) / len(scores) Add them all up with sum, then divide by how many there are with len.
  8. One name holds them all, so a loop can process them A list keeps the values together under one name, and a loop can work through every one.

Memory Match

  • A 4 len([4, 7, 1]): 3
  • B 3 max([4, 7, 1]): 7
  • C 5 sum([4, 7, 1]): 12
  • D 2 sorted([4, 7, 1]): [1, 4, 7]
  • E 6 [4, 7, 1][0]: 4
  • F 1 min([4, 7, 1]): 1