PCEP Computer Programming and Python Fundamentals Practice Question
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?
⚠ Common exam trap
The PCEP exam often tests the misconception that `input()` returns a numeric type, leading candidates to forget explicit type conversion and choose option C, which stores the input as a string and causes type-related errors in subsequent operations.
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: "))
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.
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 = str(input("Age: "))
Why it's wrong here
str() converts the input to a string, not an integer, so arithmetic on age would fail; the prompt already returns a string, making str() redundant. It is tempting because str() is the standard way to cast values for display or concatenation, which would be correct if the goal were formatting output rather than numeric conversion.
- ✗
age = float(input("Age: "))
Why it's wrong here
float() returns a floating-point number, so the age variable would hold a decimal value rather than an int, failing any integer-specific operation. Converting with float() is correct when the input genuinely represents a measurement such as height or weight.
- ✗
age = input("Age: ")
Why it's wrong here
input() returns a string, so age would hold text and arithmetic or comparison against integers would raise a TypeError. Leaving input as a string is correct when the value is a name or other non-numeric data that should not be converted.
- ✓
age = int(input("Age: "))
Why this is correct
input() always returns a string, so wrapping it in int() converts the typed characters into an integer before assignment. This satisfies the requirement to store age as a numeric type, enabling arithmetic rather than string concatenation or comparison failures.
Go deeper
Related to this question
About these practice questions
Courseiva writes every PCEP question from scratch — 482 in total, each with an explanation and a wrong-answer breakdown. None are copied from real exams or dumps. 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.