Before writing anything, get clear on what is being asked.
Think of it like this
Imagine counting a jar of coins. You do not somehow know the total by looking — you hold a running count in your head, pick up one coin at a time, and add it on. The running count is your variable; picking up each coin is the loop.
This shape — start a total at zero, walk through things one at a time, add each one on — is called an **accumulator**, and you will use it constantly for the rest of the course.
Write a function that adds up every whole number from 1 up to and including n, then gives back the total.
If n is 5, that is 1 + 2 + 3 + 4 + 5, which comes to 15. If n is 0 there is nothing to add, so the answer is 0.
Example 1
Given n = 5
Answer 15
1 + 2 + 3 + 4 + 5 = 15.
Example 2
Given n = 1
Answer 1
Example 3
Given n = 0
Answer 0
There are no numbers to add, so the total stays at 0.
Write the function in the editor, then run it against the examples.
The editor on the right already has the function signature filled in for you. Write your answer inside it and press Run — that checks your code against the examples above only, so you can experiment freely.
Each one nudges you a little further. There is no reveal-the-answer button — working it out is the part that actually teaches you.
Submit against every test, then check you can explain why it works.
| round | i | total before | total after |
|---|---|---|---|
| 1 | 1 | 0 | 1 |
| 2 | 2 | 1 | 3 |
| 3 | 3 | 3 | 6 |
| 4 | 4 | 6 | 10 |
| 5 | 5 | 10 | 15 |
total = 0
for i in range(1, n + 1):
total += i
return totalDeclare the total OUTSIDE the loop. If you create it inside, it gets reset to 0 on every round and you will always get back just the last number.
There is also a formula — n × (n + 1) ÷ 2 — that gets the answer instantly without any loop. It is worth knowing, but write the loop first: recognising when a loop can be replaced by maths is a week-19 skill, and the loop is the idea you need today.
Run your code to see the test results.
⌘/Ctrl + Enter to run · ⌘/Ctrl + Shift + Enter to submit
Before writing anything, get clear on what is being asked.
Think of it like this
Imagine counting a jar of coins. You do not somehow know the total by looking — you hold a running count in your head, pick up one coin at a time, and add it on. The running count is your variable; picking up each coin is the loop.
This shape — start a total at zero, walk through things one at a time, add each one on — is called an **accumulator**, and you will use it constantly for the rest of the course.
Write a function that adds up every whole number from 1 up to and including n, then gives back the total.
If n is 5, that is 1 + 2 + 3 + 4 + 5, which comes to 15. If n is 0 there is nothing to add, so the answer is 0.
Example 1
Given n = 5
Answer 15
1 + 2 + 3 + 4 + 5 = 15.
Example 2
Given n = 1
Answer 1
Example 3
Given n = 0
Answer 0
There are no numbers to add, so the total stays at 0.
Write the function in the editor, then run it against the examples.
The editor on the right already has the function signature filled in for you. Write your answer inside it and press Run — that checks your code against the examples above only, so you can experiment freely.
Each one nudges you a little further. There is no reveal-the-answer button — working it out is the part that actually teaches you.
Submit against every test, then check you can explain why it works.
| round | i | total before | total after |
|---|---|---|---|
| 1 | 1 | 0 | 1 |
| 2 | 2 | 1 | 3 |
| 3 | 3 | 3 | 6 |
| 4 | 4 | 6 | 10 |
| 5 | 5 | 10 | 15 |
total = 0
for i in range(1, n + 1):
total += i
return totalDeclare the total OUTSIDE the loop. If you create it inside, it gets reset to 0 on every round and you will always get back just the last number.
There is also a formula — n × (n + 1) ÷ 2 — that gets the answer instantly without any loop. It is worth knowing, but write the loop first: recognising when a loop can be replaced by maths is a week-19 skill, and the loop is the idea you need today.