While is this function still executing after?
Question: I made a simple choice game much like Rock, Paper, Scissors using Python. The
problem is that after you have won, and put in the winner's name, the while
loop still executes one more time. This is unacceptable! I've looked over it,
and looked over it again. With my knowledge, I can't seem to work out the
problem. This is a good learning moment.

Link to
[File](https://drive.google.com/file/d/0BwiB337z3HPrd043bUt0ekFhZjA/edit?usp=sharing)

    
    
    # Imports modules
    import random
    import math
    
    # Welcome message
    def main():
    
        print("//////////////////////////////////////")
        print("//////////////////////////////////////")
        print("// Welcome Ninja, Pirate, or Zombie //")
        print("//////////////////////////////////////")
        print("//////////////////////////////////////")
        print("\n\n")
    
        choose()
    
    # Prompts user to choose class
    def choose():
        choice = str(input("Make a choice! (Ninja, Pirate, or Zombie) "))
    
        choice = choice.lower()
    
        if choice == "ninja" or choice == "pirate" or choice == "zombie":
            enemy(choice)
        else:
            choose()
    
    # Randomly selects opposing enemy
    def enemy(choice):
    
        enemyRandom = random.randint(1,3)
    
        if enemyRandom == 1:
            enemy = "ninja"
        elif enemyRandom == 2:
            enemy = "pirate"
        elif enemyRandom == 3:
            enemy = "zombie"
        else:
            print("Something went wrong!")
    
        hit_factor(choice, enemy)
    
    # Determines the hit factor. Certain class are weak or strong when fighting certain
    # other classes
    def hit_factor(choice, enemy):
    
        if choice == "ninja" and enemy == "ninja":
            hitFactor = 1
        elif choice == "ninja" and enemy == "pirate":
            hitFactor = 1.2
        elif choice == "ninja" and enemy == "zombie":
            hitFactor = 0.8
        elif choice == "pirate" and enemy == "ninja":
            hitFactor = 0.8
        elif choice == "pirate" and  enemy == "pirate":
            hitFactor = 1
        elif choice == "pirate" and enemy == "zombie":
            hitFactor = 1.2
        elif choice == "zombie" and enemy == "ninja":
            hitFactor = 1.2
        elif choice == "zombie" and enemy == "pirate":
            hitFactor = 0.8
        elif choice == "zombie" and enemy == "zombie":
            hitFactor = 1
        else:
            print("Something went horribly wrong.")
    
        enemy_hit_factor(choice, enemy, hitFactor)
    
    # Determines the enemy's hit factor
    def enemy_hit_factor(choice, enemy, hitFactor):
    
        if enemy == "ninja" and choice == "ninja":
            enemyHitFactor = 1
        elif enemy == "ninja" and choice == "pirate":
            enemyHitFactor = 1.2
        elif enemy == "ninja" and choice == "zombie":
            enemyHitFactor = 0.8
        elif enemy == "pirate" and choice == "ninja":
            enemyHitFactor = 0.8
        elif enemy == "pirate" and choice == "pirate":
            enemyHitFactor = 1
        elif enemy == "pirate" and choice == "zombie":
            enemyHitFactor = 1.2
        elif enemy == "zombie" and choice == "ninja":
            enemyHitFactor = 1.2
        elif enemy == "zombie" and choice == "pirate":
            enemyHitFactor = 0.8
        elif enemy == "zombie" and choice == "zombie":
            enemyHitFactor = 1
        else:
            print("Something went horribly wrong.")
    
        combat(choice, enemy, hitFactor, enemyHitFactor)
    
    # Initiates combat
    def combat(choice, enemy, hitFactor, enemyHitFactor):
    
        yourHP = 1000
        enemyHP = 1000
    
        print("Your HP: ", yourHP)
        print("Enemy's HP: ", enemyHP)
    
        over = False
    
        while over != True:
            isHitCalc = random.randint(1,10)
            if isHitCalc > 3:
                isHit = True
            else:
                isHit = False
                print("You missed!")
    
            if isHit == True:
                randomHIT = random.randint(1,100)
                randomHitDamage = math.ceil(randomHIT * hitFactor)
                enemyHP -= randomHitDamage
    
                if enemyHP < 0:
                    enemyHP = 0
    
                print("You hit the enemy for ", randomHitDamage," damage.",sep='')
                print("Enemy's HP: ", enemyHP)
    
                if enemyHP == 0:
                    file = open("wonMessage.txt", "r")
                    content = file.read()
                    print(content)
                    over = True
                    winner()
    
            isHitCalc2 = random.randint(1,10)
            if isHitCalc2 > 3:
                isHitMe = True
            else:
                isHitMe = False
                print("Your enemy missed!")
    
            if isHitMe == True:
                randomHitMe = random.randint(1,100)
                randomHitDamageMe = math.ceil(randomHitMe * enemyHitFactor)
                yourHP -= randomHitDamageMe
    
                if yourHP < 0:
                    yourHP = 0
    
                print("The enemy hit you for ", randomHitDamageMe, " damage.", sep='')
                print("Your HP: ", yourHP)
    
                if yourHP == 0:
                    file = open("lostMessage.txt", "r")
                    content = file.read()
                    print(content)
                    over = True
    
    # Writes winner's name to text file           
    def winner():
        winner = str(input("Please enter your name: "))
        infile = open("winner.txt", "w")
        infile.write("Latest winnner's name: ")
        infile.write(winner)
    
    # Calls main
    main()
    


Answer: It is probably because after you call `winner()` in

    
    
     if enemyHP == 0:
          file = open("wonMessage.txt", "r")
          content = file.read()
          print(content)
          over = True
          winner()
    

you don't `break` out of the loop immediately.

Instead, you still proceed to process the logic below before doing the `while`
loop check (`over != True`) :

    
    
        isHitCalc2 = random.randint(1,10)
        if isHitCalc2 > 3:
            isHitMe = True
        else:
            isHitMe = False
            print("Your enemy missed!")
    
        if isHitMe == True:
            randomHitMe = random.randint(1,100)
            randomHitDamageMe = math.ceil(randomHitMe * enemyHitFactor)
            yourHP -= randomHitDamageMe
    
            if yourHP < 0:
                yourHP = 0
    
            print("The enemy hit you for ", randomHitDamageMe, " damage.", sep='')
            print("Your HP: ", yourHP)
    
            if yourHP == 0:
                file = open("lostMessage.txt", "r")
                content = file.read()
                print(content)
                over = True
    

You can fix this specific case by adding:

    
    
    if enemyHP == 0:
          file = open("wonMessage.txt", "r")
          content = file.read()
          print(content)
          over = True
          winner()
          break
    

