[{"data":1,"prerenderedAt":1605},["ShallowReactive",2],{"doc-\u002Fexamples\u002Fpython-stopwatch-example":3},{"id":4,"title":5,"body":6,"description":1598,"extension":1599,"meta":1600,"navigation":57,"path":1601,"seo":1602,"stem":1603,"__hash__":1604},"content\u002Fexamples\u002Fpython-stopwatch-example.md","Python Stopwatch Example",{"type":7,"value":8,"toc":1561},"minimark",[9,13,22,25,30,197,200,204,207,227,231,234,269,272,384,387,395,399,406,417,420,428,435,454,460,467,473,492,497,500,519,526,530,547,550,565,569,596,602,607,615,625,629,632,636,639,665,668,672,675,851,853,859,866,876,1035,1039,1042,1178,1181,1185,1188,1215,1221,1229,1239,1242,1262,1265,1286,1293,1297,1300,1328,1334,1380,1386,1390,1393,1408,1443,1446,1467,1474,1478,1482,1485,1492,1495,1499,1502,1509,1514,1518,1554,1557],[10,11,5],"h1",{"id":12},"python-stopwatch-example",[14,15,16,17,21],"p",{},"Build a simple stopwatch in Python that measures elapsed time. This example uses the ",[18,19,20],"code",{},"time"," module and shows, step by step, how to record a start time, record an end time, and calculate the difference.",[14,23,24],{},"If you are new to timing code in Python, this is a good first example because it uses only a few lines of code and one standard library 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\ninput(\"Press Enter to start the stopwatch...\")\nstart_time = time.time()\n\ninput(\"Press Enter to stop the stopwatch...\")\nend_time = time.time()\n\nelapsed = end_time - start_time\nprint(f\"Elapsed time: {elapsed:.2f} seconds\")\n","python","",[18,38,39,52,59,83,105,110,126,142,147,164],{"__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,66,70,74,78,80],{"class":42,"line":61},3,[40,63,65],{"class":64},"sptTA","input",[40,67,69],{"class":68},"sP7_E","(",[40,71,73],{"class":72},"sjJ54","\"",[40,75,77],{"class":76},"s_sjI","Press Enter to start the stopwatch...",[40,79,73],{"class":72},[40,81,82],{"class":68},")\n",[40,84,86,89,93,96,99,102],{"class":42,"line":85},4,[40,87,88],{"class":50},"start_time ",[40,90,92],{"class":91},"smGrS","=",[40,94,95],{"class":50}," time",[40,97,98],{"class":68},".",[40,100,20],{"class":101},"slqww",[40,103,104],{"class":68},"()\n",[40,106,108],{"class":42,"line":107},5,[40,109,58],{"emptyLinePlaceholder":57},[40,111,113,115,117,119,122,124],{"class":42,"line":112},6,[40,114,65],{"class":64},[40,116,69],{"class":68},[40,118,73],{"class":72},[40,120,121],{"class":76},"Press Enter to stop the stopwatch...",[40,123,73],{"class":72},[40,125,82],{"class":68},[40,127,129,132,134,136,138,140],{"class":42,"line":128},7,[40,130,131],{"class":50},"end_time ",[40,133,92],{"class":91},[40,135,95],{"class":50},[40,137,98],{"class":68},[40,139,20],{"class":101},[40,141,104],{"class":68},[40,143,145],{"class":42,"line":144},8,[40,146,58],{"emptyLinePlaceholder":57},[40,148,150,153,155,158,161],{"class":42,"line":149},9,[40,151,152],{"class":50},"elapsed ",[40,154,92],{"class":91},[40,156,157],{"class":50}," end_time ",[40,159,160],{"class":91},"-",[40,162,163],{"class":50}," start_time\n",[40,165,167,170,172,176,179,183,186,189,192,195],{"class":42,"line":166},10,[40,168,169],{"class":64},"print",[40,171,69],{"class":68},[40,173,175],{"class":174},"sbsja","f",[40,177,178],{"class":76},"\"Elapsed time: ",[40,180,182],{"class":181},"srdBf","{",[40,184,185],{"class":101},"elapsed",[40,187,188],{"class":174},":.2f",[40,190,191],{"class":181},"}",[40,193,194],{"class":76}," seconds\"",[40,196,82],{"class":68},[14,198,199],{},"This is the simplest stopwatch version. It starts when you press Enter and stops when you press Enter again.",[26,201,203],{"id":202},"what-this-example-does","What this example does",[14,205,206],{},"This stopwatch script:",[208,209,210,214,221,224],"ul",{},[211,212,213],"li",{},"Measures how much time passes between two user actions",[211,215,216,217,220],{},"Uses ",[18,218,219],{},"time.time()"," to get the current time in seconds",[211,222,223],{},"Calculates elapsed time by subtracting the start time from the end time",[211,225,226],{},"Prints the result in a readable format",[26,228,230],{"id":229},"how-the-basic-stopwatch-works","How the basic stopwatch works",[14,232,233],{},"The basic idea is simple:",[208,235,236,239,245,248,254,262],{},[211,237,238],{},"Ask the user to press Enter to begin",[211,240,241,242],{},"Store the current time in a variable like ",[18,243,244],{},"start_time",[211,246,247],{},"Ask the user to press Enter again to stop",[211,249,250,251],{},"Store the current time in ",[18,252,253],{},"end_time",[211,255,256,257,259,260],{},"Subtract ",[18,258,244],{}," from ",[18,261,253],{},[211,263,264,265,268],{},"Display the result with formatting such as ",[18,266,267],{},".2f"," for two decimal places",[14,270,271],{},"Here is the full example again:",[31,273,274],{"className":33,"code":34,"language":35,"meta":36,"style":36},[18,275,276,282,286,300,314,318,332,346,350,362],{"__ignoreMap":36},[40,277,278,280],{"class":42,"line":43},[40,279,47],{"class":46},[40,281,51],{"class":50},[40,283,284],{"class":42,"line":54},[40,285,58],{"emptyLinePlaceholder":57},[40,287,288,290,292,294,296,298],{"class":42,"line":61},[40,289,65],{"class":64},[40,291,69],{"class":68},[40,293,73],{"class":72},[40,295,77],{"class":76},[40,297,73],{"class":72},[40,299,82],{"class":68},[40,301,302,304,306,308,310,312],{"class":42,"line":85},[40,303,88],{"class":50},[40,305,92],{"class":91},[40,307,95],{"class":50},[40,309,98],{"class":68},[40,311,20],{"class":101},[40,313,104],{"class":68},[40,315,316],{"class":42,"line":107},[40,317,58],{"emptyLinePlaceholder":57},[40,319,320,322,324,326,328,330],{"class":42,"line":112},[40,321,65],{"class":64},[40,323,69],{"class":68},[40,325,73],{"class":72},[40,327,121],{"class":76},[40,329,73],{"class":72},[40,331,82],{"class":68},[40,333,334,336,338,340,342,344],{"class":42,"line":128},[40,335,131],{"class":50},[40,337,92],{"class":91},[40,339,95],{"class":50},[40,341,98],{"class":68},[40,343,20],{"class":101},[40,345,104],{"class":68},[40,347,348],{"class":42,"line":144},[40,349,58],{"emptyLinePlaceholder":57},[40,351,352,354,356,358,360],{"class":42,"line":149},[40,353,152],{"class":50},[40,355,92],{"class":91},[40,357,157],{"class":50},[40,359,160],{"class":91},[40,361,163],{"class":50},[40,363,364,366,368,370,372,374,376,378,380,382],{"class":42,"line":166},[40,365,169],{"class":64},[40,367,69],{"class":68},[40,369,175],{"class":174},[40,371,178],{"class":76},[40,373,182],{"class":181},[40,375,185],{"class":101},[40,377,188],{"class":174},[40,379,191],{"class":181},[40,381,194],{"class":76},[40,383,82],{"class":68},[14,385,386],{},"Example output:",[31,388,393],{"className":389,"code":391,"language":392,"meta":36},[390],"language-text","Press Enter to start the stopwatch...\nPress Enter to stop the stopwatch...\nElapsed time: 3.47 seconds\n","text",[18,394,391],{"__ignoreMap":36},[26,396,398],{"id":397},"code-walkthrough-points","Code walkthrough points",[400,401,403],"h3",{"id":402},"import-time",[18,404,405],{},"import time",[31,407,409],{"className":33,"code":408,"language":35,"meta":36,"style":36},"import time\n",[18,410,411],{"__ignoreMap":36},[40,412,413,415],{"class":42,"line":43},[40,414,47],{"class":46},[40,416,51],{"class":50},[14,418,419],{},"This line gives your program access to Python's time-related functions.",[14,421,422,423,98],{},"If you want a broader introduction, see the ",[424,425,427],"a",{"href":426},"\u002Fstandard-library\u002Fpython-time-module-overview\u002F","Python time module overview",[400,429,431,434],{"id":430},"input-pauses-the-program",[18,432,433],{},"input()"," pauses the program",[31,436,438],{"className":33,"code":437,"language":35,"meta":36,"style":36},"input(\"Press Enter to start the stopwatch...\")\n",[18,439,440],{"__ignoreMap":36},[40,441,442,444,446,448,450,452],{"class":42,"line":43},[40,443,65],{"class":64},[40,445,69],{"class":68},[40,447,73],{"class":72},[40,449,77],{"class":76},[40,451,73],{"class":72},[40,453,82],{"class":68},[14,455,456,457,459],{},"The ",[18,458,433],{}," function waits until the user presses Enter. That makes it useful for a simple start-and-stop stopwatch.",[14,461,462,463,98],{},"If you need help with this part, see ",[424,464,466],{"href":465},"\u002Fhow-to\u002Fhow-to-get-user-input-in-python\u002F","how to get user input in Python",[400,468,470,472],{"id":469},"timetime-gets-the-current-time",[18,471,219],{}," gets the current time",[31,474,476],{"className":33,"code":475,"language":35,"meta":36,"style":36},"start_time = time.time()\n",[18,477,478],{"__ignoreMap":36},[40,479,480,482,484,486,488,490],{"class":42,"line":43},[40,481,88],{"class":50},[40,483,92],{"class":91},[40,485,95],{"class":50},[40,487,98],{"class":68},[40,489,20],{"class":101},[40,491,104],{"class":68},[14,493,494,496],{},[18,495,219],{}," returns the current Unix timestamp as a floating-point number. That means the value includes fractions of a second.",[14,498,499],{},"Later, the script does the same thing again:",[31,501,503],{"className":33,"code":502,"language":35,"meta":36,"style":36},"end_time = time.time()\n",[18,504,505],{"__ignoreMap":36},[40,506,507,509,511,513,515,517],{"class":42,"line":43},[40,508,131],{"class":50},[40,510,92],{"class":91},[40,512,95],{"class":50},[40,514,98],{"class":68},[40,516,20],{"class":101},[40,518,104],{"class":68},[14,520,521,522,98],{},"You can learn more here: ",[424,523,525],{"href":524},"\u002Fstandard-library\u002Ftime.time-function-explained","time.time() explained",[400,527,529],{"id":528},"subtracting-timestamps-gives-elapsed-time","Subtracting timestamps gives elapsed time",[31,531,533],{"className":33,"code":532,"language":35,"meta":36,"style":36},"elapsed = end_time - start_time\n",[18,534,535],{"__ignoreMap":36},[40,536,537,539,541,543,545],{"class":42,"line":43},[40,538,152],{"class":50},[40,540,92],{"class":91},[40,542,157],{"class":50},[40,544,160],{"class":91},[40,546,163],{"class":50},[14,548,549],{},"This is the key step.",[208,551,552,557,562],{},[211,553,554,556],{},[18,555,244],{}," is the moment the stopwatch began",[211,558,559,561],{},[18,560,253],{}," is the moment it stopped",[211,563,564],{},"The difference is the elapsed time in seconds",[400,566,568],{"id":567},"f-strings-make-output-easier-to-read","f-strings make output easier to read",[31,570,572],{"className":33,"code":571,"language":35,"meta":36,"style":36},"print(f\"Elapsed time: {elapsed:.2f} seconds\")\n",[18,573,574],{"__ignoreMap":36},[40,575,576,578,580,582,584,586,588,590,592,594],{"class":42,"line":43},[40,577,169],{"class":64},[40,579,69],{"class":68},[40,581,175],{"class":174},[40,583,178],{"class":76},[40,585,182],{"class":181},[40,587,185],{"class":101},[40,589,188],{"class":174},[40,591,191],{"class":181},[40,593,194],{"class":76},[40,595,82],{"class":68},[14,597,598,599,601],{},"This uses an f-string to insert the value of ",[18,600,185],{}," into the text.",[14,603,456,604,606],{},[18,605,267],{}," part means:",[208,608,609,612],{},[211,610,611],{},"show the number as a floating-point value",[211,613,614],{},"keep 2 digits after the decimal point",[14,616,617,618,621,622,98],{},"So a value like ",[18,619,620],{},"3.472891"," becomes ",[18,623,624],{},"3.47",[26,626,628],{"id":627},"small-improvements-to-show","Small improvements to show",[14,630,631],{},"Once the basic version works, you can improve it in a few useful ways.",[400,633,635],{"id":634},"round-the-output-to-2-decimal-places","Round the output to 2 decimal places",[14,637,638],{},"The first example already does this with:",[31,640,641],{"className":33,"code":571,"language":35,"meta":36,"style":36},[18,642,643],{"__ignoreMap":36},[40,644,645,647,649,651,653,655,657,659,661,663],{"class":42,"line":43},[40,646,169],{"class":64},[40,648,69],{"class":68},[40,650,175],{"class":174},[40,652,178],{"class":76},[40,654,182],{"class":181},[40,656,185],{"class":101},[40,658,188],{"class":174},[40,660,191],{"class":181},[40,662,194],{"class":76},[40,664,82],{"class":68},[14,666,667],{},"Without formatting, the result may show too many decimal places.",[400,669,671],{"id":670},"show-minutes-and-seconds","Show minutes and seconds",[14,673,674],{},"For longer times, minutes and seconds are easier to read than a decimal number.",[31,676,678],{"className":33,"code":677,"language":35,"meta":36,"style":36},"import time\n\ninput(\"Press Enter to start the stopwatch...\")\nstart_time = time.time()\n\ninput(\"Press Enter to stop the stopwatch...\")\nend_time = time.time()\n\nelapsed = end_time - start_time\n\nminutes = int(elapsed \u002F\u002F 60)\nseconds = elapsed % 60\n\nprint(f\"Elapsed time: {minutes} minute(s) and {seconds:.2f} second(s)\")\n",[18,679,680,686,690,704,718,722,736,750,754,766,770,794,811,816],{"__ignoreMap":36},[40,681,682,684],{"class":42,"line":43},[40,683,47],{"class":46},[40,685,51],{"class":50},[40,687,688],{"class":42,"line":54},[40,689,58],{"emptyLinePlaceholder":57},[40,691,692,694,696,698,700,702],{"class":42,"line":61},[40,693,65],{"class":64},[40,695,69],{"class":68},[40,697,73],{"class":72},[40,699,77],{"class":76},[40,701,73],{"class":72},[40,703,82],{"class":68},[40,705,706,708,710,712,714,716],{"class":42,"line":85},[40,707,88],{"class":50},[40,709,92],{"class":91},[40,711,95],{"class":50},[40,713,98],{"class":68},[40,715,20],{"class":101},[40,717,104],{"class":68},[40,719,720],{"class":42,"line":107},[40,721,58],{"emptyLinePlaceholder":57},[40,723,724,726,728,730,732,734],{"class":42,"line":112},[40,725,65],{"class":64},[40,727,69],{"class":68},[40,729,73],{"class":72},[40,731,121],{"class":76},[40,733,73],{"class":72},[40,735,82],{"class":68},[40,737,738,740,742,744,746,748],{"class":42,"line":128},[40,739,131],{"class":50},[40,741,92],{"class":91},[40,743,95],{"class":50},[40,745,98],{"class":68},[40,747,20],{"class":101},[40,749,104],{"class":68},[40,751,752],{"class":42,"line":144},[40,753,58],{"emptyLinePlaceholder":57},[40,755,756,758,760,762,764],{"class":42,"line":149},[40,757,152],{"class":50},[40,759,92],{"class":91},[40,761,157],{"class":50},[40,763,160],{"class":91},[40,765,163],{"class":50},[40,767,768],{"class":42,"line":166},[40,769,58],{"emptyLinePlaceholder":57},[40,771,773,776,778,782,784,786,789,792],{"class":42,"line":772},11,[40,774,775],{"class":50},"minutes ",[40,777,92],{"class":91},[40,779,781],{"class":780},"sZMiF"," int",[40,783,69],{"class":68},[40,785,152],{"class":101},[40,787,788],{"class":91},"\u002F\u002F",[40,790,791],{"class":181}," 60",[40,793,82],{"class":68},[40,795,797,800,802,805,808],{"class":42,"line":796},12,[40,798,799],{"class":50},"seconds ",[40,801,92],{"class":91},[40,803,804],{"class":50}," elapsed ",[40,806,807],{"class":91},"%",[40,809,810],{"class":181}," 60\n",[40,812,814],{"class":42,"line":813},13,[40,815,58],{"emptyLinePlaceholder":57},[40,817,819,821,823,825,827,829,832,834,837,839,842,844,846,849],{"class":42,"line":818},14,[40,820,169],{"class":64},[40,822,69],{"class":68},[40,824,175],{"class":174},[40,826,178],{"class":76},[40,828,182],{"class":181},[40,830,831],{"class":101},"minutes",[40,833,191],{"class":181},[40,835,836],{"class":76}," minute(s) and ",[40,838,182],{"class":181},[40,840,841],{"class":101},"seconds",[40,843,188],{"class":174},[40,845,191],{"class":181},[40,847,848],{"class":76}," second(s)\"",[40,850,82],{"class":68},[14,852,386],{},[31,854,857],{"className":855,"code":856,"language":392,"meta":36},[390],"Elapsed time: 1 minute(s) and 12.35 second(s)\n",[18,858,856],{"__ignoreMap":36},[400,860,862,863],{"id":861},"handle-keyboardinterrupt","Handle ",[18,864,865],{},"KeyboardInterrupt",[14,867,868,869,872,873,875],{},"If the user stops the program with ",[18,870,871],{},"Ctrl+C",", Python raises a ",[18,874,865],{},". You can handle that so the program exits more cleanly.",[31,877,879],{"className":33,"code":878,"language":35,"meta":36,"style":36},"import time\n\ntry:\n    input(\"Press Enter to start the stopwatch...\")\n    start_time = time.time()\n\n    input(\"Press Enter to stop the stopwatch...\")\n    end_time = time.time()\n\n    elapsed = end_time - start_time\n    print(f\"Elapsed time: {elapsed:.2f} seconds\")\n\nexcept KeyboardInterrupt:\n    print(\"\\nStopwatch cancelled.\")\n",[18,880,881,887,891,899,914,929,933,947,962,966,979,1002,1006,1016],{"__ignoreMap":36},[40,882,883,885],{"class":42,"line":43},[40,884,47],{"class":46},[40,886,51],{"class":50},[40,888,889],{"class":42,"line":54},[40,890,58],{"emptyLinePlaceholder":57},[40,892,893,896],{"class":42,"line":61},[40,894,895],{"class":46},"try",[40,897,898],{"class":68},":\n",[40,900,901,904,906,908,910,912],{"class":42,"line":85},[40,902,903],{"class":64},"    input",[40,905,69],{"class":68},[40,907,73],{"class":72},[40,909,77],{"class":76},[40,911,73],{"class":72},[40,913,82],{"class":68},[40,915,916,919,921,923,925,927],{"class":42,"line":107},[40,917,918],{"class":50},"    start_time ",[40,920,92],{"class":91},[40,922,95],{"class":50},[40,924,98],{"class":68},[40,926,20],{"class":101},[40,928,104],{"class":68},[40,930,931],{"class":42,"line":112},[40,932,58],{"emptyLinePlaceholder":57},[40,934,935,937,939,941,943,945],{"class":42,"line":128},[40,936,903],{"class":64},[40,938,69],{"class":68},[40,940,73],{"class":72},[40,942,121],{"class":76},[40,944,73],{"class":72},[40,946,82],{"class":68},[40,948,949,952,954,956,958,960],{"class":42,"line":144},[40,950,951],{"class":50},"    end_time ",[40,953,92],{"class":91},[40,955,95],{"class":50},[40,957,98],{"class":68},[40,959,20],{"class":101},[40,961,104],{"class":68},[40,963,964],{"class":42,"line":149},[40,965,58],{"emptyLinePlaceholder":57},[40,967,968,971,973,975,977],{"class":42,"line":166},[40,969,970],{"class":50},"    elapsed ",[40,972,92],{"class":91},[40,974,157],{"class":50},[40,976,160],{"class":91},[40,978,163],{"class":50},[40,980,981,984,986,988,990,992,994,996,998,1000],{"class":42,"line":772},[40,982,983],{"class":64},"    print",[40,985,69],{"class":68},[40,987,175],{"class":174},[40,989,178],{"class":76},[40,991,182],{"class":181},[40,993,185],{"class":101},[40,995,188],{"class":174},[40,997,191],{"class":181},[40,999,194],{"class":76},[40,1001,82],{"class":68},[40,1003,1004],{"class":42,"line":796},[40,1005,58],{"emptyLinePlaceholder":57},[40,1007,1008,1011,1014],{"class":42,"line":813},[40,1009,1010],{"class":46},"except",[40,1012,1013],{"class":780}," KeyboardInterrupt",[40,1015,898],{"class":68},[40,1017,1018,1020,1022,1024,1028,1031,1033],{"class":42,"line":818},[40,1019,983],{"class":64},[40,1021,69],{"class":68},[40,1023,73],{"class":72},[40,1025,1027],{"class":1026},"s_hVV","\\n",[40,1029,1030],{"class":76},"Stopwatch cancelled.",[40,1032,73],{"class":72},[40,1034,82],{"class":68},[400,1036,1038],{"id":1037},"wrap-the-logic-in-a-function","Wrap the logic in a function",[14,1040,1041],{},"Putting the code in a function makes it easier to reuse later.",[31,1043,1045],{"className":33,"code":1044,"language":35,"meta":36,"style":36},"import time\n\ndef run_stopwatch():\n    input(\"Press Enter to start the stopwatch...\")\n    start_time = time.time()\n\n    input(\"Press Enter to stop the stopwatch...\")\n    end_time = time.time()\n\n    elapsed = end_time - start_time\n    print(f\"Elapsed time: {elapsed:.2f} seconds\")\n\nrun_stopwatch()\n",[18,1046,1047,1053,1057,1069,1083,1097,1101,1115,1129,1133,1145,1167,1171],{"__ignoreMap":36},[40,1048,1049,1051],{"class":42,"line":43},[40,1050,47],{"class":46},[40,1052,51],{"class":50},[40,1054,1055],{"class":42,"line":54},[40,1056,58],{"emptyLinePlaceholder":57},[40,1058,1059,1062,1066],{"class":42,"line":61},[40,1060,1061],{"class":174},"def",[40,1063,1065],{"class":1064},"sGLFI"," run_stopwatch",[40,1067,1068],{"class":68},"():\n",[40,1070,1071,1073,1075,1077,1079,1081],{"class":42,"line":85},[40,1072,903],{"class":64},[40,1074,69],{"class":68},[40,1076,73],{"class":72},[40,1078,77],{"class":76},[40,1080,73],{"class":72},[40,1082,82],{"class":68},[40,1084,1085,1087,1089,1091,1093,1095],{"class":42,"line":107},[40,1086,918],{"class":50},[40,1088,92],{"class":91},[40,1090,95],{"class":50},[40,1092,98],{"class":68},[40,1094,20],{"class":101},[40,1096,104],{"class":68},[40,1098,1099],{"class":42,"line":112},[40,1100,58],{"emptyLinePlaceholder":57},[40,1102,1103,1105,1107,1109,1111,1113],{"class":42,"line":128},[40,1104,903],{"class":64},[40,1106,69],{"class":68},[40,1108,73],{"class":72},[40,1110,121],{"class":76},[40,1112,73],{"class":72},[40,1114,82],{"class":68},[40,1116,1117,1119,1121,1123,1125,1127],{"class":42,"line":144},[40,1118,951],{"class":50},[40,1120,92],{"class":91},[40,1122,95],{"class":50},[40,1124,98],{"class":68},[40,1126,20],{"class":101},[40,1128,104],{"class":68},[40,1130,1131],{"class":42,"line":149},[40,1132,58],{"emptyLinePlaceholder":57},[40,1134,1135,1137,1139,1141,1143],{"class":42,"line":166},[40,1136,970],{"class":50},[40,1138,92],{"class":91},[40,1140,157],{"class":50},[40,1142,160],{"class":91},[40,1144,163],{"class":50},[40,1146,1147,1149,1151,1153,1155,1157,1159,1161,1163,1165],{"class":42,"line":772},[40,1148,983],{"class":64},[40,1150,69],{"class":68},[40,1152,175],{"class":174},[40,1154,178],{"class":76},[40,1156,182],{"class":181},[40,1158,185],{"class":101},[40,1160,188],{"class":174},[40,1162,191],{"class":181},[40,1164,194],{"class":76},[40,1166,82],{"class":68},[40,1168,1169],{"class":42,"line":796},[40,1170,58],{"emptyLinePlaceholder":57},[40,1172,1173,1176],{"class":42,"line":813},[40,1174,1175],{"class":101},"run_stopwatch",[40,1177,104],{"class":68},[14,1179,1180],{},"This is a good step if you want to turn the stopwatch into a larger program.",[26,1182,1184],{"id":1183},"beginner-mistakes-to-watch-for","Beginner mistakes to watch for",[14,1186,1187],{},"Common beginner mistakes with this example include:",[208,1189,1190,1196,1206,1209],{},[211,1191,1192,1193,1195],{},"Forgetting to import the ",[18,1194,20],{}," module",[211,1197,1198,1199,1202,1203,1205],{},"Using ",[18,1200,1201],{},"time.sleep()"," instead of ",[18,1204,219],{}," for measuring elapsed time",[211,1207,1208],{},"Trying to subtract strings instead of numeric time values",[211,1210,1211,1212,1214],{},"Overwriting variable names like ",[18,1213,20],{}," with your own variable",[400,1216,1218,1220],{"id":1217},"timesleep-is-not-the-same-thing",[18,1219,1201],{}," is not the same thing",[14,1222,1223,1224,1226,1227,98],{},"A very common confusion is mixing up ",[18,1225,1201],{}," and ",[18,1228,219],{},[208,1230,1231,1235],{},[211,1232,1233,472],{},[18,1234,219],{},[211,1236,1237,434],{},[18,1238,1201],{},[14,1240,1241],{},"So this is for measurement:",[31,1243,1245],{"className":33,"code":1244,"language":35,"meta":36,"style":36},"now = time.time()\n",[18,1246,1247],{"__ignoreMap":36},[40,1248,1249,1252,1254,1256,1258,1260],{"class":42,"line":43},[40,1250,1251],{"class":50},"now ",[40,1253,92],{"class":91},[40,1255,95],{"class":50},[40,1257,98],{"class":68},[40,1259,20],{"class":101},[40,1261,104],{"class":68},[14,1263,1264],{},"And this is for pausing:",[31,1266,1268],{"className":33,"code":1267,"language":35,"meta":36,"style":36},"time.sleep(2)\n",[18,1269,1270],{"__ignoreMap":36},[40,1271,1272,1274,1276,1279,1281,1284],{"class":42,"line":43},[40,1273,20],{"class":50},[40,1275,98],{"class":68},[40,1277,1278],{"class":101},"sleep",[40,1280,69],{"class":68},[40,1282,1283],{"class":181},"2",[40,1285,82],{"class":68},[14,1287,1288,1289,98],{},"If you want to understand the difference better, see ",[424,1290,1292],{"href":1291},"\u002Fstandard-library\u002Ftime.sleep-function-explained","time.sleep() explained",[26,1294,1296],{"id":1295},"common-mistakes-and-debugging","Common mistakes and debugging",[14,1298,1299],{},"Here are some common causes of problems:",[208,1301,1302,1313,1322,1325],{},[211,1303,1304,1307,1308,1310,1311],{},[18,1305,1306],{},"NameError"," if ",[18,1309,20],{}," is used without ",[18,1312,405],{},[211,1314,1315,1318,1319,1321],{},[18,1316,1317],{},"AttributeError"," if a variable named ",[18,1320,20],{}," replaces the module name",[211,1323,1324],{},"Confusion between pausing code and measuring time",[211,1326,1327],{},"Incorrect indentation when placing code inside a function or loop",[14,1329,1330,1331,1333],{},"For example, this causes a problem because ",[18,1332,20],{}," is no longer the module:",[31,1335,1337],{"className":33,"code":1336,"language":35,"meta":36,"style":36},"import time\n\ntime = \"hello\"\nprint(time.time())\n",[18,1338,1339,1345,1349,1365],{"__ignoreMap":36},[40,1340,1341,1343],{"class":42,"line":43},[40,1342,47],{"class":46},[40,1344,51],{"class":50},[40,1346,1347],{"class":42,"line":54},[40,1348,58],{"emptyLinePlaceholder":57},[40,1350,1351,1354,1356,1359,1362],{"class":42,"line":61},[40,1352,1353],{"class":50},"time ",[40,1355,92],{"class":91},[40,1357,1358],{"class":72}," \"",[40,1360,1361],{"class":76},"hello",[40,1363,1364],{"class":72},"\"\n",[40,1366,1367,1369,1371,1373,1375,1377],{"class":42,"line":85},[40,1368,169],{"class":64},[40,1370,69],{"class":68},[40,1372,20],{"class":101},[40,1374,98],{"class":68},[40,1376,20],{"class":101},[40,1378,1379],{"class":68},"())\n",[14,1381,1382,1383,1385],{},"Python will fail because ",[18,1384,20],{}," now refers to a string, not the module.",[400,1387,1389],{"id":1388},"helpful-debugging-commands","Helpful debugging commands",[14,1391,1392],{},"You can use these commands or print statements while testing:",[31,1394,1398],{"className":1395,"code":1396,"language":1397,"meta":36,"style":36},"language-bash shiki shiki-themes material-theme-lighter github-light github-dark","python stopwatch.py\n","bash",[18,1399,1400],{"__ignoreMap":36},[40,1401,1402,1405],{"class":42,"line":43},[40,1403,35],{"class":1404},"sbgvK",[40,1406,1407],{"class":76}," stopwatch.py\n",[31,1409,1411],{"className":33,"code":1410,"language":35,"meta":36,"style":36},"print(start_time)\nprint(end_time)\nprint(elapsed)\n",[18,1412,1413,1423,1433],{"__ignoreMap":36},[40,1414,1415,1417,1419,1421],{"class":42,"line":43},[40,1416,169],{"class":64},[40,1418,69],{"class":68},[40,1420,244],{"class":101},[40,1422,82],{"class":68},[40,1424,1425,1427,1429,1431],{"class":42,"line":54},[40,1426,169],{"class":64},[40,1428,69],{"class":68},[40,1430,253],{"class":101},[40,1432,82],{"class":68},[40,1434,1435,1437,1439,1441],{"class":42,"line":61},[40,1436,169],{"class":64},[40,1438,69],{"class":68},[40,1440,185],{"class":101},[40,1442,82],{"class":68},[14,1444,1445],{},"You can also trace the script step by step:",[31,1447,1449],{"className":1395,"code":1448,"language":1397,"meta":36,"style":36},"python -m trace --trace stopwatch.py\n",[18,1450,1451],{"__ignoreMap":36},[40,1452,1453,1455,1459,1462,1465],{"class":42,"line":43},[40,1454,35],{"class":1404},[40,1456,1458],{"class":1457},"stzsN"," -m",[40,1460,1461],{"class":76}," trace",[40,1463,1464],{"class":1457}," --trace",[40,1466,1407],{"class":76},[14,1468,1469,1470,98],{},"If you are new to debugging, see the ",[424,1471,1473],{"href":1472},"\u002Fhow-to\u002Fhow-to-debug-python-code-beginner-guide\u002F","beginner guide to debugging Python code",[26,1475,1477],{"id":1476},"faq","FAQ",[400,1479,1481],{"id":1480},"is-this-a-real-stopwatch-that-updates-on-the-screen","Is this a real stopwatch that updates on the screen?",[14,1483,1484],{},"No. This basic version only measures the time between start and stop. A live updating stopwatch would need a loop and repeated output.",[400,1486,1488,1489,1491],{"id":1487},"why-use-timetime-here","Why use ",[18,1490,219],{}," here?",[14,1493,1494],{},"It returns the current time in seconds, so you can subtract two values to find elapsed time.",[400,1496,1498],{"id":1497},"can-i-show-minutes-and-seconds-instead-of-decimal-seconds","Can I show minutes and seconds instead of decimal seconds?",[14,1500,1501],{},"Yes. You can divide the elapsed time into minutes and remaining seconds before printing.",[400,1503,1505,1506,1508],{"id":1504},"what-is-the-difference-between-this-and-timesleep","What is the difference between this and ",[18,1507,1201],{},"?",[14,1510,1511,1513],{},[18,1512,1201],{}," pauses the program. It does not calculate elapsed time for you.",[26,1515,1517],{"id":1516},"see-also","See also",[208,1519,1520,1524,1528,1532,1537,1542,1548],{},[211,1521,1522],{},[424,1523,427],{"href":426},[211,1525,1526],{},[424,1527,525],{"href":524},[211,1529,1530],{},[424,1531,1292],{"href":1291},[211,1533,1534],{},[424,1535,1536],{"href":465},"How to get user input in Python",[211,1538,1539],{},[424,1540,1541],{"href":1472},"How to debug Python code",[211,1543,1544],{},[424,1545,1547],{"href":1546},"\u002Fexamples\u002Fpython-countdown-timer-example\u002F","Python countdown timer example",[211,1549,1550],{},[424,1551,1553],{"href":1552},"\u002Fexamples\u002Fpython-timer-script-example\u002F","Python timer script example",[14,1555,1556],{},"Try improving this basic stopwatch next by adding a live display, formatting the result as minutes and seconds, or turning the code into a reusable function.",[1558,1559,1560],"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 .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 .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 pre.shiki code .smGrS, html code.shiki .smGrS{--shiki-light:#39ADB5;--shiki-default:#D73A49;--shiki-dark:#F97583}html pre.shiki code .slqww, html code.shiki .slqww{--shiki-light:#6182B8;--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 .srdBf, html code.shiki .srdBf{--shiki-light:#F76D47;--shiki-default:#005CC5;--shiki-dark:#79B8FF}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 .s_hVV, html code.shiki .s_hVV{--shiki-light:#90A4AE;--shiki-default:#005CC5;--shiki-dark:#79B8FF}html pre.shiki code .sGLFI, html code.shiki .sGLFI{--shiki-light:#6182B8;--shiki-default:#6F42C1;--shiki-dark:#B392F0}html pre.shiki code .sbgvK, html code.shiki .sbgvK{--shiki-light:#E2931D;--shiki-default:#6F42C1;--shiki-dark:#B392F0}html pre.shiki code .stzsN, html code.shiki .stzsN{--shiki-light:#91B859;--shiki-default:#005CC5;--shiki-dark:#79B8FF}",{"title":36,"searchDepth":54,"depth":54,"links":1562},[1563,1564,1565,1566,1575,1582,1586,1589,1597],{"id":28,"depth":54,"text":29},{"id":202,"depth":54,"text":203},{"id":229,"depth":54,"text":230},{"id":397,"depth":54,"text":398,"children":1567},[1568,1569,1571,1573,1574],{"id":402,"depth":61,"text":405},{"id":430,"depth":61,"text":1570},"input() pauses the program",{"id":469,"depth":61,"text":1572},"time.time() gets the current time",{"id":528,"depth":61,"text":529},{"id":567,"depth":61,"text":568},{"id":627,"depth":54,"text":628,"children":1576},[1577,1578,1579,1581],{"id":634,"depth":61,"text":635},{"id":670,"depth":61,"text":671},{"id":861,"depth":61,"text":1580},"Handle KeyboardInterrupt",{"id":1037,"depth":61,"text":1038},{"id":1183,"depth":54,"text":1184,"children":1583},[1584],{"id":1217,"depth":61,"text":1585},"time.sleep() is not the same thing",{"id":1295,"depth":54,"text":1296,"children":1587},[1588],{"id":1388,"depth":61,"text":1389},{"id":1476,"depth":54,"text":1477,"children":1590},[1591,1592,1594,1595],{"id":1480,"depth":61,"text":1481},{"id":1487,"depth":61,"text":1593},"Why use time.time() here?",{"id":1497,"depth":61,"text":1498},{"id":1504,"depth":61,"text":1596},"What is the difference between this and time.sleep()?",{"id":1516,"depth":54,"text":1517},"Master python stopwatch example in our comprehensive Python beginner guide.","md",{},"\u002Fexamples\u002Fpython-stopwatch-example",{"title":5,"description":1598},"examples\u002Fpython-stopwatch-example","LgyOn4eNHBXKarEn5XgUtwE_282is-ZBzarCGodMsYw",1777585508134]