PCEP Practice Question: Data Types, Variables, Basic I/O and Operators
A data scientist needs to read a list of floats from a file, one per line, and compute the sum. The current code:
total = 0.0
with open("data.txt") as f:
for line in f:total = total + line
print(total)
When run, a TypeError occurs: unsupported operand type(s) for +: 'float' and 'str'. The scientist knows that line is a string. Which fix will correctly sum the numbers?
⚠ Common exam trap
Python Institute often tests the candidate's understanding that file input is always a string and must be explicitly converted to a numeric type before arithmetic, and that `strip()` alone does not change the data type.
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
✓
Change to: total = total + float(line.strip())
`float(line.strip())` converts the string read from the file (after removing any surrounding whitespace, including the newline character) into a float, which can then be added to the float variable `total`. The original error occurs because Python does not allow implicit addition of a string to 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.
- ✗
Change to: total = total + eval(line)
Why it's wrong here
eval() executes the line as a Python expression, so it does convert the text to a float and would sum correctly, but it runs arbitrary code from the file, a serious injection risk. It is tempting as a one-line conversion shortcut, yet float(line) achieves the same result safely.
- ✗
Change to: total = total + line.strip()
Why it's wrong here
strip() removes the trailing newline but returns another string, so adding it to a float still raises TypeError. It is tempting because the newline looks like the culprit, yet the operand type is the actual problem; float(line) performs the numeric conversion the sum requires.
- ✓
Change to: total = total + float(line.strip())
Why this is correct
`line` is a string, so adding it to a float raises TypeError. Wrapping it in `float()` converts the stripped text into a numeric value, enabling correct arithmetic. `strip()` removes the trailing newline that `float()` would otherwise reject.
- ✗
Change to: total = total + int(line)
Why it's wrong here
int(line) raises ValueError on decimal values such as 3.14 and silently truncates any that parse, so the sum is wrong for float data. It is tempting because it fixes the TypeError for whole numbers, but the file holds floats, so float(line) is the conversion the scenario demands.
Go deeper
Related to this question
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 →
Same concept, more angles
1 more way this is tested on PCEP
These questions test the same concept from different angles. Work through them to make sure you can recognise it however the exam phrases it.
Variation 1. A junior developer writes a Python script to sum all numbers greater than 10 from a list. The code is: numbers = [5, 12, 8, 15, 3] total = 0 for num in numbers: if num > 10: total = total + 1 print(total) The output is 2, but the expected sum is 27 (12+15). Which change will produce the correct output?
easy- A.Change `total = 0` to `total = []`
- ✓ B.Change `total = total + 1` to `total += num`
- C.Change `if num > 10:` to `if num >= 10:`
- D.Change `for num in numbers:` to `for num in range(numbers):`
Why B: The original code increments `total` by 1 for each qualifying number, counting them instead of summing their values. Changing `total = total + 1` to `total += num` adds the actual number to the accumulator, producing the correct sum of 12 + 15 = 27.
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.