{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "<small><small><i>\n", "All the IPython Notebooks in this example series by Dr. Milan Parmar are available @ **[GitHub](https://github.com/milaan9/90_Python_Examples/tree/main/07_Python_Advanced_examples)**\n", "</i></small></small>" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Python Program to Create a Countdown Timer\n", "\n", "In this example, you will learn to create a countdown timer.\n", "\n", "To understand this example, you should have the knowledge of the following **[Python programming](https://github.com/milaan9/01_Python_Introduction/blob/main/000_Intro_to_Python.ipynb)** topics:\n", "\n", "* **[Python while Loop](https://github.com/milaan9/03_Python_Flow_Control/blob/main/006_Python_while_Loop.ipynb)**\n", "* **[Python divmod()](https://github.com/milaan9/04_Python_Functions/blob/main/002_Python_Functions_Built_in/017_Python_divmod%28%29.ipynb)**\n", "* **[Python time Module](https://github.com/milaan9/08_Python_Date_Time_Module/blob/main/007_Python_time_Module.ipynb)**" ] }, { "cell_type": "code", "execution_count": 1, "metadata": { "ExecuteTime": { "end_time": "2021-07-02T07:10:36.277472Z", "start_time": "2021-07-02T07:10:31.178383Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "stop1\n" ] }, { "data": { "text/plain": [ "'\\n>>Expected output:\\n \\n00:05\\n00:04\\n00:03\\n00:02\\n00:01\\nstop1\\n'" ] }, "execution_count": 1, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Example 1: Countdown time in Python\n", "\n", "import time\n", "\n", "def countdown(time_sec):\n", " while time_sec:\n", " mins, secs = divmod(time_sec, 60)\n", " timeformat = '{:02d}:{:02d}'.format(mins, secs)\n", " print(timeformat, end='\\r')\n", " time.sleep(1)\n", " time_sec -= 1\n", "\n", " print(\"stop\")\n", "\n", "countdown(5)\n", "\n", "'''\n", ">>Expected output:\n", " \n", "00:05\n", "00:04\n", "00:03\n", "00:02\n", "00:01\n", "stop1\n", "'''" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**Explanation:**\n", "\n", "The **`divmod()`** method takes two numbers and returns a pair of numbers (a tuple) consisting of their quotient and remainder.\n", "\n", "**`end='\\r'`** overwrites the output for each iteration. The value of **`time_sec`** is decremented at the end of each iteration." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "hide_input": false, "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.8.8" }, "toc": { "base_numbering": 1, "nav_menu": {}, "number_sections": true, "sideBar": true, "skip_h1_title": false, "title_cell": "Table of Contents", "title_sidebar": "Contents", "toc_cell": false, "toc_position": {}, "toc_section_display": true, "toc_window_display": false }, "varInspector": { "cols": { "lenName": 16, "lenType": 16, "lenVar": 40 }, "kernels_config": { "python": { "delete_cmd_postfix": "", "delete_cmd_prefix": "del ", "library": "var_list.py", "varRefreshCmd": "print(var_dic_list())" }, "r": { "delete_cmd_postfix": ") ", "delete_cmd_prefix": "rm(", "library": "var_list.r", "varRefreshCmd": "cat(var_dic_list()) " } }, "types_to_exclude": [ "module", "function", "builtin_function_or_method", "instance", "_Feature" ], "window_display": false } }, "nbformat": 4, "nbformat_minor": 4 }