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

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

A data analyst uses Python to process a CSV file containing sales data. The file has columns: 'Product', 'Price', 'Quantity'. The analyst writes a script to compute total sales: sum of Price * Quantity for each row. The code reads each row as a list of strings. The analyst uses: total = 0; for row in reader: total += row['Price'] * row['Quantity']; print(total). The script raises a TypeError. What is the best fix?

⚠ Common exam trap

Python Institute often tests the distinction between string repetition (valid) and string multiplication of two strings (invalid), leading candidates to overlook the need for explicit type conversion.

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 float(row[1]) * float(row[2]) instead of row['Price'] * row['Quantity'].

The TypeError occurs because row is a list of strings, so integer indices must be used instead of string keys. Multiplying strings also requires conversion. Option D correctly uses float(row[1]) * float(row[2]) with indices, fixing both issues. Option A only converts to float but still uses invalid string keys.

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 Price and Quantity to float before multiplication.

    Why it's wrong here

    Incorrect: still uses row['Price'] which is invalid for a list; conversion alone does not fix the indexing error.

  • Change the loop to for i in range(len(reader)): total += reader[i][1] * reader[i][2].

    Why it's wrong here

    Incorrect: reader is an iterator, so len() and indexing may not be supported; also, no type conversion.

  • Use integer multiplication and then convert to float.

    Why it's wrong here

    Incorrect: integer multiplication is not possible on strings; also indexing error remains.

  • Use float(row[1]) * float(row[2]) instead of row['Price'] * row['Quantity'].

    Why this is correct

    Correct: uses integer indices to access list elements and performs float conversion, resolving both the key error and the string multiplication error.

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.