← Python from Scratch
Lesson 36 of 78 · 5 XP
Warm-up: sum of a list warm-up
Theory and problem statement
Machine translation, not yet reviewed by a native speaker.
A function gets a list and gives back the answer with return. You can add up the items with a loop:
def total(nums):
s = 0
for n in nums:
s = s + n
return s
print(total([1, 2, 3])) # 6
Task. Write a function total(nums) that returns the sum of the numbers in the list. For an empty list — 0.
Sign up for free to solve this task right in your browser: code editor, running, test checks and hints.