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 wrong: 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)
Correctly generates 1..10
- C
range(0,101)
Why wrong: Generates 0..100, sum 5050
- D
range(1,10)
Why wrong: Generates 1..9, sum 45