Python Timer

3

I am trying to make a timer that after 10 seconds prints a string .

I tried to use time.sleep() and it did not work.

Example:

def tempo():
    #passados 10 segundos
    print "olá"
    
asked by anonymous 14.06.2014 / 20:22

2 answers

4

Use the time.sleep() method.

import time

def tempo():
    time.sleep(10)    
    print "Ola"

To make a stopwatch and print the number on the same line:

import time, sys

for i in range(0, 10):
    sys.stdout.write("\r{}".format(i))
    sys.stdout.flush()
    time.sleep(1)

print ("\nFim")

See demonstração

    
14.06.2014 / 20:23
0

I think a smaller form would be:

from  time import sleep 

for contagem in range(0,10):
    sleep(1)

print('Olá!')
    
05.09.2018 / 02:51