PCEP Control Flow, Loops, Lists and Logic Practice Question
A Python developer is writing a script that processes a list of sensor readings stored in the variable readings. The developer needs to iterate over the list and print each reading, but wants the loop to stop immediately if a reading of -999 is encountered, because that value indicates a sensor malfunction. Which code snippet correctly implements this behavior?
⚠ Common exam trap
Many exam-takers confuse break with continue, where continue skips only the current iteration but does not stop the 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
✓
for reading in readings: if reading == -999: break print(reading)
The correct implementation must iterate over the list, check for the malfunction value, and break immediately without printing that value. The snippet that places the break inside the if block and the print after the if block ensures that normal readings are printed and the loop terminates upon encountering -999. This matches the specified behavior precisely.
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 reading in readings: if reading == -999: break print(reading)
Why this is correct
This snippet iterates over each element in readings, checks if the current reading equals -999, and if so executes break to exit the loop immediately. The print statement is placed after the if block, so it only executes for non-malfunction readings. This matches the requirement exactly.
- ✗
for reading in readings: if reading != -999: print(reading) else: break
Why it's wrong here
This snippet breaks out of the loop when -999 is encountered, but the print statement is outside the loop, so it only prints the last value of reading after the loop finishes. It does not print each reading as required, and it does not stop before printing the malfunction value because printing happens after the loop. This fails the requirement.
- ✗
for reading in readings: print(reading) if reading == -999: break
Why it's wrong here
Here print executes before the check for -999, so the malfunction value itself is printed before the loop breaks. The requirement is to stop immediately without processing the malfunction reading, so printing it violates the intended behavior. The order of statements is incorrect.
- ✗
for reading in readings: if reading == -999: continue print(reading)
Why it's wrong here
Using continue when reading equals -999 would skip the print for that iteration but continue processing the remaining readings. The loop would not stop, so subsequent readings after -999 would still be printed. This does not implement the required immediate termination on encountering the malfunction value.
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.