0

I'm currently making a TicTacToe game to improve on my Python skills. I have everything done, but my enemy code won't work, since it will either not do anything, place it's mark on my mark, or place multiple marks. I'm pretty sure it's because of my loop, since the whole point of it was to fix the issues. But when I set the loop condition to False in my loop, it keeps on going (I know this because I have print()'s to help me debug)

Heres the code:

while run_loop == True:
    for tile in tiles:
        print(enemy_rng())
        if enemy_rng() == tile["num"]:
            if tile["enemy"] is True: # If matching
                enemy_rng()
                print("Matched")
            else: # If successful
                print("Successful")
                run_loop = False
        else: # If the rng doesn't match the number
            print("Nothing")
            run_loop = False
Kenji Chin
  • 41
  • 3
  • 1
    "But when I set the loop condition to False in my loop, it keeps on going" Are you under the impression that the loop should exit **immediately** when you make this change? It does not (in Python, nor any similar language I've ever heard of). It exits only *when the condition is checked*, at the top of the loop. – Karl Knechtel Nov 27 '21 at 17:18
  • If you want it to immediately exit the loop over titles, the command you are looking for is `break` ([docs](https://docs.python.org/2.0/ref/break.html) and [examples](https://docs.python.org/3/tutorial/controlflow.html#break-and-continue-statements-and-else-clauses-on-loops)) – Tyler V Nov 27 '21 at 17:28

0 Answers0