Courseiva

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

A junior developer is writing a script to calculate the total cost of items in a shopping cart. The script uses variables item_price (float) and quantity (int). The code is:

item_price = 2.5 quantity = 3 total = item_price * quantity

print("Total: " + total)

When run, this code raises a TypeError. The developer is confused because the multiplication seems correct. What is the most likely issue and the correct fix?

⚠ Common exam trap

Python Institute often tests the distinction between arithmetic operations (which work across numeric types) and string concatenation (which requires explicit type conversion), trapping candidates who assume Python will automatically convert a float to a string when using the `+` operator.

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 print function cannot concatenate a string with a float; convert total to string using str(total).

The `print` function in Python expects all arguments to be strings when using the `+` operator for concatenation. The variable `total` is a float (result of multiplying a float by an int), and Python does not implicitly convert it to a string. The error is a `TypeError: can only concatenate str (not 'float') to str`. The fix is to explicitly convert `total` to a string using `str(total)` before concatenation.

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 variable total is being overwritten by the print statement; use a different variable name.

    Why it's wrong here

    print never reassigns total; the TypeError comes from concatenating a string with a float, so str(total) is needed. Renaming variables is tempting when debugging scoping or shadowing faults, which is the correct fix when a name genuinely collides with another binding.

  • ✗

    The multiplication should be written as int(item_price) * quantity to ensure integer result.

    Why it's wrong here

    Casting item_price to int discards the decimal, so 2.5 becomes 2 and totals are wrong; the TypeError arises because print concatenates a string with a float, requiring str(total). Integer conversion is for scenarios demanding whole-number arithmetic, such as counting discrete units.

  • ✗

    The variable names are too descriptive; rename them to a and b to avoid confusion.

    Why it's wrong here

    Renaming variables does not change the types involved, so the TypeError from concatenating a string with a float persists. It is tempting because descriptive names can feel verbose, but the actual fix is converting total with str() or using an f-string, since Python cannot concatenate str and float directly.

  • ✓

    The print function cannot concatenate a string with a float; convert total to string using str(total).

    Why this is correct

    Python's + operator cannot concatenate str with float, so print raises TypeError regardless of the arithmetic being valid. Wrapping total in str() converts the numeric result to text, satisfying the concatenation requirement and producing the intended output.

About these practice questions

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