[{"data":1,"prerenderedAt":1499},["ShallowReactive",2],{"doc-\u002Ferrors\u002Frecursionerror-maximum-recursion-depth-exceeded-fix":3},{"id":4,"title":5,"body":6,"description":1492,"extension":1493,"meta":1494,"navigation":136,"path":1495,"seo":1496,"stem":1497,"__hash__":1498},"content\u002Ferrors\u002Frecursionerror-maximum-recursion-depth-exceeded-fix.md","RecursionError: maximum recursion depth exceeded (Fix)",{"type":7,"value":8,"toc":1467},"minimark",[9,13,21,24,27,32,151,157,187,190,218,225,236,240,243,246,257,266,270,273,290,293,296,305,308,312,315,373,383,386,406,409,413,418,421,493,497,500,503,579,582,599,603,606,676,683,687,694,791,795,846,853,857,860,944,949,952,956,959,962,1032,1035,1088,1091,1095,1098,1101,1163,1166,1169,1177,1184,1187,1198,1201,1232,1235,1239,1242,1259,1262,1296,1299,1311,1318,1322,1325,1336,1339,1364,1371,1375,1378,1401,1405,1409,1412,1416,1419,1423,1426,1430,1433,1437,1463],[10,11,5],"h1",{"id":12},"recursionerror-maximum-recursion-depth-exceeded-fix",[14,15,16,20],"p",{},[17,18,19],"code",{},"RecursionError: maximum recursion depth exceeded"," means Python stopped your program because a function called itself too many times.",[14,22,23],{},"This usually happens with recursive functions. A recursive function is a function that calls itself. Python allows recursion, but it also has a safety limit so your program does not keep running forever and using too much memory.",[14,25,26],{},"If you want the quick fix first: check that your function has a base case, and make sure every recursive call moves toward that stopping point.",[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 count_down(n):\n    if n \u003C= 0:  # base case\n        return\n    print(n)\n    count_down(n - 1)\n\ncount_down(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"," count_down",[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},"    count_down",[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},"count_down",[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],{},"Why this works:",[191,192,193,203,206],"ul",{},[194,195,196,199,200],"li",{},[17,197,198],{},"if n \u003C= 0:"," is the ",[154,201,202],{},"base case",[194,204,205],{},"It stops the recursion",[194,207,208,211,212,214,215],{},[17,209,210],{},"count_down(n - 1)"," moves ",[17,213,61],{}," closer to ",[17,216,217],{},"0",[14,219,220,221,224],{},"Most ",[17,222,223],{},"RecursionError"," problems happen because:",[191,226,227,230,233],{},[194,228,229],{},"the function has no base case",[194,231,232],{},"the base case is never reached",[194,234,235],{},"each call creates more calls than expected",[28,237,239],{"id":238},"what-this-error-means","What this error means",[14,241,242],{},"Python raises this error when a function keeps calling itself until it hits Python's recursion limit.",[14,244,245],{},"In simple terms:",[191,247,248,251,254],{},[194,249,250],{},"Your function did not stop in time",[194,252,253],{},"Python stopped it for safety",[194,255,256],{},"The problem is usually in the recursion logic, not in Python itself",[14,258,259,260,265],{},"If you are new to the idea of recursive functions, see ",[261,262,264],"a",{"href":263},"\u002Fglossary\u002Fwhat-is-recursion-in-python","what recursion means in Python"," first.",[28,267,269],{"id":268},"when-this-error-happens","When this error happens",[14,271,272],{},"This error commonly appears when:",[191,274,275,278,281,284,287],{},[194,276,277],{},"A recursive function has no base case",[194,279,280],{},"The base case exists but is never reached",[194,282,283],{},"The value changes in the wrong direction",[194,285,286],{},"Two functions call each other forever",[194,288,289],{},"The recursion is valid, but the depth is still too large",[14,291,292],{},"Here is the main idea:",[14,294,295],{},"A recursive function needs two things:",[297,298,299,302],"ol",{},[194,300,301],{},"A stopping condition",[194,303,304],{},"A change that moves each call toward that stopping condition",[14,306,307],{},"Without both, recursion does not end.",[28,309,311],{"id":310},"example-that-causes-the-error","Example that causes the error",[14,313,314],{},"Here is a small example that fails:",[33,316,318],{"className":35,"code":317,"language":37,"meta":38,"style":38},"def count_forever(n):\n    print(n)\n    count_forever(n - 1)\n\ncount_forever(5)\n",[17,319,320,333,343,358,362],{"__ignoreMap":38},[42,321,322,324,327,329,331],{"class":44,"line":45},[42,323,49],{"class":48},[42,325,326],{"class":52}," count_forever",[42,328,57],{"class":56},[42,330,61],{"class":60},[42,332,64],{"class":56},[42,334,335,337,339,341],{"class":44,"line":67},[42,336,103],{"class":102},[42,338,57],{"class":56},[42,340,61],{"class":108},[42,342,111],{"class":56},[42,344,345,348,350,352,354,356],{"class":44,"line":93},[42,346,347],{"class":108},"    count_forever",[42,349,57],{"class":56},[42,351,122],{"class":108},[42,353,125],{"class":78},[42,355,128],{"class":82},[42,357,111],{"class":56},[42,359,360],{"class":44,"line":99},[42,361,137],{"emptyLinePlaceholder":136},[42,363,364,367,369,371],{"class":44,"line":114},[42,365,366],{"class":108},"count_forever",[42,368,57],{"class":56},[42,370,148],{"class":82},[42,372,111],{"class":56},[14,374,375,376,378,379,382],{},"This causes a ",[17,377,223],{}," because the function has ",[154,380,381],{},"no base case",".",[14,384,385],{},"It keeps calling itself again and again:",[191,387,388,393,398,403],{},[194,389,390],{},[17,391,392],{},"count_forever(5)",[194,394,395],{},[17,396,397],{},"count_forever(4)",[194,399,400],{},[17,401,402],{},"count_forever(3)",[194,404,405],{},"and so on...",[14,407,408],{},"It never stops, so Python eventually raises the error.",[28,410,412],{"id":411},"how-to-fix-it","How to fix it",[414,415,417],"h3",{"id":416},"_1-add-a-base-case","1. Add a base case",[14,419,420],{},"A base case is the condition that stops the function.",[33,422,424],{"className":35,"code":423,"language":37,"meta":38,"style":38},"def count_down(n):\n    if n \u003C= 0:\n        return\n    print(n)\n    count_down(n - 1)\n\ncount_down(5)\n",[17,425,426,438,451,455,465,479,483],{"__ignoreMap":38},[42,427,428,430,432,434,436],{"class":44,"line":45},[42,429,49],{"class":48},[42,431,53],{"class":52},[42,433,57],{"class":56},[42,435,61],{"class":60},[42,437,64],{"class":56},[42,439,440,442,444,446,448],{"class":44,"line":67},[42,441,71],{"class":70},[42,443,75],{"class":74},[42,445,79],{"class":78},[42,447,83],{"class":82},[42,449,450],{"class":56},":\n",[42,452,453],{"class":44,"line":93},[42,454,96],{"class":70},[42,456,457,459,461,463],{"class":44,"line":99},[42,458,103],{"class":102},[42,460,57],{"class":56},[42,462,61],{"class":108},[42,464,111],{"class":56},[42,466,467,469,471,473,475,477],{"class":44,"line":114},[42,468,117],{"class":108},[42,470,57],{"class":56},[42,472,122],{"class":108},[42,474,125],{"class":78},[42,476,128],{"class":82},[42,478,111],{"class":56},[42,480,481],{"class":44,"line":133},[42,482,137],{"emptyLinePlaceholder":136},[42,484,485,487,489,491],{"class":44,"line":140},[42,486,143],{"class":108},[42,488,57],{"class":56},[42,490,148],{"class":82},[42,492,111],{"class":56},[414,494,496],{"id":495},"_2-make-sure-each-call-gets-closer-to-stopping","2. Make sure each call gets closer to stopping",[14,498,499],{},"A base case is not enough if your value never reaches it.",[14,501,502],{},"This version looks close, but it is wrong:",[33,504,506],{"className":35,"code":505,"language":37,"meta":38,"style":38},"def count_down(n):\n    if n \u003C= 0:\n        return\n    print(n)\n    count_down(n + 1)  # wrong direction\n\ncount_down(5)\n",[17,507,508,520,532,536,546,565,569],{"__ignoreMap":38},[42,509,510,512,514,516,518],{"class":44,"line":45},[42,511,49],{"class":48},[42,513,53],{"class":52},[42,515,57],{"class":56},[42,517,61],{"class":60},[42,519,64],{"class":56},[42,521,522,524,526,528,530],{"class":44,"line":67},[42,523,71],{"class":70},[42,525,75],{"class":74},[42,527,79],{"class":78},[42,529,83],{"class":82},[42,531,450],{"class":56},[42,533,534],{"class":44,"line":93},[42,535,96],{"class":70},[42,537,538,540,542,544],{"class":44,"line":99},[42,539,103],{"class":102},[42,541,57],{"class":56},[42,543,61],{"class":108},[42,545,111],{"class":56},[42,547,548,550,552,554,557,559,562],{"class":44,"line":114},[42,549,117],{"class":108},[42,551,57],{"class":56},[42,553,122],{"class":108},[42,555,556],{"class":78},"+",[42,558,128],{"class":82},[42,560,561],{"class":56},")",[42,563,564],{"class":89},"  # wrong direction\n",[42,566,567],{"class":44,"line":133},[42,568,137],{"emptyLinePlaceholder":136},[42,570,571,573,575,577],{"class":44,"line":140},[42,572,143],{"class":108},[42,574,57],{"class":56},[42,576,148],{"class":82},[42,578,111],{"class":56},[14,580,581],{},"Why it fails:",[191,583,584,590,596],{},[194,585,586,587],{},"The base case is ",[17,588,589],{},"n \u003C= 0",[194,591,592,593,595],{},"But ",[17,594,61],{}," keeps increasing",[194,597,598],{},"So the stopping condition never becomes true",[414,600,602],{"id":601},"_3-check-for-recursive-calls-using-the-same-value","3. Check for recursive calls using the same value",[14,604,605],{},"If the function keeps using the same value, it will not make progress.",[33,607,609],{"className":35,"code":608,"language":37,"meta":38,"style":38},"def count_down(n):\n    if n \u003C= 0:\n        return\n    print(n)\n    count_down(n)  # same value again\n\ncount_down(5)\n",[17,610,611,623,635,639,649,662,666],{"__ignoreMap":38},[42,612,613,615,617,619,621],{"class":44,"line":45},[42,614,49],{"class":48},[42,616,53],{"class":52},[42,618,57],{"class":56},[42,620,61],{"class":60},[42,622,64],{"class":56},[42,624,625,627,629,631,633],{"class":44,"line":67},[42,626,71],{"class":70},[42,628,75],{"class":74},[42,630,79],{"class":78},[42,632,83],{"class":82},[42,634,450],{"class":56},[42,636,637],{"class":44,"line":93},[42,638,96],{"class":70},[42,640,641,643,645,647],{"class":44,"line":99},[42,642,103],{"class":102},[42,644,57],{"class":56},[42,646,61],{"class":108},[42,648,111],{"class":56},[42,650,651,653,655,657,659],{"class":44,"line":114},[42,652,117],{"class":108},[42,654,57],{"class":56},[42,656,61],{"class":108},[42,658,561],{"class":56},[42,660,661],{"class":89},"  # same value again\n",[42,663,664],{"class":44,"line":133},[42,665,137],{"emptyLinePlaceholder":136},[42,667,668,670,672,674],{"class":44,"line":140},[42,669,143],{"class":108},[42,671,57],{"class":56},[42,673,148],{"class":82},[42,675,111],{"class":56},[14,677,678,679,682],{},"This keeps calling ",[17,680,681],{},"count_down(5)"," forever.",[414,684,686],{"id":685},"_4-print-the-value-to-trace-what-is-happening","4. Print the value to trace what is happening",[14,688,689,690,693],{},"A simple ",[17,691,692],{},"print()"," can help you see whether the function is moving toward the base case.",[33,695,697],{"className":35,"code":696,"language":37,"meta":38,"style":38},"def count_down(n):\n    print(\"calling with:\", n)\n    \n    if n \u003C= 0:\n        return\n    \n    count_down(n - 1)\n\ncount_down(3)\n",[17,698,699,711,735,740,752,756,760,774,779],{"__ignoreMap":38},[42,700,701,703,705,707,709],{"class":44,"line":45},[42,702,49],{"class":48},[42,704,53],{"class":52},[42,706,57],{"class":56},[42,708,61],{"class":60},[42,710,64],{"class":56},[42,712,713,715,717,721,725,727,730,733],{"class":44,"line":67},[42,714,103],{"class":102},[42,716,57],{"class":56},[42,718,720],{"class":719},"sjJ54","\"",[42,722,724],{"class":723},"s_sjI","calling with:",[42,726,720],{"class":719},[42,728,729],{"class":56},",",[42,731,732],{"class":108}," n",[42,734,111],{"class":56},[42,736,737],{"class":44,"line":93},[42,738,739],{"class":74},"    \n",[42,741,742,744,746,748,750],{"class":44,"line":99},[42,743,71],{"class":70},[42,745,75],{"class":74},[42,747,79],{"class":78},[42,749,83],{"class":82},[42,751,450],{"class":56},[42,753,754],{"class":44,"line":114},[42,755,96],{"class":70},[42,757,758],{"class":44,"line":133},[42,759,739],{"class":74},[42,761,762,764,766,768,770,772],{"class":44,"line":140},[42,763,117],{"class":108},[42,765,57],{"class":56},[42,767,122],{"class":108},[42,769,125],{"class":78},[42,771,128],{"class":82},[42,773,111],{"class":56},[42,775,777],{"class":44,"line":776},8,[42,778,137],{"emptyLinePlaceholder":136},[42,780,782,784,786,789],{"class":44,"line":781},9,[42,783,143],{"class":108},[42,785,57],{"class":56},[42,787,788],{"class":82},"3",[42,790,111],{"class":56},[14,792,793],{},[154,794,156],{},[33,796,798],{"className":35,"code":797,"language":37,"meta":38,"style":38},"calling with: 3\ncalling with: 2\ncalling with: 1\ncalling with: 0\n",[17,799,800,813,824,835],{"__ignoreMap":38},[42,801,802,805,808,810],{"class":44,"line":45},[42,803,804],{"class":74},"calling ",[42,806,807],{"class":70},"with",[42,809,86],{"class":56},[42,811,812],{"class":82}," 3\n",[42,814,815,817,819,821],{"class":44,"line":67},[42,816,804],{"class":74},[42,818,807],{"class":70},[42,820,86],{"class":56},[42,822,823],{"class":82}," 2\n",[42,825,826,828,830,832],{"class":44,"line":93},[42,827,804],{"class":74},[42,829,807],{"class":70},[42,831,86],{"class":56},[42,833,834],{"class":82}," 1\n",[42,836,837,839,841,843],{"class":44,"line":99},[42,838,804],{"class":74},[42,840,807],{"class":70},[42,842,86],{"class":56},[42,844,845],{"class":82}," 0\n",[14,847,848,849,382],{},"This is a fast way to debug recursive code. If you want more general debugging help, see this ",[261,850,852],{"href":851},"\u002Fhow-to\u002Fhow-to-debug-python-code-beginner-guide\u002F","beginner guide to debugging Python code",[414,854,856],{"id":855},"_5-check-for-accidental-circular-calls","5. Check for accidental circular calls",[14,858,859],{},"Sometimes one function does not call itself directly. Instead, two functions call each other forever.",[33,861,863],{"className":35,"code":862,"language":37,"meta":38,"style":38},"def func_a():\n    print(\"A\")\n    func_b()\n\ndef func_b():\n    print(\"B\")\n    func_a()\n\nfunc_a()\n",[17,864,865,875,890,898,902,911,926,933,937],{"__ignoreMap":38},[42,866,867,869,872],{"class":44,"line":45},[42,868,49],{"class":48},[42,870,871],{"class":52}," func_a",[42,873,874],{"class":56},"():\n",[42,876,877,879,881,883,886,888],{"class":44,"line":67},[42,878,103],{"class":102},[42,880,57],{"class":56},[42,882,720],{"class":719},[42,884,885],{"class":723},"A",[42,887,720],{"class":719},[42,889,111],{"class":56},[42,891,892,895],{"class":44,"line":93},[42,893,894],{"class":108},"    func_b",[42,896,897],{"class":56},"()\n",[42,899,900],{"class":44,"line":99},[42,901,137],{"emptyLinePlaceholder":136},[42,903,904,906,909],{"class":44,"line":114},[42,905,49],{"class":48},[42,907,908],{"class":52}," func_b",[42,910,874],{"class":56},[42,912,913,915,917,919,922,924],{"class":44,"line":133},[42,914,103],{"class":102},[42,916,57],{"class":56},[42,918,720],{"class":719},[42,920,921],{"class":723},"B",[42,923,720],{"class":719},[42,925,111],{"class":56},[42,927,928,931],{"class":44,"line":140},[42,929,930],{"class":108},"    func_a",[42,932,897],{"class":56},[42,934,935],{"class":44,"line":776},[42,936,137],{"emptyLinePlaceholder":136},[42,938,939,942],{"class":44,"line":781},[42,940,941],{"class":108},"func_a",[42,943,897],{"class":56},[14,945,946,947,382],{},"This also leads to a ",[17,948,223],{},[14,950,951],{},"To fix it, make sure one of the functions has a condition that stops the chain of calls.",[414,953,955],{"id":954},"_6-use-a-loop-when-recursion-is-not-necessary","6. Use a loop when recursion is not necessary",[14,957,958],{},"For many beginner tasks, a loop is simpler and safer.",[14,960,961],{},"Recursive version:",[33,963,964],{"className":35,"code":423,"language":37,"meta":38,"style":38},[17,965,966,978,990,994,1004,1018,1022],{"__ignoreMap":38},[42,967,968,970,972,974,976],{"class":44,"line":45},[42,969,49],{"class":48},[42,971,53],{"class":52},[42,973,57],{"class":56},[42,975,61],{"class":60},[42,977,64],{"class":56},[42,979,980,982,984,986,988],{"class":44,"line":67},[42,981,71],{"class":70},[42,983,75],{"class":74},[42,985,79],{"class":78},[42,987,83],{"class":82},[42,989,450],{"class":56},[42,991,992],{"class":44,"line":93},[42,993,96],{"class":70},[42,995,996,998,1000,1002],{"class":44,"line":99},[42,997,103],{"class":102},[42,999,57],{"class":56},[42,1001,61],{"class":108},[42,1003,111],{"class":56},[42,1005,1006,1008,1010,1012,1014,1016],{"class":44,"line":114},[42,1007,117],{"class":108},[42,1009,57],{"class":56},[42,1011,122],{"class":108},[42,1013,125],{"class":78},[42,1015,128],{"class":82},[42,1017,111],{"class":56},[42,1019,1020],{"class":44,"line":133},[42,1021,137],{"emptyLinePlaceholder":136},[42,1023,1024,1026,1028,1030],{"class":44,"line":140},[42,1025,143],{"class":108},[42,1027,57],{"class":56},[42,1029,148],{"class":82},[42,1031,111],{"class":56},[14,1033,1034],{},"Loop version:",[33,1036,1038],{"className":35,"code":1037,"language":37,"meta":38,"style":38},"n = 5\n\nwhile n > 0:\n    print(n)\n    n -= 1\n",[17,1039,1040,1050,1054,1068,1078],{"__ignoreMap":38},[42,1041,1042,1044,1047],{"class":44,"line":45},[42,1043,122],{"class":74},[42,1045,1046],{"class":78},"=",[42,1048,1049],{"class":82}," 5\n",[42,1051,1052],{"class":44,"line":67},[42,1053,137],{"emptyLinePlaceholder":136},[42,1055,1056,1059,1061,1064,1066],{"class":44,"line":93},[42,1057,1058],{"class":70},"while",[42,1060,75],{"class":74},[42,1062,1063],{"class":78},">",[42,1065,83],{"class":82},[42,1067,450],{"class":56},[42,1069,1070,1072,1074,1076],{"class":44,"line":99},[42,1071,103],{"class":102},[42,1073,57],{"class":56},[42,1075,61],{"class":108},[42,1077,111],{"class":56},[42,1079,1080,1083,1086],{"class":44,"line":114},[42,1081,1082],{"class":74},"    n ",[42,1084,1085],{"class":78},"-=",[42,1087,834],{"class":82},[14,1089,1090],{},"For simple repetition, a loop is often easier to read and avoids recursion depth problems.",[28,1092,1094],{"id":1093},"deep-recursion-that-is-technically-correct","Deep recursion that is technically correct",[14,1096,1097],{},"Sometimes your recursion logic is correct, but the input is so large that Python still reaches the recursion limit.",[14,1099,1100],{},"Example:",[33,1102,1104],{"className":35,"code":1103,"language":37,"meta":38,"style":38},"def count_down(n):\n    if n \u003C= 0:\n        return\n    count_down(n - 1)\n\ncount_down(2000)\n",[17,1105,1106,1118,1130,1134,1148,1152],{"__ignoreMap":38},[42,1107,1108,1110,1112,1114,1116],{"class":44,"line":45},[42,1109,49],{"class":48},[42,1111,53],{"class":52},[42,1113,57],{"class":56},[42,1115,61],{"class":60},[42,1117,64],{"class":56},[42,1119,1120,1122,1124,1126,1128],{"class":44,"line":67},[42,1121,71],{"class":70},[42,1123,75],{"class":74},[42,1125,79],{"class":78},[42,1127,83],{"class":82},[42,1129,450],{"class":56},[42,1131,1132],{"class":44,"line":93},[42,1133,96],{"class":70},[42,1135,1136,1138,1140,1142,1144,1146],{"class":44,"line":99},[42,1137,117],{"class":108},[42,1139,57],{"class":56},[42,1141,122],{"class":108},[42,1143,125],{"class":78},[42,1145,128],{"class":82},[42,1147,111],{"class":56},[42,1149,1150],{"class":44,"line":114},[42,1151,137],{"emptyLinePlaceholder":136},[42,1153,1154,1156,1158,1161],{"class":44,"line":133},[42,1155,143],{"class":108},[42,1157,57],{"class":56},[42,1159,1160],{"class":82},"2000",[42,1162,111],{"class":56},[14,1164,1165],{},"This may fail even though the function does stop eventually.",[14,1167,1168],{},"For beginners, the best fix is usually:",[191,1170,1171,1174],{},[194,1172,1173],{},"rewrite the code using a loop",[194,1175,1176],{},"avoid very deep recursion unless you truly need it",[14,1178,1179,1180,1183],{},"It is possible to change the recursion limit with ",[17,1181,1182],{},"sys.setrecursionlimit()",", but this is usually not the best first fix.",[14,1185,1186],{},"Why not?",[191,1188,1189,1192,1195],{},[194,1190,1191],{},"It can still fail",[194,1193,1194],{},"It can use more memory",[194,1196,1197],{},"It may hide a logic error instead of fixing it",[14,1199,1200],{},"If you are curious, Python can show help for that function:",[33,1202,1204],{"className":35,"code":1203,"language":37,"meta":38,"style":38},"import sys\nhelp(sys.setrecursionlimit)\n",[17,1205,1206,1214],{"__ignoreMap":38},[42,1207,1208,1211],{"class":44,"line":45},[42,1209,1210],{"class":70},"import",[42,1212,1213],{"class":74}," sys\n",[42,1215,1216,1219,1221,1224,1226,1230],{"class":44,"line":67},[42,1217,1218],{"class":102},"help",[42,1220,57],{"class":56},[42,1222,1223],{"class":108},"sys",[42,1225,382],{"class":56},[42,1227,1229],{"class":1228},"skxfh","setrecursionlimit",[42,1231,111],{"class":56},[14,1233,1234],{},"Use this carefully. In most beginner programs, changing the limit is not necessary.",[28,1236,1238],{"id":1237},"beginner-debugging-steps","Beginner debugging steps",[14,1240,1241],{},"If you see this error, use this checklist:",[297,1243,1244,1247,1250,1253,1256],{},[194,1245,1246],{},"Find the recursive function in the traceback",[194,1248,1249],{},"Identify the base case",[194,1251,1252],{},"Check whether the base case can actually become true",[194,1254,1255],{},"Check whether the argument changes on every call",[194,1257,1258],{},"Test with a very small input first",[14,1260,1261],{},"Useful debug lines:",[33,1263,1265],{"className":35,"code":1264,"language":37,"meta":38,"style":38},"print(n)\nprint(\"calling with:\", n)\n",[17,1266,1267,1278],{"__ignoreMap":38},[42,1268,1269,1272,1274,1276],{"class":44,"line":45},[42,1270,1271],{"class":102},"print",[42,1273,57],{"class":56},[42,1275,61],{"class":108},[42,1277,111],{"class":56},[42,1279,1280,1282,1284,1286,1288,1290,1292,1294],{"class":44,"line":67},[42,1281,1271],{"class":102},[42,1283,57],{"class":56},[42,1285,720],{"class":719},[42,1287,724],{"class":723},[42,1289,720],{"class":719},[42,1291,729],{"class":56},[42,1293,732],{"class":108},[42,1295,111],{"class":56},[14,1297,1298],{},"You can also inspect the traceback:",[33,1300,1302],{"className":35,"code":1301,"language":37,"meta":38,"style":38},"import traceback\n",[17,1303,1304],{"__ignoreMap":38},[42,1305,1306,1308],{"class":44,"line":45},[42,1307,1210],{"class":70},[42,1309,1310],{"class":74}," traceback\n",[14,1312,1313,1314,382],{},"If Python errors and exceptions are still confusing, read ",[261,1315,1317],{"href":1316},"\u002Flearn\u002Fpython-errors-and-exceptions-explained\u002F","Python errors and exceptions explained",[28,1319,1321],{"id":1320},"related-errors-and-confusion","Related errors and confusion",[14,1323,1324],{},"A recursion problem can make your program:",[191,1326,1327,1330,1333],{},[194,1328,1329],{},"slow before it fails",[194,1331,1332],{},"print many repeated lines",[194,1334,1335],{},"appear stuck",[14,1337,1338],{},"This error is different from other common errors:",[191,1340,1341,1352,1361],{},[194,1342,1343,1344,1347,1348,1351],{},"It is ",[154,1345,1346],{},"not"," a ",[17,1349,1350],{},"SyntaxError",", because the code can start running before failing",[194,1353,1343,1354,1356,1357,1360],{},[154,1355,1346],{}," usually a ",[17,1358,1359],{},"NameError",", because the function name normally exists",[194,1362,1363],{},"It is a runtime problem caused by how the function behaves while the program runs",[14,1365,1366,1367,382],{},"If you want to review how functions work in general, see ",[261,1368,1370],{"href":1369},"\u002Flearn\u002Fpython-functions-explained\u002F","Python functions explained",[28,1372,1374],{"id":1373},"common-causes","Common causes",[14,1376,1377],{},"These are the most common reasons beginners get this error:",[191,1379,1380,1383,1386,1389,1392,1395,1398],{},[194,1381,1382],{},"Missing base case",[194,1384,1385],{},"Wrong comparison in the base case",[194,1387,1388],{},"Recursive call uses the same value again",[194,1390,1391],{},"Recursive call moves away from the stopping condition",[194,1393,1394],{},"Mutual recursion between two functions",[194,1396,1397],{},"Using recursion for a task better solved with a loop",[194,1399,1400],{},"Trying to process very large input with recursion",[28,1402,1404],{"id":1403},"faq","FAQ",[414,1406,1408],{"id":1407},"what-is-recursion-in-python","What is recursion in Python?",[14,1410,1411],{},"Recursion is when a function calls itself. It must have a stopping condition called a base case.",[414,1413,1415],{"id":1414},"can-i-fix-this-by-increasing-the-recursion-limit","Can I fix this by increasing the recursion limit?",[14,1417,1418],{},"Sometimes, but it is usually better to fix the logic or rewrite the code with a loop first.",[414,1420,1422],{"id":1421},"why-does-my-base-case-not-work","Why does my base case not work?",[14,1424,1425],{},"The value may never reach the condition, or the comparison may be wrong.",[414,1427,1429],{"id":1428},"is-recursion-bad-in-python","Is recursion bad in Python?",[14,1431,1432],{},"No, but for many beginner tasks a loop is simpler and avoids recursion depth problems.",[28,1434,1436],{"id":1435},"see-also","See also",[191,1438,1439,1444,1448,1453,1457],{},[194,1440,1441],{},[261,1442,1443],{"href":263},"What recursion means in Python",[194,1445,1446],{},[261,1447,1370],{"href":1369},[194,1449,1450],{},[261,1451,1452],{"href":851},"Beginner guide to debugging Python code",[194,1454,1455],{},[261,1456,1317],{"href":1316},[194,1458,1459],{},[261,1460,1462],{"href":1461},"\u002Ferrors\u002Fruntimeerror-in-python-causes-and-fixes\u002F","RuntimeError in Python: causes and fixes",[1464,1465,1466],"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 .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 .skxfh, html code.shiki .skxfh{--shiki-light:#E53935;--shiki-default:#24292E;--shiki-dark:#E1E4E8}",{"title":38,"searchDepth":67,"depth":67,"links":1468},[1469,1470,1471,1472,1473,1481,1482,1483,1484,1485,1491],{"id":30,"depth":67,"text":31},{"id":238,"depth":67,"text":239},{"id":268,"depth":67,"text":269},{"id":310,"depth":67,"text":311},{"id":411,"depth":67,"text":412,"children":1474},[1475,1476,1477,1478,1479,1480],{"id":416,"depth":93,"text":417},{"id":495,"depth":93,"text":496},{"id":601,"depth":93,"text":602},{"id":685,"depth":93,"text":686},{"id":855,"depth":93,"text":856},{"id":954,"depth":93,"text":955},{"id":1093,"depth":67,"text":1094},{"id":1237,"depth":67,"text":1238},{"id":1320,"depth":67,"text":1321},{"id":1373,"depth":67,"text":1374},{"id":1403,"depth":67,"text":1404,"children":1486},[1487,1488,1489,1490],{"id":1407,"depth":93,"text":1408},{"id":1414,"depth":93,"text":1415},{"id":1421,"depth":93,"text":1422},{"id":1428,"depth":93,"text":1429},{"id":1435,"depth":67,"text":1436},"Master recursionerror maximum recursion depth exceeded fix in our comprehensive Python beginner guide.","md",{},"\u002Ferrors\u002Frecursionerror-maximum-recursion-depth-exceeded-fix",{"title":5,"description":1492},"errors\u002Frecursionerror-maximum-recursion-depth-exceeded-fix","6Z89f6JYTfEEyKQGFWrXzgMNKiZL6jIAD_Kk131-Nmk",1777585495263]