[Tutor] role playing game - help needed

Alan Gauld alan.gauld at btinternet.com
Mon Dec 6 20:16:06 CET 2010


"Al Stern" <albstern at gmail.com> wrote

> Thanks for the advice.  I think I have the dictionary function set 
> up right
> now although I'm still not clear why it is better than the list.
>
> attributes = {"strength": 0, "health": 0, "wisdom": 0, "dexterity": 
> 0}

Consider where you want to update the points for "health"

Using two lists you need to loop over the keys list to find the index
of "health" then access the values list to set the points. Something
like this:

for x in range(len(keys)):
    if keys[x] == "health":
        values[x] = newValue

With a dictionary you just need to do

attributes["health"] = newValue

That's a lot less typing and will be faster performance too.
I'd also say its a lot easier to see whats happening - its more 
readable.

HTH,

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





More information about the Tutor mailing list