[{"data":1,"prerenderedAt":1439},["ShallowReactive",2],{"doc-\u002Ferrors\u002Fnameerror-name-is-not-defined-fix":3},{"id":4,"title":5,"body":6,"description":1432,"extension":1433,"meta":1434,"navigation":535,"path":1435,"seo":1436,"stem":1437,"__hash__":1438},"content\u002Ferrors\u002Fnameerror-name-is-not-defined-fix.md","NameError: name is not defined (Fix)",{"type":7,"value":8,"toc":1408},"minimark",[9,13,22,27,78,81,85,91,99,115,121,124,140,143,175,181,185,188,214,216,240,250,253,276,278,287,295,299,304,307,321,332,360,362,387,390,417,420,424,427,430,456,459,482,488,491,495,498,558,560,585,591,594,655,657,666,669,676,680,685,712,714,739,741,776,778,787,790,826,828,836,843,847,850,853,869,880,882,907,910,930,932,941,944,977,980,984,991,1009,1012,1069,1072,1104,1115,1119,1124,1128,1131,1147,1155,1159,1162,1223,1232,1239,1243,1246,1288,1297,1304,1308,1311,1331,1338,1342,1346,1349,1353,1356,1360,1366,1370,1373,1377,1404],[10,11,5],"h1",{"id":12},"nameerror-name-is-not-defined-fix",[14,15,16,17,21],"p",{},"Fix the Python error ",[18,19,20],"code",{},"NameError: name is not defined",". This page explains what the error means, why it happens, and the most common ways to correct 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","user_name = \"Sam\"\nprint(user_name)\n","python","",[18,35,36,60],{"__ignoreMap":33},[37,38,41,45,49,53,57],"span",{"class":39,"line":40},"line",1,[37,42,44],{"class":43},"su5hD","user_name ",[37,46,48],{"class":47},"smGrS","=",[37,50,52],{"class":51},"sjJ54"," \"",[37,54,56],{"class":55},"s_sjI","Sam",[37,58,59],{"class":51},"\"\n",[37,61,63,67,71,75],{"class":39,"line":62},2,[37,64,66],{"class":65},"sptTA","print",[37,68,70],{"class":69},"sP7_E","(",[37,72,74],{"class":73},"slqww","user_name",[37,76,77],{"class":69},")\n",[14,79,80],{},"This error happens when Python sees a name that has not been created yet, is misspelled, or is outside the current scope.",[23,82,84],{"id":83},"what-this-error-means","What this error means",[14,86,87,90],{},[18,88,89],{},"NameError"," means Python found a variable, function, or module name it does not know.",[14,92,93,94,98],{},"In Python, a ",[95,96,97],"strong",{},"name"," can be:",[100,101,102,106,109,112],"ul",{},[103,104,105],"li",{},"A variable",[103,107,108],{},"A function",[103,110,111],{},"A module",[103,113,114],{},"Another object you assigned to a label",[14,116,117,118,120],{},"A name must exist before you use it. Python looks for that name in the current scope. If it cannot find it, it raises a ",[18,119,89],{},".",[14,122,123],{},"For example:",[28,125,127],{"className":30,"code":126,"language":32,"meta":33,"style":33},"print(total)\n",[18,128,129],{"__ignoreMap":33},[37,130,131,133,135,138],{"class":39,"line":40},[37,132,66],{"class":65},[37,134,70],{"class":69},[37,136,137],{"class":73},"total",[37,139,77],{"class":69},[14,141,142],{},"Output:",[28,144,146],{"className":30,"code":145,"language":32,"meta":33,"style":33},"NameError: name 'total' is not defined\n",[18,147,148],{"__ignoreMap":33},[37,149,150,153,156,159,162,164,166,169,172],{"class":39,"line":40},[37,151,89],{"class":152},"sZMiF",[37,154,155],{"class":69},":",[37,157,158],{"class":43}," name ",[37,160,161],{"class":51},"'",[37,163,137],{"class":55},[37,165,161],{"class":51},[37,167,168],{"class":47}," is",[37,170,171],{"class":47}," not",[37,173,174],{"class":43}," defined\n",[14,176,177,178,180],{},"Python stops because ",[18,179,137],{}," does not exist yet.",[23,182,184],{"id":183},"common-example-that-causes-the-error","Common example that causes the error",[14,186,187],{},"One of the most common causes is using a variable before assigning a value to it.",[28,189,191],{"className":30,"code":190,"language":32,"meta":33,"style":33},"print(total)\ntotal = 10\n",[18,192,193,203],{"__ignoreMap":33},[37,194,195,197,199,201],{"class":39,"line":40},[37,196,66],{"class":65},[37,198,70],{"class":69},[37,200,137],{"class":73},[37,202,77],{"class":69},[37,204,205,208,210],{"class":39,"line":62},[37,206,207],{"class":43},"total ",[37,209,48],{"class":47},[37,211,213],{"class":212},"srdBf"," 10\n",[14,215,142],{},[28,217,218],{"className":30,"code":145,"language":32,"meta":33,"style":33},[18,219,220],{"__ignoreMap":33},[37,221,222,224,226,228,230,232,234,236,238],{"class":39,"line":40},[37,223,89],{"class":152},[37,225,155],{"class":69},[37,227,158],{"class":43},[37,229,161],{"class":51},[37,231,137],{"class":55},[37,233,161],{"class":51},[37,235,168],{"class":47},[37,237,171],{"class":47},[37,239,174],{"class":43},[14,241,242,243,246,247,249],{},"This fails because Python runs code from top to bottom. When it reaches ",[18,244,245],{},"print(total)",", ",[18,248,137],{}," has not been created yet.",[14,251,252],{},"To fix it, define the variable first:",[28,254,256],{"className":30,"code":255,"language":32,"meta":33,"style":33},"total = 10\nprint(total)\n",[18,257,258,266],{"__ignoreMap":33},[37,259,260,262,264],{"class":39,"line":40},[37,261,207],{"class":43},[37,263,48],{"class":47},[37,265,213],{"class":212},[37,267,268,270,272,274],{"class":39,"line":62},[37,269,66],{"class":65},[37,271,70],{"class":69},[37,273,137],{"class":73},[37,275,77],{"class":69},[14,277,142],{},[28,279,281],{"className":30,"code":280,"language":32,"meta":33,"style":33},"10\n",[18,282,283],{"__ignoreMap":33},[37,284,285],{"class":39,"line":40},[37,286,280],{"class":212},[14,288,289,290,120],{},"If you are new to variables, see ",[291,292,294],"a",{"href":293},"\u002Flearn\u002Fpython-variables-explained-for-beginners\u002F","Python variables explained for beginners",[23,296,298],{"id":297},"cause-1-misspelled-variable-or-function-name","Cause 1: Misspelled variable or function name",[14,300,301,302,120],{},"A small typo can cause ",[18,303,89],{},[14,305,306],{},"Check these carefully:",[100,308,309,312,315,318],{},[103,310,311],{},"Uppercase and lowercase letters",[103,313,314],{},"Missing letters",[103,316,317],{},"Extra letters",[103,319,320],{},"Different naming styles",[14,322,323,324,327,328,331],{},"For example, ",[18,325,326],{},"userName"," and ",[18,329,330],{},"username"," are different names in Python.",[28,333,335],{"className":30,"code":334,"language":32,"meta":33,"style":33},"username = \"Sam\"\nprint(userName)\n",[18,336,337,350],{"__ignoreMap":33},[37,338,339,342,344,346,348],{"class":39,"line":40},[37,340,341],{"class":43},"username ",[37,343,48],{"class":47},[37,345,52],{"class":51},[37,347,56],{"class":55},[37,349,59],{"class":51},[37,351,352,354,356,358],{"class":39,"line":62},[37,353,66],{"class":65},[37,355,70],{"class":69},[37,357,326],{"class":73},[37,359,77],{"class":69},[14,361,142],{},[28,363,365],{"className":30,"code":364,"language":32,"meta":33,"style":33},"NameError: name 'userName' is not defined\n",[18,366,367],{"__ignoreMap":33},[37,368,369,371,373,375,377,379,381,383,385],{"class":39,"line":40},[37,370,89],{"class":152},[37,372,155],{"class":69},[37,374,158],{"class":43},[37,376,161],{"class":51},[37,378,326],{"class":55},[37,380,161],{"class":51},[37,382,168],{"class":47},[37,384,171],{"class":47},[37,386,174],{"class":43},[14,388,389],{},"Fix:",[28,391,393],{"className":30,"code":392,"language":32,"meta":33,"style":33},"username = \"Sam\"\nprint(username)\n",[18,394,395,407],{"__ignoreMap":33},[37,396,397,399,401,403,405],{"class":39,"line":40},[37,398,341],{"class":43},[37,400,48],{"class":47},[37,402,52],{"class":51},[37,404,56],{"class":55},[37,406,59],{"class":51},[37,408,409,411,413,415],{"class":39,"line":62},[37,410,66],{"class":65},[37,412,70],{"class":69},[37,414,330],{"class":73},[37,416,77],{"class":69},[14,418,419],{},"Make sure the name is spelled the same everywhere in your code.",[23,421,423],{"id":422},"cause-2-variable-created-after-use","Cause 2: Variable created after use",[14,425,426],{},"Assignment must happen before the name is used.",[14,428,429],{},"Wrong:",[28,431,433],{"className":30,"code":432,"language":32,"meta":33,"style":33},"print(score)\nscore = 95\n",[18,434,435,446],{"__ignoreMap":33},[37,436,437,439,441,444],{"class":39,"line":40},[37,438,66],{"class":65},[37,440,70],{"class":69},[37,442,443],{"class":73},"score",[37,445,77],{"class":69},[37,447,448,451,453],{"class":39,"line":62},[37,449,450],{"class":43},"score ",[37,452,48],{"class":47},[37,454,455],{"class":212}," 95\n",[14,457,458],{},"Right:",[28,460,462],{"className":30,"code":461,"language":32,"meta":33,"style":33},"score = 95\nprint(score)\n",[18,463,464,472],{"__ignoreMap":33},[37,465,466,468,470],{"class":39,"line":40},[37,467,450],{"class":43},[37,469,48],{"class":47},[37,471,455],{"class":212},[37,473,474,476,478,480],{"class":39,"line":62},[37,475,66],{"class":65},[37,477,70],{"class":69},[37,479,443],{"class":73},[37,481,77],{"class":69},[14,483,484,485,487],{},"When debugging, read your code from top to bottom. Python does not look ahead to see that ",[18,486,443],{}," will be created later.",[14,489,490],{},"This is a very common beginner issue when learning how variables work.",[23,492,494],{"id":493},"cause-3-name-is-outside-the-current-scope","Cause 3: Name is outside the current scope",[14,496,497],{},"A variable inside a function is usually not available outside it.",[28,499,501],{"className":30,"code":500,"language":32,"meta":33,"style":33},"def create_message():\n    message = \"Hello\"\n\ncreate_message()\nprint(message)\n",[18,502,503,516,530,537,546],{"__ignoreMap":33},[37,504,505,509,513],{"class":39,"line":40},[37,506,508],{"class":507},"sbsja","def",[37,510,512],{"class":511},"sGLFI"," create_message",[37,514,515],{"class":69},"():\n",[37,517,518,521,523,525,528],{"class":39,"line":62},[37,519,520],{"class":43},"    message ",[37,522,48],{"class":47},[37,524,52],{"class":51},[37,526,527],{"class":55},"Hello",[37,529,59],{"class":51},[37,531,533],{"class":39,"line":532},3,[37,534,536],{"emptyLinePlaceholder":535},true,"\n",[37,538,540,543],{"class":39,"line":539},4,[37,541,542],{"class":73},"create_message",[37,544,545],{"class":69},"()\n",[37,547,549,551,553,556],{"class":39,"line":548},5,[37,550,66],{"class":65},[37,552,70],{"class":69},[37,554,555],{"class":73},"message",[37,557,77],{"class":69},[14,559,142],{},[28,561,563],{"className":30,"code":562,"language":32,"meta":33,"style":33},"NameError: name 'message' is not defined\n",[18,564,565],{"__ignoreMap":33},[37,566,567,569,571,573,575,577,579,581,583],{"class":39,"line":40},[37,568,89],{"class":152},[37,570,155],{"class":69},[37,572,158],{"class":43},[37,574,161],{"class":51},[37,576,555],{"class":55},[37,578,161],{"class":51},[37,580,168],{"class":47},[37,582,171],{"class":47},[37,584,174],{"class":43},[14,586,587,588,590],{},"Here, ",[18,589,555],{}," only exists inside the function.",[14,592,593],{},"To fix this, return the value and store it in a variable:",[28,595,597],{"className":30,"code":596,"language":32,"meta":33,"style":33},"def create_message():\n    message = \"Hello\"\n    return message\n\nresult = create_message()\nprint(result)\n",[18,598,599,607,619,628,632,643],{"__ignoreMap":33},[37,600,601,603,605],{"class":39,"line":40},[37,602,508],{"class":507},[37,604,512],{"class":511},[37,606,515],{"class":69},[37,608,609,611,613,615,617],{"class":39,"line":62},[37,610,520],{"class":43},[37,612,48],{"class":47},[37,614,52],{"class":51},[37,616,527],{"class":55},[37,618,59],{"class":51},[37,620,621,625],{"class":39,"line":532},[37,622,624],{"class":623},"sVHd0","    return",[37,626,627],{"class":43}," message\n",[37,629,630],{"class":39,"line":539},[37,631,536],{"emptyLinePlaceholder":535},[37,633,634,637,639,641],{"class":39,"line":548},[37,635,636],{"class":43},"result ",[37,638,48],{"class":47},[37,640,512],{"class":73},[37,642,545],{"class":69},[37,644,646,648,650,653],{"class":39,"line":645},6,[37,647,66],{"class":65},[37,649,70],{"class":69},[37,651,652],{"class":73},"result",[37,654,77],{"class":69},[14,656,142],{},[28,658,660],{"className":30,"code":659,"language":32,"meta":33,"style":33},"Hello\n",[18,661,662],{"__ignoreMap":33},[37,663,664],{"class":39,"line":40},[37,665,659],{"class":43},[14,667,668],{},"A variable created in one function is also not automatically available in another function. In most cases, pass values as arguments or return them from functions.",[14,670,671,672,120],{},"If function scope is confusing, read ",[291,673,675],{"href":674},"\u002Flearn\u002Fpython-functions-explained\u002F","Python functions explained",[23,677,679],{"id":678},"cause-4-forgot-to-import-a-module-or-function","Cause 4: Forgot to import a module or function",[14,681,682,683,120],{},"If you use a module name before importing it, Python will raise a ",[18,684,89],{},[28,686,688],{"className":30,"code":687,"language":32,"meta":33,"style":33},"print(math.sqrt(16))\n",[18,689,690],{"__ignoreMap":33},[37,691,692,694,696,699,701,704,706,709],{"class":39,"line":40},[37,693,66],{"class":65},[37,695,70],{"class":69},[37,697,698],{"class":73},"math",[37,700,120],{"class":69},[37,702,703],{"class":73},"sqrt",[37,705,70],{"class":69},[37,707,708],{"class":212},"16",[37,710,711],{"class":69},"))\n",[14,713,142],{},[28,715,717],{"className":30,"code":716,"language":32,"meta":33,"style":33},"NameError: name 'math' is not defined\n",[18,718,719],{"__ignoreMap":33},[37,720,721,723,725,727,729,731,733,735,737],{"class":39,"line":40},[37,722,89],{"class":152},[37,724,155],{"class":69},[37,726,158],{"class":43},[37,728,161],{"class":51},[37,730,698],{"class":55},[37,732,161],{"class":51},[37,734,168],{"class":47},[37,736,171],{"class":47},[37,738,174],{"class":43},[14,740,389],{},[28,742,744],{"className":30,"code":743,"language":32,"meta":33,"style":33},"import math\n\nprint(math.sqrt(16))\n",[18,745,746,754,758],{"__ignoreMap":33},[37,747,748,751],{"class":39,"line":40},[37,749,750],{"class":623},"import",[37,752,753],{"class":43}," math\n",[37,755,756],{"class":39,"line":62},[37,757,536],{"emptyLinePlaceholder":535},[37,759,760,762,764,766,768,770,772,774],{"class":39,"line":532},[37,761,66],{"class":65},[37,763,70],{"class":69},[37,765,698],{"class":73},[37,767,120],{"class":69},[37,769,703],{"class":73},[37,771,70],{"class":69},[37,773,708],{"class":212},[37,775,711],{"class":69},[14,777,142],{},[28,779,781],{"className":30,"code":780,"language":32,"meta":33,"style":33},"4.0\n",[18,782,783],{"__ignoreMap":33},[37,784,785],{"class":39,"line":40},[37,786,780],{"class":212},[14,788,789],{},"You can also import a specific function:",[28,791,793],{"className":30,"code":792,"language":32,"meta":33,"style":33},"from math import sqrt\n\nprint(sqrt(16))\n",[18,794,795,808,812],{"__ignoreMap":33},[37,796,797,800,803,805],{"class":39,"line":40},[37,798,799],{"class":623},"from",[37,801,802],{"class":43}," math ",[37,804,750],{"class":623},[37,806,807],{"class":43}," sqrt\n",[37,809,810],{"class":39,"line":62},[37,811,536],{"emptyLinePlaceholder":535},[37,813,814,816,818,820,822,824],{"class":39,"line":532},[37,815,66],{"class":65},[37,817,70],{"class":69},[37,819,703],{"class":73},[37,821,70],{"class":69},[37,823,708],{"class":212},[37,825,711],{"class":69},[14,827,142],{},[28,829,830],{"className":30,"code":780,"language":32,"meta":33,"style":33},[18,831,832],{"__ignoreMap":33},[37,833,834],{"class":39,"line":40},[37,835,780],{"class":212},[14,837,838,839,120],{},"Check whether you imported the full module or a specific function. If you want to learn this properly, see ",[291,840,842],{"href":841},"\u002Flearn\u002Fhow-import-works-in-python\u002F","how import works in Python",[23,844,846],{"id":845},"cause-5-quotes-missing-around-a-string","Cause 5: Quotes missing around a string",[14,848,849],{},"This is a very common beginner mistake.",[14,851,852],{},"If you write this:",[28,854,856],{"className":30,"code":855,"language":32,"meta":33,"style":33},"print(hello)\n",[18,857,858],{"__ignoreMap":33},[37,859,860,862,864,867],{"class":39,"line":40},[37,861,66],{"class":65},[37,863,70],{"class":69},[37,865,866],{"class":73},"hello",[37,868,77],{"class":69},[14,870,871,872,874,875,877,878,120],{},"Python thinks ",[18,873,866],{}," is a variable name. Since no variable called ",[18,876,866],{}," exists, you get a ",[18,879,89],{},[14,881,142],{},[28,883,885],{"className":30,"code":884,"language":32,"meta":33,"style":33},"NameError: name 'hello' is not defined\n",[18,886,887],{"__ignoreMap":33},[37,888,889,891,893,895,897,899,901,903,905],{"class":39,"line":40},[37,890,89],{"class":152},[37,892,155],{"class":69},[37,894,158],{"class":43},[37,896,161],{"class":51},[37,898,866],{"class":55},[37,900,161],{"class":51},[37,902,168],{"class":47},[37,904,171],{"class":47},[37,906,174],{"class":43},[14,908,909],{},"To print text, use quotes:",[28,911,913],{"className":30,"code":912,"language":32,"meta":33,"style":33},"print(\"hello\")\n",[18,914,915],{"__ignoreMap":33},[37,916,917,919,921,924,926,928],{"class":39,"line":40},[37,918,66],{"class":65},[37,920,70],{"class":69},[37,922,923],{"class":51},"\"",[37,925,866],{"class":55},[37,927,923],{"class":51},[37,929,77],{"class":69},[14,931,142],{},[28,933,935],{"className":30,"code":934,"language":32,"meta":33,"style":33},"hello\n",[18,936,937],{"__ignoreMap":33},[37,938,939],{"class":39,"line":40},[37,940,934],{"class":43},[14,942,943],{},"Use either single quotes or double quotes:",[28,945,947],{"className":30,"code":946,"language":32,"meta":33,"style":33},"print('hello')\nprint(\"hello\")\n",[18,948,949,963],{"__ignoreMap":33},[37,950,951,953,955,957,959,961],{"class":39,"line":40},[37,952,66],{"class":65},[37,954,70],{"class":69},[37,956,161],{"class":51},[37,958,866],{"class":55},[37,960,161],{"class":51},[37,962,77],{"class":69},[37,964,965,967,969,971,973,975],{"class":39,"line":62},[37,966,66],{"class":65},[37,968,70],{"class":69},[37,970,923],{"class":51},[37,972,866],{"class":55},[37,974,923],{"class":51},[37,976,77],{"class":69},[14,978,979],{},"Both are valid.",[23,981,983],{"id":982},"how-to-fix-it-step-by-step","How to fix it step by step",[14,985,986,987,990],{},"When you see ",[18,988,989],{},"NameError: name 'something' is not defined",", use this checklist:",[992,993,994,997,1000,1003,1006],"ol",{},[103,995,996],{},"Find the exact name shown in the error message.",[103,998,999],{},"Check whether that name was created earlier in the code.",[103,1001,1002],{},"Check spelling and letter case.",[103,1004,1005],{},"Check whether the name is inside the correct function or block.",[103,1007,1008],{},"Check imports if the name should come from a module.",[14,1010,1011],{},"These simple debugging commands can also help:",[28,1013,1015],{"className":30,"code":1014,"language":32,"meta":33,"style":33},"print(variable_name)\nprint(locals())\nprint(globals())\ndir()\nhelp(name)\n",[18,1016,1017,1028,1040,1051,1058],{"__ignoreMap":33},[37,1018,1019,1021,1023,1026],{"class":39,"line":40},[37,1020,66],{"class":65},[37,1022,70],{"class":69},[37,1024,1025],{"class":73},"variable_name",[37,1027,77],{"class":69},[37,1029,1030,1032,1034,1037],{"class":39,"line":62},[37,1031,66],{"class":65},[37,1033,70],{"class":69},[37,1035,1036],{"class":65},"locals",[37,1038,1039],{"class":69},"())\n",[37,1041,1042,1044,1046,1049],{"class":39,"line":532},[37,1043,66],{"class":65},[37,1045,70],{"class":69},[37,1047,1048],{"class":65},"globals",[37,1050,1039],{"class":69},[37,1052,1053,1056],{"class":39,"line":539},[37,1054,1055],{"class":65},"dir",[37,1057,545],{"class":69},[37,1059,1060,1063,1065,1067],{"class":39,"line":548},[37,1061,1062],{"class":65},"help",[37,1064,70],{"class":69},[37,1066,97],{"class":73},[37,1068,77],{"class":69},[14,1070,1071],{},"Useful notes:",[100,1073,1074,1080,1086,1092,1098],{},[103,1075,1076,1079],{},[18,1077,1078],{},"print(variable_name)"," helps confirm a value exists",[103,1081,1082,1085],{},[18,1083,1084],{},"print(locals())"," shows names available in the current local scope",[103,1087,1088,1091],{},[18,1089,1090],{},"print(globals())"," shows global names",[103,1093,1094,1097],{},[18,1095,1096],{},"dir()"," shows available names in the current context",[103,1099,1100,1103],{},[18,1101,1102],{},"help(name)"," gives information about an object if it exists",[14,1105,1106,1107,1109,1110,1112,1113,120],{},"Be careful with ",[18,1108,1078],{},": if ",[18,1111,1025],{}," does not exist, that line will also raise a ",[18,1114,89],{},[23,1116,1118],{"id":1117},"how-this-differs-from-related-errors","How this differs from related errors",[14,1120,1121,1123],{},[18,1122,89],{}," is easy to confuse with other Python errors.",[1125,1126,89],"h3",{"id":1127},"nameerror",[14,1129,1130],{},"This means the name does not exist at all in the current scope.",[28,1132,1134],{"className":30,"code":1133,"language":32,"meta":33,"style":33},"print(color)\n",[18,1135,1136],{"__ignoreMap":33},[37,1137,1138,1140,1142,1145],{"class":39,"line":40},[37,1139,66],{"class":65},[37,1141,70],{"class":69},[37,1143,1144],{"class":73},"color",[37,1146,77],{"class":69},[14,1148,1149,1150,1152,1153,120],{},"If ",[18,1151,1144],{}," was never defined, Python raises ",[18,1154,89],{},[1125,1156,1158],{"id":1157},"unboundlocalerror","UnboundLocalError",[14,1160,1161],{},"This is a related scope problem that often happens inside functions.",[28,1163,1165],{"className":30,"code":1164,"language":32,"meta":33,"style":33},"count = 10\n\ndef show_count():\n    print(count)\n    count = 20\n\nshow_count()\n",[18,1166,1167,1176,1180,1189,1201,1211,1215],{"__ignoreMap":33},[37,1168,1169,1172,1174],{"class":39,"line":40},[37,1170,1171],{"class":43},"count ",[37,1173,48],{"class":47},[37,1175,213],{"class":212},[37,1177,1178],{"class":39,"line":62},[37,1179,536],{"emptyLinePlaceholder":535},[37,1181,1182,1184,1187],{"class":39,"line":532},[37,1183,508],{"class":507},[37,1185,1186],{"class":511}," show_count",[37,1188,515],{"class":69},[37,1190,1191,1194,1196,1199],{"class":39,"line":539},[37,1192,1193],{"class":65},"    print",[37,1195,70],{"class":69},[37,1197,1198],{"class":73},"count",[37,1200,77],{"class":69},[37,1202,1203,1206,1208],{"class":39,"line":548},[37,1204,1205],{"class":43},"    count ",[37,1207,48],{"class":47},[37,1209,1210],{"class":212}," 20\n",[37,1212,1213],{"class":39,"line":645},[37,1214,536],{"emptyLinePlaceholder":535},[37,1216,1218,1221],{"class":39,"line":1217},7,[37,1219,1220],{"class":73},"show_count",[37,1222,545],{"class":69},[14,1224,1225,1226,1228,1229,1231],{},"This raises ",[18,1227,1158],{}," because Python treats ",[18,1230,1198],{}," as a local variable inside the function, but it is used before assignment there.",[14,1233,1234,1235,120],{},"For a full explanation, see ",[291,1236,1238],{"href":1237},"\u002Ferrors\u002Funboundlocalerror-local-variable-referenced-before-assignment-fix","UnboundLocalError: local variable referenced before assignment",[1125,1240,1242],{"id":1241},"attributeerror","AttributeError",[14,1244,1245],{},"This happens when an object exists, but it does not have the attribute or method you asked for.",[28,1247,1249],{"className":30,"code":1248,"language":32,"meta":33,"style":33},"name = \"Sam\"\nprint(name.append(\"x\"))\n",[18,1250,1251,1264],{"__ignoreMap":33},[37,1252,1253,1256,1258,1260,1262],{"class":39,"line":40},[37,1254,1255],{"class":43},"name ",[37,1257,48],{"class":47},[37,1259,52],{"class":51},[37,1261,56],{"class":55},[37,1263,59],{"class":51},[37,1265,1266,1268,1270,1272,1274,1277,1279,1281,1284,1286],{"class":39,"line":62},[37,1267,66],{"class":65},[37,1269,70],{"class":69},[37,1271,97],{"class":73},[37,1273,120],{"class":69},[37,1275,1276],{"class":73},"append",[37,1278,70],{"class":69},[37,1280,923],{"class":51},[37,1282,1283],{"class":55},"x",[37,1285,923],{"class":51},[37,1287,711],{"class":69},[14,1289,1225,1290,1292,1293,1296],{},[18,1291,1242],{}," because strings exist, but they do not have an ",[18,1294,1295],{},"append()"," method.",[14,1298,1299,1300,120],{},"See ",[291,1301,1303],{"href":1302},"\u002Ferrors\u002Fattributeerror-object-has-no-attribute-fix\u002F","AttributeError: object has no attribute (Fix)",[23,1305,1307],{"id":1306},"common-mistakes","Common mistakes",[14,1309,1310],{},"These are the most common causes of this error:",[100,1312,1313,1316,1319,1322,1325,1328],{},[103,1314,1315],{},"Misspelled variable name",[103,1317,1318],{},"Using a variable before assignment",[103,1320,1321],{},"Case mismatch in a name",[103,1323,1324],{},"Using a local variable outside its function",[103,1326,1327],{},"Forgetting to import a module",[103,1329,1330],{},"Missing quotes around a string",[14,1332,1333,1334,120],{},"If your error looks similar but not identical, you may also want to read ",[291,1335,1337],{"href":1336},"\u002Ferrors\u002Fnameerror-in-python-causes-and-fixes\u002F","NameError in Python: causes and fixes",[23,1339,1341],{"id":1340},"faq","FAQ",[1125,1343,1345],{"id":1344},"what-does-nameerror-mean-in-python","What does NameError mean in Python?",[14,1347,1348],{},"It means Python cannot find the variable, function, or module name you used.",[1125,1350,1352],{"id":1351},"why-do-i-get-nameerror-for-a-word-i-wanted-to-print","Why do I get NameError for a word I wanted to print?",[14,1354,1355],{},"You probably forgot quotes, so Python treated the word as a variable name instead of a string.",[1125,1357,1359],{"id":1358},"is-nameerror-the-same-as-unboundlocalerror","Is NameError the same as UnboundLocalError?",[14,1361,1362,1363,1365],{},"No. ",[18,1364,1158],{}," is a more specific scope-related error that often happens inside functions.",[1125,1367,1369],{"id":1368},"can-a-typo-cause-nameerror","Can a typo cause NameError?",[14,1371,1372],{},"Yes. Even a small spelling difference or wrong capitalization can cause it.",[23,1374,1376],{"id":1375},"see-also","See also",[100,1378,1379,1383,1387,1392,1396,1400],{},[103,1380,1381],{},[291,1382,294],{"href":293},[103,1384,1385],{},[291,1386,675],{"href":674},[103,1388,1389],{},[291,1390,1391],{"href":841},"How import works in Python",[103,1393,1394],{},[291,1395,1337],{"href":1336},[103,1397,1398],{},[291,1399,1238],{"href":1237},[103,1401,1402],{},[291,1403,1303],{"href":1302},[1405,1406,1407],"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 .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 .sptTA, html code.shiki .sptTA{--shiki-light:#6182B8;--shiki-default:#005CC5;--shiki-dark:#79B8FF}html pre.shiki code .sP7_E, html code.shiki .sP7_E{--shiki-light:#39ADB5;--shiki-default:#24292E;--shiki-dark:#E1E4E8}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 .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 .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}",{"title":33,"searchDepth":62,"depth":62,"links":1409},[1410,1411,1412,1413,1414,1415,1416,1417,1418,1419,1424,1425,1431],{"id":25,"depth":62,"text":26},{"id":83,"depth":62,"text":84},{"id":183,"depth":62,"text":184},{"id":297,"depth":62,"text":298},{"id":422,"depth":62,"text":423},{"id":493,"depth":62,"text":494},{"id":678,"depth":62,"text":679},{"id":845,"depth":62,"text":846},{"id":982,"depth":62,"text":983},{"id":1117,"depth":62,"text":1118,"children":1420},[1421,1422,1423],{"id":1127,"depth":532,"text":89},{"id":1157,"depth":532,"text":1158},{"id":1241,"depth":532,"text":1242},{"id":1306,"depth":62,"text":1307},{"id":1340,"depth":62,"text":1341,"children":1426},[1427,1428,1429,1430],{"id":1344,"depth":532,"text":1345},{"id":1351,"depth":532,"text":1352},{"id":1358,"depth":532,"text":1359},{"id":1368,"depth":532,"text":1369},{"id":1375,"depth":62,"text":1376},"Master nameerror name is not defined fix in our comprehensive Python beginner guide.","md",{},"\u002Ferrors\u002Fnameerror-name-is-not-defined-fix",{"title":5,"description":1432},"errors\u002Fnameerror-name-is-not-defined-fix","wPiPu8-D6ajimIBbQr9322DpmxzKkTyNMGF7C9pAUN8",1777585501084]