1

I've been working on making a combat test for a RPG game. For whatever reason I am getting a class has no attribute error.

Any ideas how to fix this?

class Enemy:
 def __init__(self, name, H, STR):
 self.name = name
 self.H = H
 self.STR = STR
 def is_alive(self):
 return self.H > 0
 enemyturn = ["1", "2"]
class Goblin(Enemy):
 def __init__(self, name, H, STR):
 name = "Goblin"
 H = 50
 STR = 5

These classes are used in the code below:

if command == "1":
 Goblin.H -= Player.STR
 print("You strike the Goblin for {0} damage.".format(Player.STR))
 random.choice(enemyturn)
 if random.choice == "1":
 Player.H -= Goblin.STR
 print("The Goblin strikes you for {0} damage.".format(Player.STR))
 if random.choice == "2":
 pass
 Combat()
Jivan
23.4k17 gold badges92 silver badges144 bronze badges
asked Dec 31, 2014 at 4:56
2
  • Which line returns this error? Commented Dec 31, 2014 at 4:58
  • 2
    @Marcin this one: Goblin.H -= Player.STR Commented Dec 31, 2014 at 4:59

1 Answer 1

3

You are calling your attributes before the objects are instantiated.

You should declare them as class variables.

class Enemy:
 name = ""
 H = 0
 STR = 0
 def __init__(self, name, H, STR):
 self.name = name
 self.H = H
 self.STR = STR
 def is_alive(self):
 return self.H > 0
class Goblin(Enemy):
 def __init__(self, name, H, STR):
 self.name = "Goblin"
 self.H = 50
 self.STR = 5
answered Dec 31, 2014 at 5:04
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.