Home > OS >  How To Solve Error: GameCharacter() takes no arguments
How To Solve Error: GameCharacter() takes no arguments

Time:08-21

I Have Created A Game Character example which has name, x position and health. I based it on a tutorial I saw somewhere. I don't know what is wrong? I seem to have done everything exactly as I saw there. Please Help.

class GameCharacter:
 
  def _init_(self, name, x_pos, health):
    self.name = name
    self.x_pos = x_pos
    self.health = health

  def move(self, by_amount):
    self.x_pos  = by_amount

  def take_damage(self, amount):
    self.health -= amount
    if self.health < 0:
     self.health = 0

  def check_is_dead(self):
    return self.health <= 0

  game_character = GameCharacter('Rick', 5, 100)
  
print(game_character.name)

CodePudding user response:

_init_ should be __init__

it's called a dunder init, aka double underscore

CodePudding user response:

Your game_character var is inside your class. Moving it outside should help

CodePudding user response:

i think this line with a Tab before it still is part of class codes: game_character = GameCharacter('Rick', 5, 100)

so it would be: class GameCharacter:

  def _init_(self, name, x_pos, health):
    self.name = name
    self.x_pos = x_pos
    self.health = health

  def move(self, by_amount):
    self.x_pos  = by_amount

  def take_damage(self, amount):
    self.health -= amount
    if self.health < 0:
     self.health = 0

  def check_is_dead(self):
    return self.health <= 0
game_character = GameCharacter('Rick', 5, 100)

print(game_character.name)
  • Related