Tyler The Hero February 2016

getting error "list indices must be integers, not str"

Okay so the code is

    Pdmg = (lambda x: (round(x*0.75), round(x*1.0)))(['stats']['PStrength'])
    Mdmg = (lambda x: (round(x*0.75), round(x*1.0)))(['stats'['PDexterity'])
    HP = 4 * ['lvl'] * ['stats']['PVitality']
    Mage = {'name' : 'Mage',
    'Class' : 'Player',
    'Sub-Class' : 'Mage',
    'lvl' : 1,
    'xp' : 26,
    'lvlNext' : 25,
    'Weak' : {'Lightning'},
    'Normal' : {'Shock'
                'Burn'
                'Water'
                'Freeze'},
    'Resistance' : {'Ice'
                    'Fire'},
    'Null' : {'Poison'},
    'stats': {'PStrength' : 5,
            'PProsperity' : 5,
            'PDexterity' : 15,
            'PVitality' : 5,
            'PAgility' : 10,
            'hp' : 'HP',
            'MP' : 50}}

and I'm getting the error "list indices must be integers, not str" how do I fix this? (Also I've read through some of the posts that had problems like this and they really didn't help me, so please don't send me to another post.)

Answers


Prune February 2016

Well, the obvious answer is to quit using strings as list indices. If stats, PStrength, and their cognates are variables, just remove the quotation marks.

You also have to supply the name of the list you're trying to index.


Your recent edit is a great improvement. There are two critical, fatal problems with this:

  1. Your code tries to get things out of the dictionary before it's initialized.
  2. You don't give the dictionary name in the reference.

Fixing these ...

Mage = {'name' : 'Mage',
        'Class' : 'Player',
        'Sub-Class' : 'Mage',
        'lvl' : 1,
        'xp' : 26,
        'lvlNext' : 25,
        'Weak' : {'Lightning'},
        'Normal' : {'Shock'
                    'Burn'
                    'Water'
                    'Freeze'},
        'Resistance' : {'Ice'
                        'Fire'},
        'Null' : {'Poison'},
        'stats': {'PStrength' : 5,
                  'PProsperity' : 5,
                  'PDexterity' : 15,
                  'PVitality' : 5,
                  'PAgility' : 10,
                  'hp' : 'HP',
                  'MP' : 50}}

Pdmg = (lambda x: (round(x*0.75), round(x*1.0)))(Mage['stats']['PStrength'])
Mdmg = (lambda x: (round(x*0.75), round(x*1.0)))(Mage['stats']['PDexterity'])
HP = 4 * Mage['lvl'] * Mage['stats']['PVitality']

print Pdmg, Mdmg, HP

Output:

(4.0, 5.0) (11.0, 15.0) 20

I also repaired the missing left bracket in the Mdmg line.


Dietrich Epp February 2016

This is your problem:

['stats']['PVitality']

This is equivalent to the following:

my_list = [
    'stats'
]
my_list['PVitality']

It was hard to tell you how to fix this until the edit, but now I see the data structure you are using.

You want something like this:

Mage['stats']['PStrength']

Although Mage must be defined before you use it.

Minor note

Idiomatic Python would not use lambda here, since all lambda is doing is avoiding the leak of a temporary variable into the enclosing scope. More idiomatic would be something like:

x = Mage['stats']['PStrength']
Pdmg = round(x*0.75), round(x*1.0)

Or something like:

def stat_damage(stat):
    """Calculate damage (foo, bar) from stat value."""
    return round(x*0.75), round(x*.10)

However, code style is a matter of taste and the lambda does function correctly here. It will just be a bit of a surprise to other Python programmers.

Post Status

Asked in February 2016
Viewed 3,877 times
Voted 13
Answered 2 times

Search




Leave an answer