-1

TypeError:无法将“浮动”对象隐式转换为 str

print("\nYou will make {:.2f}".format(score * live - BIN))
4

3 回答 3

1

您的(score + power)表达式产生一个float值,并且您试图将其连接到一个带有+. 您不能这样做,因为这需要将值隐式转换为字符串。

打印时使用逗号:

print("\nYou will get", (score + power) - BIN)

并让print()函数为您转换它,或使用字符串格式(这使您可以更好地控制浮点数的格式):

print("\nYou will get {:.2f}".format((score + power) - BIN))

或者,将其应用于整个程序:

BIN = float(input("\nEnter the buy-it-now price of the item: £"))
Postage = float(input("\nEnter the shipping & handling cost of the item: £"))

eBayFee = (BIN + Postage) / 10
PayPalFee = ((3.4 * BIN) / 100) + 0.2

print ("\nYou will be charged £{:.2f} eBay fees and £{:.2f} PayPal fees.".format(eBayFee, PayPalFee))

print("\nYou will make {:.2f}".format(BIN - eBayFee - PayPalFee))

请注意,这是将数字四舍五入到小数点后 2 位的格式。我还更正了公式;大概“利润”是“立即购买”价格减去 eBay 和 PayPal 费用,而不是总费用减去 BIN。

于 2013-10-29T17:14:28.873 回答
1

您不能将 float 隐式转换为 str 。:)

您需要将数字部分包装在str. 或者,更好的是,使用字符串格式:

print("\nYou will get {}".format((score + power) - BIN))
于 2013-10-29T17:12:09.403 回答
0

将浮点数转换为字符串。

print("\nYou will get "+  str(score + power - BIN))
于 2013-10-29T17:13:33.530 回答