Python 2.7.6 help with modules
Chris Angelico
rosuav at gmail.com
Sat Feb 8 06:10:03 CET 2014
On Sat, Feb 8, 2014 at 3:44 PM, Scott W Dunning <swdunning at cox.net> wrote:
> - This is what I’ve been working with. I get the correct answers for
> minutes and seconds then it goes to shit after that.
>
> seconds = raw_input("Enter the number of seconds:")
> seconds = int(seconds)
> minutes = seconds/60
> seconds = seconds % 60
> minutes = minutes % 60
> hours = seconds/3600
> hours = seconds % 3600
> days = seconds/86400
> days = seconds % 86400
> weeks = seconds/604800
> weeks = seconds % 604800
> print weeks, 'weeks', days, 'days', hours, 'hours', minutes, 'minutes',
> seconds, 'seconds'
>
> Any help is greatly appreciated! Thanks again!
It might be easiest to think in terms of a single "divide into
quotient and remainder" operation. Let's leave aside
weeks/days/hours/minutes/seconds and split a number up into its
digits. (This is actually not as useless as you might think; in low
level programming, this is how to display a number on the screen, for
instance.)
number = int(raw_input("Enter a five-digit number: "))
Now we begin to split it up:
foo = number % 10
bar = number / 10
Do you know, without running the code, what 'foo' and 'bar' will be?
Give those two variables better names (hint: one of them would be
appropriately named "last_digit"), and then work on some more pieces
of the puzzle.
You should be able to get this to the point of writing out five
separate values, which are the original five digits. Each one is worth
10 of the previous value. At every step, do both halves of the
division. (Python actually has a function 'divmod' which makes this a
bit more efficient and maybe clearer and tidier, but leave that aside
for the moment and just use / and %.) Can you work out what it's doing
more easily this way?
You should then be able to follow the same principles in working out
the time unit problem. Apart from being all different factors (60, 24,
and 7), it's the same fundamental as the 10-based digit split. If you
get stuck, tell us how far you got and we can help you over the next
hump.
ChrisA
More information about the Python-list
mailing list