可以使用循环对列表元素进行相加,例如:
def sum_list(lst):
total = 0
for num in lst:
total += num
return total
# 示例
my_list = [1, 2, 3, 4, 5]
result = sum_list(my_list)
print(result) # 输出 15
在上面的示例中,我们使用循环遍历列表中的每个元素,将其累加到一个变量 total
中,并最终返回该变量的值。这样就实现了列表元素的相加,而不使用 sum
方法。