0

I can't figure out how to add two potions after I beat the enemy in the fight. Driving me nuts, I know it's probably something simple. Any help is appreciated. What I am trying to do is have the fight begin, if you win be able to press 1 to loot the enemy and add two potions to your inventory.

This is the error:

Traceback (most recent call last):
  File "C:/Users/kensai/Desktop/lab3/project/fight.py", line 137, in <module>
    firstfight()
  File "C:/Users/kensai/Desktop/lab3/project/fight.py", line 72, in firstfight
    fight(soldier, player1, name1)
  File "C:/Users/kensai/Desktop/lab3/project/fight.py", line 100, in fight
    loot(soldier, player1, name1)
  File "C:/Users/kensai/Desktop/lab3/project/fight.py", line 59, in loot
    player1.__plusPotion(number1)
AttributeError: 'user' object has no attribute '__plusPotion'

Here is the code:

import sys
import random
import time

time.sleep(.002)

number = random.randint(0,20)
number1 = 2

name1 = input("What would you like your player to be named?")

class user:

    
    def __init__ (self, player_name, player_health, health_potion):

        self.__player_health = player_health
        self.__player_name = player_name
        self.__health_potion = health_potion

    def getName(self):
        return self.__player_name

    def getHealth(self):
        return self.__player_health

    def getPotion(self):
        return self.__health_potion

    def setHealth(self):
        self.__player_health = 100
        return self.player_health

    def minusHealth(self, number):
        self.__player_health -= number
        return self.__player_health
        
    def minusPotion(self):
        self.__health_potion -= 1
        return self.__health_potion

    def plusPotion(self, number1):
        self.__health_potion += number1
        return self.__health_potion

    def plusHealth(self):
        self.__player_health += 20
        return self.__player_health

def loot(soldier, player1, name1):
    
    player1 = user(name1, player1.getHealth(), player1.getPotion())
    soldier = user('Soldier', soldier.getHealth(), soldier.getPotion())
    booty1 = input("Type ' 1 ' to loot the enemy")
    if booty1 == "1":
        print("You find two first-aid kits")
        player1.__plusPotion(number1)

        
def firstfight():
    
    time.sleep(1)
                     
    player1 = user(name1, 100, 0)
    soldier = user('Soldier', 80, 0)

    print("Your characters stats are: Health:", player1.getHealth())
    time.sleep(2)
                      
    fight(soldier, player1, name1)

    

def fight(soldier, player1, name1):
    import random
    import sys
    
    player1 = user(name1, player1.getHealth(), player1.getPotion())
    soldier = user('Soldier', soldier.getHealth(), soldier.getPotion())
        
    p = 0
    while p == 0:
        
        attack = input("1 = Attack with weapon, 2 = use potion")
                         
        if attack == "1":
            number = random.randint(5,20)             
            time.sleep(1)
            print("You strike at your enemy. Soldier's health now is", soldier.minusHealth(number))
            time.sleep(.05)
            number = random.randint(5,20)
            print("Soldier hits you back and your health is now at", player1.minusHealth(number))
            time.sleep(.003)

            if soldier.getHealth() <= 0:

                print("You have won the fight!")
                loot(soldier, player1, name1)
                p = 2
                
            elif player1.getHealth() <= 0:
                print("You died.")
                time.sleep(2)
                print("\n\n\n\n\n\n\n\n\n\n")
                print("Thanks for playing")
                time.sleep(5)
                print("\n\n\n\n\n\n\n\n\n\n")
                sys.exit(0)
                         
        elif attack == "2":
                         
            time.sleep(1)
            
            if player1.getPotion() == 0:
                print("You don't have any first-aid kits")
            else:
                print("You use a first aid-kit!")
                
                player1.plusHealth(), player1.minusPotion()

                print("Your characters stats are: Health:", player1.getHealth(), "Health Potion:", player1.getPotion())

                if player1.getHealth() > 100:
                         
                    player1.setHealth()
                    print("Soldier hits you back and your health is now at", player.minusHealth(number))
                         
        
        else:
            fight(soldier, player1, name1)




firstfight()
kensai01
  • 65
  • 1
  • 3
  • 9

1 Answers1

0

Look at the line that produces the error :

player1.__plusPotion(number1)

But the function you're trying to call is not called __plusPotion, it's called plusPotion, without the double underscore.

If you're unclear on the meaning of double undersocres in python, see :

What is the meaning of a single- and a double-underscore before an object name?

Community
  • 1
  • 1
Raveline
  • 2,660
  • 1
  • 24
  • 28