[Tutor] no loops

Christopher Spears cspears2002 at yahoo.com
Wed Jul 12 01:08:36 CEST 2006


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.

-Chris



More information about the Tutor mailing list