[Tutor] Help regarding lists, dictionaries and tuples

Hugo Arts hugo.yoshi at gmail.com
Mon Nov 22 00:42:21 CET 2010


On Mon, Nov 22, 2010 at 12:29 AM, Robert Sjöblom
<robert.sjoblom at gmail.com> wrote:
> Hi. I'm new at programming and, as some others on this list, am going
> through Python Programming for the Absolute Beginner. In the current
> chapter (dealing with lists and dictionaries), one of the challenges
> is to:
>>Write a Character Creator program for a role-playing game. The player should be given a pool of 30 points to spend on four attributes: Strength, Health, Wisdom, and Dexterity. The >player should be able to spend points from the pool on any attribute and should also be able to take points from an attribute and put them back into the pool.
>
> I don't want a direct answer on how to proceed, but a question that
> arose during my thinking of the problem was whether dictionaries can
> have integral data in them, like so:
> attributes = {"Strength" : 28, "Health" : 12}
> or if they have to be:
> attributes = {"Strength" : "28", "Health" : "12"}
>
> Ok, I'm clearly thinking in circles here. I used the interpreter to
> figure out that both are fine but the first example has integers,
> whereas the second has strings. Good to know. What I'm wondering then,
> instead, is whether there's a good way to sum up the value of integral
> data in a dictionary?
>

I will point you toward two things. First, the dict class has a method
called values(). Read the documentation on that, go into your
interpreter, make some dictionaries, and call the values() method, see
what comes up.

The second thing is the sum() method. Again, read the documentation on
it and experiment with it a little. Those two should suffice to answer
your question.

I always encourage people to play around with their problems in the
interactive interpreter, discovering what works and what doesn't for
yourself is often a great way to learn. Combine that with
http://docs.python.org and you've got some very powerful learning
tools

Hugo


More information about the Tutor mailing list