Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/350.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/visual-studio-2008/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 内置属性错误:';str';对象没有属性';名称';_Python_Attributes_Python 3.x - Fatal编程技术网

Python 内置属性错误:';str';对象没有属性';名称';

Python 内置属性错误:';str';对象没有属性';名称';,python,attributes,python-3.x,Python,Attributes,Python 3.x,文件1: 文件2: 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)''' cl

文件1:


文件2:

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

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)

我没有改变字符串,我只是检查它是否在那里。为什么它告诉我字符串没有简单的“self.name”构造函数的属性名?

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

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'
这是错误的。您可以创建一个新的英雄对象:

Hero.__init__(self.name, None)
或者让盗贼成为英雄的一个子类:

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

当你这样做的时候发生了什么

class Rogue(Hero):
    def __init__(self):
        super().__init__("Rogue", None)
“self”参数未作为第一个参数隐式传递。因此,在本例中,您实际上传递了一个字符串(self.name)作为第一个参数(而不是self),并传递了None而不是'name'参数。如果“bonenses”不是关键字参数,则此调用将产生
TypeError:\uuuu init\uuuu()正好接受3个参数(给定2个)

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

在发布之前,我试图将其作为一个子类,而我将得到的错误是内置的。NameError:name'Hero'基本上没有定义,对于每个子类,它应该将正确的“Hero”传递给Hero类。因此,如果输入是Rogue,它会将“Rogue”作为一个参数传递给Hero,我可以看到代码是这样做的。谢谢,这已经解决了。我忘记了第一个参数必须是self.Quick follow问题,所以奖金是一个元组(0,0,0),其中奖金[0]是self.hp,奖金[1]是self.strength,奖金[2]是self.radius。对于函数调用,我现在调用Hero.\uuuu init\uuuself(我不知道,self是Rogue)。我曾尝试使用(0,0,0)和{}作为第三个参数,但这两个参数都有错误。一个说我给了一个太多的参数,另一个说KeyError:0。None不可下标,[]索引超出范围。对于将成为(0,0,0)元组的对象,我的第三个参数是什么?正如我所看到的,您有两个选项:英雄。uuu init_uuuuuuuuuuuuuuuuuuself(Rogue)或英雄。uuuuuuuuuuuuuuself(Rogue),(0,0))两者都应该起作用。在任何一种情况下,bonuse都将是文件“/Applications/WingIDE.app/Contents/MacOS/src/debug/tserver/_sandbox.py”的第100行,在文件“/Applications/WingIDE.app/Contents/MacOS/src/debug/tserver/_sandbox.py”的第25行,在initialins.TypeError:u init_uuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuu参考:self.game=game(“rooms/startroom”,hero),如果我调用函数hero,它会添加一个额外的参数。如果我让函数调用Hero.\uuuInit\uuuself(Rogue),它将停止给出此错误。
Hero.__init__(self.name, None)