[Tutor] role playing game - help needed
Robert Sjöblom
robert.sjoblom at gmail.com
Tue Dec 7 13:24:30 CET 2010
>> 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.
[snip]
>
> Ok. I think I am starting to get it but still not sure how to separate the
> value from the key. Once I have this...
>
> attributes = {"strength": 0, "health": 0, "wisdom": 0, "dexterity": 0}
> MAX_POINTS = 30
>
> How do I set the variable for available_points?
>
> available_points = MAX_POINTS - (not sure what goes here)
attributes.values() will give you a list of the values inside the
attributes dictionary (remember, dictionaries work by key: value
pairs). sum() won't work on a dictionary that contains both strings
and integers, . If you only use integers in your values, the list will
only contain integers, and so you can easily use sum() on it.
> I am pretty sure I will change the values by something like this...
>
> attributes["strength"] = input("\nHow many points do you want to assign to
> strength?: ")
>
> Please let me know if this isn't advisable. It seems to work on the
> surface.
That's how you do it.
best regards,
Robert S.
More information about the Tutor
mailing list