[Tutor] Indexing a list with nested tuples

bob gailer bgailer at gmail.com
Wed Aug 3 14:52:56 CEST 2011


On 8/2/2011 11:39 PM, Alexander Quest wrote:
> Hey Bob- thanks for the reply. Here is a more complete part of that 
> code section (the ellipses are parts where I've deleted code because I 
> don't think it's important for this question):

Please always reply-all so a copy goes to the list.

Thanks for posting more code & traceback

I forgot to mention earlier - tell us which version of Python you are 
using (this looks like version 3)

You did not answer all my questions! How come? Please do so now.
>
>
> _____________________________________________________________
> attributes = [("strength", 0), ("health  ", 0), ("wisdom  ", 0), 
> ("dexterity", 0)]
> .....
> .....
> .....
> print(
>             """
>             1 - Strength
>             2 - Health
>             3 - Wisdom
>             4 - Dexterity
>
>             Any other key - Quit
>             """
>             )
>             selection = input("Selection: ")
> if selection == "1" or selection == "2" or selection == "3" or 
> selection == "4":
>                 print("You have ", points, "points available.")
>                 how_many = input("How many would you like to add to 
> this attribute?: ")
>                 while how_many < 0 or how_many > 30 or how_many == 
> "":                               # Because max points available is 
> 30, and entering less than 0 does not make sense.
>                     print("Invalid entry. You have ", points, "points 
> available.")                               # If the user enters a 
> number less than 0, greater than 30, or just presses enter, it loops.
>                     how_many = input("How many would you like to add 
> to this attribute?: ")
>                 print("Added ", points, "to ", 
> attributes[selection-1][0], "attribute.")                         # 
> Here is where I try to add the number of points to the value, based on 
> what the user entered.
>                 points = points - 
> how_many                                                                               
> # I subtract the number of points added from the total points available.
>                 attributes[selection-1][1] += 
> how_many                                                              
> # I add the number of points the user selected to the variable selected.
>
>
> __________________________________________________________________________
>
>
> Here's the traceback I get:
>
> Traceback (most recent call last):
>   File "C:\Users\Alexander\Desktop\Python Practice\Ch05-2.py", line 
> 54, in <module>
>     print("Added ", points, "to ", attributes[selection-1][0], 
> "attribute.")
> TypeError: unsupported operand type(s) for -: 'str' and 'int'
> _________________________________________________________________________
>
> Thanks for any help. I understand that I can't change tuples directly, 
> but is there a way to change them indirectly (like saying 
> attribute.remove[x] and then saying attribute.append[x] with the new 
> variable? But this seems to take out both the string and the value, 
> when I only want to increase or decrease the value for one of the 4 
> strings, strength, health, wisdom, or dexterity).

DON'T USE TUPLES. WHY DO YOU INSIST ON THEM?
>
>
>     What does the error message( unsupported operand type(s) for -:
>     'str' and 'int')  tell you? 
>

>     Why would selection be a string rather than an integer? 
>

>     This has to do with how you obtain selection from the user.
>
>     Why did you expect to be able to alter the value of a tuple
>     element? Tuples are immutable! Use a list instead.
>

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

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20110803/556fda3c/attachment.html>


More information about the Tutor mailing list