我需要forLoop1使用y
如果z
等于1,则其他人使用x
,而forLoop2则相同但反转
我的代码:
for z in range(3):
count=[0]*plys
for y in range(len(game)): #forLoop1
for x in range(len(game[y])): #forLoop2
for i in range(plys):
if game[y][x] == i+1:
count[i] += 1
for i in range(plys):
if count[i] >= 3:
print("Player " + str(i+1) + " is the winner")
count=[0]*plys
我试过这样的事情:
for y if z == 0 else x in range(len(game)):
和
for (y if z == 0 else x) in range(len(game)):
但那不起作用
非常感谢任何帮助 对不起,如果我不好解释它
答案 0 :(得分:1)
... if ... else ...
条件表达式生成一个表达式,你不能用它来处理像这样的for
循环索引变量名。
但你可以这样做:
for z in range(3):
count=[0]*plys
for k1 in range(len(game)): #forLoop1
for k2 in range(len(game)): #forLoop2
y, x = (k1, k2) if z == 1 else (k2, k1)
for i in range(plys):
if game[y][x] == i+1:
count[i] += 1
for i in range(plys):
if count[i] >= 3:
print("Player " + str(i+1) + " is the winner")
count=[0]*plys
但是,如果您只使用完整的if... else
块,可能会更清楚:
if z == 1:
y, x = k1, k2
else:
y, x = k2, k1