[{"data":1,"prerenderedAt":1641},["ShallowReactive",2],{"doc-\u002Ferrors\u002Funboundlocalerror-vs-nameerror-explained":3},{"id":4,"title":5,"body":6,"description":1634,"extension":1635,"meta":1636,"navigation":89,"path":1637,"seo":1638,"stem":1639,"__hash__":1640},"content\u002Ferrors\u002Funboundlocalerror-vs-nameerror-explained.md","UnboundLocalError vs NameError Explained",{"type":7,"value":8,"toc":1599},"minimark",[9,13,25,33,36,50,53,58,144,150,155,159,184,188,193,196,199,213,222,226,231,234,243,246,257,261,273,277,280,297,303,306,340,346,349,361,369,373,376,435,438,461,464,478,481,496,502,508,513,516,536,543,547,550,571,575,578,610,613,641,645,647,673,675,698,702,704,731,733,769,776,780,783,807,811,813,875,878,964,967,982,985,989,1050,1052,1060,1069,1073,1114,1116,1125,1128,1132,1140,1161,1165,1231,1233,1241,1248,1252,1344,1346,1354,1357,1361,1364,1384,1387,1472,1479,1483,1490,1510,1514,1518,1527,1531,1534,1538,1541,1545,1550,1554,1563,1567,1595],[10,11,5],"h1",{"id":12},"unboundlocalerror-vs-nameerror-explained",[14,15,16,20,21,24],"p",{},[17,18,19],"code",{},"UnboundLocalError"," and ",[17,22,23],{},"NameError"," can look very similar at first.",[14,26,27,28,32],{},"Both happen when Python has a problem with a variable name. But they do ",[29,30,31],"strong",{},"not"," mean the same thing.",[14,34,35],{},"This page helps you quickly tell the difference:",[37,38,39,45],"ul",{},[40,41,42,44],"li",{},[17,43,23],{}," means Python cannot find the name at all",[40,46,47,49],{},[17,48,19],{}," means Python treats the name as a local variable, but you used it before it got a value",[14,51,52],{},"If you are new to Python variables and scope, this difference can be confusing. The examples below show exactly what each error means and how to fix it.",[54,55,57],"h2",{"id":56},"quick-fix","Quick fix",[59,60,65],"pre",{"className":61,"code":62,"language":63,"meta":64,"style":64},"language-python shiki shiki-themes material-theme-lighter github-light github-dark","x = 10\n\ndef show_value():\n    # If you only want to read x, do not assign to x in this function\n    print(x)\n\nshow_value()\n","python","",[17,66,67,84,91,106,113,130,135],{"__ignoreMap":64},[68,69,72,76,80],"span",{"class":70,"line":71},"line",1,[68,73,75],{"class":74},"su5hD","x ",[68,77,79],{"class":78},"smGrS","=",[68,81,83],{"class":82},"srdBf"," 10\n",[68,85,87],{"class":70,"line":86},2,[68,88,90],{"emptyLinePlaceholder":89},true,"\n",[68,92,94,98,102],{"class":70,"line":93},3,[68,95,97],{"class":96},"sbsja","def",[68,99,101],{"class":100},"sGLFI"," show_value",[68,103,105],{"class":104},"sP7_E","():\n",[68,107,109],{"class":70,"line":108},4,[68,110,112],{"class":111},"sutJx","    # If you only want to read x, do not assign to x in this function\n",[68,114,116,120,123,127],{"class":70,"line":115},5,[68,117,119],{"class":118},"sptTA","    print",[68,121,122],{"class":104},"(",[68,124,126],{"class":125},"slqww","x",[68,128,129],{"class":104},")\n",[68,131,133],{"class":70,"line":132},6,[68,134,90],{"emptyLinePlaceholder":89},[68,136,138,141],{"class":70,"line":137},7,[68,139,140],{"class":125},"show_value",[68,142,143],{"class":104},"()\n",[14,145,146,147,149],{},"If Python says ",[17,148,23],{},", the variable name usually does not exist in the current scope.",[14,151,146,152,154],{},[17,153,19],{},", Python thinks the name is local in the function, but you used it before assigning a value.",[54,156,158],{"id":157},"what-this-page-helps-you-fix","What this page helps you fix",[37,160,161,169,178,181],{},[40,162,163,164,20,166,168],{},"Understand why ",[17,165,19],{},[17,167,23],{}," look similar",[40,170,171,172,20,175],{},"Learn the key difference between ",[29,173,174],{},"\"not defined\"",[29,176,177],{},"\"local before assignment\"",[40,179,180],{},"Use simple rules to identify the right fix",[40,182,183],{},"See short examples for both errors",[54,185,187],{"id":186},"what-nameerror-means","What NameError means",[14,189,190,192],{},[17,191,23],{}," happens when Python cannot find a variable or function name.",[14,194,195],{},"In simple terms, Python looks for the name and does not find it where it expects to.",[14,197,198],{},"Common causes:",[37,200,201,204,207,210],{},[40,202,203],{},"A typo in a variable name",[40,205,206],{},"Using a variable before creating it",[40,208,209],{},"Calling a function that was never defined",[40,211,212],{},"Forgetting to import something before using it",[14,214,215,216,221],{},"If you need a full guide, see ",[217,218,220],"a",{"href":219},"\u002Ferrors\u002Fnameerror-in-python-causes-and-fixes\u002F","NameError in Python: causes and fixes",".",[54,223,225],{"id":224},"what-unboundlocalerror-means","What UnboundLocalError means",[14,227,228,230],{},[17,229,19],{}," is a more specific scope-related error.",[14,232,233],{},"It usually happens inside a function.",[14,235,236,237,240,241,221],{},"Python decides a name is ",[29,238,239],{},"local"," if you assign to it anywhere in that function. If you try to read that name before the assignment line runs, Python raises ",[17,242,19],{},[14,244,245],{},"This often surprises beginners when a global variable has the same name.",[14,247,248,249,20,253,221],{},"If you want more background first, see ",[217,250,252],{"href":251},"\u002Flearn\u002Fpython-variables-explained-for-beginners\u002F","Python variables explained for beginners",[217,254,256],{"href":255},"\u002Flearn\u002Fpython-functions-explained\u002F","Python functions explained",[54,258,260],{"id":259},"main-difference-in-one-sentence","Main difference in one sentence",[37,262,263,268],{},[40,264,265,267],{},[17,266,23],{},": Python cannot find the name",[40,269,270,272],{},[17,271,19],{},": Python found the name as a local variable, but it has no value yet",[54,274,276],{"id":275},"example-that-causes-nameerror","Example that causes NameError",[14,278,279],{},"Here is a simple example:",[59,281,283],{"className":61,"code":282,"language":63,"meta":64,"style":64},"print(user_name)\n",[17,284,285],{"__ignoreMap":64},[68,286,287,290,292,295],{"class":70,"line":71},[68,288,289],{"class":118},"print",[68,291,122],{"class":104},[68,293,294],{"class":125},"user_name",[68,296,129],{"class":104},[14,298,299,300,302],{},"This fails because ",[17,301,294],{}," was never created.",[14,304,305],{},"You would get an error like this:",[59,307,309],{"className":61,"code":308,"language":63,"meta":64,"style":64},"NameError: name 'user_name' is not defined\n",[17,310,311],{"__ignoreMap":64},[68,312,313,316,319,322,326,329,331,334,337],{"class":70,"line":71},[68,314,23],{"class":315},"sZMiF",[68,317,318],{"class":104},":",[68,320,321],{"class":74}," name ",[68,323,325],{"class":324},"sjJ54","'",[68,327,294],{"class":328},"s_sjI",[68,330,325],{"class":324},[68,332,333],{"class":78}," is",[68,335,336],{"class":78}," not",[68,338,339],{"class":74}," defined\n",[14,341,342,343,345],{},"Python checks for ",[17,344,294],{}," and cannot find it.",[14,347,348],{},"Another common example is calling a function that does not exist:",[59,350,352],{"className":61,"code":351,"language":63,"meta":64,"style":64},"say_hello()\n",[17,353,354],{"__ignoreMap":64},[68,355,356,359],{"class":70,"line":71},[68,357,358],{"class":125},"say_hello",[68,360,143],{"class":104},[14,362,363,364,366,367,221],{},"If ",[17,365,358],{}," was never defined or imported, Python raises ",[17,368,23],{},[54,370,372],{"id":371},"example-that-causes-unboundlocalerror","Example that causes UnboundLocalError",[14,374,375],{},"This example uses a global variable:",[59,377,379],{"className":61,"code":378,"language":63,"meta":64,"style":64},"count = 10\n\ndef show_count():\n    print(count)\n    count = 20\n\nshow_count()\n",[17,380,381,390,394,403,414,424,428],{"__ignoreMap":64},[68,382,383,386,388],{"class":70,"line":71},[68,384,385],{"class":74},"count ",[68,387,79],{"class":78},[68,389,83],{"class":82},[68,391,392],{"class":70,"line":86},[68,393,90],{"emptyLinePlaceholder":89},[68,395,396,398,401],{"class":70,"line":93},[68,397,97],{"class":96},[68,399,400],{"class":100}," show_count",[68,402,105],{"class":104},[68,404,405,407,409,412],{"class":70,"line":108},[68,406,119],{"class":118},[68,408,122],{"class":104},[68,410,411],{"class":125},"count",[68,413,129],{"class":104},[68,415,416,419,421],{"class":70,"line":115},[68,417,418],{"class":74},"    count ",[68,420,79],{"class":78},[68,422,423],{"class":82}," 20\n",[68,425,426],{"class":70,"line":132},[68,427,90],{"emptyLinePlaceholder":89},[68,429,430,433],{"class":70,"line":137},[68,431,432],{"class":125},"show_count",[68,434,143],{"class":104},[14,436,437],{},"This raises:",[59,439,441],{"className":61,"code":440,"language":63,"meta":64,"style":64},"UnboundLocalError: local variable 'count' referenced before assignment\n",[17,442,443],{"__ignoreMap":64},[68,444,445,447,449,452,454,456,458],{"class":70,"line":71},[68,446,19],{"class":315},[68,448,318],{"class":104},[68,450,451],{"class":74}," local variable ",[68,453,325],{"class":324},[68,455,411],{"class":328},[68,457,325],{"class":324},[68,459,460],{"class":74}," referenced before assignment\n",[14,462,463],{},"Why?",[14,465,466,467,470,471,473,474,477],{},"Because Python sees ",[17,468,469],{},"count = 20"," inside the function. That makes ",[17,472,411],{}," a ",[29,475,476],{},"local variable"," for the whole function.",[14,479,480],{},"So this line:",[59,482,484],{"className":61,"code":483,"language":63,"meta":64,"style":64},"print(count)\n",[17,485,486],{"__ignoreMap":64},[68,487,488,490,492,494],{"class":70,"line":71},[68,489,289],{"class":118},[68,491,122],{"class":104},[68,493,411],{"class":125},[68,495,129],{"class":104},[14,497,498,499,501],{},"tries to read the local ",[17,500,411],{}," before it has been assigned a value.",[14,503,504,505,507],{},"Even though there is a global ",[17,506,411],{},", Python does not use it here.",[509,510,512],"h3",{"id":511},"a-simple-way-to-think-about-it","A simple way to think about it",[14,514,515],{},"Inside a function:",[37,517,518,525,531],{},[40,519,520,521,524],{},"If you ",[29,522,523],{},"only read"," a variable, Python may use an outer value",[40,526,520,527,530],{},[29,528,529],{},"assign"," to that variable in the function, Python treats it as local",[40,532,533,534],{},"If you read it before assigning it, you get ",[17,535,19],{},[14,537,538,539,221],{},"For a full fix guide, see ",[217,540,542],{"href":541},"\u002Ferrors\u002Funboundlocalerror-local-variable-referenced-before-assignment-fix\u002F","UnboundLocalError: local variable referenced before assignment",[54,544,546],{"id":545},"how-to-fix-nameerror","How to fix NameError",[14,548,549],{},"Try these checks:",[37,551,552,555,558,561,564],{},[40,553,554],{},"Check for spelling mistakes",[40,556,557],{},"Make sure the variable is assigned before you use it",[40,559,560],{},"Make sure the function or module was defined or imported",[40,562,563],{},"Check whether the name is inside the correct scope",[40,565,566,567,570],{},"Use ",[17,568,569],{},"print()"," to confirm what exists before the failing line",[509,572,574],{"id":573},"example-fix-a-typo","Example: fix a typo",[14,576,577],{},"Bad code:",[59,579,581],{"className":61,"code":580,"language":63,"meta":64,"style":64},"message = \"Hello\"\nprint(mesage)\n",[17,582,583,599],{"__ignoreMap":64},[68,584,585,588,590,593,596],{"class":70,"line":71},[68,586,587],{"class":74},"message ",[68,589,79],{"class":78},[68,591,592],{"class":324}," \"",[68,594,595],{"class":328},"Hello",[68,597,598],{"class":324},"\"\n",[68,600,601,603,605,608],{"class":70,"line":86},[68,602,289],{"class":118},[68,604,122],{"class":104},[68,606,607],{"class":125},"mesage",[68,609,129],{"class":104},[14,611,612],{},"Fixed code:",[59,614,616],{"className":61,"code":615,"language":63,"meta":64,"style":64},"message = \"Hello\"\nprint(message)\n",[17,617,618,630],{"__ignoreMap":64},[68,619,620,622,624,626,628],{"class":70,"line":71},[68,621,587],{"class":74},[68,623,79],{"class":78},[68,625,592],{"class":324},[68,627,595],{"class":328},[68,629,598],{"class":324},[68,631,632,634,636,639],{"class":70,"line":86},[68,633,289],{"class":118},[68,635,122],{"class":104},[68,637,638],{"class":125},"message",[68,640,129],{"class":104},[509,642,644],{"id":643},"example-assign-before-use","Example: assign before use",[14,646,577],{},[59,648,650],{"className":61,"code":649,"language":63,"meta":64,"style":64},"print(total)\ntotal = 5\n",[17,651,652,663],{"__ignoreMap":64},[68,653,654,656,658,661],{"class":70,"line":71},[68,655,289],{"class":118},[68,657,122],{"class":104},[68,659,660],{"class":125},"total",[68,662,129],{"class":104},[68,664,665,668,670],{"class":70,"line":86},[68,666,667],{"class":74},"total ",[68,669,79],{"class":78},[68,671,672],{"class":82}," 5\n",[14,674,612],{},[59,676,678],{"className":61,"code":677,"language":63,"meta":64,"style":64},"total = 5\nprint(total)\n",[17,679,680,688],{"__ignoreMap":64},[68,681,682,684,686],{"class":70,"line":71},[68,683,667],{"class":74},[68,685,79],{"class":78},[68,687,672],{"class":82},[68,689,690,692,694,696],{"class":70,"line":86},[68,691,289],{"class":118},[68,693,122],{"class":104},[68,695,660],{"class":125},[68,697,129],{"class":104},[509,699,701],{"id":700},"example-import-before-use","Example: import before use",[14,703,577],{},[59,705,707],{"className":61,"code":706,"language":63,"meta":64,"style":64},"print(math.sqrt(16))\n",[17,708,709],{"__ignoreMap":64},[68,710,711,713,715,718,720,723,725,728],{"class":70,"line":71},[68,712,289],{"class":118},[68,714,122],{"class":104},[68,716,717],{"class":125},"math",[68,719,221],{"class":104},[68,721,722],{"class":125},"sqrt",[68,724,122],{"class":104},[68,726,727],{"class":82},"16",[68,729,730],{"class":104},"))\n",[14,732,612],{},[59,734,736],{"className":61,"code":735,"language":63,"meta":64,"style":64},"import math\n\nprint(math.sqrt(16))\n",[17,737,738,747,751],{"__ignoreMap":64},[68,739,740,744],{"class":70,"line":71},[68,741,743],{"class":742},"sVHd0","import",[68,745,746],{"class":74}," math\n",[68,748,749],{"class":70,"line":86},[68,750,90],{"emptyLinePlaceholder":89},[68,752,753,755,757,759,761,763,765,767],{"class":70,"line":93},[68,754,289],{"class":118},[68,756,122],{"class":104},[68,758,717],{"class":125},[68,760,221],{"class":104},[68,762,722],{"class":125},[68,764,122],{"class":104},[68,766,727],{"class":82},[68,768,730],{"class":104},[14,770,771,772,221],{},"If imports are confusing, read ",[217,773,775],{"href":774},"\u002Flearn\u002Fhow-import-works-in-python\u002F","how import works in Python",[54,777,779],{"id":778},"how-to-fix-unboundlocalerror","How to fix UnboundLocalError",[14,781,782],{},"Good fixes usually include one of these:",[37,784,785,788,791,794,797],{},[40,786,787],{},"Assign the variable before reading it in the function",[40,789,790],{},"Use a different local variable name",[40,792,793],{},"Pass the value into the function as an argument",[40,795,796],{},"Return a new value instead of modifying outer state",[40,798,566,799,802,803,806],{},[17,800,801],{},"global"," or ",[17,804,805],{},"nonlocal"," only when you truly need shared state",[509,808,810],{"id":809},"fix-1-pass-the-value-into-the-function","Fix 1: pass the value into the function",[14,812,577],{},[59,814,816],{"className":61,"code":815,"language":63,"meta":64,"style":64},"count = 10\n\ndef update_count():\n    print(count)\n    count = count + 1\n\nupdate_count()\n",[17,817,818,826,830,839,849,864,868],{"__ignoreMap":64},[68,819,820,822,824],{"class":70,"line":71},[68,821,385],{"class":74},[68,823,79],{"class":78},[68,825,83],{"class":82},[68,827,828],{"class":70,"line":86},[68,829,90],{"emptyLinePlaceholder":89},[68,831,832,834,837],{"class":70,"line":93},[68,833,97],{"class":96},[68,835,836],{"class":100}," update_count",[68,838,105],{"class":104},[68,840,841,843,845,847],{"class":70,"line":108},[68,842,119],{"class":118},[68,844,122],{"class":104},[68,846,411],{"class":125},[68,848,129],{"class":104},[68,850,851,853,855,858,861],{"class":70,"line":115},[68,852,418],{"class":74},[68,854,79],{"class":78},[68,856,857],{"class":74}," count ",[68,859,860],{"class":78},"+",[68,862,863],{"class":82}," 1\n",[68,865,866],{"class":70,"line":132},[68,867,90],{"emptyLinePlaceholder":89},[68,869,870,873],{"class":70,"line":137},[68,871,872],{"class":125},"update_count",[68,874,143],{"class":104},[14,876,877],{},"Better code:",[59,879,881],{"className":61,"code":880,"language":63,"meta":64,"style":64},"def update_count(count):\n    print(count)\n    count = count + 1\n    return count\n\ncount = 10\ncount = update_count(count)\nprint(count)\n",[17,882,883,897,907,919,927,931,939,953],{"__ignoreMap":64},[68,884,885,887,889,891,894],{"class":70,"line":71},[68,886,97],{"class":96},[68,888,836],{"class":100},[68,890,122],{"class":104},[68,892,411],{"class":893},"sFwrP",[68,895,896],{"class":104},"):\n",[68,898,899,901,903,905],{"class":70,"line":86},[68,900,119],{"class":118},[68,902,122],{"class":104},[68,904,411],{"class":125},[68,906,129],{"class":104},[68,908,909,911,913,915,917],{"class":70,"line":93},[68,910,418],{"class":74},[68,912,79],{"class":78},[68,914,857],{"class":74},[68,916,860],{"class":78},[68,918,863],{"class":82},[68,920,921,924],{"class":70,"line":108},[68,922,923],{"class":742},"    return",[68,925,926],{"class":74}," count\n",[68,928,929],{"class":70,"line":115},[68,930,90],{"emptyLinePlaceholder":89},[68,932,933,935,937],{"class":70,"line":132},[68,934,385],{"class":74},[68,936,79],{"class":78},[68,938,83],{"class":82},[68,940,941,943,945,947,949,951],{"class":70,"line":137},[68,942,385],{"class":74},[68,944,79],{"class":78},[68,946,836],{"class":125},[68,948,122],{"class":104},[68,950,411],{"class":125},[68,952,129],{"class":104},[68,954,956,958,960,962],{"class":70,"line":955},8,[68,957,289],{"class":118},[68,959,122],{"class":104},[68,961,411],{"class":125},[68,963,129],{"class":104},[14,965,966],{},"Expected output:",[59,968,970],{"className":61,"code":969,"language":63,"meta":64,"style":64},"10\n11\n",[17,971,972,977],{"__ignoreMap":64},[68,973,974],{"class":70,"line":71},[68,975,976],{"class":82},"10\n",[68,978,979],{"class":70,"line":86},[68,980,981],{"class":82},"11\n",[14,983,984],{},"This is often the best beginner-friendly solution.",[509,986,988],{"id":987},"fix-2-use-a-different-local-variable-name","Fix 2: use a different local variable name",[59,990,992],{"className":61,"code":991,"language":63,"meta":64,"style":64},"count = 10\n\ndef show_new_count():\n    new_count = count + 1\n    print(new_count)\n\nshow_new_count()\n",[17,993,994,1002,1006,1015,1028,1039,1043],{"__ignoreMap":64},[68,995,996,998,1000],{"class":70,"line":71},[68,997,385],{"class":74},[68,999,79],{"class":78},[68,1001,83],{"class":82},[68,1003,1004],{"class":70,"line":86},[68,1005,90],{"emptyLinePlaceholder":89},[68,1007,1008,1010,1013],{"class":70,"line":93},[68,1009,97],{"class":96},[68,1011,1012],{"class":100}," show_new_count",[68,1014,105],{"class":104},[68,1016,1017,1020,1022,1024,1026],{"class":70,"line":108},[68,1018,1019],{"class":74},"    new_count ",[68,1021,79],{"class":78},[68,1023,857],{"class":74},[68,1025,860],{"class":78},[68,1027,863],{"class":82},[68,1029,1030,1032,1034,1037],{"class":70,"line":115},[68,1031,119],{"class":118},[68,1033,122],{"class":104},[68,1035,1036],{"class":125},"new_count",[68,1038,129],{"class":104},[68,1040,1041],{"class":70,"line":132},[68,1042,90],{"emptyLinePlaceholder":89},[68,1044,1045,1048],{"class":70,"line":137},[68,1046,1047],{"class":125},"show_new_count",[68,1049,143],{"class":104},[14,1051,966],{},[59,1053,1054],{"className":61,"code":981,"language":63,"meta":64,"style":64},[17,1055,1056],{"__ignoreMap":64},[68,1057,1058],{"class":70,"line":71},[68,1059,981],{"class":82},[14,1061,1062,1063,1065,1066,1068],{},"Here, ",[17,1064,411],{}," is read from outside the function, and ",[17,1067,1036],{}," is local.",[509,1070,1072],{"id":1071},"fix-3-assign-before-reading-the-local-variable","Fix 3: assign before reading the local variable",[59,1074,1076],{"className":61,"code":1075,"language":63,"meta":64,"style":64},"def show_count():\n    count = 20\n    print(count)\n\nshow_count()\n",[17,1077,1078,1086,1094,1104,1108],{"__ignoreMap":64},[68,1079,1080,1082,1084],{"class":70,"line":71},[68,1081,97],{"class":96},[68,1083,400],{"class":100},[68,1085,105],{"class":104},[68,1087,1088,1090,1092],{"class":70,"line":86},[68,1089,418],{"class":74},[68,1091,79],{"class":78},[68,1093,423],{"class":82},[68,1095,1096,1098,1100,1102],{"class":70,"line":93},[68,1097,119],{"class":118},[68,1099,122],{"class":104},[68,1101,411],{"class":125},[68,1103,129],{"class":104},[68,1105,1106],{"class":70,"line":108},[68,1107,90],{"emptyLinePlaceholder":89},[68,1109,1110,1112],{"class":70,"line":115},[68,1111,432],{"class":125},[68,1113,143],{"class":104},[14,1115,966],{},[59,1117,1119],{"className":61,"code":1118,"language":63,"meta":64,"style":64},"20\n",[17,1120,1121],{"__ignoreMap":64},[68,1122,1123],{"class":70,"line":71},[68,1124,1118],{"class":82},[14,1126,1127],{},"This works because the local variable gets its value before it is used.",[54,1129,1131],{"id":1130},"when-global-and-nonlocal-are-involved","When global and nonlocal are involved",[14,1133,1134,1135,20,1137,1139],{},"Sometimes ",[17,1136,801],{},[17,1138,805],{}," are part of the fix.",[37,1141,1142,1147,1152,1158],{},[40,1143,1144,1146],{},[17,1145,801],{}," tells Python to use a module-level variable",[40,1148,1149,1151],{},[17,1150,805],{}," tells Python to use a variable from an outer function",[40,1153,1154,1155,1157],{},"These can fix some ",[17,1156,19],{}," cases",[40,1159,1160],{},"Beginners should usually prefer arguments and return values when possible",[509,1162,1164],{"id":1163},"example-with-global","Example with global",[59,1166,1168],{"className":61,"code":1167,"language":63,"meta":64,"style":64},"count = 10\n\ndef increase():\n    global count\n    count = count + 1\n    print(count)\n\nincrease()\n",[17,1169,1170,1178,1182,1191,1198,1210,1220,1224],{"__ignoreMap":64},[68,1171,1172,1174,1176],{"class":70,"line":71},[68,1173,385],{"class":74},[68,1175,79],{"class":78},[68,1177,83],{"class":82},[68,1179,1180],{"class":70,"line":86},[68,1181,90],{"emptyLinePlaceholder":89},[68,1183,1184,1186,1189],{"class":70,"line":93},[68,1185,97],{"class":96},[68,1187,1188],{"class":100}," increase",[68,1190,105],{"class":104},[68,1192,1193,1196],{"class":70,"line":108},[68,1194,1195],{"class":96},"    global",[68,1197,926],{"class":74},[68,1199,1200,1202,1204,1206,1208],{"class":70,"line":115},[68,1201,418],{"class":74},[68,1203,79],{"class":78},[68,1205,857],{"class":74},[68,1207,860],{"class":78},[68,1209,863],{"class":82},[68,1211,1212,1214,1216,1218],{"class":70,"line":132},[68,1213,119],{"class":118},[68,1215,122],{"class":104},[68,1217,411],{"class":125},[68,1219,129],{"class":104},[68,1221,1222],{"class":70,"line":137},[68,1223,90],{"emptyLinePlaceholder":89},[68,1225,1226,1229],{"class":70,"line":955},[68,1227,1228],{"class":125},"increase",[68,1230,143],{"class":104},[14,1232,966],{},[59,1234,1235],{"className":61,"code":981,"language":63,"meta":64,"style":64},[17,1236,1237],{"__ignoreMap":64},[68,1238,1239],{"class":70,"line":71},[68,1240,981],{"class":82},[14,1242,1243,1244,1247],{},"Because of ",[17,1245,1246],{},"global count",", Python uses the module-level variable.",[509,1249,1251],{"id":1250},"example-with-nonlocal","Example with nonlocal",[59,1253,1255],{"className":61,"code":1254,"language":63,"meta":64,"style":64},"def outer():\n    count = 10\n\n    def inner():\n        nonlocal count\n        count = count + 1\n        print(count)\n\n    inner()\n\nouter()\n",[17,1256,1257,1266,1274,1278,1288,1295,1308,1319,1323,1331,1336],{"__ignoreMap":64},[68,1258,1259,1261,1264],{"class":70,"line":71},[68,1260,97],{"class":96},[68,1262,1263],{"class":100}," outer",[68,1265,105],{"class":104},[68,1267,1268,1270,1272],{"class":70,"line":86},[68,1269,418],{"class":74},[68,1271,79],{"class":78},[68,1273,83],{"class":82},[68,1275,1276],{"class":70,"line":93},[68,1277,90],{"emptyLinePlaceholder":89},[68,1279,1280,1283,1286],{"class":70,"line":108},[68,1281,1282],{"class":96},"    def",[68,1284,1285],{"class":100}," inner",[68,1287,105],{"class":104},[68,1289,1290,1293],{"class":70,"line":115},[68,1291,1292],{"class":96},"        nonlocal",[68,1294,926],{"class":74},[68,1296,1297,1300,1302,1304,1306],{"class":70,"line":132},[68,1298,1299],{"class":74},"        count ",[68,1301,79],{"class":78},[68,1303,857],{"class":74},[68,1305,860],{"class":78},[68,1307,863],{"class":82},[68,1309,1310,1313,1315,1317],{"class":70,"line":137},[68,1311,1312],{"class":118},"        print",[68,1314,122],{"class":104},[68,1316,411],{"class":125},[68,1318,129],{"class":104},[68,1320,1321],{"class":70,"line":955},[68,1322,90],{"emptyLinePlaceholder":89},[68,1324,1326,1329],{"class":70,"line":1325},9,[68,1327,1328],{"class":125},"    inner",[68,1330,143],{"class":104},[68,1332,1334],{"class":70,"line":1333},10,[68,1335,90],{"emptyLinePlaceholder":89},[68,1337,1339,1342],{"class":70,"line":1338},11,[68,1340,1341],{"class":125},"outer",[68,1343,143],{"class":104},[14,1345,966],{},[59,1347,1348],{"className":61,"code":981,"language":63,"meta":64,"style":64},[17,1349,1350],{"__ignoreMap":64},[68,1351,1352],{"class":70,"line":71},[68,1353,981],{"class":82},[14,1355,1356],{},"Use these keywords carefully. They can work, but they can also make code harder to follow.",[54,1358,1360],{"id":1359},"quick-debugging-checklist","Quick debugging checklist",[14,1362,1363],{},"When you see one of these errors, check this list:",[37,1365,1366,1369,1372,1375,1378,1381],{},[40,1367,1368],{},"Read the exact error name carefully",[40,1370,1371],{},"Check whether the problem line is inside a function",[40,1373,1374],{},"Look for assignments to the same variable later in that function",[40,1376,1377],{},"Check whether the variable was ever created",[40,1379,1380],{},"Print values before the failing line if needed",[40,1382,1383],{},"Rename variables to avoid confusing shadowing",[14,1385,1386],{},"Helpful debugging commands:",[59,1388,1390],{"className":61,"code":1389,"language":63,"meta":64,"style":64},"print(variable_name)\nprint(locals())\nprint(globals().keys())\ntype(variable_name)\nhelp('global')\nhelp('nonlocal')\n",[17,1391,1392,1403,1415,1432,1443,1458],{"__ignoreMap":64},[68,1393,1394,1396,1398,1401],{"class":70,"line":71},[68,1395,289],{"class":118},[68,1397,122],{"class":104},[68,1399,1400],{"class":125},"variable_name",[68,1402,129],{"class":104},[68,1404,1405,1407,1409,1412],{"class":70,"line":86},[68,1406,289],{"class":118},[68,1408,122],{"class":104},[68,1410,1411],{"class":118},"locals",[68,1413,1414],{"class":104},"())\n",[68,1416,1417,1419,1421,1424,1427,1430],{"class":70,"line":93},[68,1418,289],{"class":118},[68,1420,122],{"class":104},[68,1422,1423],{"class":118},"globals",[68,1425,1426],{"class":104},"().",[68,1428,1429],{"class":125},"keys",[68,1431,1414],{"class":104},[68,1433,1434,1437,1439,1441],{"class":70,"line":108},[68,1435,1436],{"class":315},"type",[68,1438,122],{"class":104},[68,1440,1400],{"class":125},[68,1442,129],{"class":104},[68,1444,1445,1448,1450,1452,1454,1456],{"class":70,"line":115},[68,1446,1447],{"class":118},"help",[68,1449,122],{"class":104},[68,1451,325],{"class":324},[68,1453,801],{"class":328},[68,1455,325],{"class":324},[68,1457,129],{"class":104},[68,1459,1460,1462,1464,1466,1468,1470],{"class":70,"line":132},[68,1461,1447],{"class":118},[68,1463,122],{"class":104},[68,1465,325],{"class":324},[68,1467,805],{"class":328},[68,1469,325],{"class":324},[68,1471,129],{"class":104},[14,1473,1474,1475,221],{},"If you want a broader step-by-step process, see ",[217,1476,1478],{"href":1477},"\u002Fhow-to\u002Fhow-to-debug-python-code-beginner-guide\u002F","how to debug Python code",[54,1480,1482],{"id":1481},"common-mistakes","Common mistakes",[14,1484,1485,1486,802,1488,318],{},"These are the most common reasons beginners see ",[17,1487,23],{},[17,1489,19],{},[37,1491,1492,1495,1498,1501,1504,1507],{},[40,1493,1494],{},"Misspelled variable or function name",[40,1496,1497],{},"Using a variable before assigning a value",[40,1499,1500],{},"Assigning to a variable inside a function after reading it",[40,1502,1503],{},"Confusing global and local variables",[40,1505,1506],{},"Forgetting to import a name before using it",[40,1508,1509],{},"Reusing the same variable name in nested scopes",[54,1511,1513],{"id":1512},"faq","FAQ",[509,1515,1517],{"id":1516},"is-unboundlocalerror-the-same-as-nameerror","Is UnboundLocalError the same as NameError?",[14,1519,1520,1521,1523,1524,1526],{},"No. ",[17,1522,19],{}," is a more specific scope-related error. ",[17,1525,23],{}," means the name cannot be found at all.",[509,1528,1530],{"id":1529},"why-do-i-get-unboundlocalerror-even-though-the-variable-exists-outside-the-function","Why do I get UnboundLocalError even though the variable exists outside the function?",[14,1532,1533],{},"Because assigning to that name inside the function makes Python treat it as a local variable for the whole function.",[509,1535,1537],{"id":1536},"should-i-use-global-to-fix-unboundlocalerror","Should I use global to fix UnboundLocalError?",[14,1539,1540],{},"Sometimes, but beginners should usually pass values into functions and return results instead.",[509,1542,1544],{"id":1543},"can-nameerror-happen-with-function-names-too","Can NameError happen with function names too?",[14,1546,1547,1548,221],{},"Yes. If you call a function that was not defined or imported, Python raises ",[17,1549,23],{},[509,1551,1553],{"id":1552},"can-a-typo-cause-both-errors","Can a typo cause both errors?",[14,1555,1556,1557,1559,1560,1562],{},"A typo usually causes ",[17,1558,23],{},". ",[17,1561,19],{}," is more often caused by scope and assignment inside a function.",[54,1564,1566],{"id":1565},"see-also","See also",[37,1568,1569,1573,1577,1581,1585,1590],{},[40,1570,1571],{},[217,1572,220],{"href":219},[40,1574,1575],{},[217,1576,542],{"href":541},[40,1578,1579],{},[217,1580,252],{"href":251},[40,1582,1583],{},[217,1584,256],{"href":255},[40,1586,1587],{},[217,1588,1589],{"href":1477},"How to debug Python code",[40,1591,1592],{},[217,1593,1594],{"href":774},"How import works in Python",[1596,1597,1598],"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 .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 .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}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 .sFwrP, html code.shiki .sFwrP{--shiki-light:#90A4AE;--shiki-light-font-style:italic;--shiki-default:#24292E;--shiki-default-font-style:inherit;--shiki-dark:#E1E4E8;--shiki-dark-font-style:inherit}",{"title":64,"searchDepth":86,"depth":86,"links":1600},[1601,1602,1603,1604,1605,1606,1607,1610,1615,1620,1624,1625,1626,1633],{"id":56,"depth":86,"text":57},{"id":157,"depth":86,"text":158},{"id":186,"depth":86,"text":187},{"id":224,"depth":86,"text":225},{"id":259,"depth":86,"text":260},{"id":275,"depth":86,"text":276},{"id":371,"depth":86,"text":372,"children":1608},[1609],{"id":511,"depth":93,"text":512},{"id":545,"depth":86,"text":546,"children":1611},[1612,1613,1614],{"id":573,"depth":93,"text":574},{"id":643,"depth":93,"text":644},{"id":700,"depth":93,"text":701},{"id":778,"depth":86,"text":779,"children":1616},[1617,1618,1619],{"id":809,"depth":93,"text":810},{"id":987,"depth":93,"text":988},{"id":1071,"depth":93,"text":1072},{"id":1130,"depth":86,"text":1131,"children":1621},[1622,1623],{"id":1163,"depth":93,"text":1164},{"id":1250,"depth":93,"text":1251},{"id":1359,"depth":86,"text":1360},{"id":1481,"depth":86,"text":1482},{"id":1512,"depth":86,"text":1513,"children":1627},[1628,1629,1630,1631,1632],{"id":1516,"depth":93,"text":1517},{"id":1529,"depth":93,"text":1530},{"id":1536,"depth":93,"text":1537},{"id":1543,"depth":93,"text":1544},{"id":1552,"depth":93,"text":1553},{"id":1565,"depth":86,"text":1566},"Master unboundlocalerror vs nameerror explained in our comprehensive Python beginner guide.","md",{},"\u002Ferrors\u002Funboundlocalerror-vs-nameerror-explained",{"title":5,"description":1634},"errors\u002Funboundlocalerror-vs-nameerror-explained","SVzBQ-8S03NEsFKd0WNFWcpinj2hRhvvLSjer-p0B24",1777585482241]