添加链接
link管理
链接快照平台
  • 输入网页链接,自动生成快照
  • 标签化管理网页链接
At Career Karma, our mission is to empower users to make confident decisions by providing a trustworthy and free directory of bootcamps and career resources.

Career Karma receives compensation from our bootcamp partners who are thoroughly vetted before being featured on our website. This commission is reinvested into growing the community to provide coaching at zero cost to their members.
print("Month: ") + turnover["month"]  
print("Net income: $") + str(turnover["net_income"])
print("Gross income: $") + str(turnover["gross_income"])
print("Highest sale: $") + str(turnover["highest_sale"])
print("Average sale: $") + str(turnover["average_sale"])

We convert all of the floating-point values in the dictionary to a string . This prevents an error that occurs when you try to concatenate a string and a float. As discussed earlier, only strings can be concatenated to strings. Next, run the code and see what happens:

Month
Traceback (most recent call last):
  File "main.py", line 9, in <module>
	     print("Month") + turnover["month"]  
TypeError: unsupported operand type(s) for +: 'NoneType' and 'str'

The code returns an error.

The Solution

The code successfully prints out the word “Month” to the console. The program stops working when you try to add the value of turnover[“month”] to the “Month” string.

The issue is that you’re concatenating the turnover[“month”] string outside of the print() statement. The print() function returns None. Because your concatenation operator comes after the print() statement, Python thinks that you are trying to add a value to it:

print("Month") + turnover["month"]  

To solve this error, we must move the concatenation operation into the print() statements:

print("Month: " + turnover["month"])
print("Net income: $" + str(turnover["net_income"]))
print("Gross income: $" + str(turnover["gross_income"]))
print("Highest sale: $" + str(turnover["highest_sale"]))
print("Average sale: $" + str(turnover["average_sale"]))

Concatenation operators should always come after a string value, not the print() statement whose value you want to concatenate. Let’s execute the program:

Month: July
Net income: $7203.97
Gross income: $23821.3
Highest sale: $320.0
Average sale: $45.0

Our code successfully executes all of the print() statements. Each print statement contains a label to which a value from the “turnover” dictionary is concatenated.

Conclusion

The “TypeError: unsupported operand type(s) for +: ‘nonetype’ and ‘str’” error is raised when you try to concatenate a value equal to None with a string. This is common if you try to concatenate strings outside of a print() statement.

To solve this error, check the values on both sides of a plus sign are strings if you want to perform a concatenation operation.

Now you’re ready to fix this error in your Python program like a professional coder!

Leave a Reply Cancel reply

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