ShareYourGroupShareYourGroupPython

ValueError: invalid literal and other cases

A ValueError means a function received a value of the right type but with unacceptable content. int accepts strings, so int("42") is fine. But int("forty") is a string that does not spell a number, and that is a ValueError.

Cause 1: converting text that is not a number

age = int("fourteen")

What Python says

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

"Invalid literal" means "this text cannot be read as a number in base 10", that is, as an ordinary decimal number. The text in quotation marks at the end of the message is what int was given, which is very useful when it came from a user or a file.

Other strings that int refuses:

print(int("3.7"))

What Python says

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

A decimal point is not allowed in int. Use float("3.7"), and then int or round on the result if you need a whole number.

print(int(""))

What Python says

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

An empty string is what input returns when the user just presses Enter.

Spaces around the number are fine: int(" 42 ") works. But commas are not, so int("1,200") fails. Remove them first with "1,200".replace(",", "").

The fix: check or catch

When the text comes from a person, expect mistakes. Wrap the conversion in try and ask again.

while True:
    try:
        number = int(input("How many? "))
        break
    except ValueError:
        print("Please type a whole number, such as 12.")
print(number + 1)

Output

How many? twelve
Please type a whole number, such as 12.
How many? 12
13

For whole numbers you can also test with isdigit before converting. It returns False for negative numbers and decimals, so try is the more general tool.

for text in ["42", "4.2", "-5", "abc"]:
    print(repr(text), text.isdigit())

Output

'42' True
'4.2' False
'-5' False
'abc' False

Cause 2: unpacking the wrong number of values

name, city = "Asha Verma Ludhiana".split()

What Python says

Traceback (most recent call last):
  File "example.py", line 1, in <module>
    name, city = "Asha Verma Ludhiana".split()
    ^^^^^^^^^^
ValueError: too many values to unpack (expected 2, got 3)

split produced three pieces and there are only two names to receive them. Either the data has an unexpected shape, or you need to split differently. split(maxsplit=1) makes at most two pieces.

day, month, year = [12, 11]

What Python says

Traceback (most recent call last):
  File "example.py", line 1, in <module>
    day, month, year = [12, 11]
    ^^^^^^^^^^^^^^^^
ValueError: not enough values to unpack (expected 3, got 2)

This is the reverse: fewer values than names.

Cause 3: a value that is not in the list

names = ["Asha", "Ravi"]
names.remove("Kabir")

What Python says

Traceback (most recent call last):
  File "example.py", line 2, in <module>
    names.remove("Kabir")
    ~~~~~~~~~~~~^^^^^^^^^
ValueError: list.remove(x): x not in list

remove and index raise a ValueError when the item is missing. Check with if "Kabir" in names: first.

Cause 4: a number outside the allowed range

import math
print(math.sqrt(-9))

What Python says

Traceback (most recent call last):
  File "example.py", line 2, in <module>
    print(math.sqrt(-9))
          ~~~~~~~~~^^^^
ValueError: expected a nonnegative input, got -9.0

The square root of a negative number is not a real number, so math.sqrt refuses. The message says "expected a nonnegative input".

ValueError or TypeError?

The two are easy to mix up.

CallErrorWhy
int("abc")ValueErrora string is allowed, but not this one
int([1, 2])TypeErrora list is never allowed
"5" + 5TypeErrortext and number cannot be added

If the kind of thing is wrong, it is a TypeError. If the kind is right and the particular value is bad, it is a ValueError.

Raising it yourself

When you write a function that cannot work with certain values, raising a ValueError with a clear message is the accepted way to say so. The lesson on try and except shows how.

Other error messages

Examples on this page were run with Python 3.14 on 12 November 2026. The output shown is the real output.

This is an independent learning site. It is not connected with or approved by the Python Software Foundation. "Python" is a trademark of the Python Software Foundation and is used here only to name the language.