{"id":12380,"date":"2021-10-01T11:00:19","date_gmt":"2021-10-01T05:30:19","guid":{"rendered":"https:\/\/python-programs.com\/?p=12380"},"modified":"2021-11-22T18:33:27","modified_gmt":"2021-11-22T13:03:27","slug":"python-program-to-create-a-countdown-timer","status":"publish","type":"post","link":"https:\/\/python-programs.com\/python-program-to-create-a-countdown-timer\/","title":{"rendered":"Python Program to Create a Countdown Timer"},"content":{"rendered":"
If you are new to Java and want to learn the java coding skills too fast. Try practicing the core java programs with the help of the Java basic programs list<\/a> available.<\/p>\n We’ll look at how to make a countdown timer in Python. The code will accept the user’s input on the length of the countdown in seconds. Following that, a countdown in the format \u2018minutes: seconds’ will commence on the screen. Here, we’ll make use of the time module.<\/p>\n Below are the ways to Create a Countdown Timer in Python.<\/p>\n The time module and its sleep() function will be used. To make a countdown timer, follow the steps below:<\/p>\n Approach:<\/strong><\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n The time module and its sleep() function will be used. To make a countdown timer, follow the steps below:<\/p>\n Approach:<\/strong><\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n Related Programs<\/strong>:<\/p>\n If you are new to Java and want to learn the java coding skills too fast. Try practicing the core java programs with the help of the Java basic programs list available. We’ll look at how to make a countdown timer in Python. The code will accept the user’s input on the length of the …<\/p>\nPython Program to Create a Countdown Timer<\/h2>\n
\n
Method #1: Using While Loop (Static Input)<\/h3>\n
\n
# Import the time module using the import statement.\r\nimport time\r\n\r\n# creating a function time which counts the time\r\n\r\n\r\ndef countdownTime(tim):\r\n # A while loop is used in this function until the time reaches zero.\r\n while (tim):\r\n # To calculate the number of minutes and seconds, use divmod().\u00a0\r\n timemins, timesecs = divmod(tim, 60)\r\n # Now, using the variable timeformat, output the minutes and seconds on the screen.\r\n timercount = '{:02d}:{:02d}'.format(timemins, timesecs)\r\n # By using end = '\\n', we force the cursor to return to the\r\n # beginning of the screen (carriage return),\r\n # causing the following line written to overwrite the preceding one.\r\n print(timercount, end=\"\\n\")\r\n # The time. sleep() function is used to make the program wait one second.\r\n time.sleep(1)\r\n # Now decrement time so that the while loop can reach a finish.\r\n tim -= 1\r\n # After the loop is completed, we will print \"The Given time is completed\" to indicate the conclusion of the countdown.\r\n print('The Given time is completed')\r\n\r\n\r\n# Then in seconds, Give the length of the countdown as static input and store it in a variable.\r\ntim = 5\r\n\r\n# Passing the given time as argument to the countdownTime() function\r\ncountdownTime(tim)\r\n<\/pre>\n
00:05\r\n00:04\r\n00:03\r\n00:02\r\n00:01\r\nThe Given time is completed<\/pre>\n
Method #2: Using While Loop (User Input)<\/h3>\n
\n
# Import the time module using the import statement.\r\nimport time\r\n\r\n# creating a function time which counts the time\r\n\r\n\r\ndef countdownTime(tim):\r\n # A while loop is used in this function until the time reaches zero.\r\n while (tim):\r\n # To calculate the number of minutes and seconds, use divmod().\u00a0\r\n timemins, timesecs = divmod(tim, 60)\r\n # Now, using the variable timeformat, output the minutes and seconds on the screen.\r\n timercount = '{:02d}:{:02d}'.format(timemins, timesecs)\r\n # By using end = '\\n', we force the cursor to return to the\r\n # beginning of the screen (carriage return),\r\n # causing the following line written to overwrite the preceding one.\r\n print(timercount, end=\"\\n\")\r\n # The time. sleep() function is used to make the program wait one second.\r\n time.sleep(1)\r\n # Now decrement time so that the while loop can reach a finish.\r\n tim -= 1\r\n # After the loop is completed, we will print \"The Given time is completed\" to indicate the conclusion of the countdown.\r\n print('The Given time is completed')\r\n\r\n\r\n# Then in seconds, Give the length of the countdown as user input and store it in a variable.\r\ntim = input('Enter some random time = ')\r\n# Convert the given time in seconds to integer data type using int() function.\r\ntim = int(tim)\r\n\r\n# Passing the given time as argument to the countdownTime() function\r\ncountdownTime(tim)\r\n<\/pre>\n
Enter some random time = 4\r\n00:04\r\n00:03\r\n00:02\r\n00:01\r\nThe Given time is completed<\/pre>\n
\n