Courseiva

PCEP Computer Programming and Python Fundamentals Practice Question

A developer writes a function that modifies a global variable inside the function:

count = 0

def increment():

count += 1

When called, an error occurs. What is the correct way to fix this?

⚠ Common exam trap

PCEP often tests the misconception that Python has a 'static' keyword or that passing arguments can directly modify globals, when the correct answer is the 'global' declaration.

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

✓

Use 'global count' inside the function

The error occurs because Python treats 'count' as a local variable inside the function due to the assignment, but it is referenced before assignment. Using 'global count' inside the function explicitly tells Python to use the global variable, allowing modification. This is the correct fix for modifying a global variable from within a function.

Answer analysis

Option-by-option breakdown

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

  • ✗

    Define count inside the function

    Why it's wrong here

    Defining count inside the function creates a new local variable, so the global remains unchanged and the increment is lost; the UnboundLocalError arises because assignment makes count local. It tempts as a way to avoid the scope conflict, but the correct fix declares global count so the existing module-level binding is modified.

  • ✗

    Use the 'static' keyword

    Why it's wrong here

    Python has no static keyword for variables; static is a C, Java and C# storage concept, so this raises a SyntaxError rather than resolving the scope conflict. It tempts because other languages use static to give a variable persistent function-level storage, but Python's equivalent mechanism here is the global declaration.

  • ✗

    Pass count as an argument to the function

    Why it's wrong here

    Passing count as an argument makes it a parameter local to the function, so the caller's variable is unchanged unless the return value is reassigned; the global still never increments. It tempts because parameter passing is a standard way to supply values, but the stem requires modifying the existing global binding via global count.

  • ✓

    Use 'global count' inside the function

    Why this is correct

    Assigning to count inside the function makes it a local variable, so the augmented assignment reads an unbound local and raises UnboundLocalError. Declaring 'global count' tells Python to bind the name to the module-level variable, allowing the increment to modify it.

About these practice questions

One of 482 original PCEP practice questions on Courseiva, each with a full explanation and wrong-answer analysis — not exam dumps or protected exam content. Learn why practice questions differ from exam dumps →

How Courseiva writes practice questions · Editorial policy

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.