Courseiva
Control Flow, Loops, Lists and LogichardMultiple ChoiceObjective-mapped

PCEP Control Flow, Loops, Lists and Logic Practice Question

A network engineer writes a script to validate IP addresses. The script checks each octet and prints 'Valid' if all octets are between 0 and 255, otherwise 'Invalid'. However, the script always prints 'Invalid' for valid IPs. The code uses a for loop with an else clause. Which logical error is likely?

⚠ Common exam trap

Python Institute often tests the `for...else` behavior by reversing the expected logic, trapping candidates who assume `else` runs only on error or break, rather than on normal loop completion.

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

The for loop's else clause executes when the loop completes without break, but the engineer expects else to run when break occurs

In Python, a `for` loop's `else` clause executes only when the loop completes normally (i.e., without hitting a `break`). The engineer likely intended the `else` to run when an invalid octet is found (triggering a `break`), but instead the `else` runs when all octets are valid and the loop finishes without breaking, causing the script to always print 'Invalid' when the validation logic is inverted.

Answer analysis

Option-by-option breakdown

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

  • The list of octets is not properly split

    Why it's wrong here

    Would cause errors earlier, not always 'Invalid'.

  • The condition uses 'or' instead of 'and'

    Why it's wrong here

    Would cause incorrect validation, but not specifically related to for-else.

  • The else clause is indented incorrectly

    Why it's wrong here

    Indentation errors would likely cause SyntaxError, not always 'Invalid'.

  • The for loop's else clause executes when the loop completes without break, but the engineer expects else to run when break occurs

    Why this is correct

    Common misunderstanding of for-else; else runs on normal completion.

About these practice questions

Courseiva writes every PCEP question from scratch — 498 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 →

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.