Help with some python homework...

S

scottwd80

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 everythingalso, 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
 
C

Chris Angelico

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

sjud9227

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. Irealize 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.
 
C

Chris Angelico

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, weare 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
 
S

sjud9227

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 mailinglist too.
 
G

Gregory Ewing

sjud9227 said:
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.)
 
C

Chris Angelico

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
 
G

Gregory Ewing

Chris said:
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.
 
N

Neil Cerutti

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.
 
S

Scott W Dunning

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.
 
S

Scott W Dunning

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’dhave 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"
 
S

Scott W Dunning

Also, any help on how to get the hours and seconds into double digits that would be cool too. 00:00:00
 
S

Scott W Dunning

Also, can any of you reccommend sites that may have little “projects” that I could work on to help me learn python better?
 
C

Chris Angelico

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
 
S

Scott W Dunning

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’dhave 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"
 
C

Chris Angelico

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 twodifferent hours, mins, and sec so I had to use one uppercase and one lowercase. 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
 
D

Denis McMahon

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.
 
D

David

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
"(e-mail address removed)" and not to the individual. That way we can
all participate in the discussion, that is best for everyone
especially you.
 
S

Scott W Dunning

Ok cool, thanks Denis!


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.
 
D

David

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
"(e-mail address removed)" 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.
 

Ask a Question

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

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

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,983
Messages
2,570,187
Members
46,747
Latest member
jojoBizaroo

Latest Threads

Top