Isaiah
Isaiah

Reputation: 31

Error when using classes and then calling on the class

I made a class as you can see here.

    class Player():
        def __init__(self, name, maxhealth, base_attack, gold, weapon, curweapon, healing_potions):
            player = Player('player', 100, 100, 5, 30, 'Normal Sword', 'Normal Sword', 0 )
            self.name = name
            self.maxhealth = maxhealth
            self.health = self.maxhealth
            self.base_attack = base_attack
            self.gold = gold
            self.weap = weapon
            self.curweapon = curweapon
            self.healing_potions = healing_potions

But then when I try and call on the healing_potions part like so

                    if question == '2':
                        player_in_diningroom = True
                        print("You enter the dining room")
                        print("")
                        print("You find a Potion Of healing on the table!")
                        print("")
                        healing_potions += 1
                        player_in_diningroom = False

Then it gives me this error

Traceback (most recent call last): File "c:/Users/Isaiah/Desktop/All of my programs/Role playing game.py", line 179, in healing_potions += 1 NameError: name 'healing_potions' is not defined PS C:\Users\Isaiah\Desktop\All of my programs>

Upvotes: 1

Views: 50

Answers (1)

Kent Kostelac
Kent Kostelac

Reputation: 2446

I don't quite understand why you initialize a player object inside your player class. This causes infinite recurison where you constantly create a player instance infinitely. You most likely need to create it outside of your class. I've added a method to the class so we can increase the health potions using a method belonging to the instance. This is generally the recommended practice.

#player class
class Player():
    def __init__(self, name, maxhealth, base_attack, gold, weapon, curweapon, healing_potions):
        self.name = name
        self.maxhealth = maxhealth
        self.health = self.maxhealth
        self.base_attack = base_attack
        self.gold = gold
        self.weap = weapon
        self.curweapon = curweapon
        self.healing_potions = healing_potions
    def increase_health_potions(self):
        self.healing_potions +=1

Then we initialize a player instance/object. I noticed you had one extra parameter in the instance you created, so I removed one to make it work

#create an instance called player
player = Player('player', 100, 100, 5, 'Normal Sword', 'Normal Sword', 0 )

question = '2'
if question == '2':
    player_in_diningroom = True
    print("You enter the dining room")
    print("")
    print("You find a Potion Of healing on the table!")
    print("")
    player.healing_potions += 1 #accesing and increasing variable belonging to instance
    player.increase_health_potions() #call method belonging to instance that increases the variable 
    player_in_diningroom = False

print(player.healing_potions)

Take a notice at

player.healing_potions += 1

You have to reference the player, that you want to increase the health potions at.

Upvotes: 1

Related Questions