AttributeError:NoneType对象没有属性“健康" [英] AttributeError: NoneType object has no attribute 'health'

查看:80
本文介绍了AttributeError:NoneType对象没有属性“健康"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的战略游戏桌战"中进行长时间的比赛时,出现此错误.每当战场上有许多单位时,这种情况似乎就会发生.这是回溯:

I get this error during a lengthy match in my strategy game, Table Wars. It seems to occur whenever there are many units on the battlefield. Here is the traceback:

Traceback (most recent call last):
  File "C:\Users\Oventoaster\Desktop\Games\Table Wars\Table Wars.py", line 727, in <module>
    main()
  File "C:\Users\Oventoaster\Desktop\Games\Table Wars\Table Wars.py", line 131, in main
RedTeam.update()
  File "C:\Python27\lib\site-packages\pygame\sprite.py", line 399, in update
for s in self.sprites(): s.update(*args)
  File "C:\Users\Oventoaster\Desktop\Games\Table Wars\Table Wars.py", line 372, in update
self.attack()
  File "C:\Users\Oventoaster\Desktop\Games\Table Wars\Table Wars.py", line 393, in attack
    self.target.health -= self.attack_damage
AttributeError: 'NoneType' object has no attribute 'health'

定位和攻击代码中似乎出现了例外,因此我将其发布在这里:

The exception seems to appear in the targeting and attacking codes, so I will post them here:

def move_toward(self, target):
    if self.target is None:
        self.rect.move_ip(1, 0)

def update(self):
    self.find_target()
    self.move_toward(self.target)
    self.attack()
    if self.health <= 0:
        self.kill()

def find_target(self):
    if self.target is not None: return
    for enemy in BluTeam.sprites():
        if enemy.rect.centerx - self.rect.centerx <= self.range and enemy.rect.centery - self.rect.centery <= self.range:
            self.target = enemy
            return
        self.target = None

def attack(self):
    global REDGOLD
    global BLUECOMMAND
    if self.target is None: return
    if self.target.health <= 0:
        REDGOLD += self.target.reward
        BLUECOMMAND += self.target.cmdback
        self.target = None
    if not self.cooldown_ready(): return
    self.target.health -= self.attack_damage

def cooldown_ready(self):
    now = pygame.time.get_ticks()
    if now - self.attack_last >= self.attack_cooldown:
        self.attack_last = now
        return True
    return False

我该如何解决?

推荐答案

解释常见错误或如何更好地理解调试程序的简单回溯:

  File "<path>\<filename>.py", line 393, in <function>

Traceback为您提供了调用堆栈,并指出了在哪个函数中引发的错误.这里没什么特别的

Traceback gives you the callstack and points to the error raises in which function. Nothin special here

    self.target.health -= self.attack_damage

友好的解释器给出引起错误的语句.这很重要,因为这意味着引发的错误与此代码行有关.

Friendly interpreter gives the statement causing the error. This is important as it means the error raised is related to this line of code.

AttributeError: 'NoneType' object has no attribute 'health'

在这里.错误是 AttributeError ;该文档对此非常清楚:我正在尝试读取的值,或者正在尝试分配给不属于相关对象成员的变量.

Here we are. The error is AttributeError ; the doc is pretty clear about it: I'm trying to read the value of or I'm trying to assign to a variable that is not a member of the related object.

我的对象是什么?这是一个'NoneType'对象;我们没有名字,但是我们有它的类型.我们遇到问题的对象是 NoneType ,因此它是特殊的对象 None .

What is my object ? It is a 'NoneType' object ; we don't have the name, but we have its type. The object we have an issue with is NoneType, thus it is the special object None.

None 对象有什么错误?它没有'health'属性,它说的是Traceback.因此,我们正在错误产生的代码行中的某处访问属性 health ,并且在 None 对象上调用了此属性.

What is the error with this Noneobject ? It doesn't have a 'health' attribute is saying the Traceback. So, we are accessing the attribute health somewhere in the line of code the error raises, and this is called on a None object.

现在我们几乎都准备好了: self.target.health 是我们使用 health 的错误行中的唯一位置,因此,这意味着 self.target None .现在,我看一下源代码,我试图理解为什么会这样,即使我在功能的开头对它进行了检查.这导致我必须在该行之后以某种方式将其设置为 None .

Now we are almost all set: self.target.health is the only location in the error line we use health, thus it means self.target is None. Now I look at the source code I try to understand why it could be, even if I put a check at the beginning at the function against it. It leads to the fact I must set it to None somehow after that line.

所有这些推理步骤都会导致简短的答案:

All of this reasoning steps lead to the short answer:

这意味着 self.target None .错误来自您的代码:

It means self.target is None. The error comes from your code:

if self.target.health <= 0:
    REDGOLD += self.target.reward
    BLUECOMMAND += self.target.cmdback
    self.target = None  # set to None
if not self.cooldown_ready(): return
self.target.health -= self.attack_damage  # self.target is None, .health failed

这篇关于AttributeError:NoneType对象没有属性“健康"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