[Tutor] Indexing a list with nested tuples
Alexander Quest
redacted@example.com
Wed Aug 3 04:44:35 CEST 2011
Hi guys- I'm having a problem with a list that has nested tuples:
attributes = [("strength", 0), ("health ", 0), ("wisdom ", 0),
("dexterity", 0)]
I've defined the list above with 4 items, each starting with a value of 0.
The player
enters how many points he or she wants to add to a given item. The selection
menu
is 1 - strength; 2 - health; 3 - wisdom; 4- dexterity. So the "selection"
variable is actually
1 more than the index location of the intended item. So I have the following
code:
print("Added ", points, "to ", attributes[selection-1][0], "attribute.")
My intent with this is to say that I've added this many points (however
many) to the
corresponding item in the list. So if the player selects "1", then selection
= 1, but I subtract
1 from that (selection -1) to get the index value of that item in the list
(in this case 0). Then I
have [0] to indicate that I want to go to the second value within that first
item, which is the
point value. I get an error saying that list indices must be integers, not
strings. I get a similar
error even if I just put attributes[selection][0] without the minus 1.
Also, it seems that the tuple within the list cannot be modified directly,
so I can't add points to the original value of "0" that all 4 items start
with. Is there a way to keep this nested list with
tuples but be able to modify the point count for each item, or will it be
better to create a dictionary or 2 separate lists (1 for the names
"Strength, Health, Wisdom, Dexterity" and one
for their starting values "0,0,0,0")? Any suggestions/help will be greatly
appreciated!!!
-Alex
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20110802/5430fdd8/attachment-0001.html>
More information about the Tutor
mailing list
l>