python - TypeError:NoneType 和 str 不支持的操作数类型

标签 python string typeerror nonetype

我是一名新的编程学生,所以我确信这只是我忽略的东西,但我不断收到以下错误:

Traceback (most recent call last):
  File "/Users/anonymous/Documents/sales with a list.py", line 11, in <module>
    print("Sales store ") + str(count + 1) + " " + locale.currency(sales[count], grouping = True)
TypeError: unsupported operand type(s) for +: 'NoneType' and 'str'

这是我尝试运行的代码:

import locale
locale.setlocale(locale.LC_ALL, ' ') 

sales = [0.0] * 5
totalSales = float(0.0)
for count in range(5):
    sales[count] = float(input("Enter store sales amount: "))
    totalSales = float(totalSales) + float(sales[count])
for count in range(5):
    print("Sales store ") + str(count + 1) + " " + locale.currency(sales[count], grouping = True)
print("Total Sales of all stores " + locale.currency(totalSales, grouping = True))

最佳答案

您正在尝试将 print 的返回值与一些字符串连接起来。试试这个:

print("Sales store " + str(count + 1) + " " + locale.currency(sales[count], grouping = True))

区别在于第一个打印。在您的初始代码中,您立即关闭“销售商店”后面的括号。 print 返回 None,因此您的代码相当于:

None + str(count + 1) + " " + locale.currency(sales[count], grouping=True)

替换版本会构建一个大字符串作为 print 的单个参数。

关于python - TypeError:NoneType 和 str 不支持的操作数类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20135001/

相关文章:

python - 如何读取带有“?

python - 使用正则表达式删除字符串中的数字

java - 如何查找只有大写字母的单词(字符串)?

ruby - `include?' 中没有将 false 隐式转换为 String>

javascript - 如何修复类型错误: registerUser is not a function

javascript - ReactJS - 无法打印出待办事项,this.props.todos.map 不是函数

python - 如何在表格格式上打印每个 for 循环值?

python - 如何使用纯 Python 和 Cython 版本分发模块

c++ - 学习 CPP : Split string and manipulation?

python - 为什么我收到 AttributeError : __getitem__