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