Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #65093 > unrolled thread

Help with some python homework...

Started byscottwd80@gmail.com
First post2014-01-30 21:12 -0800
Last post2014-02-01 23:06 -0700
Articles 20 on this page of 38 — 13 participants

Back to article view | Back to comp.lang.python


Contents

  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 1 of 2  [1] 2  Next page →


#65093 — Help with some python homework...

Fromscottwd80@gmail.com
Date2014-01-30 21:12 -0800
SubjectHelp with some python homework...
Message-ID<02c4c69d-71f1-4a01-86df-d4d5a7ffb3f5@googlegroups.com>
Here is the question that was asked and below that I'll paste the code I have so far.  Any pointers would be great.  Please keep in mind this is only my second week with python (or any programming for that matter) so I have no idea what I'm doing.  How would you code this?  Anyways, any help is GREATLY APPRECIATED! 

I'd kind of like to figure it out with % rather than subtracting everything also, but doesn't really matter either way.  



**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

     print total_time_run, "Total time run: " , hours, 'Hours: ', minutes, 'Minutes: ', seconds, 'Seconds: '


FYI, i'm using python 2.7.6

[toc] | [next] | [standalone]


#65095

FromChris Angelico <rosuav@gmail.com>
Date2014-01-31 16:30 +1100
Message-ID<mailman.6195.1391146214.18130.python-list@python.org>
In reply to#65093
On Fri, Jan 31, 2014 at 4:12 PM,  <scottwd80@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

[toc] | [prev] | [next] | [standalone]


#65097

Fromsjud9227 <scottwd80@gmail.com>
Date2014-01-30 22:24 -0800
Message-ID<8da5d692-1446-4107-9a5a-5efd91f7c051@googlegroups.com>
In reply to#65095
On Thursday, January 30, 2014 10:30:11 PM UTC-7, Chris Angelico wrote:
> On Fri, Jan 31, 2014 at 4:12 PM,  <scottwd80@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.

[toc] | [prev] | [next] | [standalone]


#65099

FromChris Angelico <rosuav@gmail.com>
Date2014-01-31 17:38 +1100
Message-ID<mailman.6197.1391150295.18130.python-list@python.org>
In reply to#65097
On Fri, Jan 31, 2014 at 5:24 PM, sjud9227 <scottwd80@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 40000. That represents some number of
hours, some number of minutes, and some number of seconds. How many
complete hours are there in 40000 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 (40000 seconds), then
try it for a few other numbers, and see how it goes.

ChrisA

[toc] | [prev] | [next] | [standalone]


#65101

