Help with some python homework...

D

Dennis Lee Bieber

hours = time_returned_home // HOURS
part_hour = time_returned_home % HOURS
minutes = part_hour // MINUTES
seconds = part_hour % MINUTES

Suggest you look at the Python manuals for the function divmod()
 
D

Denis McMahon

little different from a few things you guys had mentioned. For one, I
got the correct time by calculating the number of time run and
converting that into seconds then back out to hr:mn:sc. I didn’t
calculate from midnight.
SECONDS = 1 MINUTES = 60 * SECONDS HOURS = 60 * MINUTES

time_left_house = 6 * HOURS + 52 * MINUTES

This does actually calculate the time in seconds since midnight that you
left the house
miles_run_easy_pace = 2 * (8 * MINUTES + 15 * SECONDS)

miles_run_fast_pace = 3 * (7 * MINUTES + 12 * SECONDS)

time_returned_home = miles_run_easy_pace + miles_run_fast_pace +
time_left_house

And this calculates the time in seconds since midnight that you returned
home

So although you don't realise it, you are actually working in seconds
since midnight, and then converting seconds back into hours, minutes and
seconds.
 
D

Denis McMahon

Any chance you guys could help with another question I have? Below is a
code to a different problem. The only thing I don’t understand is why
when calculating the 'discounted price’ you have to subtract 1? Thanks
again guys!

price_per_book = 24.95
discount = .40
quantity = 60
discounted_price = (1-discount) * price_per_book
shipping = 3.0 + (60 - 1) * .75
total_price = 60 * discounted_price + shipping
print total_price, 'Total price'

You subtract 1 from the shipping price (which should be quantity - 1) to
allow for the fact that the first book costs 3.0 snargles to ship, and
extra books in the same shipment cost 0.75 snargles each.

So if the quantity is greater than one, the shipping cost is 3 snargles
for the first book plus 0.75 snargles times (quantity minus one)

The discounted price needs to be the cost, not the discount.

If the discount is 0.4 (or 40%), then the cost is 0.6 (or 60%) of the
list price. You are trying to calculate the cost, not the discount.

list_price = discounted_price + discount_amount

1.0 * list_price = 0.6 * list_price + 0.4 * list_price

Hence:

discounted_price = list_price - discount_amount

0.6 * list_price = 1.0 * list_price - 0.4 * list_price

so discounted_price = ( 1.0 - 0.4 ) * list_price

where 0.4 is the decimal fraction of the discount
 
S

Scott W Dunning

Yeah you’re right I didn’t even notice that. For some reason I just added the 60 instead of using quantity which had been defined.
 
D

David Hutto

price_per_book = 24.95
discount = .40
quantity = 60

Here:
discounted_price = (1-discount) * price_per_book

The discounted price should be price_per_book - discount

shipping = 3.0 + (60 - 1) * .75
shipping should be, I think, should be 3.0 + (quantity * .75)

total_price = 60 * discounted_price + shipping
replace 60 with quantity (quantity * discounted_price) + shipping

print total_price, 'Total price'

total_price gives:
945.45 Total price
and just 24.55(price per book - discount is ) * quantity is $1473 without the shipping, so the total is way off already:


I think the following is what you're looking for:

price_per_book = 24.95
discount = .40
quantity = 60
discounted_price = price_per_book-discount
shipping = 3.0 + (quantity*.75)
total_price = (quantity * discounted_price) + shipping
print 'Total price: $%d' % (total_price)
Total price: $1521
 
D

David Hutto

price_per_book = 24.95

discount = .40

quantity = 60



Here:

discounted_price = (1-discount) * price_per_book



The discounted price should be price_per_book - discount



shipping = 3.0 + (60 - 1) * .75

shipping should be, I think, should be 3.0 + (quantity * .75)



total_price = 60 * discounted_price + shipping

replace 60 with quantity (quantity * discounted_price) + shipping



print total_price, 'Total price'



total_price gives:

945.45 Total price

and just 24.55(price per book - discount is ) * quantity is $1473 without the shipping, so the total is way off already:





I think the following is what you're looking for:



price_per_book = 24.95

discount = .40

quantity = 60

discounted_price = price_per_book-discount

shipping = 3.0 + (quantity*.75)

total_price = (quantity * discounted_price) + shipping

print 'Total price: $%d' % (total_price)

Total price: $1521

My bad, I thought you were using $0.40
as a discount
 
D

David Hutto

The original problem says:



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?






No, the discount of 0.40 is 40%, not 40 cents.






No, the shipping is 75 cents starting from the second copy.

Revised:


price_per_book = 24.95
percent_discount = .40
discounted_price = price_per_book - (price_per_book * percent_discount)
quantity = 60
shipping = 3.0 + ((quantity - 1)*.75)
total_price = (quantity * discounted_price) + shipping
print 'Total price: $%d' % (total_price)
 
D

Denis McMahon

discounted_price = price_per_book - (price_per_book * percent_discount)

by applying some simple algebra to the right hand side

price_per_book - (price_per_book * percent_discount)

"x = (x * 1)" so "price_per_book == (price_per_book * 1)" so rhs becomes

(price_per_book * 1) - (price_per_book * percent_discount)

