PCEP Control Flow, Loops, Lists and Logic Practice Question
A developer is writing a Python script that should keep prompting for a password until the user enters the correct one. The script must not run forever if the user never guesses correctly, so it should give up after 5 attempts. Which loop construct is most appropriate for this scenario?
⚠ Common exam trap
The trap here is assuming that a while loop checking the password is equivalent to a bounded retry loop, when in fact it can run forever if the password is never correct.
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
✓
A for loop that iterates over range(5), checking the password inside and breaking when correct.
The requirement combines a fixed maximum number of attempts with the possibility of stopping early on success. A for loop over range(5) supplies the exact bound of five iterations, and a break inside ends the loop as soon as the password matches. The other constructs either loop forever when the user keeps failing or do not model repeated input attempts at all.
Answer analysis
Option-by-option breakdown
For each option: why learners choose it and why it is or isn't the right answer here.
- ✗
A for loop that iterates over the characters of the password string, comparing each character.
Why it's wrong here
Iterating over the password's characters compares individual letters, not the whole entered password. This does not model repeated user attempts at all; it models character-by-character inspection. The loop would run once per character in the stored password, which is unrelated to the five attempts the script must allow, and it never reads new input from the user.
- ✓
A for loop that iterates over range(5), checking the password inside and breaking when correct.
Why this is correct
A for loop over range(5) runs exactly five times, providing a natural attempt limit. Inside, an if statement can compare the input to the stored password and break early when it matches, so the loop stops as soon as the correct value is entered, satisfying both the retry and the give-up-after-five requirement without any manual counter management.
- ✗
A while True loop with no break statement, relying on the password check to terminate.
Why it's wrong here
A while True loop never terminates on its own. Without a break or a condition that becomes false, the loop would run indefinitely, which directly contradicts the requirement to stop after five attempts. The password check alone does not end the loop; it only controls what happens inside each iteration, so this construct would cause the script to hang.
- ✗
A while loop that continues as long as the entered password is incorrect, with no attempt counter.
Why it's wrong here
This while loop stops only when the correct password is entered. If the user never enters it, the loop repeats forever, so the five-attempt limit is not enforced. The scenario explicitly requires giving up after five tries, which demands a counter or a bounded iteration, neither of which is present in this approach.
Go deeper
Related to this question
About these practice questions
This PCEP question is part of Courseiva's 482-question bank — original exam-style content with full explanations and wrong-answer analysis, never real exam questions or exam dumps. Learn why practice questions differ from exam dumps →
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.