Fromsjud9227 <scottwd80@gmail.com>
Date2014-01-30 22:48 -0800
Message-ID<74e3a5ed-40b7-4a89-aebb-205f76d0c451@googlegroups.com>
In reply to#65099
On Thursday, January 30, 2014 11:38:05 PM UTC-7, Chris Angelico wrote:
> On Fri, Jan 31, 2014 at 5:24 PM, sjud9227 <scottwd80@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 40000. That represents some number of
> 
> hours, some number of minutes, and some number of seconds. How many
> 
> complete hours are there in 40000 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 (40000 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.

[toc] | [prev] | [next] | [standalone]


#65105

FromGregory Ewing <greg.ewing@canterbury.ac.nz>
Date2014-01-31 21:17 +1300
Message-ID<bl14h8F21ooU1@mid.individual.net>
In reply to#65097
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

[toc] | [prev] | [next] | [standalone]


#65106

FromChris Angelico <rosuav@gmail.com>
Date2014-01-31 19:30 +1100
Message-ID<mailman.6199.1391157029.18130.python-list@python.org>
In reply to#65105
On Fri, Jan 31, 2014 at 7:17 PM, Gregory Ewing
<greg.ewing@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

[toc] | [prev] | [next] | [standalone]


#65112

FromGregory Ewing <greg.ewing@canterbury.ac.nz>
Date2014-01-31 23:46 +1300
Message-ID<bl1d8eF3s8uU1@mid.individual.net>
In reply to#65106
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

[toc] | [prev] | [next] | [standalone]


#65156

FromChris Angelico <rosuav@gmail.com>
Date2014-02-01 11:57 +1100
Message-ID<mailman.6236.1391216262.18130.python-list@python.org>
In reply to#65105
On Sat, Feb 1, 2014 at 11:42 AM, Scott W Dunning <swdunning@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

[toc] | [prev] | [next] | [standalone]


#65158

FromChris Angelico <rosuav@gmail.com>
Date2014-02-01 12:34 +1100
Message-ID<mailman.6237.1391218479.18130.python-list@python.org>
In reply to#65105
On Sat, Feb 1, 2014 at 12:14 PM, Scott W Dunning <swdunning@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

[toc] | [prev] | [next] | [standalone]


#65185

FromDavid <bouncingcats@gmail.com>
Date2014-02-01 14:17 +1100
Message-ID<mailman.6256.1391232780.18130.python-list@python.org>
In reply to#65105
On 1 February 2014 12:34, Chris Angelico <rosuav@gmail.com> wrote:
> On Sat, Feb 1, 2014 at 12:14 PM, Scott W Dunning <swdunning@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.

[toc] | [prev] | [next] | [standalone]


#65186

FromScott W Dunning <swdunning@cox.net>
Date2014-01-31 18:14 -0700
Message-ID<mailman.6261.1391232938.18130.python-list@python.org>
In reply to#65105
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 <rosuav@gmail.com> wrote:

> On Sat, Feb 1, 2014 at 11:42 AM, Scott W Dunning <swdunning@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

[toc] | [prev] | [next] | [standalone]


#65231

FromDenis McMahon <denismfmcmahon@gmail.com>
Date2014-02-01 19:32 +0000
Message-ID<lcji46$k7c$4@dont-email.me>
In reply to#65186
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, denismfmcmahon@gmail.com

[toc] | [prev] | [next] | [standalone]


#65306

FromDavid Hutto <dwightdhutto@gmail.com>
Date2014-02-02 17:12 -0800
Message-ID<4130ca2c-1608-4547-a083-95742e44176f@googlegroups.com>
In reply to#65231
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

[toc] | [prev] | [next] | [standalone]


#65325

FromLarry Hudson <orgnut@yahoo.com>
Date2014-02-02 23:48 -0800
Message-ID<D_OdnWYUHfsv1HLPnZ2dnUVZ_g2dnZ2d@giganews.com>
In reply to#65306
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 -=-

[toc] | [prev] | [next] | [standalone]


#65191

FromScott W Dunning <swdunning@cox.net>
Date2014-01-31 17:46 -0700
Message-ID<mailman.6260.1391232938.18130.python-list@python.org>
In reply to#65105
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 <rosuav@gmail.com> wrote:

> On Fri, Jan 31, 2014 at 7:17 PM, Gregory Ewing
> <greg.ewing@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

[toc] | [prev] | [next] | [standalone]


#65192

FromScott W Dunning <swdunning@cox.net>
Date2014-01-31 17:42 -0700
Message-ID<mailman.6259.1391232938.18130.python-list@python.org>
In reply to#65105
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 <rosuav@gmail.com> wrote:

> On Fri, Jan 31, 2014 at 7:17 PM, Gregory Ewing
> <greg.ewing@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

[toc] | [prev] | [next] | [standalone]


#65204

FromDavid <bouncingcats@gmail.com>
Date2014-02-01 17:13 +1100
Message-ID<mailman.6270.1391243899.18130.python-list@python.org>
In reply to#65105
On 1 February 2014 14:17, David <bouncingcats@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.

[toc] | [prev] | [next] | [standalone]


#65222

FromDennis Lee Bieber <wlfraed@ix.netcom.com>
Date2014-02-01 10:44 -0500
Message-ID<mailman.6281.1391269472.18130.python-list@python.org>
In reply to#65105
On Fri, 31 Jan 2014 18:14:31 -0700, Scott W Dunning <swdunning@cox.net>
declaimed the following:


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

-- 
	Wulfraed                 Dennis Lee Bieber         AF6VN
    wlfraed@ix.netcom.com    HTTP://wlfraed.home.netcom.com/

[toc] | [prev] | [next] | [standalone]


#65118

FromNeil Cerutti <neilc@norwich.edu>
Date2014-01-31 13:51 +0000
Message-ID<mailman.6206.1391176319.18130.python-list@python.org>
In reply to#65093
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

[toc] | [prev] | [next] | [standalone]


Page 1 of 2  [1] 2  Next page →

Back to top | Article view | comp.lang.python


csiph-web