Re: Help with some python homework...
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 -- https://mail.python.org/mailman/listinfo/python-list
Re: Help with some python homework...
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 -- https://mail.python.org/mailman/listinfo/python-list
Re: Help with some python homework...
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 -- https://mail.python.org/mailman/listinfo/python-list
Re: Help with some python homework...
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) -- https://mail.python.org/mailman/listinfo/python-list
Re: Help with some python homework...
On Sat, 01 Feb 2014 05:18:34 -, Scott W Dunning swdunn...@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 -- https://mail.python.org/mailman/listinfo/python-list
Re: Help with some python homework...
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, denismfmcma...@gmail.com -- https://mail.python.org/mailman/listinfo/python-list
Re: Help with some python homework...
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. -- https://mail.python.org/mailman/listinfo/python-list
Re: Help with some python homework...
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) -- https://mail.python.org/mailman/listinfo/python-list
Re: Help with some python homework...
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) -- https://mail.python.org/mailman/listinfo/python-list
Re: Help with some python homework...
On Saturday, February 1, 2014 2:32:22 PM UTC-5, Denis McMahon wrote: On Fri, 31 Jan 2014 18:14:31 -0700, Scott W Dunning wrote: 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. -- Denis McMahon A little OT, but these might peak your interest for this: http://en.wikipedia.org/wiki/Epoch_%28reference_date%29 http://en.wikipedia.org/wiki/Leap_second -- https://mail.python.org/mailman/listinfo/python-list
Re: Help with some python homework...
On 02/02/2014 05:12 PM, David Hutto wrote: snip A little OT, but these might peak your interest for this: Also a little OT, but the word you're looking for is spelled pique. ;-) (Although, it IS pronounced 'peak'.) -=- Larry -=- -- https://mail.python.org/mailman/listinfo/python-list
Re: Help with some python homework...
On 1 February 2014 14:17, David bouncingc...@gmail.com wrote: Scott's message quoted above did not reach me, only Chris's quote of it, so I say: Scott once you begin a discussion on a mailing list like this one, please make sure that every reply you make goes to python-list@python.org and not to the individual. That way we can all participate in the discussion, that is best for everyone especially you. Please disregard the above paragraph Scott. Because 8 messages from you were just delivered to me, including that one, all via the list, some were 5 hours old. Sorry for any confusion I caused due to that delay. -- https://mail.python.org/mailman/listinfo/python-list
Re: Help with some python homework...
On Fri, 31 Jan 2014 18:14:31 -0700, Scott W Dunning wrote: 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. -- Denis McMahon, denismfmcma...@gmail.com -- https://mail.python.org/mailman/listinfo/python-list
Re: Help with some python homework...
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, denismfmcma...@gmail.com -- https://mail.python.org/mailman/listinfo/python-list
Re: Help with some python homework...
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 wlfr...@ix.netcom.com wrote: On Fri, 31 Jan 2014 22:18:34 -0700, Scott W Dunning swdunn...@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 wlfr...@ix.netcom.comHTTP://wlfraed.home.netcom.com/ -- https://mail.python.org/mailman/listinfo/python-list -- https://mail.python.org/mailman/listinfo/python-list
Re: Help with some python homework...
sjud9227 wrote: Doesn't assigning seconds/(60*60) mean that calculating 6*hours will give me 6 hours in seconds? No, it's giving you 6 seconds in hours. (That should give you a clue as to what you should have done instead. :-) Also, I don't know what you were trying to do with these two statements: seconds = seconds - hours*60*60 seconds = seconds - minutes *60 but they don't belong there at all. If you simply take them out, that part of the program is almost right. Also, why calculate how many seconds from midnight? Because the question asked what time do I get home?, not how long did it take me to get home?. You're already calculating what time do I get home with: total_time_run = miles_run_easy_pace + miles_run_fast_pace + time_left_house except that 'total_time_run' would be better called something like 'time_got_home'. Also, for the life of me I cannot figure out how to make everything display in hh:mm:ss. Here are a few hints: 1. Consider that if you take a number of seconds and divide it by the number of seconds in an hour, the quotient is the number of hours, and the remainder is the number of minutes and seconds left over, expressed in seconds. 2. If you then divide the remainder from (1) by the number of seconds in a minute, the quotient is the number of minutes, and the remainder is the number of seconds. 3. Python has the following operators for performing integer division: a // b gives the quotient of dividing a by b a % b gives the remainder (I recommend using '//' rather than just '/', because in some versions of Python, a/b does floating point division even if a and b are both integers, and that's not what you want here.) -- Greg -- https://mail.python.org/mailman/listinfo/python-list
Re: Help with some python homework...
On Fri, Jan 31, 2014 at 7:17 PM, Gregory Ewing greg.ew...@canterbury.ac.nz wrote: sjud9227 wrote: Doesn't assigning seconds/(60*60) mean that calculating 6*hours will give me 6 hours in seconds? No, it's giving you 6 seconds in hours. (That should give you a clue as to what you should have done instead. :-) ... a // b gives the quotient of dividing a by b a % b gives the remainder (I recommend using '//' rather than just '/', because in some versions of Python, a/b does floating point division even if a and b are both integers, and that's not what you want here.) OP is using 2.7.6, so short of a __future__ directive, that won't actually give 6 seconds in hours (though it will try to), and // is unnecessary. ChrisA -- https://mail.python.org/mailman/listinfo/python-list
Re: Help with some python homework...
Chris Angelico wrote: OP is using 2.7.6, so short of a __future__ directive, that won't actually give 6 seconds in hours Oops, yes, you're right! (I always use future division these days, so I tend to forget about that.) and // is unnecessary. It's still a good habit to get into, though, since it will continue to work in 3.x, and in 2.x it makes the intent of the code clear without having to know whether from __future__ import division is in effect. -- Greg -- https://mail.python.org/mailman/listinfo/python-list
Re: Help with some python homework...
On 2014-01-31, scottw...@gmail.com scottw...@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
Re: Help with some python homework...
On Sat, Feb 1, 2014 at 11:42 AM, Scott W Dunning swdunn...@cox.net wrote: Also, any help on how to get the hours and seconds into double digits that would be cool too. 00:00:00 Once you can divide the number of seconds into hours, minutes, and seconds, you can format them like this: time = %02d:%02d:%02d % (hours, minutes, seconds) I'll give you that one for free because I don't think it's particularly critical to your course, but it will look better that way :) Look up the string formatting features of Python in the docs. ChrisA -- https://mail.python.org/mailman/listinfo/python-list
Re: Help with some python homework...
On Sat, Feb 1, 2014 at 12:14 PM, Scott W Dunning swdunn...@cox.net wrote: Thanks Chris! Also, before I forget what is the difference between / and //? I remember something about floor division? In Python 2, the / operator by default is floor division. 5 divided by 2 is 2. When you divide two integers, you get back an integer. In Python 3, the / operator is sorta kinda real number division, in that it does what you're more likely to expect: 5 divided by 2 is 2.5. You'll get back a floating point number instead of an integer. You can ask Python 2 to give you the Python 3 behaviour by putting this at the top of your script: from __future__ import division Regardless of whether you're on Py2 without the future directive, Py2 with the future directive, or Py3, you can use the // operator to get the behaviour of floor division. Since that behaviour is exactly what you want when you're working with modulo, it may be worth getting into the habit of using it. But then again, it may not. You'll have other changes to make when you move to Python 3, so you can just figure it out then. (Incidentally, if there's nothing keeping you on Python 2, you may want to move sooner rather than later. There are lots of awesome features in Python 3 that will never be added to Python 2.) 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 Well, an ALL_UPPERCASE_NAME is generally a constant, which is how you're using them here. So in this specific instance, what you've done is fine. But don't treat this as a way to get yourself a few more variable names; if you'd done these ones in lower case and the other ones in caps, it would have been extremely confusing to an experienced Python programmer. For some style tips that a lot of Python programs follow, check out PEP 8: http://www.python.org/dev/peps/pep-0008/ Formally, this is the style guide for the Python standard library, but it's a fairly sensible set of rules, and a lot of projects follow them. Some of the rules are widely adopted elsewhere, others (like the insistence on spaces rather than tabs - everyone agrees that you should use one OR the other, but plenty of people advocate tabs above spaces) less so; take your pick which bits you follow, but all of it is worth a read. 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 Looks fairly good. This is where you could use the formatting notation I gave you above: print Time returned home: %02d:%02d:%02d am % (hours, minutes, seconds) And then, since you're using print with a single string argument, get in the habit of putting parentheses around it: print(Time returned home: %02d:%02d:%02d am % (hours, minutes, seconds)) This syntax (one string argument, parens around it) is common to both Python 2's print statement and Python 3's print function (as with division, you can ask Python 2 to give you a print function if you wish). So, is the program giving you the result you expect? That's really the key. If it is, then you (probably!) have a working program! ChrisA -- https://mail.python.org/mailman/listinfo/python-list
Re: Help with some python homework...
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, denismfmcma...@gmail.com -- https://mail.python.org/mailman/listinfo/python-list
Re: Help with some python homework...
On 1 February 2014 12:34, Chris Angelico ros...@gmail.com wrote: On Sat, Feb 1, 2014 at 12:14 PM, Scott W Dunning swdunn...@cox.net wrote: 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 What is actually being defined here are constants to be used for scaling or conversion of some quantity (a time) into different units. So in this situation would I define the conversion constant with an upper case name like this: SECONDS_PER_MINUTE = 60 and I would use it like this seconds = minutes * SECONDS_PER_MINUTE where seconds and minutes are the names holding the numeric data. That line has the extra benefit that it is clear to me why the units are seconds on both sides of the equals sign (because on the right hand side the minute-units cancel thus: m*s/m=s), whereas this is much less clear to me in Scott's line. Scott's message quoted above did not reach me, only Chris's quote of it, so I say: Scott once you begin a discussion on a mailing list like this one, please make sure that every reply you make goes to python-list@python.org and not to the individual. That way we can all participate in the discussion, that is best for everyone especially you. -- https://mail.python.org/mailman/listinfo/python-list
Re: Help with some python homework...
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 ne...@norwich.edu wrote: On 2014-01-31, scottw...@gmail.com scottw...@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 -- https://mail.python.org/mailman/listinfo/python-list
Re: Help with some python homework...
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 ne...@norwich.edu wrote: On 2014-01-31, scottw...@gmail.com scottw...@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 -- https://mail.python.org/mailman/listinfo/python-list
Re: Help with some python homework...
Also, can any of you reccommend sites that may have little “projects” that I could work on to help me learn python better? On Jan 31, 2014, at 1:30 AM, Chris Angelico ros...@gmail.com wrote: On Fri, Jan 31, 2014 at 7:17 PM, Gregory Ewing greg.ew...@canterbury.ac.nz wrote: sjud9227 wrote: Doesn't assigning seconds/(60*60) mean that calculating 6*hours will give me 6 hours in seconds? No, it's giving you 6 seconds in hours. (That should give you a clue as to what you should have done instead. :-) ... a // b gives the quotient of dividing a by b a % b gives the remainder (I recommend using '//' rather than just '/', because in some versions of Python, a/b does floating point division even if a and b are both integers, and that's not what you want here.) OP is using 2.7.6, so short of a __future__ directive, that won't actually give 6 seconds in hours (though it will try to), and // is unnecessary. ChrisA -- https://mail.python.org/mailman/listinfo/python-list -- https://mail.python.org/mailman/listinfo/python-list
Re: Help with some python homework...
Also, any help on how to get the hours and seconds into double digits that would be cool too. 00:00:00 On Jan 31, 2014, at 1:30 AM, Chris Angelico ros...@gmail.com wrote: On Fri, Jan 31, 2014 at 7:17 PM, Gregory Ewing greg.ew...@canterbury.ac.nz wrote: sjud9227 wrote: Doesn't assigning seconds/(60*60) mean that calculating 6*hours will give me 6 hours in seconds? No, it's giving you 6 seconds in hours. (That should give you a clue as to what you should have done instead. :-) ... a // b gives the quotient of dividing a by b a % b gives the remainder (I recommend using '//' rather than just '/', because in some versions of Python, a/b does floating point division even if a and b are both integers, and that's not what you want here.) OP is using 2.7.6, so short of a __future__ directive, that won't actually give 6 seconds in hours (though it will try to), and // is unnecessary. ChrisA -- https://mail.python.org/mailman/listinfo/python-list -- https://mail.python.org/mailman/listinfo/python-list
Re: Help with some python homework...
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 swdunn...@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 denismfmcma...@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, denismfmcma...@gmail.com -- https://mail.python.org/mailman/listinfo/python-list -- https://mail.python.org/mailman/listinfo/python-list
Re: Help with some python homework...
Thanks Chris! 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 something 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 5:57 PM, Chris Angelico ros...@gmail.com wrote: On Sat, Feb 1, 2014 at 11:42 AM, Scott W Dunning swdunn...@cox.net wrote: Also, any help on how to get the hours and seconds into double digits that would be cool too. 00:00:00 Once you can divide the number of seconds into hours, minutes, and seconds, you can format them like this: time = %02d:%02d:%02d % (hours, minutes, seconds) I'll give you that one for free because I don't think it's particularly critical to your course, but it will look better that way :) Look up the string formatting features of Python in the docs. ChrisA -- https://mail.python.org/mailman/listinfo/python-list -- https://mail.python.org/mailman/listinfo/python-list
Re: Help with some python homework...
Ok cool, thanks Denis! On Jan 31, 2014, at 8:02 PM, Denis McMahon denismfmcma...@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, denismfmcma...@gmail.com -- https://mail.python.org/mailman/listinfo/python-list -- https://mail.python.org/mailman/listinfo/python-list
Re: Help with some python homework...
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 denismfmcma...@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, denismfmcma...@gmail.com -- https://mail.python.org/mailman/listinfo/python-list -- https://mail.python.org/mailman/listinfo/python-list
Re: Help with some python homework...
On Fri, Jan 31, 2014 at 4:12 PM, scottw...@gmail.com wrote: **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?** seconds = 1 hours = seconds / (60*60) seconds = seconds - hours*60*60 minutes = seconds / 60 seconds = seconds - minutes *60 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) total_time_run = miles_run_easy_pace + miles_run_fast_pace + time_left_house Thanks for being up-front about it being homework. I'll give you one broad hint, and see if you can figure it out from there. Your beginning work is not actually achieving anything useful. To make your next steps work, what you actually want is two very simple assignments that will mean that 6 * hours comes out as the number of seconds in six hours. Then, when you've added all the different pieces together, you'll have a final time that's measured in seconds - and since that final time includes the time_left_house, it's actually going to be the number of seconds since midnight. This is actually an excellent way to represent time (number of seconds since some beginning point aka epoch). There's then just one last step: Convert it into hours, minutes, and seconds, for display. You have most of the code for doing that. So, work on this in two parts. In the first part, make your program calculate how many seconds after midnight you'll get home. (The correct answer there is 27006, according to my calculations. Of course, you need to have a program that produces the correct answer, not just the answer.) Then work out how to make that display as hh:mm:ss. I think you can probably get it from there - you're already a lot of the way toward it. But if not, you know where to find us :) ChrisA -- https://mail.python.org/mailman/listinfo/python-list
Re: Help with some python homework...
On Thursday, January 30, 2014 10:30:11 PM UTC-7, Chris Angelico wrote: On Fri, Jan 31, 2014 at 4:12 PM, scottw...@gmail.com wrote: **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?** seconds = 1 hours = seconds / (60*60) seconds = seconds - hours*60*60 minutes = seconds / 60 seconds = seconds - minutes *60 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) total_time_run = miles_run_easy_pace + miles_run_fast_pace + time_left_house Thanks for being up-front about it being homework. I'll give you one broad hint, and see if you can figure it out from there. Your beginning work is not actually achieving anything useful. To make your next steps work, what you actually want is two very simple assignments that will mean that 6 * hours comes out as the number of seconds in six hours. Then, when you've added all the different pieces together, you'll have a final time that's measured in seconds - and since that final time includes the time_left_house, it's actually going to be the number of seconds since midnight. This is actually an excellent way to represent time (number of seconds since some beginning point aka epoch). There's then just one last step: Convert it into hours, minutes, and seconds, for display. You have most of the code for doing that. So, work on this in two parts. In the first part, make your program calculate how many seconds after midnight you'll get home. (The correct answer there is 27006, according to my calculations. Of course, you need to have a program that produces the correct answer, not just the answer.) Then work out how to make that display as hh:mm:ss. I think you can probably get it from there - you're already a lot of the way toward it. But if not, you know where to find us :) ChrisA Thank you so much Chris. However, i'm still a little confused. Doesn't assigning seconds/(60*60) mean that calculating 6*hours will give me 6 hours in seconds? Also, why calculate how many seconds from midnight? wouldn't it just be from the time that you left the house at 6:52? Also, for the life of me I cannot figure out how to make everything display in hh:mm:ss. I realize I'm asking a lot especially do to the fact it's homework but, we are allowed help in class I just don't have class again until next Tuesday. Plus I really do want to learn not just get the answers. -- https://mail.python.org/mailman/listinfo/python-list
Re: Help with some python homework...
On Fri, Jan 31, 2014 at 5:24 PM, sjud9227 scottw...@gmail.com wrote: Thank you so much Chris. However, i'm still a little confused. Doesn't assigning seconds/(60*60) mean that calculating 6*hours will give me 6 hours in seconds? Also, why calculate how many seconds from midnight? wouldn't it just be from the time that you left the house at 6:52? Also, for the life of me I cannot figure out how to make everything display in hh:mm:ss. I realize I'm asking a lot especially do to the fact it's homework but, we are allowed help in class I just don't have class again until next Tuesday. Plus I really do want to learn not just get the answers. First things first: You're using Google Groups, so your lines are unwrapped and your quoted text is double spaced. Please fix this every time you post (which requires some fiddling around) or switch to a client that works. I recommend using the mailing list instead: https://mail.python.org/mailman/listinfo/python-list Now then. What is your initial seconds? With the code you posted, it's 1, which means you get nothing at all after dividing by (60*60), so you just have a big ol' zero. What you need to do is convert hours into seconds. Is that going to mean multiplying by a big number or multiplying by a very small number? Think about it as something completely separate from programming. What number will you be multiplying by? Now code that. You can calculate the total number of seconds of your run. You can calculate the number of seconds from midnight until 6:52AM. Add the two together and you get the number of seconds from midnight until you get home. The final step, formatting, is pretty straight-forward. Let's suppose I have a number of seconds, say 4. That represents some number of hours, some number of minutes, and some number of seconds. How many complete hours are there in 4 seconds? How many seconds are left over? And out of those left-over seconds, how many minutes can you make? How many seconds are left after the minutes are taken out? These questions are all answered by division and modulo operations. You can actually solve this completely separately from the other part of the problem; try answering it for the figure I gave (4 seconds), then try it for a few other numbers, and see how it goes. ChrisA -- https://mail.python.org/mailman/listinfo/python-list
Re: Help with some python homework...
On Thursday, January 30, 2014 11:38:05 PM UTC-7, Chris Angelico wrote: On Fri, Jan 31, 2014 at 5:24 PM, sjud9227 scottw...@gmail.com wrote: Thank you so much Chris. However, i'm still a little confused. Doesn't assigning seconds/(60*60) mean that calculating 6*hours will give me 6 hours in seconds? Also, why calculate how many seconds from midnight? wouldn't it just be from the time that you left the house at 6:52? Also, for the life of me I cannot figure out how to make everything display in hh:mm:ss. I realize I'm asking a lot especially do to the fact it's homework but, we are allowed help in class I just don't have class again until next Tuesday. Plus I really do want to learn not just get the answers. First things first: You're using Google Groups, so your lines are unwrapped and your quoted text is double spaced. Please fix this every time you post (which requires some fiddling around) or switch to a client that works. I recommend using the mailing list instead: https://mail.python.org/mailman/listinfo/python-list Now then. What is your initial seconds? With the code you posted, it's 1, which means you get nothing at all after dividing by (60*60), so you just have a big ol' zero. What you need to do is convert hours into seconds. Is that going to mean multiplying by a big number or multiplying by a very small number? Think about it as something completely separate from programming. What number will you be multiplying by? Now code that. You can calculate the total number of seconds of your run. You can calculate the number of seconds from midnight until 6:52AM. Add the two together and you get the number of seconds from midnight until you get home. The final step, formatting, is pretty straight-forward. Let's suppose I have a number of seconds, say 4. That represents some number of hours, some number of minutes, and some number of seconds. How many complete hours are there in 4 seconds? How many seconds are left over? And out of those left-over seconds, how many minutes can you make? How many seconds are left after the minutes are taken out? These questions are all answered by division and modulo operations. You can actually solve this completely separately from the other part of the problem; try answering it for the figure I gave (4 seconds), then try it for a few other numbers, and see how it goes. ChrisA Ok cool, I'll try this. Thank you again! Will def sign up for the mailing list too. -- https://mail.python.org/mailman/listinfo/python-list