thecodezone.
The Code Zone
Short steps, at your pace · Free lessons and practice · Member video walkthroughs
Save your next small win.

Checking your account…

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

All courses

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.

More options

Your lesson progress

Walkthrough: not startedPractice: not started

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.

Rob compares a small range from 1 to 6: filtering 2, 4 and 6 and stepping through 2, 4 and 6 both give 12.
Rob’s comic strip · Loop challenges

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.

Use the remainder to decide what to add
even_numbers.pypython · example
print(10 % 2)  # 0: even
print(9 % 2)   # 1: odd

Check 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.

Expected output
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.

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.

Replace your editor code?

Your current work will be replaced. Choose Cancel to keep it.

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

ONE STEP FORWARD

Two algorithms can select the same values while visiting different numbers. Next, update one amount repeatedly instead of adding selected values.