I am trying to make a system for my game in which the player's enemy has a decreasing amount of health. My shell output is here:
Cannon Fired!
Enemy Health= 8
Cannon 1 ammo = 10
The enemy fired back!
Your health is now 12
Fire your choice of cannon! type 'fire cannon (1 or 2)

fire cannon 1
Cannon Fired!
Enemy Health= 7
Cannon 1 ammo = 10
The enemy fired back!
Your health is now 13
Fire your choice of cannon! type 'fire cannon (1 or 2)
fire cannon 1
Cannon Fired!
Enemy Health= 9
Cannon 1 ammo = 10
The enemy fired back!
Your health is now 12
Fire your choice of cannon! type 'fire cannon (1 or 2)

My code is here:

import random

global health
global attack
global shealth
global sattack
health=100
attack=15

shealth=250
sattack=100


def tcombat():
    c1am=10
    c2am=10
    enam="Qasi"
    ehealth=15
    edam=random.choice([5,6,7,8,9])
    thealth=20
    tdam=random.choice([6,7,8,9,10])
    while ehealth > 0:
        c1am=10
        c2am=10
        enam="Qasi"
        ehealth=15
        edam=random.choice([5,6,7,8,9])
        thealth=20
        tdam=random.choice([6,7,8,9,10])
        print "Fire your choice of cannon! type 'fire cannon (1 or 2)"
        t2=raw_input(">>")
        if t2.lower()=="fire cannon 1":
            c1am - 1
            ehealth=ehealth - tdam
            print "Cannon Fired!"
            print "Enemy Health=",ehealth
            print "Cannon 1 ammo =",c1am
        elif t2.lower()=="fire cannon 2":
            c2am - 1
            ehealth=ehealth - tdam
            print "Cannon Fired!"
            print "Enemy Health=", ehealth
            print "Cannon 2 ammo =",c2am
        print "The enemy fired back!"
        thealth=thealth - random.choice([6,7,8,9])
        print "Your health is now",thealth
    print "Good Job!"
    print "You beat the training dummy."
    print "Nothing like real combat"
    print "But screw you, here you go!"
    print "(Into real combat)"
    lvl3()

def lvl2_2():
    print "Squad Nine"
    print "This team looks very....umm..Dumb."
    print "There is one guy sitting on a stool"
    print "he has a star sticker on his chest."
    print "The other guy is vomiting on his"
    print "pants."
    print "BEGIN TRAINING"
    print "TRAINING: When you are roaming around the skies, you type 'Engage [Ship Name]'"
    print "TRAINING: While in combat, type 'Fire [Cannon Number (not spelt)]' to fire a cannon at a ship"
    print "All entries must be in lower caps!"
    print "TRAINING: There may be consequences for firing upon certain ships."
    print "--BEGIN TRAINING--"
    print "There is a ship near yours, the Qasi. It is flying"
    print "the enemy flag."
    print "There are 2 cannons on your ship."
    c1am=10
    c2am=10
    enam="Qasi"
    ehealth=15
    edam=random.choice([5,6,7,8,9])
    thealth=20
    tdam=random.choice([6,7,8,9,10])
    enemyalive=True
    if ehealth==0:
        enemyalive=False
    t1=raw_input(">>")
    if t1.lower()=="engage qasi":
        print "Engaged in Combat"
        tcombat()



def lvl2_1():
    print "Squad One"
    print "This team looks much more able than Squad Nine."
    print "TYRONE:Hi! I'm Tyrone, he's James, she's Ashley, and that guy over there,"
    print "he's Bob."
    print "BEGIN TRAINING"
    print "TRAINING: When you are roaming around the skies, you type 'Engage [Ship Name]'"
    print "TRAINING: While in combat, type 'Fire [Cannon Number (not spelt)]' to fire a cannon at a ship"
    print "TRAINING: There may be consequences for firing upon certain ships."
    print "--BEGIN TRAINING--"
    print "There is a ship near yours, the Qasi. It is flying"
    print "the enemy flag."
    print "There are 2 cannons on your ship."
    c1am=10
    c2am=10
    enam="Qasi"
    ehealth=15
    edam=random.choice([5,6,7,8,9])
    thealth=20
    tdam=random.choice([6,7,8,9,10])
    enemyalive=True
    if ehealth==0:
        enemyalive=False
    t1=raw_input(">>")
    if t1.lower()=="engage qasi":
        print "Engaged in Combat"
        tcombat()

You set ehealth to 15 on line 26, which is inside the loop. Then you subtract a random amount of damage between 5 and 9. So ehealth is now somewehere between 6 and 10. That's still above zero, so the loop repeats, jumping back to line 23. So line 26 will execute again, and ehealth becomes 15 again. So the loop will continue forever without ehealth ever becoming zero (or less) because you always set it back to 15 before subtracting damage.

To avoid this problem, you should set ehealth to 15 once before the loop and then never again. Same thing with thealth.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.