Pearson Edexcel 1CP2 GCSE Computer Science · Topic 6: Problem Solving with Programming

Stretch tasks Lists, Strings and Files in Python

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 term card to its meaning card.

  • 12 cards: each term has a meaning
  • One peek at every card
  • Combo points for pairs found in a row

6 pairs, about 4 minutes

Task 4

Sort It

Sort each statement into the right box, like the tick-box tables in the exam.

  • 12 cards to sort into 3 boxes
  • Drag a card, tap a box or press its number
  • Combo points for cards sorted in a row

12 cards, about 4 minutes

The games need JavaScript. Every question is below, with the answers at the end.

Read the lesson: Lists, Strings and Files in Python

Lists, Strings and Files in Python: the questions

Quiz Challenge, round 1: Quiz Blitz

  1. What does this print?

    scores = [14, 9, 21, 6, 18]
    print(scores[-1])

  2. A list has 5 items. What is its highest index?

  3. What does this print?

    word = "Computing"
    print(word[0:3])

  4. What does strip() remove?

    • All the spaces inside the string
    • Whitespace, including the newline, from both ends
    • Every vowel
    • The first character
  5. Which file mode adds to the end without losing the existing contents?

    • "a"
    • "w"
    • "r"
    • "x"
  6. Opening a file with "w" keeps its existing contents.

    • True
    • False
  7. What does this print?

    line = "Amira,15,Year 10"
    parts = line.split(",")
    print(parts[1])

  8. Everything read from a text file has which type?

    • A list
    • A Boolean
    • A string
    • An integer
  9. What does this print?

    board = [[1, 0, 0],
             [0, 1, 0],
             [0, 0, 1]]
    print(board[1][2])

  10. What does ord("A") give?

  11. Which TWO add an item to a list?

    Tick 2.

    • pop()
    • remove()
    • sort()
    • append()
    • insert()
  12. Why use with open(...) as file?

    • It closes the file for you, even if something goes wrong
    • It opens files faster
    • It encrypts the file
    • It reads only one line

Quiz Challenge, round 2: Quick Fire

  1. Explain why this loop raises an IndexError, and how to fix it. [2 marks]

    scores = [14, 9, 21]
    for i in range(len(scores) + 1):
        print(scores[i])

  2. Explain why a value read from a file and compared with "Amira" never matches, even when the name is right. [2 marks]

  3. Explain the difference between opening a file in "w" mode and in "a" mode. [2 marks]

  4. Explain why int() is needed on a score read from a file before adding it to a total. [2 marks]

Quiz Challenge, round 3: Big Think

  1. A file scores.txt holds lines such as Amira,18. Write a Python program that reads the file and prints the name of the student with the highest score. [6 marks]

  2. Explain how lists, string methods and files would be used in a program that stores a class's quiz results so they are still there next lesson. [6 marks]

This or That: tick the right answer

  1. A five item list: what is len(scores)?

    • 4
    • 5
  2. word = "Computing". What is word[0]?

    • o
    • C
  3. Does word[0:3] include index 3?

    • No: it stops before 3
    • Yes: it includes 3
  4. Adding results to a file without losing the old ones: "a" or "w"?

    • "w"
    • "a"
  5. Which takes a comma separated line apart?

    • strip
    • split
  6. In board[1][2], is the 1 the row or the column?

    • The row
    • The column
  7. What does chr(66) give?

    • A
    • B
  8. You only need each value, not its position. Which loop?

    • for i in range(len(scores))
    • for score in scores

Memory Match: match each term to its meaning

TermMeaning
A Index1 Removes whitespace from both ends
B Slice2 Divides a string at a separator
C split3 Counts the items in a list
D strip4 A section that stops before the second index
E len5 Opens a file to add to its end
F Append mode6 Position, counted from 0

Sort It: tick one box in each row

Is each operation used on a list, a string or a file?

StatementListStringFile
word.find("put")
file.readlines()
file.write("Ben,14\n")
line.split(",")
scores.pop()
open("scores.txt", "a")
scores.sort()
for line in file:
word.strip()
word.upper()
board[1][2]
scores.append(25)
Answers

