Python Practice › Loop Programs › Program 73
1 to 10 except 5 and 6 with while
Write a Python program to print 1 to 10 except 5 and 6 using a while loop.
Output
1 2 3 4 7 8 9 10
Approach
- Increase the counter before
continue, otherwise the loop never ends.
Solution
n = 0while n < 10: n += 1 if n in (5, 6): continue print(n)Output
1 2 3 4 7 8 9 10
Line by line
Line 3
Incrementing at the top of the loop means continue cannot skip it.
Watch out
Putting n += 1 after the continue would make the loop get stuck at 5 forever.
Learn the concept
New to this topic? Read the lesson, then come back.