我正在读一本名为“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)
我得到以下结果:
总价为:945.45
我的问题是:
只有三件事需要改变:
1)你重复了书的数量:60和59都出现在代码中。你不应该有 59 在那里。
2)像这样打印结果:
print 'The total price is: %.2f' % result
3)通常的Python约定是命名variables_like_this,而不是LikeThis。
我建议的唯一改进是使用
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
。
我觉得很合适。 我会避免硬编码 59。相反,检查总数是否大于 1,并适当划分。
此外,这是次要的,但
bookDiscountAmount
应该是bookDiscountedAmount
(折扣是他们节省的金额,而不是他们支付的金额)。 其他人指出了如何改进字符串打印。
我是这样做的,但我仍然相信使用上面的功能会更好
price = 24.95
discount = price * (40/100)
d_price = price - discount
shipping = 3 + (0.75 * (60 - 1))
wholesale = d_price * 60 + shipping
请尝试这个:
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)
'''Suppose the cover price of a book is $24.95, but bookstores get a 40%
discount. Shipping costs $3 for the first copy and 75 cents for each
additional copy. What is the total wholesale cost for
60 copies?
'''
import math
book_price = float(24.95)
discount = float(book_price * 40 /100)
Book_Price= float(book_price - discount)
print ('Book Price is without shipping charges = ' , Book_Price)
shipping = 3.0
Total_Price = float(Book_Price + shipping)
print ('The book 1st price is =' ,Total_Price)
Quantity = int(input('Enter the Number of Copies = '))
if Quantity > 1:
Price = float(Book_Price * Quantity)
Additional_copy_price = 0.75
Q = float(Quantity * Additional_copy_price)
Final_Price = float(Price + Q + 3 - .75)
print (' Final price for more than one copy is = ' , Final_Price)
else:
print (Total_Price)
cov_price = 24.95 #cost of one book
discount = 0.4
per_one = (cov_price-(cov_price*discount)) #cost of one book on discount
ship_cost = 3 #shipment on first book
other_ship = 0.75 #shipment on other books
purchased = input("Enter number of books purchased : ")
purchased = int(purchased)
if(purchased):
cost = ((other_ship*(purchased-1)+(per_one*(purchased-1))) + (per_one+3))
print("The cost of books purchased including discount = ",cost)
print("Price per one is =",per_one)
我是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))
n = int(input('Enter the number of copies : '))
Book_store_price_of_one_copy = (60*24.95)/100
Shipping_cost_for_copy = (3+((n-1)*0.75))
whole_sale = (n*Book_store_price_of_one_copy)+Shipping_cost_for_copy
print(f'Whole sale of {n} copies is {whole_sale}')
bookPrice = 24.95
discount = 0.60
shippingPriceRest = 0.75
shippingPriceFirst = 3.00
totalUnits = 60
bookDiscountAmount = bookPrice * discount * totalUnits
shipping = shippingPriceRest * (totalUnits - 1) + shippingPriceFirst
result = bookDiscountAmount + shipping
print('The total price for 60 books including shipping and discount `enter code here`is:')
print('Total price of the books is: ' + str(bookDiscountAmount))
print('Total Shipping is: ' + str(shipping))
print('The Total price is: ' + str(result))
#######################################################################################
#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)