[{"data":1,"prerenderedAt":1373},["ShallowReactive",2],{"doc-\u002Ferrors\u002Frecursionerror-in-python-causes-and-fixes":3},{"id":4,"title":5,"body":6,"description":1366,"extension":1367,"meta":1368,"navigation":136,"path":1369,"seo":1370,"stem":1371,"__hash__":1372},"content\u002Ferrors\u002Frecursionerror-in-python-causes-and-fixes.md","RecursionError in Python: Causes and Fixes",{"type":7,"value":8,"toc":1336},"minimark",[9,13,21,27,32,151,157,187,193,197,212,215,230,239,243,246,260,267,271,276,328,333,336,347,350,355,359,366,369,373,433,436,440,515,519,544,547,551,554,558,632,642,646,717,721,738,741,752,756,767,770,774,845,849,901,905,929,936,940,943,957,960,1052,1056,1106,1109,1144,1151,1155,1158,1165,1210,1216,1219,1230,1233,1237,1242,1259,1266,1270,1274,1277,1281,1284,1288,1291,1295,1298,1302,1332],[10,11,5],"h1",{"id":12},"recursionerror-in-python-causes-and-fixes",[14,15,16,20],"p",{},[17,18,19],"code",{},"RecursionError"," happens when a function keeps calling itself too many times.",[14,22,23,24,26],{},"In Python, recursion means a function calls itself to solve a smaller version of the same problem. This can be useful, but the function must have a clear way to stop. If it never stops, or if it goes too deep, Python raises ",[17,25,19],{},".",[28,29,31],"h2",{"id":30},"quick-fix","Quick fix",[33,34,39],"pre",{"className":35,"code":36,"language":37,"meta":38,"style":38},"language-python shiki shiki-themes material-theme-lighter github-light github-dark","def countdown(n):\n    if n \u003C= 0:   # base case\n        return\n    print(n)\n    countdown(n - 1)\n\ncountdown(5)\n","python","",[17,40,41,65,91,97,112,131,138],{"__ignoreMap":38},[42,43,46,50,54,58,62],"span",{"class":44,"line":45},"line",1,[42,47,49],{"class":48},"sbsja","def",[42,51,53],{"class":52},"sGLFI"," countdown",[42,55,57],{"class":56},"sP7_E","(",[42,59,61],{"class":60},"sFwrP","n",[42,63,64],{"class":56},"):\n",[42,66,68,72,76,80,84,87],{"class":44,"line":67},2,[42,69,71],{"class":70},"sVHd0","    if",[42,73,75],{"class":74},"su5hD"," n ",[42,77,79],{"class":78},"smGrS","\u003C=",[42,81,83],{"class":82},"srdBf"," 0",[42,85,86],{"class":56},":",[42,88,90],{"class":89},"sutJx","   # base case\n",[42,92,94],{"class":44,"line":93},3,[42,95,96],{"class":70},"        return\n",[42,98,100,104,106,109],{"class":44,"line":99},4,[42,101,103],{"class":102},"sptTA","    print",[42,105,57],{"class":56},[42,107,61],{"class":108},"slqww",[42,110,111],{"class":56},")\n",[42,113,115,118,120,123,126,129],{"class":44,"line":114},5,[42,116,117],{"class":108},"    countdown",[42,119,57],{"class":56},[42,121,122],{"class":108},"n ",[42,124,125],{"class":78},"-",[42,127,128],{"class":82}," 1",[42,130,111],{"class":56},[42,132,134],{"class":44,"line":133},6,[42,135,137],{"emptyLinePlaceholder":136},true,"\n",[42,139,141,144,146,149],{"class":44,"line":140},7,[42,142,143],{"class":108},"countdown",[42,145,57],{"class":56},[42,147,148],{"class":82},"5",[42,150,111],{"class":56},[14,152,153],{},[154,155,156],"strong",{},"Output:",[33,158,160],{"className":35,"code":159,"language":37,"meta":38,"style":38},"5\n4\n3\n2\n1\n",[17,161,162,167,172,177,182],{"__ignoreMap":38},[42,163,164],{"class":44,"line":45},[42,165,166],{"class":82},"5\n",[42,168,169],{"class":44,"line":67},[42,170,171],{"class":82},"4\n",[42,173,174],{"class":44,"line":93},[42,175,176],{"class":82},"3\n",[42,178,179],{"class":44,"line":99},[42,180,181],{"class":82},"2\n",[42,183,184],{"class":44,"line":114},[42,185,186],{"class":82},"1\n",[14,188,189,190,192],{},"Most ",[17,191,19],{}," problems happen because the function has no base case or the base case is never reached.",[28,194,196],{"id":195},"what-this-error-means","What this error means",[198,199,200,204,209],"ul",{},[201,202,203],"li",{},"Recursion happens when a function calls itself.",[201,205,206,207,26],{},"Python stops very deep recursion and raises ",[17,208,19],{},[201,210,211],{},"This usually means the function keeps calling itself too many times.",[14,213,214],{},"A common error message looks like this:",[33,216,218],{"className":35,"code":217,"language":37,"meta":38,"style":38},"RecursionError: maximum recursion depth exceeded\n",[17,219,220],{"__ignoreMap":38},[42,221,222,225,227],{"class":44,"line":45},[42,223,19],{"class":224},"sZMiF",[42,226,86],{"class":56},[42,228,229],{"class":74}," maximum recursion depth exceeded\n",[14,231,232,233,238],{},"If you are new to functions, see ",[234,235,237],"a",{"href":236},"\u002Flearn\u002Fpython-functions-explained\u002F","Python functions explained"," first.",[28,240,242],{"id":241},"why-it-happens","Why it happens",[14,244,245],{},"This error usually happens for one of these reasons:",[198,247,248,251,254,257],{},[201,249,250],{},"There is no base case to stop the recursion.",[201,252,253],{},"The base case exists but the code never reaches it.",[201,255,256],{},"The recursive step does not move toward the stopping condition.",[201,258,259],{},"The input is too large for a recursive solution in Python.",[14,261,262,263,26],{},"If you want a simple definition of recursion, read ",[234,264,266],{"href":265},"\u002Fglossary\u002Fwhat-is-recursion-in-python\u002F","what recursion is in Python",[28,268,270],{"id":269},"example-that-causes-the-error","Example that causes the error",[14,272,273,274,86],{},"Here is a minimal example that causes ",[17,275,19],{},[33,277,279],{"className":35,"code":278,"language":37,"meta":38,"style":38},"def greet():\n    print(\"Hello\")\n    greet()\n\ngreet()\n",[17,280,281,291,309,317,321],{"__ignoreMap":38},[42,282,283,285,288],{"class":44,"line":45},[42,284,49],{"class":48},[42,286,287],{"class":52}," greet",[42,289,290],{"class":56},"():\n",[42,292,293,295,297,301,305,307],{"class":44,"line":67},[42,294,103],{"class":102},[42,296,57],{"class":56},[42,298,300],{"class":299},"sjJ54","\"",[42,302,304],{"class":303},"s_sjI","Hello",[42,306,300],{"class":299},[42,308,111],{"class":56},[42,310,311,314],{"class":44,"line":93},[42,312,313],{"class":108},"    greet",[42,315,316],{"class":56},"()\n",[42,318,319],{"class":44,"line":99},[42,320,137],{"emptyLinePlaceholder":136},[42,322,323,326],{"class":44,"line":114},[42,324,325],{"class":108},"greet",[42,327,316],{"class":56},[329,330,332],"h3",{"id":331},"why-this-fails","Why this fails",[14,334,335],{},"The line below keeps repeating forever:",[33,337,339],{"className":35,"code":338,"language":37,"meta":38,"style":38},"greet()\n",[17,340,341],{"__ignoreMap":38},[42,342,343,345],{"class":44,"line":45},[42,344,325],{"class":108},[42,346,316],{"class":56},[14,348,349],{},"The function calls itself, then calls itself again, then again, with no stopping condition.",[14,351,352,353,26],{},"After enough calls, Python stops the program and raises ",[17,354,19],{},[28,356,358],{"id":357},"fix-1-add-a-base-case","Fix 1: Add a base case",[14,360,361,362,365],{},"A ",[154,363,364],{},"base case"," is the condition that stops the function.",[14,367,368],{},"Put the base case before the recursive call, and make sure it returns or exits.",[329,370,372],{"id":371},"broken-version","Broken version",[33,374,376],{"className":35,"code":375,"language":37,"meta":38,"style":38},"def count_up(n):\n    print(n)\n    count_up(n + 1)\n\ncount_up(1)\n",[17,377,378,391,401,417,421],{"__ignoreMap":38},[42,379,380,382,385,387,389],{"class":44,"line":45},[42,381,49],{"class":48},[42,383,384],{"class":52}," count_up",[42,386,57],{"class":56},[42,388,61],{"class":60},[42,390,64],{"class":56},[42,392,393,395,397,399],{"class":44,"line":67},[42,394,103],{"class":102},[42,396,57],{"class":56},[42,398,61],{"class":108},[42,400,111],{"class":56},[42,402,403,406,408,410,413,415],{"class":44,"line":93},[42,404,405],{"class":108},"    count_up",[42,407,57],{"class":56},[42,409,122],{"class":108},[42,411,412],{"class":78},"+",[42,414,128],{"class":82},[42,416,111],{"class":56},[42,418,419],{"class":44,"line":99},[42,420,137],{"emptyLinePlaceholder":136},[42,422,423,426,428,431],{"class":44,"line":114},[42,424,425],{"class":108},"count_up",[42,427,57],{"class":56},[42,429,430],{"class":82},"1",[42,432,111],{"class":56},[14,434,435],{},"This never stops.",[329,437,439],{"id":438},"fixed-version","Fixed version",[33,441,443],{"className":35,"code":442,"language":37,"meta":38,"style":38},"def count_up(n):\n    if n > 5:   # base case\n        return\n    print(n)\n    count_up(n + 1)\n\ncount_up(1)\n",[17,444,445,457,473,477,487,501,505],{"__ignoreMap":38},[42,446,447,449,451,453,455],{"class":44,"line":45},[42,448,49],{"class":48},[42,450,384],{"class":52},[42,452,57],{"class":56},[42,454,61],{"class":60},[42,456,64],{"class":56},[42,458,459,461,463,466,469,471],{"class":44,"line":67},[42,460,71],{"class":70},[42,462,75],{"class":74},[42,464,465],{"class":78},">",[42,467,468],{"class":82}," 5",[42,470,86],{"class":56},[42,472,90],{"class":89},[42,474,475],{"class":44,"line":93},[42,476,96],{"class":70},[42,478,479,481,483,485],{"class":44,"line":99},[42,480,103],{"class":102},[42,482,57],{"class":56},[42,484,61],{"class":108},[42,486,111],{"class":56},[42,488,489,491,493,495,497,499],{"class":44,"line":114},[42,490,405],{"class":108},[42,492,57],{"class":56},[42,494,122],{"class":108},[42,496,412],{"class":78},[42,498,128],{"class":82},[42,500,111],{"class":56},[42,502,503],{"class":44,"line":133},[42,504,137],{"emptyLinePlaceholder":136},[42,506,507,509,511,513],{"class":44,"line":140},[42,508,425],{"class":108},[42,510,57],{"class":56},[42,512,430],{"class":82},[42,514,111],{"class":56},[14,516,517],{},[154,518,156],{},[33,520,522],{"className":35,"code":521,"language":37,"meta":38,"style":38},"1\n2\n3\n4\n5\n",[17,523,524,528,532,536,540],{"__ignoreMap":38},[42,525,526],{"class":44,"line":45},[42,527,186],{"class":82},[42,529,530],{"class":44,"line":67},[42,531,181],{"class":82},[42,533,534],{"class":44,"line":93},[42,535,176],{"class":82},[42,537,538],{"class":44,"line":99},[42,539,171],{"class":82},[42,541,542],{"class":44,"line":114},[42,543,166],{"class":82},[14,545,546],{},"The important part is that the function checks the stopping condition before making the next recursive call.",[28,548,550],{"id":549},"fix-2-move-toward-the-base-case","Fix 2: Move toward the base case",[14,552,553],{},"Even if you have a base case, the function still fails if the values do not move toward it.",[329,555,557],{"id":556},"wrong-direction","Wrong direction",[33,559,561],{"className":35,"code":560,"language":37,"meta":38,"style":38},"def countdown(n):\n    if n == 0:\n        return\n    print(n)\n    countdown(n + 1)\n\ncountdown(3)\n",[17,562,563,575,589,593,603,617,621],{"__ignoreMap":38},[42,564,565,567,569,571,573],{"class":44,"line":45},[42,566,49],{"class":48},[42,568,53],{"class":52},[42,570,57],{"class":56},[42,572,61],{"class":60},[42,574,64],{"class":56},[42,576,577,579,581,584,586],{"class":44,"line":67},[42,578,71],{"class":70},[42,580,75],{"class":74},[42,582,583],{"class":78},"==",[42,585,83],{"class":82},[42,587,588],{"class":56},":\n",[42,590,591],{"class":44,"line":93},[42,592,96],{"class":70},[42,594,595,597,599,601],{"class":44,"line":99},[42,596,103],{"class":102},[42,598,57],{"class":56},[42,600,61],{"class":108},[42,602,111],{"class":56},[42,604,605,607,609,611,613,615],{"class":44,"line":114},[42,606,117],{"class":108},[42,608,57],{"class":56},[42,610,122],{"class":108},[42,612,412],{"class":78},[42,614,128],{"class":82},[42,616,111],{"class":56},[42,618,619],{"class":44,"line":133},[42,620,137],{"emptyLinePlaceholder":136},[42,622,623,625,627,630],{"class":44,"line":140},[42,624,143],{"class":108},[42,626,57],{"class":56},[42,628,629],{"class":82},"3",[42,631,111],{"class":56},[14,633,634,635,637,638,641],{},"This has a base case, but ",[17,636,61],{}," moves away from ",[17,639,640],{},"0"," instead of toward it.",[329,643,645],{"id":644},"correct-version","Correct version",[33,647,649],{"className":35,"code":648,"language":37,"meta":38,"style":38},"def countdown(n):\n    if n == 0:\n        return\n    print(n)\n    countdown(n - 1)\n\ncountdown(3)\n",[17,650,651,663,675,679,689,703,707],{"__ignoreMap":38},[42,652,653,655,657,659,661],{"class":44,"line":45},[42,654,49],{"class":48},[42,656,53],{"class":52},[42,658,57],{"class":56},[42,660,61],{"class":60},[42,662,64],{"class":56},[42,664,665,667,669,671,673],{"class":44,"line":67},[42,666,71],{"class":70},[42,668,75],{"class":74},[42,670,583],{"class":78},[42,672,83],{"class":82},[42,674,588],{"class":56},[42,676,677],{"class":44,"line":93},[42,678,96],{"class":70},[42,680,681,683,685,687],{"class":44,"line":99},[42,682,103],{"class":102},[42,684,57],{"class":56},[42,686,61],{"class":108},[42,688,111],{"class":56},[42,690,691,693,695,697,699,701],{"class":44,"line":114},[42,692,117],{"class":108},[42,694,57],{"class":56},[42,696,122],{"class":108},[42,698,125],{"class":78},[42,700,128],{"class":82},[42,702,111],{"class":56},[42,704,705],{"class":44,"line":133},[42,706,137],{"emptyLinePlaceholder":136},[42,708,709,711,713,715],{"class":44,"line":140},[42,710,143],{"class":108},[42,712,57],{"class":56},[42,714,629],{"class":82},[42,716,111],{"class":56},[14,718,719],{},[154,720,156],{},[33,722,724],{"className":35,"code":723,"language":37,"meta":38,"style":38},"3\n2\n1\n",[17,725,726,730,734],{"__ignoreMap":38},[42,727,728],{"class":44,"line":45},[42,729,176],{"class":82},[42,731,732],{"class":44,"line":67},[42,733,181],{"class":82},[42,735,736],{"class":44,"line":93},[42,737,186],{"class":82},[14,739,740],{},"When writing recursive code:",[198,742,743,746,749],{},[201,744,745],{},"Change the argument on each call.",[201,747,748],{},"Make the problem smaller each time.",[201,750,751],{},"Check that the value really gets closer to the stopping condition.",[28,753,755],{"id":754},"fix-3-use-a-loop-instead","Fix 3: Use a loop instead",[14,757,758,759,762,763,766],{},"Some tasks are easier with a ",[17,760,761],{},"for"," loop or ",[17,764,765],{},"while"," loop.",[14,768,769],{},"Loops do not build up recursive calls in the same way, so they avoid deep recursion problems.",[329,771,773],{"id":772},"recursive-version","Recursive version",[33,775,777],{"className":35,"code":776,"language":37,"meta":38,"style":38},"def countdown(n):\n    if n \u003C= 0:\n        return\n    print(n)\n    countdown(n - 1)\n\ncountdown(5)\n",[17,778,779,791,803,807,817,831,835],{"__ignoreMap":38},[42,780,781,783,785,787,789],{"class":44,"line":45},[42,782,49],{"class":48},[42,784,53],{"class":52},[42,786,57],{"class":56},[42,788,61],{"class":60},[42,790,64],{"class":56},[42,792,793,795,797,799,801],{"class":44,"line":67},[42,794,71],{"class":70},[42,796,75],{"class":74},[42,798,79],{"class":78},[42,800,83],{"class":82},[42,802,588],{"class":56},[42,804,805],{"class":44,"line":93},[42,806,96],{"class":70},[42,808,809,811,813,815],{"class":44,"line":99},[42,810,103],{"class":102},[42,812,57],{"class":56},[42,814,61],{"class":108},[42,816,111],{"class":56},[42,818,819,821,823,825,827,829],{"class":44,"line":114},[42,820,117],{"class":108},[42,822,57],{"class":56},[42,824,122],{"class":108},[42,826,125],{"class":78},[42,828,128],{"class":82},[42,830,111],{"class":56},[42,832,833],{"class":44,"line":133},[42,834,137],{"emptyLinePlaceholder":136},[42,836,837,839,841,843],{"class":44,"line":140},[42,838,143],{"class":108},[42,840,57],{"class":56},[42,842,148],{"class":82},[42,844,111],{"class":56},[329,846,848],{"id":847},"loop-version","Loop version",[33,850,852],{"className":35,"code":851,"language":37,"meta":38,"style":38},"n = 5\n\nwhile n > 0:\n    print(n)\n    n -= 1\n",[17,853,854,864,868,880,890],{"__ignoreMap":38},[42,855,856,858,861],{"class":44,"line":45},[42,857,122],{"class":74},[42,859,860],{"class":78},"=",[42,862,863],{"class":82}," 5\n",[42,865,866],{"class":44,"line":67},[42,867,137],{"emptyLinePlaceholder":136},[42,869,870,872,874,876,878],{"class":44,"line":93},[42,871,765],{"class":70},[42,873,75],{"class":74},[42,875,465],{"class":78},[42,877,83],{"class":82},[42,879,588],{"class":56},[42,881,882,884,886,888],{"class":44,"line":99},[42,883,103],{"class":102},[42,885,57],{"class":56},[42,887,61],{"class":108},[42,889,111],{"class":56},[42,891,892,895,898],{"class":44,"line":114},[42,893,894],{"class":74},"    n ",[42,896,897],{"class":78},"-=",[42,899,900],{"class":82}," 1\n",[14,902,903],{},[154,904,156],{},[33,906,907],{"className":35,"code":159,"language":37,"meta":38,"style":38},[17,908,909,913,917,921,925],{"__ignoreMap":38},[42,910,911],{"class":44,"line":45},[42,912,166],{"class":82},[42,914,915],{"class":44,"line":67},[42,916,171],{"class":82},[42,918,919],{"class":44,"line":93},[42,920,176],{"class":82},[42,922,923],{"class":44,"line":99},[42,924,181],{"class":82},[42,926,927],{"class":44,"line":114},[42,928,186],{"class":82},[14,930,931,932,26],{},"If recursion is not necessary, iteration is often simpler. You can learn more in ",[234,933,935],{"href":934},"\u002Flearn\u002Fpython-while-loops-explained\u002F","Python while loops explained",[28,937,939],{"id":938},"debugging-checklist","Debugging checklist",[14,941,942],{},"If your recursive function is failing, try this checklist:",[198,944,945,948,951,954],{},[201,946,947],{},"Print the function argument before the recursive call.",[201,949,950],{},"Check whether the value changes on each call.",[201,952,953],{},"Confirm the base case can actually become true.",[201,955,956],{},"Test with a very small input first.",[14,958,959],{},"Useful debugging examples:",[33,961,963],{"className":35,"code":962,"language":37,"meta":38,"style":38},"def countdown(n):\n    print(\"calling with:\", n)\n\n    if n \u003C= 0:\n        return\n\n    countdown(n - 1)\n\ncountdown(3)\n",[17,964,965,977,998,1002,1014,1018,1022,1036,1041],{"__ignoreMap":38},[42,966,967,969,971,973,975],{"class":44,"line":45},[42,968,49],{"class":48},[42,970,53],{"class":52},[42,972,57],{"class":56},[42,974,61],{"class":60},[42,976,64],{"class":56},[42,978,979,981,983,985,988,990,993,996],{"class":44,"line":67},[42,980,103],{"class":102},[42,982,57],{"class":56},[42,984,300],{"class":299},[42,986,987],{"class":303},"calling with:",[42,989,300],{"class":299},[42,991,992],{"class":56},",",[42,994,995],{"class":108}," n",[42,997,111],{"class":56},[42,999,1000],{"class":44,"line":93},[42,1001,137],{"emptyLinePlaceholder":136},[42,1003,1004,1006,1008,1010,1012],{"class":44,"line":99},[42,1005,71],{"class":70},[42,1007,75],{"class":74},[42,1009,79],{"class":78},[42,1011,83],{"class":82},[42,1013,588],{"class":56},[42,1015,1016],{"class":44,"line":114},[42,1017,96],{"class":70},[42,1019,1020],{"class":44,"line":133},[42,1021,137],{"emptyLinePlaceholder":136},[42,1023,1024,1026,1028,1030,1032,1034],{"class":44,"line":140},[42,1025,117],{"class":108},[42,1027,57],{"class":56},[42,1029,122],{"class":108},[42,1031,125],{"class":78},[42,1033,128],{"class":82},[42,1035,111],{"class":56},[42,1037,1039],{"class":44,"line":1038},8,[42,1040,137],{"emptyLinePlaceholder":136},[42,1042,1044,1046,1048,1050],{"class":44,"line":1043},9,[42,1045,143],{"class":108},[42,1047,57],{"class":56},[42,1049,629],{"class":82},[42,1051,111],{"class":56},[14,1053,1054],{},[154,1055,156],{},[33,1057,1059],{"className":35,"code":1058,"language":37,"meta":38,"style":38},"calling with: 3\ncalling with: 2\ncalling with: 1\ncalling with: 0\n",[17,1060,1061,1074,1085,1095],{"__ignoreMap":38},[42,1062,1063,1066,1069,1071],{"class":44,"line":45},[42,1064,1065],{"class":74},"calling ",[42,1067,1068],{"class":70},"with",[42,1070,86],{"class":56},[42,1072,1073],{"class":82}," 3\n",[42,1075,1076,1078,1080,1082],{"class":44,"line":67},[42,1077,1065],{"class":74},[42,1079,1068],{"class":70},[42,1081,86],{"class":56},[42,1083,1084],{"class":82}," 2\n",[42,1086,1087,1089,1091,1093],{"class":44,"line":93},[42,1088,1065],{"class":74},[42,1090,1068],{"class":70},[42,1092,86],{"class":56},[42,1094,900],{"class":82},[42,1096,1097,1099,1101,1103],{"class":44,"line":99},[42,1098,1065],{"class":74},[42,1100,1068],{"class":70},[42,1102,86],{"class":56},[42,1104,1105],{"class":82}," 0\n",[14,1107,1108],{},"You can also inspect Python's current recursion limit:",[33,1110,1112],{"className":35,"code":1111,"language":37,"meta":38,"style":38},"import sys\n\nprint(sys.getrecursionlimit())\n",[17,1113,1114,1122,1126],{"__ignoreMap":38},[42,1115,1116,1119],{"class":44,"line":45},[42,1117,1118],{"class":70},"import",[42,1120,1121],{"class":74}," sys\n",[42,1123,1124],{"class":44,"line":67},[42,1125,137],{"emptyLinePlaceholder":136},[42,1127,1128,1131,1133,1136,1138,1141],{"class":44,"line":93},[42,1129,1130],{"class":102},"print",[42,1132,57],{"class":56},[42,1134,1135],{"class":108},"sys",[42,1137,26],{"class":56},[42,1139,1140],{"class":108},"getrecursionlimit",[42,1142,1143],{"class":56},"())\n",[14,1145,1146,1147,26],{},"If you are still stuck, see this ",[234,1148,1150],{"href":1149},"\u002Fhow-to\u002Fhow-to-debug-python-code-beginner-guide\u002F","beginner guide to debugging Python code",[28,1152,1154],{"id":1153},"about-increasing-the-recursion-limit","About increasing the recursion limit",[14,1156,1157],{},"Python has a recursion limit to protect your program from crashing due to infinite or very deep recursion.",[14,1159,1160,1161,1164],{},"You can change the limit with ",[17,1162,1163],{},"sys.setrecursionlimit()",", like this:",[33,1166,1168],{"className":35,"code":1167,"language":37,"meta":38,"style":38},"import sys\n\nsys.setrecursionlimit(2000)\nprint(sys.getrecursionlimit())\n",[17,1169,1170,1176,1180,1196],{"__ignoreMap":38},[42,1171,1172,1174],{"class":44,"line":45},[42,1173,1118],{"class":70},[42,1175,1121],{"class":74},[42,1177,1178],{"class":44,"line":67},[42,1179,137],{"emptyLinePlaceholder":136},[42,1181,1182,1184,1186,1189,1191,1194],{"class":44,"line":93},[42,1183,1135],{"class":74},[42,1185,26],{"class":56},[42,1187,1188],{"class":108},"setrecursionlimit",[42,1190,57],{"class":56},[42,1192,1193],{"class":82},"2000",[42,1195,111],{"class":56},[42,1197,1198,1200,1202,1204,1206,1208],{"class":44,"line":99},[42,1199,1130],{"class":102},[42,1201,57],{"class":56},[42,1203,1135],{"class":108},[42,1205,26],{"class":56},[42,1207,1140],{"class":108},[42,1209,1143],{"class":56},[14,1211,1212,1213,26],{},"But this is ",[154,1214,1215],{},"usually not the first fix",[14,1217,1218],{},"For beginners, the better solution is normally to fix the recursive logic:",[198,1220,1221,1224,1227],{},[201,1222,1223],{},"Add a base case",[201,1225,1226],{},"Make sure the function moves toward that base case",[201,1228,1229],{},"Use a loop if recursion is not needed",[14,1231,1232],{},"Raising the limit can hide a bug instead of solving it.",[28,1234,1236],{"id":1235},"common-mistakes","Common mistakes",[14,1238,1239,1240,86],{},"These are the most common causes of ",[17,1241,19],{},[198,1243,1244,1247,1250,1253,1256],{},[201,1245,1246],{},"Missing base case",[201,1248,1249],{},"Wrong comparison in the base case",[201,1251,1252],{},"Recursive call uses the same value every time",[201,1254,1255],{},"Value changes in the wrong direction",[201,1257,1258],{},"Using recursion for very large input",[14,1260,1261,1262,26],{},"For a broader overview of Python exceptions, read ",[234,1263,1265],{"href":1264},"\u002Flearn\u002Fpython-errors-and-exceptions-explained\u002F","Python errors and exceptions explained",[28,1267,1269],{"id":1268},"faq","FAQ",[329,1271,1273],{"id":1272},"what-is-a-base-case-in-recursion","What is a base case in recursion?",[14,1275,1276],{},"It is the condition that stops the function from calling itself again.",[329,1278,1280],{"id":1279},"can-i-fix-recursionerror-by-increasing-the-recursion-limit","Can I fix RecursionError by increasing the recursion limit?",[14,1282,1283],{},"Sometimes, but usually the better fix is to correct the recursive logic or use a loop.",[329,1285,1287],{"id":1286},"why-does-my-recursive-function-never-stop","Why does my recursive function never stop?",[14,1289,1290],{},"Usually because the stopping condition is missing, incorrect, or never reached.",[329,1292,1294],{"id":1293},"is-recursion-bad-in-python","Is recursion bad in Python?",[14,1296,1297],{},"No, but Python is not ideal for very deep recursion. Simple recursion is fine when the stopping condition is clear.",[28,1299,1301],{"id":1300},"see-also","See also",[198,1303,1304,1308,1313,1317,1322,1328],{},[201,1305,1306],{},[234,1307,237],{"href":236},[201,1309,1310],{},[234,1311,1312],{"href":265},"What is recursion in Python?",[201,1314,1315],{},[234,1316,1265],{"href":1264},[201,1318,1319],{},[234,1320,1321],{"href":1149},"How to debug Python code",[201,1323,1324],{},[234,1325,1327],{"href":1326},"\u002Ferrors\u002Fruntimeerror-in-python-causes-and-fixes\u002F","RuntimeError in Python: causes and fixes",[201,1329,1330],{},[234,1331,935],{"href":934},[1333,1334,1335],"style",{},"html pre.shiki code .sbsja, html code.shiki .sbsja{--shiki-light:#9C3EDA;--shiki-default:#D73A49;--shiki-dark:#F97583}html pre.shiki code .sGLFI, html code.shiki .sGLFI{--shiki-light:#6182B8;--shiki-default:#6F42C1;--shiki-dark:#B392F0}html pre.shiki code .sP7_E, html code.shiki .sP7_E{--shiki-light:#39ADB5;--shiki-default:#24292E;--shiki-dark:#E1E4E8}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}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 .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 .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 .sZMiF, html code.shiki .sZMiF{--shiki-light:#E2931D;--shiki-default:#005CC5;--shiki-dark:#79B8FF}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}",{"title":38,"searchDepth":67,"depth":67,"links":1337},[1338,1339,1340,1341,1344,1348,1352,1356,1357,1358,1359,1365],{"id":30,"depth":67,"text":31},{"id":195,"depth":67,"text":196},{"id":241,"depth":67,"text":242},{"id":269,"depth":67,"text":270,"children":1342},[1343],{"id":331,"depth":93,"text":332},{"id":357,"depth":67,"text":358,"children":1345},[1346,1347],{"id":371,"depth":93,"text":372},{"id":438,"depth":93,"text":439},{"id":549,"depth":67,"text":550,"children":1349},[1350,1351],{"id":556,"depth":93,"text":557},{"id":644,"depth":93,"text":645},{"id":754,"depth":67,"text":755,"children":1353},[1354,1355],{"id":772,"depth":93,"text":773},{"id":847,"depth":93,"text":848},{"id":938,"depth":67,"text":939},{"id":1153,"depth":67,"text":1154},{"id":1235,"depth":67,"text":1236},{"id":1268,"depth":67,"text":1269,"children":1360},[1361,1362,1363,1364],{"id":1272,"depth":93,"text":1273},{"id":1279,"depth":93,"text":1280},{"id":1286,"depth":93,"text":1287},{"id":1293,"depth":93,"text":1294},{"id":1300,"depth":67,"text":1301},"Master recursionerror in python causes and fixes in our comprehensive Python beginner guide.","md",{},"\u002Ferrors\u002Frecursionerror-in-python-causes-and-fixes",{"title":5,"description":1366},"errors\u002Frecursionerror-in-python-causes-and-fixes","GS-NdbD-wzgujRj9ahzo_j3RrCxFrYzzEhPqArgK-V8",1777585480828]