首页 > 解决方案 > 我如何只打印最终列表结果而不是之前的列表?

问题描述

正如标题所暗示的,

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)

标签: pythonlistprinting

解决方案


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]:

推荐阅读