我在下面定义了一个函数,它打印列表中的每个整数,并且它工作得很好。我想做的是创建第二个函数,该函数将调用或重新利用 int_list()
函数来显示已生成的列表的总和。
我不确定这是否是由代码本身固有地执行的——我对 Python 语法相当陌生。
integer_list = [5, 10, 15, 20, 25, 30, 35, 40, 45]
def int_list(self):
for n in integer_list
index = 0
index += n
print index
原文由 webbm 发布,翻译遵循 CC BY-SA 4.0 许可协议
在你的代码中,你在每个循环中设置
index=0
,所以它应该在for
循环之前初始化:输出: