<p>我刚刚意识到这段代码并没有像我希望的那样工作。。。在</p>
<pre><code>for i in [0,2,4]:
if i%2==0:
print i
else:
print "There are no EVEN #s"
</code></pre>
<p>那么,编码这种结构的最佳方式是什么</p>
<p>这样做的目的是,如果“if”语句在“for”循环的所有迭代中都不为真,则运行“else”语句代码</p>
<p>只需在if语句中设置一个标志,以便在for循环运行完毕后进行检查。在</p>
<p>例如</p>
<pre><code>flag = False
for i in [0,2,4]:
if i%2==0:
print i
flag = True
if not flag:
print "There is an ODD #"
</code></pre>