Courseiva
Computer Programming and Python FundamentalsmediumMultiple ChoiceObjective-mapped

PCEP Computer Programming and Python Fundamentals Practice Question

A developer is writing a script to process user input. The script should repeatedly ask for a number until a valid integer is entered. Which loop structure is most appropriate?

⚠ Common exam trap

The PCEP exam often tests the misconception that a for loop can handle indefinite repetition by using a large range, but the trap is that for loops are designed for definite iteration over a sequence, not for conditions that depend on runtime input validation.

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

A while loop with a condition is the most appropriate because it allows the script to repeatedly prompt for input until a valid integer is entered, checking the condition before each iteration. In Python, the while loop continues as long as the condition evaluates to True, making it ideal for indefinite iteration where the number of repetitions is unknown beforehand. This pattern is commonly used with a try-except block to validate integer conversion.

Answer analysis

Option-by-option breakdown

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

  • while loop with a condition

    Why this is correct

    A while loop can run until a condition is met, ideal for input validation.

  • for loop

    Why it's wrong here

    For loops are designed for iterating over a known sequence, not for indefinite repetition.

  • do-while loop

    Why it's wrong here

    Python does not have a do-while loop.

  • Recursion

    Why it's wrong here

    Recursion could be used but is not typical and risks stack overflow.

About these practice questions

One of 498 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 by Johnson Ajibi, MSc IT Security

Senior Network & Security Engineer · founder of Courseiva

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.