← Back

Home › Errors › ValueError

ValueError: invalid literal for int() with base 10 — decoded

Exception: ValueErrorCategory: runtimeRaised at: inside the int() call itself

What this message means

A ValueError means: the type was fine, the value was impossible. You handed int() a string — correct type for conversion — but the *characters* inside it cannot be read as a whole number in base 10. int() refuses to guess, and the message quotes the exact text it choked on.

base 10 in the message is just "ordinary decimal numbers". It appears because int() can also parse other bases — int("ff", 16) is a valid 255 — and the error is telling you which base it was using when the text failed.

Traceback (most recent call last):
  File "main.py", line 1, in <module>
    int("12.5")
        ^^^^^^^
ValueError: invalid literal for int() with base 10: '12.5'

What int() accepts, and what it refuses

int("42")       # 42    — digits, optional sign, optional spaces
int(" -7 ")     # -7    — surrounding whitespace is fine
int("42 kg")    # ValueError — trailing text
int("12.5")     # ValueError — a decimal point is not an integer
int("")         # ValueError — nothing to read
int("1,200")    # ValueError — the comma is not a digit

The list is deliberately strict: only characters that can be part of a decimal integer. One stray space inside, one comma, one decimal point, one unit — and the whole conversion fails. Note the surprise: int(12.5) works (it truncates to 12) but int("12.5") does not — a *string* has to be read, and a number that is already a number is just truncated.

A real example

weight = input("Weight in kg: ")    # user types: 62.5
kilos = int(weight)                # ValueError: invalid literal for int() with base 10: '62.5'

The user typed a sensible weight — the bug is the *converter*, not the input. For anything that can hold a fraction, float() is the right tool; int() is for counts of whole things:

kilos = float(weight)   # 62.5 — accepts the decimal point
grams  = int(kilos * 100)  # 6250 — convert deliberately, after the maths

The fixes, from quick to robust

  1. Clean first, convert second. text.strip() removes stray spaces; text.replace(",", "") rescues thousands separators.
  2. Pick the right converter. float() for measurements, int() only for whole counts. You can truncate or round afterwards, deliberately.
  3. Guard the conversion. if text.strip().lstrip("-").isdigit(): accepts only clean integers before calling int().
  4. Or forgive the failure. Wrap it in try/except ValueError and re-ask, so one bad keystroke cannot kill the program.
while True:
    text = input("How many? ").strip()
    try:
        count = int(text)
        break
    except ValueError:
        print(f"{text!r} is not a whole number — try again.")

Why this error is a ValueError and not a TypeError

Compare the two siblings: int(12.5) — a float — is a *type* question, but Python answers it by truncating. int("12.5") — a string — has the *type* the function asked for, and still fails on the *content*. That is the dividing line: TypeError = wrong kind of thing; ValueError = right kind of thing, impossible value. Reading which one you got tells you whether to convert the type or clean the data.

Related messages worth knowing

Fix it interactively

These free problems practice safe conversion — each one is a real int()/float() trap to repair in the browser:

Practice more Python bugs →
🇮🇳 Hindi में समझें

ValueError का मतलब: type ठीक था, value impossible थी। int() को string देना सही है, पर उस string के अंदर ऐसे characters होने चाहिए जो पूरा decimal number बना सकें — सिर्फ digits, माइनस sign और किनारों के spaces। "12.5" (decimal point), "" (खाली), "42 kg" (unit), "1,200" (comma) — सब fail होते हैं। Fix: दशमलव के लिए float() लो, पहले .strip() करो, और user से आने वाले data को try/except ValueError में लिखो ताकि एक गलत keystroke पूरा program बंद न कर दे।

पूरी Hindi explanation पढ़ें →