Python timer countdown

Deadly 提交于 2019-12-01 11:54:48

Use time.sleep.

import time

def abc():
    print 'Hi'
    print 'Hello'
    print 'Hai'

for i in xrange(3):
    time.sleep(1)
    abc()   

time.sleep is fine in this case but what if the abc() function takes half a second to execute? Or 5 minutes? In this case you should use a Timer object.

from threading import Timer

def abc():
    print 'Hi'  
    print 'Hello'
    print 'Hai'

for i in xrange(3):
    Timer(i, abc).start()

You should look into time.sleep(). For example:

for i in xrange(5):
  abc()
  time.sleep(3)

That will print your lines 5 times with a 3 second delay between.

import time
def abc()
    for i in range(3):
        print 'Hi'  
        print 'Hello'
        print 'Hai'
        time.sleep(1)
import time
def abc():
 print 'Hi'
 print 'Hello'
 print 'Hai'

for i in range(3):
 time.sleep(3-i)
 abc()

usually for me this works...

import time

def abc():
    print 'Hi'
    time.sleep(1)
    print 'Hello'
    time.sleep(1)
    print 'Hai'
    time.sleep(1)

I think you can guess after that...

import sys
import time

c=':'
sec = 0
min = 0
hour = 0

#count up clock

while True:
for y in range(59):                                                 #hours
    for x in range (59):                                            #min
        sec = sec+1
        sec1 = ('%02.f' % sec)                                      #format
        min1 = ('%02.f' % min)
        hour1= ('%02.f' % hour)
        sys.stdout.write('\r'+str(hour1)+c+str(min1)+c+str(sec1))   #clear and write
        time.sleep(1)
    sec = 0
    sys.stdout.write('\r' + str(hour1) + c + str(min1) + c + '00')  #ensure proper timing and display
    time.sleep(1)
    min=min+1
min = 0
sys.stdout.write('\r' + str(hour1) + c + str(min1) + c + '00')      #ensure proper timing and display
time.sleep(1)
hour=hour+1
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!