Python 2.7.6 help with modules

Chris Angelico rosuav at gmail.com
Sat Feb 8 20:46:27 EST 2014


On Sun, Feb 9, 2014 at 12:21 PM, Scott W Dunning <swdunning at cox.net> wrote:
> I figured it out!  Thanks Chris!  Taking it one step at a time with the five digit number really helped me to see how to break it all up!  Are you a teacher?  I appreciate the help and the patients!  I like that you don’t just give me the answer that you break it down and help me so that I can figure it out on my own!  Thanks to all of you for all of your help, you guys are helping me a lot and I really appreciate it!
>

Awesome!

No, I'm not a teacher by profession, but I was homeschooled, and since
I'm the second of seven children [1], I got used to teaching things to
my siblings. Also, every week I run a Dungeons and Dragons campaign
online, which requires similar skills. (Teaching maths is way easier
than getting my players to figure out some puzzles. I had no idea
that, in a group of half a dozen nerds, nobody would recognize this
broken text: "In brightest day, in blackest night / No evil ......
sight / Let .... worship .... mi.... / .... my power .... tern...." -
I'd have thought the first line alone would be enough for anyone who's
at least mildly nerdy and/or geeky.)

(We start in fifteen minutes. Want to come watch? Just telnet to
minstrelhall.com port 221!)

> Here is how I did it if you’re curious.….
>
> seconds = raw_input("Enter the number of seconds:")
> seconds = int(seconds)
> weeks = seconds/604800
> weeks_rem = seconds % 604800
> days = weeks_rem / 86400
> days_rem = seconds % 86400
> hours = days_rem / 3600
> hours_rem = seconds % 3600
> minutes = hours_rem / 60
> minutes_rem = seconds % 60
> seconds = minutes_rem % 60
>
> print weeks, 'weeks', days, 'days', hours, 'hours', minutes, 'minutes', seconds, 'seconds'
>
> Not sure if that’s the correct way to do it but it works!   If there is any other advice I’ll take it.

That's certainly effective. It's going to give you the right result. I
would be inclined to start from the small end and strip off the
seconds first, then the minutes, etc, because then you're working with
smaller divisors (60, 60, 24, 7 instead of 604800, 86400, 3600, 60);
most people will understand that a week is 7 days, but only people who
work with DNS will know that it's 604800 seconds. But both work.

You'll also note that you're trimming off bits and leaving "residual
seconds". I would put all the "_rem" values back into "seconds", which
would let you use augmented assignment:

weeks = seconds/604800
seconds %= 604800
days = seconds / 86400
seconds %= 86400
hours = seconds / 3600
seconds %= 3600
minutes = seconds / 60
seconds %= 60

This emphasizes that you're stripping components off and keeping the
number of remaining seconds for the next step. But that's pretty
minor. Main thing is, you know what you're doing; you have to not just
get the right answer, but know WHY you get that answer.

ChrisA

[1] Our father's not a captain, but there's still nothing about being
a non-captain with seven children. And that movie is definitely one of
our favorite things.



More information about the Python-list mailing list