[Tutor] high score lists

Max Noel maxnoel_fr at yahoo.fr
Sat Apr 16 01:12:51 CEST 2005


On Apr 15, 2005, at 21:30, D. Hartley wrote:

> Unless you can explain what "lambda x:
> x[1]" does, in preschool-speak ;)

	That's an anonymous function, also known as a lambda function. Let's 
take an example:

 >>> a = range(10)
 >>> a
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
 >>> map(lambda x: 2*x, a)
[0, 2, 4, 6, 8, 10, 12, 14, 16, 18]

	The first and second lines should be obvious ;) . Now, on line 3, I 
use two things:
1) The "map" function. It takes 2 arguments: a function and an iterable 
(a list, here). It then applies the function to each member of the 
list, and returns the result.
2) A lambda function. "lambda x: 2*x" is a function that takes 1 
argument, x, and returns 2*x. You see, I couldn't be bothered with 
def'ing such a trivial function earlier in the program. I could have 
done that instead:

 >>> def twice(x):
 >>>     return 2*x
 >>> a = range(10)
 >>> a
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
 >>> map(twice, a)
[0, 2, 4, 6, 8, 10, 12, 14, 16, 18]

	And the result would have been the same. So a lambda function is just 
a 1-line function you only want to use as an argument to another 
function (functions are objects) and can't be bothered with/don't want 
to def elsewhere in the program. That's all.

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

	The "rjust" string method is what you're looking for, methinks. 
help("".rjust) for more information. :)

> 1. what is/where can i get the "pickle" module for storing/saving
> changes to the high score list?

	pickle is part of the standard Python distribution. import pickle is 
all you need to do.

> 3. displaying the score/scorelist stuff on the graphics window instead
> of the python window

	That's heavily dependent on what you're using. Pygame? Tkinter? 
Something else?

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

	That moment will come sooner than you think. Python is remarkably easy 
to learn, especially with the help of this mailing list. It's amazing 
the insight you gain by trying to help others.
	I've been using Python for about 6 months now, and it definitely is my 
favorite language. I can't wait to do some serious programming in it 
(don't have enough time at the moment, with all the work I have to do 
in Java... But as soon as Shadowrun 4 hits the stores, I'll start 
working on a character generator, perhaps learning PyObjC in the 
process).

-- 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?"




More information about the Tutor mailing list