← Python from Scratch Lesson 32 of 78 · 5 XP

Warm-up: piggy bank warm-up

Theory and problem statement

Machine translation, not yet reviewed by a native speaker.

To add up many numbers, make a piggy-bank variable and add one number to it on each loop step:

total = 0
for i in range(1, 4):
    total = total + i   # 0+1, then +2, then +3
print(total)            # 6

Task. Read a number n and print the sum of all numbers from 1 to n.

Sign up for free to solve this task right in your browser: code editor, running, test checks and hints.