Courseiva

PCEP Practice Question: Data Types, Variables, Basic I/O and Operators

A data analyst needs to read two integers from the user and compute their average as a float. The current code:

a = int(input()) b = int(input()) avg = a + b / 2

print(avg)

The output is always incorrect when a=5 and b=7 (expected 6.0, actual 8.5). The analyst cannot identify the bug. What is the root cause and correct fix?

⚠ Common exam trap

Python Institute often tests operator precedence by presenting a simple arithmetic expression without parentheses, leading candidates to overlook the order of operations and incorrectly blame input conversion or output formatting.

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 addition and division have wrong operator precedence; use parentheses: (a + b) / 2.

In Python, the division operator `/` has higher precedence than addition `+`, so `a + b / 2` is evaluated as `a + (b / 2)`, not `(a + b) / 2`. For a=5 and b=7, this computes `5 + (7 / 2) = 5 + 3.5 = 8.5` instead of the expected `(5 + 7) / 2 = 12 / 2 = 6.0`. Adding parentheses around `a + b` forces the addition to occur first, yielding the correct average as a float.

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 input function is not converting correctly; use float(input()) instead.

    Why it's wrong here

    int(input()) already parses the entered digits correctly, and float() would still yield 8.5 because operator precedence, not input type, causes the error. float() is the right tool when inputs legitimately contain decimal points, which this integer-only scenario does not.

  • ✗

    The print function is rounding the result; use print("{:.1f}".format(avg)).

    Why it's wrong here

    print outputs the float it receives without altering its value, so formatting cannot change 8.5 into 6.0. String formatting is genuinely useful for controlling displayed decimal places, but the arithmetic itself must be corrected with parentheses before formatting matters.

  • ✓

    The addition and division have wrong operator precedence; use parentheses: (a + b) / 2.

    Why this is correct

    Division binds tighter than addition, so a + b / 2 computes a + (b/2) = 5 + 3.5 = 8.5. Parenthesising as (a + b) / 2 forces the sum first, giving 12 / 2 = 6.0, the expected float average.

  • ✗

    The division operator / always returns an integer; use // instead.

    Why it's wrong here

    Python's / performs true division and returns a float, so 7/2 gives 3.5; // is floor division and would return 3, worsening the result. Floor division is correct only when an integer quotient is genuinely wanted, not for computing an average.

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 →

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.