TypeError: __init __ () accepts at least 2 arguments (1 given) error

I am developing a simple text game Dungeon using Python3. First, the user is prompted to select a hero from the screen.py file.

from game import * 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) def play(self): '''(Game) -> NoneType The main game loop.''' exit = False while not exit: print(self) if self.game.game_over(): break c = input("Next: ") if c in ['q', 'x']: print("Thanks for playing!") exit = True elif c == 'w': # UP self.game.move_hero(-1, 0) elif c == 's': # DOWN self.game.move_hero(1, 0) elif c == 'a': # LEFT self.game.move_hero(0, -1) elif c == 'd': # RIGHT self.game.move_hero(0, 1) elif c == 'r': ## RESTART GAME self.initialize_game() else: pass def __str__(self): '''(GameScreen) -> NoneType Return a string representing the current room. Include the game Hero string represetation and a status message from the last action taken.''' room = self.game.current_room s = "" if self.game.game_over(): #render a GAME OVER screen with text mostly centered #in the space of the room in which the character died. #top row s += "X" * (2 + room.cols) + "\n" #empty rows above GAME OVER for i in list(range(floor((room.rows - 2) / 2))): s += "X" + " " * room.cols + "X\n" # GAME OVER rows s += ("X" + " " * floor((room.cols - 4) / 2) + "GAME" + " " * ceil((room.cols - 4) / 2) + "X\n") s += ("X" + " " * floor((room.cols - 4) / 2) + "OVER" + " " * ceil((room.cols - 4) / 2) + "X\n") #empty rows below GAME OVER for i in list(range(ceil((room.rows - 2) / 2))): s += "X" + " " * room.cols + "X\n" #bottom row s += "X" * (2 + room.cols) + "\n" else: for i in range(room.rows): for j in room.grid[i]: if j is not None: if j.visible: s += j.symbol() else: #This is the symbol for 'not yet explored' : ? s += "?" s += "\n" #hero representation s += str(self.game.hero) #last status message s += room.status return s if __name__ == '__main__': gs = GameScreen() gs.initialize_game() gs.play() 

Whenever I run this code, I get this error: TypeError: init () takes at least 2 arguments (1 given) that are related to Rogue () or other hero classes. Here is hero.py.

 class Rogue(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, rogue, bonuses=(0, 0, 0)): '''(Rogue, str, list) -> NoneType Create a new hero with name Rogue, an empty list of items and bonuses to hp, strength, gold and radius as specified in bonuses''' self.rogue = rogue self.items = [] self.hp = 10 + bonuses[0] self.strength = 2 + bonuses[1] self.radius = 2 + bonuses[2] Tile.__init__(self, True) def symbol(self): '''(Rogue) -> str Return the map representation symbol of Hero: O.''' #return "\u263b" return "O" def __str__(self): '''(Item) -> str Return the Hero name.''' return "{}\nHP:{:2d} STR:{:2d} RAD:{:2d}\n".format( self.rogue, self.hp, self.strength, self.radius) def take(self, item): '''ADD SIGNATURE HERE Add item to hero items and update their stats as a result.''' # IMPLEMENT TAKE METHOD HERE pass def fight(self, baddie): '''ADD SIGNATURE HERE -> str Fight baddie and return the outcome of the battle in string format.''' # Baddie strikes first # Until one opponent is dead # attacker deals damage equal to their strength # attacker and defender alternate if self.hp < 0: return "Killed by" return "Defeated" 

What am I doing wrong?

+7
source share
2 answers

Problem

In GameScreen.initialize_game() you set hero=Rogue() , but the Rogue constructor takes Rogue as an argument. (In other words, __init__ of Rogue requires passing Rogue .) You probably have the same problem if you set hero=Mage and hero=Barbarian .

Decision

Fortunately, the fix is ​​simple; you can simply change hero=Rogue() to hero=Rogue("MyRogueName") . Perhaps you can ask the user for a name in initialize_game , and then use that name.

Notes on "at least 2 arguments (1 given)"

When you see such errors, it means that you called a function or method without passing enough arguments to it. ( __init__ is a special method that is called when an object is initialized.) Therefore, when you debug things like this in the future, look where you call the function / method and where you define it, and make sure the two have the same number of parameters.

One thing that poses a difficult problem with such errors is self , which is passed in.

 >>> class MyClass: ... def __init__(self): ... self.foo = 'foo' ... >>> myObj = MyClass() 

In this example, you might think: "Strange, I initialized myObj , so MyClass.__init__ was called, why didn’t I have to pass something for self ?" The answer is that self effectively passed whenever the notation "object.method ()" is used. Hope this helps resolve the error and explains how to debug it in the future.

+9
source
 Class Rogue: ... def __init__(self, rogue, bonuses=(0, 0, 0)): ... 

__init__ for your Rogue class needs the Rogue parameter, but you create it as hero = Rogue() in initialize_game .

You need to pass it the appropriate parameter, for example hero = Rogue('somename')

+1
source

All Articles