PCEP Practice Question: Data Types, Variables, Basic I/O and Operators
A script uses the input() function to get a user's age: age = input('Enter age: '). Later it computes age > 18. This raises a TypeError. What is the root cause?
⚠ Common exam trap
Python Institute often tests the misconception that `input()` returns a numeric type when the user types a number, or that Python automatically converts strings to integers for comparison, leading candidates to overlook the need for explicit type conversion.
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
✓
The variable age is a string, not an integer.
The `input()` function in Python always returns a string, regardless of what the user types. When the user enters their age, the variable `age` holds a string like '25', not an integer. Comparing a string to an integer with the `>` operator raises a `TypeError` because Python does not automatically convert strings to numbers for comparison.
Answer analysis
Option-by-option breakdown
For each option: why learners choose it and why it is or isn't the right answer here.
- ✗
The input() function cannot read numbers.
Why it's wrong here
It can read any input as string.
- ✗
The variable age is automatically converted to int.
Why it's wrong here
No automatic conversion occurs.
- ✓
The variable age is a string, not an integer.
Why this is correct
input() returns string; convert to int first.
- ✗
The comparison operator > is not valid for strings.
Why it's wrong here
It is valid between strings, but not mixed types.
Go deeper
Related to this question
About these practice questions
This PCEP question is part of Courseiva's 498-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 →
Same concept, more angles
4 more ways this is tested on PCEP
These questions test the same concept from different angles. Work through them to make sure you can recognise it however the exam phrases it.
Variation 1. A Python program prompts the user for their age and stores it in a variable. Which is the correct way to convert the input to an integer?
easy- A.age = str(input("Age: "))
- B.age = float(input("Age: "))
- C.age = input("Age: ")
- ✓ D.age = int(input("Age: "))
Why D: The `input()` function in Python always returns a string, and to perform arithmetic operations or numeric comparisons, the string must be explicitly converted to an integer using the `int()` constructor. This is a fundamental requirement for type conversion in Python when handling user input.
Variation 2. A program prompts a user for their age using input(). Which line of code correctly stores the age as an integer?
easy- A.age = str(input("Enter age: "))
- B.age = float(input("Enter age: "))
- ✓ C.age = int(input("Enter age: "))
- D.age = input("Enter age: ")
Why C: The `int()` function explicitly converts the string returned by `input()` into an integer, which is required for storing a numeric age value. The `input()` function always returns a string in Python, so without conversion, arithmetic operations would fail or produce unexpected results.
Variation 3. A developer needs to read an integer from user input and store it. Which code snippet accomplishes this?
medium- ✓ A.x = int(input())
- B.x = read_int()
- C.x = input(int())
- D.x = int(input)
Why A: `int(input())` first calls `input()` to read a string from the user, then passes that string to `int()` to convert it to an integer. This is the standard Python pattern for reading an integer from standard input.
Variation 4. What function is used to read input from the user in Python 3?
easy- A.read_input()
- B.sys.stdin.readline()
- ✓ C.input()
- D.raw_input()
Why C: In Python 3, the built-in `input()` function reads a line from standard input and returns it as a string. This is the standard and simplest way to capture user input, replacing the Python 2 `raw_input()` function.
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.