Courseiva
Data Types, Variables, Basic I/O and OperatorseasyMultiple ChoiceObjective-mapped

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

A developer writes code to compute the average of two numbers entered by the user: x = input('Enter first number: '); y = input('Enter second number: '); avg = (x + y) / 2. The program produces an error. What is the best practice to fix the code?

⚠ Common exam trap

Python Institute often tests the misconception that `input()` returns a numeric type, leading candidates to pick Option B (using `int`) or Option C (casting inside the expression), but the trap is that string concatenation happens before any conversion, so the error occurs at the `+` operator, not at the division.

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

Convert x and y to float before the operation: x = float(input(...)); y = float(input(...))

The `input()` function in Python always returns a string. When you use the `+` operator on two strings, it concatenates them (e.g., '5' + '3' = '53'), not adds them numerically. Dividing a concatenated string by 2 raises a TypeError. The best practice is to convert both inputs to `float` immediately after receiving them, ensuring the subsequent arithmetic works correctly.

Answer analysis

Option-by-option breakdown

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

  • Convert the result to int after the operation: avg = int((x + y) / 2)

    Why it's wrong here

    This does not solve the string concatenation issue; x + y would concatenate strings.

  • Use int() on the input() values: x = int(input(...)); y = int(input(...))

    Why it's wrong here

    This works only if the input is a whole number, but the problem does not specify integers.

  • Use type casting in the division: avg = (float(x) + float(y)) / 2

    Why it's wrong here

    This would work but is less efficient than converting once; however, the order is correct, so this could also work but is not best practice because it repeats conversion.

  • Convert x and y to float before the operation: x = float(input(...)); y = float(input(...))

    Why this is correct

    This ensures the addition and division work on numeric values.

About these practice questions

One of 498 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 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.