AttributeError:'list'对象没有属性'f1index'

时间:2014-01-10 11:00:39

标签: python

我从这段代码中收到错误。

fighter1, fighter2 = random.sample(players, 2)
f1index = (players).index(fighter1)
f2index = (players).index(fighter2)
# ^ finds both fighters index in list
f1strength = strengths.f1index
f2strength = strengths.f2index
# ^ finds fighters strength attributes

这是我得到的错误。

Traceback (most recent call last):
  File "N:\Files\Computing studies\Battle\Tournament.py", line 94, in <module>
    f1strength = strengths.f1index
AttributeError: 'list' object has no attribute 'f1index'

1 个答案:

答案 0 :(得分:2)

要访问位置f1index的元素,您必须使用下标符号,例如

f1strength = strengths[f1index]
f2strength = strengths[f2index]