简体   繁体   English

AttributeError:'str'对象没有属性'attack'“'Help!”

[英]AttributeError: 'str' object has no attribute 'attack' “'Help!”

I have q quick question regarding an Attribute Error regarding a basic Arena battler I am writing for my Intro to Programming Class. 我有一个关于基本错误的竞技场战斗人员的属性错误的快速解答,我正在为我的《编程入门》编写。 Here is the chunk of code that I am having trouble with upon running the program, 这是我在运行程序时遇到的代码块,

class Enemy:
    def __init__(self,player,weapons,armor):

        self.name = "Bad Guy" 
        self.health = 100
        self.attackPower = (player.attack + randint(-5,5))
        self.defensePower = (player.defense + randint(-5,5))
        self.weapon = player.weapon
        self.armor = player.armor
    def name_generator(self):
        import random
        element = ["Thunder","Lightning","Wind","Fire", "Stone"]
        tool = ["Hammer","Drill","Cutter","Knife", "Saw"]
        randomNumber1 = random.randrange(0,len(element))
        randomNumber2 = random.randrange(0,len(tool))
        self.randomname = element[randomNumber1] + " " + tool[randomNumber2]
        return self.randomname

Lol, ignore the Name Generator for now, thats an idea Ill try to work out later. 大声笑,暂时不要使用名称生成器,这是一个想法,稍后我会尝试解决。 The Current problem I am having now is that when I run the program through IDLE i get the following error; 我现在遇到的当前问题是,当我通过IDLE运行程序时,出现以下错误;

  File "C:\Users\Caleb Walter\Downloads\Arena_Battler.py", line 150, in __init__
    self.attackPower = int(player.attack + randint(-5,5))
AttributeError: 'str' object has no attribute 'attack'

Any Help would be appreciated on the program error, as I have done research and tried to find the answer but all of the other casses of 'str' error involved lists. 对程序错误的任何帮助将不胜感激,因为我已经进行了研究并试图找到答案,但所有其他'str'错误的情况都涉及到列表。 Thank You in Advance! 先感谢您!

显然,您正在使用播放器名称(字符串)而不是播放器对象(带有参数“攻击”,“防御”等)来调用此类的构造函数(在其中实例化)。

How to read and understand error messages, so that you can help yourself: 如何阅读和理解错误消息,以帮助自己:

First, we read it: 首先,我们阅读它:

File "C:\Users\Caleb Walter\Downloads\Arena_Battler.py", line 150, in __init__
self.attackPower = int(player.attack + randint(-5,5))
AttributeError: 'str' object has no attribute 'attack'

The first line says what file and line we were at when the problem occurred. 第一行说明问题发生时我们所在的文件和行。 The second line is a copy of the line causing the problem, so we can look at it without having to go back and look it up. 第二行是导致问题的行的副本,因此我们可以查看它而不必回头查找它。 The last line is a description of what the actual problem was. 最后一行是对实际问题的描述。

Therefore, the next step is to read what the problem was. 因此,下一步是阅读问题所在。 AttributeError means that we tried to get an attribute from something that doesn't have it. AttributeError意味着我们试图从没有属性的东西中获取属性。 The rest says what kind of thing we tried to get it from (a 'str' object , ie, a string), and what we tried to get (an attribute named attack ). 其余的内容说明了我们试图从哪种'str' object (一个'str' object ,即一个字符串)获得什么,以及我们试图得到什么(一个名为attack的属性)。

Next we look at the line in question. 接下来,我们来看待讨论的行。 Where do we attempt to get an attribute named attack from something? 我们在哪里尝试从某事物获取名为attack的属性? Clearly, it's the part where we've written player.attack . 显然,这是我们编写player.attack的部分。 Thus, we now know what happened: our code tries to get the attack attribute from the player variable, but player is referring to a string, and strings don't have an attack attribute. 因此,我们现在知道发生了什么:我们的代码尝试从player变量获取attack属性,但是player引用的是字符串,而字符串没有attack属性。 (Obviously; what is the attack of "hi mom" ? That doesn't make any sense.) (显然; "hi mom"attack是什么?这没有任何意义。)

How did this happen? 这怎么发生的? Well, if we wrote code that tries to read an attack attribute, clearly we must have been expecting it to be there. 好吧,如果我们编写了尝试读取attack属性的代码,那么显然我们一定一直在期待它的存在。 We don't expect such an attribute on strings, so the error is that we've put a string into player when we expected something else. 我们不希望在字符串上有这样的属性,所以错误是当我们期望其他内容时,我们已经将字符串放入player

How does the value get into player ? 价值如何进入player It's a parameter to the function. 这是函数的参数。 Therefore, we called it wrongly, and we go off to find the line of code where we called it (hint: look at the previous entry in the stack-trace ), and fix the surrounding code. 因此,我们错误地调用了它,然后开始查找我们称之为它的代码行(提示: 查看stack-trace中的上一个条目 ),并修复周围的代码。 What were we supposed to pass? 我们应该通过什么? Well, something that does have an attack , obviously. 好吧,显然有些东西确实有attack Did you have some kind of Player class? 您有某种球员课吗? Then you should be passing an instance of that, rather than a (the player's?) name or whatever. 然后,您应该传递一个实例,而不是(玩家的?)名称或其他名称。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM