擅长:python、mysql、java
<p>你的意图是集体地检查它们,但是相反,你是单独地检查每一个。你知道吗</p>
<p>试试这样的。这里有一个解决方案,我尽可能少地修改你的代码。你知道吗</p>
<pre><code>def checkfortie():
tie = True
for i in range(w):
for j in range(h):
if board[i][j] == 0:
tie = False
if tie:
print("It's a tie!")
</code></pre>
<p>更简单的方法可能是展平阵列:</p>
<pre><code>def checkfortie():
return all(x == 1 for row in board for x in row)
if (checkfortie()):
print("It's a tie!")
</code></pre>
<p>注意,在您的原始代码中,您将从w和h中减去1。这不是必需的,实际上是在产生问题。尝试运行以下命令:</p>
<pre><code>for i in range(w-1):
for j in range(h-1):
print(i, j)
</code></pre>
<p>你会看到它只打印0,0</p>
<pre><code>for i in range(w):
for j in range(h):
print(i, j)
</code></pre>
<p>将打印上述代码</p>
<p>0 0<br/>
0 1<br/>
10<br/>
1 1</p>