Using two counters in a loop Using two counters in a loop tkinter tkinter

Using two counters in a loop


You want to continue fighting (looping) while either condition (points <= 3) is True. You need to add another condition to the while loop.

You only want to return from the function after the fighting is over so remove all of those returns and add one that is outside of the loop after the fighting is over. Also, you only want to create the messages after the fighting is over.

def Fight():    player_wins = 0    alien_wins = 0    while player_wins <= 3 and alien_wins <= 3:        player_roll = random.randint(0, 10)        alien_roll = random.randint(0, 7)        if player_roll > alien_roll:            contents.set("You manage to fire a shot of your laser pistol and the alien backs off.")            player_wins += 1        elif alien_roll > player_roll:            contents.set("The alien reaches out and strikes you with its claws.")            alien_wins += 1        elif alien_roll == player_roll:            contents.set("You both grapple eachother and eventually back off.")            if player_wins == 3:        contents.set("You manage to overcome the alien. It leaps from wall to wall and crawls into the vents.")        win = True    elif alien_wins == 3:        contents.set("You lose the fight to the alien. Game Over!")    return


I cleaned up a few things. Your main problem of looping was fixed by making the loop dependent on both player_wins and alien_wins being <= 3. This way, whenever one of them hits 3, the loop terminates, and control goes to the following if/else statement which will then set the winner.

def Fight():        player_wins = 0    alien_wins = 0    while player_wins <= 3 and alien_wins <= 3:            player_roll = random.randint(0, 10)        alien_roll = random.randint(0, 7)        if player_roll > alien_roll:            contents.set("You manage to fire a shot of your laser pistol and the alien backs off.")            player_wins += 1        elif alien_roll > player_roll:            contents.set("The alien reaches out and strikes you with its claws.")            alien_wins += 1        else:            contents.set("You both grapple eachother and eventually back off.")         if player_wins == 3:        contents.set("You manage to overcome the alien. It leaps from wall to wall and crawls into the vents.")        win = True    else:        contents.set("You lose the fight to the alien. Game Over!")

You also didn't need those break statements, as that was breaking out of the loop entirely. You probably were thinking of continue, which will just go to the next iteration of the loop, however even that isn't needed since the entire contents of the loop is just an if/else statement, so only one branch will execute.

Also, the elif where you checked for the two rolls being equal was unnecessary, since you already ruled out that neither is greater/less than the other, so it can be assumed they're equal. Same goes for the final if/else where you check for who has 3. If we are out of the loop, we know somebody has 3, so we only need to actually check a single players score.