【发布时间】:2011-06-27 14:05:29
【问题描述】:
我正在阅读这本名为“Python for software design”的 Python 书籍,其中包含以下练习:
假设一本书的封面价格是 24.95 美元,但书店获得了 40% 的折扣。 第一份运费为 3 美元,每增加一份运费为 75 美分。什么 是 60 份的总批发成本
好的,我有以下代码:
bookPrice = 24.95
discount = .60
shippingPriceRest = .75
shippingPriceFirst = 3.00
totalUnits = 60
bookDiscountAmount = bookPrice * discount * totalUnits
shipping = shippingPriceRest * 59 + shippingPriceFirst
result = bookDiscountAmount + shipping
print 'The total price for 60 books including shipping and discount is: '
print 'Total price of the books is: ' + str(bookDiscountAmount)
print 'Total Shipping is: ' + str(shipping)
print 'The Total price is: ' + str(result)
我得到以下结果:
- 60本书的总价包括运费和折扣是:
- 图书总价:898.2
- 总运费为:47.25
总价为:945.45
-
我的问题是:
- 这是正确的吗?
- 我怎样才能使这段代码更好?
【问题讨论】:
-
更多的是基础数学而不是 Python...
-
是的,我正在尝试学习语法、变量以及如何处理它们。
-
一个建议:不要在金钱上使用浮点数。以美分记录所有价格。用美分进行所有计算。输出时转换为美元。
-
是的,我读到浮点数不是很精确
标签: python