PCEP Control Flow, Loops, Lists and Logic Practice Question
A logistics analyst maintains a list `weights` representing the mass of parcels in kilograms. The analyst needs to determine how many parcels weigh more than 10 kg, but the list also contains a few `None` values that indicate missing measurements and must not be counted. Which code correctly counts only the parcels heavier than 10 kg while ignoring missing entries?
⚠ Common exam trap
The trap here is assuming that a simple numeric comparison will automatically skip None values, when in fact comparing None with an integer raises a TypeError.
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
✓
count = 0 for w in weights: if w is not None and w > 10: count += 1
The correct code uses a combined condition that first verifies the element is not None, then compares it with 10. This ordering exploits short-circuit evaluation so that None never reaches the numeric comparison. As a result, only actual numeric weights greater than 10 increment the counter, and missing measurements are silently skipped as required by the analyst.
Answer analysis
Option-by-option breakdown
For each option: why learners choose it and why it is or isn't the right answer here.
- ✓
count = 0 for w in weights: if w is not None and w > 10: count += 1
Why this is correct
The condition first checks that the element is not None, short-circuiting before the numeric comparison. This prevents a TypeError on missing measurements and correctly increments the counter only for parcels whose weight exceeds 10 kg. It satisfies both requirements: counting heavy parcels and ignoring None entries, using standard Python truth evaluation.
- ✗
count = 0 for w in weights: if w > 10: count += 1
Why it's wrong here
This loop compares every element to 10, but when it reaches a None value, Python raises a TypeError because None cannot be compared with an integer using the greater-than operator. The missing measurements would cause the script to crash instead of being skipped, so this approach does not meet the requirement of ignoring None entries.
- ✗
count = sum(1 for w in weights if w > 10)
Why it's wrong here
The generator expression applies w > 10 to every element, including None, and will raise a TypeError when it encounters a missing measurement. Although sum with a generator is concise, it provides no protection against None values, so the script fails on data that contains missing entries and does not fulfill the scenario.
- ✗
count = 0 for w in weights: if w > 10 or w is None: count += 1
Why it's wrong here
Using or here still evaluates w > 10 for None values because the left operand is checked first, causing a TypeError before the None check can help. Even if it did not crash, the logic would count None entries as heavy parcels, which contradicts the requirement to exclude missing measurements from the tally.
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.