1

我正在用 python 编码(或者更确切地说是试图编码)一个游戏,我有以下问题。当我使用循环时,这个循环应该给我字典中的值(x.chest 等,每个字典看起来像那样{'fire': 0.0, 'water': 0.0, 'acid': 0.0,'air': 0.0}):

for damag in (x.chest, x.stomach, x.lhand, x.rhand, x.lleg, x.rleg):
    for value in damag.values():
        print(value)

我明白了

AttributeError: 'function' object has no attribute 'values'

如果我将代码更改为:

for damag in (x.chest, x.stomach, x.lhand, x.rhand, x.lleg, x.rleg):
    for value in damag().values():
        print(value)

它有效,但只是部分有效。它从 x.chest 返回值,然后给出错误:

TypeError: 'dict' object is not callable

我认为这个问题的解决方法一定很简单,但是我的想法有缺陷,所以我找不到它。我可以通过为每个字典做单独的循环来解决这个问题,但我认为这不是最好的主意。

4

1 回答 1

5

显然,您的x属性列表并不相同。您列出的至少一个属性是函数,而至少另一个是字典。

于 2012-07-15T09:25:10.487 回答