Round 1: Quiz Blitz

  1. 18 Index -1 is the last item.
  2. 4 Indexes start at 0, so the last is len - 1.
  3. Com A slice stops before the second index: characters 0, 1 and 2.
  4. Whitespace, including the newline, from both ends Forgetting it is why values read from a file often fail to match.
  5. "a" "a" appends. "w" empties the file first.
  6. False Write mode empties the file before anything is written.
  7. 15 split makes the list ['Amira', '15', 'Year 10'].
  8. A string Every time, so numbers must be cast.
  9. 0 Row 1 is [0, 1, 0], and position 2 in it is 0.
  10. 65 ord gives the character code. chr converts back.
  11. append(); insert() append adds to the end. insert adds at a position.
  12. It closes the file for you, even if something goes wrong It is the safer form.

Round 2: Quick Fire

    • The highest index is 2, but the loop goes one too far
    • Remove the + 1, or loop over the items
    Model answer

    The list has 3 items, so its highest index is 2, but range(len(scores) + 1) goes up to 3, one too far, which raises an IndexError. Remove the + 1, so the loop is for i in range(len(scores)):, or loop over the items with for score in scores:.

    • Each line still ends with a newline character
    • strip() removes it
    Model answer

    Each line read from a file still ends with a newline character, so the value is "Amira" followed by a newline, not "Amira". Calling strip() removes the newline so the comparison works.

    • "w" empties the file first, destroying what was there
    • "a" adds to the end, keeping the existing contents
    Model answer

    Write mode, "w", empties the file before anything is written, so the existing contents are destroyed. Append mode, "a", adds the new data to the end and keeps what is already there.

    • Everything read from a text file is a string
    • Adding a string to a number fails
    Model answer

    Everything read from a text file is a string, so the score is the text "18", not the number 18. Adding a string to a number total raises a TypeError, so int() must convert it first.

Round 3: Big Think

    • Open the file
    • Loop through each line
    • strip each line
    • split it at the comma
    • Cast the score to an integer
    • Compare it with the best so far
    • Remember the name that goes with it
    • Print the name after the loop
    Model answer
    Python
    best_score = -1
    best_name = ""
    with open("scores.txt", "r") as file:
        for line in file:
            parts = line.strip().split(",")
            score = int(parts[1])
            if score > best_score:
                best_score = score
                best_name = parts[0]
    print(best_name)
    • A list holds many results under one name
    • append adds each new result
    • A loop works through the list, such as to total the scores
    • A file makes the results persist between runs
    • Append mode keeps earlier results, while write mode wipes them
    • Each result is written as one line
    • Reading back uses strip and split
    • Scores are cast with int before arithmetic
    Model answer

    During the lesson, the results are held in a list, which stores many values under one name, and append adds each new result. A for loop works through the list to total the scores and find the average. A list disappears when the program closes, so the results are saved to a file, which makes them persist between runs. Each result is written as one line, such as Amira,18, using append mode so earlier results are kept; write mode would wipe them. Next lesson, the program reads the file back, using strip to remove each newline and split to separate the name from the score, then int to convert the score before doing arithmetic.

This or That

  1. 5 len counts the items. The highest index is 4.
  2. C Indexing starts at 0.
  3. No: it stops before 3 Like range, a slice stops before the second number.
  4. "a" "w" empties the file first. "a" appends to the end.
  5. split strip removes whitespace. split divides a string at a separator.
  6. The row The first index selects the row, the second the position within it.
  7. B chr turns a code back into its character, and 65 is A.
  8. for score in scores Loop over the items when you only need the values.

Memory Match

  • A 6 Index: Position, counted from 0
  • B 4 Slice: A section that stops before the second index
  • C 2 split: Divides a string at a separator
  • D 1 strip: Removes whitespace from both ends
  • E 3 len: Counts the items in a list
  • F 5 Append mode: Opens a file to add to its end

Sort It

  1. String word.find("put") find searches a string.
  2. File file.readlines() Reading every line of a file.
  3. File file.write("Ben,14\n") Writing a line to a file.
  4. String line.split(",") split divides a string into a list.
  5. List scores.pop() pop removes an item from a list.
  6. File open("scores.txt", "a") Opening a file in append mode.
  7. List scores.sort() sort puts a list in order.
  8. File for line in file: Looping through a file line by line.
  9. String word.strip() strip removes whitespace from a string.
  10. String word.upper() upper changes the case of a string.
  11. List board[1][2] Row then position in a 2D list.
  12. List scores.append(25) append adds an item to the end of a list.