Saturday, February 6, 2021

Python Pool: Invalid literal for int() with base 10 | Error and Resolution

Hello coders!! In this article, we will be learning about ‘invalid literal for int() with base 10’. The causes of this error, we will also run how to resolve such errors. Let us see what causes this error.

Cause of invalid literal for int() with base 10 error:

ValueError in Python occurs when we one passes an inappropriate argument type. Invalid literal for int() with base 10 error is caused by passing an incorrect argument to the int() function. A ValueError is raised when we pass any string representation other than that of int.

Let us understand it in detail!

ValueError: invalid literal for int() with base 10

This error message informs that there is an invalid literal for an integer in base 10. This error means that the value that we have passed cannot be converted.

Let us consider an example:

int('1.9') 
Invalid literal for int() with base 10 exampleOutput

It may happen that we can think that while executing the above code, the decimal part,i.e, ‘.9’ will be truncated giving the output 1. However, this does not happen as the int( ) function uses the decimal number system as its base for conversion. This means that the default base for the conversion is 10. In the decimal number system, we have numbers from 0 to 9. Thus, int() with base = 10 can only convert a string representation of int and not floats or chars.

Let us see a few examples where this error can occur:

Example 1:

print(int("pythonpool"))
example 1

In this example the value “pythonpool” is a string value passed to the int() method which gives rise to the error.

Example 2:

print(int("41.1"))
example 2

As the value we have used here is float inside string, this gives rise to invalid literal for int() error.

Example 3:

print(int("[12]"))
example 3 Invalid literal for int() with base 10

We get error in this example as we have used list inside string.

Example 4:

print(int("(12)"))
invalid literal example 4

The error invalid literal for int() arises because we have used tuple inside string.

Example 5:

print(int("{12:1}"))
dictionary inside string which gives rise to the error

Here, we have used dictionary inside string which gives rise to the error.

Example 6:

print(int(""))
invalid literal for int() with base 10 example 6

The error arises in this code as we have used the empty string in the int() method.

Resolution to the Error: invalid literal for int() with base 10:

Using float to avoid decimal numbers:

print(int(float('1.9'))) 
1

Here, we first converted the string representation into float using the float() method. We then used the int() method to convert it into an integer.

using try-catch: to resolve invalid literal for int() with base 10

try:
    x=int("12.1")
except:
    print("Error in converting to string")
Error in converting to string

Here we have used the try-catch method to rid the invalid literal for int() with base 10 error. Basically, if the error occurs inside the try block, it is caught in the catch block, thus preventing the error.

Using isdigit():

x="12"
if x.isdigit():
    x=int(x)
    print(type(x))
<class ' int '>

In this method, we first make sure that the content inside the string is integer using the isdigit() method. As a result, the error does not occur.

Conclusion:

With this, we come to an end to this article. This was an easy way to get rid of the value error in Python. If the above method does not work, then one must if the string and make sure that it does not contain any letter.

However, if you have any doubts or questions, do let me know in the comment section below. I will try to help you as soon as possible.

Happy Pythoning!

The post Invalid literal for int() with base 10 | Error and Resolution appeared first on Python Pool.



from Planet Python
via read more

No comments:

Post a Comment

TestDriven.io: Working with Static and Media Files in Django

This article looks at how to work with static and media files in a Django project, locally and in production. from Planet Python via read...