[Tutor] no loops

Bob Gailer bgailer at alum.rpi.edu
Wed Jul 12 03:53:03 CEST 2006


Christopher Spears wrote:
> I am working on another problem from "How To Think
> Like A Computer Scientist".  Here is a function:
>
> def increment(time, seconds):
>   time.seconds = time.seconds + seconds
>
>   while time.seconds >= 60:
>     time.seconds = time.seconds - 60
>     time.minutes = time.minutes + 1
>
>   while time.minutes >= 60:
>     time.minutes = time.minutes - 60
>     time.hours = time.hours + 1 
>
> Here is the function in action:
>
>   
>>>> from time import *
>>>> atime = Time()
>>>> atime.hours = 1
>>>> atime.minutes = 60
>>>> atime.seconds = 120
>>>> printTime(atime)
>>>>         
> 1:60:120
>   
>>>> increment(atime,1)
>>>> printTime(atime)
>>>>         
> 2:2:1
>
> Now the exercise is:
> As an exercise, rewrite this function so that it
> doesn't contain any loops.
>
> I have been staring at this function and drawing a
> blank.  Something tells me that I need to use
> iteration, but I am not sure how I could implement it.
>   
take a look at the divmod built-in function.
>   


-- 
Bob Gailer
510-978-4454



More information about the Tutor mailing list