Python For GCSE
High Score Table

Imagine a game where player's finish with a score. It might be nice to have a high score table to record the highest scores. It would be even better if the high score table could be saved to a file and reloaded every time the program runs. This task focuses on the high score table part of that program.

Step 1 – Just The Scores

To start off, we can try to work out the key code we need to manipulate a table of scores. For the purposes of this task, let's work with a table with 5 rows. Each row of the table will contain a name and a score. For example,

MHA1001
ABC723
FGH301
PPP99
LOL3

We'll use two lists to do this. When we start the program, we'll start with 5 scores of 0 and 5 'empty' names.

Python code

All of the subroutines that we write will go above these two lines. To see our table in action, we can write a subroutine to print out the contents of these two lists,

Python code

And we can test this in the shell like so,

Python code

With our table empty, this is what we should see.

Step 2 – Setting Up For Input

It will help us to make a small function to ask the user to enter a name and a score. We can get the function to return those values. This function will make our code a little easier to read.

Python code

The last line of this function returns something that we call a tuple.

Step 3 – Inserting The Score

This is the key algorithm in the solution and here is an outline for you. The comments should direct you as to the purpose of each of the lines.

Python code

Step 4 – Testing The Code We Have So Far

Now we need some code to use our high score lists and our subroutines together as a complete program. This code needs to go after our two lines defining the list.

Python code

The work so far needs to be tested thoroughly. The key subroutines that we have are going to be used in a new program.

Step 5 – Completely New Program

With the program fully tested, we are ready to rebuild the program around a more useful structure. This time we are going to introduce a better menu system and some file handling.

Save the file with a new name and then delete the code you wrote for Step 4.

Step 6 – Main Menu

We will use a subroutine to do this job for us,

Python code

Step 7 – Main Program Loop

We'll leave some gaps for now but will get the main part of the program going so that we can test that our high score table works for us in the new setting.

Python code

Step 8 – Saving Scores

Now we can write a subroutine to save the current table to a file.

Python code

Test that this works.

Step 9 – Loading Scores

Before you can make this work, you need to have saved some scores in the file.

Python code

Step 10 - Test, Review & Reflect

Make sure that you have tested your code well. Write down any questions or observations that you have about the program, the code and the task as a whole.

Step 11 - Use What You Have Learned

You can use the code you have here in any game or quiz program that ends with a score.