Py学习  »  Python

Python:仅当总和不为零时打印

Jwencar • 3 年前 • 1242 次点击  

如果mult_list或non_mult_list之和等于零,我不希望它像下面那样打印0,它不需要打印任何东西。我应该添加什么来限制这种情况?

这是我的密码

n , m = map(int, input().split()) 
num_list = list(map(int,input().split()))

mult_list=[]
non_mult_list=[]
if m >= 2 and m <= 9:
    for i in (num_list)[:n]:
        if i%m == 0:
            mult_list.append(i)
        
    for j in (num_list)[:n]:
        if j%m != 0:
            non_mult_list.append(j)

print (sum(mult_list))
print (sum(non_mult_list))

以下是我的意见:

2 9

5 7

然后它的输出:

0

12

Python社区是高质量的Python/Django开发社区
本文地址:http://www.python88.com/topic/132068
 
1242 次点击  
文章 [ 2 ]  |  最新文章 3 年前