Exceptions & Files · Question 2 · Easy

Which exception does Python raise when int() is given text that is not a whole number, as below?

n = int("12a")
  1. TypeError
  2. ValueError
  3. NameError
  4. SyntaxError
Answer and explanation

Answer: B. ValueError

Explanation: The argument is of the right type (a string) but its content cannot be read as an integer. A right type with an unacceptable value is reported as ValueError.

Previous: A division by zero happens inside a try block that has a matching except… · Next: In a try statement, when does the code inside the finally block run?

Practise exceptions & files

Try a fresh set of exceptions & files questions with an explanation after every answer, free and without signing up: Exceptions & Files practice. Or sit a full placement mock test.

More exceptions & files questions

More python practice

Built-ins & Standard Library · Variables, Types & Operators · Python Strings · Lists & Tuples · Dictionaries & Sets · Conditions & Loops · Functions & Scope · Comprehensions & Generators

LogicWiz Exams home · All practice topics · Placement mock tests · Daily 10 challenge