I have the below code and want to create a countdown clock that counts down from 10 to 9, 8, 7 etc.

I want one number to replace the previous on the same line and same position (i.e. you see one number at any time).

Is there a print method to do that?

I am new to programming and found that os.system('cls') can do the trick but there must be a more eloquent solution?

 import time

    def stopwatch(seconds):
        start = time.time()
        time.clock()    
        elapsed = 0
        while elapsed < seconds:
            elapsed = time.time() - start
            print ("loop cycle time: %f, seconds count: %02d" % (time.clock() , elapsed), end=" ") 
        time.sleep(1)  

    stopwatch(20)
1

There are 1 best solutions below

0
On

You can add printing \r as below :

print ("loop cycle time: %f, seconds count: %02d" % (time.clock() , elapsed), end=" ") 
print ('\r')

Carriage Return takes cursor to the start of line