python - getting error "list indices must be integers, not str" -
okay 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 error "list indices must integers, not str" how fix this? (also i've read through of posts had problems , didn't me, please don't send me post.)
this problem:
['stats']['pvitality'] this equivalent following:
my_list = [ 'stats' ] my_list['pvitality'] it hard tell how fix until edit, see data structure using.
you want this:
mage['stats']['pstrength'] although mage must defined before use it.
minor note
idiomatic python not use lambda here, since lambda doing avoiding leak of temporary variable enclosing scope. more idiomatic like:
x = mage['stats']['pstrength'] pdmg = round(x*0.75), round(x*1.0) or like:
def stat_damage(stat): """calculate damage (foo, bar) stat value.""" return round(x*0.75), round(x*.10) however, code style matter of taste , lambda function correctly here. bit of surprise other python programmers.
Comments
Post a Comment