How does building sum_odds step by step expose the off-by-one bug hiding inside range — and why is solving the easier version first the winning move?
Short drills on what this video just taught. Write the code, run the checks, and reveal the answer only if you are stuck.
range(start, stop) produces the integers starting at start but stops before stop — it never includes stop itself. Using a = 2 and b = 6 below, assign to nums the list of every integer from a up to and including b.
Hint: range(start, stop) stops one short of stop, and list(...) turns a range into a list.
Planning on paper before writing code
Work the two paper examples, a = 2 with b = 4 and a = 2 with b = 7, listing every number in range and computing the expected sum by hand first.
Solving the simpler sum-all problem first
Write both loops that add every number from a to b — the for over range(a, b) and the while with i = a, i <= b and an increment — and initialise sum_of_odds above them.
The range off-by-one bug
Record the test on a = 2, b = 4 where the for version gave 5 and the while version gave 9, the print that showed 4 was skipped, and the fix range(a, b + 1).
Adding the odd-only nuance
Write the guard both ways, if not is_even(i) reusing the function already written and debugged, and if i % 2 == 1, then re-test against the a = 2, b = 7 paper answer.