Edexcel GCSE Computer Science June 2025 Paper 2, Question 4: the multiplication square file

Pearson Edexcel 1CP2/02 June 2025, Question 4: complete a Python program that writes an n by n multiplication square to a new file with commas between the values and no labels, using the constants provided. The eleven marks explained, with the program to run.

Past paper questionPearson 1CP2/02June 2025 Paper 211 marksComplete the program

Question 4 of the Pearson Edexcel GCSE Computer Science Paper 2 sat on 20 May 2025 (1CP2/02) is worth 11 marks. Nested loops build each line of a multiplication square as a string, and the string is written to a file. The detail that matters is where the commas go.

We do not copy the exam paper or Pearson's code files here. Open the paper beside this page: Edexcel June 2025 Paper 2 question paper (PDF). When you have finished, check the mark scheme too.

The question in short

The user enters a number for the size of the square. The program writes the values to a new file, separated by commas, with no row or column labels. For 5 the file is five lines, from 1,2,3,4,5 to 5,10,15,20,25. The file gives you constants: the output file name, the comma and the line feed.

Building a line

For each row, start with an empty string. For each column, add the product as a string. After it add a comma, unless it is the last column, in which case add a line feed. Then write the string.

A finished program

OUT_FILE = "Square.txt"
COMMA = ","
LF = "\n"

size = int(input("Enter the size of the square: "))

outFile = open(OUT_FILE, "w")
for row in range(1, size + 1):
    outString = ""
    for column in range(1, size + 1):
        num = row * column
        outString = outString + str(num)
        if column < size:
            outString = outString + COMMA
        else:
            outString = outString + LF
    outFile.write(outString)
outFile.close()

Where the eleven marks are

Eight single marks: the file opened for writing; the value calculated as row * column; the value converted to a string and added to outString; a comma as the separator; a line feed on the last column only; the line written to the file; the file closed outside the row loop; and at least one of the constants used.

Then up to 3 for how well it runs.

Where the marks are lost

  • range(size). That gives 0 to 4, and a row of zeros. The square starts at 1.
  • A comma after every value, including the last. Compare your file with the paper's, character by character.
  • outFile.write() inside the column loop. It writes each value the moment it is made, so the line feed logic breaks. The write goes once per row.
  • outFile.close() inside the loop. The file closes after the first row, and the second write crashes.
  • Writing the number. write() needs a string: str(num).

Run it

It writes the file and then reads it back so that you can see it. Try 5, then 8.

Enter 5 and it prints the five lines from the paper. No line ends in a comma.
The program
from bugbot import *
connect()

OUT_FILE = "Square.txt"
COMMA = ","
LF = "\n"

size = int(input("Enter the size of the square: "))

outFile = open(OUT_FILE, "w")
for row in range(1, size + 1):
    outString = ""
    for column in range(1, size + 1):
        num = row * column
        outString = outString + str(num)
        if column < size:
            outString = outString + COMMA
        else:
            outString = outString + LF
    outFile.write(outString)
outFile.close()

inFile = open(OUT_FILE, "r")
print(inFile.read())
inFile.close()
Put this demo on your own site

Paste it into a school website, Moodle, Google Sites or a blog. More options on the embed page.

Questions

How do I write a table to a text file in Python?

Loop over the rows. For each row build one string, adding each value with a separator after it and a line feed after the last, and write the string. Open the file before the loop and close it after.

How do I avoid a comma after the last value on a line?

Test the column number inside the inner loop: add a comma if it is not the last column, and a line feed if it is.

What does range(1, size + 1) give?

The numbers from 1 up to and including size. range stops one before its second argument, so the + 1 is needed to reach size.

More from this paper

Every Edexcel 1CP2 question we have worked

Learn it step by step

  1. F7.1 Reading and writing files Files and databases
  2. F2.7 Loop patterns Decisions and loops
  3. F3.1 String handling Strings, lists and records
Open the lessons

This is our own explanation of a published exam question. It is not written or endorsed by Pearson, and the question paper and mark scheme remain Pearson's copyright. Read them on Pearson's site with the links on this page.