Python ValueError: invalid literal for int()
ValueError: invalid literal for int() with base 10: '3 5'
int() can only convert a string that looks like an integer, such as "42". If any other character is mixed in, you get this error. The text in quotes at the very end of the error message is the string that actually went into int(), so read that first and the cause is usually obvious.
Cause 1. There are two or more numbers on one line
input() reads a whole line. If the input is 3 5, you get "3 5", and the space in the middle means it cannot become an integer. If you see numbers separated by a space inside the quotes, this is your case.
Wrong code
a = int(input())b = int(input())print(a + b)3 5
Traceback (most recent call last): File "main.py", line 1, in <module> ValueError: invalid literal for int() with base 10: '3 5'
Fixed code
Split at the spaces with split(), then convert the pieces one by one. map(int, ...) runs int() on each piece.
If you do not know how many numbers there are, read them into a list: nums = list(map(int, input().split())).
Cause 2. You tried to convert a decimal with int()
int() turns the number 3.5 into 3, but it cannot convert the string "3.5". The dot does not look like part of an integer.
Wrong code
x = int(input())print(x * 2)3.5
Traceback (most recent call last): File "main.py", line 1, in <module> ValueError: invalid literal for int() with base 10: '3.5'
Fixed code
If a decimal can come in, convert with float(). If you need an integer, convert with float() first and then wrap that in int() (the part after the decimal point is dropped).
Cause 3. You read an empty line
If there is nothing inside the quotes (''), you tried to convert an empty string. This happens when there is an empty line in the middle of the input, or when your count of the lines is off by one.
Wrong code
a = int(input())b = int(input())print(a + b)3 5
Traceback (most recent call last): File "main.py", line 2, in <module> ValueError: invalid literal for int() with base 10: ''
Fixed code
Count again which line of the input holds what. If the input may contain empty lines, keep reading the next line as long as the line is empty.
a = int(input())line = input()while line.strip() == "": line = input()b = int(line)print(a + b)3 5
8
int() strips spaces around the number by itself, so " 5 " converts fine. It only fails when there are only spaces or nothing at all.
Cause 4. Non-digit characters are mixed in
If there is even one thing that is not a digit, like the comma in 1,000, the unit in 25kg or the sign in $5, the string cannot be converted.
Wrong code
price = int(input())print(price * 2)1,000
Traceback (most recent call last): File "main.py", line 1, in <module> ValueError: invalid literal for int() with base 10: '1,000'
Fixed code
Remove the characters you do not need with replace() before converting.
What to check when you see this error
- Look inside the quotes at the very end of the error message. That is the string that went into
int(). - If you see numbers separated by a space, you need
split(). If you see a dot, usefloat(). If it is empty, you read an empty line. - Read the problem's input format again, and match what comes on each line, and how many, with the number of times you call
input(). - Add
print(repr(value))above the line where the error happened (the number afterline) to make even spaces and newlines visible.
Practice problems
See also
- Python ValueError: not enough values to unpack
- Python EOFError: EOF when reading a line
- Python TypeError: unsupported operand type(s)
- Syntax: Python input(): Read User Input
- Syntax: Python split(): Split a String into a List
- Learn it step by step: Python Reading input
- All Python error messages
- Online Python interpreter