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 is dangerous and not recommended.
- ✗
Change to: total = total + line.strip()
Why it's wrong here
This still tries to add a string to a float.
- ✓
Change to: total = total + float(line.strip())
Why this is correct
float() converts the string to a number.
- ✗
Change to: total = total + int(line)
Why it's wrong here
int() fails if the number has a decimal point.
Go deeper
Related to this question
About these practice questions
This PCEP question is part of Courseiva's 498-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.