1

I'm reading this python book called "Python for software design" and it has the following exercise:

Suppose the cover price of a book is 24ドル.95, but the 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

ok, i have the following code:

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)

With that I get the following results:

  • The total price for 60 books including shipping and discount is:
  • Total price of the books is: 898.2
  • Total Shipping is: 47.25
  • The Total price is: 945.45

  • My questions are:

    • Is this correct?
    • How can i make this code better?
asked Jun 27, 2011 at 14:05
4
  • 4
    It's more about basic math than Python... Commented Jun 27, 2011 at 14:06
  • yes I'm trying to learn syntax, variables and how to approach them. Commented Jun 27, 2011 at 14:10
  • 4
    One suggestion: don't use floating points with money. Record all the prices in cents. Do all the calculations with cents. Convert to dollars when you output. Commented Jun 27, 2011 at 14:15
  • yes I'm reading that floating points are not very precise Commented Jun 27, 2011 at 14:25

12 Answers 12

6

There are only three things to change:

1) You duplicated the number of books: 60 and 59 both appear in the code. You shouldn't have 59 in there.

2) Print you results like this: print 'The total price is: %.2f' % result

3) Usual Python convention is to name variables_like_this, notLikeThis.

answered Jun 27, 2011 at 14:08
Sign up to request clarification or add additional context in comments.

2 Comments

so a good way would be to do the following? shipping = shippingPriceRest * totalUnits - 1 + shippingPriceFirst
If you try that, you'll see you need to include some parens to get it to evaluate the way you meant.
1

The only improvement I would suggest is to use the format-function instead of string concatenation:

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)

this makes all the numbers nicely aligned and equally formatted (with two digits after the decimal-point and a total precision of 7).

Edit: And of course, as the other pointed out, don't hard-code the 59 there.

answered Jun 27, 2011 at 14:09

Comments

1

It looks right to me. I would avoid hard-coding 59. Instead, check whether the total is more than one, and divide up as appropriate.

Also, this is minor, but bookDiscountAmount should be bookDiscountedAmount (the discount is the amount they save, not the amount they pay). Other people have pointed out how to improve the string printing.

answered Jun 27, 2011 at 14:10

1 Comment

yeah, sorry, english is not my mother tongue, thanks for the heads up
1

i did it this way but i still believe that with a function as above would be better

price = 24.95
discount = price * (40/100)
d_price = price - discount
shipping = 3 + (0.75 * (60 - 1))
wholesale = d_price * 60 + shipping
answered Nov 8, 2016 at 9:06

Comments

0

Please try this:

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)
Zilicon
3,8584 gold badges34 silver badges43 bronze badges
answered Jun 17, 2015 at 11:47

Comments

0
'''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)
answered Apr 28, 2017 at 22:29

Comments

0
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)

i'm a beginner to python and this worked fine for me

answered Sep 12, 2018 at 13:51

Comments

0
 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)
answered Jan 31, 2020 at 8:35

1 Comment

you should probably describe how the code is better than what came before, also the original question asked if it was correct.
0

This is my basic solution to this problem

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))
answered Aug 23, 2020 at 4:30

Comments

0
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}')
Bagle
2,3463 gold badges15 silver badges39 bronze badges
answered Jun 14, 2021 at 13:24

1 Comment

Hi, and welcome to Stackoverflow! Next time, please try to explain what each part of your code does, or at least explain how this answers the question. It makes it easier for those who stumble upon your code again. Thank you!
0
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))
answered Dec 17, 2024 at 22:10

Comments

-1
#######################################################################################
#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)
answered Jun 27, 2011 at 14:19

1 Comment

%d ? Did you try this code? A loop to incrementally add the shipping? Why??

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.