[{"data":1,"prerenderedAt":1709},["ShallowReactive",2],{"doc-\u002Fexamples\u002Fpython-countdown-timer-example":3},{"id":4,"title":5,"body":6,"description":1702,"extension":1703,"meta":1704,"navigation":73,"path":1705,"seo":1706,"stem":1707,"__hash__":1708},"content\u002Fexamples\u002Fpython-countdown-timer-example.md","Python Countdown Timer Example",{"type":7,"value":8,"toc":1655},"minimark",[9,13,22,25,38,41,46,185,188,192,195,206,209,230,234,237,240,254,257,268,272,275,363,366,391,395,398,405,416,422,429,435,448,451,458,464,481,484,497,506,513,519,534,537,542,561,564,567,573,586,592,595,613,618,622,641,644,647,651,654,662,665,669,672,780,784,803,812,815,835,839,846,873,879,882,908,918,922,925,942,945,1056,1059,1169,1173,1176,1182,1194,1197,1258,1261,1271,1280,1285,1287,1312,1315,1317,1344,1348,1351,1363,1366,1370,1373,1389,1392,1399,1404,1408,1411,1415,1429,1432,1436,1456,1459,1463,1484,1490,1494,1497,1522,1526,1529,1562,1566,1570,1573,1580,1589,1596,1605,1609,1612,1616,1648,1651],[10,11,5],"h1",{"id":12},"python-countdown-timer-example",[14,15,16,17,21],"p",{},"A countdown timer is a great beginner Python project. It helps you practice variables, loops, and pauses with ",[18,19,20],"code",{},"sleep()",".",[14,23,24],{},"This example shows how to build a simple countdown timer that:",[26,27,28,32,35],"ul",{},[29,30,31],"li",{},"counts down one number at a time",[29,33,34],{},"waits 1 second between numbers",[29,36,37],{},"prints a final message when it finishes",[14,39,40],{},"Start with the basic version first. Once it works, you can safely change the starting number or add user input.",[42,43,45],"h2",{"id":44},"quick-example","Quick example",[47,48,53],"pre",{"className":49,"code":50,"language":51,"meta":52,"style":52},"language-python shiki shiki-themes material-theme-lighter github-light github-dark","import time\n\nseconds = 5\n\nwhile seconds > 0:\n    print(seconds)\n    time.sleep(1)\n    seconds -= 1\n\nprint(\"Time's up!\")\n","python","",[18,54,55,68,75,89,94,113,130,148,160,165],{"__ignoreMap":52},[56,57,60,64],"span",{"class":58,"line":59},"line",1,[56,61,63],{"class":62},"sVHd0","import",[56,65,67],{"class":66},"su5hD"," time\n",[56,69,71],{"class":58,"line":70},2,[56,72,74],{"emptyLinePlaceholder":73},true,"\n",[56,76,78,81,85],{"class":58,"line":77},3,[56,79,80],{"class":66},"seconds ",[56,82,84],{"class":83},"smGrS","=",[56,86,88],{"class":87},"srdBf"," 5\n",[56,90,92],{"class":58,"line":91},4,[56,93,74],{"emptyLinePlaceholder":73},[56,95,97,100,103,106,109],{"class":58,"line":96},5,[56,98,99],{"class":62},"while",[56,101,102],{"class":66}," seconds ",[56,104,105],{"class":83},">",[56,107,108],{"class":87}," 0",[56,110,112],{"class":111},"sP7_E",":\n",[56,114,116,120,123,127],{"class":58,"line":115},6,[56,117,119],{"class":118},"sptTA","    print",[56,121,122],{"class":111},"(",[56,124,126],{"class":125},"slqww","seconds",[56,128,129],{"class":111},")\n",[56,131,133,136,138,141,143,146],{"class":58,"line":132},7,[56,134,135],{"class":66},"    time",[56,137,21],{"class":111},[56,139,140],{"class":125},"sleep",[56,142,122],{"class":111},[56,144,145],{"class":87},"1",[56,147,129],{"class":111},[56,149,151,154,157],{"class":58,"line":150},8,[56,152,153],{"class":66},"    seconds ",[56,155,156],{"class":83},"-=",[56,158,159],{"class":87}," 1\n",[56,161,163],{"class":58,"line":162},9,[56,164,74],{"emptyLinePlaceholder":73},[56,166,168,171,173,177,181,183],{"class":58,"line":167},10,[56,169,170],{"class":118},"print",[56,172,122],{"class":111},[56,174,176],{"class":175},"sjJ54","\"",[56,178,180],{"class":179},"s_sjI","Time's up!",[56,182,176],{"class":175},[56,184,129],{"class":111},[14,186,187],{},"This is the fastest working version. It counts down from 5 to 1, waits 1 second between numbers, then prints a finish message.",[42,189,191],{"id":190},"what-this-example-builds","What this example builds",[14,193,194],{},"This script creates:",[26,196,197,200,203],{},[29,198,199],{},"A countdown that prints one number at a time",[29,201,202],{},"A pause of 1 second between each number",[29,204,205],{},"A final message when the countdown ends",[14,207,208],{},"It is a good first project for practicing:",[26,210,211,214,219],{},[29,212,213],{},"variables",[29,215,216,218],{},[18,217,99],{}," loops",[29,220,221,222],{},"the ",[223,224,226,229],"a",{"href":225},"\u002Fstandard-library\u002Ftime.sleep-function-explained",[18,227,228],{},"time.sleep()"," function",[42,231,233],{"id":232},"before-you-start","Before you start",[14,235,236],{},"You do not need much to follow this example.",[14,238,239],{},"It helps if you already know:",[26,241,242,245,251],{},[29,243,244],{},"basic variables",[29,246,247,248,250],{},"the idea of a ",[18,249,99],{}," loop, or are willing to follow one simple loop",[29,252,253],{},"how to run a Python file",[14,255,256],{},"You also need Python installed.",[14,258,259,260,263,264,21],{},"This example uses Python’s built-in ",[18,261,262],{},"time"," module. You can learn more in this ",[223,265,267],{"href":266},"\u002Fstandard-library\u002Fpython-time-module-overview\u002F","Python time module overview",[42,269,271],{"id":270},"simple-countdown-timer-example","Simple countdown timer example",[14,273,274],{},"Here is the full script again:",[47,276,277],{"className":49,"code":50,"language":51,"meta":52,"style":52},[18,278,279,285,289,297,301,313,323,337,345,349],{"__ignoreMap":52},[56,280,281,283],{"class":58,"line":59},[56,282,63],{"class":62},[56,284,67],{"class":66},[56,286,287],{"class":58,"line":70},[56,288,74],{"emptyLinePlaceholder":73},[56,290,291,293,295],{"class":58,"line":77},[56,292,80],{"class":66},[56,294,84],{"class":83},[56,296,88],{"class":87},[56,298,299],{"class":58,"line":91},[56,300,74],{"emptyLinePlaceholder":73},[56,302,303,305,307,309,311],{"class":58,"line":96},[56,304,99],{"class":62},[56,306,102],{"class":66},[56,308,105],{"class":83},[56,310,108],{"class":87},[56,312,112],{"class":111},[56,314,315,317,319,321],{"class":58,"line":115},[56,316,119],{"class":118},[56,318,122],{"class":111},[56,320,126],{"class":125},[56,322,129],{"class":111},[56,324,325,327,329,331,333,335],{"class":58,"line":132},[56,326,135],{"class":66},[56,328,21],{"class":111},[56,330,140],{"class":125},[56,332,122],{"class":111},[56,334,145],{"class":87},[56,336,129],{"class":111},[56,338,339,341,343],{"class":58,"line":150},[56,340,153],{"class":66},[56,342,156],{"class":83},[56,344,159],{"class":87},[56,346,347],{"class":58,"line":162},[56,348,74],{"emptyLinePlaceholder":73},[56,350,351,353,355,357,359,361],{"class":58,"line":167},[56,352,170],{"class":118},[56,354,122],{"class":111},[56,356,176],{"class":175},[56,358,180],{"class":179},[56,360,176],{"class":175},[56,362,129],{"class":111},[14,364,365],{},"This version works like this:",[26,367,368,371,377,380,383,388],{},[29,369,370],{},"Start with a number stored in a variable",[29,372,373,374],{},"Keep looping while the number is greater than ",[18,375,376],{},"0",[29,378,379],{},"Print the current number",[29,381,382],{},"Pause for 1 second",[29,384,385,386],{},"Subtract ",[18,387,145],{},[29,389,390],{},"Print a final message after the loop ends",[42,392,394],{"id":393},"how-the-code-works","How the code works",[14,396,397],{},"Let’s go through it line by line.",[399,400,402],"h3",{"id":401},"import-time",[18,403,404],{},"import time",[47,406,408],{"className":49,"code":407,"language":51,"meta":52,"style":52},"import time\n",[18,409,410],{"__ignoreMap":52},[56,411,412,414],{"class":58,"line":59},[56,413,63],{"class":62},[56,415,67],{"class":66},[14,417,418,419,421],{},"This gives your program access to the ",[18,420,262],{}," module.",[14,423,424,425,428],{},"Without this line, ",[18,426,427],{},"time.sleep(1)"," will not work.",[399,430,432],{"id":431},"seconds-5",[18,433,434],{},"seconds = 5",[47,436,438],{"className":49,"code":437,"language":51,"meta":52,"style":52},"seconds = 5\n",[18,439,440],{"__ignoreMap":52},[56,441,442,444,446],{"class":58,"line":59},[56,443,80],{"class":66},[56,445,84],{"class":83},[56,447,88],{"class":87},[14,449,450],{},"This sets the starting value for the countdown.",[14,452,453,454,457],{},"If you change it to ",[18,455,456],{},"10",", the timer will count down from 10 instead.",[399,459,461],{"id":460},"while-seconds-0",[18,462,463],{},"while seconds > 0",[47,465,467],{"className":49,"code":466,"language":51,"meta":52,"style":52},"while seconds > 0:\n",[18,468,469],{"__ignoreMap":52},[56,470,471,473,475,477,479],{"class":58,"line":59},[56,472,99],{"class":62},[56,474,102],{"class":66},[56,476,105],{"class":83},[56,478,108],{"class":87},[56,480,112],{"class":111},[14,482,483],{},"This means:",[26,485,486,489],{},[29,487,488],{},"keep running the loop",[29,490,491,492,494,495],{},"only while ",[18,493,126],{}," is greater than ",[18,496,376],{},[14,498,499,500,502,503,505],{},"When ",[18,501,126],{}," becomes ",[18,504,376],{},", the loop stops.",[14,507,508,509,21],{},"If you are new to loops, see ",[223,510,512],{"href":511},"\u002Flearn\u002Fpython-while-loops-explained\u002F","Python while loops explained",[399,514,516],{"id":515},"printseconds",[18,517,518],{},"print(seconds)",[47,520,522],{"className":49,"code":521,"language":51,"meta":52,"style":52},"print(seconds)\n",[18,523,524],{"__ignoreMap":52},[56,525,526,528,530,532],{"class":58,"line":59},[56,527,170],{"class":118},[56,529,122],{"class":111},[56,531,126],{"class":125},[56,533,129],{"class":111},[14,535,536],{},"This shows the current countdown number on the screen.",[399,538,540],{"id":539},"timesleep1",[18,541,427],{},[47,543,545],{"className":49,"code":544,"language":51,"meta":52,"style":52},"time.sleep(1)\n",[18,546,547],{"__ignoreMap":52},[56,548,549,551,553,555,557,559],{"class":58,"line":59},[56,550,262],{"class":66},[56,552,21],{"class":111},[56,554,140],{"class":125},[56,556,122],{"class":111},[56,558,145],{"class":87},[56,560,129],{"class":111},[14,562,563],{},"This pauses the program for 1 second.",[14,565,566],{},"It does not print anything by itself. It only makes the program wait.",[399,568,570],{"id":569},"seconds-1",[18,571,572],{},"seconds -= 1",[47,574,576],{"className":49,"code":575,"language":51,"meta":52,"style":52},"seconds -= 1\n",[18,577,578],{"__ignoreMap":52},[56,579,580,582,584],{"class":58,"line":59},[56,581,80],{"class":66},[56,583,156],{"class":83},[56,585,159],{"class":87},[14,587,588,589,591],{},"This reduces ",[18,590,126],{}," by 1 each time through the loop.",[14,593,594],{},"It is the same as writing:",[47,596,598],{"className":49,"code":597,"language":51,"meta":52,"style":52},"seconds = seconds - 1\n",[18,599,600],{"__ignoreMap":52},[56,601,602,604,606,608,611],{"class":58,"line":59},[56,603,80],{"class":66},[56,605,84],{"class":83},[56,607,102],{"class":66},[56,609,610],{"class":83},"-",[56,612,159],{"class":87},[14,614,615,616,21],{},"This line is very important. Without it, the countdown would never move toward ",[18,617,376],{},[399,619,621],{"id":620},"final-message","Final message",[47,623,625],{"className":49,"code":624,"language":51,"meta":52,"style":52},"print(\"Time's up!\")\n",[18,626,627],{"__ignoreMap":52},[56,628,629,631,633,635,637,639],{"class":58,"line":59},[56,630,170],{"class":118},[56,632,122],{"class":111},[56,634,176],{"class":175},[56,636,180],{"class":179},[56,638,176],{"class":175},[56,640,129],{"class":111},[14,642,643],{},"This line runs only after the loop finishes.",[14,645,646],{},"That is why you see the message at the end, not during the countdown.",[42,648,650],{"id":649},"expected-output","Expected output",[14,652,653],{},"When you run the script, you should see something like this:",[47,655,660],{"className":656,"code":658,"language":659,"meta":52},[657],"language-text","5\n4\n3\n2\n1\nTime's up!\n","text",[18,661,658],{"__ignoreMap":52},[14,663,664],{},"There will be a 1-second pause between each number.",[42,666,668],{"id":667},"version-that-gets-user-input","Version that gets user input",[14,670,671],{},"You can also ask the user for the starting number.",[47,673,675],{"className":49,"code":674,"language":51,"meta":52,"style":52},"import time\n\nseconds = int(input(\"Enter the number of seconds: \"))\n\nwhile seconds > 0:\n    print(seconds)\n    time.sleep(1)\n    seconds -= 1\n\nprint(\"Time's up!\")\n",[18,676,677,683,687,714,718,730,740,754,762,766],{"__ignoreMap":52},[56,678,679,681],{"class":58,"line":59},[56,680,63],{"class":62},[56,682,67],{"class":66},[56,684,685],{"class":58,"line":70},[56,686,74],{"emptyLinePlaceholder":73},[56,688,689,691,693,697,699,702,704,706,709,711],{"class":58,"line":77},[56,690,80],{"class":66},[56,692,84],{"class":83},[56,694,696],{"class":695},"sZMiF"," int",[56,698,122],{"class":111},[56,700,701],{"class":118},"input",[56,703,122],{"class":111},[56,705,176],{"class":175},[56,707,708],{"class":179},"Enter the number of seconds: ",[56,710,176],{"class":175},[56,712,713],{"class":111},"))\n",[56,715,716],{"class":58,"line":91},[56,717,74],{"emptyLinePlaceholder":73},[56,719,720,722,724,726,728],{"class":58,"line":96},[56,721,99],{"class":62},[56,723,102],{"class":66},[56,725,105],{"class":83},[56,727,108],{"class":87},[56,729,112],{"class":111},[56,731,732,734,736,738],{"class":58,"line":115},[56,733,119],{"class":118},[56,735,122],{"class":111},[56,737,126],{"class":125},[56,739,129],{"class":111},[56,741,742,744,746,748,750,752],{"class":58,"line":132},[56,743,135],{"class":66},[56,745,21],{"class":111},[56,747,140],{"class":125},[56,749,122],{"class":111},[56,751,145],{"class":87},[56,753,129],{"class":111},[56,755,756,758,760],{"class":58,"line":150},[56,757,153],{"class":66},[56,759,156],{"class":83},[56,761,159],{"class":87},[56,763,764],{"class":58,"line":162},[56,765,74],{"emptyLinePlaceholder":73},[56,767,768,770,772,774,776,778],{"class":58,"line":167},[56,769,170],{"class":118},[56,771,122],{"class":111},[56,773,176],{"class":175},[56,775,180],{"class":179},[56,777,176],{"class":175},[56,779,129],{"class":111},[399,781,783],{"id":782},"how-this-version-is-different","How this version is different",[26,785,786,792,797],{},[29,787,788,791],{},[18,789,790],{},"input()"," asks the user to type something",[29,793,794,796],{},[18,795,790],{}," returns text",[29,798,799,802],{},[18,800,801],{},"int()"," converts that text to a whole number",[14,804,805,806,809,810,21],{},"So if the user types ",[18,807,808],{},"5",", Python turns it into the number ",[18,811,808],{},[14,813,814],{},"If you want to learn more, see:",[26,816,817,823,829],{},[29,818,819],{},[223,820,822],{"href":821},"\u002Freference\u002Fpython-input-function-explained\u002F","Python input() function explained",[29,824,825],{},[223,826,828],{"href":827},"\u002Freference\u002Fpython-int-function-explained\u002F","Python int() function explained",[29,830,831],{},[223,832,834],{"href":833},"\u002Fhow-to\u002Fhow-to-convert-user-input-to-numbers-in-python\u002F","How to convert user input to numbers in Python",[399,836,838],{"id":837},"important-note-about-bad-input","Important note about bad input",[14,840,841,842,845],{},"If the user types something like ",[18,843,844],{},"hello"," instead of a number, this line will fail:",[47,847,849],{"className":49,"code":848,"language":51,"meta":52,"style":52},"seconds = int(input(\"Enter the number of seconds: \"))\n",[18,850,851],{"__ignoreMap":52},[56,852,853,855,857,859,861,863,865,867,869,871],{"class":58,"line":59},[56,854,80],{"class":66},[56,856,84],{"class":83},[56,858,696],{"class":695},[56,860,122],{"class":111},[56,862,701],{"class":118},[56,864,122],{"class":111},[56,866,176],{"class":175},[56,868,708],{"class":179},[56,870,176],{"class":175},[56,872,713],{"class":111},[14,874,875,876,21],{},"That raises a ",[18,877,878],{},"ValueError",[14,880,881],{},"Example:",[47,883,884],{"className":49,"code":848,"language":51,"meta":52,"style":52},[18,885,886],{"__ignoreMap":52},[56,887,888,890,892,894,896,898,900,902,904,906],{"class":58,"line":59},[56,889,80],{"class":66},[56,891,84],{"class":83},[56,893,696],{"class":695},[56,895,122],{"class":111},[56,897,701],{"class":118},[56,899,122],{"class":111},[56,901,176],{"class":175},[56,903,708],{"class":179},[56,905,176],{"class":175},[56,907,713],{"class":111},[14,909,910,911,21],{},"If the input is not a valid whole number, read ",[223,912,914,915],{"href":913},"\u002Ferrors\u002Fvalueerror-invalid-literal-for-int-with-base-10-fix\u002F","how to fix ",[18,916,917],{},"ValueError: invalid literal for int() with base 10",[42,919,921],{"id":920},"small-improvements-beginners-can-try","Small improvements beginners can try",[14,923,924],{},"Once the basic version works, try small changes like these:",[26,926,927,930,933,936,939],{},[29,928,929],{},"Change the finish message",[29,931,932],{},"Start from a different number",[29,934,935],{},"Add a message before the countdown starts",[29,937,938],{},"Wrap the code in a function",[29,940,941],{},"Build a later version that shows minutes and seconds",[14,943,944],{},"Example with a start message:",[47,946,948],{"className":49,"code":947,"language":51,"meta":52,"style":52},"import time\n\nseconds = 5\n\nprint(\"Starting countdown...\")\n\nwhile seconds > 0:\n    print(seconds)\n    time.sleep(1)\n    seconds -= 1\n\nprint(\"Done!\")\n",[18,949,950,956,960,968,972,987,991,1003,1013,1027,1035,1040],{"__ignoreMap":52},[56,951,952,954],{"class":58,"line":59},[56,953,63],{"class":62},[56,955,67],{"class":66},[56,957,958],{"class":58,"line":70},[56,959,74],{"emptyLinePlaceholder":73},[56,961,962,964,966],{"class":58,"line":77},[56,963,80],{"class":66},[56,965,84],{"class":83},[56,967,88],{"class":87},[56,969,970],{"class":58,"line":91},[56,971,74],{"emptyLinePlaceholder":73},[56,973,974,976,978,980,983,985],{"class":58,"line":96},[56,975,170],{"class":118},[56,977,122],{"class":111},[56,979,176],{"class":175},[56,981,982],{"class":179},"Starting countdown...",[56,984,176],{"class":175},[56,986,129],{"class":111},[56,988,989],{"class":58,"line":115},[56,990,74],{"emptyLinePlaceholder":73},[56,992,993,995,997,999,1001],{"class":58,"line":132},[56,994,99],{"class":62},[56,996,102],{"class":66},[56,998,105],{"class":83},[56,1000,108],{"class":87},[56,1002,112],{"class":111},[56,1004,1005,1007,1009,1011],{"class":58,"line":150},[56,1006,119],{"class":118},[56,1008,122],{"class":111},[56,1010,126],{"class":125},[56,1012,129],{"class":111},[56,1014,1015,1017,1019,1021,1023,1025],{"class":58,"line":162},[56,1016,135],{"class":66},[56,1018,21],{"class":111},[56,1020,140],{"class":125},[56,1022,122],{"class":111},[56,1024,145],{"class":87},[56,1026,129],{"class":111},[56,1028,1029,1031,1033],{"class":58,"line":167},[56,1030,153],{"class":66},[56,1032,156],{"class":83},[56,1034,159],{"class":87},[56,1036,1038],{"class":58,"line":1037},11,[56,1039,74],{"emptyLinePlaceholder":73},[56,1041,1043,1045,1047,1049,1052,1054],{"class":58,"line":1042},12,[56,1044,170],{"class":118},[56,1046,122],{"class":111},[56,1048,176],{"class":175},[56,1050,1051],{"class":179},"Done!",[56,1053,176],{"class":175},[56,1055,129],{"class":111},[14,1057,1058],{},"Example wrapped in a function:",[47,1060,1062],{"className":49,"code":1061,"language":51,"meta":52,"style":52},"import time\n\ndef countdown(seconds):\n    while seconds > 0:\n        print(seconds)\n        time.sleep(1)\n        seconds -= 1\n    print(\"Time's up!\")\n\ncountdown(5)\n",[18,1063,1064,1070,1074,1092,1105,1116,1131,1140,1154,1158],{"__ignoreMap":52},[56,1065,1066,1068],{"class":58,"line":59},[56,1067,63],{"class":62},[56,1069,67],{"class":66},[56,1071,1072],{"class":58,"line":70},[56,1073,74],{"emptyLinePlaceholder":73},[56,1075,1076,1080,1084,1086,1089],{"class":58,"line":77},[56,1077,1079],{"class":1078},"sbsja","def",[56,1081,1083],{"class":1082},"sGLFI"," countdown",[56,1085,122],{"class":111},[56,1087,126],{"class":1088},"sFwrP",[56,1090,1091],{"class":111},"):\n",[56,1093,1094,1097,1099,1101,1103],{"class":58,"line":91},[56,1095,1096],{"class":62},"    while",[56,1098,102],{"class":66},[56,1100,105],{"class":83},[56,1102,108],{"class":87},[56,1104,112],{"class":111},[56,1106,1107,1110,1112,1114],{"class":58,"line":96},[56,1108,1109],{"class":118},"        print",[56,1111,122],{"class":111},[56,1113,126],{"class":125},[56,1115,129],{"class":111},[56,1117,1118,1121,1123,1125,1127,1129],{"class":58,"line":115},[56,1119,1120],{"class":66},"        time",[56,1122,21],{"class":111},[56,1124,140],{"class":125},[56,1126,122],{"class":111},[56,1128,145],{"class":87},[56,1130,129],{"class":111},[56,1132,1133,1136,1138],{"class":58,"line":132},[56,1134,1135],{"class":66},"        seconds ",[56,1137,156],{"class":83},[56,1139,159],{"class":87},[56,1141,1142,1144,1146,1148,1150,1152],{"class":58,"line":150},[56,1143,119],{"class":118},[56,1145,122],{"class":111},[56,1147,176],{"class":175},[56,1149,180],{"class":179},[56,1151,176],{"class":175},[56,1153,129],{"class":111},[56,1155,1156],{"class":58,"line":162},[56,1157,74],{"emptyLinePlaceholder":73},[56,1159,1160,1163,1165,1167],{"class":58,"line":167},[56,1161,1162],{"class":125},"countdown",[56,1164,122],{"class":111},[56,1166,808],{"class":87},[56,1168,129],{"class":111},[42,1170,1172],{"id":1171},"common-beginner-mistakes","Common beginner mistakes",[14,1174,1175],{},"These are the most common problems with simple countdown timers.",[399,1177,1179,1180],{"id":1178},"forgetting-to-import-time","Forgetting to import ",[18,1181,262],{},[14,1183,1184,1185,1187,1188,1190,1191,21],{},"If you use ",[18,1186,427],{}," without importing ",[18,1189,262],{},", Python will raise a ",[18,1192,1193],{},"NameError",[14,1195,1196],{},"Wrong:",[47,1198,1200],{"className":49,"code":1199,"language":51,"meta":52,"style":52},"seconds = 5\n\nwhile seconds > 0:\n    print(seconds)\n    time.sleep(1)\n    seconds -= 1\n",[18,1201,1202,1210,1214,1226,1236,1250],{"__ignoreMap":52},[56,1203,1204,1206,1208],{"class":58,"line":59},[56,1205,80],{"class":66},[56,1207,84],{"class":83},[56,1209,88],{"class":87},[56,1211,1212],{"class":58,"line":70},[56,1213,74],{"emptyLinePlaceholder":73},[56,1215,1216,1218,1220,1222,1224],{"class":58,"line":77},[56,1217,99],{"class":62},[56,1219,102],{"class":66},[56,1221,105],{"class":83},[56,1223,108],{"class":87},[56,1225,112],{"class":111},[56,1227,1228,1230,1232,1234],{"class":58,"line":91},[56,1229,119],{"class":118},[56,1231,122],{"class":111},[56,1233,126],{"class":125},[56,1235,129],{"class":111},[56,1237,1238,1240,1242,1244,1246,1248],{"class":58,"line":96},[56,1239,135],{"class":66},[56,1241,21],{"class":111},[56,1243,140],{"class":125},[56,1245,122],{"class":111},[56,1247,145],{"class":87},[56,1249,129],{"class":111},[56,1251,1252,1254,1256],{"class":58,"line":115},[56,1253,153],{"class":66},[56,1255,156],{"class":83},[56,1257,159],{"class":87},[14,1259,1260],{},"Fix:",[47,1262,1263],{"className":49,"code":407,"language":51,"meta":52,"style":52},[18,1264,1265],{"__ignoreMap":52},[56,1266,1267,1269],{"class":58,"line":59},[56,1268,63],{"class":62},[56,1270,67],{"class":66},[399,1272,1274,1275,1277,1278],{"id":1273},"using-input-without-int","Using ",[18,1276,790],{}," without ",[18,1279,801],{},[14,1281,1282,1284],{},[18,1283,790],{}," returns text, not a number.",[14,1286,1196],{},[47,1288,1290],{"className":49,"code":1289,"language":51,"meta":52,"style":52},"seconds = input(\"Enter seconds: \")\n",[18,1291,1292],{"__ignoreMap":52},[56,1293,1294,1296,1298,1301,1303,1305,1308,1310],{"class":58,"line":59},[56,1295,80],{"class":66},[56,1297,84],{"class":83},[56,1299,1300],{"class":118}," input",[56,1302,122],{"class":111},[56,1304,176],{"class":175},[56,1306,1307],{"class":179},"Enter seconds: ",[56,1309,176],{"class":175},[56,1311,129],{"class":111},[14,1313,1314],{},"If you try to compare that text with numbers in a loop, your program may fail or behave incorrectly.",[14,1316,1260],{},[47,1318,1320],{"className":49,"code":1319,"language":51,"meta":52,"style":52},"seconds = int(input(\"Enter seconds: \"))\n",[18,1321,1322],{"__ignoreMap":52},[56,1323,1324,1326,1328,1330,1332,1334,1336,1338,1340,1342],{"class":58,"line":59},[56,1325,80],{"class":66},[56,1327,84],{"class":83},[56,1329,696],{"class":695},[56,1331,122],{"class":111},[56,1333,701],{"class":118},[56,1335,122],{"class":111},[56,1337,176],{"class":175},[56,1339,1307],{"class":179},[56,1341,176],{"class":175},[56,1343,713],{"class":111},[399,1345,1347],{"id":1346},"forgetting-to-decrease-the-counter","Forgetting to decrease the counter",[14,1349,1350],{},"If you leave out this line:",[47,1352,1353],{"className":49,"code":575,"language":51,"meta":52,"style":52},[18,1354,1355],{"__ignoreMap":52},[56,1356,1357,1359,1361],{"class":58,"line":59},[56,1358,80],{"class":66},[56,1360,156],{"class":83},[56,1362,159],{"class":87},[14,1364,1365],{},"the value never changes, so the loop may run forever.",[399,1367,1369],{"id":1368},"using-the-wrong-loop-condition","Using the wrong loop condition",[14,1371,1372],{},"This line controls when the countdown stops:",[47,1374,1375],{"className":49,"code":466,"language":51,"meta":52,"style":52},[18,1376,1377],{"__ignoreMap":52},[56,1378,1379,1381,1383,1385,1387],{"class":58,"line":59},[56,1380,99],{"class":62},[56,1382,102],{"class":66},[56,1384,105],{"class":83},[56,1386,108],{"class":87},[56,1388,112],{"class":111},[14,1390,1391],{},"If the condition is wrong, the timer may stop too early or never stop.",[399,1393,1395,1396,1398],{"id":1394},"expecting-sleep-to-print-something","Expecting ",[18,1397,20],{}," to print something",[14,1400,1401,1403],{},[18,1402,427],{}," only pauses the program. It does not display output.",[42,1405,1407],{"id":1406},"debugging-tips","Debugging tips",[14,1409,1410],{},"If your countdown is not working, try these simple checks.",[399,1412,1414],{"id":1413},"print-the-counter-value-each-loop","Print the counter value each loop",[47,1416,1417],{"className":49,"code":521,"language":51,"meta":52,"style":52},[18,1418,1419],{"__ignoreMap":52},[56,1420,1421,1423,1425,1427],{"class":58,"line":59},[56,1422,170],{"class":118},[56,1424,122],{"class":111},[56,1426,126],{"class":125},[56,1428,129],{"class":111},[14,1430,1431],{},"This helps you confirm that the value is changing.",[399,1433,1435],{"id":1434},"print-a-message-inside-the-loop","Print a message inside the loop",[47,1437,1439],{"className":49,"code":1438,"language":51,"meta":52,"style":52},"print(\"loop running\")\n",[18,1440,1441],{"__ignoreMap":52},[56,1442,1443,1445,1447,1449,1452,1454],{"class":58,"line":59},[56,1444,170],{"class":118},[56,1446,122],{"class":111},[56,1448,176],{"class":175},[56,1450,1451],{"class":179},"loop running",[56,1453,176],{"class":175},[56,1455,129],{"class":111},[14,1457,1458],{},"If this prints forever, your loop condition or counter update is probably wrong.",[399,1460,1462],{"id":1461},"check-the-type-of-user-input","Check the type of user input",[47,1464,1466],{"className":49,"code":1465,"language":51,"meta":52,"style":52},"print(type(user_input))\n",[18,1467,1468],{"__ignoreMap":52},[56,1469,1470,1472,1474,1477,1479,1482],{"class":58,"line":59},[56,1471,170],{"class":118},[56,1473,122],{"class":111},[56,1475,1476],{"class":695},"type",[56,1478,122],{"class":111},[56,1480,1481],{"class":125},"user_input",[56,1483,713],{"class":111},[14,1485,1486,1487,1489],{},"This helps when you are working with ",[18,1488,790],{}," and want to see whether you still have text instead of a number.",[399,1491,1493],{"id":1492},"problems-and-likely-causes","Problems and likely causes",[14,1495,1496],{},"Common causes include:",[26,1498,1499,1509,1516,1519],{},[29,1500,1501,1503,1504,1506,1507],{},[18,1502,1193],{}," because ",[18,1505,262],{}," was used without ",[18,1508,404],{},[29,1510,1511,1513,1514],{},[18,1512,878],{}," when converting non-number text with ",[18,1515,801],{},[29,1517,1518],{},"an infinite loop because the counter is never decreased",[29,1520,1521],{},"the wrong countdown length because the starting value is not what the user expected",[42,1523,1525],{"id":1524},"next-steps","Next steps",[14,1527,1528],{},"After you get this version working, good next steps are:",[26,1530,1531,1537,1547,1550],{},[29,1532,1533,1534],{},"learn more about ",[223,1535,1536],{"href":511},"Python while loops",[29,1538,1539,1540,1544,1545],{},"practice with ",[223,1541,1542],{"href":821},[18,1543,790],{}," and ",[18,1546,801],{},[29,1548,1549],{},"try a timer that uses minutes and seconds",[29,1551,1552,1553,1557,1558],{},"build another small script like a ",[223,1554,1556],{"href":1555},"\u002Fexamples\u002Fpython-timer-script-example\u002F","Python timer script example"," or a ",[223,1559,1561],{"href":1560},"\u002Fexamples\u002Fpython-stopwatch-example\u002F","Python stopwatch example",[42,1563,1565],{"id":1564},"faq","FAQ",[399,1567,1569],{"id":1568},"why-does-my-countdown-timer-not-stop","Why does my countdown timer not stop?",[14,1571,1572],{},"Usually the counter is not being decreased inside the loop, or the loop condition is wrong.",[399,1574,1576,1577,1579],{"id":1575},"why-do-i-need-import-time","Why do I need ",[18,1578,404],{},"?",[14,1581,1582,1583,1585,1586,1588],{},"Because ",[18,1584,20],{}," is part of Python’s ",[18,1587,262],{}," module. It is not available automatically by itself.",[399,1590,1592,1593,1595],{"id":1591},"why-does-input-cause-an-error","Why does ",[18,1594,790],{}," cause an error?",[14,1597,1598,1600,1601,1604],{},[18,1599,790],{}," returns text. You usually need ",[18,1602,1603],{},"int(input(...))"," if you want to count down from a number.",[399,1606,1608],{"id":1607},"can-i-make-the-timer-use-minutes-and-seconds","Can I make the timer use minutes and seconds?",[14,1610,1611],{},"Yes. That is a good next step after the simple version works.",[42,1613,1615],{"id":1614},"see-also","See also",[26,1617,1618,1622,1627,1631,1637,1641],{},[29,1619,1620],{},[223,1621,267],{"href":266},[29,1623,1624],{},[223,1625,1626],{"href":225},"time.sleep() function explained",[29,1628,1629],{},[223,1630,512],{"href":511},[29,1632,1633],{},[223,1634,1636],{"href":1635},"\u002Fhow-to\u002Fhow-to-get-user-input-in-python\u002F","How to get user input in Python",[29,1638,1639],{},[223,1640,834],{"href":833},[29,1642,1643],{},[223,1644,1645,1646],{"href":913},"How to fix ",[18,1647,917],{},[14,1649,1650],{},"Copy the simple script first and run it as-is. Then try one small change, like starting from a different number or letting the user choose the countdown value.",[1652,1653,1654],"style",{},"html pre.shiki code .sVHd0, html code.shiki .sVHd0{--shiki-light:#39ADB5;--shiki-light-font-style:italic;--shiki-default:#D73A49;--shiki-default-font-style:inherit;--shiki-dark:#F97583;--shiki-dark-font-style:inherit}html pre.shiki code .su5hD, html code.shiki .su5hD{--shiki-light:#90A4AE;--shiki-default:#24292E;--shiki-dark:#E1E4E8}html pre.shiki code .smGrS, html code.shiki .smGrS{--shiki-light:#39ADB5;--shiki-default:#D73A49;--shiki-dark:#F97583}html pre.shiki code .srdBf, html code.shiki .srdBf{--shiki-light:#F76D47;--shiki-default:#005CC5;--shiki-dark:#79B8FF}html pre.shiki code .sP7_E, html code.shiki .sP7_E{--shiki-light:#39ADB5;--shiki-default:#24292E;--shiki-dark:#E1E4E8}html pre.shiki code .sptTA, html code.shiki .sptTA{--shiki-light:#6182B8;--shiki-default:#005CC5;--shiki-dark:#79B8FF}html pre.shiki code .slqww, html code.shiki .slqww{--shiki-light:#6182B8;--shiki-default:#24292E;--shiki-dark:#E1E4E8}html pre.shiki code .sjJ54, html code.shiki .sjJ54{--shiki-light:#39ADB5;--shiki-default:#032F62;--shiki-dark:#9ECBFF}html pre.shiki code .s_sjI, html code.shiki .s_sjI{--shiki-light:#91B859;--shiki-default:#032F62;--shiki-dark:#9ECBFF}html .light .shiki span {color: var(--shiki-light);background: var(--shiki-light-bg);font-style: var(--shiki-light-font-style);font-weight: var(--shiki-light-font-weight);text-decoration: var(--shiki-light-text-decoration);}html.light .shiki span {color: var(--shiki-light);background: var(--shiki-light-bg);font-style: var(--shiki-light-font-style);font-weight: var(--shiki-light-font-weight);text-decoration: var(--shiki-light-text-decoration);}html .default .shiki span {color: var(--shiki-default);background: var(--shiki-default-bg);font-style: var(--shiki-default-font-style);font-weight: var(--shiki-default-font-weight);text-decoration: var(--shiki-default-text-decoration);}html .shiki span {color: var(--shiki-default);background: var(--shiki-default-bg);font-style: var(--shiki-default-font-style);font-weight: var(--shiki-default-font-weight);text-decoration: var(--shiki-default-text-decoration);}html .dark .shiki span {color: var(--shiki-dark);background: var(--shiki-dark-bg);font-style: var(--shiki-dark-font-style);font-weight: var(--shiki-dark-font-weight);text-decoration: var(--shiki-dark-text-decoration);}html.dark .shiki span {color: var(--shiki-dark);background: var(--shiki-dark-bg);font-style: var(--shiki-dark-font-style);font-weight: var(--shiki-dark-font-weight);text-decoration: var(--shiki-dark-text-decoration);}html pre.shiki code .sZMiF, html code.shiki .sZMiF{--shiki-light:#E2931D;--shiki-default:#005CC5;--shiki-dark:#79B8FF}html pre.shiki code .sbsja, html code.shiki .sbsja{--shiki-light:#9C3EDA;--shiki-default:#D73A49;--shiki-dark:#F97583}html pre.shiki code .sGLFI, html code.shiki .sGLFI{--shiki-light:#6182B8;--shiki-default:#6F42C1;--shiki-dark:#B392F0}html pre.shiki code .sFwrP, html code.shiki .sFwrP{--shiki-light:#90A4AE;--shiki-light-font-style:italic;--shiki-default:#24292E;--shiki-default-font-style:inherit;--shiki-dark:#E1E4E8;--shiki-dark-font-style:inherit}",{"title":52,"searchDepth":70,"depth":70,"links":1656},[1657,1658,1659,1660,1661,1670,1671,1675,1676,1686,1692,1693,1701],{"id":44,"depth":70,"text":45},{"id":190,"depth":70,"text":191},{"id":232,"depth":70,"text":233},{"id":270,"depth":70,"text":271},{"id":393,"depth":70,"text":394,"children":1662},[1663,1664,1665,1666,1667,1668,1669],{"id":401,"depth":77,"text":404},{"id":431,"depth":77,"text":434},{"id":460,"depth":77,"text":463},{"id":515,"depth":77,"text":518},{"id":539,"depth":77,"text":427},{"id":569,"depth":77,"text":572},{"id":620,"depth":77,"text":621},{"id":649,"depth":70,"text":650},{"id":667,"depth":70,"text":668,"children":1672},[1673,1674],{"id":782,"depth":77,"text":783},{"id":837,"depth":77,"text":838},{"id":920,"depth":70,"text":921},{"id":1171,"depth":70,"text":1172,"children":1677},[1678,1680,1682,1683,1684],{"id":1178,"depth":77,"text":1679},"Forgetting to import time",{"id":1273,"depth":77,"text":1681},"Using input() without int()",{"id":1346,"depth":77,"text":1347},{"id":1368,"depth":77,"text":1369},{"id":1394,"depth":77,"text":1685},"Expecting sleep() to print something",{"id":1406,"depth":70,"text":1407,"children":1687},[1688,1689,1690,1691],{"id":1413,"depth":77,"text":1414},{"id":1434,"depth":77,"text":1435},{"id":1461,"depth":77,"text":1462},{"id":1492,"depth":77,"text":1493},{"id":1524,"depth":70,"text":1525},{"id":1564,"depth":70,"text":1565,"children":1694},[1695,1696,1698,1700],{"id":1568,"depth":77,"text":1569},{"id":1575,"depth":77,"text":1697},"Why do I need import time?",{"id":1591,"depth":77,"text":1699},"Why does input() cause an error?",{"id":1607,"depth":77,"text":1608},{"id":1614,"depth":70,"text":1615},"Master python countdown timer example in our comprehensive Python beginner guide.","md",{},"\u002Fexamples\u002Fpython-countdown-timer-example",{"title":5,"description":1702},"examples\u002Fpython-countdown-timer-example","_j5TvY0Bv5A1RH91lrVq5pFHLJSp3F81U8LhpxT3ibY",1777585474946]