Courseiva

PCEP Control Flow, Loops, Lists and Logic Practice Question

A Python developer is writing a program that prompts the user to enter a positive integer. The program should repeatedly ask for input until the user enters a number greater than zero. Which loop construct is most appropriate to implement this requirement?

⚠ Common exam trap

The trap here is assuming Python has a do-while loop like some other languages, or confusing the role of if and break with a loop.

Answer choices

Why each option matters

Answer the question above first, then reveal the full breakdown to understand why each option is right or wrong.

Correct answer & explanation

✓

while loop with a condition

The requirement is to repeatedly prompt the user until a positive integer is entered. This calls for a loop that continues based on a condition that depends on user input. A while loop is designed for such indefinite iteration, as it checks the condition before each iteration and can be controlled by the input value. Other constructs either cannot repeat indefinitely or do not exist in Python.

Answer analysis

Option-by-option breakdown

For each option: why learners choose it and why it is or isn't the right answer here.

  • ✗

    for loop with a range

    Why it's wrong here

    A for loop iterates over a sequence, such as a range of numbers. It is not suitable for indefinite repetition based on user input because the number of iterations is predetermined. Using a for loop would require knowing in advance how many attempts the user needs, which is not the case here.

  • ✓

    while loop with a condition

    Why this is correct

    A while loop repeatedly executes a block of code as long as a specified condition remains true. In this scenario, the condition can be set to continue prompting while the entered number is not greater than zero. This allows the loop to run until the user provides valid input, making it the ideal choice for indefinite repetition.

  • ✗

    if statement with a break

    Why it's wrong here

    An if statement is a conditional, not a loop. It executes a block once if a condition is true. While break can exit a loop, an if alone cannot repeat the prompt. Without a surrounding loop, the program would only ask once and then continue, failing to repeat until valid input is received.

  • ✗

    do-while loop

    Why it's wrong here

    Python does not have a do-while loop construct. Some other languages have it, but in Python, similar behavior is achieved with a while loop and a break condition. Therefore, this option is not valid in Python and cannot be used to implement the requirement.

About these practice questions

One of 482 original PCEP practice questions on Courseiva, each with a full explanation and wrong-answer analysis — not exam dumps or protected exam content. Learn why practice questions differ from exam dumps →

How Courseiva writes practice questions · Editorial policy

JA

Written and reviewed by Johnson Ajibi, MSc IT Security

Senior Network & Security Engineer · founder of Courseiva

Last reviewed September 2026 · checked against the official Python Institute exam blueprint

This PCEP practice question is part of Courseiva's free Python Institute certification practice question bank. Courseiva provides original exam-style practice questions with explanations, topic-based practice, mock exams, readiness tracking, and study analytics to help learners prepare for the PCEP exam.