Courseiva

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

    input() reads keyboard data and returns it as a string; it accepts digits without error. It tempts because the resulting value is text, but the function itself reads numbers fine — the missing int() conversion causes the TypeError.

  • ✗

    The variable age is automatically converted to int.

    Why it's wrong here

    input() never converts automatically; it always returns a str, so age stays a string. It tempts because conversion is what the script needs, but Python performs none implicitly — the missing int() call is the root cause.

  • ✓

    The variable age is a string, not an integer.

    Why this is correct

    input() always returns a string in Python 3, so age holds text like "18". Comparing a str with an int using > raises TypeError, since Python won't implicitly coerce types. Converting with int(age) before the comparison resolves it.

  • ✗

    The comparison operator > is not valid for strings.

    Why it's wrong here

    The `>` operator does work on strings, comparing them lexicographically, so no TypeError arises from the operator itself. The real cause is that `input()` returns a string, and comparing that string to the integer `18` raises the TypeError. This option is tempting because string-to-string comparison genuinely uses `>`.

About these practice questions

This PCEP question is part of Courseiva's 482-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 →

How Courseiva writes practice questions · Editorial policy

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.