. Advertisement .
..3..
. Advertisement .
..4..
Amid your program, this error message shows up: Unsupported operand type(s) for +: ‘NoneType’ and ‘str’. What is it, and how can we make it go away? Check out the detailed example below for some practical hints.
Why Does It Occur?
This Python error happens when programmers attempt to use (+) (addition operators) with None values. Here is an example of such errors:
Code:
exp_string1 = None
exp_string2 = 'Python'
result = exp_string1 + exp_string2
print(result)
Output:
Traceback (most recent call last):
File "code.py", line 3, in <module>
result = exp_string1 + exp_string2
TypeError: unsupported operand type(s) for +: 'NoneType' and 'str'
Hence, we need to identify the None value assigned to the variable and correct that assignment.
How to Fix the Error: Unsupported operand type(s) for +: ‘NoneType’ and ‘str’
Suppose we set up a program printing out messages that display data about car shop earnings during the first three months of 2022. This data includes the sales of the car manufacturers that this store distributes.
First, we need to declare dictionaries which store values about the shop’s earnings in the first three months of 2022:
Example 1:
total_revenue = {
"time": "The first three months of 2022",
"Mercedes": 9225000,
"BMW": 10217200,
"Ferrari": 7810000,
"Bentley": 6754500,
"Audi": 6461900,
"Tesla":6049620
}
The car shop owner must observe all these values during the program’s operation. Hence, we will use print statements to navigate the store’s turnover values. Now, let’s adopt five print statements for information displayed in the dictionaries:
Example 1 (continue):
print("Time: ") + total_revenue["time"]
print("Mercedes sales: $") + str(total_revenue["Mercedes"])
print("BMW sales: $") + str(total_revenue["BMW"])
print("Ferrari sales: $") + str(total_revenue["Ferrari"])
print("Bentley sales: $") + str(total_revenue["Bentley"])
print("Audi sales: $") + str(total_revenue["Audi"])
print("Tesla sales: $") + str(total_revenue["Tesla"])
In the next step, we transform floating-point dictionary values into a string. This move prevents possible errors while attempting to concatenate a float and a string. Remember that only strings can concatenate themselves into strings.
Once done, let’s operate the codes and observe what happens next:
Example 1 (Error):
Time:
Traceback (most recent call last):
File "code.py", line 12, in <module>
print("Time: ") + total_revenue["time"]
TypeError: unsupported operand type(s) for +: 'NoneType' and 'str'
As you can see, the code sends back an error.
We cannot deny that the code has successfully printed out the “Time” word on the console. Nevertheless, once you add the turnover value “time” to your “Time” string, the program still stops running.
What is the main issue here? It seems you have concatenated the “time” turnover string outside print() statements. Print() functions return None. Since print() statements precede concatenation operators, the Python program assumes that you want to add values to that.
Example 1 (continue):
print("Time: " + total_revenue["time"])
How can we solve this error? We suggest you move concatenation operations into print() statements:
Example 1 (Solution):
print("Time: " + total_revenue["time"])
print("Mercedes sales: $" + str(total_revenue["Mercedes"]))
print("BMW sales: $" + str(total_revenue["BMW"]))
print("Ferrari sales: $" + str(total_revenue["Ferrari"]))
print("Bentley sales: $" + str(total_revenue["Bentley"]))
print("Audi sales: $" + str(total_revenue["Audi"]))
print("Tesla sales: $" + str(total_revenue["Tesla"]))
A concatenation operator should arrive after string values instead of print statements (whose values you wish to concatenate). Now let’s run this program:
Example 1 (Output):
Time: The first three months of 2022
Mercedes sales: $9225000
BMW sales: $10217200
Ferrari sales: $7810000
Bentley sales: $6754500
Audi sales: $6461900
Tesla sales: $6049620
Conclusion
This article has shown you how to rectify the error: Unsupported operand type(s) for +: ‘NoneType’ and ‘str’ – in Python. For other similar Python errors (such as [ERR_HTTP_HEADERS_SENT]: Cannot set headers after they are sent to the client), feel free to browse through ITtutoria tutorials for more guidance.
Leave a comment