2012-10-17 51 views
1

文件1:builtins.AttributeError: '海峽' 對象有沒有屬性 '名'

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) 

有多種不同的文件,但這些都是隻有重要的部分。上面的代碼要求輸入,然後根據輸入調用英雄課程。這個類是我必須創建的部分。我創建了一個Rogue類,我用特定的參數調用Hero。我收到以下錯誤:

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

我不更改字符串,我只是檢查它是否存在。爲什麼它告訴我該字符串沒有屬性名稱作爲簡單的'self.name'構造函數?

回答

2

當你做

Hero.__init__(self.name, None) 

正在發生的事情是,「自我'參數不作爲第一個參數隱式傳遞。所以在這種情況下,你實際上傳遞一個字符串(self.name)作爲第一個參數(而不是self)和None而不是'name'參數。如果「獎金」是不是關鍵字參數,這個調用會產生TypeError: __init__() takes exactly 3 arguments (2 given)

所以: self.name代表自我 無代表名稱 和獎金被初始化爲它的默認(0,0,0)

+0

謝謝,這解決了。我忘了第一個參數必須是自己的。 – user1754499

+0

快速後續問題,所以獎金是一個元組(0,0,0),其中獎金[0]是self.hp,獎金[1]是self.strength和獎金[2]是self.radius。對於函數調用,我現在調用Hero .__ init __(self,「Rogue」,我不知道)。我試過使用(0,0,0)和{}作爲第三個參數,但它給了我兩個錯誤。一個說我給了一個太多的參數,另一個說KeyError:0.沒有不可代換的,[]索引超出範圍。我的第三個參數對於將成爲(0,0,0)元組的東西是什麼? – user1754499

+0

正如我所看到的,你有兩個選擇: 英雄.__的init __(自我, 「流氓」) 或 英雄.__的init __(自我, 「流氓」,(0,0,0)) 雙方應該工作。在這兩種情況下,紅利將是(0,0,0) –

0

Hero.__init__初始化一個Hero對象。要構建新的英雄對象,您必須致電Hero。因此,在Rogue.__init__中,該行

Hero.__init__(self.name, None) 

有問題。要麼你想創建一個新的英雄對象:

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

還是讓RogueHero一個子類:

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

我試圖使它張貼,我會得到錯誤是 builtins.NameError前一個子類:名「英雄」沒有定義 基本上,每個子類,它應該通過正確的「英雄」到英雄班。所以如果輸入是盜賊,它將「流氓」作爲參數傳遞給英雄,我可以看到代碼的作用。 – user1754499

相關問題