PCEP Practice Question: Data Types, Variables, Basic I/O and Operators
A company needs to process user input that must be a whole number between 1 and 100. Which code snippet correctly validates and converts the input?
⚠ Common exam trap
Python Institute often tests the misconception that `int(input(...))` is safe as long as you later check the value, but the trap is that the conversion itself can raise a `ValueError` before any validation occurs, making Options A and B incorrect.
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
✓
num = input("Enter: "); if num.isdigit() and 1 <= int(num) <= 100: ...
It first checks that the input string consists only of digits using `isdigit()`, which prevents `ValueError` from non-numeric input, and then safely converts to `int` only after validation. This ensures the input is a whole number between 1 and 100 before any further processing.
Answer analysis
Option-by-option breakdown
For each option: why learners choose it and why it is or isn't the right answer here.
- ✗
num = int(input("Enter: ")); if 1 <= num <= 100: ...
Why it's wrong here
Raises ValueError if input is not an integer.
- ✗
num = int(input("Enter: ")); if num in range(1,101): ...
Why it's wrong here
Raises ValueError if input is not an integer.
- ✓
num = input("Enter: "); if num.isdigit() and 1 <= int(num) <= 100: ...
Why this is correct
Checks if input is digit first, then converts safely.
- ✗
All of the above are correct.
Why it's wrong here
A and C are flawed.
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.