Back to the topic

The mark scheme starts on a new page, so you can print pages one and two for a student and keep the last page yourself. Everything prints in black on white whichever theme you are using.

Key Stage 3 · Y9.3

Python Programming

Section A: 10 multiple choice questions, 10 marks. Section B: 5 written questions, 17 marks. Total 27 marks.

Name

Class

Date

Mark  / 27

Section A

Circle the letter of the correct answer. One mark each.

  1. 1What is the difference between a function and a procedure?1
    1. AThere is no difference
    2. BA function returns a value, a procedure does not
    3. CA procedure returns a value, a function does not
    4. DFunctions can only be called once
  2. 2In grid = [[1,2,3],[4,5,6]], what does grid[1][0] give?1
    1. A1
    2. B2
    3. C4
    4. D6
  3. 3Which file mode adds to a file without erasing what is already there?1
    1. A"w"
    2. B"x"
    3. C"r"
    4. D"a"
  4. 4What does a dictionary store?1
    1. AA grid of values
    2. BPairs of keys and values
    3. COnly numbers
    4. DOnly text
  5. 5Why are local variables usually preferred to global ones?1
    1. AOther parts of the program cannot change them by accident
    2. BThey can store more data
    3. CThey are faster to access
    4. DThey are saved automatically
  6. 6What does scores.append(50) do?1
    1. ASorts the list
    2. BRemoves 50
    3. CReplaces the list with 50
    4. DAdds 50 to the end of the list
  7. 7What does student["name"] do if student is a dictionary?1
    1. AReturns the value stored under the key name
    2. BDeletes the key name
    3. CAdds a new key called name
    4. DReturns the first item in the dictionary
  8. 8Why should a program be split into subroutines?1
    1. AIt makes the program run at a higher clock speed
    2. BPython requires it
    3. CEach part can be tested separately and code is not repeated
    4. DIt removes the need for validation
  9. 9What does line.strip().split(",") do?1
    1. ARemoves the newline then splits the line into parts at each comma
    2. BConverts the line to a number
    3. CDeletes every comma
    4. DJoins the line to another
  10. 10What happens if a program opens a file that does not exist in read mode?1
    1. ANothing happens
    2. BIt raises an error unless the program handles it
    3. CIt creates an empty file
    4. DIt returns an empty string

Section B

Answer in the space provided. The number of marks tells you how many separate points to make.

  1. 1StateState two reasons why a programmer would use subroutines.2
  2. 2WriteWrite a Python function that takes a list of numbers as a parameter and returns the average.4
  3. 3ExplainExplain the difference between opening a file in write mode and in append mode, and state when each should be used.3
  4. 4StateA program uses board = [["X","O"],["O","X"],["X","X"]]. State what board[2][0] contains and explain how a 2D list is accessed.3
  5. 5WriteWrite a program that asks the user for a name and a score, then adds them to a file called scores.txt without deleting the existing contents.5

Mark scheme

Section A

1B2C3D4B5A6D7A8C9A10B

Section B

1 State two reasons why a programmer would use subroutines. 2

One mark for each of the following, to a maximum of 2.

  • Code can be reused rather than written out several times
  • Each subroutine can be tested separately, making errors easier to find

Model answer. Subroutines let the same code be written once and then called wherever it is needed, so it does not have to be repeated and any change only has to be made in one place. They also make a program much easier to develop and test, because each subroutine does one clearly defined job and can be tested on its own until it is known to work.

2 Write a Python function that takes a list of numbers as a parameter and returns the average. 4

One mark for each of the following, to a maximum of 4.

  • Defines a function with a parameter
  • Calculates the total of the list
  • Divides by the number of items
  • Uses return to send the answer back

Model answer. def average(numbers): total = sum(numbers) return total / len(numbers) The function takes the list as a parameter called numbers. The built in sum function adds every value in the list, and dividing that total by len(numbers), which gives how many items there are, produces the mean. The return statement sends the answer back to the code that called the function, so it can be printed or stored.

3 Explain the difference between opening a file in write mode and in append mode, and state when each should be used. 3

One mark for each of the following, to a maximum of 3.

  • Write mode erases the existing contents of the file
  • Append mode adds to the end without deleting existing data
  • Use write to start a new file and append to add a record to existing data

Model answer. Opening a file in write mode erases everything already in it before anything new is written, so all previous content is lost. Opening in append mode leaves the existing content untouched and adds anything written to the end of the file. Write mode should be used when the file is being created from scratch or when the whole contents are being replaced, for example saving a complete set of records back to disk. Append mode should be used when a single new record is being added to data that must be kept, such as adding one new high score to an existing list.

4 A program uses board = [["X","O"],["O","X"],["X","X"]]. State what board[2][0] contains and explain how a 2D list is accessed. 3

One mark for each of the following, to a maximum of 3.

  • board[2][0] contains X
  • The first index gives the row
  • The second index gives the column or position within that row

Model answer. board[2][0] contains "X". A 2D list is a list whose items are themselves lists, so it behaves like a grid. The first index selects which inner list, which corresponds to the row, and the second index selects a position within that inner list, which corresponds to the column. Here index 2 selects the third row, which is ["X","X"], and index 0 selects the first item in it, which is "X". Because both indexes start at 0, care is needed not to swap them round.

5 Write a program that asks the user for a name and a score, then adds them to a file called scores.txt without deleting the existing contents. 5

One mark for each of the following, to a maximum of 5.

  • Asks for the name
  • Asks for the score
  • Opens the file in append mode
  • Writes the name and score to the file
  • Closes the file, or uses with open

Model answer. name = input("Enter the name: ") score = input("Enter the score: ") with open("scores.txt", "a") as file: file.write(name + "," + score + "\n") print("Score saved.") The file is opened in append mode, which is essential here because write mode would delete every score already stored. The name and score are written on one line separated by a comma so they can be split apart when the file is read back, and the newline character puts each record on its own line. Using with open means the file is closed automatically once the block finishes, even if an error occurs.