Software Development

How can I correctly implement a while loop in Python to print exactly the first 10 even numbers?

JE Asked by Jessica Taylor · 08-01-2024
0 upvotes 11,618 views 0 comments
The question

I am currently practicing control flow structures and I'm trying to use a while loop to display the first 10 even numbers starting from 2. I keep running into infinite loops or getting an incorrect count of numbers. Could someone show me the proper way to set up the counter and the loop condition so it terminates precisely after the tenth even number is printed?

3 answers

0
MA
Answered on 15-02-2024

To print exactly 10 even numbers using a while loop, you need two variables: one to track the current number being evaluated and another to count how many even numbers you have already printed. Start your count at 0 and your number at 2. Your condition should be while count < 10. Inside the loop, print the number, then increment the number by 2 and the count by 1. This ensures you don't rely on the value of the even number itself for the loop termination, which is a cleaner way to handle logic. Using an increment of number += 2 is more efficient than checking every integer with a modulo operator % if you already know your starting point is even.

0
DA
Answered on 20-03-2024

Would using a for loop with a range(2, 22, 2) be more concise for this specific task, or is there a particular reason you need to stick with a while loop for your assignment?

ST 22-03-2024

Daniel, while a for loop is certainly more "Pythonic" for a fixed range, understanding while loops is critical for scenarios where the end condition isn't known upfront. For a beginner like Jessica, mastering the while loop helps in understanding manual state management and incrementing. It’s the building block for more complex logic like reading files or handling user input streams where you don't have a pre-defined sequence length to iterate over.

0
NA
Answered on 12-05-2024

The simplest way is: i = 2 and while i <= 20:. Then just print i and do i += 2. This avoids needing a separate counter variable entirely.

JE 14-05-2024

I agree with Nancy. If the goal is just to reach the number 20, keeping it simple is best. However, Jessica should be careful—if the requirement changes to "the first 100 even numbers," calculating that the end point is 200 becomes a chore, which is why Margaret's counter method is a better long-term habit!

Share your thoughts

Your email address will not be published. Required fields are marked (*)

Professional Counselling Session

Still have questions?
Schedule a free counselling session

Our experts are ready to help you with any questions about courses, admissions, or career paths. Get personalized guidance from industry professionals.

Request a Call Back

Search Online

We Accept

We Accept

Follow Us

"PMI®", "PMBOK®", "PMP®", "CAPM®" and "PMI-ACP®" are registered marks of the Project Management Institute, Inc. | "CSM", "CST" are Registered Trade Marks of The Scrum Alliance, USA. | COBIT® is a trademark of ISACA® registered in the United States and other countries.

Book Free Session