Python Practice › Loop Programs › Program 73

1 to 10 except 5 and 6 with while

EasyLoop Programs · 73 of 90

Write a Python program to print 1 to 10 except 5 and 6 using a while loop.

Advertisement
Output
1
2
3
4
7
8
9
10

Approach

  1. Increase the counter before continue, otherwise the loop never ends.

Solution

Python
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.

More loop programs

← All 90 Loop Programs

Leave a Comment