In this example, you will learn to create a countdown timer.
To understand this example, you should have the knowledge of the following Python programming topics:
- Python while Loop
- Python divmod()
- Python time Module
Countdown time in Python
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)
- 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_secis decremented at the end of each iteration.
Related posts:
Building Chatbots with Python Using Natural Language Processing and Machine Learning - Sumit Raj
Python Set symmetric_difference()
Python Set copy()
Python Program to Find LCM
Introduction to Scientific Programming with Python - Joakim Sundnes
Python Objects and Classes
Python Dictionary popitem()
Python Deep Learning - Valentino Zocca & Gianmario Spacagna & Daniel Slater & Peter Roelants
Python Data Analytics with Pandas, NumPy and Matplotlib - Fabio Nelli
Python Tuple count()
Python tuple()
Python Object Oriented Programming
Python Program to Find Armstrong Number in an Interval
Python Program to Check if a Number is Odd or Even
Python Program to Find Hash of File
Machine Learning Applications Using Python - Cases studies form Healthcare, Retail, and Finance - Pu...
Python Program to Display Fibonacci Sequence Using Recursion
Python time Module
Python type()
Python Program to Get the Full Path of the Current Working Directory
Python Program to Find the Sum of Natural Numbers
Debug a JavaMail Program
Python String lower()
Python Program to Compute the Power of a Number
Python String rjust()
Python len()
Python Machine Learning - Sebastian Raschka
Python Set isdisjoint()
Python Operators
Deep Learning in Python - LazyProgrammer
Python oct()
Python Machine Learning Eqution Reference - Sebastian Raschka