Pearson Edexcel 1CP2 GCSE Computer Science · Topic 6: Problem Solving with Programming
Stretch tasks Lists, Strings and Files in Python
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 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
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
What does this print?
scores = [14, 9, 21, 6, 18] print(scores[-1])A list has 5 items. What is its highest index?
What does this print?
word = "Computing" print(word[0:3])What does strip() remove?
- All the spaces inside the string
- Whitespace, including the newline, from both ends
- Every vowel
- The first character
Which file mode adds to the end without losing the existing contents?
- "a"
- "w"
- "r"
- "x"
Opening a file with "w" keeps its existing contents.
- True
- False
What does this print?
line = "Amira,15,Year 10" parts = line.split(",") print(parts[1])Everything read from a text file has which type?
- A list
- A Boolean
- A string
- An integer
What does this print?
board = [[1, 0, 0], [0, 1, 0], [0, 0, 1]] print(board[1][2])What does ord("A") give?
Which TWO add an item to a list?
Tick 2.
- pop()
- remove()
- sort()
- append()
- insert()
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
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])Explain why a value read from a file and compared with "Amira" never matches, even when the name is right. [2 marks]
Explain the difference between opening a file in "w" mode and in "a" mode. [2 marks]
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
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]
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
A five item list: what is len(scores)?
- 4
- 5
word = "Computing". What is word[0]?
- o
- C
Does word[0:3] include index 3?
- No: it stops before 3
- Yes: it includes 3
Adding results to a file without losing the old ones: "a" or "w"?
- "w"
- "a"
Which takes a comma separated line apart?
- strip
- split
In board[1][2], is the 1 the row or the column?
- The row
- The column
What does chr(66) give?
- A
- B
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
| Term | Meaning |
|---|---|
| A Index | 1 Removes whitespace from both ends |
| B Slice | 2 Divides a string at a separator |
| C split | 3 Counts the items in a list |
| D strip | 4 A section that stops before the second index |
| E len | 5 Opens a file to add to its end |
| F Append mode | 6 Position, counted from 0 |
Sort It: tick one box in each row
Is each operation used on a list, a string or a file?
| Statement | List | String | File |
|---|---|---|---|
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
- 18 Index -1 is the last item.
- 4 Indexes start at 0, so the last is len - 1.
- Com A slice stops before the second index: characters 0, 1 and 2.
- Whitespace, including the newline, from both ends Forgetting it is why values read from a file often fail to match.
- "a" "a" appends. "w" empties the file first.
- False Write mode empties the file before anything is written.
- 15 split makes the list ['Amira', '15', 'Year 10'].
- A string Every time, so numbers must be cast.
- 0 Row 1 is [0, 1, 0], and position 2 in it is 0.
- 65 ord gives the character code. chr converts back.
- append(); insert() append adds to the end. insert adds at a position.
- 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 answerThe 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 answerEach 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 answerWrite 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 answerEverything 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 answerPythonbest_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 answerDuring 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
- 5 len counts the items. The highest index is 4.
- C Indexing starts at 0.
- No: it stops before 3 Like range, a slice stops before the second number.
- "a" "w" empties the file first. "a" appends to the end.
- split strip removes whitespace. split divides a string at a separator.
- The row The first index selects the row, the second the position within it.
- B chr turns a code back into its character, and 65 is A.
- 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
- String
word.find("put")find searches a string. - File
file.readlines()Reading every line of a file. - File
file.write("Ben,14\n")Writing a line to a file. - String
line.split(",")split divides a string into a list. - List
scores.pop()pop removes an item from a list. - File
open("scores.txt", "a")Opening a file in append mode. - List
scores.sort()sort puts a list in order. - File
for line in file:Looping through a file line by line. - String
word.strip()strip removes whitespace from a string. - String
word.upper()upper changes the case of a string. - List
board[1][2]Row then position in a 2D list. - List
scores.append(25)append adds an item to the end of a list.