<p>我试图检查列表中的所有元素,看看它们是否满足条件“小于5”。我要做的是,如果我的列表中没有小于5的数字,我想打印一个语句“这个列表中没有小于5的元素”,否则只打印那些小于5的数字,而不是“这个列表中没有小于5的元素”。你知道吗</p>
<pre><code>list = [100, 2, 1, 3000]
for x in list:
if int(x) < 5:
print(x)
else:
print("There are no elements in this list less than 5.")
</code></pre>
<p>这将产生输出:</p>
<pre><code>2
1
There are no elements in this list less than 5.
</code></pre>
<p>我怎样才能去掉输出的最后一行呢?你知道吗</p>
<p>你可以这样做:</p>
<pre><code>if max(mylist) < 5:
print('there are no elements in this list greater than 5')
else:
for x in mylist:
if int(x) < 5:
print(x)
</code></pre>
<p>这将检查列表中是否包含任何大于5的内容,如果有,则运行循环。你知道吗</p>