and "(a * x) - (a * y)" == "a * (x - y)" so rhs becomes

price_per_book * (1 - percent_discount)

hence:

discounted_price = price_per_book * (1 - percent_discount)
 
D

David Hutto

by applying some simple algebra to the right hand side



price_per_book - (price_per_book * percent_discount)



"x = (x * 1)" so "price_per_book == (price_per_book * 1)" so rhs becomes



(price_per_book * 1) - (price_per_book * percent_discount)



and "(a * x) - (a * y)" == "a * (x - y)" so rhs becomes



price_per_book * (1 - percent_discount)



hence:



discounted_price = price_per_book * (1 - percent_discount)

The one just looks out of place compare to using properly defined names,(algebra aside) like this:

def order_total():
price_per_book = float(raw_input("Enter price per book: $"))
percent_discount_amount = float(raw_input("Enter percent discount amount(in format example .40): "))
quantity = float(raw_input("Enter quantity of books: "))
first_book_shipping = float(raw_input("Enter first book's shipping: $"))
extra_book_shipping = float(raw_input("Enter extra book's shipping costs: $"))
percent_discount = price_per_book * percent_discount_amount
amount_of_first_books = 1 # of course it would equal 1
discounted_price = price_per_book - percent_discount
shipping = first_book_shipping + ((quantity - amount_of_first_books) * extra_book_shipping)
total_price = (quantity * discounted_price) + shipping
print 'Total price: $%d' % (total_price)

order_total()



************Or Use with params for iterating through larger amounts of books to be calculated*****************




def order_total(price_per_book,percent_discount_amount,quantity,first_book_shipping,extra_book_shipping):
percent_discount = price_per_book * percent_discount_amount
amount_of_first_book = 1 # of course it would equal 1
discounted_price = price_per_book - percent_discount
shipping = first_book_shipping + ((quantity - amount_of_first_book) * extra_book_shipping)
total_price = (quantity * discounted_price) + shipping
print 'Total price: $%d' % (total_price)


price_per_book = float(raw_input("Enter price per book: $"))
percent_discount_amount = float(raw_input("Enter percent discount amount(in format example .40): "))
quantity = float(raw_input("Enter quantity of books: "))
first_book_shipping = float(raw_input("Enter first book's shipping: $"))
extra_book_shipping = float(raw_input("Enter extra book's shipping costs: $"))

order_total(price_per_book,percent_discount_amount,quantity,first_book_shipping,extra_book_shipping)






The numbers just seem out of place when a var can be used that properly defines it, or another way to arrive at the same solution.
 
D

David Hutto

Or a better iterating example for a database of shipping, or ordering bookswould be:


import random as r
def order_total(price_per_book,percent_discount_amount,quantity,first_book_shipping,extra_book_shipping):
percent_discount = price_per_book * percent_discount_amount
amount_of_first_book = 1 # of course it would equal 1
discounted_price = price_per_book - percent_discount
shipping = first_book_shipping + ((quantity - amount_of_first_book) * extra_book_shipping)
total_price = (quantity * discounted_price) + shipping
print "Book XYZ-%d-ABC \nPrice per book: $%d\nPercent discount amount: %f\nQuantity of books: %f\nFirst book's shipping: $%f\nextra_book_shipping: $%f\nTotal price: $%f\n" % (books,price_per_book,percent_discount_amount,quantity,first_book_shipping,extra_book_shipping,total_price)


if __name__ == '__main__':
for books in range(0,10):
price_per_book = float(r.randint(1,100))
percent_discount_amount = float(".%d" % r.randint(0,100))
quantity = float(r.randint(0,100))
first_book_shipping = float(r.randint(0,100))
extra_book_shipping = float(r.randint(0,100))

order_total(price_per_book,percent_discount_amount,quantity,first_book_shipping,extra_book_shipping)
 
D

David Hutto

Should have been the following, which just shows the books price as a floatas well, but you get the point by now, I'm sure:

import random as r
def order_total(price_per_book,percent_discount_amount,quantity,first_book_shipping,extra_book_shipping):
percent_discount = price_per_book * percent_discount_amount
amount_of_first_book = 1 # of course it would equal 1
discounted_price = price_per_book - percent_discount
shipping = first_book_shipping + ((quantity - amount_of_first_book) * extra_book_shipping)
total_price = (quantity * discounted_price) + shipping
print "Book XYZ-%d-ABC \nPrice per book: $%f\nPercent discount amount: %f\nQuantity of books: %f\nFirst book's shipping: $%f\nextra_book_shipping: $%f\nTotal price: $%f\n" % (books,price_per_book,percent_discount_amount,quantity,first_book_shipping,extra_book_shipping,total_price)


if __name__ == '__main__':
for books in range(0,10):
price_per_book = float(r.randint(1,100))
percent_discount_amount = float(".%d" % r.randint(0,100))
quantity = float(r.randint(0,100))
first_book_shipping = float(r.randint(0,100))
extra_book_shipping = float(r.randint(0,100))

order_total(price_per_book,percent_discount_amount,quantity,first_book_shipping,extra_book_shipping)
 
D

David Hutto

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,989
Messages
2,570,207
Members
46,782
Latest member
ThomasGex

Latest Threads

Top