On 8/2/2011 10:44 PM, Alexander Quest wrote:
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!!!
Thanks for inquiring. Some guidelines about questions:

1 - show us more code. in this case specifically how you obtain user input.
2 - show the complete traceback

What does the error message tell you? Why would selection be a string rather than an integer? This has to do with how you obtain selection from the user. What

Why did you expect to be able to alter the value of a tuple element? Tuples are immutable! Use a list instead.

HTH

--
Bob Gailer
919-636-4239
Chapel Hill NC

_______________________________________________
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor

Reply via email to