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