Python 2.7.6 help with modules

Rustom Mody rustompmody at gmail.com
Sat Feb 8 00:50:54 EST 2014


On Saturday, February 8, 2014 10:35:49 AM UTC+5:30, Rustom Mody wrote:
> On Saturday, February 8, 2014 10:14:10 AM UTC+5:30, Scott W Dunning wrote:
> 
> > I have a question that was a part of my homework and I got it correct but the teacher urged me to do it using the % sign rather than subtracting everything, for some reason I'm having issues getting it to calculate correctly.  I'll put the question below, and what I originally had and below that what I've been working on with the %.   

One more general hint.
If you write code with inputs and prints, you will end up writing messy code.
The better alternative is to write pure functions -- inputs in arguments, outputs
returned, not printed.

To see the difference, look at a trivial subset of your problem:
Convert seconds into minutes and seconds.
I'll write it in your (print version) and an rv (return version)

>>> def secs2minsec():
...   sec = int(raw_input("Enter number of secs "))
...   min, sec = divmod(sec,60)
...   print min, "mins", sec, "secs"

>>> def secs2minsecsrv(sec): return divmod(sec,60)


Now you may think the second one is trivial and useless.
However if you slightly increase the problem to adding hours, you will find that
you can REUSE secs2minsecsrv whereas secs2minsec is more or less useless



More information about the Python-list mailing list