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.