Write Python. Understand every error.

Open the editor

Error guide · ValueError

ValueError: invalid literal for int() with base 10

The type of value is right, but the value itself cannot be used.

Example

text = "12a"
number = int(text)
print(number + 1)

What Python says

ValueError: invalid literal for int() with base 10: '12a'

As printed by Python 3.12. Other versions may word it slightly differently.

Try this code in Py-CRAFT

Why it happens

int() accepts strings, but only strings that look like a whole number. "12a", "3.5" and "" (an empty string, often from pressing Enter without typing) cannot become an int.

How to fix it

text = "12"
number = int(text)
print(number + 1)

Output: 13

Tips

Learn the ideas behind it

Other common errors

Share this page: