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