Python – high score table

code for high score part 1

high score code part 1

I’m returning to the high score table task, seeing what I can do with it.  Here I feel my lack of practice probably helps me to see the issues more clearly than if I was a fluent programmer (well, that’s how I’m comforting myself, anyway!).

A reminder of the problem: it’s based on the third task in the specimen paper from OCR GCSE computing, where the requirement is for a high score table to store names and scores for up to 10 players, and be able to write these to a file and read them back in.  I’ve adapted the task a little, so it may well exceed the stated requirements, but then I’m also coming at the code with a hammer rather than a scalpel, I suspect!

I’ve decided to store the high scores in a list of lists, so it will take the format [[335, “Fred Bloggs”],[387, “George Smith”],[938, “Sarah Jones”]], where the first entry relates to the score and the second to the name of the scorer in each case.

Functions that I need to build are:

get_high_score(hstable) returns the highest score and player name

display_high_scores(hstable) prints out the list of scores and names

add_high_score(hstable, score, name) will add a high score to the table if it qualifies

delete_high_score(hstable, name) removes this person and their score from the table

delete_name(hstable, score) removes this score and the associated person from the table

get_high_score(hstable, name) returns the highest score of that person

get_name(hstable, score) returns the name of the person who scored that score

high score code part 2

high score code part 2

I also need to find a way to sort the list so that the scorers are in order from highest score to lowest score.  A little research came up with the way to sort the list by one of the attributes of the inner lists, and a way to reverse that list, by using hstable.sort(key=lambda x: x[0]) then hstable.reverse() – there was supposed to be a way to combine those two commands but the system refused to co-operate on it.

Working out that the easiest way to add a record to the table in the right place if it qualified was to add it to the list, sort the list and then remove the last item if it was more than 10 items long, things quickly fell into place.

The only thing that I had real trouble with was retrieving a saved file, which stubbornly refused to work.  I could check the file had saved correctly, but it refused to load back in.  The console was a real lifesaver, as I could run the file and then type in lines in the console to check the code on the fly.

One other quirk was that when I removed a name from the list two of the three occurrences would be removed.  I could then run the same command again to remove the third occurrence – no idea why it won’t remove all three the first time until I have time to go through step by step.

Generally, though, I’m happy I have a program that will do most of what it’s supposed to, and could quite easily be adapted to be a class for use in a games program.  At this point I think I need to double check the use of the terms functions and methods!

I found that if I got stuck I could generally find the solution somewhere on the web, as long as I knew roughly what I was trying to do, but I’m not sure students new to programming would find it quite so easy.  This is partly why I’m trying to find all the pitfalls and work them out at this stage, so I can make sure they have the best chance of knowing everything they need.  Still, the ability to use class documentation and help systems is invaluable for anyone intending to do any serious coding.



About emmyleigh
Writer/editor/proofreader who loves technology

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: