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

The error “ValueError: Invalid literal for int with base 10” is caused by a parsing error in the function int() with a string argument which can not be parsed as an integer. The default base for the int() function is 10. The digits should be between 0 and 9. The integer can also have negative numbers.

The int() function is used to convert a string containing an integer value to an integer value. The string argument should have a valid integer value. The error is thrown if the string does not contain a valid integer value.

The error “ValueError: Invalid literal for int with base 10” is thrown due to some error that occurred while parsing the string into an integer value in the int() function. If the string is empty or contains characters, the string cannot be converted as an integer.



Exception

The error “ValueError: Invalid literal for int with base 10” will be shown as below the stack trace.

Traceback (most recent call last):
  File "/Users/python/Desktop/test.py", line 1, in <module>
    print int('')
ValueError: invalid literal for int() with base 10: ''
[Finished in 0.1s with exit code 1]


Root Cause

The String argument in the int() function contains a non integer value. The int() function parses the string value to an integer value. There is a parsing error because the string does not have a valid integer value. This parsing error causes this “ValueError: invalid literal for int() with base 10” error.



Valid arguments in int() function

The following are the valid arguments for int() function. If you use one of the following, the error will not be thrown.

int() function with no argument – The default int() function which has no argument passed returns default value 0.

print int()    # returns 0

int() function with an integer value – If an integer value is passed as an argument in int() function, returns the integer value.

print int(5)   # returns 5

int() functions with a string containing an integer value – If a string having an integer value is passed as an argument in int() function, returns the integer value.

print int('5')   # returns 5

int() function with a float value – If a float value is passed as an argument in int() function, returns the integer part of the value.

print int(5.4)   # returns 5

int() function with a boolean value – If a boolean value is passed as an argument in int() function, returns the integer value for the boolean value.

print int(True)   # returns 1


Invalid arguments in int() function

int() function with an empty string – The empty string can not be parsed as an integer value

print int('')     # throws ValueError: invalid literal for int() with base 10: ''

int() function with a string having a float value – If a string having a float value is passed as an argument, int() function will throw value error.

print int('5.4')     # throws ValueError: invalid literal for int() with base 10: '5.4'

int() function with a non-integer string – If a string contains a non-integer values such as characters and passed as an argument, the int() function will throw value error.

print int('q')     # throws ValueError: invalid literal for int() with base 10: 'q'


Solution 1

If the string contains a float value that is passed as an argument in the int() function, the function will throw the value error. The string is to be converted as a float value, then you can pass it as an argument in the int() function. This will resolve the value error.

Program

x='5.4'
print int(x)

Output

Traceback (most recent call last):
  File "/Users/python/Desktop/test.py", line 3, in <module>
    print int(x)
ValueError: invalid literal for int() with base 10: '5.4'
[Finished in 0.1s with exit code 1]

Solution

x='5.4'
print int(float(x))

Output

5
[Finished in 0.0s]


Solution 2

The string value should be checked whether it is a number or not using buid-in isdigit() function. If a string contains a number, it is passed as an argument in the int() function. Otherwise, the user will be shown an error message.

Program

x='5.4'
print int(x)

Output

Traceback (most recent call last):
  File "/Users/python/Desktop/test.py", line 3, in <module>
    print int(x)
ValueError: invalid literal for int() with base 10: '5.4'
[Finished in 0.1s with exit code 1]

Solution

x='5.4'
if x.isdigit():
	print "Integer value is " , int(x)
else :
	print "Not an integer number"

Output

Not an integer number
[Finished in 0.0s]


Solution 3

If a string occasionally contains a non-integer number, the build-in function isdigit() is not a good option. In this case, try catch method will solve this issue. If the string contains an integer, it will be executed in the try block. Otherwise, an error message will be shown to the user in the error block.

Program

x='5.4'
print ("Integer value is " , int(x))

Output

Traceback (most recent call last):
  File "/Users/python/Desktop/test.py", line 3, in <module>
    print int(x)
ValueError: invalid literal for int() with base 10: '5.4'
[Finished in 0.1s with exit code 1]

Solution

x='5.4'
try:
	print ("Integer value is " , int(x))
except:
	print "Not an integer number"

Output

Not an integer number
[Finished in 0.0s]



Related Articles

Leave a Reply

Your email address will not be published. Required fields are marked *