Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #65093 > unrolled thread
| Started by | scottwd80@gmail.com |
|---|---|
| First post | 2014-01-30 21:12 -0800 |
| Last post | 2014-02-01 23:06 -0700 |
| Articles | 18 on this page of 38 — 13 participants |
Back to article view | Back to comp.lang.python
Help with some python homework... scottwd80@gmail.com - 2014-01-30 21:12 -0800
Re: Help with some python homework... Chris Angelico <rosuav@gmail.com> - 2014-01-31 16:30 +1100
Re: Help with some python homework... sjud9227 <scottwd80@gmail.com> - 2014-01-30 22:24 -0800
Re: Help with some python homework... Chris Angelico <rosuav@gmail.com> - 2014-01-31 17:38 +1100
Re: Help with some python homework... sjud9227 <scottwd80@gmail.com> - 2014-01-30 22:48 -0800
Re: Help with some python homework... Gregory Ewing <greg.ewing@canterbury.ac.nz> - 2014-01-31 21:17 +1300
Re: Help with some python homework... Chris Angelico <rosuav@gmail.com> - 2014-01-31 19:30 +1100
Re: Help with some python homework... Gregory Ewing <greg.ewing@canterbury.ac.nz> - 2014-01-31 23:46 +1300
Re: Help with some python homework... Chris Angelico <rosuav@gmail.com> - 2014-02-01 11:57 +1100
Re: Help with some python homework... Chris Angelico <rosuav@gmail.com> - 2014-02-01 12:34 +1100
Re: Help with some python homework... David <bouncingcats@gmail.com> - 2014-02-01 14:17 +1100
Re: Help with some python homework... Scott W Dunning <swdunning@cox.net> - 2014-01-31 18:14 -0700
Re: Help with some python homework... Denis McMahon <denismfmcmahon@gmail.com> - 2014-02-01 19:32 +0000
Re: Help with some python homework... David Hutto <dwightdhutto@gmail.com> - 2014-02-02 17:12 -0800
Re: Help with some python homework... Larry Hudson <orgnut@yahoo.com> - 2014-02-02 23:48 -0800
Re: Help with some python homework... Scott W Dunning <swdunning@cox.net> - 2014-01-31 17:46 -0700
Re: Help with some python homework... Scott W Dunning <swdunning@cox.net> - 2014-01-31 17:42 -0700
Re: Help with some python homework... David <bouncingcats@gmail.com> - 2014-02-01 17:13 +1100
Re: Help with some python homework... Dennis Lee Bieber <wlfraed@ix.netcom.com> - 2014-02-01 10:44 -0500
Re: Help with some python homework... Neil Cerutti <neilc@norwich.edu> - 2014-01-31 13:51 +0000
Re: Help with some python homework... Denis McMahon <denismfmcmahon@gmail.com> - 2014-02-01 03:02 +0000
Re: Help with some python homework... Scott W Dunning <swdunning@cox.net> - 2014-01-31 17:35 -0700
Re: Help with some python homework... Scott W Dunning <swdunning@cox.net> - 2014-01-31 22:18 -0700
Re: Help with some python homework... Denis McMahon <denismfmcmahon@gmail.com> - 2014-02-01 19:45 +0000
Re: Help with some python homework... David Hutto <dwightdhutto@gmail.com> - 2014-02-02 08:11 -0800
Re: Help with some python homework... David Hutto <dwightdhutto@gmail.com> - 2014-02-02 08:36 -0800
Re: Help with some python homework... MRAB <python@mrabarnett.plus.com> - 2014-02-02 16:38 +0000
Re: Help with some python homework... David Hutto <dwightdhutto@gmail.com> - 2014-02-02 08:57 -0800
Re: Help with some python homework... Denis McMahon <denismfmcmahon@gmail.com> - 2014-02-02 17:43 +0000
Re: Help with some python homework... David Hutto <dwightdhutto@gmail.com> - 2014-02-02 10:09 -0800
Re: Help with some python homework... David Hutto <dwightdhutto@gmail.com> - 2014-02-02 11:08 -0800
Re: Help with some python homework... David Hutto <dwightdhutto@gmail.com> - 2014-02-02 11:21 -0800
Re: Help with some python homework... "Rhodri James" <rhodri@wildebst.org.uk> - 2014-02-02 17:15 +0000
Re: Help with some python homework... Scott W Dunning <swdunning@cox.net> - 2014-01-31 22:04 -0700
Re: Help with some python homework... Scott W Dunning <swdunning@cox.net> - 2014-01-31 17:07 -0700
Re: Help with some python homework... Scott W Dunning <swdunning@cox.net> - 2014-01-31 22:19 -0700
Re: Help with some python homework... Dennis Lee Bieber <wlfraed@ix.netcom.com> - 2014-02-01 10:50 -0500
Re: Help with some python homework... Scott W Dunning <swdunning@cox.net> - 2014-02-01 23:06 -0700
Page 2 of 2 — ← Prev page 1 [2]
| From | Denis McMahon <denismfmcmahon@gmail.com> |
|---|---|
| Date | 2014-02-01 03:02 +0000 |
| Message-ID | <lcho4p$btv$1@dont-email.me> |
| In reply to | #65093 |
On Thu, 30 Jan 2014 21:12:19 -0800, scottwd80 wrote:
> Here is the question that was asked and below that I'll paste the code I
> have so far.
The following is a reasonably but not highly obfuscated short solution to
the problem set in python 2.7. With a bit of luck, after each lesson of
your course, you'll be able to understand a bit more of how it works.
M=60;H=M*60
def s(h,m,s): return h*H+m*M+s
def hms(s): return (int(s/H),int((s%H)/M),s%M)
(a,b,c)=hms(s(6,52,0)+3*s(0,7,12)+2*s(0,8,15))
print "{:02d}:{:02d}:{:02d}".format(a,b,c)
When you can write a short paragraph describing what each line of the
program does, you'll be on your way to understanding a few of the
structures, syntaxes and mechanisms of python.
Or you could show it to your lecturer or a TA and say it was suggested
that you ask her or him to work through it with you.
--
Denis McMahon, denismfmcmahon@gmail.com
[toc] | [prev] | [next] | [standalone]
| From | Scott W Dunning <swdunning@cox.net> |
|---|---|
| Date | 2014-01-31 17:35 -0700 |
| Message-ID | <mailman.6258.1391232938.18130.python-list@python.org> |
| In reply to | #65093 |
So, this is what I came up with. It works, which is good but it’s a 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. That seemed more complicated to me because I’d have to figure the number of seconds from midnight to 6:52am then do the calculations for number of seconds run until I got home, then I got kind of lost. Also, before I forget what is the difference between / and //? I remember somthing about floor division? Not sure what that means though. Is it like a % where it gives the remainder after dividing? Thanks again. Code below. Also, I think I found out through a little trial and error that I had two different hours, mins, and sec so I had to use one uppercase and one lower case. Is that frowned upon? And should I have come up with a different name instead? SECONDS = 1 MINUTES = 60 * SECONDS HOURS = 60 * MINUTES time_left_house = 6 * HOURS + 52 * MINUTES 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 hours = time_returned_home // HOURS part_hour = time_returned_home % HOURS minutes = part_hour // MINUTES seconds = part_hour % MINUTES print "Time returned home:", hours,":", minutes,":", seconds,"am" On Jan 31, 2014, at 6:51 AM, Neil Cerutti <neilc@norwich.edu> wrote: > On 2014-01-31, scottwd80@gmail.com <scottwd80@gmail.com> wrote: >> Here is the question that was asked and below that I'll paste >> the code I have so far. >> >> **If I leave my house at 6:52 am and run 1 mile at an easy pace >> (8:15 per mile), then 3 miles at tempo (7:12 per mile) and 1 >> mile at easy pace again, what time do I get home for >> breakfast?** > > That depends on the directions in which you run. Also, you are > fast! > > But seriously, my advice is to find the answer the old fashioned > way first, with pencil and paper. Then you'll have two things you > don't now: > > 1. A correct answer to test your program's answer with. > 2. A general idea of how to solve the problem. > > It's often a mistake to start writing code. Eventually you'll be > able to go directly from problem to code more often, but it will > take practice. > > -- > Neil Cerutti > > -- > https://mail.python.org/mailman/listinfo/python-list
[toc] | [prev] | [next] | [standalone]
| From | Scott W Dunning <swdunning@cox.net> |
|---|---|
| Date | 2014-01-31 22:18 -0700 |
| Message-ID | <mailman.6264.1391232938.18130.python-list@python.org> |
| In reply to | #65093 |
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'
Scott
On Jan 31, 2014, at 8:02 PM, Denis McMahon <denismfmcmahon@gmail.com> wrote:
> On Thu, 30 Jan 2014 21:12:19 -0800, scottwd80 wrote:
>
>> Here is the question that was asked and below that I'll paste the code I
>> have so far.
>
> The following is a reasonably but not highly obfuscated short solution to
> the problem set in python 2.7. With a bit of luck, after each lesson of
> your course, you'll be able to understand a bit more of how it works.
>
> M=60;H=M*60
> def s(h,m,s): return h*H+m*M+s
> def hms(s): return (int(s/H),int((s%H)/M),s%M)
> (a,b,c)=hms(s(6,52,0)+3*s(0,7,12)+2*s(0,8,15))
> print "{:02d}:{:02d}:{:02d}".format(a,b,c)
>
> When you can write a short paragraph describing what each line of the
> program does, you'll be on your way to understanding a few of the
> structures, syntaxes and mechanisms of python.
>
> Or you could show it to your lecturer or a TA and say it was suggested
> that you ask her or him to work through it with you.
>
> --
> Denis McMahon, denismfmcmahon@gmail.com
> --
> https://mail.python.org/mailman/listinfo/python-list
[toc] | [prev] | [next] | [standalone]
| From | Denis McMahon <denismfmcmahon@gmail.com> |
|---|---|
| Date | 2014-02-01 19:45 +0000 |
| Message-ID | <lcjiss$k7c$5@dont-email.me> |
| In reply to | #65189 |
On Fri, 31 Jan 2014 22:18:34 -0700, Scott W Dunning wrote: > 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 -- Denis McMahon, denismfmcmahon@gmail.com
[toc] | [prev] | [next] | [standalone]
| From | David Hutto <dwightdhutto@gmail.com> |
|---|---|
| Date | 2014-02-02 08:11 -0800 |
| Message-ID | <d8c2b5bd-d56d-4327-b219-85f0b304b187@googlegroups.com> |
| In reply to | #65189 |
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
[toc] | [prev] | [next] | [standalone]
| From | David Hutto <dwightdhutto@gmail.com> |
|---|---|
| Date | 2014-02-02 08:36 -0800 |
| Message-ID | <c793a061-7e2b-4fd8-87cb-a70d76632ff7@googlegroups.com> |
| In reply to | #65267 |
On Sunday, February 2, 2014 11:11:07 AM UTC-5, David Hutto wrote: > 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
[toc] | [prev] | [next] | [standalone]
| From | MRAB <python@mrabarnett.plus.com> |
|---|---|
| Date | 2014-02-02 16:38 +0000 |
| Message-ID | <mailman.6308.1391359140.18130.python-list@python.org> |
| In reply to | #65267 |
On 2014-02-02 16:11, David Hutto wrote: > price_per_book = 24.95 > discount = .40 > quantity = 60 > 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? > Here: > discounted_price = (1-discount) * price_per_book > > The discounted price should be price_per_book - discount > No, the discount of 0.40 is 40%, not 40 cents. > shipping = 3.0 + (60 - 1) * .75 > shipping should be, I think, should be 3.0 + (quantity * .75) > No, the shipping is 75 cents starting from the second copy. > 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 >
[toc] | [prev] | [next] | [standalone]
| From | David Hutto <dwightdhutto@gmail.com> |
|---|---|
| Date | 2014-02-02 08:57 -0800 |
| Message-ID | <be2d121a-fa68-41b3-b5b6-790d52f009a5@googlegroups.com> |
| In reply to | #65270 |
On Sunday, February 2, 2014 11:38:57 AM UTC-5, MRAB wrote: > On 2014-02-02 16:11, David Hutto wrote: > > > price_per_book = 24.95 > > > discount = .40 > > > quantity = 60 > > > > > 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? > > > > > Here: > > > discounted_price = (1-discount) * price_per_book > > > > > > The discounted price should be price_per_book - discount > > > > > No, the discount of 0.40 is 40%, not 40 cents. > > > > > shipping = 3.0 + (60 - 1) * .75 > > > shipping should be, I think, should be 3.0 + (quantity * .75) > > > > > No, the shipping is 75 cents starting from the second copy. > > > > > 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 > > > 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)
[toc] | [prev] | [next] | [standalone]
| From | Denis McMahon <denismfmcmahon@gmail.com> |
|---|---|
| Date | 2014-02-02 17:43 +0000 |
| Message-ID | <lcm035$70b$1@dont-email.me> |
| In reply to | #65271 |
On Sun, 02 Feb 2014 08:57:03 -0800, David Hutto wrote: > Revised: > 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) -- Denis McMahon, denismfmcmahon@gmail.com
[toc] | [prev] | [next] | [standalone]
| From | David Hutto <dwightdhutto@gmail.com> |
|---|---|
| Date | 2014-02-02 10:09 -0800 |
| Message-ID | <dcce80cc-e633-4a13-9d10-1efef79c0cef@googlegroups.com> |
| In reply to | #65273 |
On Sunday, February 2, 2014 12:43:01 PM UTC-5, Denis McMahon wrote:
> On Sun, 02 Feb 2014 08:57:03 -0800, David Hutto wrote:
>
>
>
> > Revised:
>
>
>
> > 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)
>
>
>
> --
>
> Denis McMahon
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.
[toc] | [prev] | [next] | [standalone]
| From | David Hutto <dwightdhutto@gmail.com> |
|---|---|
| Date | 2014-02-02 11:08 -0800 |
| Message-ID | <b8162867-5eb0-42fa-8e04-20c6d6a9b705@googlegroups.com> |
| In reply to | #65273 |
Or a better iterating example for a database of shipping, or ordering books would 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)
[toc] | [prev] | [next] | [standalone]
| From | David Hutto <dwightdhutto@gmail.com> |
|---|---|
| Date | 2014-02-02 11:21 -0800 |
| Message-ID | <d85a6344-e439-43a0-b693-c02dbf66f7ae@googlegroups.com> |
| In reply to | #65280 |
Should have been the following, which just shows the books price as a float as 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)
[toc] | [prev] | [next] | [standalone]
| From | "Rhodri James" <rhodri@wildebst.org.uk> |
|---|---|
| Date | 2014-02-02 17:15 +0000 |
| Message-ID | <op.xanzjfvv5079vu@gnudebeest> |
| In reply to | #65189 |
On Sat, 01 Feb 2014 05:18:34 -0000, Scott W Dunning <swdunning@cox.net>
wrote:
> 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'
The thing that is confusing you is that "discounted_price" is conflating
several steps into one. Think of it like this:
* "discount" is the discount rate; in this case meaning that a book costs
40% less than its list price ("price_per_book").
* In other words, the book costs "discount * price_per_book" less than
its list price.
* So the book costs "price_per_book - (discount * price_per_book)" after
applying the discount.
* refactoring, that's "(1 - discount) * price_per_book." Ta da!
--
Rhodri James *-* Wildebeest Herder to the Masses
[toc] | [prev] | [next] | [standalone]
| From | Scott W Dunning <swdunning@cox.net> |
|---|---|
| Date | 2014-01-31 22:04 -0700 |
| Message-ID | <mailman.6263.1391232938.18130.python-list@python.org> |
| In reply to | #65093 |
Ok cool, thanks Denis!
On Jan 31, 2014, at 8:02 PM, Denis McMahon <denismfmcmahon@gmail.com> wrote:
> On Thu, 30 Jan 2014 21:12:19 -0800, scottwd80 wrote:
>
>> Here is the question that was asked and below that I'll paste the code I
>> have so far.
>
> The following is a reasonably but not highly obfuscated short solution to
> the problem set in python 2.7. With a bit of luck, after each lesson of
> your course, you'll be able to understand a bit more of how it works.
>
> M=60;H=M*60
> def s(h,m,s): return h*H+m*M+s
> def hms(s): return (int(s/H),int((s%H)/M),s%M)
> (a,b,c)=hms(s(6,52,0)+3*s(0,7,12)+2*s(0,8,15))
> print "{:02d}:{:02d}:{:02d}".format(a,b,c)
>
> When you can write a short paragraph describing what each line of the
> program does, you'll be on your way to understanding a few of the
> structures, syntaxes and mechanisms of python.
>
> Or you could show it to your lecturer or a TA and say it was suggested
> that you ask her or him to work through it with you.
>
> --
> Denis McMahon, denismfmcmahon@gmail.com
> --
> https://mail.python.org/mailman/listinfo/python-list
[toc] | [prev] | [next] | [standalone]
| From | Scott W Dunning <swdunning@cox.net> |
|---|---|
| Date | 2014-01-31 17:07 -0700 |
| Message-ID | <mailman.6257.1391232938.18130.python-list@python.org> |
| In reply to | #65093 |
You guys are awesome! I think I was over complicating things for one. Plus I was looking at some code I wrote for another problem that asked to put in the number of seconds to calculate the problem and I didn’t need some of the things I added to this problem. Anyways, you guys have given me a lot of help and I think I can get it now. I’ll post what I got when I’m done so you guys can help with unnecessary code if needed or just to see how you helped. On Jan 31, 2014, at 6:51 AM, Neil Cerutti <neilc@norwich.edu> wrote: > On 2014-01-31, scottwd80@gmail.com <scottwd80@gmail.com> wrote: >> Here is the question that was asked and below that I'll paste >> the code I have so far. >> >> **If I leave my house at 6:52 am and run 1 mile at an easy pace >> (8:15 per mile), then 3 miles at tempo (7:12 per mile) and 1 >> mile at easy pace again, what time do I get home for >> breakfast?** > > That depends on the directions in which you run. Also, you are > fast! > > But seriously, my advice is to find the answer the old fashioned > way first, with pencil and paper. Then you'll have two things you > don't now: > > 1. A correct answer to test your program's answer with. > 2. A general idea of how to solve the problem. > > It's often a mistake to start writing code. Eventually you'll be > able to go directly from problem to code more often, but it will > take practice. > > -- > Neil Cerutti > > -- > https://mail.python.org/mailman/listinfo/python-list
[toc] | [prev] | [next] | [standalone]
| From | Scott W Dunning <swdunning@cox.net> |
|---|---|
| Date | 2014-01-31 22:19 -0700 |
| Message-ID | <mailman.6265.1391232939.18130.python-list@python.org> |
| In reply to | #65093 |
[Multipart message — attachments visible in raw view] — view raw
If you’re interested in what the problem is here it is…
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?
On Jan 31, 2014, at 10:18 PM, Scott W Dunning <swdunning@cox.net> wrote:
> 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'
>
> Scott
>
>
>
> On Jan 31, 2014, at 8:02 PM, Denis McMahon <denismfmcmahon@gmail.com> wrote:
>
>> On Thu, 30 Jan 2014 21:12:19 -0800, scottwd80 wrote:
>>
>>> Here is the question that was asked and below that I'll paste the code I
>>> have so far.
>>
>> The following is a reasonably but not highly obfuscated short solution to
>> the problem set in python 2.7. With a bit of luck, after each lesson of
>> your course, you'll be able to understand a bit more of how it works.
>>
>> M=60;H=M*60
>> def s(h,m,s): return h*H+m*M+s
>> def hms(s): return (int(s/H),int((s%H)/M),s%M)
>> (a,b,c)=hms(s(6,52,0)+3*s(0,7,12)+2*s(0,8,15))
>> print "{:02d}:{:02d}:{:02d}".format(a,b,c)
>>
>> When you can write a short paragraph describing what each line of the
>> program does, you'll be on your way to understanding a few of the
>> structures, syntaxes and mechanisms of python.
>>
>> Or you could show it to your lecturer or a TA and say it was suggested
>> that you ask her or him to work through it with you.
>>
>> --
>> Denis McMahon, denismfmcmahon@gmail.com
>> --
>> https://mail.python.org/mailman/listinfo/python-list
>
[toc] | [prev] | [next] | [standalone]
| From | Dennis Lee Bieber <wlfraed@ix.netcom.com> |
|---|---|
| Date | 2014-02-01 10:50 -0500 |
| Message-ID | <mailman.6282.1391269813.18130.python-list@python.org> |
| In reply to | #65093 |
On Fri, 31 Jan 2014 22:18:34 -0700, Scott W Dunning <swdunning@cox.net>
declaimed the following:
>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!
>
Because the discount rate you have is the amount taken OFF the price.
But you need the price AFTER removing the discount amount 100% (1.0) - 40%
(0.4) discount => 60% (0.6) final price
>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 defined "quantity" but then never used it in the shipping and
total_price lines.
--
Wulfraed Dennis Lee Bieber AF6VN
wlfraed@ix.netcom.com HTTP://wlfraed.home.netcom.com/
[toc] | [prev] | [next] | [standalone]
| From | Scott W Dunning <swdunning@cox.net> |
|---|---|
| Date | 2014-02-01 23:06 -0700 |
| Message-ID | <mailman.6299.1391321188.18130.python-list@python.org> |
| In reply to | #65093 |
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. On Feb 1, 2014, at 8:50 AM, Dennis Lee Bieber <wlfraed@ix.netcom.com> wrote: > On Fri, 31 Jan 2014 22:18:34 -0700, Scott W Dunning <swdunning@cox.net> > declaimed the following: > >> 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! >> > > Because the discount rate you have is the amount taken OFF the price. > But you need the price AFTER removing the discount amount 100% (1.0) - 40% > (0.4) discount => 60% (0.6) final price > >> 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 defined "quantity" but then never used it in the shipping and > total_price lines. > -- > Wulfraed Dennis Lee Bieber AF6VN > wlfraed@ix.netcom.com HTTP://wlfraed.home.netcom.com/ > > -- > https://mail.python.org/mailman/listinfo/python-list
[toc] | [prev] | [standalone]
Page 2 of 2 — ← Prev page 1 [2]
Back to top | Article view | comp.lang.python
csiph-web