[Tutor] Some ways lists and dictionaries can be usedunconventionally?

Alan Gauld alan.gauld at btinternet.com
Mon May 23 01:44:55 CEST 2011


"Tidal Espeon" <tidal.espeon at gmail.com> wrote 

> I've been trying to make a text-based character creator program, 
> ...chapter about learning lists and dictionaries, ...
> I've been trying to think of how lists and dictionaries 
> can be useful ... but I fail to see any significant connection.

OK, Assuming your characters attributes have names, 
would it be useful to have all the attributes of your character 
accessible by looking up their name? As the value 
part of a dictionary say?

And since you have a bunch of characters to manage 
wouldn't it be useful to have them stored in a collection, 
maybe a list say? Or even a dictionary where the key 
is the characters name? So we wind up with a dictionary 
full of characters, each of which is a dictionary of attributes.

Then we can do things like:

characters["Tidal"]["strength"] = 45
print characters["Alan"]["Weapons"]

Which is probably easier to read than using fixed 
variables like player1_strength and player1_name
which is pretty much what you would need to do 
without a dictionary or list.

Does that help?

-- 
Alan Gauld
Author of the Learn to Program web site
http://www.alan-g.me.uk/




More information about the Tutor mailing list