擅长:python、mysql、java
<p>关于守则:</p>
<pre><code>for row in csv_reader:
if row[0] == idNum:
print("matching barcode found")
return True
else:
print("barcode not on file. Adding...")
return False
</code></pre>
<p>如果您允许,这个<code>for</code>循环确实会处理每一行,但是您不允许它,因为<code>if</code>语句的真部分和假部分在读取第一行后返回,实际上忽略了所有其他部分</p>
<hr/>
<p>您可能需要的是这个方案:如果您在第一行中没有找到它,不要立即返回false-您需要检查所有<em>其他</em>行,并且只有在<em>没有</em>行时才返回false</p>
<p>换句话说,类似这样的事情:</p>
<pre><code># Check ALL rows.
for row in csv_reader:
# If found in THIS row, notify success.
if row[0] == idNum:
print("matching barcode found")
return True
# If found in NO rows, notify failure.
print("barcode not on file. Adding...")
return False
</code></pre>