list calling confusion

This is the place for queries that don't fit in any of the other categories.

list calling confusion

Postby Wommbatt » Tue Oct 08, 2013 7:52 am

Been making great progress on my Learn-Python-by-Making-a-Game project, but have once again become stumped
Here's my wonderful code:

Code: Select all
# This is the Skill Library
# tuple layout (Name, Level, +Attack, +Defense, +Heal, Cost)

SKILLZ = (('Fireball',      1, 4, 0, 0, 10),
          ('Heal',          1, 0, 0, 3, 10),
          ('Ice Lance',     1, 3, 0, 0, 10),
          ('Rock Wall',     1, 0, 4, 0, 10)
)

shopping = 'yes'

while shopping == 'yes':
    num = 1
    for i in SKILLZ:
        print(num, ")", [i[0]], [i[5]], "exp\n")
        num += 1
    print("What would you like to buy?")
    purchase = int(input("> ")) - 1 #-1 to compensate for numbering

    print(SKILLZ[purchase]) #shows the full line from SKILLZ list
    print(SKILLZ[purchase[0]]) #Should show the skill name but doesn't
    buying(SKILLZ[purchase[5]], exp) #This doesn't work at all cause of the above problem
    shopping = 1


Now the problem I'm getting is that when I run the program, specifically this part:
Code: Select all
    print(SKILLZ[purchase[0]])

I get this particular error: 'int' object is not subscriptable

What I don't understand is why it's happening. Earlier in the code I use:
Code: Select all
for i in SKILLZ:
        print(num, ")", [i[0]], [i[5]], "exp\n")


Aren't those two lines essentially the same?

As always, thanks in advance,
-wommbatt
Wommbatt
 
Posts: 24
Joined: Thu Jun 13, 2013 2:15 pm

Re: list calling confusion

Postby Mekire » Tue Oct 08, 2013 8:36 am

The purchase variable is an integer.

I believe this is what you wanted instead:
Code: Select all
print(SKILLZ[purchase][0])

This gives you the entire row of stats:
Code: Select all
SKILLZ[purchase]
and you want the first (index 0) element of that row.

-Mek
User avatar
Mekire
 
Posts: 1125
Joined: Thu Feb 07, 2013 11:33 pm
Location: Asakusa, Japan

Re: list calling confusion

Postby Wommbatt » Tue Oct 08, 2013 11:23 am

Yup, that's the trick.
Thanks once again Mekire. I was under the impression that the index had to be inside the [] with the row because of how the code works in the for loop.
Didn't occur to me to put it outside.
Now I know.

-wommbatt
Wommbatt
 
Posts: 24
Joined: Thu Jun 13, 2013 2:15 pm


Return to General Coding Help

Who is online

Users browsing this forum: Majestic-12 [Bot] and 3 guests