Python ValueError: right type, impossible value
Exception: ValueErrorCategory: value validationRaised at: runtime
What this error means
The function received an argument of the correct type but with a value it cannot work with. int('abc') is the canonical case: a string is a perfectly legal argument type for int(), but these particular characters do not form a number. That type-vs-value distinction is exactly the line between TypeError and ValueError — and interviewers love asking for it.
The most-searched variant by far is ValueError: invalid literal for int() with base 10: 'abc'. The quoted part shows precisely what int() received, including invisible characters — read it before theorising.
A real example
age_text = input("Your age: ") # user types: twenty five
age = int(age_text)
Traceback (most recent call last):
File "app.py", line 2, in <module>
age = int(age_text)
^^^^^^^^^^^^^
ValueError: invalid literal for int() with base 10: 'twenty five'
What int() accepts is narrow: optional surrounding whitespace, an optional sign, then digits. Nothing else — not '12.5', not '', not '1,000', not '₹500'.
The other famous ValueErrors
too many values to unpack (expected 2)— tuple unpacking where the counts disagree:a, b = 1, 2, 3.not enough values to unpack— the same disagreement in the other direction.list.remove(x): x not in list/substring not found— removal or.index()by a value that is absent.math domain error—math.sqrt(-1): a float, but outside the function's domain.
How to debug it
- Read the rejected value in the message —
repr-style quoting exposes empty strings, spaces, and stray symbols. - Trace where that value came from: user input, a file, an API. The fix usually belongs at that boundary, not at the conversion.
- Clean deliberately:
text.strip()for whitespace; a policy decision (not a reflex) for decimals —int(float(s))truncates. - Guard the conversion, narrowly:
try: n = int(s) except ValueError: …— around one line, catching one type, with a real fallback plan. - For unpacking errors, count both sides of the
=; a starred target (first, *rest = items) absorbs variable length.
Fix it interactively
Practice more Python bugs →🇮🇳 Hindi में समझें
Type सही था, value गलत — int("abc") में string देना allowed है पर "abc" number नहीं बनता। int() सिर्फ digits (और +/− sign) मानता है; "12.5" भी fail होगा। User input convert कर रहे हो तो सिर्फ conversion वाली line को try/except ValueError में रखो।
Related errors
- TypeError — wrong type entirely — int(None) is a TypeError, int('abc') a ValueError.
- ZeroDivisionError — a legal int that happens to be zero in the denominator.
More conversion-adjacent failures in the full errors guide.