Im trying to make a code so when the player attacks the enemy, the enemies health goes down and doesnt reset

Im new to coding and was struggling to make it so that when the player attacks the enemy, it updates the enemies health and the player is able to attack again until the health of the enemy is 0.

Ive come up with this but everytime I make a second attack the goblins health goes back to 100 and goes from there

for example if i do a "basic attack" it does 100-20 = 80, but when i attack again, lets say another "basic attack" it displays 80 again instead of 60.

def combat_enemy_goblin():

small_goblin_health = 100
attack_basic = 20
attack_special = 50
attack_ultimate = 100

print("You are now in combat with a small goblin!")
print("")
print("")
print("Small Goblin: ", small_goblin_health, "Health Points")

while True:
    try:
        users_attack = int(input("""
        Your Moves
        ----------------------------
        1 - Basic attack [20]
        2 - Special attack [50]
        3 - Ultimate attack [100]
        4 - Run [repeating action]
        ----------------------------

        What do you choose? """))
        if  users_attack == 1:
            print("")
            print("You use your basic attack")
            print("")
            print("The goblin has taken some damage")
            print("")
            print("Small Goblin: ", small_goblin_health - 20, "Health points")
            print("")

        else:
            if users_attack == 2:
                print("")
                print("You use your basic attack")
                print("")
                print("The goblin has taken some damage")
                print("")
                print("Small Goblin: ", small_goblin_health - 50, "Health points")
                print("")

            else:
                if users_attack == 3:
                    print("")
                    print("You use your basic attack")
                    print("")
                    print("The goblin has taken some damage")
                    print("")
                    print("Small Goblin: ", small_goblin_health - 100, "Health points")
                    print("")   
                
                else:
                    if users_attack == 4:
                        print("")
                        print(" You tried to run away but failed!")      

    except:
        print("")
        print("You cant do that!")
        print("")

combat_enemy_goblin()

>Solution :

Your problem in code is you are only subtracting value from goblin health but not changing it.

I have written a comment where I change the code.

-= in code means that you are removing the value from a variable and assigning it to the result.

num = 10
print(num) # Output: 10
num-=1
print(num) # Output: 9

One more change in your code is you do not exit the program even if the goblin health is 0 or less I am adding this in the below code.

small_goblin_health = 100
attack_basic = 20
attack_special = 50
attack_ultimate = 100

print("You are now in combat with a small goblin!")
print("")
print("")
print("Small Goblin: ", small_goblin_health, "Health Points")

while True:
    if small_goblin_health<=0: # edited
        break # exit the loop if small_goblin_health is less than or equal to 0.
    try:
        users_attack = int(input("""
        Your Moves
        ----------------------------
        1 - Basic attack [20]
        2 - Special attack [50]
        3 - Ultimate attack [100]
        4 - Run [repeating action]
        ----------------------------

        What do you choose? """))
        if  users_attack == 1:
            small_goblin_health-=20 #edited
            print("")
            print("You use your basic attack")
            print("")
            print("The goblin has taken some damage")
            print("")
            print("Small Goblin: ", small_goblin_health, "Health points")
            print("")

        elif users_attack == 2:
            small_goblin_health-=50 #edited

            print("")
            print("You use your basic attack")
            print("")
            print("The goblin has taken some damage")
            print("")
            print("Small Goblin: ", small_goblin_health, "Health points")
            print("")

        elif users_attack == 3:
            small_goblin_health-=100 #edited
            print("")
            print("You use your basic attack")
            print("")
            print("The goblin has taken some damage")
            print("")
            print("Small Goblin: ", small_goblin_health, "Health points")
            print("")   
        elif users_attack == 4:
                        print("")
                        print(" You tried to run away but failed!")      

    except ValueError: # edited
        print("")
        print("You cant do that!")
        print("")

Leave a Reply