【问题标题】:TypeError: unsupported operand type for NoneType and strTypeError:NoneType 和 str 的操作数类型不受支持
【发布时间】:2013-11-22 00:58:21
【问题描述】:

我是一名新的编程学生,所以我确定这只是我正在寻找的东西,但我不断收到以下错误:

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))

【问题讨论】:

  • 您可能需要编辑错误消息以从文件路径中省略您的全名,并将其替换为虚拟名称。

标签: python string typeerror nonetype


【解决方案1】:

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

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

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

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-12-26
    • 2013-02-20
    • 2018-12-06
    • 1970-01-01
    相关资源
    最近更新 更多