Project : Pokemon masters (AttributeError)

The 2nd last line : "self.cur_health += amt"is giving me builtins.AttributeError: ‘int’ object has no attribute
it says there is no attribute called “cur_health”, however, I defined cur_health in the constructor as:
self.cur_health = cur_health.
I don’t seem to understand what’s the problem with my code

Thank you in advance

class Pokemon:
    def __init__(self,name="N/A", level= 0, p_type="No type", ko = False):
        self.nem = name #pokemon name
        self.level = level # level of pokemon
        self.pType = p_type #pokemon type 
        self.m_health = level * 5 # dependent on the level
        self.cur_health = level*5
        self.ko = ko #pmon knocked out or not
    def lose_health(self,amt):
        self.cur_health = self.cur_health - amt
        print(self.nem, "now has ", self.cur_health)
        return  self.cur_health

    def get_health(self):
        return self.cur_health
    
    def gain_health(self,amt=0):
        self.cur_health += amt
        print("{n} has gained {a} hp and now has {h} hp ".format(n=self.nem, a = amt,h=self.cur_health)) #**here it is giving me  builtins.AttributeError: 'int' object has no attribute 'cur_health'**strong text**

Hi @digitalpro01859,

Welcome to the forums!

Whatever code is giving you that error must be outside of your class definition.
I simulated your code by copy-pasting it here and it works just fine.

Hi, thank you for looking into my code. It works the way you simulated, however, when I run the full code it shows me an attribute error when testing with “testT.use_potion() in the #test section”

    def __init__(self,name="N/A", level= 0, p_type="No type", ko = False):
        self.nem = name #pokemon name
        self.level = level # level of pokemon
        self.pType = p_type #pokemon type 
        self.m_health = level * 5 # dependent on the level
        self.cur_health = level*5
        self.ko = ko #pmon knocked out or not
    def lose_health(self,amt):
        self.cur_health = self.cur_health - amt
        print(self.nem, "now has ", self.cur_health)
        return  self.cur_health

    def get_health(self):
        return self.cur_health
    
    def gain_health(self,amt=0):
        self.cur_health += amt
        print("{n} has gained {a} hp and now has {h} hp ".format(n=self.nem, a = amt,h=self.cur_health))
        
       
     

    def knock_out(self):
        self.cur_health = 0
        self.ko = True
        print(self.nem, " is knocked out")
        return self.cur_health, self.ko

    def revive(self):
        self.cur_health = self.m_health
        self.ko = False
        print(self.nem, " is revived out")
        return self.cur_health, self.ko
    
    def attack(self,otherPokemon,att=0):
        damage = att
        if self.pType == "Fire" and otherPokemon.pType == "Grass":
            damage = att*2
        elif self.pType == "Grass" and otherPokemon.pType == "Fire":
            damage = att * 0.5
        elif self.pType == "Fire" and otherPokemon.pType == "Water":
            damage = att * 0.5
        elif self.pType == "Water" and otherPokemon.pType == "Fire":
            damage = att*2
        else:
            damage = att
        print(self.nem," damaged ", damage, "pts of", otherPokemon.nem)
        return otherPokemon.lose_health(damage)


class Trainer:
    def __init__(self, name, no_potions, pokemons, active_pokemon = "a" ):
        self.nem = name
        self.potions = no_potions
        self.pokemons = pokemons
        self.active_pokemon = active_pokemon
        print("Welcome {nem} to the world of Pokemon, you have {pokemons} pokemons,{potions} potions and your current pokemon is {active}".format(nem = self.nem, potions=self.potions, pokemons= len(self.pokemons), active = self.active_pokemon))
    
    
    def use_potion(self):
        self.potions -=1
        print("You used a potion, you have {}(s)".format(self.potions))
        Pokemon.gain_health(10)
    
    
    def attack_other_trainer(self,otherTrainer):
        their_pokemon = otherTrainer.pokemon[otherTrainer.active_pokemon]
        print("{you} attactked {that}".format(you= self.nem, that = otherTrainer.nem))
        return Pokemon.attack(their_pokemon)
    
    
    
    def choose_pokemon(self):
        userInp = int(input("Choose pokemon from the list, enter a number corresponding to position of the pokemon in the list:"))
        self.active_pokemon = self.pokemons[userInp]
        return self.active_pokemon



#test
testT = Trainer("Sabri",3,["a","b","c"],"c")
testT.use_potion() ##**This one is causing problem**
testPo = Pokemon("Charmander", 50,"Fire",  ko = False)
#testT.use_potion()
testPo2 = Pokemon("mander", 50,"Water",False)
testPo.attack(testPo2,50)
#print(testPo2.get_health())```

Thanks for providing your full, formatted code! That makes things a lot easier.

So you had just a couple of things that needed changing to get your program up and running.

First, you are referencing Pokemon in your use_potion method.
image
This won’t work, because you have to tell your program exactly which pokemon to use the potion on.

The other problem is that you are not passing instances of your Pokemon class in to your Trainer — you are only passing in strings:
image
Once you fix those two things your code works fine. Just make sure you have created an instance of the Pokemon class before you pass it in to your trainer.

If you need a little help figuring out how to do these fixes, you can see how I did it here.

Thank you so much for putting in the time and solve the problem, It worked. Hope you have a nice day.