[英]Python Syntax Error with test code
This is my 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
whenever I run it in python I get the above error, can anyone identify the issues with my code? 每当我在python中运行它时,我都会收到上述错误,有人可以确定我的代码存在问题吗?
You are closing your print
-statement too early. 您太早关闭了print
语句。
All you want to print has to be between the brackets: 您要打印的所有内容必须在方括号之间:
print("The Unit Price for your " + ItemName + " is: $" + str(UnitPrice))
print("The Total Price for your " + ItemName + " is: $" + str(TotalPrice))
You also need to cast your float
- and int
-objects to string
because you can only concatenate ( +
) string
-objects. 您还需要投你的float
-和int
-objects到string
,因为你只能串联( +
) string
-objects。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.