python - 如何在嵌套的 if 语句中增加一个 for 循环
问题描述
我通过使用 MOD 来区分井字游戏中的 2 个玩家,该 MOD 在递增的 for 循环中使用。我无法在嵌套的 if 语句中增加 for 循环。
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 循环。
解决方案
因为您使用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
分配 value 0
,然后打印并递增(所以现在它等于 2)。接下来,i
分配值1
(来自range
生成器),因此您所做的增量被覆盖。
要解决此问题,请使用while
循环:
i = 1
while i < 10:
# do stuff
i += 1
现在每次执行i
时都不会覆盖的值loop
推荐阅读
- hbase - 如何使用 RowCounter 计算 Hbase 表中的行数
- python - 在 Spyder 之类的同一调试会话中运行任意行?
- visual-studio-2017 - 找不到类型 [Microsoft.TeamFoundation.Client.TeamFoundationServerFactory]
- linux - Linux UART 比指定的波特率慢
- android - 在片段中设置图像变量
- ms-office - Office 加载项:有什么方法可以让一个始终运行?
- asp.net-boilerplate - 我想从 asp 样板框架中的表中硬删除
- r - 根据 R 中的索引将数据帧拆分为多个部分
- python - 如何在 Scrapy 中无错误地下载图像?
- node.js - 如何使用 Mongoose 在 MongoDB 中插入 JSON 数组