65.8k views
2 votes
Write a program that prints even numbers 2 through 20 using a while True loop. Make sure that the loop stops correctly after the number 20 is printed. Make sure the following components are in your program:

1.Name the program: U1-M4-Odd Numbers-TB (Use your initials - not mine)
2. Include a while loop structure.
3.Include program and line comments.
4.Proper spelling, punctuation, capitalization, and grammar.
5.Proper naming conventions.

User Himansh
by
7.4k points

1 Answer

4 votes

Final answer:

The Python program uses a 'while True' loop to print even numbers from 2 through 20, including a break statement to stop the loop after 20, as well as comments for explanation. (option 2)

Step-by-step explanation:

Python Program to Print Even Numbers from 2 to 20

The task at hand is to write a Python program that prints even numbers ranging from 2 through 20 using a while True loop and to ensure the loop stops after printing the number 20. The program will be named according to the specified format, and comments will be included for clarity. Below is the Python program that accomplishes this task:

# Program name: U1-M4-Even Numbers-YI (Replace YI with your own initials)
# Initialize the starting number
number = 2

# Start the while loop
while True:
# Check if the number is even
if number % 2 == 0:
print(number)
# Increment the number by 1
number += 1
# Break the loop if the number exceeds 20
if number > 20:
break

The above program uses a while loop structure, includes proper naming conventions, and the comments provides necessary explanations about the code. The True loop iteratively checks for even numbers and prints them until the number exceeds 20, at which point it uses 'break' to terminate the loop.

User Mike Gold
by
7.3k points