[Tutor] high score lists

D. Hartley denise.hartley at gmail.com
Fri Apr 15 01:33:42 CEST 2005


my apologize to Alberto - instead of replying to the whole list, I
accidentally replied only to him! Here are my replies, for anyone else
who might be reading/interested:


This is what I have so far:

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

userscore = (441,"Joe")

def add_score(userscore):
   if userscore[0] > high_scorelist[len(high_scorelist)-1][0]:
       print "You made the high score list!"
       high_scorelist.append(userscore)
       high_scorelist.sort(reverse=True)
       del high_scorelist[len(high_scorelist)-1]
       return high_scorelist
   else:
       print high_scorelist

I had to enter in the variable for "userscore" like that, when I add a
tuple directly into the add_score function, it seems to delete the
last item in the list (i.e., the one I just added) BEFORE it sorts it
(i.e., removing the lowest score).  I have no idea why this is.  But
if I define userscore = (465,"Jane"), for instance, and then run
add_score(userscore), it'll change and print my 10-person high score
list, just like I want it to.  But the problem remains both how I
could display this (if even just in the python window), and how I
could get my program to create the tuple to plug in there.  (I also
just ended up writing the list like [score,user] because I couldnt
figure out how to get it to sort by the second half of the tuple.  I
need a better tutorial book, I think!)

Thanks again for any suggestions/pointers!

~Denise

and  a question about sorting (either way):

 I am trying it out with a list of tuples right now. The first problem
 I ran into is that when i sort it (scorelist.sort(reverse=True)), it
 sorts by the person's name (the first entry), and not by the score.
 plus I'm not quite sure yet how I'd get the user's name or score
 *into* the list - without manually adding it as a tuple? Since I can't
 do something like ... ("John", 100) .... username = "John", userscore
 = 100, if userscore > lowestuserscore etc.

 With a dictionary, could I point something like username to key and
 something like userscore to value?

 But then the problem with dictionaries comes back around to sorting
 too: Can I sort (and then display) a dictionary?

 Also, thanks for the idea about the pickle module - where can I get
 that and documentation for it?

 Thank you guys for your suggestions, and I apologize for the "newbie"
 level of my questions.  I am trying to answer as much as I can from
 the tutorial materials I have here, but they don't always explain
 everything (such as what the "cmp=None, key=None" means in the ( )
 helper of the .sort function.  I figured reverse=True/False out by
 playing with it).

 I really appreciate all this help! I'm trying to make this game for a
 birthday joke for the person who is teaching me programming, so
 obviously I cant ask him for help!

 :)  Denise



> On 4/14/05, Alberto Troiano <albertito_g at hotmail.com> wrote:
> >
> >
> > Hi
> >
> > I've read somewhere that the appropiate way to make a best score list is
> > with a dictionarie
> >
> > So you'll have something like this:
> >
> > best_score={"Peter":100,"Jhon":23}
> >
> > Best Regards
> >
> > Alberto
> >
> >
> >
> >
> >  Gaucho>From: Kent Johnson <kent37 at tds.net> >CC: Python tutor
> > <tutor at python.org> >Subject: Re: [Tutor] high score lists >Date: Thu, 14 Apr
> > 2005 18:24:55 -0400 > >R. Alan Monroe wrote: >>>Anyone have some good
> > beginning ideas/references to creating a >>>high >>>score list and storing
> > scores in a simple python game? (if there's >>>something in the pygames
> > module, or a simpler python way). I'm >>>mod'ing a space invaders-type game
> > and would like to add a high >>>score >>>list :) >> >> >>Quick and dirty
> > approach: make a list of tuples >>[ (40000, 'John Doe'), >> (30000, 'Steve
> > Austin') ] >> >>You can append new ones to the end of the list, sort it,
> > reverse >>it, >>etc. > >And you can use the pickle module to save and
> > restore the list. > >Kent >
> > >_______________________________________________ >Tutor
> > maillist - Tutor at python.org
> > >http://mail.python.org/mailman/listinfo/tutor
> > _______________________________________________
> > Tutor maillist  -  Tutor at python.org
> > http://mail.python.org/mailman/listinfo/tutor
> >
> >
> >
>


More information about the Tutor mailing list