PCEP Control Flow, Loops, Lists and Logic Practice Question
A junior developer writes a Python script to sum all numbers greater than 10 from a list. The code is:
numbers = [5, 12, 8, 15, 3] total = 0
for num in numbers:
if num > 10:total = total + 1
print(total)
The output is 2, but the expected sum is 27 (12+15). Which change will produce the correct output?
⚠ Common exam trap
Many candidates confuse counting with summing — they see `total = total + 1` and think it's accumulating values, but it actually increments by a constant, not by the variable `num`.
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
✓
Change `total = total + 1` to `total += num`
The original code increments `total` by 1 for each qualifying number, counting them instead of summing their values. Changing `total = total + 1` to `total += num` adds the actual number to the accumulator, producing the correct sum of 12 + 15 = 27.
Answer analysis
Option-by-option breakdown
For each option: why learners choose it and why it is or isn't the right answer here.
- ✗
Change `total = 0` to `total = []`
Why it's wrong here
Initialising total as an empty list makes total + 1 a TypeError, since list and int cannot be concatenated; a list suits collecting matching values, not accumulating a running numeric sum, which requires total = total + num.
- ✓
Change `total = total + 1` to `total += num`
Why this is correct
Replacing the increment with `total += num` accumulates each qualifying value rather than counting matches, satisfying the stem's requirement to sum numbers greater than 10. The `if num > 10` filter already isolates 12 and 15, so the accumulator yields 27 instead of the tally 2.
- ✗
Change `if num > 10:` to `if num >= 10:`
Why it's wrong here
This would include 10 if present, but the sum would still be 27 if 10 were not there; however, the problem is that the code counts instead of sums.
- ✗
Change `for num in numbers:` to `for num in range(numbers):`
Why it's wrong here
Iterating with `range(numbers)` raises a TypeError, since `range()` requires an integer argument, not a list. It is tempting because `range()` genuinely suits index-based loops, such as `for i in range(len(numbers))`, where you then access `numbers[i]` — the correct pattern when you need each element's position as well as its value.
Go deeper
Related to this question
About these practice questions
Courseiva writes every PCEP question from scratch — 482 in total, each with an explanation and a wrong-answer breakdown. None are copied from real exams or dumps. Learn why practice questions differ from exam dumps →
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.