Proof Academy · Python · Journey 12

Project: build a results analyser

Design, test and explain a program that validates records and produces an exam-style report.

Lab and core lesson: 60–75 minutes, plus guided study · Builds on From Python to the processor

01 · Understand and predict

Decompose the brief before coding

Start with the report contract. Decide what a valid record looks like, how one is classified, and how to handle a file with zero accepted rows. Sketch tests before writing code, including each boundary and an invalid record.

Split the work into reading, validation, classification and reporting. Write a helper that can classify one pair of scores independently. Keep a count of valid records and a count of passes. Store clean report rows before writing them, or write each accepted row as it is processed.

def outcome(first, second):
    if first >= 6 and second >= 6:
        return "Pass"
    return "Retry"

print(outcome(6, 6))
print(outcome(10, 5))

Worked trace

  1. Both scores meet the threshold in (6, 6), so the function returns Pass.
  2. The second score is too low in (10, 5), even though their sum is larger. The function returns Retry.
  3. The caller can display the value or write it to a file. The function has no file or display dependency.
Which test distinguishes 'both scores at least 6' from 'total at least 12'?

From code to machine. An input file, working variables and the output report have different lifetimes. The runtime uses memory for the current record and counters. On a local computer the saved report remains in secondary storage. This browser lab uses temporary files for practice.

Guided study

Read it, trace it, test it

Read the brief as a list of deliverables

Start by identifying the supplied input file, the required output file and the exact rules. In this project the input is results.csv, each accepted record produces one report row, and the file ends with a summary. If the instructions say “both scores at least 6”, compare both scores separately. A total of 12 is not sufficient on its own. Write the required output for one sample by hand so you know what to compare with the running program.

Use this decomposition before you write the whole program:

  1. Read each line and split it into fields. Check the field count before you use an index.
  2. Validate the identifier, the numeric format and the permitted numeric range. Work through these in that order. Only accepted rows go further.
  3. Call a helper that returns Pass or Retry. Check its result for (6, 6), (6, 5) and (5, 6) before involving files.
  4. Write the report row and update the counters. Use a single write-mode file for the whole report, then add the summary after processing the last row.
  5. Reopen the output and compare it with the expected file. Check that a repeat run replaces the earlier report.

Plan the trace and test evidence

RowDecisionValid countPass countOutput row
A01,6,6Both reach 611A01,12,Pass
B04,10,5Valid, second below 621B04,15,Retry
C02,11,6Score out of range21None
D03,0,0Valid, below threshold31D03,0,Retry
,6,6Blank identifier31None

The summary for this example is Valid: 3; Pass: 1. A typical case checks that records and totals are written. The cases at 0 and 10 check the range limits. The 6 and 5 pair checks the classification boundary. A missing field or malformed score should be ignored without a conversion or indexing crash. Test a file with no accepted records, because that exercises a path where the counters stay zero and only the summary is written.

Work like you would in an IDE

In the browser lab, give the first line as the number of raw records, followed by that many rows. The first block makes an input file from those values. Preserve that block for browser tests. Add your solution below it and use Run with my input to test a small example before checking the full set. If you get an error, find the last traceback line, then inspect the field list or types on that input. A temporary debug print can help you find the error. Remove it before comparing exact output.

For a local rehearsal, download the Python file and the original practice CSV. Put them in the same project folder. Remove only the browser setup block that asks for the row count and creates results.csv; the IDE run should read the supplied file instead. Run the file, open the actual report.csv, and compare it with your hand calculation. Save your edited Python file. Before using an official practice paper, read its instructions and keep its supplied files and names as directed. The browser is a place to learn the coding workflow. Paper 2 uses a local Python IDE and supplied digital files.

Review your submission under a time limit

Spend the first few minutes reading the full question, identifying which file you should edit and running the starter where appropriate. Fix the smallest part that lets you make progress. After a change, run a normal case and a case likely to expose that exact bug. Near the end, re-run an earlier case and check the requested filenames, output order, spelling and spaces. If you cannot complete every part, leave your working changes in the file. A partially working program with a correctly implemented requirement is worth reviewing against the mark guidance.

Practise a fresh change request

After completing the report, add a separate count of valid candidates whose total is 15 or higher. Keep Pass and Retry based on the original rule. Decide where to update the new counter and write a case that makes the new count and the pass count different. Add the number to the summary without changing the report rows. Test an empty file after running with a non-empty file to check that no old result remains.

If a timed rehearsal feels too large, complete validation and output first, then repeat the full task with a new input file. Keep the two results so you can see which step became quicker and which still needs practice.

Check your guided practice

Initialise the new counter at zero before the loop. Update it inside the valid-record branch after calculating the total. The valid row B04,10,5 contributes to the new count, since its total is 15, but it does not add to the pass count. The row C02,11,6 contributes to neither count even though the mathematical total is high, since 11 is invalid. For an empty file all counters should be zero and the report should contain only the revised summary.

02 · Run and repair

Require both thresholds

Repair the starter so each of the two scores must be at least 6. Use its two-line input, then explain why testing only 6 and 6 would miss the bug.

Enter one value per line. Each input() reads the next line.

Ready to run.

Need a hint?

Use and when both comparisons have to be true.

03 · Build independently

Produce the results report

The starter creates results.csv from supplied test rows. Preserve it for the browser exercises. Independently write the program that reads the file and produces report.csv.

  • A valid row contains exactly three fields: a non-empty candidate code and two integers from 0 to 10. Trim surrounding spaces. Require decimal digits before conversion. Ignore invalid rows.
  • Write accepted rows as code,total,outcome in input order. A candidate passes when both scores are at least 6. Otherwise write Retry. Use a function to return the outcome.
  • Replace report.csv on each run. After writing the result rows, add one summary line in the format Valid: n; Pass: p. Reopen the report and print exactly its contents.
  • Use the test results and the rubric below to review both the output and the structure of your solution.

Enter one value per line. Each input() reads the next line.

Ready to run.

Need a hint?

Write the classification function first. For each row, check the field count, code and digit strings before calling int. Keep valid_count and pass_count outside the loop.

04 · Explain and transfer

Explain your reasoning

Explain why counting valid rows before validation would make the summary unreliable.

Compare your explanation

A line may have missing fields, invalid text or a score outside the range. Update the count only after the record has passed every check. A file containing only invalid rows should give zero valid records and zero passes.

Mark your project out of 10

Award one mark for each of the two checks in a row that your solution meets. Use the code and output as evidence.

  • 2 marks · Read the CSV line by line and split it into fields. Check the field count before using positions.
  • 2 marks · Check the code and numeric format. Reject scores outside 0–10 before classification.
  • 2 marks · Define a function that returns the outcome. Correctly require both scores to meet the threshold.
  • 2 marks · Replace the report and write accepted rows in order with the correct total and outcome.
  • 2 marks · Count only accepted rows and passes. Write the correct summary for a zero-valid-row file.

Independent follow-up. Download your solution and run a timed 30-minute attempt in a local IDE with a fresh copy of the brief. Remove the browser setup block and put your own results.csv next to the file. Include a normal case, the 6/5 boundary, an invalid record and an empty file. Save the resulting report and compare it with your predictions.

Download original practice data for your IDE

Original GCSE practice · Pearson Edexcel 1CP2 6.1.1–6.1.6, 6.2.2, 6.4.2–6.4.3 and 6.6.2. Download your .py file to keep it after leaving the page.

Next: Paper 2 rehearsal →