[Tutor] high score lists

D. Hartley denise.hartley at gmail.com
Fri Apr 15 21:30:26 CEST 2005


Hello, everyone!

Thank you for your very helpful comments.  A few follow up questions,
and then I'll show you where I've come so far:

1. Max - I think you're right about keeping it (score, name).  It *is*
much simpler to work with it that way, and would be easier to change
the display and just have the code work in an intuitive way for me, at
the moment.

2. To everyone who suggested indexing with [-1]: thank you for the
reminder! I have done this before but was, as I often do, "playing on
hard" when I wrote the whole "len(high_scorelist)-1" business. Simple
is better!

3.  Re: "key" and "cmp" and figuring out what things do..  I
appreciate all of your explanations, although with my extremely
limited programming experience, even some of the basic explanations
are beyond me.  Of course if I keep the list arranged by (score, name)
rather than the other way around, it does not matter at this exact
moment, but I'm still unclear on how to practically use these
elements.  I'll just have to play with them more.  the "arr =
zip(range(10),range(10,0,-1)" example was interesting, although I had
trouble following how it marked out what it was sorting by (i.e.,
"key=lambda x: x[1]"  .... ???).  It's just an issue of not having
enough familiarity with reading other people's code, I think.  I'll
have to get it to successfully work in something of my own, probably,
before it will make sense.  Unless you can explain what "lambda x:
x[1]" does, in preschool-speak ;)  But regardless, I *am* storing all
this information away, and I know it's helping me learn, piece by
piece.  I very much appreciate it all!

4. Now, onto displaying the score list.  I have managed to get it to
work as follows:

high_scorelist = [(1000,"Denise"), (945,"Denise"), (883,"Denise"),
                  (823,"Grant"), (779,"Aaron"), (702,"Pete"),
                  (555,"Tom"), (443,"Tom"), (442,"Robin"), (404,"Pete")]

def add_score():
    name = read_string("What's your name?")
    score = read_number("What was your score?")
    userscore = (score,name)
    if userscore[0] > high_scorelist[-1][0]:
        print "You made the high score list!"
        high_scorelist.append(userscore)
        if len(high_scorelist) > 10:
            high_scorelist.sort(reverse=True)
            del high_scorelist[-1]
            for score, name in high_scorelist:
                print name," - ",score
    else:
        print "Sorry, didn't quite make the high score list!"
        for score, name in high_scorelist:
            print name," - ",score


Of course at this point it's asking the user for their name/score, and
eventually I will make the program supply the score, but I set it up
this way just for the purposes of testing the score program.  In any
case, when I entered in a score that would make the list, it did as
follows:

>>> add_score()
What's your name?Jack
What was your score? 581
You made the high score list!
Denise  -  1000
Denise  -  945
Denise  -  883
Grant  -  823
Aaron  -  779
Pete  -  702
Jack  -  581
Tom  -  555
Tom  -  443
Robin  -  442
>>> 

If I enter in a score that does not make it, it prints the "didnt make
it" message and the scores, in the same format as above.

Now, this is not the world's most elegant code, clearly, and it will
get changed a lot between now and when it goes in the game, simply
because I have to make it work IN the game.  Even allowing it to run
in the python window (I'd rather have it display on the graphics
window, but I dont know how, so I'm focusing on making it work first),
I'll want it to test whether the score makes the list before it asks
the user for their name (otherwise, why would it matter!).  Also, I
hard-coded in the " - " between the name and the score - I'd like to
put a couple of tabs in there and have the scores in a left-aligned
column an inch or two over from the names.  I know one of my old
tutorial packages had a command for inserting tabs into strings, but
now I cant find it.  Does anyone know?  Also how to make the scores
line up in a column (and not just a fixed amount of space, either
through tabs or hard-coding)?

At that point I'll have a program that works and displays a
not-too-bad-looking high score list.  Then I can worry about importing
the score, and provide an update.


So I'm sure that's probably way too much information for most of you!!
But my remaining questions are these:

1. what is/where can i get the "pickle" module for storing/saving
changes to the high score list?
2. tabs/aligning the score in a column when i display it
3. displaying the score/scorelist stuff on the graphics window instead
of the python window
4. key=lambda x: x[1] ?

Thank you so much for all of your suggestions!  I can't wait until I
learn enough python to be able to give some help back :)

~Denise


More information about the Tutor mailing list