Create a Countdown Timer with Python

Configurare noua (How To)

Situatie

  • The divmod() method takes two numbers and returns a pair of numbers (a tuple) consisting of their quotient and remainder.
  • end='\r' overwrites the output for each iteration.
  • The value of time_sec is decremented at the end of each iteration.

Backup

import time

def countdown(time_sec):
    while time_sec:
        mins, secs = divmod(time_sec, 60)
        timeformat = '{:02d}:{:02d}'.format(mins, secs)
        print(timeformat, end='\r')
        time.sleep(1)
        time_sec -= 1

    print("stop")

countdown(5)

Solutie

Tip solutie

Permanent

Voteaza

(1 din 4 persoane apreciaza acest articol)

Despre Autor

Leave A Comment?