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

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,
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

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

New Users, Read This
  • Use code tags when posting code.
  • Include any errors with your post (in code tags).
  • Describe your problem; not your chosen solution.
  • Make examples the minimum length to demonstrate your issue.
User avatar
Posts: 1711
Joined: Thu Feb 07, 2013 11:33 pm
Location: Tucson, Arizona

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.

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

Return to General Coding Help

Who is online

Users browsing this forum: Bing [Bot], Google [Bot], Majestic-12 [Bot] and 9 guests