PCEP Computer Programming and Python Fundamentals Practice Question
A team is developing a script that processes user input. They want to ensure that if the user enters a non-numeric value when asked for age, the program does not crash. Which approach should they use?
⚠ Common exam trap
The PCEP exam often tests the misconception that type checking after `input()` can prevent crashes, but candidates forget that `input()` always returns a string, making type checks like `isinstance()` useless without conversion, and that `isdigit()` is insufficient for numeric validation beyond simple positive integers.
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 int(input()) within a try-except block
Wrapping `int(input())` in a `try-except` block catches the `ValueError` that occurs when `int()` receives a non-numeric string. This prevents the program from crashing and allows graceful handling of invalid input, which is the standard Pythonic approach for robust user input validation.
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 raw_input() and then int()
Why it's wrong here
raw_input() returns a string, so int() on non-numeric input still raises ValueError and crashes the script. It is tempting because raw_input() plus int() is the standard idiom for reading integers, and would be correct if the input were guaranteed numeric or wrapped in a try/except block.
- ✗
Use input() with a type check after input
Why it's wrong here
Checking the type after input() still requires the conversion to have succeeded, so a non-numeric string raises ValueError before the check runs. It suits validating already-typed values, whereas try/except around int() catches the conversion failure directly.
- ✓
Use int(input()) within a try-except block
Why this is correct
int() raises a ValueError when the input cannot be parsed as an integer, so wrapping the conversion in try-except lets the script catch that exception and handle it gracefully instead of terminating. This directly satisfies the requirement that non-numeric input must not crash the program.
- ✗
Use a while loop to check if input.isdigit()
Why it's wrong here
isdigit() returns False for negative numbers and decimals like "3.5", rejecting valid ages, and a while loop alone never converts the string. It suits strictly non-negative integer input, while try/except around int() handles the full numeric range.
Go deeper
Related to this question
About these practice questions
One of 482 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.