PCEP Practice Question: Data Types, Variables, Basic I/O and Operators
A program needs to read a user's age and print a message if they are 18 or older. Which code snippet correctly accomplishes this?
⚠ Common exam trap
Python Institute often tests the distinction between string and integer types, and the trap here is that candidates forget to convert the input to an integer, leading to a type mismatch error when using comparison operators like `>=`.
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
✓
age = int(input('Age: ')) if age >= 18: print('Adult')
It uses `int()` to convert the user input to an integer, then compares it with the integer `18` using the `>=` operator. This ensures a proper numeric comparison, and if the condition is true, it prints 'Adult'.
Answer analysis
Option-by-option breakdown
For each option: why learners choose it and why it is or isn't the right answer here.
- ✗
age = int(input('Age: ')) if age >= '18': print('Adult')
Why it's wrong here
Comparing an integer to the string '18' raises a TypeError in Python 3, so the condition never evaluates correctly. It is tempting because the input is converted with int(), and the snippet would work only if the literal were also written as the integer 18.
- ✓
age = int(input('Age: ')) if age >= 18: print('Adult')
Why this is correct
int(input('Age: ')) converts the string returned by input() into an integer, enabling numeric comparison. The condition age >= 18 then prints 'Adult' when the age is 18 or above, satisfying the stem's requirement of handling users aged 18 or older.
- ✗
age = input('Age: ') if age >= 18: print('Adult')
Why it's wrong here
input() returns a string, and comparing that string to the integer 18 raises a TypeError in Python 3, so the condition fails at runtime. Reading input as text is otherwise fine, but the value must be converted with int() before any numeric comparison against 18.
- ✗
age = input('Age: ') if age == 18: print('Adult')
Why it's wrong here
The equality test only matches exactly 18, so ages 19, 25 or 40 print nothing, contradicting the '18 or older' requirement. Reading input as a string is acceptable if converted, but the operator must be >= against an integer 18, not ==.
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.