2014-09-20 25 views
0

這是我的代碼: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我得到上述錯誤,任何人都可以找出我的代碼的問題是什麼?

回答

0

您正在關閉print-陳述過早。

所有你想打印有可能成爲括號:

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

您還需要投你float - 和int -objects到string因爲你只能串聯(+string -objects。

+0

我已編輯,這是我現在得到TypeError的錯誤:不能將'float'對象隱式轉換爲str我的最後兩行:print(「您的」+ ItemName +「的單價是:」+ UnitPrice) print(「您的」+ ItemName +「的總價格爲:」「+ TotalPrice) – Atom 2014-09-20 18:27:20

+0

我編輯了我的答案。 – Leistungsabfall 2014-09-20 18:27:43

+0

啊完美,比你!我不知道我必須添加str。 – Atom 2014-09-20 18:30:53