[Tutor] setting a timer

Steven D'Aprano steve at pearwood.info
Wed Sep 12 20:24:07 CEST 2012


On 13/09/12 03:56, Matthew Ngaha wrote:

> class Game(object):
>      interval = 0
>
>      def play(self):
>          Game.interval = 40
>          while Game.interval>  0:
>                      self.count_down()
>
>      def count_down(self):
>          Game.interval -= 1
>
> so the play() method gives interval a value of 40. the while loop
> should reduce interval by 1 everytime it calls the count_down()
> method. The problem is this is happening instantly. i've even put the
> Game.interval value as high as 50000 and it reaches 0 instantly.

Get a slower computer.

I expect that if you find an old Commodore 64 from 1982, or perhaps
an 1984 Apple Macintosh, it might be slow enough for your count down
idea to work. But with modern computers, counting up to, or down from,
50000 is more or less instantaneous in human terms.

A better way to pause for a moment is this:

import time
time.sleep(0.1)  # pause for 0.1 second
time.sleep(60)  # pause for 1 minutes




-- 
Steven


More information about the Tutor mailing list