PCEP Computer Programming and Python Fundamentals Practice Question
You are a junior developer at a small startup. Your team has a Python script that automates daily data processing. The script reads a CSV file, processes each row, and writes results to a new file. Recently, the script started crashing with a 'ValueError: invalid literal for int()' error. The error occurs on a line that converts a field to an integer using int() on a string value. The CSV file comes from an external source that sometimes contains non-numeric values like 'N/A' or empty strings. Which course of action is best to handle this robustly without stopping the entire process?
⚠ Common exam trap
The PCEP exam often tests the misconception that logging or pre-processing (like regex) is sufficient to prevent runtime errors, when in fact only exception handling can gracefully recover from an exception that has already been raised.
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
✓
Wrap the conversion in a try-except block and handle the exception appropriately for each row.
Wrapping the conversion in a try-except block allows the script to catch the ValueError for each row individually, log or handle the problematic row (e.g., skip it or use a default value), and continue processing the remaining rows without crashing. This is the standard Pythonic approach for handling expected but unpredictable data quality issues in external input, as it separates error handling from the main logic and preserves the robustness of the batch process.
Answer analysis
Option-by-option breakdown
For each option: why learners choose it and why it is or isn't the right answer here.
- ✓
Wrap the conversion in a try-except block and handle the exception appropriately for each row.
Why this is correct
A try-except around int() catches ValueError for each non-numeric field such as 'N/A' or empty strings, letting the loop skip or substitute that row and continue. This satisfies the requirement to handle bad data robustly without stopping the entire process.
- ✗
Add logging before the conversion to print the problematic value.
Why it's wrong here
Logging records the offending value but the int() call still raises ValueError, halting the loop. Logging serves diagnostics and auditing, not control flow. Robust handling requires catching the exception or validating the field before conversion, so processing continues for remaining rows.
- ✗
Use a regex to replace all non-digit characters before conversion.
Why it's wrong here
Stripping non-digits turns 'N/A' into an empty string, which int() still rejects, and corrupts values like '12.5' or negative numbers. Regex substitution suits format normalisation of known patterns. The scenario needs explicit validation or exception handling around the conversion instead.
- ✗
Contact the external source to ensure no missing values are sent.
Why it's wrong here
Contacting the source cannot guarantee clean input, and the script still crashes on any 'N/A' or empty string already received. Supplier negotiation suits contractual data-quality governance, not runtime resilience. The code itself must tolerate malformed values without terminating the batch.
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 →
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.