A developer writes a loop to sum numbers from 1 to 10. The code outputs 55, but the expected sum is 55. However, the loop uses a range that includes 0. Which range should be used to achieve the correct sum?
Trap 1: range(0,11)
Includes 0, which is unnecessary but still sums to 55; however the question says the loop includes 0 and that's not desired.
Trap 2: range(0,101)
Generates 0..100, sum 5050
Trap 3: range(1,10)
Generates 1..9, sum 45
- A
range(0,11)
Why it fails: Includes 0, which is unnecessary but still sums to 55; however the question says the loop includes 0 and that's not desired.
- B
range(1,11)
range(1,11) generates integers 1 through 10 inclusive, since the stop value is exclusive. Summing these yields 55, matching the expected total while excluding 0, which adds nothing but violates the stated constraint of starting at 1.
- C
range(0,101)
Why it fails: Generates 0..100, sum 5050
- D
range(1,10)
Why it fails: Generates 1..9, sum 45