PCEP Practice Question: Data Types, Variables, Basic I/O and Operators
A student is learning Python and writes a program to compute the area of a rectangle. The code:
length = input("Enter length: ") width = input("Enter width: ") area = length * width
print("Area:", area)When the user enters 5 and 3, the program crashes with a TypeError: can't multiply sequence by non-int of type 'str'. The student is puzzled because they thought input returns numbers. What is the correct explanation and fix?
⚠ Common exam trap
Python Institute often tests the misconception that `input()` returns a number when the user types digits, leading candidates to forget explicit type conversion, and they may incorrectly choose options that suggest renaming variables or using `eval()`.
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 input function returns a string, so use int(input("Enter length: ")) and int(input("Enter width: ")).
The `input()` function in Python always returns a string, even if the user types a number. When you try to multiply two strings (or a string by a string), Python raises a `TypeError` because it cannot multiply sequences by non-integers. The fix is to explicitly convert the input to an integer using `int()` before performing arithmetic.
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 returns a string, so use int(input("Enter length: ")) and int(input("Enter width: ")).
Why this is correct
`input()` always returns a `str`, so `length` and `width` hold `"5"` and `"3"`. Multiplying two strings is invalid, hence the `TypeError`. Wrapping each call in `int()` converts the text to integers, satisfying the arithmetic the area calculation requires.
- ✗
The error is due to variable names length and width conflicting with built-in functions; rename them to l and w.
Why it's wrong here
Renaming variables does not affect the TypeError, which arises because input() returns a string and multiplying two strings is invalid. It tempts because shadowing built-ins is a real Python pitfall, but length and width are not built-ins; int() conversion is the actual fix.
- ✗
The print function cannot handle the multiplication result because area is a string; convert area to int.
Why it's wrong here
input() returns a string, so length * width multiplies a str by a str, raising TypeError before print runs; area is never a string. Converting area to int cannot help. The fix is int(input(...)) on both values. The option tempts because print does display strings, but the crash occurs earlier.
- ✗
The error can be fixed by using the eval function to directly evaluate the input as Python code.
Why it's wrong here
eval() would parse the input as Python code, which is a security risk and unnecessary here; the TypeError stems from string operands, not from needing code evaluation. It tempts because eval appears to convert text to numbers, but int() is the correct, safe conversion.
Go deeper
Related to this question
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 →
Same concept, more angles
1 more way 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 developer writes code to calculate the area of a rectangle and prints it. The code is: length = 10 width = 5 area = length + width print('The area is', area) If the width is accidentally assigned a string '5', what error will occur?
easy- A.ValueError
- B.SyntaxError
- ✓ C.TypeError
- D.NameError
Why C: In Python, adding an integer and a string is not a valid operation; it will raise a TypeError because Python cannot implicitly convert the string to an integer for arithmetic. Therefore, the code will raise a TypeError when executed.
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.