[Tutor] no loops

Alan Gauld alan.gauld at freenet.co.uk
Wed Jul 12 08:48:38 CEST 2006


> def increment(time, seconds):
>  time.seconds = time.seconds + seconds
> 
>  while time.seconds >= 60:
>    time.seconds = time.seconds - 60
>    time.minutes = time.minutes + 1

Tale a look at what this loop is doing.
Think about its purpose. If you werre doing this 
with paper and pencil would you really use iteration?
Think division....

> 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.

The loops are implementing a mathematical function 
which doesn't need a loop. Look at the division and 
modulo operators.

Alan Gauld
Author of the Learn to Program web site
http://www.freenetpages.co.uk/hp/alan.gauld



More information about the Tutor mailing list