python代码的帮助
我正在阅读一本名为python for software design的python书籍,它有以下练习: 假设一本书的封面价格是24.95美元,但书店可以得到40%的折扣。 第一份的运费是3美元,每增加一份75美分。什么 60份的总批发成本是多少 好的,我有以下代码:python代码的帮助,python,Python,我正在阅读一本名为python for software design的python书籍,它有以下练习: 假设一本书的封面价格是24.95美元,但书店可以得到40%的折扣。 第一份的运费是3美元,每增加一份75美分。什么 60份的总批发成本是多少 好的,我有以下代码: bookPrice = 24.95 discount = .60 shippingPriceRest = .75 shippingPriceFirst = 3.00 totalUnits = 60 bookDiscountAmo
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美元
我的问题是:
这是正确的吗?
我怎样才能使这个代码更好?
只有三件事需要改变: 1您复制了图书的数量:60和59都出现在代码中。你不应该有59个 2按如下方式打印结果:打印“总价为:%.2f%”结果
3通常的Python惯例是这样命名变量,而不是这样。我建议的唯一改进是使用format函数而不是字符串连接:
print """The total price for {0:d} books including shipping and discount is:
Total price of the books is: {1:7.2f}
Total Shipping is: {2:7.2f}
The Total price is: {3:7.2f}""".format(totalUnits, bookDiscountAmount
shipping, result)
这使得所有数字都很好地对齐,格式相同,小数点后有两位数字,总精度为7
编辑:当然,正如另一位所指出的,不要在那里硬编码59。我觉得它很合适。我会避免硬编码。相反,检查总数是否不止一个,并根据需要进行划分 此外,这是次要的,但BookDeffortAmount应该是BookDeffortedAmount折扣是他们节省的金额,而不是他们支付的金额。其他人已经指出了如何改进字符串打印。请尝试以下方法:
#######################################################################################
#Your code, commented
#######################################################################################
bookPrice = 24.95
discount = .60
shippingPriceRest = .75
shippingPriceFirst = 3.00
totalUnits = 60
bookDiscountAmount = bookPrice * discount * totalUnits # Poor variable name choice. This is not the book discount amount, it's the cost of all books without shipping
shipping = shippingPriceRest * 59 + shippingPriceFirst # You should really use your variables as much as possible. Instead of 59, use totalUnits - 1
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)
#######################################################################################
#An example of your code, cleaned up
#######################################################################################
bookPrice = 24.95
discount = .60
shippingPriceRest = .75
shippingPriceFirst = 3.00
totalUnits = 60
totalCostBeforeShipping = (bookPrice * discount) * totalUnits
shipping = (shippingPriceRest * (totalUnits-1)) + shippingPriceFirst
result = totalCostBeforeShipping + shipping
print 'The total price for 60 books including shipping and discount is: '
print 'Total price of the books is: ' + str(totalCostBeforeShipping)
print 'Total Shipping is: ' + str(shipping)
print 'The Total price is: ' + str(result)
#######################################################################################
#An example of another method, using a loop
#######################################################################################
bookPrice = 24.95
discount = 40 # %
bookPriceWithDiscount = bookPrice * ((100.0-discount)/100.0)
firstBookShippingPrice = 3
otherBookShippingPrice = .75
totalBooks = 60
totalCost = 0
for book in range(totalBooks):
if book == 0:
totalCost += firstBookShippingPrice
else:
totalCost += otherBookShippingPrice
totalCost += bookPriceWithDiscount
shipping = firstBookShippingPrice + (otherBookShippingPrice * totalBooks)
print 'The total price for 60 books including shipping and discount is:'
print 'Total price per book is: %d'%(bookPriceWithDiscount)
print 'Total shipping is: %d'%(shipping)
print 'The total price is: %d'%(result)
bookCost = 24.95
numBooks = 60.0
def cost(numBooks):
bulkBookCost = ((bookCost * 0.60) * numBooks)
shippingCost = (3.0 + (0.75 * (numBooks - 1)))
totalCost = bulkBookCost + shippingCost
print 'The total cost is: $', totalCost
cost(numBooks)
我是这样做的,但我仍然相信使用上面的功能会更好
price = 24.95
discount = price * (40/100)
d_price = price - discount
shipping = 3 + (0.75 * (60 - 1))
wholesale = d_price * 60 + shipping
我是python的初学者,这对我来说很好这是我解决这个问题的基本方法
bc = 24.95 #book cost real price
dis = 0.40 # discount percent
bcd = bc*dis # discount amount
bp = bc - bcd # book price
scf = 3 # shipping cost for the first book
scr = 0.75 # shipping cost for the rest books
cost1 = bp*scf
cost2 = bp*scr*59
TotalCost = cost1 + cost2
print(TotalCost)
price = 24.95 - (24.95)*4/10
order = 60
shipping = 3 + 0.75*(order-1)
print('The total price for 60 books including shipping and discount is %.2f$' % (order*price + shipping))
它更多的是关于基础数学而不是Python…是的,我正在尝试学习语法、变量以及如何处理它们。一个建议是:不要在金钱上使用浮点。以美分记录所有价格。用美分做所有的计算。输出时转换成美元。是的,我读到浮点不是很精确。所以一个好的方法是做以下操作?shipping=shippingPriceRest*totalUnits-1+ShippingPriceFirst如果您尝试这样做,您会发现您需要包括一些参数,以便评估您的意思。是的,对不起,英语不是我的母语,谢谢您的提示%d?你试过这个代码吗?以增量方式添加装运的循环?为什么?您可能应该描述代码是如何比以前更好,以及最初的问题是它是否正确。
bc = 24.95 #book cost real price
dis = 0.40 # discount percent
bcd = bc*dis # discount amount
bp = bc - bcd # book price
scf = 3 # shipping cost for the first book
scr = 0.75 # shipping cost for the rest books
cost1 = bp*scf
cost2 = bp*scr*59
TotalCost = cost1 + cost2
print(TotalCost)
price = 24.95 - (24.95)*4/10
order = 60
shipping = 3 + 0.75*(order-1)
print('The total price for 60 books including shipping and discount is %.2f$' % (order*price + shipping))