繁体   English   中英

我如何只打印最终列表结果而不是以前的列表?

[英]How do i just print the final list result rather than the previous lists too?

如标题所示,

def multiples_of_3(x, y):
    y = y + 1
    storage_list = []
    for i in range(x, y):
        if i % 3 == 0:
            storage_list.append(i)
            print(storage_list)

multiples_of_3(3,9)

print(storage_list)移到forloop之外

In [3]: def multiples_of_3(x, y):
   ...:     y = y + 1
   ...:     storage_list = []
   ...:     for i in range(x, y):
   ...:         if i % 3 == 0:
   ...:             storage_list.append(i)
   ...:     print(storage_list)
   ...:
   ...:

In [4]: multiples_of_3(3, 9)
[3, 6, 9]

In [5]:

我以下是您想要的:

def multiples_of_3(x, y):
    y = y + 1
    storage_list = []
    for i in range(x, y):
        if i % 3 == 0:
            storage_list.append(i)
    print(storage_list)

multiples_of_3(3, 9)

您只需要将print语句带出到if和for循环之外,以便仅在两个语句都执行完后才打印。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM