PCEP Practice Question: Data Types, Variables, Basic I/O and Operators
A system administrator wrote a Python script to monitor disk usage. The script reads the output of a system command that returns a string like 'Used: 45%' and extracts the percentage. The code uses slicing to get the numeric part and converts to int. However, on some servers, the output format changes to 'Used: 45.2%', causing a ValueError when converting to int. The administrator needs a robust solution that works with both integer and floating-point percentages while still producing an integer result (e.g., 45 for 45.2%). Which option is the best approach?
⚠ Common exam trap
Python Institute often tests the distinction between int() and float() conversion behavior, and the trap here is that candidates may overlook that int('45.2') raises a ValueError, leading them to choose a simpler but incorrect approach like direct int conversion after removing '%'.
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 a try-except block to attempt int conversion first; if a ValueError occurs, convert to float and then convert to int (which truncates the decimal part).
It uses a try-except block to first attempt int conversion for integer percentages, and if a ValueError occurs (due to a decimal point), it converts to float and then to int, which truncates the decimal part. This approach handles both '45%' and '45.2%' formats without raising an error, producing an integer result as required.
Answer analysis
Option-by-option breakdown
For each option: why learners choose it and why it is or isn't the right answer here.
- ✗
Use string splitting to extract the numeric part, then convert to float and round to the nearest integer.
Why it's wrong here
Rounding may not match the desired behavior (e.g., 45.2 rounds to 45, but 45.8 rounds to 46). The original intent is to truncate? The problem says 'integer result', but rounding is not exactly truncation.
- ✗
Use a regular expression to extract the number and convert to float, then convert to int.
Why it's wrong here
Regular expressions are outside the PCEP exam scope and may be overkill for this task.
- ✗
Use the .replace() method to remove the '%' character and then convert to int.
Why it's wrong here
This works only for integer percentages; for '45.2%', removing '%' gives '45.2' which cannot be converted to int.
- ✓
Use a try-except block to attempt int conversion first; if a ValueError occurs, convert to float and then convert to int (which truncates the decimal part).
Why this is correct
This handles both integer and floating-point inputs by attempting direct int conversion first, and falling back to float then int truncation.
Go deeper
Related to this question
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 →
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.