如何在嵌套的 if 语句中递增 for 循环
How to increment a foor loop within a nested if statement
我在 TicTacToe 中使用 MOD 来区分 2 个玩家,它用于递增的 for 循环。我在嵌套 if 语句中递增 foor 循环时遇到问题。
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
我希望能够在有人点击某个坐标时增加 for 循环 space。
因为您使用 for i in range(1, 10)
,所以在迭代期间对 i
进行的任何更改都无关紧要,当它返回到 for
循环时,它将被分配range
中的下一个值。示例:
for i in range(5):
print(i)
i += 2
你期望发生的事情是这样的
>>> 0
>>> 3
但实际上会发生的是:
>>> 0
>>> 1
>>> 2
>>> 3
>>> 4
这里发生了什么?首先,i
被赋值为 0
,然后它被打印并递增(所以现在它等于 2)。接下来,i
被赋予值 1
(来自 range
生成器),因此您所做的增量被覆盖。
要解决此问题,请使用 while
循环:
i = 1
while i < 10:
# do stuff
i += 1
现在 i
的值不会在每次 loop
执行时被覆盖
表达式 range(n)
只计算一次,而不是每次迭代都计算。您不能设置 n 并期望 range(n)
被更改。它不允许这样做。 Check it here for details
你必须为此使用 while 循环。
i=0
while i<=5:
#do your tasks
#do your increment
我在 TicTacToe 中使用 MOD 来区分 2 个玩家,它用于递增的 for 循环。我在嵌套 if 语句中递增 foor 循环时遇到问题。
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
我希望能够在有人点击某个坐标时增加 for 循环 space。
因为您使用 for i in range(1, 10)
,所以在迭代期间对 i
进行的任何更改都无关紧要,当它返回到 for
循环时,它将被分配range
中的下一个值。示例:
for i in range(5):
print(i)
i += 2
你期望发生的事情是这样的
>>> 0
>>> 3
但实际上会发生的是:
>>> 0
>>> 1
>>> 2
>>> 3
>>> 4
这里发生了什么?首先,i
被赋值为 0
,然后它被打印并递增(所以现在它等于 2)。接下来,i
被赋予值 1
(来自 range
生成器),因此您所做的增量被覆盖。
要解决此问题,请使用 while
循环:
i = 1
while i < 10:
# do stuff
i += 1
现在 i
的值不会在每次 loop
执行时被覆盖
表达式 range(n)
只计算一次,而不是每次迭代都计算。您不能设置 n 并期望 range(n)
被更改。它不允许这样做。 Check it here for details
你必须为此使用 while 循环。
i=0
while i<=5:
#do your tasks
#do your increment