{ }codelper

Error Messages › Python

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)
Input
3 5
When you run it
Traceback (most recent call last):
  File "main.py", line 1, in <module>
ValueError: invalid literal for int() with base 10: '3 5'

Edit and run it →

Fixed code

Split at the spaces with split(), then convert the pieces one by one. map(int, ...) runs int() on each piece.

a, b = map(int, input().split())print(a + b)
Input
3 5
Output
8

Edit and run it →

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)
Input
3.5
When you run it
Traceback (most recent call last):
  File "main.py", line 1, in <module>
ValueError: invalid literal for int() with base 10: '3.5'

Edit and run it →

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).

x = float(input())print(x * 2)print(int(x))
Input
3.5
Output
7.0
3

Edit and run it →

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)
Input
3

5
When you run it
Traceback (most recent call last):
  File "main.py", line 2, in <module>
ValueError: invalid literal for int() with base 10: ''

Edit and run it →

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)
Input
3

5
Output
8

Edit and run it →

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)
Input
1,000
When you run it
Traceback (most recent call last):
  File "main.py", line 1, in <module>
ValueError: invalid literal for int() with base 10: '1,000'

Edit and run it →

Fixed code

Remove the characters you do not need with replace() before converting.

price = int(input().replace(",", ""))print(price * 2)
Input
1,000
Output
2000

Edit and run it →

What to check when you see this error

  1. Look inside the quotes at the very end of the error message. That is the string that went into int().
  2. If you see numbers separated by a space, you need split(). If you see a dot, use float(). If it is empty, you read an empty line.
  3. 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().
  4. Add print(repr(value)) above the line where the error happened (the number after line) to make even spaces and newlines visible.

Practice problems

See also