How To Fix the Python Error: Typeerror: Int() Argument Must Be a String, a Bytes-Like Object or a Number, Not ‘Nonetype’

The Python language is well known for its data types and flexibility in variable manipulation. However, there are instances where Python’s extensive capabilities there can create some confusing errors. And that’s exactly the case with a ” typeerror: int() argument must be a string, a bytes-like object or a number, not ‘nonetype’ ” error. But it’s generally easy to fix once we’ve clarified some of the more obscure points surrounding it.

What Does the Error Mean?

The Int function tries to convert any passed value into an integer. For example, if you passed a string consisting of “5” it would convert it into an integer whose value is 5. Int is more flexible than you might assume. It can even work with bytes-like objects. But when you see this python error it means the interpreter has encountered a None type. This essentially means that the Int function received a variable with no data within it. Understanding this issue requires a deeper dive into the nature of Python’s types and variables.

Taking a Closer Look at Python’s Less Commonly Used Data Types<

This error tends to be confusing due to the relative scarcity of None types in Python. None is a useful way to signal function termination as it can quite literally show that there’s nothing to return from data that was passed to the function. And None can result from an attempt to get data from a function that doesn’t have a return. But None isn’t typically used when someone is writing more utilitarian code. It can be something of a surprise when an error like this shows up for the first time.

It’s also important to keep in mind that there’s a difference between None and other values and data types. None isn’t a null value. Nor is Null a numerical placeholder like 0. As such, Python’s interpreter will treat None very differently than you might expect. For example, take a look at the following Python code.

ourVar = False
print(type(ourVar))
toInt = int(ourVar)
print(toInt)
ourVar = 0
print(type(ourVar))
toInt = int(ourVar)
print(toInt)
ourVar = None
print(type(ourVar))
toInt = int(ourVar)
print(toInt)

This simple script starts by assigning a False value to ourVar. The script then prints out the result of passing ourVar to the Type function. Next, the script passes ourVar to int and assigns the result to toInt. The toInt variable is then printed to the screen. We repeat this with a 0 and then a None value.

As you might expect, the script exits with the ” int() argument must be a string, a bytes-like object or a number, not ‘nonetype’ ” error message when it tries to convert None into an integer. But it’s important to note that both 0 and False were processed by Int with no errors. The reason becomes more clear when we examine the output of Type. On the surface, False might seem functionally identical to None. But that’s not the case.

Remember that None is a total absence of data. False still provides information, in that it signals something isn’t true. Type reveals this by showing it as a bool. Meaning that it’s part of a notation system that has two possible values. False is a value, while None is an absence of value. And because boolean values translate into 0 and 1, Int is able to convert False into an integer with a value of 0.

We see something similar with an actual 0. This too might seem like it’d be synonymous with None. We usually think of it that way when performing basic math. But 0 isn’t an absence of value. It’s essentially a mathematical placeholder. And it’s also, of course, a standard integer. All of this means that Int has no trouble taking in the 0 value and returning an integer whose value is 0.

It’s only when the script gets to a real value of None that we see our familiar Python error. But at this point, you might wonder where None values come from. There’s a virtually unlimited number of answers to that question. But we can highlight the most common with the following example.

def getOurValue():
&nbsp;&nbsp;&nbsp;&nbsp;x = “999”
&nbsp;&nbsp;&nbsp;&nbsp;print(‘Function Finished’)
&nbsp;&nbsp;&nbsp;&nbsp;return x

returnedValue = int(getOurValue())
print(returnedValue)

In this simple script, we create a function called getOurValue. The function assigns 999 as a string to a variable called x. It then prints out a notification that the function has successfully completed its task. When the script begins it tries to call getOurValue, pass the result to Int, and then assign it to returnedValue. The script should close by printing out returnedValue. However, we receive the typeerror. The error is due to the fact that getOurValue doesn’t actually return anything, not even a 0 or False value.

Fixing the TypeError

Fixing the error is largely dependent on how the None type was generated. The previous example highlights one of the most common causes for this error, a function with no return. And the following example will show how to fix it.

def getOurValue():
&nbsp;&nbsp;&nbsp;&nbsp;x = “999”
&nbsp;&nbsp;&nbsp;&nbsp;print(‘Function Finished’)
&nbsp;&nbsp;&nbsp;&nbsp;return x

returnedValue = int(getOurValue())
print(returnedValue)

The problem with the original code was that we weren’t returning a value when getOurValue was called. The end result is that nothing was passed to Int. In this example, we fixed the original intent by returning the numerical string in x.

But there are also times when we might actually want to use None. In those cases, we can simply test variables before working with them. The following example shows a variation in the previous fix.

def getOurValue():
&nbsp;&nbsp;&nbsp;&nbsp;x = “999”
&nbsp;&nbsp;&nbsp;&nbsp;print(‘Function Finished’)
&nbsp;&nbsp;&nbsp;&nbsp;#return x

returnedValue = getOurValue()
if isinstance(returnedValue, type(None)):
&nbsp;&nbsp;&nbsp;&nbsp;print (“It’s none”)
&nbsp;&nbsp;&nbsp;&nbsp;returnedValue = 0
print(int(returnedValue))

In this example, we comment out getOurValue’s return, which results in a None value for the returnedValue variable. After that assignment, we use isinstance to test its data type. If the type is None we can act to rectify that issue. In our simple example, this just means assigning 0 to returnedValue. The script will run without any error message. And you can uncomment the return statement to see that the script will behave differently now that the None type situation has been rectified.

How To Fix the Python Error: Typeerror: Int() Argument Must Be a String, a Bytes-Like Object or a Number, Not ‘Nonetype’
Scroll to top