TypeError: unsupported operand type(s) for +: ‘NoneType’ and ‘int’

In python, TypeError: unsupported operand type(s) for +: ‘NoneType’ and ‘int’ error occurs when an integer value is added to a variable that is None. You can add an integer number with a another number. You can’t add a number to None. The Error TypeError: unsupported operand type(s) for +: ‘int’ and ‘NoneType’ will be thrown if an integer value is added with an operand or an object that is None.

An mathematical addition is performed between two numbers. It may be an integer, a float, a long number, etc. The number can not be added to the operand or object that is None. None means that no value is assigned to the variable. So if you try to add a number to a variable that is None, the error TypeError: unsupported operand type(s) for +: ‘NoneType’ and ‘int’ will be thrown.

Objects other than numbers can not be used for arithmetic operations such as addition , subtraction, etc. If you try to add a number to a variable that is None, the error TypeError: unsupported operand type(s) for +: ‘NoneType’ and ‘int’ will be displayed. The None variable should be assigned to an integer before it is added to another number.



Exception

The error TypeError: unsupported operand type(s) for +: ‘NoneType’ and ‘int’ will be shown as below the stack trace. The stack trace shows the line where an integer value is added to a variable that is None.

Traceback (most recent call last):
  File "/Users/python/Desktop/test.py", line 3, in <module>
    print x + y
TypeError: unsupported operand type(s) for +: 'NoneType' and 'int'
[Finished in 0.0s with exit code 1]


How to reproduce this error

If you try to add an integer value with a variable that is not assigned to a value, the error will be reproduced. Create two python variables. Assign a variable that has a valid integer number. Assign another variable to None. Add the two variables to the arithmetic addition operator.

x = None
y = 2
print x + y

Output

Traceback (most recent call last):
  File "/Users/python/Desktop/test.py", line 3, in <module>
    print x + y
TypeError: unsupported operand type(s) for +: 'NoneType' and 'int'
[Finished in 0.0s with exit code 1]


Root Cause

In Python, the arithmetic operation addition can be used to add two valid numbers. If an integer value is added with a variable that is assigned with None, this error TypeError: unsupported operand type(s) for +: ‘NoneType’ and ‘int’ will be thrown. The None variable should be assigned a valid number before it is added to the integer value.



Solution 1

If an integer value is added with a variable that is None, the error will be shown. A valid integer number must be assigned to the unassigned variable before the mathematical addition operation is carried out. This is going to address the error.

x = 3
y = 2
print x + y

Output

5
[Finished in 0.1s]


Solution 2

If the variable is assigned to None, skip to add the variable to the integer. If the variable does not have a value, you can not add it. If the variable type is NoneType, avoid adding the variable to the integer. Otherwise, the output will be unpredictable. If a variable is None, use an alternate flow in the code.

x = None
y = 2
if x is not None :
	print x + y
else :
	print y

Output

2
[Finished in 0.1s]


Solution 3

If the variable is assigned to None, assign the default value to the variable. For eg, if the variable is None, assign the variable to 0. The error will be thrown if the default value is assigned to it. The variable that is None has no meaning at all. You may assign a default value that does not change the value of the expression. In addition, the default value is set to 0. In the case of multiplication, the default value is 1.

x = None
y = 2
if x is None :
	x = 0
print x + y

Output

2
[Finished in 0.1s]



Related Articles

Leave a Reply

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