Python fight game never ends -
Python fight game never ends -
i can't find problems in game, other fact won't end. can't see wrong it.
def fight(enemy, playerh, enemyh): #defining function fighting slay =true def testdeath(phealth, ehealth, enemy): #defining function test if player/enemy dead if phealth <= 0: slay = false print"you have lost fight, improve luck next time." elif ehealth <= 0: slay = false print"you have slain the",enemy,". win! congratulations!" else: slay = true #end of testdeath function print"you fighting a",enemy while slay: #while loop perform fighting playerd = randint(0,5) #making each characters harm using randint dragond = randint(0,7) trolld = randint(0,4) if enemy == "troll": enemyd = trolld elif enemy == "dragon": enemyd = dragond print"your health:",playerh print enemy,"'s health:",enemyh sleep(1) #sleep makes next message appear after (1) second(s) if playerd == 5 or playerd == 4: #player's attack print"you deal",playerd,"damage points. critical hit!" enemyh = enemyh - playerd #removing player harm enemy health testdeath(playerh, enemyh, enemy) if slay == false: break #sleep(1.5) #print"the",enemy,"has",enemyh,"health points remaining." sleep(2) elif playerd == 0: print"you attack has missed. deal 0 harm points." sleep(2) else: print"you deal",playerd,"damage points." enemyh = enemyh - playerd testdeath(playerh, enemyh, enemy) if slay == false: break #sleep(1.5) #print"the",enemy,"has",enemyh,"health points remaining." sleep(2) if enemy == "troll": if enemyd == 4: print"the troll deals 4 harm points. critical hit!" playerh = playerh - enemyd testdeath(playerh, enemyh, enemy) if slay == false: break #sleep(1.5) #print"you have",playerh,"health points remaining." sleep(2) elif enemyd == 0: print"the troll's attack missed. deals 0 harm points." sleep(2) else: print"the troll deals",enemyd,"damage points." playerh = playerh - enemyd testdeath(playerh, enemyh, enemy) if slay == false: break #sleep(1.5) #print"you have",playerh,"health points remaining." sleep(2) elif enemy == "dragon": if enemyd == 6 or enemyd == 7: print"the dragon deals",enemyd,"damage points. critical hit!" playerh = playerh - enemyd testdeath(playerh, enemyh, enemy) if slay == false: break #sleep(1.5) #print"you have",playerh,"health points remaining." sleep(2) elif enemyd == 0: print"the dragon's attack missed. deals 0 harm points." sleep(2) else: print"the dragon deals",enemyd,"damage points." playerh = playerh - enemyd testdeath(playerh, enemyh, enemy) if slay == false: break #sleep(1.5) #print"you have",playerh,"health points remaining." sleep(2)
i know that's lot of code don't think less enough. may be, can't see problem don't know part post here.
everything works fine until dies. when dies "congratulations win"
battle go on. can see problem?
your testdeath
function defines own local slay
. not connected value while
loop testing. need return
:
def testdeath(phealth, ehealth, enemy): #defining function test if player/enemy dead if phealth <= 0: slay = false print"you have lost fight, improve luck next time." elif ehealth <= 0: slay = false print"you have slain the",enemy,". win! congratulations!" else: slay = true homecoming slay
and assign it:
slay = testdeath(playerh, enemyh, enemy)
you rearrange function create neater:
def testdeath(phealth, ehealth, enemy): """test whether player or enemy dead.""" if phealth <= 0: print "you have lost fight, improve luck next time." homecoming false elif ehealth <= 0: print "you have slain {0}. win! congratulations!".format(enemy) homecoming false homecoming true
note utilize of str.format
, docstrings more logical structure.
python
Comments
Post a Comment