Android & iPhone apps are coming soon. Log in for a plan and reminders made for you →

Coding basics · 5 of 20

Add up 1 to n

Return 1 + 2 + … + n. For n = 0 the sum is 0.

What you need

A for loop repeats its indented lines once for each value. range(a, b) counts from a up to b - 1, so range(1, n + 1) is 1 to n. += adds to a variable.

for i in range(1, 4):
    print(i)   # 1, then 2, then 3

Examples

Input n = 5

Output 15

1 + 2 + 3 + 4 + 5

Input n = 0

Output 0

Your code · Python

Stuck?