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

PCEP Control Flow, Loops, Lists and Logic Practice Question

You are working on a network automation script that reads a configuration file containing firewall rules. Each rule is a dictionary with keys 'source_ip', 'dest_ip', 'action'. The script must iterate over the rules and print all rules where action is 'allow'. However, the script is not printing any output even though there are allow rules in the file. The code snippet is:

rules = [{'source_ip':'10.0.0.1','dest_ip':'10.0.0.2','action':'allow'},

{'source_ip':'10.0.0.2','dest_ip':'10.0.0.3','action':'deny'}]
for rule in rules:
    if rule('action') == 'allow':
        print(rule)

What is the most likely cause of the problem?

⚠ Common exam trap

The trap here is that candidates may overlook the subtle difference between parentheses and brackets for dictionary access, assuming both work similarly, but Python strictly requires square brackets for key lookup.

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 developer used rule('action') instead of rule['action']

In Python, dictionary values are accessed using square brackets (e.g., rule['action']) or the .get() method, not parentheses. Using parentheses like rule('action') attempts to call the dictionary as a function, which raises a TypeError and prevents the script from executing, so no output is printed even though allow rules exist.

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 dictionary does not have an 'action' key

    Why it's wrong here

    It does have the key.

  • The developer used rule['action'] instead of rule.get('action')

    Why it's wrong here

    Both work; get is not required.

  • The for loop syntax is incorrect; it should be for rule in rules:

    Why it's wrong here

    Syntax is correct.

  • The developer used rule('action') instead of rule['action']

    Why this is correct

    Dictionary access uses square brackets or get method.

About these practice questions

This PCEP question is part of Courseiva's 498-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 →

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.