我是一名新的编程学生,所以我确定这只是我在看的东西,但我一直收到以下错误:
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))
答案 0 :(得分:2)
您尝试将print
的返回值与某些字符串连接起来。试试这个:
print("Sales store " + str(count + 1) + " " + locale.currency(sales[count], grouping = True))
区别在于第一个print
。在您的初始代码中,您立即关闭“Sales store”之后的paren。 print
返回None,因此您的代码等同于:
None + str(count + 1) + " " + locale.currency(sales[count], grouping=True)
替换版本会将一个大字符串构建为print
的单个参数。