PCEP-30-02 · Question #235
QUESTION 262 What is the output of the following code? try: value = input("Enter a value: ") print(value/value) except ValueError: print("Bad input...") except ZeroDivisionError: print("Very bad…
The correct answer is A. 1 | Very very bad input. Option A is correct because input() in Python 3 always returns a string, so when the code executes value/value, it is attempting to divide a string by a string - an operation Python does not support, which raises a TypeError. Since TypeError is explicitly caught before the…
Question
Options
- A1 | Very very bad input...
- B1 | Bad input.
- C1 | Very bad input...
- D1 | Booo!
How the community answered
(21 responses)- A81% (17)
- B10% (2)
- C5% (1)
- D5% (1)
Explanation
Option A is correct because input() in Python 3 always returns a string, so when the code executes value/value, it is attempting to divide a string by a string - an operation Python does not support, which raises a TypeError. Since TypeError is explicitly caught before the generic except clause, "Very very bad input..." is printed. The | in the choices represents two scenarios: the first (1) would occur if the value were a valid non-zero number (as in Python 2 where input() evaluates the expression), while the second shows the Python 3 reality.
Why the distractors are wrong:
- B (ValueError / "Bad input...") -
ValueErroris raised for things likeint("hello"), not for division of incompatible types. - C (ZeroDivisionError / "Very bad input...") -
ZeroDivisionErroronly occurs when dividing a number by zero; you can't even reach that state when dividing strings. - D (generic except / "Booo!") - The generic
exceptwould only fire if no earlier specific handler matched; sinceTypeErroris explicitly listed, it is caught there first.
Memory tip: Think of exception clauses like a chain of security guards - Python checks them in order and stops at the first match. Since TypeError has its own guard before the generic one, it never reaches "Booo!". A key fact to burn in: input() always returns str in Python 3, so arithmetic on its result will almost always cause a TypeError.
Community Discussion
No community discussion yet for this question.