On Apr 15, 2005, at 01:33, D. Hartley wrote:

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

Okay, a few little comments:
- high_scorelist[len(high_scorelist)-1] can be written as high_scorelist[-1]. Python slicing operators are very powerful.
- That's me nitpicking, but you should pick a naming convention for your whole program (camelCase, names_with_underscores or whatever) and stick to it. It makes things more readable. For example, userscore and high_scorelist should be named userScore and highScoreList, or user_score and high_score_list.
- Since lists are mutable objects, the function add_score, by way of the append and sort methods, modifies the high_scorelist list in place. Why do you return it afterwards?


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.

What did your code look like when it did that?

(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!)

Not really. Writing things that way is the natural thing to do (even though it might feel counter-intuitive at the beginning), as it allows you to do what you want without extra code, and the less code you write for a given functionality, the better. Every line of code you write is a liability.
Formatting data for display is left to the programmer (i.e. you). The user never sees what the internal data structure looks like, so when you're designing it, the only thing you should take into account is how easy to use it's going to be for *you*, the programmer.


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).

cmp is a user-supplied comparison function. When it is given 2 arguments, x and y, it must return -1 when x < y, 0 when x == y and 1 when x > y. it allows you to sort the list based on arbitrary criteria without having to re-write a sort function.
If it is None, the default sort function is used.
I'm still using Python 2.3, so someone else will have to tell you what the "key" argument is.


Oh, and when you're wondering what something does, remember that:
- Experimenting is good.
- Your 2 best friends are the interactive Python shell, and the help() function.


-- Max
maxnoel_fr at yahoo dot fr -- ICQ #85274019
"Look at you hacker... A pathetic creature of meat and bone, panting and sweating as you run through my corridors... How can you challenge a perfect, immortal machine?"



_______________________________________________ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor

Reply via email to