簡體   English   中英

測試代碼的Python語法錯誤

[英]Python Syntax Error with test code

這是我的代碼:

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中運行它時,我都會收到上述錯誤,有人可以確定我的代碼存在問題嗎?

您太早關閉了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。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM