小编Jor*_*lor的帖子

如果if语句至少满足一次,如何不执行for循环的else语句?

我试图检查列表中的所有元素,看看它们是否符合"小于5"的条件.我想要做的是如果我的列表中没有数字小于5,我想打印一条声明"此列表中没有小于5的元素",否则只打印那些数字,而不是"此列表中没有小于5的元素." 也.

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.")
Run Code Online (Sandbox Code Playgroud)

这会产生输出:

2
1
There are no elements in this list less than 5.
Run Code Online (Sandbox Code Playgroud)

如何摆脱输出的最后一行?

python python-3.x for-else

1
推荐指数
1
解决办法
68
查看次数

标签 统计

for-else ×1

python ×1

python-3.x ×1