Objective
Develop a Python program that writes the even numbers from 10 to 20, both inclusive, except 16, in 3 different ways:
Incrementing by 2 in each step (use "continue" to skip 16)
Incrementing by 1 in each step (use "continue" to skip 16)
Using an endless loop (with "break" and "continue")
Example Python Exercise
Show Python Code
#Incrementing by 2 in each step (use "continue" to skip 16)
# Initialize the counter variable
i = 10
# Use a while loop to display even numbers from 10 to 20, skipping 16
while i <= 20:
if i == 16:
i += 2
continue
print(i)
i += 2
#Incrementing by 1 in each step (use "continue" to skip 16)
# Initialize the counter variable
i = 10
# Use a while loop to display even numbers from 10 to 20, skipping 16
while i <= 20:
if i % 2 == 0:
if i == 16:
i += 1
continue
print(i)
i += 1
#Using an endless loop (with "break" and "continue")
# Initialize the counter variable
i = 10
# Use an endless loop to display even numbers from 10 to 20, skipping 16
while True:
if i > 20:
break
if i == 16:
i += 2
continue
print(i)
i += 2
Output
10
12
14
18
20
Share this Python Exercise