The plan
After this: Say what the grade book does, in six sentences, and name the four stages that build it.
On this page · 5 sections · 2 checks
A teacher keeps a file of marks. Each line holds a student's name and two marks. The teacher wants a report: every student's average, who did best, and the average of the whole class, saved as a file so that it can be printed or sent.
You have met every piece this needs. Reading and writing a file was in "Input, files and the standard library", the average of a list and the loop that keeps the biggest so far in "Lists", catching a bad line in "Errors and debugging", and a thing that holds a name with its marks in "Classes and objects". This tutorial puts the pieces together into one program, the way programs are written: in stages, each one run and checked before the next is started.
What the program does
In six sentences:
- It opens
marks.txtand reads it line by line. - It cuts each line into a name and two marks, and keeps them together.
- If a line cannot be read, it says so and goes on to the next line.
- It works out each student's average, the top student and the class average.
- It writes a report, one line per student and two summary lines, to
report.txt. - It reads the report back and shows it.
The input file:
Ana,72,85Ben,90,60Cy,88,92The report it should produce:
Ana: 78.50Ben: 75.00Cy: 90.00Top student: CyClass average: 81.17Check the numbers by hand once, so that you know what to expect from every stage. Ana's average is (72 + 85) / 2, which is 78.5. Ben's is 75 and Cy's is 90, so Cy is the top student. The class average is (78.5 + 75 + 90) / 3, which is 81.1666…, and the report writes it with two decimals as 81.17.
The stages
A program of sixty lines is not written in one go. It is written in stages, and every stage is a program that runs and prints something you can check.
| Stage | What it adds | What it prints when run |
|---|---|---|
| 1 Load the data | read the file, cut up the lines, skip a bad one | how many students there are, and one student's marks |
| 2 Work it out | functions for the average, the top student and the class average | the three averages and the two summary values |
| 3 Make it a class | a Student that holds a name and marks and knows its own average | the same, from objects |
| 4 Report and save | numbers with two decimals; the report written to a file | the report, read back from the file |
Each stage keeps everything the one before had. If stage 2 prints a wrong number, stage 1 still works, so the fault is in the lines stage 2 added. That is the whole reason for working this way: you never look for a fault in sixty lines, only in the newest ten.
The smallest piece
Before anything can be worked out, the program has to see the file. The program below opens marks.txt and prints its lines, nothing more. for line in f goes through the file one line at a time, a form that Input, files and the standard library mentioned; stage 1 reads the lines into a list instead, so that it can go back over them.It reads the lines and goes through them in one line; stage 1 keeps the lines in a box first.
with open("marks.txt") as f: for line in f: print(line.strip())run the program to see its boxes
nothing printed yetPress Run it, then step through the program one line at a time.
- lines
- 0
- prints
- 0
- calls
- 0
Python runs in the browser when you press Run on the block below. Java does not run here: if you are learning Java, step through the trace above instead, and paste the code into a Java tool on your computer when you want to run it for real. The browser cannot read the files on your computer, so every Run block in this tutorial gives the program a small pretend marks.txt holding the three lines above.
Change line.strip() to line.strip().upper() and run it again.
Check yourself
Stage 1 ran and printed the right number of students. You add the lines of stage 2, run it, and the class average is wrong. Where is the fault most likely?
One line of the file says Dee,abc,70. By the plan, what does the finished program do with it?
Honesty notes
- Say what the grade book does in six sentences: read the file, cut up each line, skip a bad one, work out the averages, write the report, read it back.
- Build a program in stages, each one a program that runs, so that a fault can only be in the lines the newest stage added.
- Check the worked numbers by hand once: averages 78.5, 75 and 90, the top student Cy, and a class average of 81.17 with two decimals.
- Say that the Run block gives the program a small pretend marks.txt, because the browser cannot read your files.