1

I'm fairly new to the idea of classes and subclasses in Python. I've found some questions/answers here on Stackoverflow related to my question, but they don't seem really answer the specifics that I am looking for. I consulted YouTube and other websites, but I'm not having much luck. Now, I might be asking the wrong questions or looking in the wrong places.So, please bear with me for a bit.

Now, I do understand how classes work in Python. That's not the problem.

class Person:
  def __init__(self, name, age):
    self.name = name
    self.age = age

p1 = Person("John", 36)

print(p1.name)
print(p1.age)

This is a relatively basic concept that I think everyone can grasp.

However, I'm having trouble understanding how subclasses work together and how to call up functions that are stored within a class. I want to add a function called "damage" to the class monster or person. I have tried making the class monster inherit the class person so that it also has its properties and can do damage to its health.

I want to create something like this:

class character:
    def __init__(self, name, health ):
        self.name = name
        self.health = health

class  monster(character):
    def __init__(self, name, health):
        character.__init__(self,name,health)
        self.health = health
        self.name = name

    def attack (self):
         self.health-1

john=character("john",22)
boo=monster("boo",20,22)
monster.attack()

By invoking this code outside of the classes, I want to use the function attack so that the health of the user is reduced.

I am still a novice and I have tried various methods.

So that you get a better idea of what I mean, here is the reference code that I am trying to use:

https://trinket.io/python/07c3a147aa

Unfortunately, it's written in legacy code and I am not familiar with all the differences between 2.7 and 3.8. I've tried to adapt and to dissect the code so that I better understand the underlying processes, but I'm not having much luck. It keeps breaking in multiple spots and I keep on getting "Attribute Errors" all over the place.

The thing that I really like about this code is that the character can enter a command like "help" or "attack" and then the function from the respective class is called up. I wanted to modify this code so that it better suits my needs like adding other functions and characters to it, but I am not really having much luck.

If someone could help fix my very flawed code and thinking, I would very appreciative.

I also thank you for taking the time out to read this.

Christopher

  • if this is the whole code you have, then the solution is simple: you don't have an attribute called "stärke" also I suggest don't use "ä, ö, and ü" in coding. Otherwise if that is not all of your code please provide the rest of the code. – Boendal Nov 19 '19 at 08:58
  • Hello, Boendal, that is not the problem. I simply forgot change the terms. I'll still get euros like TypeError: attack() missing 1 required positional argument: 'self' NameError: name 'self' is not defined Thank for letting me know that I forgot to update it. – Poetry Lion Nov 19 '19 at 09:01
  • I'm not entirely sure what you're asking here. But don't you mean `boo.attack()`? "monster" is the class, not the instance (note that this would be clearer if you used proper Python style, which is InitialCaps for class names: Monster, Character). – Daniel Roseman Nov 19 '19 at 09:02
  • Hey, wow, that actually helped out a lot. The program doesn't show me any errors, but it also doesn't produce the desired effect, Forgive me for having proper command of the terminology. Thank your for pointing out proper style. I forgot that classes have to be capitalized. I just forgot to use it because I hate using caps while writing code. Basically, whenever I invoke the function "boo.attack()", I want for the health of the character, John, to be reduced by let's say 1 point. – Poetry Lion Nov 19 '19 at 09:10
  • Well, then you need to tell the monster who it is that they are attacking, ie john. So you need to accept (eg) "target" as a parameter to "attack", and subtract health from that rather than self. – Daniel Roseman Nov 19 '19 at 09:21
  • Thank you ! That helped a lot. Now the program functions the way that I wanted it to ! – Poetry Lion Nov 19 '19 at 09:29

1 Answers1

1

Your code has two problems besides the name mix-up you already noticed.

def attack (self):
     self.health-1

This should be:

def attack(self):
    self.health -= 1

Your original code subtracted one from the value of health and then threw away the result.

And the second problem:

boo=monster("boo",20,22)
monster.attack()

This should be:

boo=monster("boo",20)
boo.attack()

If you want to call a class method you need to do so on an object of that class, not on the class itself.

Edit: And as noticed by the commenter below, there was one parameter too many.

Edit2: In regards to your request about the attack function, the obvious solution is:

john.attack()

(this of course would require a suitable attack function for the character class)

But maybe you would like the monster to attack john. That could be implemented like this:

def attack(self, target):
    target.health -= 1

And then for performing the attack:

boo.attack(john)
Strick
  • 545
  • 1
  • 4
  • 12
  • another mix up he did is that monster only has 2 parameter and not 3 – Boendal Nov 19 '19 at 09:09
  • Thank you very much for your help ! Hey, yeah, I noticed the parameters too. I am basically almost though. So, the code that you suggested does work very well. However, I want it to from John, but not from boo. Whenever I invoke boo.attack(), that object gets reduced, but I want to the object John to be reduced. – Poetry Lion Nov 19 '19 at 09:17
  • I made an edit in regards to this request. For the future I suggest you include this kind of detail in the original question. Saves a lot of editing. – Strick Nov 19 '19 at 09:24
  • Yes, you're correct. I will include that kind of information in my future question. I tried to be as detailed as possible. I appreciate your help and it really helped me see what was wrong with my code. Thanks ! – Poetry Lion Nov 19 '19 at 09:30