04.01 · Loop challenges / Practice · 18 min
Reach the same even sum in two ways
One loop visits every number and filters. The other visits only even numbers. Both should reach the same total.
Activity 1 of 2 in Loop challenges
Python 2: Logic, Loops & Functions / 04.01
Sum even numbers two ways
Not started
Sum the even numbers from 1 through 100 using a remainder test and a step of two.
Your lesson progress
Empty: not started. Yellow: in progress. Green: complete. Marking the whole activity complete does not replace your test results. These circles save in this browser for this pilot.

Your task: build two independent totals
This is the original Sum the Even Numbers task, paired with its text-based solution. Keep limit assigned to 100, total1 initialized to 0 and total2 initialized to 0. Include 100 in both methods. Use the two existing print calls in their current order.
For method 1, visit every whole number from 1 through limit. Add the number to total1 only when its remainder after division by 2 is zero. For method 2, visit even numbers only, starting at 2 and stepping by 2. Add each visited number to total2. Do not obtain one total by copying the other.
Predict a smaller version before writing the loops
The comic uses limit 6. The first method visits 1, 2, 3, 4, 5 and 6, but adds only 2, 4 and 6. The second method visits only 2, 4 and 6. Both totals are 12. This makes the difference in visits concrete.
The % operator asks for a remainder. An even whole number has remainder 0 when divided by 2. Initialize each total before its loop; resetting it inside would discard earlier additions.
print(10 % 2) # 0: even
print(9 % 2) # 1: oddCheck both results, then test a smaller limit
The checks inspect limit, both stored totals and both output lines for the starting value 100. They do not prove which algorithm you used or every other limit. After passing, try limit 6: both totals should be 12. Try 5: both should be 6. Try 1: both should be 0. Restore 100 before rerunning the fixed-input checks.
After passing or three unsuccessful checks, open the explained example solution. Its walkthrough first compares the two methods on six numbers, then shows the full task. Each step leaves your practice code alone unless you explicitly choose to load that example.
Build it one step at a time
Prefer learning by typing? Work through short instructions, run the Python code and check each change before continuing. Your guided project saves separately from the challenge below.
The mini-lesson opens in a new tab. Your own code carries forward; each step has a focused check and an optional hint.
Start mini-lesson(opens in a new tab)5 steps · Saved work · Instant feedback
YOUR TURN
Sum even values with two different loops
Keep limit = 100 and initialize total1 and total2 to 0. For method 1, loop over every whole number from 1 through limit and use % to add only evens to total1. For method 2, loop over even numbers using a step of 2 and add them to total2. Keep the two supplied print calls in order.
Total using modulus check: 2550 Total using step in loop: 2550
Keep the starting inputs. Print these lines in this order, without extra output.
- Keep limit at 100
- Store 2550 in total1
- Store 2550 in total2
- Print the modulus result on line 1
- Print the stepped result on line 2
Edit Python, then run it. Check challenge checks your output and the listed Python requirements. Your code saves on this browser after a short pause. Sign in and choose Save account draft to keep it across devices. Word wrap is enabled.
Ready to checkRun the checks to see your results here.
Your check results
Need a nudge?
Include the upper limit
range excludes its stop. Choose a stop that still visits limit when it is even.
Initialize once; accumulate on each selected value
Keep each zero assignment outside its loop. += keeps earlier additions. The second loop can select evens by its starting value and step, without a parity test.
One possible solution
Your code may look different and still be correct. Compare the approach with your own.
The remainder-based loop visits all 100 numbers and selects 50 of them. The stepped loop visits just those 50 even numbers. Each adds 2 + 4 + … + 100 independently. The small walkthrough shows what each turn is doing.
Walk through the fix.
Choose Next to inspect one change at a time. Highlighted lines show what to notice. Your editor stays unchanged unless you choose to load a step.
Step 1 of 3
Visit six values, select three
For limit 6, num visits every value from 1 through 6. The remainder test selects 2, 4 and 6. total1 changes from 0 to 2 to 6 to 12; odd values leave it unchanged.
limit = 6
total1 = 0
for num in range(1, limit + 1):
if num % 2 == 0:
total1 += num
print(total1)Expected output for this step
12
Visit only the selected values
Starting at 2 and stepping by 2 visits 2, 4 and 6 directly. This loop has three turns instead of six, and reaches the same sum without testing odd numbers.
limit = 6
total2 = 0
for num in range(2, limit + 1, 2):
total2 += num
print(total2)Expected output for this step
12
Apply both methods through 100
Each loop now uses limit + 1 as its excluded stop so 100 is included. The totals stay independent, and the two final print calls show both answers.
limit = 100
total1 = 0
total2 = 0
for num in range(1, limit + 1):
if num % 2 == 0:
total1 += num
for num in range(2, limit + 1, 2):
total2 += num
print("Total using modulus check:", total1)
print("Total using step in loop:", total2)Expected output for this step
Total using modulus check: 2550 Total using step in loop: 2550
See the complete solution
limit = 100
total1 = 0
total2 = 0
for num in range(1, limit + 1):
if num % 2 == 0:
total1 += num
for num in range(2, limit + 1, 2):
total2 += num
print("Total using modulus check:", total1)
print("Total using step in loop:", total2)Ready to try it?
Practice in your browser and see your results.
Progress details
Have a question?
Ask the Code Zone community.
Ask a question in The Code Zone (opens in a new tab)Continue learning

7 days freeTry The Code Zone
THE CODE ZONE COMMUNITY
Your next step is easier together.
Try the community free for 7 days.
No charge during your trial. Choose your plan on Skool.
- Ask questions and get support in the community
- Watch every course video walkthrough
- Find a study buddy in the 24/7 study room
Make the most of your trial with Premium.
Choose Premium on Skool to try Thursday live help sessions. Bring your questions, work through code, and get unstuck together.
Scheduled Thursdays in Eastern Time. Check the Skool calendar for your local date and time.
Have a website account? Sign in