Python语法错误与测试代码

时间:2014-09-20 18:17:29

标签: python

这是我的代码:

ItemName = input("Enter your item name: ")
PricePerPound = float(input("Enter the Price per pound: "))
ItemWeightPounds = float(input("Enter the weight of the item in pounds: "))
ItemWeightOunces = float(input("Enter the weight of the item in Ounces: "))
UnitPrice = (PricePerPound/16)
TotalPrice = (PricePerPound * ItemWeightPounds + ItemWeightOunces/16)
print("The Unit Price for your " + ItemName + " is: $" + UnitPrice)
print("The Total Price for your " + ItemName + " is: $" + TotalPrice)

Traceback (most recent call last):
print("The Unit Price for your " + ItemName + " is: $" + UnitPrice)
TypeError: Can't convert 'float' object to str implicitly

每当我在python中运行它时,我都会收到上述错误,是否有人可以识别我的代码问题?

1 个答案:

答案 0 :(得分:0)

您过早关闭print语句。

您要打印的所有内容都必须位于括号之间:

print("The Unit Price for your " + ItemName + " is: $" + str(UnitPrice))
print("The Total Price for your " + ItemName + " is: $" + str(TotalPrice))

您还需要将float - 和int - 对象投射到string,因为您只能连接(+string - 对象。< / p>