builtins.AttributeError:“str”对象没有属性“name”

2024-05-14 17:23:52 发布

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

文件1:

class Rogue():
    def __init__(self):
        self.name = "Rogue"
        Hero.__init__(self.name, None)

'''class Barbarian(Hero):
    Hero.__init__(self, name, bonuses)

class Mage(Hero):
    Hero.__init__(self, "Mage", bonuses)'''


class Hero(Tile):
'''A class representing the hero venturing into the dungeon.
Heroes have the following attributes: a name, a list of items,
hit points, strength, gold, and a viewing radius. Heroes
inherit the visible boolean from Tile.'''

def __init__(self, name, bonuses=(0, 0, 0)):
    '''(Hero, str, list) -> NoneType
    Create a new hero with name name,
    an empty list of items and bonuses to
    hp, strength, gold and radius as specified
    in bonuses'''

    self.name = name
    self.items = []
    #Rogue
    if self.name == "Rogue":
        self.hp = 10 + bonuses[0]
        self.strength = 2 + bonuses[1]
        self.radius = 2 + bonuses[2]
    #Barbarian
    elif self.name == "Barbarian":
        self.hp = 12 + bonuses[0]
        self.strength = 3 + bonuses[1]
        self.radius = 1 + bonuses[2]
    #Mage
    elif self.name == "Mage":
        self.hp = 8 + bonuses[0]
        self.strength = 2 + bonuses[1]
        self.radius = 3 + bonuses[2]

    Tile.__init__(self, True)

文件2:

class GameScreen:
    '''Display the current state of a game in a text-based format.
    This class is fully implemented and needs no
    additional work from students.'''

def initialize_game(self):
    '''(GameScreen) -> NoneType
    Initialize new game with new user-selected hero class
    and starting room files.'''

    hero = None
    while hero is None:
        c = input("Select hero type:\n(R)ogue (M)age (B)arbarian\n")
        c = c.lower()
        if c == 'r':
            hero = Rogue()
        elif c == 'm':
            hero = Mage()
        elif c == 'b':
            hero = Barbarian()

    self.game = Game("rooms/startroom", hero)

有多个不同的文件,但这些是唯一重要的部分。上面的代码要求输入,然后根据输入调用hero类。这个类是我必须创建的部分。我创建了一个类Rogue,在这里我用特定的参数调用Hero。我得到以下错误:

File "/Users//Documents/CSC148/Assignment 2/hero.py", line 7, in __init__
Hero.__init__(self.name, None)
  File "/Users//Documents/CSC148/Assignment 2/hero.py", line 30, in __init__
self.name = name
builtins.AttributeError: 'str' object has no attribute 'name'

我不是在换绳子,我只是在检查它是否在那儿。为什么它告诉我字符串对于简单的“self.name”构造函数没有属性名?


Tags: andthenameselfnoneinitstrengthclass
2条回答

当你这样做的时候

Hero.__init__(self.name, None)

“self”参数不是作为第一个参数隐式传递的。因此在本例中,您实际上传递了一个字符串(self.name)作为第一个参数(而不是self),而没有传递任何字符串来代替“name”参数。如果“红利”不是关键字参数,则此调用将产生TypeError: __init__() takes exactly 3 arguments (2 given)

所以: self.name代表self 没有代表名字 奖金初始化为默认值(0,0,0)

Hero.__init__初始化Hero对象。要构造新的Hero对象,必须调用Hero。因此,在Rogue.__init__

Hero.__init__(self.name, None)

是错误的。您要么要创建一个新的英雄对象:

class Rogue:
    def __init__(self):
        self.name = "Rogue"
        self.enemy = Hero(self.name, None)

或者让Rogue成为Hero的子类:

class Rogue(Hero):
    def __init__(self):
        super().__init__("Rogue", None)

相关问题 更多 >

    热门问题