Skip to content
Worked Examples · Example 3.11

Q.age = int(input("Enter your age "))
if age >= 18: # use ':' to indicate end of condition.
print("Eligible to vote")

Gujarat GsebTextbookSubjective· 2mImportance★★★★★est
44% · 11/25 Questions
🔒 Locked · start free trial →

You're viewing a preview — the full solution, concept, methods & PYQ mapping are locked.

Start your 14-day free trial to unlock the full solution →

The input() function in Python always returns a string, so the variable age holds a string like "20", not the integer 20. The comparison "20" >= 18 raises a TypeError because Python cannot compare a string with an integer.

This is a code-analysis / debugging question. The code looks correct at first glance, but it contains a subtle type mismatch that will crash the program at runtime.

The Core Idea: input() Returns a String

The input() function in Python reads whatever the user types as a string — always. Even if the user types a number like 20, Python stores it as the string "20". This is a deliberate design choice: it gives the programmer full control over type conversion.

So when the user runs this code and enters 20, the variable age holds "20" (a string), not 20 (an integer).

What Happens Next

The if condition checks age >= 18. Python tries to compare a string ("20") with an integer (18). In Python 3, comparing a string with an integer raises a TypeError — the program crashes before it can print anything.

Watch out

Classic pitfall: Beginners often forget that input() returns a string. The fix is to wrap the input with int(): age = int(input("Enter your age ")). Without this conversion, any numeric comparison will fail.

Step-by-Step Trace

StepCode ExecutedState of ageWhat Happens
1input("Enter your age ")(user types 20)input() returns the string "20"
2age = "20""20" (string)Assignment completes
3if "20" >= 18:"20" (string)Python tries "20" >= 18
4Comparison fails—TypeError: '>=' not supported between instances of 'str' and 'int'

The program never reaches the print() statement.

Why the Other Options Would Be Wrong (if this were an MCQ) …

Unlock everything free for 14 days

  • Full step-by-step solutions
  • Concept-first explanations
  • Methods, shortcuts & mistakes
  • PYQ mapping + timed mock tests

Full access for 14 days. No credit card required.