我正在用python编写(或者更确切地说是试图编写)一个游戏,我遇到了以下问题。
当我使用loop这个循环时,它应该给出字典中的值(x.chest等,每个dict看起来都是这样的{'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
我认为解决这个问题一定很容易,但我的思想有缺陷,所以找不到。我可以通过对每本字典分别循环来解决这个问题,但我认为这不是最好的主意。
显然,您的
x
属性列表是不同质的;至少您列出的属性中有一个是函数,而至少另一个是dict相关问题 更多 >
编程相关推荐