Exercise 2: Counting Iterations for range(1,8) vs range(1,9) — Possible Solution ==================================================================== WHAT THE PSEUDOCODE INTENDS ------------------------------ This chapter established that "for i = 1 to n" is standard textbook convention meaning INCLUSIVE of n. For n=8, that means the loop is intended to visit 1, 2, 3, 4, 5, 6, 7, 8 - eight values total. (a) PYTHON range(1, 8) ------------------------------ Python's range(start, stop) is exclusive of its stop value. range(1,8) produces [1, 2, 3, 4, 5, 6, 7] - seven values. Iteration count: 7 Matches the inclusive convention? NO - it silently drops the value 8, exactly the same off-by-one gap this chapter's own worked example demonstrated (there, n=5 dropped the value 5). (b) PYTHON range(1, 9) ------------------------------ range(1, 9) produces [1, 2, 3, 4, 5, 6, 7, 8] - eight values. Iteration count: 8 Matches the inclusive convention? YES - this is the correct translation of "for i = 1 to 8 inclusive" into Python, since adding 1 to the intended upper bound compensates for range()'s own exclusive-of-stop behavior. WHY THIS CONFIRMS THE CHAPTER'S OWN PATTERN ------------------------------ Both cases here follow exactly the same mechanism this chapter verified with n=5: a direct, unadjusted translation of "1 to n" into range(1, n) always produces one fewer iteration than the pseudocode intended, regardless of what n actually is. The correct translation pattern is always range(1, n+1) when the pseudocode's own convention is inclusive - which is exactly what part (b) demonstrates. WHY THIS WORKS AS AN ANSWER ------------------------------ The answer states both iteration counts precisely, explicitly checks each against the chapter's own inclusive-convention definition rather than just reporting a count, and connects the general pattern (why adding 1 to the stop value fixes the translation) back to the exact mechanism the chapter's own n=5 example already demonstrated.