Pause & Proceed - Python Programming Exercise

In this exercise, you will develop a Python program that writes the even numbers from 10 to 20, both inclusive, except 16. The program will implement three different methods to achieve this: Using a "while loop" and incrementing by 2 in each step, utilizing the "continue" statement to skip the number 16. Using a "while loop" and incrementing by 1 in each step, also employing "continue" to skip the number 16. Using an endless "while loop" with "break" and "continue" to stop the loop when the desired numbers are printed. This task will help you practice loops in Python, specifically how to control the flow of the loop using "continue" to skip iterations and "break" to exit the loop. You will also work with conditional statements to check for specific numbers like 16 and avoid printing them. By completing this exercise, you will improve your understanding of loop control and enhance your ability to handle edge cases in Python programming. These techniques are widely used in real-world applications to manage iterative processes efficiently.

 Category

Mastering Flow Control

 Exercise

Pause & Proceed

 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

 Copy 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

 More Python Programming Exercises of Mastering Flow Control

Explore our set of Python Programming Exercises! Specifically designed for beginners, these exercises will help you develop a solid understanding of the basics of Python. From variables and data types to control structures and simple functions, each exercise is crafted to challenge you incrementally as you build confidence in coding in Python.

  •  Quadrilateral V2

    In this Python exercise, you will develop a program that prompts the user to input a number, width, and height and displays a rectangle of the specified...

  •  Iterative Patterns

    This Python program allows the user to input two numbers and displays all the numbers between them (inclusive) three times using different types of loops: "for"...

  •  Numerical Digits

    This Python program demonstrates how to calculate the number of digits in a positive integer by repeatedly dividing the number by 10. If the user enters a n...

  •  Empty Square

    This Python program prompts the user to input a symbol and a width, then displays a hollow square with the specified width. The square's outer border is...

  •  Output

    This Python program prompts the user for two integer numbers and calculates their product without using the "*" operator. Instead, it uses consecutiv...

  •  Absolute Magnitude

    This Python program calculates and displays the absolute value of a number x. The absolute value of a number is defined as the number itself if it is positive,...