python - 我如何只打印最终列表结果而不是之前的列表?
问题描述
正如标题所暗示的,
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]:
推荐阅读
- c++ - 递归模板函数不能分配给具有 const 限定类型“const tt &”的变量“状态”
- html - 如何使用 css 制作平坦而不实心的边框?
- react-native - 反应原生谷歌地图
- php - yii\base\InvalidRouteException: 无法解析 yii2 中的请求
- c# - 消息弹出窗口
- webpack - 无法在 Chrome 源选项卡中加载 webpack 源文件的内容
- javascript - 有 javascript 循环引用问题
- c# - 如何检测玩家是否在向左/向右/向后/向前移动
- python - 重写几个矩阵运算以避免内存错误?
- javascript - 根据 JSON 键更改搜索值的字体颜色