AttributeError:“function”对象没有属性“values”

2024-09-27 02:22:35 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在用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

我认为解决这个问题一定很容易,但我的思想有缺陷,所以找不到。我可以通过对每本字典分别循环来解决这个问题,但我认为这不是最好的主意。


Tags: infor字典objectvaluedictvaluesprint

热门问题