0

Im distinguishing 2 players in TicTacToe by utilising MOD, which is used upon an incrementing for loop. Im having trouble incrementing the foor loop within a nested if statement.

            for i in range (1,10):
                if i % 2==1:
                    #1st row
                    if click[0]<=250 and click[0]>=100 and click[1]<=250 and click[1]>=100:
                        current_game[0][0] = "X"
                        i+=1
                    elif click[0]<=550 and click[0]>=250 and click[1]<=250 and click[1]>=100:
                        current_game[0][1] = "X"
                        i += 1
                    elif click[0]<=700 and click[0]>=550 and click[1]<=250 and click[1]>=100:
                        current_game[0][2] = "X"
                        i += 1
                    #2nd row
                    elif click[0]<=250 and click[0]>=100 and click[1]<= 550and click[1]>=250:
                        current_game[1][0] = "X"
                        i += 1
                    elif click[0]<=550 and click[0]>=250 and click[1]<=550 and click[1]>=250:
                        current_game[1][1] = "X"
                        i += 1
                    elif click[0] <= 700 and click[0] >= 550 and click[1] <= 550 and click[1] >= 250:
                        current_game[1][2] = "X"
                        i += 1
                    #3rd row
                    elif click[0]<=250 and click[0]>=100 and click[1]<= 700and click[1]>=550:
                        current_game[2][0] = "X"
                        i += 1
                    elif click[0] <= 550 and click[0] >= 250 and click[1] <= 700 and click[1] >= 550:
                        current_game[2][1] = "X"
                        i += 1
                    elif click[0] <= 700 and click[0] >= 550 and click[1] <= 700 and click[1] >= 550:
                        current_game[2][2] = "X"
                        i += 1
                elif i % 2==0:
                    # 1st row
                    if click[0] <= 250 and click[0] >= 100 and click[1] <= 250 and click[1] >= 100:
                        current_game[0][0] = "o"
                        i += 1
                    elif click[0] <= 550 and click[0] >= 250 and click[1] <= 250 and click[1] >= 100:
                        current_game[0][1] = "o"
                        i += 1
                    elif click[0] <= 700 and click[0] >= 550 and click[1] <= 250 and click[1] >= 100:
                        current_game[0][2] = "o"
                        i += 1
                    # 2nd row
                    elif click[0] <= 250 and click[0] >= 100 and click[1] <= 550 and click[1] >= 250:
                        current_game[1][0] = "o"
                        i += 1
                    elif click[0] <= 550 and click[0] >= 250 and click[1] <= 550 and click[1] >= 250:
                        current_game[1][1] = "o"
                        i += 1
                    elif click[0] <= 700 and click[0] >= 550 and click[1] <= 550 and click[1] >= 250:
                        current_game[1][2] = "o"
                        i += 1
                    # 3rd row
                    elif click[0] <= 250 and click[0] >= 100 and click[1] <= 700 and click[1] >= 550:
                        current_game[2][0] = "o"
                        i += 1
                    elif click[0] <= 550 and click[0] >= 250 and click[1] <= 700 and click[1] >= 550:
                        current_game[2][1] = "o"
                        i += 1
                    elif click[0] <= 700 and click[0] >= 550 and click[1] <= 700 and click[1] >= 550:
                        current_game[2][2] = "o"
                        i += 1 

I want to be able to increment the for loop if someone clicks on a certain co-ordinate space.

1
  • Replace for i in range (1,10): with while i<10 and add i =1 before that. Commented May 30, 2020 at 8:27

2 Answers 2

2

Because you use for i in range(1, 10), it doesn't matter what changes you make to i during an iteration, when it comes back to the for loop it will be assigned the next value in the range. example:

for i in range(5):
    print(i)
    i += 2

What you expect to happen is something like

>>> 0
>>> 3

but what actually will happen is this:

>>> 0
>>> 1
>>> 2
>>> 3
>>> 4

What happened here? First,i is assigned the value 0, then it was printed and increment (so now it is equal to 2). Next, i is assigned the value 1 (from the range generator), so the increment you made was overridden.

To solve this use a while loop:

i = 1
while i < 10:
    # do stuff
    i += 1

Now the value of i will not be overridden every time the loop executes

Sign up to request clarification or add additional context in comments.

Comments

0

The expression range(n) is evaluated just once, not on every iteration. You can't set n and expect the range(n) be changed. It doesn't allow this. Check it here for details

You have to use while loop for that.

i=0
while i<=5:
    #do your tasks
    #do your increment

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.