if I count 2 rocks do I count from 0 to 2 or from 1 to 2 — Dawnstorm
I think of it like this: before i count, i place my finger on 0 in the number line, and when i make my first count, i move my finger to 1 on the number line, and so on. That 0 tells me what i have before i start counting. If i place my finger at 1 on the number line before counting, then for my first count, my finger moves to number 2 on the number line. That 1 tells me what i had before i started counting. So the process of counting is adding to the prior count. Sometimes that count is 0 (no count), and sometimes it's more than 0.
(< 3) and (<= 2) are essentially the same in the context of the counting loop. Let's try (<= 2):
With print statement before the count:
rocks = 0
while rocks <= 2:
print(rocks)
rocks += 1
output = [0, 1, 2] # incorrect output
rocks = 1
while rocks <= 2:
print(rocks)
rocks += 1
output = [1, 2] # correct output
In this second counting loop, the number of rocks you start with is already 1, and you are printing your first "count" without having counted yet. So, the loop actually just counts 1 time to get to 2. Although the output is apparently correct, the logic behind the count is not. The loop would function as an accurate rock counter nonetheless.
With print statement after the count:
rocks = 0
while rocks <= 2:
rocks += 1
print(rocks)
output = [1, 2, 3] # incorrect output
rocks = 1
while rocks <= 2:
rocks += 1
print(rocks)
output = [2, 3] # incorrect output
The reason i place the print statement inside these loops is so that we can see the process of counting as it happens. If i place the print statement outside the loop after it is done counting, then the result will be the same for both counting loops.
rocks = 0
while rocks < 2:
rocks += 1
print(rocks)
output = [2] # correct final output
rocks = 1
while rocks < 2:
rocks += 1
print(rocks)
output = [2] # correct final output