0

I am creating a battle and, from what I could tell, nearly everything is working. The attack deals proper damage and the player and enemies have the correct health, and all the items work. However, when you win, it repeats the text "you win" forever until you forcibly end the program. It also doesn't happen if the health is exactly 0. The code that I would believe to be the problem is...

def enemyHP():
    global enemyhealth
    global playerhealth
    if enemyhealth and playerhealth >0:
        global dmg
        global alcohol
        if alcohol==1:
            miss=random.randint(0,2)
            print("The alcohol makes you more furious...")
            if miss==0:
                dmg=0
            else:
                dmg=dmg*2
        enemyhealth=enemyhealth-dmg
        if enemyhealth<=0:
            print("you won")
            exit()
        else:
            print("you did",dmg,"damage")
            print("enemy health is:",enemyhealth)

I have tried finding a better way to end the code using exit(),quit(),and sys.exit(). None of them worked, and I was wondering if there was a way to fix this. Thanks in advance

From what I could simplify, the full code is this

backpacklist=["sword","hand"]    
enemyhealth=100
playerhealth=100
def enemyHP():
    global enemyhealth
    global playerhealth
    if enemyhealth and playerhealth >0:
        global dmg
        enemyhealth=enemyhealth-dmg
        if enemyhealth<=0:
            print("you won")
            exit
        else:
            print("you did",dmg,"damage,","enemy health is:",enemyhealth)
    return enemyhealth
def playerattack():
    global playerhealth
    global enemyhealth
    if playerhealth and enemyhealth >0:
        print(backpacklist)
        global dmg
        help1=0
        whatweapon=input("what weapon?:")
        while backpacklist[help1]!=whatweapon:
            help1=help1+1
        if backpacklist[help1]==whatweapon:
            if backpacklist[help1]=="hand": #to quickly kill
                dmg=1000
            elif backpacklist[help1]=="sword":
                dmg=20
    return dmg,playerhealth
def playerHP(enemydmg):
    global enemyhealth
    global playerhealth
    if playerhealth and enemyhealth >0:
        playerhealth=playerhealth-enemydmg
        if playerhealth<=0:
            print("you lose")
            exit
        else:
            print("your health is:",playerhealth)
    return playerhealth
def fight1(enemydmg):
    while enemyhealth and playerhealth>0:
        global dmg
        dmg=0
        playerattack()
        enemyHP()
        playerHP(enemydmg)
enemydmg=10    
if enemyhealth and playerhealth >0:
    fight1(enemydmg)

Ok, I've managed to solve it by setting the enemy's HP to 0 after it recorded the health being below 0. I don't know why that worked, but if worked.

SOD
  • 31
  • 4
  • I would check the value of dmg, I don’t see where you set it to a value other than 0. – Ethan Nov 28 '19 at 01:21
  • 1
    Do you mean "you won" is printing? And is that `exit` the built-in, or is that your own function? If it's a built-in, you must be restarting your program or something. "you won" won't be able to print over and over with just one call to this function. Show a [mcve]. – Carcigenicate Nov 28 '19 at 01:24
  • Are all those global variables necessary? It seems dangerous. There seems to be a need for some major refactoring. – AMC Nov 28 '19 at 03:05

2 Answers2

2

Firstly, I would recommend using sys.exit() over either exit() or quit(), have a look Here as to why that's generally a good idea. I don't have enough information here to say why sys.exit() in that position doesn't exit the program, but I would recommend having a look at where you call that function and changing that.

For example, presumably you are running this function in some form of loop where it is called each time a round occurs in combat, however rather than force quitting the program mid-function, it is sometimes better practice to just let the program continue when the preferred state is met, so for example, you could have the function return False if the enemy has health remaining, and return True if the enemy is dead, and have the loop end if the function has returned True, or even better just return enemyhealth and deal with figuring out whether you have won or lost outside of the function.

Fyrefly
  • 88
  • 7
2

Here is a cleaned up version of your code that works well without looping as you mentioned:

import random, sys

def enemyHP():
    global enemyhealth, playerhealth, dmg, alcohol
    if enemyhealth > 0 and playerhealth > 0:
        if alcohol == 1:
            miss = random.randint(0,2)
            print("The alcohol makes you more furious...")
            dmg = 0 if miss == 0 else dmg*2
        enemyhealth -= dmg
        if enemyhealth <= 0:
            print("you won")
            sys.exit()
        else:
            print("you did {} damage \nenemy health is: {}".format(dmg, enemyhealth))

# Your variables
enemyhealth = 1
playerhealth = 2
dmg = 1
alcohol = 1

# Call the function
enemyHP()

Sample output:

The alcohol makes you more furious...
you did 0 damage 
enemy health is: 1

This is based on the above variable assignments

lbragile
  • 7,549
  • 3
  • 27
  • 64
  • 1
    I am not entirely sure what the best approach is, but I think you should place the global variables as argument parameters to the function `enemyHP()` – lbragile Nov 28 '19 at 01:39
  • 1
    I tried to simplify all the code (I'm sure it could be simplified more), and here is what I got. – SOD Nov 28 '19 at 01:48
  • @SOD if my answer helped you please kindly consider up-voting and/or accepting (click on grey checkmark to turn it green) it. – lbragile Nov 30 '19 at 07:35