Repository navigation
exercise7/answer.py: break at 5 stops the loop — was continue the intended tool?
#33
|
In for num in range(10):
if num == 5:
break
print(num)This prints 0–4 and stops entirely; 6–9 never print. If the goal was "skip 5, keep going", |
Replies: 1 comment
|
Actually the file answers this itself — for num in range(10):
if num == 5:
break
print(num) # 0 1 2 3 4 — stop entirely
for num in range(5):
if num == 2:
continue
print(num) # 0 1 3 4 — skip one, keep goingPlus a third variant further down ( Two things worth adding as comments/short exercises rather than changing the code:
So: canonical answer stays |
Actually the file answers this itself —
breakis the intended demo there, andcontinuealready gets its own loop three lines later. The file is showing both keywords, not making the mistake:Plus a third variant further down (
for nut in range(16): if nut % 2 == 0: continue— keep only odds), socontinueis demonstrated twice. Thebreakloop's comment-worthy property is precisely that 6–9 never print:breakexits the whole loop, wherecontinuewould have kept running. "Skip 5…