1

I am programming on my phone. I've created the class character to hold all the information about the character such as the Hp.

After defining the hp, I make a function called getHp() which returns the hp. Later when I call "getHp()" in my "stats()" function it says that "getHp()" is not defined. It does the same for all my functions in my class. Just fyi "stats()" is just a function to gather all my variables (within the class) and print them.

#Adventure game
import random, time

#===========================#
class character():
      self.xp = 0
    def getLvl(self, xp):
            if xp < 5:
                  level = 1
            elif xp < 20:
                  level = 2
            elif xp < 60:
                  level = 3
            elif xp < 120:
                  level = 4
            else:
                  level = 5
            return level
          self.level = getLvl(self, xp)
#-----------------------------------------#
      self.inventory = {"knife": 1 , "bread": 2 , "gold": 10}

      self.armor = 0
#-----------------------------------------#
      self.attack = 6
      def getAttack(self, attack):
            return attack
#-----------------------------------------#
      self.dmg = 1
      def getDmg(self, dmg):
            return dmg
#-----------------------------------------#
      self.hp = 10 * level + armor
      def getHp(self, hp):
            return hp
      def updateHp(self, playerHp):
            self.hp = playerHp
#-----------------------------------------#
      def stats(self):
            self.getLvl(xp)
            self.getHp(hp)
            self.getAttack(attack)
            self.getDmg(dmg)
            print("Player: \n")
            print("Hp: ", hp, "\nLvl: ", level, "\nAttack: ", attack, "\nDmg: ", dmg)
            print("Inventory: ", self.inventory)
#===========================#

character.stats()
TylerH
  • 20,799
  • 66
  • 75
  • 101
Kijilas
  • 33
  • 6

1 Answers1

2

When you are calling the functions within the class, are you using "self"? In your stats() method it should look like this:

def stats(self):
    self.getHp()

This is a way that python knows to refer to the getHp() within the class.

You can find more on Classes and self in the python documentations

https://docs.python.org/3/tutorial/classes.html

mwen1993
  • 63
  • 4
  • just saw the code that you updated, you are missing the 'self' in function calls, function parameters, and variables as well. Add those in and you'll be good – mwen1993 Oct 02 '18 at 09:13
  • I'm guessing I would need an "init" function as well? – Kijilas Oct 02 '18 at 09:14
  • __init__ is a special function that gets executed when you instantiate the object. So if you include an __init__ in your character class, whenever you instantiate a character() object, the __init__ is executed. – mwen1993 Oct 02 '18 at 09:17
  • say you def init and have it print('hello'), then later on you instantiate your character class c1 = character(), c2 = character(), 'hello' is printed out twice – mwen1993 Oct 02 '18 at 09:19
  • Ah ok and I have added in the "self"s but it still isn't working? I'll update the question with the new code. – Kijilas Oct 02 '18 at 09:24
  • You have to instatiate a character() object before you can call it’s methods. So try doing c1 = character(), then call c1.stats() – mwen1993 Oct 02 '18 at 09:33
  • "Self is not defined" – Kijilas Oct 02 '18 at 09:34
  • I've tried that just now and it still says "self is not defined" and I also added self to "class character(self):" and still nothing – Kijilas Oct 02 '18 at 09:47
  • Try class character: you don’t need the () after character – mwen1993 Oct 02 '18 at 09:49
  • No, it still came up with the same thing: "self is not defined" – Kijilas Oct 02 '18 at 09:54
  • Could it be that my app needs me to add some sort of module to get self to work? – Kijilas Oct 02 '18 at 10:02
  • hey i ran your code on an IDE, and there are several mistakes that prevented the code from running. 1. the indentation of your code, they matter as thats how python separates code blocks. 2. where you initialize the variables, if it is within the class but not within any methods, you don't need self. I suggest reading up on variable scoping. I also suggest starting off small, create an empty class and have 1 method and 1 variable. Play with the scoping of that variable to understand it better, then create more methods. – mwen1993 Oct 02 '18 at 17:28
  • Yeah I know about the indentation, I had to change it around a little bit for it to look normal in the thread above. And for the variables I did a bit of research on "self" and it told me about "_init_" and I saw something about var scoping so I will read up on that a bit more. Thanks a lot for the help though, you were very helpful and I will definitely start again like you said only 1 method. – Kijilas Oct 02 '18 at 19:36