[{"data":1,"prerenderedAt":123805},["ShallowReactive",2],{"doc-\u002Ferrors\u002Fstopiteration-exception-in-python-explained":3,"nav-errors":1629},{"id":4,"title":5,"body":6,"description":1622,"extension":1623,"meta":1624,"navigation":138,"path":1625,"seo":1626,"stem":1627,"__hash__":1628},"content\u002Ferrors\u002Fstopiteration-exception-in-python-explained.md","StopIteration Exception in Python Explained",{"type":7,"value":8,"toc":1592},"minimark",[9,13,26,36,39,66,76,81,90,216,219,239,255,259,264,267,278,291,299,303,309,336,339,416,419,423,426,495,497,534,537,565,569,578,581,634,636,649,655,664,670,674,684,690,791,793,811,817,823,905,907,925,934,940,947,956,1006,1008,1020,1023,1030,1034,1040,1043,1054,1064,1068,1102,1105,1109,1133,1136,1155,1158,1165,1169,1175,1204,1207,1283,1286,1316,1319,1338,1341,1345,1348,1373,1377,1380,1481,1483,1508,1511,1515,1519,1522,1526,1529,1533,1541,1545,1551,1555,1588],[10,11,5],"h1",{"id":12},"stopiteration-exception-in-python-explained",[14,15,16,20,21,25],"p",{},[17,18,19],"code",{},"StopIteration"," happens when an ",[22,23,24],"strong",{},"iterator"," has no more values to return.",[14,27,28,29,32,33,35],{},"This does ",[22,30,31],{},"not always mean something is broken",". In Python, ",[17,34,19],{}," is a normal signal used to say, “there are no more items.”",[14,37,38],{},"Beginners usually see it when:",[40,41,42,50,57,60],"ul",{},[43,44,45,46,49],"li",{},"calling ",[17,47,48],{},"next()"," too many times",[43,51,52,53,56],{},"using a ",[17,54,55],{},"while True"," loop with an iterator",[43,58,59],{},"reusing an iterator that is already finished",[43,61,62,63,65],{},"raising ",[17,64,19],{}," manually inside a generator",[14,67,68,69,72,73,75],{},"If you use a ",[17,70,71],{},"for"," loop, Python usually handles this for you automatically. The exception becomes visible when you call ",[17,74,48],{}," yourself and do not handle the end of the iterator.",[77,78,80],"h2",{"id":79},"quick-fix","Quick fix",[14,82,83,84,86,87,89],{},"If you are calling ",[17,85,48],{}," manually, catch ",[17,88,19],{},":",[91,92,97],"pre",{"className":93,"code":94,"language":95,"meta":96,"style":96},"language-python shiki shiki-themes material-theme-lighter github-light github-dark","numbers = iter([1, 2])\n\ntry:\n    while True:\n        print(next(numbers))\nexcept StopIteration:\n    print(\"No more items\")\n","python","",[17,98,99,133,140,150,162,183,195],{"__ignoreMap":96},[100,101,104,108,112,116,120,124,127,130],"span",{"class":102,"line":103},"line",1,[100,105,107],{"class":106},"su5hD","numbers ",[100,109,111],{"class":110},"smGrS","=",[100,113,115],{"class":114},"sptTA"," iter",[100,117,119],{"class":118},"sP7_E","([",[100,121,123],{"class":122},"srdBf","1",[100,125,126],{"class":118},",",[100,128,129],{"class":122}," 2",[100,131,132],{"class":118},"])\n",[100,134,136],{"class":102,"line":135},2,[100,137,139],{"emptyLinePlaceholder":138},true,"\n",[100,141,143,147],{"class":102,"line":142},3,[100,144,146],{"class":145},"sVHd0","try",[100,148,149],{"class":118},":\n",[100,151,153,156,160],{"class":102,"line":152},4,[100,154,155],{"class":145},"    while",[100,157,159],{"class":158},"s39Yj"," True",[100,161,149],{"class":118},[100,163,165,168,171,174,176,180],{"class":102,"line":164},5,[100,166,167],{"class":114},"        print",[100,169,170],{"class":118},"(",[100,172,173],{"class":114},"next",[100,175,170],{"class":118},[100,177,179],{"class":178},"slqww","numbers",[100,181,182],{"class":118},"))\n",[100,184,186,189,193],{"class":102,"line":185},6,[100,187,188],{"class":145},"except",[100,190,192],{"class":191},"sZMiF"," StopIteration",[100,194,149],{"class":118},[100,196,198,201,203,207,211,213],{"class":102,"line":197},7,[100,199,200],{"class":114},"    print",[100,202,170],{"class":118},[100,204,206],{"class":205},"sjJ54","\"",[100,208,210],{"class":209},"s_sjI","No more items",[100,212,206],{"class":205},[100,214,215],{"class":118},")\n",[14,217,218],{},"Output:",[91,220,222],{"className":93,"code":221,"language":95,"meta":96,"style":96},"1\n2\nNo more items\n",[17,223,224,229,234],{"__ignoreMap":96},[100,225,226],{"class":102,"line":103},[100,227,228],{"class":122},"1\n",[100,230,231],{"class":102,"line":135},[100,232,233],{"class":122},"2\n",[100,235,236],{"class":102,"line":142},[100,237,238],{"class":106},"No more items\n",[14,240,241,242,245,246,248,249,251,252,254],{},"Use ",[17,243,244],{},"try-except"," when you call ",[17,247,48],{}," yourself. In a ",[17,250,71],{}," loop, Python handles ",[17,253,19],{}," automatically.",[77,256,258],{"id":257},"what-stopiteration-means","What StopIteration means",[14,260,261,263],{},[17,262,19],{}," is raised when an iterator has no more values to give.",[14,265,266],{},"Important points:",[40,268,269,272,275],{},[43,270,271],{},"It is part of normal Python iteration",[43,273,274],{},"It does not always mean there is a bug",[43,276,277],{},"It is how Python knows an iterator is finished",[14,279,280,281,284,285,287,288,290],{},"For example, a list can be turned into an iterator with ",[17,282,283],{},"iter()",". Each call to ",[17,286,48],{}," gets one item. When there are no items left, Python raises ",[17,289,19],{},".",[14,292,293,294,290],{},"If iterators are new to you, see ",[295,296,298],"a",{"href":297},"\u002Flearn\u002Fiterators-and-iterable-objects-explained","iterators and iterable objects explained",[77,300,302],{"id":301},"when-beginners-usually-see-this-error","When beginners usually see this error",[14,304,305,306,308],{},"You will usually run into ",[17,307,19],{}," in these situations:",[40,310,311,318,323,331],{},[43,312,313],{},[22,314,315,316,49],{},"Calling ",[17,317,48],{},[43,319,320],{},[22,321,322],{},"Reading past the end of a custom iterator",[43,324,325],{},[22,326,327,328,330],{},"Raising ",[17,329,19],{}," directly inside a generator",[43,332,333],{},[22,334,335],{},"Using iterator code without checking when it is finished",[14,337,338],{},"A very common example is this:",[91,340,342],{"className":93,"code":341,"language":95,"meta":96,"style":96},"numbers = iter([10, 20])\n\nprint(next(numbers))\nprint(next(numbers))\nprint(next(numbers))  # No more values left\n",[17,343,344,364,368,383,397],{"__ignoreMap":96},[100,345,346,348,350,352,354,357,359,362],{"class":102,"line":103},[100,347,107],{"class":106},[100,349,111],{"class":110},[100,351,115],{"class":114},[100,353,119],{"class":118},[100,355,356],{"class":122},"10",[100,358,126],{"class":118},[100,360,361],{"class":122}," 20",[100,363,132],{"class":118},[100,365,366],{"class":102,"line":135},[100,367,139],{"emptyLinePlaceholder":138},[100,369,370,373,375,377,379,381],{"class":102,"line":142},[100,371,372],{"class":114},"print",[100,374,170],{"class":118},[100,376,173],{"class":114},[100,378,170],{"class":118},[100,380,179],{"class":178},[100,382,182],{"class":118},[100,384,385,387,389,391,393,395],{"class":102,"line":152},[100,386,372],{"class":114},[100,388,170],{"class":118},[100,390,173],{"class":114},[100,392,170],{"class":118},[100,394,179],{"class":178},[100,396,182],{"class":118},[100,398,399,401,403,405,407,409,412],{"class":102,"line":164},[100,400,372],{"class":114},[100,402,170],{"class":118},[100,404,173],{"class":114},[100,406,170],{"class":118},[100,408,179],{"class":178},[100,410,411],{"class":118},"))",[100,413,415],{"class":414},"sutJx","  # No more values left\n",[14,417,418],{},"The third call fails because the iterator is already exhausted.",[77,420,422],{"id":421},"example-that-causes-stopiteration","Example that causes StopIteration",[14,424,425],{},"Here is a short example that raises the exception:",[91,427,429],{"className":93,"code":428,"language":95,"meta":96,"style":96},"numbers = iter([1, 2])\n\nprint(next(numbers))\nprint(next(numbers))\nprint(next(numbers))\n",[17,430,431,449,453,467,481],{"__ignoreMap":96},[100,432,433,435,437,439,441,443,445,447],{"class":102,"line":103},[100,434,107],{"class":106},[100,436,111],{"class":110},[100,438,115],{"class":114},[100,440,119],{"class":118},[100,442,123],{"class":122},[100,444,126],{"class":118},[100,446,129],{"class":122},[100,448,132],{"class":118},[100,450,451],{"class":102,"line":135},[100,452,139],{"emptyLinePlaceholder":138},[100,454,455,457,459,461,463,465],{"class":102,"line":142},[100,456,372],{"class":114},[100,458,170],{"class":118},[100,460,173],{"class":114},[100,462,170],{"class":118},[100,464,179],{"class":178},[100,466,182],{"class":118},[100,468,469,471,473,475,477,479],{"class":102,"line":152},[100,470,372],{"class":114},[100,472,170],{"class":118},[100,474,173],{"class":114},[100,476,170],{"class":118},[100,478,179],{"class":178},[100,480,182],{"class":118},[100,482,483,485,487,489,491,493],{"class":102,"line":164},[100,484,372],{"class":114},[100,486,170],{"class":118},[100,488,173],{"class":114},[100,490,170],{"class":118},[100,492,179],{"class":178},[100,494,182],{"class":118},[14,496,218],{},[91,498,500],{"className":93,"code":499,"language":95,"meta":96,"style":96},"1\n2\nTraceback (most recent call last):\n  ...\nStopIteration\n",[17,501,502,506,510,523,529],{"__ignoreMap":96},[100,503,504],{"class":102,"line":103},[100,505,228],{"class":122},[100,507,508],{"class":102,"line":135},[100,509,233],{"class":122},[100,511,512,515,517,520],{"class":102,"line":142},[100,513,514],{"class":178},"Traceback ",[100,516,170],{"class":118},[100,518,519],{"class":178},"most recent call last",[100,521,522],{"class":118},"):\n",[100,524,525],{"class":102,"line":152},[100,526,528],{"class":527},"s_hVV","  ...\n",[100,530,531],{"class":102,"line":164},[100,532,533],{"class":191},"StopIteration\n",[14,535,536],{},"What happens here:",[40,538,539,545,554,560],{},[43,540,541,544],{},[17,542,543],{},"iter([1, 2])"," creates an iterator",[43,546,547,548,551,552],{},"the first ",[17,549,550],{},"next(numbers)"," returns ",[17,553,123],{},[43,555,556,557],{},"the second returns ",[17,558,559],{},"2",[43,561,562,563],{},"the third has nothing left to return, so Python raises ",[17,564,19],{},[77,566,568],{"id":567},"why-for-loops-usually-do-not-crash","Why for loops usually do not crash",[14,570,571,572,574,575,577],{},"A ",[17,573,71],{}," loop uses ",[17,576,48],{}," behind the scenes.",[14,579,580],{},"For example:",[91,582,584],{"className":93,"code":583,"language":95,"meta":96,"style":96},"numbers = [1, 2]\n\nfor number in numbers:\n    print(number)\n",[17,585,586,604,608,623],{"__ignoreMap":96},[100,587,588,590,592,595,597,599,601],{"class":102,"line":103},[100,589,107],{"class":106},[100,591,111],{"class":110},[100,593,594],{"class":118}," [",[100,596,123],{"class":122},[100,598,126],{"class":118},[100,600,129],{"class":122},[100,602,603],{"class":118},"]\n",[100,605,606],{"class":102,"line":135},[100,607,139],{"emptyLinePlaceholder":138},[100,609,610,612,615,618,621],{"class":102,"line":142},[100,611,71],{"class":145},[100,613,614],{"class":106}," number ",[100,616,617],{"class":145},"in",[100,619,620],{"class":106}," numbers",[100,622,149],{"class":118},[100,624,625,627,629,632],{"class":102,"line":152},[100,626,200],{"class":114},[100,628,170],{"class":118},[100,630,631],{"class":178},"number",[100,633,215],{"class":118},[14,635,218],{},[91,637,639],{"className":93,"code":638,"language":95,"meta":96,"style":96},"1\n2\n",[17,640,641,645],{"__ignoreMap":96},[100,642,643],{"class":102,"line":103},[100,644,228],{"class":122},[100,646,647],{"class":102,"line":135},[100,648,233],{"class":122},[14,650,651,652,654],{},"This loop stops cleanly because Python catches ",[17,653,19],{}," internally.",[14,656,657,658,660,661,663],{},"That is why normal ",[17,659,71],{}," loops do not usually show this error. The loop keeps calling ",[17,662,48],{}," until the iterator is finished, then it stops.",[14,665,666,667,669],{},"This is one reason ",[17,668,71],{}," loops are often safer than manual iterator code.",[77,671,673],{"id":672},"how-to-fix-it-when-using-next","How to fix it when using next()",[675,676,678,679,681,682],"h3",{"id":677},"wrap-next-in-try-except","Wrap ",[17,680,48],{}," in ",[17,683,244],{},[14,685,686,687,689],{},"If you want to keep calling ",[17,688,48],{}," yourself, catch the exception:",[91,691,693],{"className":93,"code":692,"language":95,"meta":96,"style":96},"numbers = iter([1, 2])\n\nwhile True:\n    try:\n        value = next(numbers)\n        print(value)\n    except StopIteration:\n        print(\"Finished\")\n        break\n",[17,694,695,713,717,726,733,749,760,769,785],{"__ignoreMap":96},[100,696,697,699,701,703,705,707,709,711],{"class":102,"line":103},[100,698,107],{"class":106},[100,700,111],{"class":110},[100,702,115],{"class":114},[100,704,119],{"class":118},[100,706,123],{"class":122},[100,708,126],{"class":118},[100,710,129],{"class":122},[100,712,132],{"class":118},[100,714,715],{"class":102,"line":135},[100,716,139],{"emptyLinePlaceholder":138},[100,718,719,722,724],{"class":102,"line":142},[100,720,721],{"class":145},"while",[100,723,159],{"class":158},[100,725,149],{"class":118},[100,727,728,731],{"class":102,"line":152},[100,729,730],{"class":145},"    try",[100,732,149],{"class":118},[100,734,735,738,740,743,745,747],{"class":102,"line":164},[100,736,737],{"class":106},"        value ",[100,739,111],{"class":110},[100,741,742],{"class":114}," next",[100,744,170],{"class":118},[100,746,179],{"class":178},[100,748,215],{"class":118},[100,750,751,753,755,758],{"class":102,"line":185},[100,752,167],{"class":114},[100,754,170],{"class":118},[100,756,757],{"class":178},"value",[100,759,215],{"class":118},[100,761,762,765,767],{"class":102,"line":197},[100,763,764],{"class":145},"    except",[100,766,192],{"class":191},[100,768,149],{"class":118},[100,770,772,774,776,778,781,783],{"class":102,"line":771},8,[100,773,167],{"class":114},[100,775,170],{"class":118},[100,777,206],{"class":205},[100,779,780],{"class":209},"Finished",[100,782,206],{"class":205},[100,784,215],{"class":118},[100,786,788],{"class":102,"line":787},9,[100,789,790],{"class":145},"        break\n",[14,792,218],{},[91,794,796],{"className":93,"code":795,"language":95,"meta":96,"style":96},"1\n2\nFinished\n",[17,797,798,802,806],{"__ignoreMap":96},[100,799,800],{"class":102,"line":103},[100,801,228],{"class":122},[100,803,804],{"class":102,"line":135},[100,805,233],{"class":122},[100,807,808],{"class":102,"line":142},[100,809,810],{"class":106},"Finished\n",[675,812,814,815],{"id":813},"use-a-default-value-with-next","Use a default value with ",[17,816,48],{},[14,818,819,820,822],{},"You can avoid the exception by giving ",[17,821,48],{}," a default value:",[91,824,826],{"className":93,"code":825,"language":95,"meta":96,"style":96},"numbers = iter([1, 2])\n\nprint(next(numbers, None))\nprint(next(numbers, None))\nprint(next(numbers, None))\n",[17,827,828,846,850,869,887],{"__ignoreMap":96},[100,829,830,832,834,836,838,840,842,844],{"class":102,"line":103},[100,831,107],{"class":106},[100,833,111],{"class":110},[100,835,115],{"class":114},[100,837,119],{"class":118},[100,839,123],{"class":122},[100,841,126],{"class":118},[100,843,129],{"class":122},[100,845,132],{"class":118},[100,847,848],{"class":102,"line":135},[100,849,139],{"emptyLinePlaceholder":138},[100,851,852,854,856,858,860,862,864,867],{"class":102,"line":142},[100,853,372],{"class":114},[100,855,170],{"class":118},[100,857,173],{"class":114},[100,859,170],{"class":118},[100,861,179],{"class":178},[100,863,126],{"class":118},[100,865,866],{"class":158}," None",[100,868,182],{"class":118},[100,870,871,873,875,877,879,881,883,885],{"class":102,"line":152},[100,872,372],{"class":114},[100,874,170],{"class":118},[100,876,173],{"class":114},[100,878,170],{"class":118},[100,880,179],{"class":178},[100,882,126],{"class":118},[100,884,866],{"class":158},[100,886,182],{"class":118},[100,888,889,891,893,895,897,899,901,903],{"class":102,"line":164},[100,890,372],{"class":114},[100,892,170],{"class":118},[100,894,173],{"class":114},[100,896,170],{"class":118},[100,898,179],{"class":178},[100,900,126],{"class":118},[100,902,866],{"class":158},[100,904,182],{"class":118},[14,906,218],{},[91,908,910],{"className":93,"code":909,"language":95,"meta":96,"style":96},"1\n2\nNone\n",[17,911,912,916,920],{"__ignoreMap":96},[100,913,914],{"class":102,"line":103},[100,915,228],{"class":122},[100,917,918],{"class":102,"line":135},[100,919,233],{"class":122},[100,921,922],{"class":102,"line":142},[100,923,924],{"class":158},"None\n",[14,926,927,928,931,932,290],{},"The third call returns ",[17,929,930],{},"None"," instead of raising ",[17,933,19],{},[14,935,936,937,939],{},"This is often the simplest fix when ",[17,938,930],{}," is an acceptable “no more data” value.",[675,941,943,944,946],{"id":942},"stop-calling-next-when-the-iterator-is-finished","Stop calling ",[17,945,48],{}," when the iterator is finished",[14,948,949,950,952,953,955],{},"In many cases, the best fix is to use a ",[17,951,71],{}," loop instead of manual ",[17,954,48],{}," calls:",[91,957,959],{"className":93,"code":958,"language":95,"meta":96,"style":96},"numbers = iter([1, 2])\n\nfor value in numbers:\n    print(value)\n",[17,960,961,979,983,996],{"__ignoreMap":96},[100,962,963,965,967,969,971,973,975,977],{"class":102,"line":103},[100,964,107],{"class":106},[100,966,111],{"class":110},[100,968,115],{"class":114},[100,970,119],{"class":118},[100,972,123],{"class":122},[100,974,126],{"class":118},[100,976,129],{"class":122},[100,978,132],{"class":118},[100,980,981],{"class":102,"line":135},[100,982,139],{"emptyLinePlaceholder":138},[100,984,985,987,990,992,994],{"class":102,"line":142},[100,986,71],{"class":145},[100,988,989],{"class":106}," value ",[100,991,617],{"class":145},[100,993,620],{"class":106},[100,995,149],{"class":118},[100,997,998,1000,1002,1004],{"class":102,"line":152},[100,999,200],{"class":114},[100,1001,170],{"class":118},[100,1003,757],{"class":178},[100,1005,215],{"class":118},[14,1007,218],{},[91,1009,1010],{"className":93,"code":638,"language":95,"meta":96,"style":96},[17,1011,1012,1016],{"__ignoreMap":96},[100,1013,1014],{"class":102,"line":103},[100,1015,228],{"class":122},[100,1017,1018],{"class":102,"line":135},[100,1019,233],{"class":122},[14,1021,1022],{},"If your goal is just to loop through values, this is the easiest approach.",[14,1024,1025,1026,290],{},"For more general exception handling, see ",[295,1027,1029],{"href":1028},"\u002Fhow-to\u002Fhow-to-handle-exceptions-in-python","how to handle exceptions in Python",[77,1031,1033],{"id":1032},"generators-and-stopiteration","Generators and StopIteration",[14,1035,571,1036,1039],{},[22,1037,1038],{},"generator"," is a function that yields values one at a time.",[14,1041,1042],{},"Generators end in a normal way when:",[40,1044,1045,1048],{},[43,1046,1047],{},"they reach the end of the function",[43,1049,1050,1051],{},"they use ",[17,1052,1053],{},"return",[14,1055,1056,1057,1060,1061,1063],{},"Beginners should ",[22,1058,1059],{},"not"," manually raise ",[17,1062,19],{}," inside a generator.",[675,1065,1067],{"id":1066},"bad-example","Bad example",[91,1069,1071],{"className":93,"code":1070,"language":95,"meta":96,"style":96},"def count():\n    yield 1\n    raise StopIteration\n",[17,1072,1073,1086,1094],{"__ignoreMap":96},[100,1074,1075,1079,1083],{"class":102,"line":103},[100,1076,1078],{"class":1077},"sbsja","def",[100,1080,1082],{"class":1081},"sGLFI"," count",[100,1084,1085],{"class":118},"():\n",[100,1087,1088,1091],{"class":102,"line":135},[100,1089,1090],{"class":145},"    yield",[100,1092,1093],{"class":122}," 1\n",[100,1095,1096,1099],{"class":102,"line":142},[100,1097,1098],{"class":145},"    raise",[100,1100,1101],{"class":191}," StopIteration\n",[14,1103,1104],{},"In modern Python, this is not the right way to end a generator.",[675,1106,1108],{"id":1107},"good-example","Good example",[91,1110,1112],{"className":93,"code":1111,"language":95,"meta":96,"style":96},"def count():\n    yield 1\n    return\n",[17,1113,1114,1122,1128],{"__ignoreMap":96},[100,1115,1116,1118,1120],{"class":102,"line":103},[100,1117,1078],{"class":1077},[100,1119,1082],{"class":1081},[100,1121,1085],{"class":118},[100,1123,1124,1126],{"class":102,"line":135},[100,1125,1090],{"class":145},[100,1127,1093],{"class":122},[100,1129,1130],{"class":102,"line":142},[100,1131,1132],{"class":145},"    return\n",[14,1134,1135],{},"Or even more simply:",[91,1137,1139],{"className":93,"code":1138,"language":95,"meta":96,"style":96},"def count():\n    yield 1\n",[17,1140,1141,1149],{"__ignoreMap":96},[100,1142,1143,1145,1147],{"class":102,"line":103},[100,1144,1078],{"class":1077},[100,1146,1082],{"class":1081},[100,1148,1085],{"class":118},[100,1150,1151,1153],{"class":102,"line":135},[100,1152,1090],{"class":145},[100,1154,1093],{"class":122},[14,1156,1157],{},"When the generator finishes, Python handles the end correctly.",[14,1159,1160,1161,290],{},"If you want to learn more, see ",[295,1162,1164],{"href":1163},"\u002Flearn\u002Fgenerators-in-python-explained","generators in Python explained",[77,1166,1168],{"id":1167},"debugging-checklist","Debugging checklist",[14,1170,1171,1172,1174],{},"If you see ",[17,1173,19],{},", check these things:",[40,1176,1177,1183,1186,1189,1198],{},[43,1178,1179,1180,1182],{},"Check where ",[17,1181,48],{}," is called",[43,1184,1185],{},"Check how many values the iterator contains",[43,1187,1188],{},"Print values before the failing call",[43,1190,1191,1192,1194,1195,1197],{},"See if a ",[17,1193,71],{}," loop can replace manual ",[17,1196,48],{}," calls",[43,1199,1200,1201],{},"If using a generator, remove manual ",[17,1202,1203],{},"raise StopIteration",[14,1205,1206],{},"Useful debugging lines:",[91,1208,1210],{"className":93,"code":1209,"language":95,"meta":96,"style":96},"print(next(my_iter))\nprint(list(my_iter))\nprint(type(my_iter))\nprint(hasattr(my_iter, '__next__'))\n",[17,1211,1212,1227,1242,1257],{"__ignoreMap":96},[100,1213,1214,1216,1218,1220,1222,1225],{"class":102,"line":103},[100,1215,372],{"class":114},[100,1217,170],{"class":118},[100,1219,173],{"class":114},[100,1221,170],{"class":118},[100,1223,1224],{"class":178},"my_iter",[100,1226,182],{"class":118},[100,1228,1229,1231,1233,1236,1238,1240],{"class":102,"line":135},[100,1230,372],{"class":114},[100,1232,170],{"class":118},[100,1234,1235],{"class":191},"list",[100,1237,170],{"class":118},[100,1239,1224],{"class":178},[100,1241,182],{"class":118},[100,1243,1244,1246,1248,1251,1253,1255],{"class":102,"line":142},[100,1245,372],{"class":114},[100,1247,170],{"class":118},[100,1249,1250],{"class":191},"type",[100,1252,170],{"class":118},[100,1254,1224],{"class":178},[100,1256,182],{"class":118},[100,1258,1259,1261,1263,1266,1268,1270,1272,1275,1278,1281],{"class":102,"line":152},[100,1260,372],{"class":114},[100,1262,170],{"class":118},[100,1264,1265],{"class":114},"hasattr",[100,1267,170],{"class":118},[100,1269,1224],{"class":178},[100,1271,126],{"class":118},[100,1273,1274],{"class":205}," '",[100,1276,1277],{"class":209},"__next__",[100,1279,1280],{"class":205},"'",[100,1282,182],{"class":118},[14,1284,1285],{},"What these can help you find:",[40,1287,1288,1294,1300,1310],{},[43,1289,1290,1293],{},[17,1291,1292],{},"print(type(my_iter))"," shows what kind of object you are working with",[43,1295,1296,1299],{},[17,1297,1298],{},"print(hasattr(my_iter, '__next__'))"," checks if it behaves like an iterator",[43,1301,1302,1305,1306,1309],{},[17,1303,1304],{},"print(list(my_iter))"," shows remaining values, but it also ",[22,1307,1308],{},"consumes"," the iterator",[43,1311,1312,1315],{},[17,1313,1314],{},"print(next(my_iter))"," helps test one step at a time",[14,1317,1318],{},"Be careful with this line:",[91,1320,1322],{"className":93,"code":1321,"language":95,"meta":96,"style":96},"print(list(my_iter))\n",[17,1323,1324],{"__ignoreMap":96},[100,1325,1326,1328,1330,1332,1334,1336],{"class":102,"line":103},[100,1327,372],{"class":114},[100,1329,170],{"class":118},[100,1331,1235],{"class":191},[100,1333,170],{"class":118},[100,1335,1224],{"class":178},[100,1337,182],{"class":118},[14,1339,1340],{},"It uses up the remaining items. After that, the iterator may already be exhausted.",[77,1342,1344],{"id":1343},"common-causes","Common causes",[14,1346,1347],{},"These are the most common reasons this exception appears:",[40,1349,1350,1355,1364,1370],{},[43,1351,315,1352,1354],{},[17,1353,48],{}," after the iterator is exhausted",[43,1356,1357,1358,1360,1361,1363],{},"Using ",[17,1359,48],{}," in a ",[17,1362,55],{}," loop without handling the end",[43,1365,1366,1367,1369],{},"Manually raising ",[17,1368,19],{}," inside a generator",[43,1371,1372],{},"Reusing an iterator that has already been consumed",[675,1374,1376],{"id":1375},"reusing-a-consumed-iterator","Reusing a consumed iterator",[14,1378,1379],{},"This is another common mistake:",[91,1381,1383],{"className":93,"code":1382,"language":95,"meta":96,"style":96},"numbers = iter([1, 2, 3])\n\nfor n in numbers:\n    print(n)\n\nprint(\"Second loop:\")\n\nfor n in numbers:\n    print(n)\n",[17,1384,1385,1408,1412,1425,1436,1440,1455,1459,1471],{"__ignoreMap":96},[100,1386,1387,1389,1391,1393,1395,1397,1399,1401,1403,1406],{"class":102,"line":103},[100,1388,107],{"class":106},[100,1390,111],{"class":110},[100,1392,115],{"class":114},[100,1394,119],{"class":118},[100,1396,123],{"class":122},[100,1398,126],{"class":118},[100,1400,129],{"class":122},[100,1402,126],{"class":118},[100,1404,1405],{"class":122}," 3",[100,1407,132],{"class":118},[100,1409,1410],{"class":102,"line":135},[100,1411,139],{"emptyLinePlaceholder":138},[100,1413,1414,1416,1419,1421,1423],{"class":102,"line":142},[100,1415,71],{"class":145},[100,1417,1418],{"class":106}," n ",[100,1420,617],{"class":145},[100,1422,620],{"class":106},[100,1424,149],{"class":118},[100,1426,1427,1429,1431,1434],{"class":102,"line":152},[100,1428,200],{"class":114},[100,1430,170],{"class":118},[100,1432,1433],{"class":178},"n",[100,1435,215],{"class":118},[100,1437,1438],{"class":102,"line":164},[100,1439,139],{"emptyLinePlaceholder":138},[100,1441,1442,1444,1446,1448,1451,1453],{"class":102,"line":185},[100,1443,372],{"class":114},[100,1445,170],{"class":118},[100,1447,206],{"class":205},[100,1449,1450],{"class":209},"Second loop:",[100,1452,206],{"class":205},[100,1454,215],{"class":118},[100,1456,1457],{"class":102,"line":197},[100,1458,139],{"emptyLinePlaceholder":138},[100,1460,1461,1463,1465,1467,1469],{"class":102,"line":771},[100,1462,71],{"class":145},[100,1464,1418],{"class":106},[100,1466,617],{"class":145},[100,1468,620],{"class":106},[100,1470,149],{"class":118},[100,1472,1473,1475,1477,1479],{"class":102,"line":787},[100,1474,200],{"class":114},[100,1476,170],{"class":118},[100,1478,1433],{"class":178},[100,1480,215],{"class":118},[14,1482,218],{},[91,1484,1486],{"className":93,"code":1485,"language":95,"meta":96,"style":96},"1\n2\n3\nSecond loop:\n",[17,1487,1488,1492,1496,1501],{"__ignoreMap":96},[100,1489,1490],{"class":102,"line":103},[100,1491,228],{"class":122},[100,1493,1494],{"class":102,"line":135},[100,1495,233],{"class":122},[100,1497,1498],{"class":102,"line":142},[100,1499,1500],{"class":122},"3\n",[100,1502,1503,1506],{"class":102,"line":152},[100,1504,1505],{"class":106},"Second loop",[100,1507,149],{"class":118},[14,1509,1510],{},"The second loop prints nothing because the iterator was already consumed in the first loop.",[77,1512,1514],{"id":1513},"faq","FAQ",[675,1516,1518],{"id":1517},"is-stopiteration-always-an-error","Is StopIteration always an error?",[14,1520,1521],{},"No. It is a normal signal that an iterator is finished. It becomes a visible error only when your code does not handle it.",[675,1523,1525],{"id":1524},"why-does-a-for-loop-not-show-stopiteration","Why does a for loop not show StopIteration?",[14,1527,1528],{},"Because Python handles it internally to stop the loop.",[675,1530,1532],{"id":1531},"how-do-i-avoid-stopiteration-with-next","How do I avoid StopIteration with next()?",[14,1534,241,1535,1537,1538,290],{},[17,1536,244],{}," or pass a default value like ",[17,1539,1540],{},"next(my_iter, None)",[675,1542,1544],{"id":1543},"should-i-raise-stopiteration-in-a-generator","Should I raise StopIteration in a generator?",[14,1546,1547,1548,1550],{},"No. In generators, use ",[17,1549,1053],{}," or let the function end naturally.",[77,1552,1554],{"id":1553},"see-also","See also",[40,1556,1557,1562,1567,1577,1582],{},[43,1558,1559],{},[295,1560,1561],{"href":297},"Iterators and iterable objects explained",[43,1563,1564],{},[295,1565,1566],{"href":1163},"Generators in Python explained",[43,1568,1569],{},[295,1570,1572,1573,1576],{"href":1571},"\u002Freference\u002Fpython-enumerate-function-explained","Python ",[17,1574,1575],{},"enumerate()"," function explained",[43,1578,1579],{},[295,1580,1581],{"href":1028},"How to handle exceptions in Python",[43,1583,1584],{},[295,1585,1587],{"href":1586},"\u002Flearn\u002Fpython-errors-and-exceptions-explained","Python errors and exceptions explained",[1589,1590,1591],"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 .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 .srdBf, html code.shiki .srdBf{--shiki-light:#F76D47;--shiki-default:#005CC5;--shiki-dark:#79B8FF}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 .s39Yj, html code.shiki .s39Yj{--shiki-light:#39ADB5;--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 .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 .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 .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 .s_hVV, html code.shiki .s_hVV{--shiki-light:#90A4AE;--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}",{"title":96,"searchDepth":135,"depth":135,"links":1593},[1594,1595,1596,1597,1598,1599,1607,1611,1612,1615,1621],{"id":79,"depth":135,"text":80},{"id":257,"depth":135,"text":258},{"id":301,"depth":135,"text":302},{"id":421,"depth":135,"text":422},{"id":567,"depth":135,"text":568},{"id":672,"depth":135,"text":673,"children":1600},[1601,1603,1605],{"id":677,"depth":142,"text":1602},"Wrap next() in try-except",{"id":813,"depth":142,"text":1604},"Use a default value with next()",{"id":942,"depth":142,"text":1606},"Stop calling next() when the iterator is finished",{"id":1032,"depth":135,"text":1033,"children":1608},[1609,1610],{"id":1066,"depth":142,"text":1067},{"id":1107,"depth":142,"text":1108},{"id":1167,"depth":135,"text":1168},{"id":1343,"depth":135,"text":1344,"children":1613},[1614],{"id":1375,"depth":142,"text":1376},{"id":1513,"depth":135,"text":1514,"children":1616},[1617,1618,1619,1620],{"id":1517,"depth":142,"text":1518},{"id":1524,"depth":142,"text":1525},{"id":1531,"depth":142,"text":1532},{"id":1543,"depth":142,"text":1544},{"id":1553,"depth":135,"text":1554},"Master stopiteration exception in python explained in our comprehensive Python beginner guide.","md",{},"\u002Ferrors\u002Fstopiteration-exception-in-python-explained",{"title":5,"description":1622},"errors\u002Fstopiteration-exception-in-python-explained","NCvwaHOAt9Z-uYbMs0WX7idt19reaBIdNtR1vsWaQfM",[1630,3726,5720,7365,9232,10730,12883,14591,16156,18008,20695,23207,24788,26057,27534,28803,29785,30660,31728,32618,35058,37743,39456,41092,42301,43942,45582,46851,48734,50714,52165,53212,54249,55488,56739,58345,59582,60817,61889,63677,64918,66265,67775,69051,70061,70927,71993,73006,74013,74864,76538,77677,79505,80761,82198,83972,85163,86898,88329,89670,91441,93782,95821,96926,98657,100661,102176,103644,105163,106693,107835,109240,110502,112331,113675,115871,117157,119044,120946,122304],{"id":1631,"title":1632,"body":1633,"description":3720,"extension":1623,"meta":3721,"navigation":138,"path":3722,"seo":3723,"stem":3724,"__hash__":3725},"content\u002Ferrors\u002Fassertionerror-in-python-causes-and-fixes.md","AssertionError in Python: Causes and Fixes",{"type":7,"value":1634,"toc":3685},[1635,1638,1650,1653,1655,1715,1725,1744,1750,1754,1762,1768,1776,1779,1782,1793,1797,1800,1803,1815,1818,1839,1842,1845,1879,1885,1889,1892,1931,1933,2009,2012,2036,2041,2044,2070,2072,2128,2131,2133,2138,2164,2167,2220,2226,2230,2233,2237,2240,2274,2281,2284,2316,2322,2326,2329,2390,2392,2431,2434,2496,2500,2503,2587,2590,2593,2664,2667,2671,2674,2677,2698,2701,2730,2738,2744,2754,2813,2819,2825,2829,2835,2837,2923,2930,2933,3005,3007,3035,3038,3041,3051,3054,3058,3067,3073,3084,3086,3172,3182,3193,3195,3256,3259,3271,3275,3281,3307,3309,3399,3401,3455,3458,3462,3468,3472,3485,3490,3511,3521,3525,3540,3544,3565,3568,3572,3575,3577,3603,3609,3611,3615,3622,3626,3632,3636,3645,3649,3657,3659,3682],[10,1636,1632],{"id":1637},"assertionerror-in-python-causes-and-fixes",[14,1639,1640,20,1643,1646,1647,290],{},[17,1641,1642],{},"AssertionError",[17,1644,1645],{},"assert"," statement checks a condition and that condition is ",[17,1648,1649],{},"False",[14,1651,1652],{},"This error usually means your program reached a state that the code assumed should never happen. It is common in debugging, tests, and code that checks important assumptions.",[77,1654,80],{"id":79},[91,1656,1658],{"className":93,"code":1657,"language":95,"meta":96,"style":96},"age = 20\nassert age >= 18\n\nname = \"\"\nassert name, \"name must not be empty\"\n",[17,1659,1660,1670,1683,1687,1697],{"__ignoreMap":96},[100,1661,1662,1665,1667],{"class":102,"line":103},[100,1663,1664],{"class":106},"age ",[100,1666,111],{"class":110},[100,1668,1669],{"class":122}," 20\n",[100,1671,1672,1674,1677,1680],{"class":102,"line":135},[100,1673,1645],{"class":145},[100,1675,1676],{"class":106}," age ",[100,1678,1679],{"class":110},">=",[100,1681,1682],{"class":122}," 18\n",[100,1684,1685],{"class":102,"line":142},[100,1686,139],{"emptyLinePlaceholder":138},[100,1688,1689,1692,1694],{"class":102,"line":152},[100,1690,1691],{"class":106},"name ",[100,1693,111],{"class":110},[100,1695,1696],{"class":205}," \"\"\n",[100,1698,1699,1701,1704,1706,1709,1712],{"class":102,"line":164},[100,1700,1645],{"class":145},[100,1702,1703],{"class":106}," name",[100,1705,126],{"class":118},[100,1707,1708],{"class":205}," \"",[100,1710,1711],{"class":209},"name must not be empty",[100,1713,1714],{"class":205},"\"\n",[14,1716,1717,20,1719,1721,1722,1724],{},[17,1718,1642],{},[17,1720,1645],{}," condition is ",[17,1723,1649],{},". To fix it:",[40,1726,1727,1730,1733,1738],{},[43,1728,1729],{},"Check whether the condition is correct",[43,1731,1732],{},"Check whether the variable value is correct",[43,1734,1735,1736],{},"Add a useful message to the ",[17,1737,1645],{},[43,1739,1740,1741,1743],{},"Do not use ",[17,1742,1645],{}," for normal user input validation",[14,1745,1746,1747,290],{},"For broader background, see ",[295,1748,1587],{"href":1749},"\u002Flearn\u002Fpython-errors-and-exceptions-explained\u002F",[77,1751,1753],{"id":1752},"what-assertionerror-means","What AssertionError means",[14,1755,1756,1758,1759,1761],{},[17,1757,1642],{}," is raised when an ",[17,1760,1645],{}," statement fails.",[14,1763,1764,1765,1767],{},"An ",[17,1766,1645],{}," statement is a quick way to say:",[40,1769,1770,1773],{},[43,1771,1772],{},"\"This condition should be true\"",[43,1774,1775],{},"\"If it is not true, stop the program here\"",[14,1777,1778],{},"For example, if your code assumes a number must be positive, you might write an assertion to check that assumption.",[14,1780,1781],{},"Assertions are often used:",[40,1783,1784,1787,1790],{},[43,1785,1786],{},"While debugging",[43,1788,1789],{},"In tests",[43,1791,1792],{},"To catch incorrect program states early",[77,1794,1796],{"id":1795},"basic-syntax-of-assert","Basic syntax of assert",[14,1798,1799],{},"There are two common forms.",[14,1801,1802],{},"Basic form:",[91,1804,1806],{"className":93,"code":1805,"language":95,"meta":96,"style":96},"assert condition\n",[17,1807,1808],{"__ignoreMap":96},[100,1809,1810,1812],{"class":102,"line":103},[100,1811,1645],{"class":145},[100,1813,1814],{"class":106}," condition\n",[14,1816,1817],{},"With a custom message:",[91,1819,1821],{"className":93,"code":1820,"language":95,"meta":96,"style":96},"assert condition, \"message\"\n",[17,1822,1823],{"__ignoreMap":96},[100,1824,1825,1827,1830,1832,1834,1837],{"class":102,"line":103},[100,1826,1645],{"class":145},[100,1828,1829],{"class":106}," condition",[100,1831,126],{"class":118},[100,1833,1708],{"class":205},[100,1835,1836],{"class":209},"message",[100,1838,1714],{"class":205},[14,1840,1841],{},"The second form is usually better because it tells you what went wrong.",[14,1843,1844],{},"Example:",[91,1846,1848],{"className":93,"code":1847,"language":95,"meta":96,"style":96},"age = 16\nassert age >= 18, \"age must be at least 18\"\n",[17,1849,1850,1859],{"__ignoreMap":96},[100,1851,1852,1854,1856],{"class":102,"line":103},[100,1853,1664],{"class":106},[100,1855,111],{"class":110},[100,1857,1858],{"class":122}," 16\n",[100,1860,1861,1863,1865,1867,1870,1872,1874,1877],{"class":102,"line":135},[100,1862,1645],{"class":145},[100,1864,1676],{"class":106},[100,1866,1679],{"class":110},[100,1868,1869],{"class":122}," 18",[100,1871,126],{"class":118},[100,1873,1708],{"class":205},[100,1875,1876],{"class":209},"age must be at least 18",[100,1878,1714],{"class":205},[14,1880,1881,1882,1884],{},"If the condition is false, Python raises ",[17,1883,1642],{}," with the message.",[77,1886,1888],{"id":1887},"example-that-causes-assertionerror","Example that causes AssertionError",[14,1890,1891],{},"Here is a simple example:",[91,1893,1895],{"className":93,"code":1894,"language":95,"meta":96,"style":96},"number = -5\nassert number > 0, \"number must be positive\"\n",[17,1896,1897,1910],{"__ignoreMap":96},[100,1898,1899,1902,1904,1907],{"class":102,"line":103},[100,1900,1901],{"class":106},"number ",[100,1903,111],{"class":110},[100,1905,1906],{"class":110}," -",[100,1908,1909],{"class":122},"5\n",[100,1911,1912,1914,1916,1919,1922,1924,1926,1929],{"class":102,"line":135},[100,1913,1645],{"class":145},[100,1915,614],{"class":106},[100,1917,1918],{"class":110},">",[100,1920,1921],{"class":122}," 0",[100,1923,126],{"class":118},[100,1925,1708],{"class":205},[100,1927,1928],{"class":209},"number must be positive",[100,1930,1714],{"class":205},[14,1932,218],{},[91,1934,1936],{"className":93,"code":1935,"language":95,"meta":96,"style":96},"Traceback (most recent call last):\n  File \"example.py\", line 2, in \u003Cmodule>\n    assert number > 0, \"number must be positive\"\nAssertionError: number must be positive\n",[17,1937,1938,1948,1981,2000],{"__ignoreMap":96},[100,1939,1940,1942,1944,1946],{"class":102,"line":103},[100,1941,514],{"class":178},[100,1943,170],{"class":118},[100,1945,519],{"class":178},[100,1947,522],{"class":118},[100,1949,1950,1953,1955,1958,1960,1962,1965,1967,1969,1972,1975,1978],{"class":102,"line":135},[100,1951,1952],{"class":106},"  File ",[100,1954,206],{"class":205},[100,1956,1957],{"class":209},"example.py",[100,1959,206],{"class":205},[100,1961,126],{"class":118},[100,1963,1964],{"class":106}," line ",[100,1966,559],{"class":122},[100,1968,126],{"class":118},[100,1970,1971],{"class":110}," in",[100,1973,1974],{"class":110}," \u003C",[100,1976,1977],{"class":106},"module",[100,1979,1980],{"class":110},">\n",[100,1982,1983,1986,1988,1990,1992,1994,1996,1998],{"class":102,"line":142},[100,1984,1985],{"class":145},"    assert",[100,1987,614],{"class":106},[100,1989,1918],{"class":110},[100,1991,1921],{"class":122},[100,1993,126],{"class":118},[100,1995,1708],{"class":205},[100,1997,1928],{"class":209},[100,1999,1714],{"class":205},[100,2001,2002,2004,2006],{"class":102,"line":152},[100,2003,1642],{"class":191},[100,2005,89],{"class":118},[100,2007,2008],{"class":106}," number must be positive\n",[14,2010,2011],{},"Why did this fail?",[40,2013,2014,2020,2029],{},[43,2015,2016,2017],{},"The condition is ",[17,2018,2019],{},"number > 0",[43,2021,2022,2023,2025,2026],{},"The value of ",[17,2024,631],{}," is ",[17,2027,2028],{},"-5",[43,2030,2031,2025,2034],{},[17,2032,2033],{},"-5 > 0",[17,2035,1649],{},[14,2037,2038,2039,290],{},"So Python raises ",[17,2040,1642],{},[14,2042,2043],{},"Here is the same idea without a message:",[91,2045,2047],{"className":93,"code":2046,"language":95,"meta":96,"style":96},"number = -5\nassert number > 0\n",[17,2048,2049,2059],{"__ignoreMap":96},[100,2050,2051,2053,2055,2057],{"class":102,"line":103},[100,2052,1901],{"class":106},[100,2054,111],{"class":110},[100,2056,1906],{"class":110},[100,2058,1909],{"class":122},[100,2060,2061,2063,2065,2067],{"class":102,"line":135},[100,2062,1645],{"class":145},[100,2064,614],{"class":106},[100,2066,1918],{"class":110},[100,2068,2069],{"class":122}," 0\n",[14,2071,218],{},[91,2073,2075],{"className":93,"code":2074,"language":95,"meta":96,"style":96},"Traceback (most recent call last):\n  File \"example.py\", line 2, in \u003Cmodule>\n    assert number > 0\nAssertionError\n",[17,2076,2077,2087,2113,2123],{"__ignoreMap":96},[100,2078,2079,2081,2083,2085],{"class":102,"line":103},[100,2080,514],{"class":178},[100,2082,170],{"class":118},[100,2084,519],{"class":178},[100,2086,522],{"class":118},[100,2088,2089,2091,2093,2095,2097,2099,2101,2103,2105,2107,2109,2111],{"class":102,"line":135},[100,2090,1952],{"class":106},[100,2092,206],{"class":205},[100,2094,1957],{"class":209},[100,2096,206],{"class":205},[100,2098,126],{"class":118},[100,2100,1964],{"class":106},[100,2102,559],{"class":122},[100,2104,126],{"class":118},[100,2106,1971],{"class":110},[100,2108,1974],{"class":110},[100,2110,1977],{"class":106},[100,2112,1980],{"class":110},[100,2114,2115,2117,2119,2121],{"class":102,"line":142},[100,2116,1985],{"class":145},[100,2118,614],{"class":106},[100,2120,1918],{"class":110},[100,2122,2069],{"class":122},[100,2124,2125],{"class":102,"line":152},[100,2126,2127],{"class":191},"AssertionError\n",[14,2129,2130],{},"This works, but it is harder to debug because the error gives less information.",[77,2132,1344],{"id":1343},[14,2134,2135,2137],{},[17,2136,1642],{}," usually happens for one of these reasons:",[40,2139,2140,2147,2150,2153,2156,2161],{},[43,2141,1764,2142,2144,2145],{},[17,2143,1645],{}," condition evaluates to ",[17,2146,1649],{},[43,2148,2149],{},"Unexpected input was passed to a function",[43,2151,2152],{},"A variable has the wrong value because of earlier code",[43,2154,2155],{},"The code made an assumption that is not always true",[43,2157,2158,2160],{},[17,2159,1645],{}," was used for user input validation",[43,2162,2163],{},"No custom message was added, so the error is harder to understand",[14,2165,2166],{},"Example of a variable changing earlier in the program:",[91,2168,2170],{"className":93,"code":2169,"language":95,"meta":96,"style":96},"count = 10\ncount = count - 15\n\nassert count >= 0, \"count should not be negative\"\n",[17,2171,2172,2182,2197,2201],{"__ignoreMap":96},[100,2173,2174,2177,2179],{"class":102,"line":103},[100,2175,2176],{"class":106},"count ",[100,2178,111],{"class":110},[100,2180,2181],{"class":122}," 10\n",[100,2183,2184,2186,2188,2191,2194],{"class":102,"line":135},[100,2185,2176],{"class":106},[100,2187,111],{"class":110},[100,2189,2190],{"class":106}," count ",[100,2192,2193],{"class":110},"-",[100,2195,2196],{"class":122}," 15\n",[100,2198,2199],{"class":102,"line":142},[100,2200,139],{"emptyLinePlaceholder":138},[100,2202,2203,2205,2207,2209,2211,2213,2215,2218],{"class":102,"line":152},[100,2204,1645],{"class":145},[100,2206,2190],{"class":106},[100,2208,1679],{"class":110},[100,2210,1921],{"class":122},[100,2212,126],{"class":118},[100,2214,1708],{"class":205},[100,2216,2217],{"class":209},"count should not be negative",[100,2219,1714],{"class":205},[14,2221,2222,2223,2225],{},"The real problem may be earlier in the code, not on the ",[17,2224,1645],{}," line itself.",[77,2227,2229],{"id":2228},"how-to-fix-assertionerror","How to fix AssertionError",[14,2231,2232],{},"Start by checking the exact condition that failed.",[675,2234,2236],{"id":2235},"_1-check-the-assert-condition","1. Check the assert condition",[14,2238,2239],{},"Make sure the condition matches what you really want to test.",[91,2241,2243],{"className":93,"code":2242,"language":95,"meta":96,"style":96},"score = 0\nassert score >= 0, \"score must not be negative\"\n",[17,2244,2245,2254],{"__ignoreMap":96},[100,2246,2247,2250,2252],{"class":102,"line":103},[100,2248,2249],{"class":106},"score ",[100,2251,111],{"class":110},[100,2253,2069],{"class":122},[100,2255,2256,2258,2261,2263,2265,2267,2269,2272],{"class":102,"line":135},[100,2257,1645],{"class":145},[100,2259,2260],{"class":106}," score ",[100,2262,1679],{"class":110},[100,2264,1921],{"class":122},[100,2266,126],{"class":118},[100,2268,1708],{"class":205},[100,2270,2271],{"class":209},"score must not be negative",[100,2273,1714],{"class":205},[14,2275,2276,2277,2280],{},"This assertion is fine if ",[17,2278,2279],{},"0"," is allowed.",[14,2282,2283],{},"But if you wrote this by mistake:",[91,2285,2287],{"className":93,"code":2286,"language":95,"meta":96,"style":96},"score = 0\nassert score > 0, \"score must be positive\"\n",[17,2288,2289,2297],{"__ignoreMap":96},[100,2290,2291,2293,2295],{"class":102,"line":103},[100,2292,2249],{"class":106},[100,2294,111],{"class":110},[100,2296,2069],{"class":122},[100,2298,2299,2301,2303,2305,2307,2309,2311,2314],{"class":102,"line":135},[100,2300,1645],{"class":145},[100,2302,2260],{"class":106},[100,2304,1918],{"class":110},[100,2306,1921],{"class":122},[100,2308,126],{"class":118},[100,2310,1708],{"class":205},[100,2312,2313],{"class":209},"score must be positive",[100,2315,1714],{"class":205},[14,2317,2318,2319,2321],{},"then ",[17,2320,2279],{}," will fail even if it should be valid.",[675,2323,2325],{"id":2324},"_2-print-values-before-the-failing-line","2. Print values before the failing line",[14,2327,2328],{},"If you are not sure why the assertion failed, print the values first.",[91,2330,2332],{"className":93,"code":2331,"language":95,"meta":96,"style":96},"number = -5\nprint(number)\nprint(number > 0)\n\nassert number > 0, \"number must be positive\"\n",[17,2333,2334,2344,2354,2368,2372],{"__ignoreMap":96},[100,2335,2336,2338,2340,2342],{"class":102,"line":103},[100,2337,1901],{"class":106},[100,2339,111],{"class":110},[100,2341,1906],{"class":110},[100,2343,1909],{"class":122},[100,2345,2346,2348,2350,2352],{"class":102,"line":135},[100,2347,372],{"class":114},[100,2349,170],{"class":118},[100,2351,631],{"class":178},[100,2353,215],{"class":118},[100,2355,2356,2358,2360,2362,2364,2366],{"class":102,"line":142},[100,2357,372],{"class":114},[100,2359,170],{"class":118},[100,2361,1901],{"class":178},[100,2363,1918],{"class":110},[100,2365,1921],{"class":122},[100,2367,215],{"class":118},[100,2369,2370],{"class":102,"line":152},[100,2371,139],{"emptyLinePlaceholder":138},[100,2373,2374,2376,2378,2380,2382,2384,2386,2388],{"class":102,"line":164},[100,2375,1645],{"class":145},[100,2377,614],{"class":106},[100,2379,1918],{"class":110},[100,2381,1921],{"class":122},[100,2383,126],{"class":118},[100,2385,1708],{"class":205},[100,2387,1928],{"class":209},[100,2389,1714],{"class":205},[14,2391,218],{},[91,2393,2395],{"className":93,"code":2394,"language":95,"meta":96,"style":96},"-5\nFalse\nTraceback (most recent call last):\n...\nAssertionError: number must be positive\n",[17,2396,2397,2403,2408,2418,2423],{"__ignoreMap":96},[100,2398,2399,2401],{"class":102,"line":103},[100,2400,2193],{"class":110},[100,2402,1909],{"class":122},[100,2404,2405],{"class":102,"line":135},[100,2406,2407],{"class":158},"False\n",[100,2409,2410,2412,2414,2416],{"class":102,"line":142},[100,2411,514],{"class":178},[100,2413,170],{"class":118},[100,2415,519],{"class":178},[100,2417,522],{"class":118},[100,2419,2420],{"class":102,"line":152},[100,2421,2422],{"class":527},"...\n",[100,2424,2425,2427,2429],{"class":102,"line":164},[100,2426,1642],{"class":191},[100,2428,89],{"class":118},[100,2430,2008],{"class":106},[14,2432,2433],{},"Useful debugging commands include:",[91,2435,2437],{"className":93,"code":2436,"language":95,"meta":96,"style":96},"print(value)\nprint(type(value))\nprint(condition_result)\nhelp(assert)\npython your_script.py\n",[17,2438,2439,2449,2463,2474,2485],{"__ignoreMap":96},[100,2440,2441,2443,2445,2447],{"class":102,"line":103},[100,2442,372],{"class":114},[100,2444,170],{"class":118},[100,2446,757],{"class":178},[100,2448,215],{"class":118},[100,2450,2451,2453,2455,2457,2459,2461],{"class":102,"line":135},[100,2452,372],{"class":114},[100,2454,170],{"class":118},[100,2456,1250],{"class":191},[100,2458,170],{"class":118},[100,2460,757],{"class":178},[100,2462,182],{"class":118},[100,2464,2465,2467,2469,2472],{"class":102,"line":142},[100,2466,372],{"class":114},[100,2468,170],{"class":118},[100,2470,2471],{"class":178},"condition_result",[100,2473,215],{"class":118},[100,2475,2476,2479,2481,2483],{"class":102,"line":152},[100,2477,2478],{"class":114},"help",[100,2480,170],{"class":118},[100,2482,1645],{"class":145},[100,2484,215],{"class":118},[100,2486,2487,2490,2492],{"class":102,"line":164},[100,2488,2489],{"class":106},"python your_script",[100,2491,290],{"class":118},[100,2493,2495],{"class":2494},"skxfh","py\n",[675,2497,2499],{"id":2498},"_3-make-sure-the-data-is-valid","3. Make sure the data is valid",[14,2501,2502],{},"If a function expects a certain kind of input, check what you passed into it.",[91,2504,2506],{"className":93,"code":2505,"language":95,"meta":96,"style":96},"def divide_total(total, people):\n    assert people > 0, \"people must be greater than 0\"\n    return total \u002F people\n\nprint(divide_total(100, 4))\n",[17,2507,2508,2528,2548,2562,2566],{"__ignoreMap":96},[100,2509,2510,2512,2515,2517,2521,2523,2526],{"class":102,"line":103},[100,2511,1078],{"class":1077},[100,2513,2514],{"class":1081}," divide_total",[100,2516,170],{"class":118},[100,2518,2520],{"class":2519},"sFwrP","total",[100,2522,126],{"class":118},[100,2524,2525],{"class":2519}," people",[100,2527,522],{"class":118},[100,2529,2530,2532,2535,2537,2539,2541,2543,2546],{"class":102,"line":135},[100,2531,1985],{"class":145},[100,2533,2534],{"class":106}," people ",[100,2536,1918],{"class":110},[100,2538,1921],{"class":122},[100,2540,126],{"class":118},[100,2542,1708],{"class":205},[100,2544,2545],{"class":209},"people must be greater than 0",[100,2547,1714],{"class":205},[100,2549,2550,2553,2556,2559],{"class":102,"line":142},[100,2551,2552],{"class":145},"    return",[100,2554,2555],{"class":106}," total ",[100,2557,2558],{"class":110},"\u002F",[100,2560,2561],{"class":106}," people\n",[100,2563,2564],{"class":102,"line":152},[100,2565,139],{"emptyLinePlaceholder":138},[100,2567,2568,2570,2572,2575,2577,2580,2582,2585],{"class":102,"line":164},[100,2569,372],{"class":114},[100,2571,170],{"class":118},[100,2573,2574],{"class":178},"divide_total",[100,2576,170],{"class":118},[100,2578,2579],{"class":122},"100",[100,2581,126],{"class":118},[100,2583,2584],{"class":122}," 4",[100,2586,182],{"class":118},[14,2588,2589],{},"This works.",[14,2591,2592],{},"But this will fail:",[91,2594,2596],{"className":93,"code":2595,"language":95,"meta":96,"style":96},"def divide_total(total, people):\n    assert people > 0, \"people must be greater than 0\"\n    return total \u002F people\n\nprint(divide_total(100, 0))\n",[17,2597,2598,2614,2632,2642,2646],{"__ignoreMap":96},[100,2599,2600,2602,2604,2606,2608,2610,2612],{"class":102,"line":103},[100,2601,1078],{"class":1077},[100,2603,2514],{"class":1081},[100,2605,170],{"class":118},[100,2607,2520],{"class":2519},[100,2609,126],{"class":118},[100,2611,2525],{"class":2519},[100,2613,522],{"class":118},[100,2615,2616,2618,2620,2622,2624,2626,2628,2630],{"class":102,"line":135},[100,2617,1985],{"class":145},[100,2619,2534],{"class":106},[100,2621,1918],{"class":110},[100,2623,1921],{"class":122},[100,2625,126],{"class":118},[100,2627,1708],{"class":205},[100,2629,2545],{"class":209},[100,2631,1714],{"class":205},[100,2633,2634,2636,2638,2640],{"class":102,"line":142},[100,2635,2552],{"class":145},[100,2637,2555],{"class":106},[100,2639,2558],{"class":110},[100,2641,2561],{"class":106},[100,2643,2644],{"class":102,"line":152},[100,2645,139],{"emptyLinePlaceholder":138},[100,2647,2648,2650,2652,2654,2656,2658,2660,2662],{"class":102,"line":164},[100,2649,372],{"class":114},[100,2651,170],{"class":118},[100,2653,2574],{"class":178},[100,2655,170],{"class":118},[100,2657,2579],{"class":122},[100,2659,126],{"class":118},[100,2661,1921],{"class":122},[100,2663,182],{"class":118},[14,2665,2666],{},"The fix is to pass a valid value, or change the function so it handles invalid input more clearly.",[675,2668,2670],{"id":2669},"_4-add-a-helpful-message","4. Add a helpful message",[14,2672,2673],{},"A good message makes debugging much faster.",[14,2675,2676],{},"Less helpful:",[91,2678,2680],{"className":93,"code":2679,"language":95,"meta":96,"style":96},"username = \"\"\nassert username\n",[17,2681,2682,2691],{"__ignoreMap":96},[100,2683,2684,2687,2689],{"class":102,"line":103},[100,2685,2686],{"class":106},"username ",[100,2688,111],{"class":110},[100,2690,1696],{"class":205},[100,2692,2693,2695],{"class":102,"line":135},[100,2694,1645],{"class":145},[100,2696,2697],{"class":106}," username\n",[14,2699,2700],{},"More helpful:",[91,2702,2704],{"className":93,"code":2703,"language":95,"meta":96,"style":96},"username = \"\"\nassert username, \"username must not be empty\"\n",[17,2705,2706,2714],{"__ignoreMap":96},[100,2707,2708,2710,2712],{"class":102,"line":103},[100,2709,2686],{"class":106},[100,2711,111],{"class":110},[100,2713,1696],{"class":205},[100,2715,2716,2718,2721,2723,2725,2728],{"class":102,"line":135},[100,2717,1645],{"class":145},[100,2719,2720],{"class":106}," username",[100,2722,126],{"class":118},[100,2724,1708],{"class":205},[100,2726,2727],{"class":209},"username must not be empty",[100,2729,1714],{"class":205},[675,2731,2733,2734,2737],{"id":2732},"_5-use-if-and-raise-a-normal-exception-for-user-input","5. Use ",[17,2735,2736],{},"if"," and raise a normal exception for user input",[14,2739,2740,2741,2743],{},"If you are validating normal input from a user, ",[17,2742,1645],{}," is usually not the best tool.",[14,2745,2746,2747,2749,2750,290],{},"Use an ",[17,2748,2736],{}," statement and raise a clear exception instead. See also ",[295,2751,2753],{"href":2752},"\u002Fhow-to\u002Fhow-to-raise-an-exception-in-python\u002F","how to raise an exception in Python",[91,2755,2757],{"className":93,"code":2756,"language":95,"meta":96,"style":96},"def set_age(age):\n    if age \u003C 0:\n        raise ValueError(\"age cannot be negative\")\n    return age\n",[17,2758,2759,2773,2787,2806],{"__ignoreMap":96},[100,2760,2761,2763,2766,2768,2771],{"class":102,"line":103},[100,2762,1078],{"class":1077},[100,2764,2765],{"class":1081}," set_age",[100,2767,170],{"class":118},[100,2769,2770],{"class":2519},"age",[100,2772,522],{"class":118},[100,2774,2775,2778,2780,2783,2785],{"class":102,"line":135},[100,2776,2777],{"class":145},"    if",[100,2779,1676],{"class":106},[100,2781,2782],{"class":110},"\u003C",[100,2784,1921],{"class":122},[100,2786,149],{"class":118},[100,2788,2789,2792,2795,2797,2799,2802,2804],{"class":102,"line":142},[100,2790,2791],{"class":145},"        raise",[100,2793,2794],{"class":191}," ValueError",[100,2796,170],{"class":118},[100,2798,206],{"class":205},[100,2800,2801],{"class":209},"age cannot be negative",[100,2803,206],{"class":205},[100,2805,215],{"class":118},[100,2807,2808,2810],{"class":102,"line":152},[100,2809,2552],{"class":145},[100,2811,2812],{"class":106}," age\n",[14,2814,2815,2816,2818],{},"This is clearer than using ",[17,2817,1645],{}," for ordinary bad input.",[14,2820,2821,2822,290],{},"If you want to handle errors without stopping the whole program, read ",[295,2823,1029],{"href":2824},"\u002Fhow-to\u002Fhow-to-handle-exceptions-in-python\u002F",[77,2826,2828],{"id":2827},"assertionerror-in-functions","AssertionError in functions",[14,2830,2831,2832,2834],{},"A function may use ",[17,2833,1645],{}," to check assumptions about its inputs.",[14,2836,1844],{},[91,2838,2840],{"className":93,"code":2839,"language":95,"meta":96,"style":96},"def greet(name):\n    assert isinstance(name, str), \"name must be a string\"\n    return \"Hello, \" + name\n\nprint(greet(\"Maya\"))\n",[17,2841,2842,2856,2882,2899,2903],{"__ignoreMap":96},[100,2843,2844,2846,2849,2851,2854],{"class":102,"line":103},[100,2845,1078],{"class":1077},[100,2847,2848],{"class":1081}," greet",[100,2850,170],{"class":118},[100,2852,2853],{"class":2519},"name",[100,2855,522],{"class":118},[100,2857,2858,2860,2863,2865,2867,2869,2872,2875,2877,2880],{"class":102,"line":135},[100,2859,1985],{"class":145},[100,2861,2862],{"class":114}," isinstance",[100,2864,170],{"class":118},[100,2866,2853],{"class":178},[100,2868,126],{"class":118},[100,2870,2871],{"class":191}," str",[100,2873,2874],{"class":118},"),",[100,2876,1708],{"class":205},[100,2878,2879],{"class":209},"name must be a string",[100,2881,1714],{"class":205},[100,2883,2884,2886,2888,2891,2893,2896],{"class":102,"line":142},[100,2885,2552],{"class":145},[100,2887,1708],{"class":205},[100,2889,2890],{"class":209},"Hello, ",[100,2892,206],{"class":205},[100,2894,2895],{"class":110}," +",[100,2897,2898],{"class":106}," name\n",[100,2900,2901],{"class":102,"line":152},[100,2902,139],{"emptyLinePlaceholder":138},[100,2904,2905,2907,2909,2912,2914,2916,2919,2921],{"class":102,"line":164},[100,2906,372],{"class":114},[100,2908,170],{"class":118},[100,2910,2911],{"class":178},"greet",[100,2913,170],{"class":118},[100,2915,206],{"class":205},[100,2917,2918],{"class":209},"Maya",[100,2920,206],{"class":205},[100,2922,182],{"class":118},[14,2924,2925,2926,2929],{},"This works because ",[17,2927,2928],{},"\"Maya\""," is a string.",[14,2931,2932],{},"But this fails:",[91,2934,2936],{"className":93,"code":2935,"language":95,"meta":96,"style":96},"def greet(name):\n    assert isinstance(name, str), \"name must be a string\"\n    return \"Hello, \" + name\n\nprint(greet(123))\n",[17,2937,2938,2950,2972,2986,2990],{"__ignoreMap":96},[100,2939,2940,2942,2944,2946,2948],{"class":102,"line":103},[100,2941,1078],{"class":1077},[100,2943,2848],{"class":1081},[100,2945,170],{"class":118},[100,2947,2853],{"class":2519},[100,2949,522],{"class":118},[100,2951,2952,2954,2956,2958,2960,2962,2964,2966,2968,2970],{"class":102,"line":135},[100,2953,1985],{"class":145},[100,2955,2862],{"class":114},[100,2957,170],{"class":118},[100,2959,2853],{"class":178},[100,2961,126],{"class":118},[100,2963,2871],{"class":191},[100,2965,2874],{"class":118},[100,2967,1708],{"class":205},[100,2969,2879],{"class":209},[100,2971,1714],{"class":205},[100,2973,2974,2976,2978,2980,2982,2984],{"class":102,"line":142},[100,2975,2552],{"class":145},[100,2977,1708],{"class":205},[100,2979,2890],{"class":209},[100,2981,206],{"class":205},[100,2983,2895],{"class":110},[100,2985,2898],{"class":106},[100,2987,2988],{"class":102,"line":152},[100,2989,139],{"emptyLinePlaceholder":138},[100,2991,2992,2994,2996,2998,3000,3003],{"class":102,"line":164},[100,2993,372],{"class":114},[100,2995,170],{"class":118},[100,2997,2911],{"class":178},[100,2999,170],{"class":118},[100,3001,3002],{"class":122},"123",[100,3004,182],{"class":118},[14,3006,218],{},[91,3008,3010],{"className":93,"code":3009,"language":95,"meta":96,"style":96},"Traceback (most recent call last):\n...\nAssertionError: name must be a string\n",[17,3011,3012,3022,3026],{"__ignoreMap":96},[100,3013,3014,3016,3018,3020],{"class":102,"line":103},[100,3015,514],{"class":178},[100,3017,170],{"class":118},[100,3019,519],{"class":178},[100,3021,522],{"class":118},[100,3023,3024],{"class":102,"line":135},[100,3025,2422],{"class":527},[100,3027,3028,3030,3032],{"class":102,"line":142},[100,3029,1642],{"class":191},[100,3031,89],{"class":118},[100,3033,3034],{"class":106}," name must be a string\n",[14,3036,3037],{},"This can be useful when a function should only be called in a certain way.",[14,3039,3040],{},"But there is an important idea here:",[40,3042,3043,3048],{},[43,3044,3045,3047],{},[17,3046,1645],{}," checks programmer assumptions",[43,3049,3050],{},"It is not the same as handling normal user mistakes",[14,3052,3053],{},"If a user might enter bad data, explicit exceptions are usually better.",[77,3055,3057],{"id":3056},"assert-vs-raising-an-exception-yourself","assert vs raising an exception yourself",[14,3059,3060,3061,3063,3064,290],{},"It helps to know when to use ",[17,3062,1645],{}," and when to use ",[17,3065,3066],{},"raise",[675,3068,241,3070,3072],{"id":3069},"use-assert-when",[17,3071,1645],{}," when:",[40,3074,3075,3078,3081],{},[43,3076,3077],{},"You are checking an internal assumption",[43,3079,3080],{},"You are debugging code",[43,3082,3083],{},"You want to fail early if something impossible happened",[14,3085,1844],{},[91,3087,3089],{"className":93,"code":3088,"language":95,"meta":96,"style":96},"items = [1, 2, 3]\nindex = 1\n\nassert 0 \u003C= index \u003C len(items), \"index should be within list bounds\"\nprint(items[index])\n",[17,3090,3091,3112,3121,3125,3156],{"__ignoreMap":96},[100,3092,3093,3096,3098,3100,3102,3104,3106,3108,3110],{"class":102,"line":103},[100,3094,3095],{"class":106},"items ",[100,3097,111],{"class":110},[100,3099,594],{"class":118},[100,3101,123],{"class":122},[100,3103,126],{"class":118},[100,3105,129],{"class":122},[100,3107,126],{"class":118},[100,3109,1405],{"class":122},[100,3111,603],{"class":118},[100,3113,3114,3117,3119],{"class":102,"line":135},[100,3115,3116],{"class":106},"index ",[100,3118,111],{"class":110},[100,3120,1093],{"class":122},[100,3122,3123],{"class":102,"line":142},[100,3124,139],{"emptyLinePlaceholder":138},[100,3126,3127,3129,3131,3134,3137,3139,3142,3144,3147,3149,3151,3154],{"class":102,"line":152},[100,3128,1645],{"class":145},[100,3130,1921],{"class":122},[100,3132,3133],{"class":110}," \u003C=",[100,3135,3136],{"class":106}," index ",[100,3138,2782],{"class":110},[100,3140,3141],{"class":114}," len",[100,3143,170],{"class":118},[100,3145,3146],{"class":178},"items",[100,3148,2874],{"class":118},[100,3150,1708],{"class":205},[100,3152,3153],{"class":209},"index should be within list bounds",[100,3155,1714],{"class":205},[100,3157,3158,3160,3162,3164,3167,3170],{"class":102,"line":164},[100,3159,372],{"class":114},[100,3161,170],{"class":118},[100,3163,3146],{"class":178},[100,3165,3166],{"class":118},"[",[100,3168,3169],{"class":178},"index",[100,3171,132],{"class":118},[675,3173,241,3175,3178,3179,3072],{"id":3174},"use-raise-valueerror-or-raise-typeerror-when",[17,3176,3177],{},"raise ValueError"," or ",[17,3180,3181],{},"raise TypeError",[40,3183,3184,3187,3190],{},[43,3185,3186],{},"A user gives bad input",[43,3188,3189],{},"A function argument is invalid",[43,3191,3192],{},"You want a clear, intentional error in production code",[14,3194,1844],{},[91,3196,3198],{"className":93,"code":3197,"language":95,"meta":96,"style":96},"def calculate_discount(price):\n    if price \u003C 0:\n        raise ValueError(\"price cannot be negative\")\n    return price * 0.9\n",[17,3199,3200,3214,3227,3244],{"__ignoreMap":96},[100,3201,3202,3204,3207,3209,3212],{"class":102,"line":103},[100,3203,1078],{"class":1077},[100,3205,3206],{"class":1081}," calculate_discount",[100,3208,170],{"class":118},[100,3210,3211],{"class":2519},"price",[100,3213,522],{"class":118},[100,3215,3216,3218,3221,3223,3225],{"class":102,"line":135},[100,3217,2777],{"class":145},[100,3219,3220],{"class":106}," price ",[100,3222,2782],{"class":110},[100,3224,1921],{"class":122},[100,3226,149],{"class":118},[100,3228,3229,3231,3233,3235,3237,3240,3242],{"class":102,"line":142},[100,3230,2791],{"class":145},[100,3232,2794],{"class":191},[100,3234,170],{"class":118},[100,3236,206],{"class":205},[100,3238,3239],{"class":209},"price cannot be negative",[100,3241,206],{"class":205},[100,3243,215],{"class":118},[100,3245,3246,3248,3250,3253],{"class":102,"line":152},[100,3247,2552],{"class":145},[100,3249,3220],{"class":106},[100,3251,3252],{"class":110},"*",[100,3254,3255],{"class":122}," 0.9\n",[14,3257,3258],{},"This is usually easier to understand than an assertion for user input.",[14,3260,3261,3262,3266,3267,290],{},"For more on related exception types, compare this with ",[295,3263,3265],{"href":3264},"\u002Ferrors\u002Fvalueerror-in-python-causes-and-fixes\u002F","ValueError in Python: causes and fixes"," and ",[295,3268,3270],{"href":3269},"\u002Ferrors\u002Ftypeerror-vs-valueerror-in-python-explained\u002F","TypeError vs ValueError in Python explained",[77,3272,3274],{"id":3273},"debugging-steps","Debugging steps",[14,3276,3277,3278,3280],{},"When you see ",[17,3279,1642],{},", follow these steps:",[3282,3283,3284,3287,3293,3298,3301,3304],"ol",{},[43,3285,3286],{},"Read the traceback carefully",[43,3288,3289,3290,3292],{},"Find the exact ",[17,3291,1645],{}," line that failed",[43,3294,3295,3296],{},"Look at the condition inside the ",[17,3297,1645],{},[43,3299,3300],{},"Check the values used in that condition",[43,3302,3303],{},"Print variables before the failing line",[43,3305,3306],{},"Test with small known values",[14,3308,1844],{},[91,3310,3312],{"className":93,"code":3311,"language":95,"meta":96,"style":96},"value = \"10\"\n\nprint(value)\nprint(type(value))\nprint(value.isdigit())\n\nassert isinstance(value, int), \"value must be an integer\"\n",[17,3313,3314,3327,3331,3341,3355,3371,3375],{"__ignoreMap":96},[100,3315,3316,3319,3321,3323,3325],{"class":102,"line":103},[100,3317,3318],{"class":106},"value ",[100,3320,111],{"class":110},[100,3322,1708],{"class":205},[100,3324,356],{"class":209},[100,3326,1714],{"class":205},[100,3328,3329],{"class":102,"line":135},[100,3330,139],{"emptyLinePlaceholder":138},[100,3332,3333,3335,3337,3339],{"class":102,"line":142},[100,3334,372],{"class":114},[100,3336,170],{"class":118},[100,3338,757],{"class":178},[100,3340,215],{"class":118},[100,3342,3343,3345,3347,3349,3351,3353],{"class":102,"line":152},[100,3344,372],{"class":114},[100,3346,170],{"class":118},[100,3348,1250],{"class":191},[100,3350,170],{"class":118},[100,3352,757],{"class":178},[100,3354,182],{"class":118},[100,3356,3357,3359,3361,3363,3365,3368],{"class":102,"line":164},[100,3358,372],{"class":114},[100,3360,170],{"class":118},[100,3362,757],{"class":178},[100,3364,290],{"class":118},[100,3366,3367],{"class":178},"isdigit",[100,3369,3370],{"class":118},"())\n",[100,3372,3373],{"class":102,"line":185},[100,3374,139],{"emptyLinePlaceholder":138},[100,3376,3377,3379,3381,3383,3385,3387,3390,3392,3394,3397],{"class":102,"line":197},[100,3378,1645],{"class":145},[100,3380,2862],{"class":114},[100,3382,170],{"class":118},[100,3384,757],{"class":178},[100,3386,126],{"class":118},[100,3388,3389],{"class":191}," int",[100,3391,2874],{"class":118},[100,3393,1708],{"class":205},[100,3395,3396],{"class":209},"value must be an integer",[100,3398,1714],{"class":205},[14,3400,218],{},[91,3402,3404],{"className":93,"code":3403,"language":95,"meta":96,"style":96},"10\n\u003Cclass 'str'>\nTrue\nTraceback (most recent call last):\n...\nAssertionError: value must be an integer\n",[17,3405,3406,3411,3427,3432,3442,3446],{"__ignoreMap":96},[100,3407,3408],{"class":102,"line":103},[100,3409,3410],{"class":122},"10\n",[100,3412,3413,3415,3418,3420,3423,3425],{"class":102,"line":135},[100,3414,2782],{"class":110},[100,3416,3417],{"class":1077},"class",[100,3419,1274],{"class":205},[100,3421,3422],{"class":209},"str",[100,3424,1280],{"class":205},[100,3426,1980],{"class":110},[100,3428,3429],{"class":102,"line":142},[100,3430,3431],{"class":158},"True\n",[100,3433,3434,3436,3438,3440],{"class":102,"line":152},[100,3435,514],{"class":178},[100,3437,170],{"class":118},[100,3439,519],{"class":178},[100,3441,522],{"class":118},[100,3443,3444],{"class":102,"line":164},[100,3445,2422],{"class":527},[100,3447,3448,3450,3452],{"class":102,"line":185},[100,3449,1642],{"class":191},[100,3451,89],{"class":118},[100,3453,3454],{"class":106}," value must be an integer\n",[14,3456,3457],{},"This tells you the problem is not the number itself. The problem is that the value is a string, not an integer.",[77,3459,3461],{"id":3460},"related-errors-and-confusion-points","Related errors and confusion points",[14,3463,3464,3465,3467],{},"Beginners often confuse ",[17,3466,1642],{}," with other errors.",[675,3469,3471],{"id":3470},"assertionerror-vs-valueerror","AssertionError vs ValueError",[40,3473,3474,3479],{},[43,3475,3476,3478],{},[17,3477,1642],{}," means an assertion failed",[43,3480,3481,3484],{},[17,3482,3483],{},"ValueError"," means the value has the right type, but the value is not acceptable",[14,3486,3487,3488,89],{},"Example of ",[17,3489,3483],{},[91,3491,3493],{"className":93,"code":3492,"language":95,"meta":96,"style":96},"int(\"hello\")\n",[17,3494,3495],{"__ignoreMap":96},[100,3496,3497,3500,3502,3504,3507,3509],{"class":102,"line":103},[100,3498,3499],{"class":191},"int",[100,3501,170],{"class":118},[100,3503,206],{"class":205},[100,3505,3506],{"class":209},"hello",[100,3508,206],{"class":205},[100,3510,215],{"class":118},[14,3512,3513,3514,3516,3517,3520],{},"This raises ",[17,3515,3483],{}," because ",[17,3518,3519],{},"\"hello\""," cannot be converted to an integer.",[675,3522,3524],{"id":3523},"assertionerror-vs-typeerror","AssertionError vs TypeError",[40,3526,3527,3534],{},[43,3528,3529,3531,3532],{},[17,3530,1642],{}," comes from a failed ",[17,3533,1645],{},[43,3535,3536,3539],{},[17,3537,3538],{},"TypeError"," happens when an operation uses the wrong type",[14,3541,3487,3542,89],{},[17,3543,3538],{},[91,3545,3547],{"className":93,"code":3546,"language":95,"meta":96,"style":96},"\"5\" + 2\n",[17,3548,3549],{"__ignoreMap":96},[100,3550,3551,3554,3558,3560,3562],{"class":102,"line":103},[100,3552,206],{"class":3553},"s2W-s",[100,3555,3557],{"class":3556},"sithA","5",[100,3559,206],{"class":3553},[100,3561,2895],{"class":110},[100,3563,3564],{"class":122}," 2\n",[14,3566,3567],{},"This fails because Python cannot add a string and an integer together.",[675,3569,3571],{"id":3570},"assertionerror-vs-syntax-errors","AssertionError vs syntax errors",[14,3573,3574],{},"A syntax error means the code itself is written incorrectly.",[14,3576,1844],{},[91,3578,3580],{"className":93,"code":3579,"language":95,"meta":96,"style":96},"if True\n    print(\"hello\")\n",[17,3581,3582,3589],{"__ignoreMap":96},[100,3583,3584,3586],{"class":102,"line":103},[100,3585,2736],{"class":145},[100,3587,3588],{"class":158}," True\n",[100,3590,3591,3593,3595,3597,3599,3601],{"class":102,"line":135},[100,3592,200],{"class":114},[100,3594,170],{"class":118},[100,3596,206],{"class":205},[100,3598,3506],{"class":209},[100,3600,206],{"class":205},[100,3602,215],{"class":118},[14,3604,3605,3606,3608],{},"That is different from ",[17,3607,1642],{},", which happens when the code runs and a condition is false.",[77,3610,1514],{"id":1513},[675,3612,3614],{"id":3613},"what-causes-assertionerror-in-python","What causes AssertionError in Python?",[14,3616,3617,3618,1646,3620,290],{},"It happens when an ",[17,3619,1645],{},[17,3621,1649],{},[675,3623,3625],{"id":3624},"how-do-i-fix-assertionerror","How do I fix AssertionError?",[14,3627,3628,3629,3631],{},"Check the ",[17,3630,1645],{}," condition, inspect the values used in it, and make sure your program data matches what the code expects.",[675,3633,3635],{"id":3634},"should-i-use-assert-for-user-input-validation","Should I use assert for user input validation?",[14,3637,3638,3639,3641,3642,3644],{},"Usually no. For user input, use normal checks with ",[17,3640,2736],{}," statements and raise exceptions like ",[17,3643,3483],{}," when needed.",[675,3646,3648],{"id":3647},"what-is-the-difference-between-assertionerror-and-valueerror","What is the difference between AssertionError and ValueError?",[14,3650,3651,3653,3654,3656],{},[17,3652,1642],{}," means an internal check failed. ",[17,3655,3483],{}," usually means a value is the right type but not acceptable.",[77,3658,1554],{"id":1553},[40,3660,3661,3665,3669,3674,3678],{},[43,3662,3663],{},[295,3664,1587],{"href":1749},[43,3666,3667],{},[295,3668,1581],{"href":2824},[43,3670,3671],{},[295,3672,3673],{"href":2752},"How to raise an exception in Python",[43,3675,3676],{},[295,3677,3265],{"href":3264},[43,3679,3680],{},[295,3681,3270],{"href":3269},[1589,3683,3684],{},"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 .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 .sjJ54, html code.shiki .sjJ54{--shiki-light:#39ADB5;--shiki-default:#032F62;--shiki-dark:#9ECBFF}html pre.shiki code .sP7_E, html code.shiki .sP7_E{--shiki-light:#39ADB5;--shiki-default:#24292E;--shiki-dark:#E1E4E8}html pre.shiki code .s_sjI, html code.shiki .s_sjI{--shiki-light:#91B859;--shiki-default:#032F62;--shiki-dark:#9ECBFF}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 .slqww, html code.shiki .slqww{--shiki-light:#6182B8;--shiki-default:#24292E;--shiki-dark:#E1E4E8}html pre.shiki code .sZMiF, html code.shiki .sZMiF{--shiki-light:#E2931D;--shiki-default:#005CC5;--shiki-dark:#79B8FF}html pre.shiki code .sptTA, html code.shiki .sptTA{--shiki-light:#6182B8;--shiki-default:#005CC5;--shiki-dark:#79B8FF}html pre.shiki code .s39Yj, html code.shiki .s39Yj{--shiki-light:#39ADB5;--shiki-default:#005CC5;--shiki-dark:#79B8FF}html pre.shiki code .s_hVV, html code.shiki .s_hVV{--shiki-light:#90A4AE;--shiki-default:#005CC5;--shiki-dark:#79B8FF}html pre.shiki code .skxfh, html code.shiki .skxfh{--shiki-light:#E53935;--shiki-default:#24292E;--shiki-dark:#E1E4E8}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 .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 .s2W-s, html code.shiki .s2W-s{--shiki-light:#39ADB5;--shiki-light-font-style:italic;--shiki-default:#032F62;--shiki-default-font-style:inherit;--shiki-dark:#9ECBFF;--shiki-dark-font-style:inherit}html pre.shiki code .sithA, html code.shiki .sithA{--shiki-light:#90A4AE;--shiki-light-font-style:italic;--shiki-default:#032F62;--shiki-default-font-style:inherit;--shiki-dark:#9ECBFF;--shiki-dark-font-style:inherit}",{"title":96,"searchDepth":135,"depth":135,"links":3686},[3687,3688,3689,3690,3691,3692,3700,3701,3707,3708,3713,3719],{"id":79,"depth":135,"text":80},{"id":1752,"depth":135,"text":1753},{"id":1795,"depth":135,"text":1796},{"id":1887,"depth":135,"text":1888},{"id":1343,"depth":135,"text":1344},{"id":2228,"depth":135,"text":2229,"children":3693},[3694,3695,3696,3697,3698],{"id":2235,"depth":142,"text":2236},{"id":2324,"depth":142,"text":2325},{"id":2498,"depth":142,"text":2499},{"id":2669,"depth":142,"text":2670},{"id":2732,"depth":142,"text":3699},"5. Use if and raise a normal exception for user input",{"id":2827,"depth":135,"text":2828},{"id":3056,"depth":135,"text":3057,"children":3702},[3703,3705],{"id":3069,"depth":142,"text":3704},"Use assert when:",{"id":3174,"depth":142,"text":3706},"Use raise ValueError or raise TypeError when:",{"id":3273,"depth":135,"text":3274},{"id":3460,"depth":135,"text":3461,"children":3709},[3710,3711,3712],{"id":3470,"depth":142,"text":3471},{"id":3523,"depth":142,"text":3524},{"id":3570,"depth":142,"text":3571},{"id":1513,"depth":135,"text":1514,"children":3714},[3715,3716,3717,3718],{"id":3613,"depth":142,"text":3614},{"id":3624,"depth":142,"text":3625},{"id":3634,"depth":142,"text":3635},{"id":3647,"depth":142,"text":3648},{"id":1553,"depth":135,"text":1554},"Master assertionerror in python causes and fixes in our comprehensive Python beginner guide.",{},"\u002Ferrors\u002Fassertionerror-in-python-causes-and-fixes",{"title":1632,"description":3720},"errors\u002Fassertionerror-in-python-causes-and-fixes","u239u237fgAROOmmI99PjiJg0Y6ZX4t4e-cpU8Wy2T0",{"id":3727,"title":3728,"body":3729,"description":5714,"extension":1623,"meta":5715,"navigation":138,"path":5716,"seo":5717,"stem":5718,"__hash__":5719},"content\u002Ferrors\u002Fattributeerror-in-python-causes-and-fixes.md","AttributeError in Python: Causes and Fixes",{"type":7,"value":3730,"toc":5694},[3731,3734,3740,3743,3759,3766,3768,3771,3838,3847,3854,3858,3864,3867,3892,3895,3926,3929,3941,3945,3949,3969,3972,3994,3998,4001,4064,4066,4092,4095,4113,4124,4128,4131,4185,4187,4206,4209,4276,4278,4319,4322,4376,4378,4411,4414,4422,4433,4437,4440,4443,4476,4478,4505,4511,4543,4545,4554,4557,4566,4628,4630,4643,4647,4650,4679,4684,4686,4749,4751,4797,4799,4822,4825,4877,4879,4888,4891,4962,4971,4975,4978,4980,5076,5078,5147,5153,5156,5173,5176,5249,5258,5262,5267,5293,5296,5362,5364,5442,5445,5459,5464,5468,5471,5498,5501,5515,5518,5545,5549,5555,5576,5579,5593,5595,5599,5608,5612,5621,5625,5633,5637,5646,5648,5691],[10,3732,3728],{"id":3733},"attributeerror-in-python-causes-and-fixes",[14,3735,3736,3739],{},[17,3737,3738],{},"AttributeError"," happens when Python cannot find an attribute or method on an object.",[14,3741,3742],{},"This usually means one of these is true:",[40,3744,3745,3748,3751,3756],{},[43,3746,3747],{},"You used a method on the wrong type of value",[43,3749,3750],{},"You misspelled the attribute name",[43,3752,3753,3754],{},"Your variable is ",[17,3755,930],{},[43,3757,3758],{},"The variable changed to a different type earlier in the code",[14,3760,3761,3762,3765],{},"A common example is trying to use ",[17,3763,3764],{},".split()"," on a list instead of a string.",[77,3767,80],{"id":79},[14,3769,3770],{},"Start by checking what the variable really is:",[91,3772,3774],{"className":93,"code":3773,"language":95,"meta":96,"style":96},"value = [1, 2, 3]\n\nprint(type(value))\nprint(dir(value))\n\n# Check the method name and make sure the object type supports it\n",[17,3775,3776,3796,3800,3814,3829,3833],{"__ignoreMap":96},[100,3777,3778,3780,3782,3784,3786,3788,3790,3792,3794],{"class":102,"line":103},[100,3779,3318],{"class":106},[100,3781,111],{"class":110},[100,3783,594],{"class":118},[100,3785,123],{"class":122},[100,3787,126],{"class":118},[100,3789,129],{"class":122},[100,3791,126],{"class":118},[100,3793,1405],{"class":122},[100,3795,603],{"class":118},[100,3797,3798],{"class":102,"line":135},[100,3799,139],{"emptyLinePlaceholder":138},[100,3801,3802,3804,3806,3808,3810,3812],{"class":102,"line":142},[100,3803,372],{"class":114},[100,3805,170],{"class":118},[100,3807,1250],{"class":191},[100,3809,170],{"class":118},[100,3811,757],{"class":178},[100,3813,182],{"class":118},[100,3815,3816,3818,3820,3823,3825,3827],{"class":102,"line":152},[100,3817,372],{"class":114},[100,3819,170],{"class":118},[100,3821,3822],{"class":114},"dir",[100,3824,170],{"class":118},[100,3826,757],{"class":178},[100,3828,182],{"class":118},[100,3830,3831],{"class":102,"line":164},[100,3832,139],{"emptyLinePlaceholder":138},[100,3834,3835],{"class":102,"line":185},[100,3836,3837],{"class":414},"# Check the method name and make sure the object type supports it\n",[14,3839,3840,3841,3843,3844,3846],{},"Most ",[17,3842,3738],{}," problems happen because you called a method or attribute on the wrong type, used the wrong name, or got ",[17,3845,930],{}," instead of the object you expected.",[14,3848,3849,3850,290],{},"If you are new to debugging, this pairs well with a beginner guide on ",[295,3851,3853],{"href":3852},"\u002Fhow-to\u002Fhow-to-debug-python-code-beginner-guide\u002F","how to debug Python code",[77,3855,3857],{"id":3856},"what-attributeerror-means","What AttributeError means",[14,3859,3860,3861,3863],{},"Python raises ",[17,3862,3738],{}," when you try to use an attribute or method that an object does not have.",[14,3865,3866],{},"A few important points:",[40,3868,3869,3875,3881],{},[43,3870,3871,3872],{},"An attribute can be a value like ",[17,3873,3874],{},"user.name",[43,3876,3877,3878],{},"An attribute can also be a method like ",[17,3879,3880],{},"text.upper()",[43,3882,3883,3884],{},"The error message usually tells you:\n",[40,3885,3886,3889],{},[43,3887,3888],{},"the object type",[43,3890,3891],{},"the missing attribute name",[14,3893,3894],{},"Example error message:",[91,3896,3898],{"className":93,"code":3897,"language":95,"meta":96,"style":96},"AttributeError: 'list' object has no attribute 'split'\n",[17,3899,3900],{"__ignoreMap":96},[100,3901,3902,3904,3906,3908,3910,3912,3915,3918,3920,3923],{"class":102,"line":103},[100,3903,3738],{"class":191},[100,3905,89],{"class":118},[100,3907,1274],{"class":205},[100,3909,1235],{"class":209},[100,3911,1280],{"class":205},[100,3913,3914],{"class":191}," object",[100,3916,3917],{"class":106}," has no attribute ",[100,3919,1280],{"class":205},[100,3921,3922],{"class":209},"split",[100,3924,3925],{"class":205},"'\n",[14,3927,3928],{},"In that example:",[40,3930,3931,3936],{},[43,3932,3933,3934],{},"the object type is ",[17,3935,1235],{},[43,3937,3938,3939],{},"the missing attribute is ",[17,3940,3922],{},[77,3942,3944],{"id":3943},"why-this-error-happens","Why this error happens",[14,3946,3947,2137],{},[17,3948,3738],{},[40,3950,3951,3954,3957,3963,3966],{},[43,3952,3953],{},"You used a method that belongs to a different type",[43,3955,3956],{},"You made a spelling mistake in the attribute name",[43,3958,3959,3960,3962],{},"A variable contains ",[17,3961,930],{},", not the object you expected",[43,3964,3965],{},"You reassigned a variable to a new type by mistake",[43,3967,3968],{},"You expected a module, class, or object to have a name that does not exist",[14,3970,3971],{},"Common causes include:",[40,3973,3974,3977,3980,3983,3988,3991],{},[43,3975,3976],{},"Calling a string method on a list",[43,3978,3979],{},"Calling a list method on a string",[43,3981,3982],{},"Using the wrong capitalization in a method name",[43,3984,3985,3986],{},"Trying to access an attribute on ",[17,3987,930],{},[43,3989,3990],{},"Reassigning a variable to a different type",[43,3992,3993],{},"Using an attribute that does not exist on a module or custom object",[77,3995,3997],{"id":3996},"example-that-causes-attributeerror","Example that causes AttributeError",[14,3999,4000],{},"Here is a short example that fails:",[91,4002,4004],{"className":93,"code":4003,"language":95,"meta":96,"style":96},"values = [\"a\", \"b\", \"c\"]\nprint(values.split(\",\"))\n",[17,4005,4006,4041],{"__ignoreMap":96},[100,4007,4008,4011,4013,4015,4017,4019,4021,4023,4025,4028,4030,4032,4034,4037,4039],{"class":102,"line":103},[100,4009,4010],{"class":106},"values ",[100,4012,111],{"class":110},[100,4014,594],{"class":118},[100,4016,206],{"class":205},[100,4018,295],{"class":209},[100,4020,206],{"class":205},[100,4022,126],{"class":118},[100,4024,1708],{"class":205},[100,4026,4027],{"class":209},"b",[100,4029,206],{"class":205},[100,4031,126],{"class":118},[100,4033,1708],{"class":205},[100,4035,4036],{"class":209},"c",[100,4038,206],{"class":205},[100,4040,603],{"class":118},[100,4042,4043,4045,4047,4050,4052,4054,4056,4058,4060,4062],{"class":102,"line":135},[100,4044,372],{"class":114},[100,4046,170],{"class":118},[100,4048,4049],{"class":178},"values",[100,4051,290],{"class":118},[100,4053,3922],{"class":178},[100,4055,170],{"class":118},[100,4057,206],{"class":205},[100,4059,126],{"class":209},[100,4061,206],{"class":205},[100,4063,182],{"class":118},[14,4065,218],{},[91,4067,4068],{"className":93,"code":3897,"language":95,"meta":96,"style":96},[17,4069,4070],{"__ignoreMap":96},[100,4071,4072,4074,4076,4078,4080,4082,4084,4086,4088,4090],{"class":102,"line":103},[100,4073,3738],{"class":191},[100,4075,89],{"class":118},[100,4077,1274],{"class":205},[100,4079,1235],{"class":209},[100,4081,1280],{"class":205},[100,4083,3914],{"class":191},[100,4085,3917],{"class":106},[100,4087,1280],{"class":205},[100,4089,3922],{"class":209},[100,4091,3925],{"class":205},[14,4093,4094],{},"Why this happens:",[40,4096,4097,4102,4107],{},[43,4098,4099,4101],{},[17,4100,4049],{}," is a list",[43,4103,4104,4106],{},[17,4105,3764],{}," is a string method",[43,4108,4109,4110,4112],{},"Lists do not have a ",[17,4111,3764],{}," method",[14,4114,4115,4116,290],{},"If you are dealing with this exact error, see ",[295,4117,4119,4120,4123],{"href":4118},"\u002Ferrors\u002Fattributeerror-list-object-has-no-attribute-fix\u002F","how to fix ",[17,4121,4122],{},"'list' object has no attribute"," errors",[77,4125,4127],{"id":4126},"fix-1-use-the-correct-method-for-the-object-type","Fix 1: Use the correct method for the object type",[14,4129,4130],{},"First, check the type of the value:",[91,4132,4134],{"className":93,"code":4133,"language":95,"meta":96,"style":96},"value = [\"apple\", \"banana\", \"orange\"]\nprint(type(value))\n",[17,4135,4136,4171],{"__ignoreMap":96},[100,4137,4138,4140,4142,4144,4146,4149,4151,4153,4155,4158,4160,4162,4164,4167,4169],{"class":102,"line":103},[100,4139,3318],{"class":106},[100,4141,111],{"class":110},[100,4143,594],{"class":118},[100,4145,206],{"class":205},[100,4147,4148],{"class":209},"apple",[100,4150,206],{"class":205},[100,4152,126],{"class":118},[100,4154,1708],{"class":205},[100,4156,4157],{"class":209},"banana",[100,4159,206],{"class":205},[100,4161,126],{"class":118},[100,4163,1708],{"class":205},[100,4165,4166],{"class":209},"orange",[100,4168,206],{"class":205},[100,4170,603],{"class":118},[100,4172,4173,4175,4177,4179,4181,4183],{"class":102,"line":135},[100,4174,372],{"class":114},[100,4176,170],{"class":118},[100,4178,1250],{"class":191},[100,4180,170],{"class":118},[100,4182,757],{"class":178},[100,4184,182],{"class":118},[14,4186,218],{},[91,4188,4190],{"className":93,"code":4189,"language":95,"meta":96,"style":96},"\u003Cclass 'list'>\n",[17,4191,4192],{"__ignoreMap":96},[100,4193,4194,4196,4198,4200,4202,4204],{"class":102,"line":103},[100,4195,2782],{"class":110},[100,4197,3417],{"class":1077},[100,4199,1274],{"class":205},[100,4201,1235],{"class":209},[100,4203,1280],{"class":205},[100,4205,1980],{"class":110},[14,4207,4208],{},"Now use a method that belongs to a list, not a string:",[91,4210,4212],{"className":93,"code":4211,"language":95,"meta":96,"style":96},"value = [\"apple\", \"banana\", \"orange\"]\nvalue.append(\"grape\")\nprint(value)\n",[17,4213,4214,4246,4266],{"__ignoreMap":96},[100,4215,4216,4218,4220,4222,4224,4226,4228,4230,4232,4234,4236,4238,4240,4242,4244],{"class":102,"line":103},[100,4217,3318],{"class":106},[100,4219,111],{"class":110},[100,4221,594],{"class":118},[100,4223,206],{"class":205},[100,4225,4148],{"class":209},[100,4227,206],{"class":205},[100,4229,126],{"class":118},[100,4231,1708],{"class":205},[100,4233,4157],{"class":209},[100,4235,206],{"class":205},[100,4237,126],{"class":118},[100,4239,1708],{"class":205},[100,4241,4166],{"class":209},[100,4243,206],{"class":205},[100,4245,603],{"class":118},[100,4247,4248,4250,4252,4255,4257,4259,4262,4264],{"class":102,"line":135},[100,4249,757],{"class":106},[100,4251,290],{"class":118},[100,4253,4254],{"class":178},"append",[100,4256,170],{"class":118},[100,4258,206],{"class":205},[100,4260,4261],{"class":209},"grape",[100,4263,206],{"class":205},[100,4265,215],{"class":118},[100,4267,4268,4270,4272,4274],{"class":102,"line":142},[100,4269,372],{"class":114},[100,4271,170],{"class":118},[100,4273,757],{"class":178},[100,4275,215],{"class":118},[14,4277,218],{},[91,4279,4281],{"className":93,"code":4280,"language":95,"meta":96,"style":96},"['apple', 'banana', 'orange', 'grape']\n",[17,4282,4283],{"__ignoreMap":96},[100,4284,4285,4287,4289,4291,4293,4295,4297,4299,4301,4303,4305,4307,4309,4311,4313,4315,4317],{"class":102,"line":103},[100,4286,3166],{"class":118},[100,4288,1280],{"class":205},[100,4290,4148],{"class":209},[100,4292,1280],{"class":205},[100,4294,126],{"class":118},[100,4296,1274],{"class":205},[100,4298,4157],{"class":209},[100,4300,1280],{"class":205},[100,4302,126],{"class":118},[100,4304,1274],{"class":205},[100,4306,4166],{"class":209},[100,4308,1280],{"class":205},[100,4310,126],{"class":118},[100,4312,1274],{"class":205},[100,4314,4261],{"class":209},[100,4316,1280],{"class":205},[100,4318,603],{"class":118},[14,4320,4321],{},"If you actually need string behavior, use a string:",[91,4323,4325],{"className":93,"code":4324,"language":95,"meta":96,"style":96},"text = \"apple,banana,orange\"\nparts = text.split(\",\")\nprint(parts)\n",[17,4326,4327,4341,4365],{"__ignoreMap":96},[100,4328,4329,4332,4334,4336,4339],{"class":102,"line":103},[100,4330,4331],{"class":106},"text ",[100,4333,111],{"class":110},[100,4335,1708],{"class":205},[100,4337,4338],{"class":209},"apple,banana,orange",[100,4340,1714],{"class":205},[100,4342,4343,4346,4348,4351,4353,4355,4357,4359,4361,4363],{"class":102,"line":135},[100,4344,4345],{"class":106},"parts ",[100,4347,111],{"class":110},[100,4349,4350],{"class":106}," text",[100,4352,290],{"class":118},[100,4354,3922],{"class":178},[100,4356,170],{"class":118},[100,4358,206],{"class":205},[100,4360,126],{"class":209},[100,4362,206],{"class":205},[100,4364,215],{"class":118},[100,4366,4367,4369,4371,4374],{"class":102,"line":142},[100,4368,372],{"class":114},[100,4370,170],{"class":118},[100,4372,4373],{"class":178},"parts",[100,4375,215],{"class":118},[14,4377,218],{},[91,4379,4381],{"className":93,"code":4380,"language":95,"meta":96,"style":96},"['apple', 'banana', 'orange']\n",[17,4382,4383],{"__ignoreMap":96},[100,4384,4385,4387,4389,4391,4393,4395,4397,4399,4401,4403,4405,4407,4409],{"class":102,"line":103},[100,4386,3166],{"class":118},[100,4388,1280],{"class":205},[100,4390,4148],{"class":209},[100,4392,1280],{"class":205},[100,4394,126],{"class":118},[100,4396,1274],{"class":205},[100,4398,4157],{"class":209},[100,4400,1280],{"class":205},[100,4402,126],{"class":118},[100,4404,1274],{"class":205},[100,4406,4166],{"class":209},[100,4408,1280],{"class":205},[100,4410,603],{"class":118},[14,4412,4413],{},"The key idea is simple:",[40,4415,4416,4419],{},[43,4417,4418],{},"If the value is a string, use string methods",[43,4420,4421],{},"If the value is a list, use list methods",[14,4423,4424,4425,4432],{},"You can also use ",[295,4426,4428,4431],{"href":4427},"\u002Freference\u002Fpython-type-function-explained\u002F",[17,4429,4430],{},"type()"," in Python"," to confirm what kind of object you have.",[77,4434,4436],{"id":4435},"fix-2-check-for-spelling-mistakes","Fix 2: Check for spelling mistakes",[14,4438,4439],{},"Attribute names are case-sensitive.",[14,4441,4442],{},"For example, this is wrong:",[91,4444,4446],{"className":93,"code":4445,"language":95,"meta":96,"style":96},"text = \"hello\"\nprint(text.Upper())\n",[17,4447,4448,4460],{"__ignoreMap":96},[100,4449,4450,4452,4454,4456,4458],{"class":102,"line":103},[100,4451,4331],{"class":106},[100,4453,111],{"class":110},[100,4455,1708],{"class":205},[100,4457,3506],{"class":209},[100,4459,1714],{"class":205},[100,4461,4462,4464,4466,4469,4471,4474],{"class":102,"line":135},[100,4463,372],{"class":114},[100,4465,170],{"class":118},[100,4467,4468],{"class":178},"text",[100,4470,290],{"class":118},[100,4472,4473],{"class":178},"Upper",[100,4475,3370],{"class":118},[14,4477,218],{},[91,4479,4481],{"className":93,"code":4480,"language":95,"meta":96,"style":96},"AttributeError: 'str' object has no attribute 'Upper'\n",[17,4482,4483],{"__ignoreMap":96},[100,4484,4485,4487,4489,4491,4493,4495,4497,4499,4501,4503],{"class":102,"line":103},[100,4486,3738],{"class":191},[100,4488,89],{"class":118},[100,4490,1274],{"class":205},[100,4492,3422],{"class":209},[100,4494,1280],{"class":205},[100,4496,3914],{"class":191},[100,4498,3917],{"class":106},[100,4500,1280],{"class":205},[100,4502,4473],{"class":209},[100,4504,3925],{"class":205},[14,4506,4507,4508,89],{},"The correct method name is ",[17,4509,4510],{},".upper()",[91,4512,4514],{"className":93,"code":4513,"language":95,"meta":96,"style":96},"text = \"hello\"\nprint(text.upper())\n",[17,4515,4516,4528],{"__ignoreMap":96},[100,4517,4518,4520,4522,4524,4526],{"class":102,"line":103},[100,4519,4331],{"class":106},[100,4521,111],{"class":110},[100,4523,1708],{"class":205},[100,4525,3506],{"class":209},[100,4527,1714],{"class":205},[100,4529,4530,4532,4534,4536,4538,4541],{"class":102,"line":135},[100,4531,372],{"class":114},[100,4533,170],{"class":118},[100,4535,4468],{"class":178},[100,4537,290],{"class":118},[100,4539,4540],{"class":178},"upper",[100,4542,3370],{"class":118},[14,4544,218],{},[91,4546,4548],{"className":93,"code":4547,"language":95,"meta":96,"style":96},"HELLO\n",[17,4549,4550],{"__ignoreMap":96},[100,4551,4552],{"class":102,"line":103},[100,4553,4547],{"class":527},[14,4555,4556],{},"Small typos often cause this error.",[14,4558,4559,4560,89],{},"To inspect what names an object supports, use ",[295,4561,4563],{"href":4562},"\u002Freference\u002Fpython-dir-function-explained\u002F",[17,4564,4565],{},"dir()",[91,4567,4569],{"className":93,"code":4568,"language":95,"meta":96,"style":96},"text = \"hello\"\nprint(\"upper\" in dir(text))\nprint(\"Upper\" in dir(text))\n",[17,4570,4571,4583,4606],{"__ignoreMap":96},[100,4572,4573,4575,4577,4579,4581],{"class":102,"line":103},[100,4574,4331],{"class":106},[100,4576,111],{"class":110},[100,4578,1708],{"class":205},[100,4580,3506],{"class":209},[100,4582,1714],{"class":205},[100,4584,4585,4587,4589,4591,4593,4595,4597,4600,4602,4604],{"class":102,"line":135},[100,4586,372],{"class":114},[100,4588,170],{"class":118},[100,4590,206],{"class":205},[100,4592,4540],{"class":209},[100,4594,206],{"class":205},[100,4596,1971],{"class":145},[100,4598,4599],{"class":114}," dir",[100,4601,170],{"class":118},[100,4603,4468],{"class":178},[100,4605,182],{"class":118},[100,4607,4608,4610,4612,4614,4616,4618,4620,4622,4624,4626],{"class":102,"line":142},[100,4609,372],{"class":114},[100,4611,170],{"class":118},[100,4613,206],{"class":205},[100,4615,4473],{"class":209},[100,4617,206],{"class":205},[100,4619,1971],{"class":145},[100,4621,4599],{"class":114},[100,4623,170],{"class":118},[100,4625,4468],{"class":178},[100,4627,182],{"class":118},[14,4629,218],{},[91,4631,4633],{"className":93,"code":4632,"language":95,"meta":96,"style":96},"True\nFalse\n",[17,4634,4635,4639],{"__ignoreMap":96},[100,4636,4637],{"class":102,"line":103},[100,4638,3431],{"class":158},[100,4640,4641],{"class":102,"line":135},[100,4642,2407],{"class":158},[77,4644,4646],{"id":4645},"fix-3-watch-for-none-values","Fix 3: Watch for None values",[14,4648,4649],{},"A very common version of this error is:",[91,4651,4653],{"className":93,"code":4652,"language":95,"meta":96,"style":96},"AttributeError: 'NoneType' object has no attribute 'something'\n",[17,4654,4655],{"__ignoreMap":96},[100,4656,4657,4659,4661,4663,4666,4668,4670,4672,4674,4677],{"class":102,"line":103},[100,4658,3738],{"class":191},[100,4660,89],{"class":118},[100,4662,1274],{"class":205},[100,4664,4665],{"class":209},"NoneType",[100,4667,1280],{"class":205},[100,4669,3914],{"class":191},[100,4671,3917],{"class":106},[100,4673,1280],{"class":205},[100,4675,4676],{"class":209},"something",[100,4678,3925],{"class":205},[14,4680,4681,4682,290],{},"This means your variable is ",[17,4683,930],{},[14,4685,1844],{},[91,4687,4689],{"className":93,"code":4688,"language":95,"meta":96,"style":96},"def get_name():\n    print(\"Running function\")\n    # No return statement\n\nname = get_name()\nprint(name.upper())\n",[17,4690,4691,4700,4715,4720,4724,4735],{"__ignoreMap":96},[100,4692,4693,4695,4698],{"class":102,"line":103},[100,4694,1078],{"class":1077},[100,4696,4697],{"class":1081}," get_name",[100,4699,1085],{"class":118},[100,4701,4702,4704,4706,4708,4711,4713],{"class":102,"line":135},[100,4703,200],{"class":114},[100,4705,170],{"class":118},[100,4707,206],{"class":205},[100,4709,4710],{"class":209},"Running function",[100,4712,206],{"class":205},[100,4714,215],{"class":118},[100,4716,4717],{"class":102,"line":142},[100,4718,4719],{"class":414},"    # No return statement\n",[100,4721,4722],{"class":102,"line":152},[100,4723,139],{"emptyLinePlaceholder":138},[100,4725,4726,4728,4730,4732],{"class":102,"line":164},[100,4727,1691],{"class":106},[100,4729,111],{"class":110},[100,4731,4697],{"class":178},[100,4733,4734],{"class":118},"()\n",[100,4736,4737,4739,4741,4743,4745,4747],{"class":102,"line":185},[100,4738,372],{"class":114},[100,4740,170],{"class":118},[100,4742,2853],{"class":178},[100,4744,290],{"class":118},[100,4746,4540],{"class":178},[100,4748,3370],{"class":118},[14,4750,218],{},[91,4752,4754],{"className":93,"code":4753,"language":95,"meta":96,"style":96},"Running function\nTraceback (most recent call last):\n  ...\nAttributeError: 'NoneType' object has no attribute 'upper'\n",[17,4755,4756,4761,4771,4775],{"__ignoreMap":96},[100,4757,4758],{"class":102,"line":103},[100,4759,4760],{"class":106},"Running function\n",[100,4762,4763,4765,4767,4769],{"class":102,"line":135},[100,4764,514],{"class":178},[100,4766,170],{"class":118},[100,4768,519],{"class":178},[100,4770,522],{"class":118},[100,4772,4773],{"class":102,"line":142},[100,4774,528],{"class":527},[100,4776,4777,4779,4781,4783,4785,4787,4789,4791,4793,4795],{"class":102,"line":152},[100,4778,3738],{"class":191},[100,4780,89],{"class":118},[100,4782,1274],{"class":205},[100,4784,4665],{"class":209},[100,4786,1280],{"class":205},[100,4788,3914],{"class":191},[100,4790,3917],{"class":106},[100,4792,1280],{"class":205},[100,4794,4540],{"class":209},[100,4796,3925],{"class":205},[14,4798,4094],{},[40,4800,4801,4807,4815],{},[43,4802,4803,4806],{},[17,4804,4805],{},"get_name()"," does not return anything",[43,4808,4809,4810,4812,4813],{},"A function with no ",[17,4811,1053],{}," gives ",[17,4814,930],{},[43,4816,4817,4819,4820,4112],{},[17,4818,930],{}," does not have an ",[17,4821,4510],{},[14,4823,4824],{},"One fix is to return a value:",[91,4826,4828],{"className":93,"code":4827,"language":95,"meta":96,"style":96},"def get_name():\n    return \"sam\"\n\nname = get_name()\nprint(name.upper())\n",[17,4829,4830,4838,4849,4853,4863],{"__ignoreMap":96},[100,4831,4832,4834,4836],{"class":102,"line":103},[100,4833,1078],{"class":1077},[100,4835,4697],{"class":1081},[100,4837,1085],{"class":118},[100,4839,4840,4842,4844,4847],{"class":102,"line":135},[100,4841,2552],{"class":145},[100,4843,1708],{"class":205},[100,4845,4846],{"class":209},"sam",[100,4848,1714],{"class":205},[100,4850,4851],{"class":102,"line":142},[100,4852,139],{"emptyLinePlaceholder":138},[100,4854,4855,4857,4859,4861],{"class":102,"line":152},[100,4856,1691],{"class":106},[100,4858,111],{"class":110},[100,4860,4697],{"class":178},[100,4862,4734],{"class":118},[100,4864,4865,4867,4869,4871,4873,4875],{"class":102,"line":164},[100,4866,372],{"class":114},[100,4868,170],{"class":118},[100,4870,2853],{"class":178},[100,4872,290],{"class":118},[100,4874,4540],{"class":178},[100,4876,3370],{"class":118},[14,4878,218],{},[91,4880,4882],{"className":93,"code":4881,"language":95,"meta":96,"style":96},"SAM\n",[17,4883,4884],{"__ignoreMap":96},[100,4885,4886],{"class":102,"line":103},[100,4887,4881],{"class":527},[14,4889,4890],{},"Another fix is to check before using the value:",[91,4892,4894],{"className":93,"code":4893,"language":95,"meta":96,"style":96},"name = None\n\nif name is not None:\n    print(name.upper())\nelse:\n    print(\"name is None\")\n",[17,4895,4896,4905,4909,4926,4940,4947],{"__ignoreMap":96},[100,4897,4898,4900,4902],{"class":102,"line":103},[100,4899,1691],{"class":106},[100,4901,111],{"class":110},[100,4903,4904],{"class":158}," None\n",[100,4906,4907],{"class":102,"line":135},[100,4908,139],{"emptyLinePlaceholder":138},[100,4910,4911,4913,4916,4919,4922,4924],{"class":102,"line":142},[100,4912,2736],{"class":145},[100,4914,4915],{"class":106}," name ",[100,4917,4918],{"class":110},"is",[100,4920,4921],{"class":110}," not",[100,4923,866],{"class":158},[100,4925,149],{"class":118},[100,4927,4928,4930,4932,4934,4936,4938],{"class":102,"line":152},[100,4929,200],{"class":114},[100,4931,170],{"class":118},[100,4933,2853],{"class":178},[100,4935,290],{"class":118},[100,4937,4540],{"class":178},[100,4939,3370],{"class":118},[100,4941,4942,4945],{"class":102,"line":164},[100,4943,4944],{"class":145},"else",[100,4946,149],{"class":118},[100,4948,4949,4951,4953,4955,4958,4960],{"class":102,"line":185},[100,4950,200],{"class":114},[100,4952,170],{"class":118},[100,4954,206],{"class":205},[100,4956,4957],{"class":209},"name is None",[100,4959,206],{"class":205},[100,4961,215],{"class":118},[14,4963,4964,4965,290],{},"If this is your exact problem, see ",[295,4966,4119,4968,4123],{"href":4967},"\u002Ferrors\u002Fattributeerror-nonetype-object-has-no-attribute-fix\u002F",[17,4969,4970],{},"'NoneType' object has no attribute",[77,4972,4974],{"id":4973},"fix-4-trace-where-the-variable-changed","Fix 4: Trace where the variable changed",[14,4976,4977],{},"Sometimes a variable starts as one type and later becomes another type.",[14,4979,1844],{},[91,4981,4983],{"className":93,"code":4982,"language":95,"meta":96,"style":96},"value = \"1,2,3\"\nprint(type(value))\n\nvalue = [1, 2, 3]\nprint(type(value))\n\nprint(value.split(\",\"))\n",[17,4984,4985,4998,5012,5016,5036,5050,5054],{"__ignoreMap":96},[100,4986,4987,4989,4991,4993,4996],{"class":102,"line":103},[100,4988,3318],{"class":106},[100,4990,111],{"class":110},[100,4992,1708],{"class":205},[100,4994,4995],{"class":209},"1,2,3",[100,4997,1714],{"class":205},[100,4999,5000,5002,5004,5006,5008,5010],{"class":102,"line":135},[100,5001,372],{"class":114},[100,5003,170],{"class":118},[100,5005,1250],{"class":191},[100,5007,170],{"class":118},[100,5009,757],{"class":178},[100,5011,182],{"class":118},[100,5013,5014],{"class":102,"line":142},[100,5015,139],{"emptyLinePlaceholder":138},[100,5017,5018,5020,5022,5024,5026,5028,5030,5032,5034],{"class":102,"line":152},[100,5019,3318],{"class":106},[100,5021,111],{"class":110},[100,5023,594],{"class":118},[100,5025,123],{"class":122},[100,5027,126],{"class":118},[100,5029,129],{"class":122},[100,5031,126],{"class":118},[100,5033,1405],{"class":122},[100,5035,603],{"class":118},[100,5037,5038,5040,5042,5044,5046,5048],{"class":102,"line":164},[100,5039,372],{"class":114},[100,5041,170],{"class":118},[100,5043,1250],{"class":191},[100,5045,170],{"class":118},[100,5047,757],{"class":178},[100,5049,182],{"class":118},[100,5051,5052],{"class":102,"line":185},[100,5053,139],{"emptyLinePlaceholder":138},[100,5055,5056,5058,5060,5062,5064,5066,5068,5070,5072,5074],{"class":102,"line":197},[100,5057,372],{"class":114},[100,5059,170],{"class":118},[100,5061,757],{"class":178},[100,5063,290],{"class":118},[100,5065,3922],{"class":178},[100,5067,170],{"class":118},[100,5069,206],{"class":205},[100,5071,126],{"class":209},[100,5073,206],{"class":205},[100,5075,182],{"class":118},[14,5077,218],{},[91,5079,5081],{"className":93,"code":5080,"language":95,"meta":96,"style":96},"\u003Cclass 'str'>\n\u003Cclass 'list'>\nTraceback (most recent call last):\n  ...\nAttributeError: 'list' object has no attribute 'split'\n",[17,5082,5083,5097,5111,5121,5125],{"__ignoreMap":96},[100,5084,5085,5087,5089,5091,5093,5095],{"class":102,"line":103},[100,5086,2782],{"class":110},[100,5088,3417],{"class":1077},[100,5090,1274],{"class":205},[100,5092,3422],{"class":209},[100,5094,1280],{"class":205},[100,5096,1980],{"class":110},[100,5098,5099,5101,5103,5105,5107,5109],{"class":102,"line":135},[100,5100,2782],{"class":110},[100,5102,3417],{"class":1077},[100,5104,1274],{"class":205},[100,5106,1235],{"class":209},[100,5108,1280],{"class":205},[100,5110,1980],{"class":110},[100,5112,5113,5115,5117,5119],{"class":102,"line":142},[100,5114,514],{"class":178},[100,5116,170],{"class":118},[100,5118,519],{"class":178},[100,5120,522],{"class":118},[100,5122,5123],{"class":102,"line":152},[100,5124,528],{"class":527},[100,5126,5127,5129,5131,5133,5135,5137,5139,5141,5143,5145],{"class":102,"line":164},[100,5128,3738],{"class":191},[100,5130,89],{"class":118},[100,5132,1274],{"class":205},[100,5134,1235],{"class":209},[100,5136,1280],{"class":205},[100,5138,3914],{"class":191},[100,5140,3917],{"class":106},[100,5142,1280],{"class":205},[100,5144,3922],{"class":209},[100,5146,3925],{"class":205},[14,5148,5149,5150,5152],{},"The problem is not only the last line. The real problem is that ",[17,5151,757],{}," changed from a string to a list.",[14,5154,5155],{},"To debug this:",[40,5157,5158,5161,5167,5170],{},[43,5159,5160],{},"Print the value before the failing line",[43,5162,5163,5164],{},"Print ",[17,5165,5166],{},"type(value)",[43,5168,5169],{},"Look for earlier reassignment",[43,5171,5172],{},"Use clearer variable names",[14,5174,5175],{},"For example, this is easier to understand:",[91,5177,5179],{"className":93,"code":5178,"language":95,"meta":96,"style":96},"text = \"1,2,3\"\nnumbers = [1, 2, 3]\n\nprint(text.split(\",\"))\nprint(numbers)\n",[17,5180,5181,5193,5213,5217,5239],{"__ignoreMap":96},[100,5182,5183,5185,5187,5189,5191],{"class":102,"line":103},[100,5184,4331],{"class":106},[100,5186,111],{"class":110},[100,5188,1708],{"class":205},[100,5190,4995],{"class":209},[100,5192,1714],{"class":205},[100,5194,5195,5197,5199,5201,5203,5205,5207,5209,5211],{"class":102,"line":135},[100,5196,107],{"class":106},[100,5198,111],{"class":110},[100,5200,594],{"class":118},[100,5202,123],{"class":122},[100,5204,126],{"class":118},[100,5206,129],{"class":122},[100,5208,126],{"class":118},[100,5210,1405],{"class":122},[100,5212,603],{"class":118},[100,5214,5215],{"class":102,"line":142},[100,5216,139],{"emptyLinePlaceholder":138},[100,5218,5219,5221,5223,5225,5227,5229,5231,5233,5235,5237],{"class":102,"line":152},[100,5220,372],{"class":114},[100,5222,170],{"class":118},[100,5224,4468],{"class":178},[100,5226,290],{"class":118},[100,5228,3922],{"class":178},[100,5230,170],{"class":118},[100,5232,206],{"class":205},[100,5234,126],{"class":209},[100,5236,206],{"class":205},[100,5238,182],{"class":118},[100,5240,5241,5243,5245,5247],{"class":102,"line":164},[100,5242,372],{"class":114},[100,5244,170],{"class":118},[100,5246,179],{"class":178},[100,5248,215],{"class":118},[14,5250,4424,5251,5257],{},[295,5252,5254,4431],{"href":5253},"\u002Freference\u002Fpython-isinstance-function-explained\u002F",[17,5255,5256],{},"isinstance()"," when you need to check types during debugging.",[77,5259,5261],{"id":5260},"how-to-debug-attributeerror-step-by-step","How to debug AttributeError step by step",[14,5263,3277,5264,5266],{},[17,5265,3738],{},", follow this process:",[3282,5268,5269,5272,5275,5278,5284,5290],{},[43,5270,5271],{},"Read the full error message carefully",[43,5273,5274],{},"Identify the object type named in the message",[43,5276,5277],{},"Identify the missing attribute name",[43,5279,5280,5281],{},"Print the variable and ",[17,5282,5283],{},"type(variable)",[43,5285,241,5286,5289],{},[17,5287,5288],{},"dir(variable)"," to see available attributes",[43,5291,5292],{},"Check the documentation or your class definition if needed",[14,5294,5295],{},"Useful debugging commands:",[91,5297,5299],{"className":93,"code":5298,"language":95,"meta":96,"style":96},"print(value)\nprint(type(value))\nprint(dir(value))\nprint(hasattr(value, \"attribute_name\"))\n",[17,5300,5301,5311,5325,5339],{"__ignoreMap":96},[100,5302,5303,5305,5307,5309],{"class":102,"line":103},[100,5304,372],{"class":114},[100,5306,170],{"class":118},[100,5308,757],{"class":178},[100,5310,215],{"class":118},[100,5312,5313,5315,5317,5319,5321,5323],{"class":102,"line":135},[100,5314,372],{"class":114},[100,5316,170],{"class":118},[100,5318,1250],{"class":191},[100,5320,170],{"class":118},[100,5322,757],{"class":178},[100,5324,182],{"class":118},[100,5326,5327,5329,5331,5333,5335,5337],{"class":102,"line":142},[100,5328,372],{"class":114},[100,5330,170],{"class":118},[100,5332,3822],{"class":114},[100,5334,170],{"class":118},[100,5336,757],{"class":178},[100,5338,182],{"class":118},[100,5340,5341,5343,5345,5347,5349,5351,5353,5355,5358,5360],{"class":102,"line":152},[100,5342,372],{"class":114},[100,5344,170],{"class":118},[100,5346,1265],{"class":114},[100,5348,170],{"class":118},[100,5350,757],{"class":178},[100,5352,126],{"class":118},[100,5354,1708],{"class":205},[100,5356,5357],{"class":209},"attribute_name",[100,5359,206],{"class":205},[100,5361,182],{"class":118},[14,5363,1844],{},[91,5365,5367],{"className":93,"code":5366,"language":95,"meta":96,"style":96},"value = 100\n\nprint(value)\nprint(type(value))\nprint(hasattr(value, \"append\"))\nprint(dir(value))\n",[17,5368,5369,5378,5382,5392,5406,5428],{"__ignoreMap":96},[100,5370,5371,5373,5375],{"class":102,"line":103},[100,5372,3318],{"class":106},[100,5374,111],{"class":110},[100,5376,5377],{"class":122}," 100\n",[100,5379,5380],{"class":102,"line":135},[100,5381,139],{"emptyLinePlaceholder":138},[100,5383,5384,5386,5388,5390],{"class":102,"line":142},[100,5385,372],{"class":114},[100,5387,170],{"class":118},[100,5389,757],{"class":178},[100,5391,215],{"class":118},[100,5393,5394,5396,5398,5400,5402,5404],{"class":102,"line":152},[100,5395,372],{"class":114},[100,5397,170],{"class":118},[100,5399,1250],{"class":191},[100,5401,170],{"class":118},[100,5403,757],{"class":178},[100,5405,182],{"class":118},[100,5407,5408,5410,5412,5414,5416,5418,5420,5422,5424,5426],{"class":102,"line":164},[100,5409,372],{"class":114},[100,5411,170],{"class":118},[100,5413,1265],{"class":114},[100,5415,170],{"class":118},[100,5417,757],{"class":178},[100,5419,126],{"class":118},[100,5421,1708],{"class":205},[100,5423,4254],{"class":209},[100,5425,206],{"class":205},[100,5427,182],{"class":118},[100,5429,5430,5432,5434,5436,5438,5440],{"class":102,"line":185},[100,5431,372],{"class":114},[100,5433,170],{"class":118},[100,5435,3822],{"class":114},[100,5437,170],{"class":118},[100,5439,757],{"class":178},[100,5441,182],{"class":118},[14,5443,5444],{},"This helps you answer questions like:",[40,5446,5447,5453,5456],{},[43,5448,5449,5450,5452],{},"Is ",[17,5451,757],{}," really the type I expected?",[43,5454,5455],{},"Does this object actually have the method I want?",[43,5457,5458],{},"Did I misspell the attribute?",[14,5460,5461,5462,290],{},"If you want a broader overview, read ",[295,5463,1587],{"href":1749},[77,5465,5467],{"id":5466},"common-attributeerror-examples","Common AttributeError examples",[14,5469,5470],{},"Here are some common forms of this error:",[40,5472,5473,5478,5483,5488,5493],{},[43,5474,5475],{},[17,5476,5477],{},"'list' object has no attribute ...",[43,5479,5480],{},[17,5481,5482],{},"'str' object has no attribute ...",[43,5484,5485],{},[17,5486,5487],{},"'NoneType' object has no attribute ...",[43,5489,5490],{},[17,5491,5492],{},"'int' object has no attribute ...",[43,5494,5495],{},[17,5496,5497],{},"module has no attribute ...",[14,5499,5500],{},"Examples:",[40,5502,5503,5506,5509,5512],{},[43,5504,5505],{},"A list used like a string",[43,5507,5508],{},"A string used like a list",[43,5510,5511],{},"An integer used like an object with string or list methods",[43,5513,5514],{},"A module name used with an attribute that does not exist",[14,5516,5517],{},"For object-specific help, see:",[40,5519,5520,5529,5537],{},[43,5521,5522],{},[295,5523,5525,5526,4123],{"href":5524},"\u002Ferrors\u002Fattributeerror-str-object-has-no-attribute-fix\u002F","Fix ",[17,5527,5528],{},"'str' object has no attribute",[43,5530,5531],{},[295,5532,5525,5534,4123],{"href":5533},"\u002Ferrors\u002Fattributeerror-int-object-has-no-attribute-fix\u002F",[17,5535,5536],{},"'int' object has no attribute",[43,5538,5539],{},[295,5540,5525,5542,4123],{"href":5541},"\u002Ferrors\u002Fattributeerror-module-has-no-attribute-fix\u002F",[17,5543,5544],{},"module has no attribute",[77,5546,5548],{"id":5547},"how-to-prevent-attributeerror","How to prevent AttributeError",[14,5550,5551,5552,5554],{},"You can prevent many ",[17,5553,3738],{}," problems with a few habits:",[40,5556,5557,5560,5563,5566,5573],{},[43,5558,5559],{},"Learn the basic methods for strings, lists, dictionaries, and sets",[43,5561,5562],{},"Check return values from functions",[43,5564,5565],{},"Avoid reusing one variable name for different types",[43,5567,241,5568,3266,5570,5572],{},[17,5569,4430],{},[17,5571,5256],{}," when debugging",[43,5574,5575],{},"Write small tests for important functions",[14,5577,5578],{},"Good habits make a big difference:",[40,5580,5581,5584,5587,5590],{},[43,5582,5583],{},"Use clear variable names",[43,5585,5586],{},"Print values when something looks wrong",[43,5588,5589],{},"Read the error message fully",[43,5591,5592],{},"Check method names carefully",[77,5594,1514],{"id":1513},[675,5596,5598],{"id":5597},"what-is-an-attribute-in-python","What is an attribute in Python?",[14,5600,5601,5602,5604,5605,290],{},"An attribute is a name attached to an object. It can be a stored value like ",[17,5603,3874],{}," or a method like ",[17,5606,5607],{},"text.lower()",[675,5609,5611],{"id":5610},"what-is-the-difference-between-attributeerror-and-nameerror","What is the difference between AttributeError and NameError?",[14,5613,5614,5617,5618,5620],{},[17,5615,5616],{},"NameError"," means a variable name does not exist. ",[17,5619,3738],{}," means the object exists, but the attribute or method does not.",[675,5622,5624],{"id":5623},"why-do-i-get-nonetype-object-has-no-attribute","Why do I get 'NoneType' object has no attribute?",[14,5626,3753,5627,5629,5630,5632],{},[17,5628,930],{},", and ",[17,5631,930],{}," does not have the attribute you tried to use. This often happens when a function returns nothing.",[675,5634,5636],{"id":5635},"how-do-i-see-what-attributes-an-object-has","How do I see what attributes an object has?",[14,5638,241,5639,5642,5643,290],{},[17,5640,5641],{},"dir(object)"," to list available attributes and methods. For a quick type check, use ",[17,5644,5645],{},"type(object)",[77,5647,1554],{"id":1553},[40,5649,5650,5656,5662,5668,5674,5680,5686],{},[43,5651,5652],{},[295,5653,5525,5654,4123],{"href":4118},[17,5655,4122],{},[43,5657,5658],{},[295,5659,5525,5660,4123],{"href":5524},[17,5661,5528],{},[43,5663,5664],{},[295,5665,5525,5666,4123],{"href":4967},[17,5667,4970],{},[43,5669,5670],{},[295,5671,1572,5672,1576],{"href":4427},[17,5673,4430],{},[43,5675,5676],{},[295,5677,1572,5678,1576],{"href":4562},[17,5679,4565],{},[43,5681,5682],{},[295,5683,1572,5684,1576],{"href":5253},[17,5685,5256],{},[43,5687,5688],{},[295,5689,5690],{"href":3852},"How to debug Python code",[1589,5692,5693],{},"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 .sP7_E, html code.shiki .sP7_E{--shiki-light:#39ADB5;--shiki-default:#24292E;--shiki-dark:#E1E4E8}html pre.shiki code .srdBf, html code.shiki .srdBf{--shiki-light:#F76D47;--shiki-default:#005CC5;--shiki-dark:#79B8FF}html pre.shiki code .sptTA, html code.shiki .sptTA{--shiki-light:#6182B8;--shiki-default:#005CC5;--shiki-dark:#79B8FF}html pre.shiki code .sZMiF, html code.shiki .sZMiF{--shiki-light:#E2931D;--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 .sbsja, html code.shiki .sbsja{--shiki-light:#9C3EDA;--shiki-default:#D73A49;--shiki-dark:#F97583}html pre.shiki code .s_hVV, html code.shiki .s_hVV{--shiki-light:#90A4AE;--shiki-default:#005CC5;--shiki-dark:#79B8FF}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 .s39Yj, html code.shiki .s39Yj{--shiki-light:#39ADB5;--shiki-default:#005CC5;--shiki-dark:#79B8FF}html pre.shiki code .sGLFI, html code.shiki .sGLFI{--shiki-light:#6182B8;--shiki-default:#6F42C1;--shiki-dark:#B392F0}",{"title":96,"searchDepth":135,"depth":135,"links":5695},[5696,5697,5698,5699,5700,5701,5702,5703,5704,5705,5706,5707,5713],{"id":79,"depth":135,"text":80},{"id":3856,"depth":135,"text":3857},{"id":3943,"depth":135,"text":3944},{"id":3996,"depth":135,"text":3997},{"id":4126,"depth":135,"text":4127},{"id":4435,"depth":135,"text":4436},{"id":4645,"depth":135,"text":4646},{"id":4973,"depth":135,"text":4974},{"id":5260,"depth":135,"text":5261},{"id":5466,"depth":135,"text":5467},{"id":5547,"depth":135,"text":5548},{"id":1513,"depth":135,"text":1514,"children":5708},[5709,5710,5711,5712],{"id":5597,"depth":142,"text":5598},{"id":5610,"depth":142,"text":5611},{"id":5623,"depth":142,"text":5624},{"id":5635,"depth":142,"text":5636},{"id":1553,"depth":135,"text":1554},"Master attributeerror in python causes and fixes in our comprehensive Python beginner guide.",{},"\u002Ferrors\u002Fattributeerror-in-python-causes-and-fixes",{"title":3728,"description":5714},"errors\u002Fattributeerror-in-python-causes-and-fixes","aLjSZXXVAn3op9drl4nLOFL7dvezSm3KH_mwwU1Sqik",{"id":5721,"title":5722,"body":5723,"description":7359,"extension":1623,"meta":7360,"navigation":138,"path":7361,"seo":7362,"stem":7363,"__hash__":7364},"content\u002Ferrors\u002Fattributeerror-int-object-has-no-attribute-fix.md","AttributeError: 'int' object has no attribute (Fix)",{"type":7,"value":5724,"toc":7335},[5725,5733,5740,5746,5748,5864,5878,5882,5885,5909,5915,5942,5946,5949,5976,5978,6005,6007,6029,6032,6063,6065,6074,6081,6083,6086,6124,6127,6159,6192,6241,6245,6248,6289,6291,6314,6317,6346,6361,6365,6368,6372,6400,6404,6445,6447,6465,6470,6511,6513,6521,6524,6553,6555,6564,6571,6575,6578,6582,6640,6642,6674,6680,6683,6792,6794,6856,6859,6918,6921,6925,6928,6954,6957,7037,7040,7072,7078,7082,7085,7088,7114,7118,7121,7148,7150,7192,7201,7207,7261,7263,7267,7270,7274,7277,7281,7287,7291,7296,7298,7332],[10,5726,5728,5729,5732],{"id":5727},"attributeerror-int-object-has-no-attribute-fix","AttributeError: ",[17,5730,5731],{},"'int'"," object has no attribute (Fix)",[14,5734,5735,5736,5739],{},"Fix the error ",[17,5737,5738],{},"AttributeError: 'int' object has no attribute"," by finding where a number is being used like a string, list, dictionary, or custom object.",[14,5741,5742,5743,5745],{},"This usually means a variable contains an ",[17,5744,3499],{},", but your code is trying to use a method or attribute that belongs to some other type.",[77,5747,80],{"id":79},[91,5749,5751],{"className":93,"code":5750,"language":95,"meta":96,"style":96},"value = 123\n\n# Wrong: integers do not have string methods\n# print(value.lower())\n\n# Fix 1: convert to string first\nprint(str(value).lower())\n\n# Fix 2: check the type before calling an attribute\nif isinstance(value, str):\n    print(value.lower())\nelse:\n    print(value)\n",[17,5752,5753,5762,5766,5771,5776,5780,5785,5805,5809,5814,5831,5846,5853],{"__ignoreMap":96},[100,5754,5755,5757,5759],{"class":102,"line":103},[100,5756,3318],{"class":106},[100,5758,111],{"class":110},[100,5760,5761],{"class":122}," 123\n",[100,5763,5764],{"class":102,"line":135},[100,5765,139],{"emptyLinePlaceholder":138},[100,5767,5768],{"class":102,"line":142},[100,5769,5770],{"class":414},"# Wrong: integers do not have string methods\n",[100,5772,5773],{"class":102,"line":152},[100,5774,5775],{"class":414},"# print(value.lower())\n",[100,5777,5778],{"class":102,"line":164},[100,5779,139],{"emptyLinePlaceholder":138},[100,5781,5782],{"class":102,"line":185},[100,5783,5784],{"class":414},"# Fix 1: convert to string first\n",[100,5786,5787,5789,5791,5793,5795,5797,5800,5803],{"class":102,"line":197},[100,5788,372],{"class":114},[100,5790,170],{"class":118},[100,5792,3422],{"class":191},[100,5794,170],{"class":118},[100,5796,757],{"class":178},[100,5798,5799],{"class":118},").",[100,5801,5802],{"class":178},"lower",[100,5804,3370],{"class":118},[100,5806,5807],{"class":102,"line":771},[100,5808,139],{"emptyLinePlaceholder":138},[100,5810,5811],{"class":102,"line":787},[100,5812,5813],{"class":414},"# Fix 2: check the type before calling an attribute\n",[100,5815,5817,5819,5821,5823,5825,5827,5829],{"class":102,"line":5816},10,[100,5818,2736],{"class":145},[100,5820,2862],{"class":114},[100,5822,170],{"class":118},[100,5824,757],{"class":178},[100,5826,126],{"class":118},[100,5828,2871],{"class":191},[100,5830,522],{"class":118},[100,5832,5834,5836,5838,5840,5842,5844],{"class":102,"line":5833},11,[100,5835,200],{"class":114},[100,5837,170],{"class":118},[100,5839,757],{"class":178},[100,5841,290],{"class":118},[100,5843,5802],{"class":178},[100,5845,3370],{"class":118},[100,5847,5849,5851],{"class":102,"line":5848},12,[100,5850,4944],{"class":145},[100,5852,149],{"class":118},[100,5854,5856,5858,5860,5862],{"class":102,"line":5855},13,[100,5857,200],{"class":114},[100,5859,170],{"class":118},[100,5861,757],{"class":178},[100,5863,215],{"class":118},[14,5865,5866,5867,5870,5871,5870,5874,5877],{},"This error happens when you call a method or access an attribute that integers do not have, such as ",[17,5868,5869],{},".lower()",", ",[17,5872,5873],{},".append()",[17,5875,5876],{},".keys()",", or a custom attribute name.",[77,5879,5881],{"id":5880},"what-this-error-means","What this error means",[14,5883,5884],{},"Python is telling you:",[40,5886,5887,5898,5901,5906],{},[43,5888,5889,5890,5870,5892,5894,5895],{},"It found an integer value such as ",[17,5891,3557],{},[17,5893,2579],{},", or ",[17,5896,5897],{},"-3",[43,5899,5900],{},"Your code tried to use an attribute or method on that integer",[43,5902,5903,5904],{},"That attribute does not exist for ",[17,5905,3499],{},[43,5907,5908],{},"The real problem is often that the variable has a different type than you expected",[14,5910,5911,5912,5914],{},"For example, this will fail because ",[17,5913,5869],{}," is a string method, not an integer method:",[91,5916,5918],{"className":93,"code":5917,"language":95,"meta":96,"style":96},"value = 100\nprint(value.lower())\n",[17,5919,5920,5928],{"__ignoreMap":96},[100,5921,5922,5924,5926],{"class":102,"line":103},[100,5923,3318],{"class":106},[100,5925,111],{"class":110},[100,5927,5377],{"class":122},[100,5929,5930,5932,5934,5936,5938,5940],{"class":102,"line":135},[100,5931,372],{"class":114},[100,5933,170],{"class":118},[100,5935,757],{"class":178},[100,5937,290],{"class":118},[100,5939,5802],{"class":178},[100,5941,3370],{"class":118},[77,5943,5945],{"id":5944},"simple-example-that-causes-the-error","Simple example that causes the error",[14,5947,5948],{},"Here is a small example:",[91,5950,5952],{"className":93,"code":5951,"language":95,"meta":96,"style":96},"value = 123\nprint(value.lower())\n",[17,5953,5954,5962],{"__ignoreMap":96},[100,5955,5956,5958,5960],{"class":102,"line":103},[100,5957,3318],{"class":106},[100,5959,111],{"class":110},[100,5961,5761],{"class":122},[100,5963,5964,5966,5968,5970,5972,5974],{"class":102,"line":135},[100,5965,372],{"class":114},[100,5967,170],{"class":118},[100,5969,757],{"class":178},[100,5971,290],{"class":118},[100,5973,5802],{"class":178},[100,5975,3370],{"class":118},[14,5977,218],{},[91,5979,5981],{"className":93,"code":5980,"language":95,"meta":96,"style":96},"AttributeError: 'int' object has no attribute 'lower'\n",[17,5982,5983],{"__ignoreMap":96},[100,5984,5985,5987,5989,5991,5993,5995,5997,5999,6001,6003],{"class":102,"line":103},[100,5986,3738],{"class":191},[100,5988,89],{"class":118},[100,5990,1274],{"class":205},[100,5992,3499],{"class":209},[100,5994,1280],{"class":205},[100,5996,3914],{"class":191},[100,5998,3917],{"class":106},[100,6000,1280],{"class":205},[100,6002,5802],{"class":209},[100,6004,3925],{"class":205},[14,6006,4094],{},[40,6008,6009,6016,6024],{},[43,6010,6011,6013,6014],{},[17,6012,757],{}," contains the integer ",[17,6015,3002],{},[43,6017,6018,6020,6021],{},[17,6019,5869],{}," works on strings such as ",[17,6022,6023],{},"\"HELLO\"",[43,6025,6026,6027,4112],{},"Integers do not have a ",[17,6028,5869],{},[14,6030,6031],{},"Correct version:",[91,6033,6035],{"className":93,"code":6034,"language":95,"meta":96,"style":96},"value = 123\nprint(str(value).lower())\n",[17,6036,6037,6045],{"__ignoreMap":96},[100,6038,6039,6041,6043],{"class":102,"line":103},[100,6040,3318],{"class":106},[100,6042,111],{"class":110},[100,6044,5761],{"class":122},[100,6046,6047,6049,6051,6053,6055,6057,6059,6061],{"class":102,"line":135},[100,6048,372],{"class":114},[100,6050,170],{"class":118},[100,6052,3422],{"class":191},[100,6054,170],{"class":118},[100,6056,757],{"class":178},[100,6058,5799],{"class":118},[100,6060,5802],{"class":178},[100,6062,3370],{"class":118},[14,6064,218],{},[91,6066,6068],{"className":93,"code":6067,"language":95,"meta":96,"style":96},"123\n",[17,6069,6070],{"__ignoreMap":96},[100,6071,6072],{"class":102,"line":103},[100,6073,6067],{"class":122},[14,6075,6076,6077,290],{},"If you need help with string behavior, see ",[295,6078,6080],{"href":6079},"\u002Flearn\u002Fpython-strings-explained-basics-and-examples","Python strings explained",[77,6082,1344],{"id":1343},[14,6084,6085],{},"This error often happens in one of these situations:",[40,6087,6088,6098,6103,6111,6114,6121],{},[43,6089,6090,6091,5870,6093,5894,6096],{},"Using string methods on a number, such as ",[17,6092,5869],{},[17,6094,6095],{},".strip()",[17,6097,3764],{},[43,6099,6100,6101],{},"Using list methods on a number, such as ",[17,6102,5873],{},[43,6104,6105,6106,3178,6108],{},"Using dictionary methods on a number, such as ",[17,6107,5876],{},[17,6109,6110],{},".get()",[43,6112,6113],{},"Overwriting a variable with an integer later in the program",[43,6115,6116,6117,6120],{},"Converting input with ",[17,6118,6119],{},"int()"," and then forgetting the value is no longer a string",[43,6122,6123],{},"A function returns an integer but you treat it like an object with custom attributes",[14,6125,6126],{},"Examples of common mistakes:",[91,6128,6130],{"className":93,"code":6129,"language":95,"meta":96,"style":96},"value = 10\nvalue.append(20)   # int has no .append()\n",[17,6131,6132,6140],{"__ignoreMap":96},[100,6133,6134,6136,6138],{"class":102,"line":103},[100,6135,3318],{"class":106},[100,6137,111],{"class":110},[100,6139,2181],{"class":122},[100,6141,6142,6144,6146,6148,6150,6153,6156],{"class":102,"line":135},[100,6143,757],{"class":106},[100,6145,290],{"class":118},[100,6147,4254],{"class":178},[100,6149,170],{"class":118},[100,6151,6152],{"class":122},"20",[100,6154,6155],{"class":118},")",[100,6157,6158],{"class":414},"   # int has no .append()\n",[91,6160,6162],{"className":93,"code":6161,"language":95,"meta":96,"style":96},"value = 42\nprint(value.keys())   # int has no .keys()\n",[17,6163,6164,6173],{"__ignoreMap":96},[100,6165,6166,6168,6170],{"class":102,"line":103},[100,6167,3318],{"class":106},[100,6169,111],{"class":110},[100,6171,6172],{"class":122}," 42\n",[100,6174,6175,6177,6179,6181,6183,6186,6189],{"class":102,"line":135},[100,6176,372],{"class":114},[100,6178,170],{"class":118},[100,6180,757],{"class":178},[100,6182,290],{"class":118},[100,6184,6185],{"class":178},"keys",[100,6187,6188],{"class":118},"())",[100,6190,6191],{"class":414},"   # int has no .keys()\n",[91,6193,6195],{"className":93,"code":6194,"language":95,"meta":96,"style":96},"user_input = int(input(\"Enter a number: \"))\nprint(user_input.strip())   # .strip() works on strings, not ints\n",[17,6196,6197,6222],{"__ignoreMap":96},[100,6198,6199,6202,6204,6206,6208,6211,6213,6215,6218,6220],{"class":102,"line":103},[100,6200,6201],{"class":106},"user_input ",[100,6203,111],{"class":110},[100,6205,3389],{"class":191},[100,6207,170],{"class":118},[100,6209,6210],{"class":114},"input",[100,6212,170],{"class":118},[100,6214,206],{"class":205},[100,6216,6217],{"class":209},"Enter a number: ",[100,6219,206],{"class":205},[100,6221,182],{"class":118},[100,6223,6224,6226,6228,6231,6233,6236,6238],{"class":102,"line":135},[100,6225,372],{"class":114},[100,6227,170],{"class":118},[100,6229,6230],{"class":178},"user_input",[100,6232,290],{"class":118},[100,6234,6235],{"class":178},"strip",[100,6237,6188],{"class":118},[100,6239,6240],{"class":414},"   # .strip() works on strings, not ints\n",[77,6242,6244],{"id":6243},"how-to-fix-it","How to fix it",[14,6246,6247],{},"Start with the simplest check: inspect the value and its type.",[91,6249,6251],{"className":93,"code":6250,"language":95,"meta":96,"style":96},"value = 123\n\nprint(value)\nprint(type(value))\n",[17,6252,6253,6261,6265,6275],{"__ignoreMap":96},[100,6254,6255,6257,6259],{"class":102,"line":103},[100,6256,3318],{"class":106},[100,6258,111],{"class":110},[100,6260,5761],{"class":122},[100,6262,6263],{"class":102,"line":135},[100,6264,139],{"emptyLinePlaceholder":138},[100,6266,6267,6269,6271,6273],{"class":102,"line":142},[100,6268,372],{"class":114},[100,6270,170],{"class":118},[100,6272,757],{"class":178},[100,6274,215],{"class":118},[100,6276,6277,6279,6281,6283,6285,6287],{"class":102,"line":152},[100,6278,372],{"class":114},[100,6280,170],{"class":118},[100,6282,1250],{"class":191},[100,6284,170],{"class":118},[100,6286,757],{"class":178},[100,6288,182],{"class":118},[14,6290,218],{},[91,6292,6294],{"className":93,"code":6293,"language":95,"meta":96,"style":96},"123\n\u003Cclass 'int'>\n",[17,6295,6296,6300],{"__ignoreMap":96},[100,6297,6298],{"class":102,"line":103},[100,6299,6067],{"class":122},[100,6301,6302,6304,6306,6308,6310,6312],{"class":102,"line":135},[100,6303,2782],{"class":110},[100,6305,3417],{"class":1077},[100,6307,1274],{"class":205},[100,6309,3499],{"class":209},[100,6311,1280],{"class":205},[100,6313,1980],{"class":110},[14,6315,6316],{},"Useful steps:",[40,6318,6319,6325,6328,6331,6337,6340],{},[43,6320,6321,6322],{},"Print the variable and its type with ",[17,6323,6324],{},"print(value, type(value))",[43,6326,6327],{},"Check where the variable was last assigned",[43,6329,6330],{},"Use the correct method for the actual type",[43,6332,6333,6334],{},"Convert the value first if needed, such as ",[17,6335,6336],{},"str(number)",[43,6338,6339],{},"Rename variables clearly so you do not confuse text values and number values",[43,6341,6342,6343,6345],{},"Add ",[17,6344,5256],{}," checks when the type may change",[14,6347,6348,6349,3266,6355,6360],{},"The built-in ",[295,6350,6352,6354],{"href":6351},"\u002Freference\u002Fpython-type-function-explained",[17,6353,4430],{}," function",[295,6356,6358,6354],{"href":6357},"\u002Freference\u002Fpython-isinstance-function-explained",[17,6359,5256],{}," are especially helpful for this kind of bug.",[77,6362,6364],{"id":6363},"fix-example-convert-to-the-right-type","Fix example: convert to the right type",[14,6366,6367],{},"If your goal is to use string methods, convert the integer to a string first.",[675,6369,6371],{"id":6370},"wrong","Wrong",[91,6373,6375],{"className":93,"code":6374,"language":95,"meta":96,"style":96},"value = 456\nprint(value.split())\n",[17,6376,6377,6386],{"__ignoreMap":96},[100,6378,6379,6381,6383],{"class":102,"line":103},[100,6380,3318],{"class":106},[100,6382,111],{"class":110},[100,6384,6385],{"class":122}," 456\n",[100,6387,6388,6390,6392,6394,6396,6398],{"class":102,"line":135},[100,6389,372],{"class":114},[100,6391,170],{"class":118},[100,6393,757],{"class":178},[100,6395,290],{"class":118},[100,6397,3922],{"class":178},[100,6399,3370],{"class":118},[675,6401,6403],{"id":6402},"right","Right",[91,6405,6407],{"className":93,"code":6406,"language":95,"meta":96,"style":96},"value = 456\ntext = str(value)\nprint(text.split())\n",[17,6408,6409,6417,6431],{"__ignoreMap":96},[100,6410,6411,6413,6415],{"class":102,"line":103},[100,6412,3318],{"class":106},[100,6414,111],{"class":110},[100,6416,6385],{"class":122},[100,6418,6419,6421,6423,6425,6427,6429],{"class":102,"line":135},[100,6420,4331],{"class":106},[100,6422,111],{"class":110},[100,6424,2871],{"class":191},[100,6426,170],{"class":118},[100,6428,757],{"class":178},[100,6430,215],{"class":118},[100,6432,6433,6435,6437,6439,6441,6443],{"class":102,"line":142},[100,6434,372],{"class":114},[100,6436,170],{"class":118},[100,6438,4468],{"class":178},[100,6440,290],{"class":118},[100,6442,3922],{"class":178},[100,6444,3370],{"class":118},[14,6446,218],{},[91,6448,6450],{"className":93,"code":6449,"language":95,"meta":96,"style":96},"['456']\n",[17,6451,6452],{"__ignoreMap":96},[100,6453,6454,6456,6458,6461,6463],{"class":102,"line":103},[100,6455,3166],{"class":118},[100,6457,1280],{"class":205},[100,6459,6460],{"class":209},"456",[100,6462,1280],{"class":205},[100,6464,603],{"class":118},[14,6466,6467,6468,89],{},"Another example with ",[17,6469,5869],{},[91,6471,6473],{"className":93,"code":6472,"language":95,"meta":96,"style":96},"value = 123\ntext = str(value)\nprint(text.lower())\n",[17,6474,6475,6483,6497],{"__ignoreMap":96},[100,6476,6477,6479,6481],{"class":102,"line":103},[100,6478,3318],{"class":106},[100,6480,111],{"class":110},[100,6482,5761],{"class":122},[100,6484,6485,6487,6489,6491,6493,6495],{"class":102,"line":135},[100,6486,4331],{"class":106},[100,6488,111],{"class":110},[100,6490,2871],{"class":191},[100,6492,170],{"class":118},[100,6494,757],{"class":178},[100,6496,215],{"class":118},[100,6498,6499,6501,6503,6505,6507,6509],{"class":102,"line":142},[100,6500,372],{"class":114},[100,6502,170],{"class":118},[100,6504,4468],{"class":178},[100,6506,290],{"class":118},[100,6508,5802],{"class":178},[100,6510,3370],{"class":118},[14,6512,218],{},[91,6514,6515],{"className":93,"code":6067,"language":95,"meta":96,"style":96},[17,6516,6517],{"__ignoreMap":96},[100,6518,6519],{"class":102,"line":103},[100,6520,6067],{"class":122},[14,6522,6523],{},"If your real goal is math, do not convert to a string. Keep the value as an integer and use numeric operations instead.",[91,6525,6527],{"className":93,"code":6526,"language":95,"meta":96,"style":96},"value = 123\nprint(value + 10)\n",[17,6528,6529,6537],{"__ignoreMap":96},[100,6530,6531,6533,6535],{"class":102,"line":103},[100,6532,3318],{"class":106},[100,6534,111],{"class":110},[100,6536,5761],{"class":122},[100,6538,6539,6541,6543,6545,6548,6551],{"class":102,"line":135},[100,6540,372],{"class":114},[100,6542,170],{"class":118},[100,6544,3318],{"class":178},[100,6546,6547],{"class":110},"+",[100,6549,6550],{"class":122}," 10",[100,6552,215],{"class":118},[14,6554,218],{},[91,6556,6558],{"className":93,"code":6557,"language":95,"meta":96,"style":96},"133\n",[17,6559,6560],{"__ignoreMap":96},[100,6561,6562],{"class":102,"line":103},[100,6563,6557],{"class":122},[14,6565,6566,6567,290],{},"If you specifically need this conversion, see ",[295,6568,6570],{"href":6569},"\u002Fhow-to\u002Fhow-to-convert-int-to-string-in-python\u002F","how to convert int to string in Python",[77,6572,6574],{"id":6573},"fix-example-find-accidental-reassignment","Fix example: find accidental reassignment",[14,6576,6577],{},"A variable may start as one type and later become an integer.",[675,6579,6581],{"id":6580},"example-that-fails","Example that fails",[91,6583,6585],{"className":93,"code":6584,"language":95,"meta":96,"style":96},"value = \"HELLO\"\nprint(value.lower())\n\nvalue = 100\nprint(value.lower())\n",[17,6586,6587,6600,6614,6618,6626],{"__ignoreMap":96},[100,6588,6589,6591,6593,6595,6598],{"class":102,"line":103},[100,6590,3318],{"class":106},[100,6592,111],{"class":110},[100,6594,1708],{"class":205},[100,6596,6597],{"class":209},"HELLO",[100,6599,1714],{"class":205},[100,6601,6602,6604,6606,6608,6610,6612],{"class":102,"line":135},[100,6603,372],{"class":114},[100,6605,170],{"class":118},[100,6607,757],{"class":178},[100,6609,290],{"class":118},[100,6611,5802],{"class":178},[100,6613,3370],{"class":118},[100,6615,6616],{"class":102,"line":142},[100,6617,139],{"emptyLinePlaceholder":138},[100,6619,6620,6622,6624],{"class":102,"line":152},[100,6621,3318],{"class":106},[100,6623,111],{"class":110},[100,6625,5377],{"class":122},[100,6627,6628,6630,6632,6634,6636,6638],{"class":102,"line":164},[100,6629,372],{"class":114},[100,6631,170],{"class":118},[100,6633,757],{"class":178},[100,6635,290],{"class":118},[100,6637,5802],{"class":178},[100,6639,3370],{"class":118},[14,6641,218],{},[91,6643,6645],{"className":93,"code":6644,"language":95,"meta":96,"style":96},"hello\nAttributeError: 'int' object has no attribute 'lower'\n",[17,6646,6647,6652],{"__ignoreMap":96},[100,6648,6649],{"class":102,"line":103},[100,6650,6651],{"class":106},"hello\n",[100,6653,6654,6656,6658,6660,6662,6664,6666,6668,6670,6672],{"class":102,"line":135},[100,6655,3738],{"class":191},[100,6657,89],{"class":118},[100,6659,1274],{"class":205},[100,6661,3499],{"class":209},[100,6663,1280],{"class":205},[100,6665,3914],{"class":191},[100,6667,3917],{"class":106},[100,6669,1280],{"class":205},[100,6671,5802],{"class":209},[100,6673,3925],{"class":205},[14,6675,6676,6677,6679],{},"The problem is that ",[17,6678,757],{}," was reassigned.",[14,6681,6682],{},"A simple way to debug this is to print the variable before the failing line:",[91,6684,6686],{"className":93,"code":6685,"language":95,"meta":96,"style":96},"value = \"HELLO\"\nprint(value.lower())\n\nvalue = 100\n\nprint(\"Before lower():\", value)\nprint(\"Type:\", type(value))\n\nprint(value.lower())\n",[17,6687,6688,6700,6714,6718,6726,6730,6750,6774,6778],{"__ignoreMap":96},[100,6689,6690,6692,6694,6696,6698],{"class":102,"line":103},[100,6691,3318],{"class":106},[100,6693,111],{"class":110},[100,6695,1708],{"class":205},[100,6697,6597],{"class":209},[100,6699,1714],{"class":205},[100,6701,6702,6704,6706,6708,6710,6712],{"class":102,"line":135},[100,6703,372],{"class":114},[100,6705,170],{"class":118},[100,6707,757],{"class":178},[100,6709,290],{"class":118},[100,6711,5802],{"class":178},[100,6713,3370],{"class":118},[100,6715,6716],{"class":102,"line":142},[100,6717,139],{"emptyLinePlaceholder":138},[100,6719,6720,6722,6724],{"class":102,"line":152},[100,6721,3318],{"class":106},[100,6723,111],{"class":110},[100,6725,5377],{"class":122},[100,6727,6728],{"class":102,"line":164},[100,6729,139],{"emptyLinePlaceholder":138},[100,6731,6732,6734,6736,6738,6741,6743,6745,6748],{"class":102,"line":185},[100,6733,372],{"class":114},[100,6735,170],{"class":118},[100,6737,206],{"class":205},[100,6739,6740],{"class":209},"Before lower():",[100,6742,206],{"class":205},[100,6744,126],{"class":118},[100,6746,6747],{"class":178}," value",[100,6749,215],{"class":118},[100,6751,6752,6754,6756,6758,6761,6763,6765,6768,6770,6772],{"class":102,"line":197},[100,6753,372],{"class":114},[100,6755,170],{"class":118},[100,6757,206],{"class":205},[100,6759,6760],{"class":209},"Type:",[100,6762,206],{"class":205},[100,6764,126],{"class":118},[100,6766,6767],{"class":191}," type",[100,6769,170],{"class":118},[100,6771,757],{"class":178},[100,6773,182],{"class":118},[100,6775,6776],{"class":102,"line":771},[100,6777,139],{"emptyLinePlaceholder":138},[100,6779,6780,6782,6784,6786,6788,6790],{"class":102,"line":787},[100,6781,372],{"class":114},[100,6783,170],{"class":118},[100,6785,757],{"class":178},[100,6787,290],{"class":118},[100,6789,5802],{"class":178},[100,6791,3370],{"class":118},[14,6793,218],{},[91,6795,6797],{"className":93,"code":6796,"language":95,"meta":96,"style":96},"hello\nBefore lower(): 100\nType: \u003Cclass 'int'>\nAttributeError: 'int' object has no attribute 'lower'\n",[17,6798,6799,6803,6815,6834],{"__ignoreMap":96},[100,6800,6801],{"class":102,"line":103},[100,6802,6651],{"class":106},[100,6804,6805,6808,6810,6813],{"class":102,"line":135},[100,6806,6807],{"class":106},"Before ",[100,6809,5802],{"class":178},[100,6811,6812],{"class":118},"():",[100,6814,5377],{"class":122},[100,6816,6817,6820,6822,6824,6826,6828,6830,6832],{"class":102,"line":142},[100,6818,6819],{"class":106},"Type",[100,6821,89],{"class":118},[100,6823,1974],{"class":110},[100,6825,3417],{"class":1077},[100,6827,1274],{"class":205},[100,6829,3499],{"class":209},[100,6831,1280],{"class":205},[100,6833,1980],{"class":110},[100,6835,6836,6838,6840,6842,6844,6846,6848,6850,6852,6854],{"class":102,"line":152},[100,6837,3738],{"class":191},[100,6839,89],{"class":118},[100,6841,1274],{"class":205},[100,6843,3499],{"class":209},[100,6845,1280],{"class":205},[100,6847,3914],{"class":191},[100,6849,3917],{"class":106},[100,6851,1280],{"class":205},[100,6853,5802],{"class":209},[100,6855,3925],{"class":205},[14,6857,6858],{},"A better fix is to use clearer variable names:",[91,6860,6862],{"className":93,"code":6861,"language":95,"meta":96,"style":96},"message = \"HELLO\"\nprint(message.lower())\n\ncount = 100\nprint(count + 1)\n",[17,6863,6864,6877,6891,6895,6903],{"__ignoreMap":96},[100,6865,6866,6869,6871,6873,6875],{"class":102,"line":103},[100,6867,6868],{"class":106},"message ",[100,6870,111],{"class":110},[100,6872,1708],{"class":205},[100,6874,6597],{"class":209},[100,6876,1714],{"class":205},[100,6878,6879,6881,6883,6885,6887,6889],{"class":102,"line":135},[100,6880,372],{"class":114},[100,6882,170],{"class":118},[100,6884,1836],{"class":178},[100,6886,290],{"class":118},[100,6888,5802],{"class":178},[100,6890,3370],{"class":118},[100,6892,6893],{"class":102,"line":142},[100,6894,139],{"emptyLinePlaceholder":138},[100,6896,6897,6899,6901],{"class":102,"line":152},[100,6898,2176],{"class":106},[100,6900,111],{"class":110},[100,6902,5377],{"class":122},[100,6904,6905,6907,6909,6911,6913,6916],{"class":102,"line":164},[100,6906,372],{"class":114},[100,6908,170],{"class":118},[100,6910,2176],{"class":178},[100,6912,6547],{"class":110},[100,6914,6915],{"class":122}," 1",[100,6917,215],{"class":118},[14,6919,6920],{},"This is a very common mistake in longer scripts and loops.",[77,6922,6924],{"id":6923},"beginner-debugging-steps","Beginner debugging steps",[14,6926,6927],{},"When you see this error, follow these steps:",[3282,6929,6930,6932,6935,6938,6945,6948,6951],{},[43,6931,5271],{},[43,6933,6934],{},"Find the line that caused the problem",[43,6936,6937],{},"Print the variable right before that line",[43,6939,5163,6940,6942,6943],{},[17,6941,5283],{}," to confirm it is an ",[17,6944,3499],{},[43,6946,6947],{},"Search earlier in the code for where that variable was assigned",[43,6949,6950],{},"Check function return values if the variable came from a function",[43,6952,6953],{},"Make sure your fix matches the type you actually want",[14,6955,6956],{},"These commands are useful while debugging:",[91,6958,6960],{"className":93,"code":6959,"language":95,"meta":96,"style":96},"print(value)\nprint(type(value))\nprint(isinstance(value, int))\nprint(isinstance(value, str))\nprint(dir(value))\n",[17,6961,6962,6972,6986,7005,7023],{"__ignoreMap":96},[100,6963,6964,6966,6968,6970],{"class":102,"line":103},[100,6965,372],{"class":114},[100,6967,170],{"class":118},[100,6969,757],{"class":178},[100,6971,215],{"class":118},[100,6973,6974,6976,6978,6980,6982,6984],{"class":102,"line":135},[100,6975,372],{"class":114},[100,6977,170],{"class":118},[100,6979,1250],{"class":191},[100,6981,170],{"class":118},[100,6983,757],{"class":178},[100,6985,182],{"class":118},[100,6987,6988,6990,6992,6995,6997,6999,7001,7003],{"class":102,"line":142},[100,6989,372],{"class":114},[100,6991,170],{"class":118},[100,6993,6994],{"class":114},"isinstance",[100,6996,170],{"class":118},[100,6998,757],{"class":178},[100,7000,126],{"class":118},[100,7002,3389],{"class":191},[100,7004,182],{"class":118},[100,7006,7007,7009,7011,7013,7015,7017,7019,7021],{"class":102,"line":152},[100,7008,372],{"class":114},[100,7010,170],{"class":118},[100,7012,6994],{"class":114},[100,7014,170],{"class":118},[100,7016,757],{"class":178},[100,7018,126],{"class":118},[100,7020,2871],{"class":191},[100,7022,182],{"class":118},[100,7024,7025,7027,7029,7031,7033,7035],{"class":102,"line":164},[100,7026,372],{"class":114},[100,7028,170],{"class":118},[100,7030,3822],{"class":114},[100,7032,170],{"class":118},[100,7034,757],{"class":178},[100,7036,182],{"class":118},[14,7038,7039],{},"What they do:",[40,7041,7042,7048,7054,7060,7066],{},[43,7043,7044,7047],{},[17,7045,7046],{},"print(value)"," shows the current value",[43,7049,7050,7053],{},[17,7051,7052],{},"print(type(value))"," shows the exact type",[43,7055,7056,7059],{},[17,7057,7058],{},"print(isinstance(value, int))"," checks whether it is an integer",[43,7061,7062,7065],{},[17,7063,7064],{},"print(isinstance(value, str))"," checks whether it is a string",[43,7067,7068,7071],{},[17,7069,7070],{},"print(dir(value))"," shows available attributes and methods",[14,7073,7074,7075,290],{},"If you are new to debugging, read this ",[295,7076,7077],{"href":3852},"beginner guide to debugging Python code",[77,7079,7081],{"id":7080},"related-errors-to-compare","Related errors to compare",[14,7083,7084],{},"Sometimes the problem is similar, but the type is different.",[14,7086,7087],{},"You may also want to check:",[40,7089,7090,7096,7102,7108],{},[43,7091,7092],{},[295,7093,7095],{"href":7094},"\u002Ferrors\u002Fattributeerror-object-has-no-attribute-fix","AttributeError: object has no attribute",[43,7097,7098],{},[295,7099,7101],{"href":7100},"\u002Ferrors\u002Fattributeerror-str-object-has-no-attribute-fix","AttributeError: 'str' object has no attribute",[43,7103,7104],{},[295,7105,7107],{"href":7106},"\u002Ferrors\u002Fattributeerror-list-object-has-no-attribute-fix","AttributeError: 'list' object has no attribute",[43,7109,7110],{},[295,7111,7113],{"href":7112},"\u002Ferrors\u002Fattributeerror-nonetype-object-has-no-attribute-fix","AttributeError: 'NoneType' object has no attribute",[77,7115,7117],{"id":7116},"common-mistakes","Common mistakes",[14,7119,7120],{},"These are very common beginner mistakes that cause this error:",[40,7122,7123,7128,7132,7136,7139,7145],{},[43,7124,315,7125,7127],{},[17,7126,5869],{}," on an integer",[43,7129,315,7130,7127],{},[17,7131,5873],{},[43,7133,315,7134,7127],{},[17,7135,5876],{},[43,7137,7138],{},"Reusing a variable name and changing it from string to int",[43,7140,1357,7141,7144],{},[17,7142,7143],{},"int(input(...))"," and later treating the result like text",[43,7146,7147],{},"Expecting a custom object but receiving a plain integer",[14,7149,580],{},[91,7151,7153],{"className":93,"code":7152,"language":95,"meta":96,"style":96},"age = int(input(\"Enter your age: \"))\nprint(age.strip())\n",[17,7154,7155,7178],{"__ignoreMap":96},[100,7156,7157,7159,7161,7163,7165,7167,7169,7171,7174,7176],{"class":102,"line":103},[100,7158,1664],{"class":106},[100,7160,111],{"class":110},[100,7162,3389],{"class":191},[100,7164,170],{"class":118},[100,7166,6210],{"class":114},[100,7168,170],{"class":118},[100,7170,206],{"class":205},[100,7172,7173],{"class":209},"Enter your age: ",[100,7175,206],{"class":205},[100,7177,182],{"class":118},[100,7179,7180,7182,7184,7186,7188,7190],{"class":102,"line":135},[100,7181,372],{"class":114},[100,7183,170],{"class":118},[100,7185,2770],{"class":178},[100,7187,290],{"class":118},[100,7189,6235],{"class":178},[100,7191,3370],{"class":118},[14,7193,7194,7195,7197,7198,290],{},"This fails because ",[17,7196,2770],{}," is already an integer after ",[17,7199,7200],{},"int(...)",[14,7202,7203,7204,7206],{},"If you wanted text methods like ",[17,7205,6095],{},", do them before converting:",[91,7208,7210],{"className":93,"code":7209,"language":95,"meta":96,"style":96},"age_text = input(\"Enter your age: \").strip()\nage = int(age_text)\nprint(age)\n",[17,7211,7212,7236,7251],{"__ignoreMap":96},[100,7213,7214,7217,7219,7222,7224,7226,7228,7230,7232,7234],{"class":102,"line":103},[100,7215,7216],{"class":106},"age_text ",[100,7218,111],{"class":110},[100,7220,7221],{"class":114}," input",[100,7223,170],{"class":118},[100,7225,206],{"class":205},[100,7227,7173],{"class":209},[100,7229,206],{"class":205},[100,7231,5799],{"class":118},[100,7233,6235],{"class":178},[100,7235,4734],{"class":118},[100,7237,7238,7240,7242,7244,7246,7249],{"class":102,"line":135},[100,7239,1664],{"class":106},[100,7241,111],{"class":110},[100,7243,3389],{"class":191},[100,7245,170],{"class":118},[100,7247,7248],{"class":178},"age_text",[100,7250,215],{"class":118},[100,7252,7253,7255,7257,7259],{"class":102,"line":142},[100,7254,372],{"class":114},[100,7256,170],{"class":118},[100,7258,2770],{"class":178},[100,7260,215],{"class":118},[77,7262,1514],{"id":1513},[675,7264,7266],{"id":7265},"why-does-python-say-an-int-has-no-attribute","Why does Python say an int has no attribute?",[14,7268,7269],{},"Because your variable currently holds an integer, and the attribute or method you used does not exist for integers.",[675,7271,7273],{"id":7272},"how-do-i-know-which-variable-is-an-int","How do I know which variable is an int?",[14,7275,7276],{},"Check the traceback, then print the variable and its type right before the line that fails.",[675,7278,7280],{"id":7279},"can-i-use-string-methods-on-a-number","Can I use string methods on a number?",[14,7282,7283,7284,7286],{},"Not directly. Convert the number to a string first with ",[17,7285,6336],{}," if that matches your goal.",[675,7288,7290],{"id":7289},"why-did-this-work-earlier-but-fail-now","Why did this work earlier but fail now?",[14,7292,7293,7294,290],{},"The variable may have been reassigned later in the code and changed from another type to ",[17,7295,3499],{},[77,7297,1554],{"id":1553},[40,7299,7300,7306,7310,7316,7322,7327],{},[43,7301,7302],{},[295,7303,7305],{"href":7304},"\u002Flearn\u002Fpython-numbers-explained-int-float-complex","Python numbers explained",[43,7307,7308],{},[295,7309,6080],{"href":6079},[43,7311,7312],{},[295,7313,1572,7314,1576],{"href":6351},[17,7315,4430],{},[43,7317,7318],{},[295,7319,1572,7320,1576],{"href":6357},[17,7321,5256],{},[43,7323,7324],{},[295,7325,7326],{"href":6569},"How to convert int to string in Python",[43,7328,7329],{},[295,7330,7331],{"href":3852},"How to debug Python code: beginner guide",[1589,7333,7334],{},"html pre.shiki code .su5hD, html code.shiki .su5hD{--shiki-light:#90A4AE;--shiki-default:#24292E;--shiki-dark:#E1E4E8}html pre.shiki code .smGrS, html code.shiki .smGrS{--shiki-light:#39ADB5;--shiki-default:#D73A49;--shiki-dark:#F97583}html pre.shiki code .srdBf, html code.shiki .srdBf{--shiki-light:#F76D47;--shiki-default:#005CC5;--shiki-dark:#79B8FF}html pre.shiki code .sutJx, html code.shiki .sutJx{--shiki-light:#90A4AE;--shiki-light-font-style:italic;--shiki-default:#6A737D;--shiki-default-font-style:inherit;--shiki-dark:#6A737D;--shiki-dark-font-style:inherit}html pre.shiki code .sptTA, html code.shiki .sptTA{--shiki-light:#6182B8;--shiki-default:#005CC5;--shiki-dark:#79B8FF}html pre.shiki code .sP7_E, html code.shiki .sP7_E{--shiki-light:#39ADB5;--shiki-default:#24292E;--shiki-dark:#E1E4E8}html pre.shiki code .sZMiF, html code.shiki .sZMiF{--shiki-light:#E2931D;--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 .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 .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 .sbsja, html code.shiki .sbsja{--shiki-light:#9C3EDA;--shiki-default:#D73A49;--shiki-dark:#F97583}",{"title":96,"searchDepth":135,"depth":135,"links":7336},[7337,7338,7339,7340,7341,7342,7346,7349,7350,7351,7352,7358],{"id":79,"depth":135,"text":80},{"id":5880,"depth":135,"text":5881},{"id":5944,"depth":135,"text":5945},{"id":1343,"depth":135,"text":1344},{"id":6243,"depth":135,"text":6244},{"id":6363,"depth":135,"text":6364,"children":7343},[7344,7345],{"id":6370,"depth":142,"text":6371},{"id":6402,"depth":142,"text":6403},{"id":6573,"depth":135,"text":6574,"children":7347},[7348],{"id":6580,"depth":142,"text":6581},{"id":6923,"depth":135,"text":6924},{"id":7080,"depth":135,"text":7081},{"id":7116,"depth":135,"text":7117},{"id":1513,"depth":135,"text":1514,"children":7353},[7354,7355,7356,7357],{"id":7265,"depth":142,"text":7266},{"id":7272,"depth":142,"text":7273},{"id":7279,"depth":142,"text":7280},{"id":7289,"depth":142,"text":7290},{"id":1553,"depth":135,"text":1554},"Master attributeerror int object has no attribute fix in our comprehensive Python beginner guide.",{},"\u002Ferrors\u002Fattributeerror-int-object-has-no-attribute-fix",{"title":5722,"description":7359},"errors\u002Fattributeerror-int-object-has-no-attribute-fix","E1yEY3FUWzEZsG37Uf4uG9OOB9OQrKEzXmj86K0bMu8",{"id":7366,"title":7367,"body":7368,"description":9227,"extension":1623,"meta":9228,"navigation":138,"path":7106,"seo":9229,"stem":9230,"__hash__":9231},"content\u002Ferrors\u002Fattributeerror-list-object-has-no-attribute-fix.md","AttributeError: 'list' object has no attribute (Fix)",{"type":7,"value":7369,"toc":9193},[7370,7376,7385,7396,7448,7454,7456,7587,7589,7630,7633,7635,7637,7663,7665,7708,7711,7738,7742,7745,7762,7769,7773,7776,7780,7827,7832,7836,7887,7893,7900,7941,7947,7951,8004,8007,8063,8065,8074,8078,8081,8105,8112,8114,8117,8149,8153,8221,8223,8270,8273,8277,8285,8339,8341,8366,8370,8373,8421,8423,8431,8435,8438,8512,8514,8540,8547,8551,8554,8571,8573,8677,8680,8711,8715,8718,8724,8749,8753,8813,8815,8824,8828,8918,8920,8954,8961,8963,8966,8997,8999,9002,9019,9022,9095,9107,9109,9113,9116,9120,9134,9141,9146,9150,9157,9159,9187,9190],[10,7371,5728,7373,7375],{"id":7372},"attributeerror-list-object-has-no-attribute-fix",[17,7374,4122],{}," (Fix)",[14,7377,7378,7379,7381,7382,290],{},"This error happens when Python sees a ",[22,7380,1235],{},", but your code tries to use a method or attribute that ",[22,7383,7384],{},"lists do not have",[14,7386,7387,7388,7391,7392,7395],{},"A common example is using a ",[22,7389,7390],{},"string method"," like ",[17,7393,7394],{},"lower()"," on a list:",[91,7397,7399],{"className":93,"code":7398,"language":95,"meta":96,"style":96},"my_list = [\"A\", \"B\", \"C\"]\nmy_list.lower()\n",[17,7400,7401,7437],{"__ignoreMap":96},[100,7402,7403,7406,7408,7410,7412,7415,7417,7419,7421,7424,7426,7428,7430,7433,7435],{"class":102,"line":103},[100,7404,7405],{"class":106},"my_list ",[100,7407,111],{"class":110},[100,7409,594],{"class":118},[100,7411,206],{"class":205},[100,7413,7414],{"class":209},"A",[100,7416,206],{"class":205},[100,7418,126],{"class":118},[100,7420,1708],{"class":205},[100,7422,7423],{"class":209},"B",[100,7425,206],{"class":205},[100,7427,126],{"class":118},[100,7429,1708],{"class":205},[100,7431,7432],{"class":209},"C",[100,7434,206],{"class":205},[100,7436,603],{"class":118},[100,7438,7439,7442,7444,7446],{"class":102,"line":135},[100,7440,7441],{"class":106},"my_list",[100,7443,290],{"class":118},[100,7445,5802],{"class":178},[100,7447,4734],{"class":118},[14,7449,7450,7451,7453],{},"That fails because ",[17,7452,7394],{}," works on strings, not on lists.",[77,7455,80],{"id":79},[91,7457,7459],{"className":93,"code":7458,"language":95,"meta":96,"style":96},"my_list = [\"a\", \"b\", \"c\"]\n\n# Wrong: lists do not have string methods like lower()\n# my_list.lower()\n\n# Fix 1: use a list method\nmy_list.append(\"d\")\n\n# Fix 2: apply the string method to each item\nlowered = [item.lower() for item in my_list]\nprint(lowered)\n",[17,7460,7461,7493,7497,7502,7507,7511,7516,7535,7539,7544,7576],{"__ignoreMap":96},[100,7462,7463,7465,7467,7469,7471,7473,7475,7477,7479,7481,7483,7485,7487,7489,7491],{"class":102,"line":103},[100,7464,7405],{"class":106},[100,7466,111],{"class":110},[100,7468,594],{"class":118},[100,7470,206],{"class":205},[100,7472,295],{"class":209},[100,7474,206],{"class":205},[100,7476,126],{"class":118},[100,7478,1708],{"class":205},[100,7480,4027],{"class":209},[100,7482,206],{"class":205},[100,7484,126],{"class":118},[100,7486,1708],{"class":205},[100,7488,4036],{"class":209},[100,7490,206],{"class":205},[100,7492,603],{"class":118},[100,7494,7495],{"class":102,"line":135},[100,7496,139],{"emptyLinePlaceholder":138},[100,7498,7499],{"class":102,"line":142},[100,7500,7501],{"class":414},"# Wrong: lists do not have string methods like lower()\n",[100,7503,7504],{"class":102,"line":152},[100,7505,7506],{"class":414},"# my_list.lower()\n",[100,7508,7509],{"class":102,"line":164},[100,7510,139],{"emptyLinePlaceholder":138},[100,7512,7513],{"class":102,"line":185},[100,7514,7515],{"class":414},"# Fix 1: use a list method\n",[100,7517,7518,7520,7522,7524,7526,7528,7531,7533],{"class":102,"line":197},[100,7519,7441],{"class":106},[100,7521,290],{"class":118},[100,7523,4254],{"class":178},[100,7525,170],{"class":118},[100,7527,206],{"class":205},[100,7529,7530],{"class":209},"d",[100,7532,206],{"class":205},[100,7534,215],{"class":118},[100,7536,7537],{"class":102,"line":771},[100,7538,139],{"emptyLinePlaceholder":138},[100,7540,7541],{"class":102,"line":787},[100,7542,7543],{"class":414},"# Fix 2: apply the string method to each item\n",[100,7545,7546,7549,7551,7553,7556,7558,7560,7563,7566,7569,7571,7574],{"class":102,"line":5816},[100,7547,7548],{"class":106},"lowered ",[100,7550,111],{"class":110},[100,7552,594],{"class":118},[100,7554,7555],{"class":106},"item",[100,7557,290],{"class":118},[100,7559,5802],{"class":178},[100,7561,7562],{"class":118},"()",[100,7564,7565],{"class":145}," for",[100,7567,7568],{"class":106}," item ",[100,7570,617],{"class":145},[100,7572,7573],{"class":106}," my_list",[100,7575,603],{"class":118},[100,7577,7578,7580,7582,7585],{"class":102,"line":5833},[100,7579,372],{"class":114},[100,7581,170],{"class":118},[100,7583,7584],{"class":178},"lowered",[100,7586,215],{"class":118},[14,7588,218],{},[91,7590,7592],{"className":93,"code":7591,"language":95,"meta":96,"style":96},"['a', 'b', 'c', 'd']\n",[17,7593,7594],{"__ignoreMap":96},[100,7595,7596,7598,7600,7602,7604,7606,7608,7610,7612,7614,7616,7618,7620,7622,7624,7626,7628],{"class":102,"line":103},[100,7597,3166],{"class":118},[100,7599,1280],{"class":205},[100,7601,295],{"class":209},[100,7603,1280],{"class":205},[100,7605,126],{"class":118},[100,7607,1274],{"class":205},[100,7609,4027],{"class":209},[100,7611,1280],{"class":205},[100,7613,126],{"class":118},[100,7615,1274],{"class":205},[100,7617,4036],{"class":209},[100,7619,1280],{"class":205},[100,7621,126],{"class":118},[100,7623,1274],{"class":205},[100,7625,7530],{"class":209},[100,7627,1280],{"class":205},[100,7629,603],{"class":118},[14,7631,7632],{},"This error usually means you are treating a list like a string, dictionary, or another object type.",[77,7634,5881],{"id":5880},[14,7636,5884],{},[40,7638,7639,7645,7648,7654],{},[43,7640,7641,7642],{},"It found a ",[22,7643,7644],{},"list object",[43,7646,7647],{},"Your code tried to use an attribute or method that lists do not have",[43,7649,1764,7650,7653],{},[22,7651,7652],{},"attribute"," can be a value or a method attached to an object",[43,7655,7656,7657,7660,7661],{},"Lists have methods like ",[17,7658,7659],{},"append()",", but they do not have methods like ",[17,7662,7394],{},[14,7664,580],{},[91,7666,7668],{"className":93,"code":7667,"language":95,"meta":96,"style":96},"words = [\"Hello\", \"World\"]\nwords.lower()\n",[17,7669,7670,7697],{"__ignoreMap":96},[100,7671,7672,7675,7677,7679,7681,7684,7686,7688,7690,7693,7695],{"class":102,"line":103},[100,7673,7674],{"class":106},"words ",[100,7676,111],{"class":110},[100,7678,594],{"class":118},[100,7680,206],{"class":205},[100,7682,7683],{"class":209},"Hello",[100,7685,206],{"class":205},[100,7687,126],{"class":118},[100,7689,1708],{"class":205},[100,7691,7692],{"class":209},"World",[100,7694,206],{"class":205},[100,7696,603],{"class":118},[100,7698,7699,7702,7704,7706],{"class":102,"line":135},[100,7700,7701],{"class":106},"words",[100,7703,290],{"class":118},[100,7705,5802],{"class":178},[100,7707,4734],{"class":118},[14,7709,7710],{},"This raises:",[91,7712,7714],{"className":93,"code":7713,"language":95,"meta":96,"style":96},"AttributeError: 'list' object has no attribute 'lower'\n",[17,7715,7716],{"__ignoreMap":96},[100,7717,7718,7720,7722,7724,7726,7728,7730,7732,7734,7736],{"class":102,"line":103},[100,7719,3738],{"class":191},[100,7721,89],{"class":118},[100,7723,1274],{"class":205},[100,7725,1235],{"class":209},[100,7727,1280],{"class":205},[100,7729,3914],{"class":191},[100,7731,3917],{"class":106},[100,7733,1280],{"class":205},[100,7735,5802],{"class":209},[100,7737,3925],{"class":205},[77,7739,7741],{"id":7740},"why-this-happens","Why this happens",[14,7743,7744],{},"This error often appears when:",[40,7746,7747,7750,7753,7756,7759],{},[43,7748,7749],{},"You expected a string but actually have a list",[43,7751,7752],{},"You expected a dictionary but actually have a list",[43,7754,7755],{},"You used a method from another type by mistake",[43,7757,7758],{},"A function returned a list and you forgot to check the result",[43,7760,7761],{},"Your variable name does not make the data type clear",[14,7763,7764,7765,290],{},"If you are not sure what type a variable contains, check it with ",[295,7766,7767],{"href":4427},[17,7768,4430],{},[77,7770,7772],{"id":7771},"common-examples-that-cause-the-error","Common examples that cause the error",[14,7774,7775],{},"Here are some common mistakes.",[675,7777,7779],{"id":7778},"calling-string-methods-on-a-list","Calling string methods on a list",[91,7781,7783],{"className":93,"code":7782,"language":95,"meta":96,"style":96},"names = [\"ALICE\", \"BOB\"]\nprint(names.lower())\n",[17,7784,7785,7812],{"__ignoreMap":96},[100,7786,7787,7790,7792,7794,7796,7799,7801,7803,7805,7808,7810],{"class":102,"line":103},[100,7788,7789],{"class":106},"names ",[100,7791,111],{"class":110},[100,7793,594],{"class":118},[100,7795,206],{"class":205},[100,7797,7798],{"class":209},"ALICE",[100,7800,206],{"class":205},[100,7802,126],{"class":118},[100,7804,1708],{"class":205},[100,7806,7807],{"class":209},"BOB",[100,7809,206],{"class":205},[100,7811,603],{"class":118},[100,7813,7814,7816,7818,7821,7823,7825],{"class":102,"line":135},[100,7815,372],{"class":114},[100,7817,170],{"class":118},[100,7819,7820],{"class":178},"names",[100,7822,290],{"class":118},[100,7824,5802],{"class":178},[100,7826,3370],{"class":118},[14,7828,7829,7831],{},[17,7830,7394],{}," is a string method, so this fails.",[675,7833,7835],{"id":7834},"calling-dictionary-methods-on-a-list","Calling dictionary methods on a list",[91,7837,7839],{"className":93,"code":7838,"language":95,"meta":96,"style":96},"items = [\"a\", \"b\", \"c\"]\nprint(items.keys())\n",[17,7840,7841,7873],{"__ignoreMap":96},[100,7842,7843,7845,7847,7849,7851,7853,7855,7857,7859,7861,7863,7865,7867,7869,7871],{"class":102,"line":103},[100,7844,3095],{"class":106},[100,7846,111],{"class":110},[100,7848,594],{"class":118},[100,7850,206],{"class":205},[100,7852,295],{"class":209},[100,7854,206],{"class":205},[100,7856,126],{"class":118},[100,7858,1708],{"class":205},[100,7860,4027],{"class":209},[100,7862,206],{"class":205},[100,7864,126],{"class":118},[100,7866,1708],{"class":205},[100,7868,4036],{"class":209},[100,7870,206],{"class":205},[100,7872,603],{"class":118},[100,7874,7875,7877,7879,7881,7883,7885],{"class":102,"line":135},[100,7876,372],{"class":114},[100,7878,170],{"class":118},[100,7880,3146],{"class":178},[100,7882,290],{"class":118},[100,7884,6185],{"class":178},[100,7886,3370],{"class":118},[14,7888,7889,7892],{},[17,7890,7891],{},"keys()"," is for dictionaries, not lists.",[675,7894,315,7896,7899],{"id":7895},"calling-items-on-a-list",[17,7897,7898],{},"items()"," on a list",[91,7901,7903],{"className":93,"code":7902,"language":95,"meta":96,"style":96},"data = [1, 2, 3]\nprint(data.items())\n",[17,7904,7905,7926],{"__ignoreMap":96},[100,7906,7907,7910,7912,7914,7916,7918,7920,7922,7924],{"class":102,"line":103},[100,7908,7909],{"class":106},"data ",[100,7911,111],{"class":110},[100,7913,594],{"class":118},[100,7915,123],{"class":122},[100,7917,126],{"class":118},[100,7919,129],{"class":122},[100,7921,126],{"class":118},[100,7923,1405],{"class":122},[100,7925,603],{"class":118},[100,7927,7928,7930,7932,7935,7937,7939],{"class":102,"line":135},[100,7929,372],{"class":114},[100,7931,170],{"class":118},[100,7933,7934],{"class":178},"data",[100,7936,290],{"class":118},[100,7938,3146],{"class":178},[100,7940,3370],{"class":118},[14,7942,7943,7944,7946],{},"Again, ",[17,7945,7898],{}," belongs to dictionaries.",[675,7948,7950],{"id":7949},"using-dot-notation-when-you-meant-indexing","Using dot notation when you meant indexing",[91,7952,7954],{"className":93,"code":7953,"language":95,"meta":96,"style":96},"letters = [\"a\", \"b\", \"c\"]\nprint(letters.upper())\n",[17,7955,7956,7989],{"__ignoreMap":96},[100,7957,7958,7961,7963,7965,7967,7969,7971,7973,7975,7977,7979,7981,7983,7985,7987],{"class":102,"line":103},[100,7959,7960],{"class":106},"letters ",[100,7962,111],{"class":110},[100,7964,594],{"class":118},[100,7966,206],{"class":205},[100,7968,295],{"class":209},[100,7970,206],{"class":205},[100,7972,126],{"class":118},[100,7974,1708],{"class":205},[100,7976,4027],{"class":209},[100,7978,206],{"class":205},[100,7980,126],{"class":118},[100,7982,1708],{"class":205},[100,7984,4036],{"class":209},[100,7986,206],{"class":205},[100,7988,603],{"class":118},[100,7990,7991,7993,7995,7998,8000,8002],{"class":102,"line":135},[100,7992,372],{"class":114},[100,7994,170],{"class":118},[100,7996,7997],{"class":178},"letters",[100,7999,290],{"class":118},[100,8001,4540],{"class":178},[100,8003,3370],{"class":118},[14,8005,8006],{},"If you wanted the first item and then the string method, use indexing:",[91,8008,8010],{"className":93,"code":8009,"language":95,"meta":96,"style":96},"letters = [\"a\", \"b\", \"c\"]\nprint(letters[0].upper())\n",[17,8011,8012,8044],{"__ignoreMap":96},[100,8013,8014,8016,8018,8020,8022,8024,8026,8028,8030,8032,8034,8036,8038,8040,8042],{"class":102,"line":103},[100,8015,7960],{"class":106},[100,8017,111],{"class":110},[100,8019,594],{"class":118},[100,8021,206],{"class":205},[100,8023,295],{"class":209},[100,8025,206],{"class":205},[100,8027,126],{"class":118},[100,8029,1708],{"class":205},[100,8031,4027],{"class":209},[100,8033,206],{"class":205},[100,8035,126],{"class":118},[100,8037,1708],{"class":205},[100,8039,4036],{"class":209},[100,8041,206],{"class":205},[100,8043,603],{"class":118},[100,8045,8046,8048,8050,8052,8054,8056,8059,8061],{"class":102,"line":135},[100,8047,372],{"class":114},[100,8049,170],{"class":118},[100,8051,7997],{"class":178},[100,8053,3166],{"class":118},[100,8055,2279],{"class":122},[100,8057,8058],{"class":118},"].",[100,8060,4540],{"class":178},[100,8062,3370],{"class":118},[14,8064,218],{},[91,8066,8068],{"className":93,"code":8067,"language":95,"meta":96,"style":96},"A\n",[17,8069,8070],{"__ignoreMap":96},[100,8071,8072],{"class":102,"line":103},[100,8073,8067],{"class":106},[675,8075,8077],{"id":8076},"assuming-all-objects-support-the-same-methods","Assuming all objects support the same methods",[14,8079,8080],{},"Different Python types have different methods.",[40,8082,8083,8091,8098],{},[43,8084,8085,8086,3266,8088],{},"Strings have methods like ",[17,8087,7394],{},[17,8089,8090],{},"split()",[43,8092,8093,8094,3266,8096],{},"Dictionaries have methods like ",[17,8095,7891],{},[17,8097,7898],{},[43,8099,7656,8100,3266,8102],{},[17,8101,7659],{},[17,8103,8104],{},"pop()",[14,8106,8107,8108,290],{},"If you need a refresher, see ",[295,8109,8111],{"href":8110},"\u002Flearn\u002Fpython-lists-explained-beginner-guide\u002F","Python lists explained",[77,8113,6244],{"id":6243},[14,8115,8116],{},"Use these steps to fix the error.",[40,8118,8119,8124,8127,8140,8143,8146],{},[43,8120,8121,8122],{},"Check what type the variable really is with ",[17,8123,4430],{},[43,8125,8126],{},"Print the variable before the failing line",[43,8128,8129,8130,5870,8132,5870,8135,5894,8137],{},"Use a real list method such as ",[17,8131,7659],{},[17,8133,8134],{},"extend()",[17,8136,8104],{},[17,8138,8139],{},"sort()",[43,8141,8142],{},"If you need one item, access it first with an index",[43,8144,8145],{},"If each item is a string, loop through the list or use a list comprehension",[43,8147,8148],{},"Rename variables so the data type is easier to recognize",[675,8150,8152],{"id":8151},"fix-1-check-the-type-first","Fix 1: Check the type first",[91,8154,8156],{"className":93,"code":8155,"language":95,"meta":96,"style":96},"value = [\"One\", \"Two\", \"Three\"]\n\nprint(value)\nprint(type(value))\n",[17,8157,8158,8193,8197,8207],{"__ignoreMap":96},[100,8159,8160,8162,8164,8166,8168,8171,8173,8175,8177,8180,8182,8184,8186,8189,8191],{"class":102,"line":103},[100,8161,3318],{"class":106},[100,8163,111],{"class":110},[100,8165,594],{"class":118},[100,8167,206],{"class":205},[100,8169,8170],{"class":209},"One",[100,8172,206],{"class":205},[100,8174,126],{"class":118},[100,8176,1708],{"class":205},[100,8178,8179],{"class":209},"Two",[100,8181,206],{"class":205},[100,8183,126],{"class":118},[100,8185,1708],{"class":205},[100,8187,8188],{"class":209},"Three",[100,8190,206],{"class":205},[100,8192,603],{"class":118},[100,8194,8195],{"class":102,"line":135},[100,8196,139],{"emptyLinePlaceholder":138},[100,8198,8199,8201,8203,8205],{"class":102,"line":142},[100,8200,372],{"class":114},[100,8202,170],{"class":118},[100,8204,757],{"class":178},[100,8206,215],{"class":118},[100,8208,8209,8211,8213,8215,8217,8219],{"class":102,"line":152},[100,8210,372],{"class":114},[100,8212,170],{"class":118},[100,8214,1250],{"class":191},[100,8216,170],{"class":118},[100,8218,757],{"class":178},[100,8220,182],{"class":118},[14,8222,218],{},[91,8224,8226],{"className":93,"code":8225,"language":95,"meta":96,"style":96},"['One', 'Two', 'Three']\n\u003Cclass 'list'>\n",[17,8227,8228,8256],{"__ignoreMap":96},[100,8229,8230,8232,8234,8236,8238,8240,8242,8244,8246,8248,8250,8252,8254],{"class":102,"line":103},[100,8231,3166],{"class":118},[100,8233,1280],{"class":205},[100,8235,8170],{"class":209},[100,8237,1280],{"class":205},[100,8239,126],{"class":118},[100,8241,1274],{"class":205},[100,8243,8179],{"class":209},[100,8245,1280],{"class":205},[100,8247,126],{"class":118},[100,8249,1274],{"class":205},[100,8251,8188],{"class":209},[100,8253,1280],{"class":205},[100,8255,603],{"class":118},[100,8257,8258,8260,8262,8264,8266,8268],{"class":102,"line":135},[100,8259,2782],{"class":110},[100,8261,3417],{"class":1077},[100,8263,1274],{"class":205},[100,8265,1235],{"class":209},[100,8267,1280],{"class":205},[100,8269,1980],{"class":110},[14,8271,8272],{},"Now you know the variable is a list.",[675,8274,8276],{"id":8275},"fix-2-use-a-real-list-method","Fix 2: Use a real list method",[14,8278,8279,8280,89],{},"If you wanted to add an item, use a list method like ",[295,8281,8283],{"href":8282},"\u002Freference\u002Fpython-list-append-method\u002F",[17,8284,7659],{},[91,8286,8288],{"className":93,"code":8287,"language":95,"meta":96,"style":96},"numbers = [1, 2, 3]\nnumbers.append(4)\n\nprint(numbers)\n",[17,8289,8290,8310,8325,8329],{"__ignoreMap":96},[100,8291,8292,8294,8296,8298,8300,8302,8304,8306,8308],{"class":102,"line":103},[100,8293,107],{"class":106},[100,8295,111],{"class":110},[100,8297,594],{"class":118},[100,8299,123],{"class":122},[100,8301,126],{"class":118},[100,8303,129],{"class":122},[100,8305,126],{"class":118},[100,8307,1405],{"class":122},[100,8309,603],{"class":118},[100,8311,8312,8314,8316,8318,8320,8323],{"class":102,"line":135},[100,8313,179],{"class":106},[100,8315,290],{"class":118},[100,8317,4254],{"class":178},[100,8319,170],{"class":118},[100,8321,8322],{"class":122},"4",[100,8324,215],{"class":118},[100,8326,8327],{"class":102,"line":142},[100,8328,139],{"emptyLinePlaceholder":138},[100,8330,8331,8333,8335,8337],{"class":102,"line":152},[100,8332,372],{"class":114},[100,8334,170],{"class":118},[100,8336,179],{"class":178},[100,8338,215],{"class":118},[14,8340,218],{},[91,8342,8344],{"className":93,"code":8343,"language":95,"meta":96,"style":96},"[1, 2, 3, 4]\n",[17,8345,8346],{"__ignoreMap":96},[100,8347,8348,8350,8352,8354,8356,8358,8360,8362,8364],{"class":102,"line":103},[100,8349,3166],{"class":118},[100,8351,123],{"class":122},[100,8353,126],{"class":118},[100,8355,129],{"class":122},[100,8357,126],{"class":118},[100,8359,1405],{"class":122},[100,8361,126],{"class":118},[100,8363,2584],{"class":122},[100,8365,603],{"class":118},[675,8367,8369],{"id":8368},"fix-3-access-one-item-first","Fix 3: Access one item first",[14,8371,8372],{},"If the list contains strings and you want to use a string method, get one string from the list first:",[91,8374,8376],{"className":93,"code":8375,"language":95,"meta":96,"style":96},"words = [\"HELLO\", \"WORLD\"]\nprint(words[0].lower())\n",[17,8377,8378,8403],{"__ignoreMap":96},[100,8379,8380,8382,8384,8386,8388,8390,8392,8394,8396,8399,8401],{"class":102,"line":103},[100,8381,7674],{"class":106},[100,8383,111],{"class":110},[100,8385,594],{"class":118},[100,8387,206],{"class":205},[100,8389,6597],{"class":209},[100,8391,206],{"class":205},[100,8393,126],{"class":118},[100,8395,1708],{"class":205},[100,8397,8398],{"class":209},"WORLD",[100,8400,206],{"class":205},[100,8402,603],{"class":118},[100,8404,8405,8407,8409,8411,8413,8415,8417,8419],{"class":102,"line":135},[100,8406,372],{"class":114},[100,8408,170],{"class":118},[100,8410,7701],{"class":178},[100,8412,3166],{"class":118},[100,8414,2279],{"class":122},[100,8416,8058],{"class":118},[100,8418,5802],{"class":178},[100,8420,3370],{"class":118},[14,8422,218],{},[91,8424,8425],{"className":93,"code":6651,"language":95,"meta":96,"style":96},[17,8426,8427],{"__ignoreMap":96},[100,8428,8429],{"class":102,"line":103},[100,8430,6651],{"class":106},[675,8432,8434],{"id":8433},"fix-4-apply-the-method-to-every-item","Fix 4: Apply the method to every item",[14,8436,8437],{},"If you want to change all strings in the list, loop through it or use a list comprehension:",[91,8439,8441],{"className":93,"code":8440,"language":95,"meta":96,"style":96},"words = [\"HELLO\", \"WORLD\"]\nlowered_words = [word.lower() for word in words]\n\nprint(lowered_words)\n",[17,8442,8443,8467,8497,8501],{"__ignoreMap":96},[100,8444,8445,8447,8449,8451,8453,8455,8457,8459,8461,8463,8465],{"class":102,"line":103},[100,8446,7674],{"class":106},[100,8448,111],{"class":110},[100,8450,594],{"class":118},[100,8452,206],{"class":205},[100,8454,6597],{"class":209},[100,8456,206],{"class":205},[100,8458,126],{"class":118},[100,8460,1708],{"class":205},[100,8462,8398],{"class":209},[100,8464,206],{"class":205},[100,8466,603],{"class":118},[100,8468,8469,8472,8474,8476,8479,8481,8483,8485,8487,8490,8492,8495],{"class":102,"line":135},[100,8470,8471],{"class":106},"lowered_words ",[100,8473,111],{"class":110},[100,8475,594],{"class":118},[100,8477,8478],{"class":106},"word",[100,8480,290],{"class":118},[100,8482,5802],{"class":178},[100,8484,7562],{"class":118},[100,8486,7565],{"class":145},[100,8488,8489],{"class":106}," word ",[100,8491,617],{"class":145},[100,8493,8494],{"class":106}," words",[100,8496,603],{"class":118},[100,8498,8499],{"class":102,"line":142},[100,8500,139],{"emptyLinePlaceholder":138},[100,8502,8503,8505,8507,8510],{"class":102,"line":152},[100,8504,372],{"class":114},[100,8506,170],{"class":118},[100,8508,8509],{"class":178},"lowered_words",[100,8511,215],{"class":118},[14,8513,218],{},[91,8515,8517],{"className":93,"code":8516,"language":95,"meta":96,"style":96},"['hello', 'world']\n",[17,8518,8519],{"__ignoreMap":96},[100,8520,8521,8523,8525,8527,8529,8531,8533,8536,8538],{"class":102,"line":103},[100,8522,3166],{"class":118},[100,8524,1280],{"class":205},[100,8526,3506],{"class":209},[100,8528,1280],{"class":205},[100,8530,126],{"class":118},[100,8532,1274],{"class":205},[100,8534,8535],{"class":209},"world",[100,8537,1280],{"class":205},[100,8539,603],{"class":118},[14,8541,8542,8543,290],{},"If you need help with loops, see ",[295,8544,8546],{"href":8545},"\u002Fhow-to\u002Fhow-to-loop-through-a-list-in-python\u002F","how to loop through a list in Python",[77,8548,8550],{"id":8549},"step-by-step-debugging-checklist","Step-by-step debugging checklist",[14,8552,8553],{},"When you see this error, try this process:",[3282,8555,8556,8559,8562,8565,8568],{},[43,8557,8558],{},"Read the exact method name in the error message",[43,8560,8561],{},"Find the variable before the dot",[43,8563,8564],{},"Print the variable and its type",[43,8566,8567],{},"Decide whether you need the whole list or one item from it",[43,8569,8570],{},"Replace the wrong method with the correct list method or item method",[14,8572,5295],{},[91,8574,8576],{"className":93,"code":8575,"language":95,"meta":96,"style":96},"my_list = [\"Hello\", \"World\"]\n\nprint(my_list)\nprint(type(my_list))\nprint(dir(my_list))\nprint(my_list[0])\nprint(type(my_list[0]))\n",[17,8577,8578,8602,8606,8616,8630,8644,8658],{"__ignoreMap":96},[100,8579,8580,8582,8584,8586,8588,8590,8592,8594,8596,8598,8600],{"class":102,"line":103},[100,8581,7405],{"class":106},[100,8583,111],{"class":110},[100,8585,594],{"class":118},[100,8587,206],{"class":205},[100,8589,7683],{"class":209},[100,8591,206],{"class":205},[100,8593,126],{"class":118},[100,8595,1708],{"class":205},[100,8597,7692],{"class":209},[100,8599,206],{"class":205},[100,8601,603],{"class":118},[100,8603,8604],{"class":102,"line":135},[100,8605,139],{"emptyLinePlaceholder":138},[100,8607,8608,8610,8612,8614],{"class":102,"line":142},[100,8609,372],{"class":114},[100,8611,170],{"class":118},[100,8613,7441],{"class":178},[100,8615,215],{"class":118},[100,8617,8618,8620,8622,8624,8626,8628],{"class":102,"line":152},[100,8619,372],{"class":114},[100,8621,170],{"class":118},[100,8623,1250],{"class":191},[100,8625,170],{"class":118},[100,8627,7441],{"class":178},[100,8629,182],{"class":118},[100,8631,8632,8634,8636,8638,8640,8642],{"class":102,"line":164},[100,8633,372],{"class":114},[100,8635,170],{"class":118},[100,8637,3822],{"class":114},[100,8639,170],{"class":118},[100,8641,7441],{"class":178},[100,8643,182],{"class":118},[100,8645,8646,8648,8650,8652,8654,8656],{"class":102,"line":185},[100,8647,372],{"class":114},[100,8649,170],{"class":118},[100,8651,7441],{"class":178},[100,8653,3166],{"class":118},[100,8655,2279],{"class":122},[100,8657,132],{"class":118},[100,8659,8660,8662,8664,8666,8668,8670,8672,8674],{"class":102,"line":197},[100,8661,372],{"class":114},[100,8663,170],{"class":118},[100,8665,1250],{"class":191},[100,8667,170],{"class":118},[100,8669,7441],{"class":178},[100,8671,3166],{"class":118},[100,8673,2279],{"class":122},[100,8675,8676],{"class":118},"]))\n",[14,8678,8679],{},"What these do:",[40,8681,8682,8687,8693,8699,8705],{},[43,8683,8684,7047],{},[17,8685,8686],{},"print(my_list)",[43,8688,8689,8692],{},[17,8690,8691],{},"print(type(my_list))"," shows that it is a list",[43,8694,8695,8698],{},[17,8696,8697],{},"print(dir(my_list))"," shows available list attributes and methods",[43,8700,8701,8704],{},[17,8702,8703],{},"print(my_list[0])"," shows the first item",[43,8706,8707,8710],{},[17,8708,8709],{},"print(type(my_list[0]))"," shows the type of that item",[77,8712,8714],{"id":8713},"when-you-meant-to-use-a-string-method","When you meant to use a string method",[14,8716,8717],{},"This is one of the most common versions of the error.",[14,8719,8720,8721,89],{},"Methods like these are ",[22,8722,8723],{},"string methods",[40,8725,8726,8730,8735,8740,8745],{},[43,8727,8728],{},[17,8729,7394],{},[43,8731,8732],{},[17,8733,8734],{},"upper()",[43,8736,8737],{},[17,8738,8739],{},"strip()",[43,8741,8742],{},[17,8743,8744],{},"replace()",[43,8746,8747],{},[17,8748,8090],{},[675,8750,8752],{"id":8751},"use-the-method-on-one-string-item","Use the method on one string item",[91,8754,8756],{"className":93,"code":8755,"language":95,"meta":96,"style":96},"fruits = [\"Apple\", \"Banana\", \"Cherry\"]\nprint(fruits[1].lower())\n",[17,8757,8758,8794],{"__ignoreMap":96},[100,8759,8760,8763,8765,8767,8769,8772,8774,8776,8778,8781,8783,8785,8787,8790,8792],{"class":102,"line":103},[100,8761,8762],{"class":106},"fruits ",[100,8764,111],{"class":110},[100,8766,594],{"class":118},[100,8768,206],{"class":205},[100,8770,8771],{"class":209},"Apple",[100,8773,206],{"class":205},[100,8775,126],{"class":118},[100,8777,1708],{"class":205},[100,8779,8780],{"class":209},"Banana",[100,8782,206],{"class":205},[100,8784,126],{"class":118},[100,8786,1708],{"class":205},[100,8788,8789],{"class":209},"Cherry",[100,8791,206],{"class":205},[100,8793,603],{"class":118},[100,8795,8796,8798,8800,8803,8805,8807,8809,8811],{"class":102,"line":135},[100,8797,372],{"class":114},[100,8799,170],{"class":118},[100,8801,8802],{"class":178},"fruits",[100,8804,3166],{"class":118},[100,8806,123],{"class":122},[100,8808,8058],{"class":118},[100,8810,5802],{"class":178},[100,8812,3370],{"class":118},[14,8814,218],{},[91,8816,8818],{"className":93,"code":8817,"language":95,"meta":96,"style":96},"banana\n",[17,8819,8820],{"__ignoreMap":96},[100,8821,8822],{"class":102,"line":103},[100,8823,8817],{"class":106},[675,8825,8827],{"id":8826},"use-the-method-on-every-string-in-the-list","Use the method on every string in the list",[91,8829,8831],{"className":93,"code":8830,"language":95,"meta":96,"style":96},"fruits = [\" Apple \", \" Banana \", \" Cherry \"]\ncleaned = [fruit.strip().lower() for fruit in fruits]\n\nprint(cleaned)\n",[17,8832,8833,8868,8903,8907],{"__ignoreMap":96},[100,8834,8835,8837,8839,8841,8843,8846,8848,8850,8852,8855,8857,8859,8861,8864,8866],{"class":102,"line":103},[100,8836,8762],{"class":106},[100,8838,111],{"class":110},[100,8840,594],{"class":118},[100,8842,206],{"class":205},[100,8844,8845],{"class":209}," Apple ",[100,8847,206],{"class":205},[100,8849,126],{"class":118},[100,8851,1708],{"class":205},[100,8853,8854],{"class":209}," Banana ",[100,8856,206],{"class":205},[100,8858,126],{"class":118},[100,8860,1708],{"class":205},[100,8862,8863],{"class":209}," Cherry ",[100,8865,206],{"class":205},[100,8867,603],{"class":118},[100,8869,8870,8873,8875,8877,8880,8882,8884,8887,8889,8891,8893,8896,8898,8901],{"class":102,"line":135},[100,8871,8872],{"class":106},"cleaned ",[100,8874,111],{"class":110},[100,8876,594],{"class":118},[100,8878,8879],{"class":106},"fruit",[100,8881,290],{"class":118},[100,8883,6235],{"class":178},[100,8885,8886],{"class":118},"().",[100,8888,5802],{"class":178},[100,8890,7562],{"class":118},[100,8892,7565],{"class":145},[100,8894,8895],{"class":106}," fruit ",[100,8897,617],{"class":145},[100,8899,8900],{"class":106}," fruits",[100,8902,603],{"class":118},[100,8904,8905],{"class":102,"line":142},[100,8906,139],{"emptyLinePlaceholder":138},[100,8908,8909,8911,8913,8916],{"class":102,"line":152},[100,8910,372],{"class":114},[100,8912,170],{"class":118},[100,8914,8915],{"class":178},"cleaned",[100,8917,215],{"class":118},[14,8919,218],{},[91,8921,8923],{"className":93,"code":8922,"language":95,"meta":96,"style":96},"['apple', 'banana', 'cherry']\n",[17,8924,8925],{"__ignoreMap":96},[100,8926,8927,8929,8931,8933,8935,8937,8939,8941,8943,8945,8947,8950,8952],{"class":102,"line":103},[100,8928,3166],{"class":118},[100,8930,1280],{"class":205},[100,8932,4148],{"class":209},[100,8934,1280],{"class":205},[100,8936,126],{"class":118},[100,8938,1274],{"class":205},[100,8940,4157],{"class":209},[100,8942,1280],{"class":205},[100,8944,126],{"class":118},[100,8946,1274],{"class":205},[100,8948,8949],{"class":209},"cherry",[100,8951,1280],{"class":205},[100,8953,603],{"class":118},[14,8955,8956,8957,290],{},"If you meant to split a single string, not a list, see ",[295,8958,8960],{"href":8959},"\u002Fhow-to\u002Fhow-to-split-a-string-in-python\u002F","how to split a string in Python",[77,8962,7081],{"id":7080},[14,8964,8965],{},"Some similar errors come from the same basic problem: the object is not the type you expected.",[40,8967,8968,8975,8982,8991],{},[43,8969,8970,8974],{},[295,8971,8973],{"href":8972},"\u002Ferrors\u002Fattributeerror-in-python-causes-and-fixes\u002F","AttributeError in Python: causes and fixes"," for the general pattern",[43,8976,8977,8981],{},[295,8978,5728,8979],{"href":5524},[17,8980,5528],{}," when the object is a string",[43,8983,8984,8988,8989],{},[295,8985,5728,8986],{"href":4967},[17,8987,4970],{}," when a variable is ",[17,8990,930],{},[43,8992,8993,8996],{},[17,8994,8995],{},"TypeError: list indices must be integers or slices"," when list access syntax is wrong",[77,8998,7117],{"id":7116},[14,9000,9001],{},"These are the most common causes of this error:",[40,9003,9004,9007,9010,9013,9016],{},[43,9005,9006],{},"Using string methods on a list",[43,9008,9009],{},"Using dictionary methods on a list",[43,9011,9012],{},"Confusing one list item with the whole list",[43,9014,9015],{},"Getting an unexpected function return value",[43,9017,9018],{},"Reassigning a variable so its type changes",[14,9020,9021],{},"Example of variable reassignment changing the type:",[91,9023,9025],{"className":93,"code":9024,"language":95,"meta":96,"style":96},"data = \"hello\"\nprint(data.upper())\n\ndata = [\"hello\", \"world\"]\nprint(data.upper())\n",[17,9026,9027,9039,9053,9057,9081],{"__ignoreMap":96},[100,9028,9029,9031,9033,9035,9037],{"class":102,"line":103},[100,9030,7909],{"class":106},[100,9032,111],{"class":110},[100,9034,1708],{"class":205},[100,9036,3506],{"class":209},[100,9038,1714],{"class":205},[100,9040,9041,9043,9045,9047,9049,9051],{"class":102,"line":135},[100,9042,372],{"class":114},[100,9044,170],{"class":118},[100,9046,7934],{"class":178},[100,9048,290],{"class":118},[100,9050,4540],{"class":178},[100,9052,3370],{"class":118},[100,9054,9055],{"class":102,"line":142},[100,9056,139],{"emptyLinePlaceholder":138},[100,9058,9059,9061,9063,9065,9067,9069,9071,9073,9075,9077,9079],{"class":102,"line":152},[100,9060,7909],{"class":106},[100,9062,111],{"class":110},[100,9064,594],{"class":118},[100,9066,206],{"class":205},[100,9068,3506],{"class":209},[100,9070,206],{"class":205},[100,9072,126],{"class":118},[100,9074,1708],{"class":205},[100,9076,8535],{"class":209},[100,9078,206],{"class":205},[100,9080,603],{"class":118},[100,9082,9083,9085,9087,9089,9091,9093],{"class":102,"line":164},[100,9084,372],{"class":114},[100,9086,170],{"class":118},[100,9088,7934],{"class":178},[100,9090,290],{"class":118},[100,9092,4540],{"class":178},[100,9094,3370],{"class":118},[14,9096,9097,9098,9100,9101,9103,9104,9106],{},"The first ",[17,9099,8734],{}," works because ",[17,9102,7934],{}," is a string. The second fails because ",[17,9105,7934],{}," is now a list.",[77,9108,1514],{"id":1513},[675,9110,9112],{"id":9111},"why-does-python-say-a-list-has-no-attribute","Why does Python say a list has no attribute?",[14,9114,9115],{},"Because you called a method or attribute that is not defined for list objects.",[675,9117,9119],{"id":9118},"how-do-i-know-what-methods-a-list-has","How do I know what methods a list has?",[14,9121,241,9122,9125,9126,5870,9128,5870,9130,5629,9132,290],{},[17,9123,9124],{},"dir(your_list)"," or check list method reference pages such as ",[17,9127,7659],{},[17,9129,8134],{},[17,9131,8104],{},[17,9133,8139],{},[675,9135,9137,9138,9140],{"id":9136},"why-does-lower-fail-on-a-list","Why does ",[17,9139,7394],{}," fail on a list?",[14,9142,9143,9145],{},[17,9144,7394],{}," is a string method, not a list method. Use it on one string item or on each item in the list.",[675,9147,9149],{"id":9148},"how-do-i-fix-this-if-my-list-contains-strings","How do I fix this if my list contains strings?",[14,9151,9152,9153,9156],{},"Use indexing for one item, like ",[17,9154,9155],{},"my_list[0].lower()",", or use a loop or list comprehension for all items.",[77,9158,1554],{"id":1553},[40,9160,9161,9165,9169,9176,9182],{},[43,9162,9163],{},[295,9164,8973],{"href":8972},[43,9166,9167],{},[295,9168,8111],{"href":8110},[43,9170,9171],{},[295,9172,9173,9174,4112],{"href":8282},"Python list ",[17,9175,7659],{},[43,9177,9178],{},[295,9179,1572,9180,1576],{"href":4427},[17,9181,4430],{},[43,9183,9184],{},[295,9185,9186],{"href":8545},"How to loop through a list in Python",[14,9188,9189],{},"Check the object type first. Then choose the correct list method or work with a single item from the list.",[1589,9191,9192],{},"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 .sP7_E, html code.shiki .sP7_E{--shiki-light:#39ADB5;--shiki-default:#24292E;--shiki-dark:#E1E4E8}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 .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 .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 .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 .sptTA, html code.shiki .sptTA{--shiki-light:#6182B8;--shiki-default:#005CC5;--shiki-dark:#79B8FF}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}",{"title":96,"searchDepth":135,"depth":135,"links":9194},[9195,9196,9197,9198,9206,9212,9213,9217,9218,9219,9226],{"id":79,"depth":135,"text":80},{"id":5880,"depth":135,"text":5881},{"id":7740,"depth":135,"text":7741},{"id":7771,"depth":135,"text":7772,"children":9199},[9200,9201,9202,9204,9205],{"id":7778,"depth":142,"text":7779},{"id":7834,"depth":142,"text":7835},{"id":7895,"depth":142,"text":9203},"Calling items() on a list",{"id":7949,"depth":142,"text":7950},{"id":8076,"depth":142,"text":8077},{"id":6243,"depth":135,"text":6244,"children":9207},[9208,9209,9210,9211],{"id":8151,"depth":142,"text":8152},{"id":8275,"depth":142,"text":8276},{"id":8368,"depth":142,"text":8369},{"id":8433,"depth":142,"text":8434},{"id":8549,"depth":135,"text":8550},{"id":8713,"depth":135,"text":8714,"children":9214},[9215,9216],{"id":8751,"depth":142,"text":8752},{"id":8826,"depth":142,"text":8827},{"id":7080,"depth":135,"text":7081},{"id":7116,"depth":135,"text":7117},{"id":1513,"depth":135,"text":1514,"children":9220},[9221,9222,9223,9225],{"id":9111,"depth":142,"text":9112},{"id":9118,"depth":142,"text":9119},{"id":9136,"depth":142,"text":9224},"Why does lower() fail on a list?",{"id":9148,"depth":142,"text":9149},{"id":1553,"depth":135,"text":1554},"Master attributeerror list object has no attribute fix in our comprehensive Python beginner guide.",{},{"title":7367,"description":9227},"errors\u002Fattributeerror-list-object-has-no-attribute-fix","EeUM3stGJtB8BHxRFLlCN0Z0z7V2qGIONPQ0VKfs6Ok",{"id":9233,"title":9234,"body":9235,"description":10724,"extension":1623,"meta":10725,"navigation":138,"path":10726,"seo":10727,"stem":10728,"__hash__":10729},"content\u002Ferrors\u002Fattributeerror-module-has-no-attribute-fix.md","AttributeError: module has no attribute (Fix)",{"type":7,"value":9236,"toc":10697},[9237,9240,9248,9250,9307,9310,9324,9326,9329,9343,9349,9363,9376,9382,9384,9414,9417,9436,9439,9467,9475,9505,9509,9512,9532,9536,9539,9546,9563,9572,9601,9604,9669,9672,9683,9687,9690,9692,9707,9710,9748,9751,9788,9797,9822,9830,9834,9837,9840,9862,9869,9874,9910,9919,9921,9955,9966,9970,9973,10000,10003,10033,10036,10043,10072,10078,10125,10131,10138,10142,10145,10149,10183,10188,10192,10228,10236,10240,10277,10285,10288,10317,10323,10326,10330,10333,10336,10347,10350,10366,10369,10380,10384,10387,10430,10433,10484,10486,10489,10510,10512,10516,10519,10530,10534,10540,10542,10597,10600,10604,10607,10610,10620,10624,10630,10633,10644,10646,10684,10694],[10,9238,9234],{"id":9239},"attributeerror-module-has-no-attribute-fix",[14,9241,5735,9242,9247],{},[22,9243,9244],{},[17,9245,9246],{},"AttributeError: module has no attribute"," by checking what module Python imported, whether the attribute name is correct, and whether your own file name is hiding the real module.",[77,9249,80],{"id":79},[91,9251,9253],{"className":93,"code":9252,"language":95,"meta":96,"style":96},"import math\nprint(math.sqrt(9))\n\n# Check what names the module actually has\nprint(dir(math))\n",[17,9254,9255,9263,9284,9288,9293],{"__ignoreMap":96},[100,9256,9257,9260],{"class":102,"line":103},[100,9258,9259],{"class":145},"import",[100,9261,9262],{"class":106}," math\n",[100,9264,9265,9267,9269,9272,9274,9277,9279,9282],{"class":102,"line":135},[100,9266,372],{"class":114},[100,9268,170],{"class":118},[100,9270,9271],{"class":178},"math",[100,9273,290],{"class":118},[100,9275,9276],{"class":178},"sqrt",[100,9278,170],{"class":118},[100,9280,9281],{"class":122},"9",[100,9283,182],{"class":118},[100,9285,9286],{"class":102,"line":142},[100,9287,139],{"emptyLinePlaceholder":138},[100,9289,9290],{"class":102,"line":152},[100,9291,9292],{"class":414},"# Check what names the module actually has\n",[100,9294,9295,9297,9299,9301,9303,9305],{"class":102,"line":164},[100,9296,372],{"class":114},[100,9298,170],{"class":118},[100,9300,3822],{"class":114},[100,9302,170],{"class":118},[100,9304,9271],{"class":178},[100,9306,182],{"class":118},[14,9308,9309],{},"If the attribute is missing, check:",[40,9311,9312,9315,9321],{},[43,9313,9314],{},"the spelling",[43,9316,9317,9318,9320],{},"your ",[17,9319,9259],{}," statement",[43,9322,9323],{},"whether you named your own file the same as the module",[77,9325,5881],{"id":5880},[14,9327,9328],{},"This error means:",[40,9330,9331,9336],{},[43,9332,9333,9334],{},"Python successfully imported a ",[22,9335,1977],{},[43,9337,9338,9339,9342],{},"but Python could not find the ",[22,9340,9341],{},"attribute name"," you tried to use",[14,9344,9345,9346,9348],{},"In this error, an ",[22,9347,7652],{}," can be:",[40,9350,9351,9354,9357,9360],{},[43,9352,9353],{},"a function",[43,9355,9356],{},"a class",[43,9358,9359],{},"a variable",[43,9361,9362],{},"a submodule",[14,9364,9365,9366,9369,9370,9373,9374,290],{},"So if you write something like ",[17,9367,9368],{},"module_name.some_name",", Python expects ",[17,9371,9372],{},"some_name"," to exist inside that module. If it does not, you get an ",[17,9375,3738],{},[14,9377,9378,9379,290],{},"If you want a deeper explanation of attributes on non-module objects, see ",[295,9380,9381],{"href":7094},"AttributeError: object has no attribute (Fix)",[77,9383,5945],{"id":5944},[91,9385,9387],{"className":93,"code":9386,"language":95,"meta":96,"style":96},"import math\n\nprint(math.random())\n",[17,9388,9389,9395,9399],{"__ignoreMap":96},[100,9390,9391,9393],{"class":102,"line":103},[100,9392,9259],{"class":145},[100,9394,9262],{"class":106},[100,9396,9397],{"class":102,"line":135},[100,9398,139],{"emptyLinePlaceholder":138},[100,9400,9401,9403,9405,9407,9409,9412],{"class":102,"line":142},[100,9402,372],{"class":114},[100,9404,170],{"class":118},[100,9406,9271],{"class":178},[100,9408,290],{"class":118},[100,9410,9411],{"class":178},"random",[100,9413,3370],{"class":118},[14,9415,9416],{},"This raises an error because:",[40,9418,9419,9425],{},[43,9420,9421,9422,9424],{},"the ",[17,9423,9271],{}," module exists",[43,9426,9427,9428,2025,9431,9433,9434],{},"but ",[17,9429,9430],{},"random()",[22,9432,1059],{}," part of ",[17,9435,9271],{},[14,9437,9438],{},"You would see an error like this:",[91,9440,9442],{"className":93,"code":9441,"language":95,"meta":96,"style":96},"AttributeError: module 'math' has no attribute 'random'\n",[17,9443,9444],{"__ignoreMap":96},[100,9445,9446,9448,9450,9453,9455,9457,9459,9461,9463,9465],{"class":102,"line":103},[100,9447,3738],{"class":191},[100,9449,89],{"class":118},[100,9451,9452],{"class":106}," module ",[100,9454,1280],{"class":205},[100,9456,9271],{"class":209},[100,9458,1280],{"class":205},[100,9460,3917],{"class":106},[100,9462,1280],{"class":205},[100,9464,9411],{"class":209},[100,9466,3925],{"class":205},[14,9468,9469,9470,9472,9473,89],{},"The correct module here is ",[17,9471,9411],{},", not ",[17,9474,9271],{},[91,9476,9478],{"className":93,"code":9477,"language":95,"meta":96,"style":96},"import random\n\nprint(random.random())\n",[17,9479,9480,9487,9491],{"__ignoreMap":96},[100,9481,9482,9484],{"class":102,"line":103},[100,9483,9259],{"class":145},[100,9485,9486],{"class":106}," random\n",[100,9488,9489],{"class":102,"line":135},[100,9490,139],{"emptyLinePlaceholder":138},[100,9492,9493,9495,9497,9499,9501,9503],{"class":102,"line":142},[100,9494,372],{"class":114},[100,9496,170],{"class":118},[100,9498,9411],{"class":178},[100,9500,290],{"class":118},[100,9502,9411],{"class":178},[100,9504,3370],{"class":118},[77,9506,9508],{"id":9507},"most-common-causes","Most common causes",[14,9510,9511],{},"The most common reasons are:",[40,9513,9514,9517,9520,9523,9526,9529],{},[43,9515,9516],{},"misspelled attribute name",[43,9518,9519],{},"using the wrong module",[43,9521,9522],{},"naming your own file the same as a standard library module",[43,9524,9525],{},"importing the module in a different way than expected",[43,9527,9528],{},"using code from a different Python version",[43,9530,9531],{},"trying to use a submodule without importing it correctly",[77,9533,9535],{"id":9534},"fix-1-check-the-attribute-name","Fix 1: Check the attribute name",[14,9537,9538],{},"A very common cause is simply typing the wrong name.",[14,9540,9541,9542,9545],{},"Python is ",[22,9543,9544],{},"case-sensitive",", so these are different:",[40,9547,9548,9553,9558],{},[43,9549,9550],{},[17,9551,9552],{},"math.sqrt",[43,9554,9555],{},[17,9556,9557],{},"math.Sqrt",[43,9559,9560],{},[17,9561,9562],{},"math.SQRT",[14,9564,241,9565,9571],{},[295,9566,9568,9570],{"href":9567},"\u002Freference\u002Fpython-dir-function-explained",[17,9569,4565],{}," explained"," to see what names a module really has.",[91,9573,9575],{"className":93,"code":9574,"language":95,"meta":96,"style":96},"import math\n\nprint(dir(math))\n",[17,9576,9577,9583,9587],{"__ignoreMap":96},[100,9578,9579,9581],{"class":102,"line":103},[100,9580,9259],{"class":145},[100,9582,9262],{"class":106},[100,9584,9585],{"class":102,"line":135},[100,9586,139],{"emptyLinePlaceholder":138},[100,9588,9589,9591,9593,9595,9597,9599],{"class":102,"line":142},[100,9590,372],{"class":114},[100,9592,170],{"class":118},[100,9594,3822],{"class":114},[100,9596,170],{"class":118},[100,9598,9271],{"class":178},[100,9600,182],{"class":118},[14,9602,9603],{},"If you are looking for a specific name:",[91,9605,9607],{"className":93,"code":9606,"language":95,"meta":96,"style":96},"import math\n\nprint(\"sqrt\" in dir(math))     # True\nprint(\"random\" in dir(math))   # False\n",[17,9608,9609,9615,9619,9644],{"__ignoreMap":96},[100,9610,9611,9613],{"class":102,"line":103},[100,9612,9259],{"class":145},[100,9614,9262],{"class":106},[100,9616,9617],{"class":102,"line":135},[100,9618,139],{"emptyLinePlaceholder":138},[100,9620,9621,9623,9625,9627,9629,9631,9633,9635,9637,9639,9641],{"class":102,"line":142},[100,9622,372],{"class":114},[100,9624,170],{"class":118},[100,9626,206],{"class":205},[100,9628,9276],{"class":209},[100,9630,206],{"class":205},[100,9632,1971],{"class":145},[100,9634,4599],{"class":114},[100,9636,170],{"class":118},[100,9638,9271],{"class":178},[100,9640,411],{"class":118},[100,9642,9643],{"class":414},"     # True\n",[100,9645,9646,9648,9650,9652,9654,9656,9658,9660,9662,9664,9666],{"class":102,"line":152},[100,9647,372],{"class":114},[100,9649,170],{"class":118},[100,9651,206],{"class":205},[100,9653,9411],{"class":209},[100,9655,206],{"class":205},[100,9657,1971],{"class":145},[100,9659,4599],{"class":114},[100,9661,170],{"class":118},[100,9663,9271],{"class":178},[100,9665,411],{"class":118},[100,9667,9668],{"class":414},"   # False\n",[14,9670,9671],{},"This helps you quickly spot:",[40,9673,9674,9677,9680],{},[43,9675,9676],{},"spelling mistakes",[43,9678,9679],{},"wrong capitalization",[43,9681,9682],{},"names that do not exist in that module",[77,9684,9686],{"id":9685},"fix-2-make-sure-you-imported-the-correct-module","Fix 2: Make sure you imported the correct module",[14,9688,9689],{},"Beginners often expect a function to be in one module when it actually belongs to another.",[14,9691,1844],{},[40,9693,9694,9702],{},[43,9695,9696,9699,9700],{},[17,9697,9698],{},"random.randint()"," belongs to ",[17,9701,9411],{},[43,9703,9704,9705],{},"not ",[17,9706,9271],{},[14,9708,9709],{},"Wrong:",[91,9711,9713],{"className":93,"code":9712,"language":95,"meta":96,"style":96},"import math\n\nprint(math.randint(1, 10))\n",[17,9714,9715,9721,9725],{"__ignoreMap":96},[100,9716,9717,9719],{"class":102,"line":103},[100,9718,9259],{"class":145},[100,9720,9262],{"class":106},[100,9722,9723],{"class":102,"line":135},[100,9724,139],{"emptyLinePlaceholder":138},[100,9726,9727,9729,9731,9733,9735,9738,9740,9742,9744,9746],{"class":102,"line":142},[100,9728,372],{"class":114},[100,9730,170],{"class":118},[100,9732,9271],{"class":178},[100,9734,290],{"class":118},[100,9736,9737],{"class":178},"randint",[100,9739,170],{"class":118},[100,9741,123],{"class":122},[100,9743,126],{"class":118},[100,9745,6550],{"class":122},[100,9747,182],{"class":118},[14,9749,9750],{},"Correct:",[91,9752,9754],{"className":93,"code":9753,"language":95,"meta":96,"style":96},"import random\n\nprint(random.randint(1, 10))\n",[17,9755,9756,9762,9766],{"__ignoreMap":96},[100,9757,9758,9760],{"class":102,"line":103},[100,9759,9259],{"class":145},[100,9761,9486],{"class":106},[100,9763,9764],{"class":102,"line":135},[100,9765,139],{"emptyLinePlaceholder":138},[100,9767,9768,9770,9772,9774,9776,9778,9780,9782,9784,9786],{"class":102,"line":142},[100,9769,372],{"class":114},[100,9771,170],{"class":118},[100,9773,9411],{"class":178},[100,9775,290],{"class":118},[100,9777,9737],{"class":178},[100,9779,170],{"class":118},[100,9781,123],{"class":122},[100,9783,126],{"class":118},[100,9785,6550],{"class":122},[100,9787,182],{"class":118},[14,9789,9790,9791,89],{},"If you are unsure what a module contains, use ",[295,9792,9794,9570],{"href":9793},"\u002Freference\u002Fpython-help-function-explained",[17,9795,9796],{},"help()",[91,9798,9800],{"className":93,"code":9799,"language":95,"meta":96,"style":96},"import random\n\nhelp(random)\n",[17,9801,9802,9808,9812],{"__ignoreMap":96},[100,9803,9804,9806],{"class":102,"line":103},[100,9805,9259],{"class":145},[100,9807,9486],{"class":106},[100,9809,9810],{"class":102,"line":135},[100,9811,139],{"emptyLinePlaceholder":138},[100,9813,9814,9816,9818,9820],{"class":102,"line":142},[100,9815,2478],{"class":114},[100,9817,170],{"class":118},[100,9819,9411],{"class":178},[100,9821,215],{"class":118},[14,9823,9824,9825,9829],{},"You can also review ",[295,9826,9828],{"href":9827},"\u002Fhow-to\u002Fhow-to-import-a-module-in-python","how to import a module in Python"," if import statements still feel confusing.",[77,9831,9833],{"id":9832},"fix-3-check-for-file-naming-conflicts","Fix 3: Check for file naming conflicts",[14,9835,9836],{},"This is one of the most common beginner mistakes.",[14,9838,9839],{},"If your file is named something like:",[40,9841,9842,9847,9852,9857],{},[43,9843,9844],{},[17,9845,9846],{},"random.py",[43,9848,9849],{},[17,9850,9851],{},"math.py",[43,9853,9854],{},[17,9855,9856],{},"json.py",[43,9858,9859],{},[17,9860,9861],{},"os.py",[14,9863,9864,9865,9868],{},"then Python may import ",[22,9866,9867],{},"your file"," instead of the real module.",[14,9870,9871,9872,89],{},"For example, if your script is named ",[17,9873,9846],{},[91,9875,9876],{"className":93,"code":9753,"language":95,"meta":96,"style":96},[17,9877,9878,9884,9888],{"__ignoreMap":96},[100,9879,9880,9882],{"class":102,"line":103},[100,9881,9259],{"class":145},[100,9883,9486],{"class":106},[100,9885,9886],{"class":102,"line":135},[100,9887,139],{"emptyLinePlaceholder":138},[100,9889,9890,9892,9894,9896,9898,9900,9902,9904,9906,9908],{"class":102,"line":142},[100,9891,372],{"class":114},[100,9893,170],{"class":118},[100,9895,9411],{"class":178},[100,9897,290],{"class":118},[100,9899,9737],{"class":178},[100,9901,170],{"class":118},[100,9903,123],{"class":122},[100,9905,126],{"class":118},[100,9907,6550],{"class":122},[100,9909,182],{"class":118},[14,9911,9912,9913,9915,9916,9918],{},"you may get an error because Python imported your own ",[17,9914,9846],{}," file, not the standard library ",[17,9917,9411],{}," module.",[675,9920,6244],{"id":6243},[3282,9922,9923,9936,9952],{},[43,9924,9925,9926,9929,9930,9932,9933],{},"Rename your file",[9927,9928],"br",{},"\nExample: change ",[17,9931,9846],{}," to ",[17,9934,9935],{},"my_random_test.py",[43,9937,9938,9939],{},"Delete cached files if they exist:",[40,9940,9941,9946],{},[43,9942,9943],{},[17,9944,9945],{},"__pycache__\u002F",[43,9947,9948,9951],{},[17,9949,9950],{},".pyc"," files",[43,9953,9954],{},"Run the script again",[14,9956,9957,9958,3266,9962,290],{},"This problem is also related to import errors such as ",[295,9959,9961],{"href":9960},"\u002Ferrors\u002Fimporterror-no-module-named-x-fix","ImportError: No module named X",[295,9963,9965],{"href":9964},"\u002Ferrors\u002Fimporterror-cannot-import-name-fix","ImportError: cannot import name",[77,9967,9969],{"id":9968},"fix-4-check-what-python-actually-imported","Fix 4: Check what Python actually imported",[14,9971,9972],{},"If you are not sure what Python imported, print the module itself.",[91,9974,9976],{"className":93,"code":9975,"language":95,"meta":96,"style":96},"import json\n\nprint(json)\n",[17,9977,9978,9985,9989],{"__ignoreMap":96},[100,9979,9980,9982],{"class":102,"line":103},[100,9981,9259],{"class":145},[100,9983,9984],{"class":106}," json\n",[100,9986,9987],{"class":102,"line":135},[100,9988,139],{"emptyLinePlaceholder":138},[100,9990,9991,9993,9995,9998],{"class":102,"line":142},[100,9992,372],{"class":114},[100,9994,170],{"class":118},[100,9996,9997],{"class":178},"json",[100,9999,215],{"class":118},[14,10001,10002],{},"You might see output like:",[91,10004,10006],{"className":93,"code":10005,"language":95,"meta":96,"style":96},"\u003Cmodule 'json' from '\u002FUsers\u002Fyou\u002Fproject\u002Fjson.py'>\n",[17,10007,10008],{"__ignoreMap":96},[100,10009,10010,10012,10015,10017,10019,10021,10024,10026,10029,10031],{"class":102,"line":103},[100,10011,2782],{"class":110},[100,10013,10014],{"class":106},"module ",[100,10016,1280],{"class":205},[100,10018,9997],{"class":209},[100,10020,1280],{"class":205},[100,10022,10023],{"class":145}," from",[100,10025,1274],{"class":205},[100,10027,10028],{"class":209},"\u002FUsers\u002Fyou\u002Fproject\u002Fjson.py",[100,10030,1280],{"class":205},[100,10032,1980],{"class":110},[14,10034,10035],{},"If that path points to your project file, then your file is shadowing the real module.",[14,10037,10038,10039,10042],{},"You can also inspect ",[17,10040,10041],{},"__file__"," directly:",[91,10044,10046],{"className":93,"code":10045,"language":95,"meta":96,"style":96},"import json\n\nprint(json.__file__)\n",[17,10047,10048,10054,10058],{"__ignoreMap":96},[100,10049,10050,10052],{"class":102,"line":103},[100,10051,9259],{"class":145},[100,10053,9984],{"class":106},[100,10055,10056],{"class":102,"line":135},[100,10057,139],{"emptyLinePlaceholder":138},[100,10059,10060,10062,10064,10066,10068,10070],{"class":102,"line":142},[100,10061,372],{"class":114},[100,10063,170],{"class":118},[100,10065,9997],{"class":178},[100,10067,290],{"class":118},[100,10069,10041],{"class":527},[100,10071,215],{"class":118},[14,10073,10074,10075,10077],{},"For some modules, ",[17,10076,10041],{}," may not exist, so a safer version is:",[91,10079,10081],{"className":93,"code":10080,"language":95,"meta":96,"style":96},"import json\n\nprint(getattr(json, \"__file__\", \"built-in module\"))\n",[17,10082,10083,10089,10093],{"__ignoreMap":96},[100,10084,10085,10087],{"class":102,"line":103},[100,10086,9259],{"class":145},[100,10088,9984],{"class":106},[100,10090,10091],{"class":102,"line":135},[100,10092,139],{"emptyLinePlaceholder":138},[100,10094,10095,10097,10099,10102,10104,10106,10108,10110,10112,10114,10116,10118,10121,10123],{"class":102,"line":142},[100,10096,372],{"class":114},[100,10098,170],{"class":118},[100,10100,10101],{"class":114},"getattr",[100,10103,170],{"class":118},[100,10105,9997],{"class":178},[100,10107,126],{"class":118},[100,10109,1708],{"class":205},[100,10111,10041],{"class":209},[100,10113,206],{"class":205},[100,10115,126],{"class":118},[100,10117,1708],{"class":205},[100,10119,10120],{"class":209},"built-in module",[100,10122,206],{"class":205},[100,10124,182],{"class":118},[14,10126,10127,10128,290],{},"If the path is wrong, rename the conflicting file and remove ",[17,10129,10130],{},"__pycache__",[14,10132,10133,10134,290],{},"If you want to understand why Python chooses one file over another, read ",[295,10135,10137],{"href":10136},"\u002Flearn\u002Fhow-import-works-in-python","how import works in Python",[77,10139,10141],{"id":10140},"fix-5-verify-your-import-style","Fix 5: Verify your import style",[14,10143,10144],{},"Different import styles give you access to names in different ways.",[675,10146,10148],{"id":10147},"style-1-import-the-module","Style 1: Import the module",[91,10150,10152],{"className":93,"code":10151,"language":95,"meta":96,"style":96},"import math\n\nprint(math.sqrt(16))\n",[17,10153,10154,10160,10164],{"__ignoreMap":96},[100,10155,10156,10158],{"class":102,"line":103},[100,10157,9259],{"class":145},[100,10159,9262],{"class":106},[100,10161,10162],{"class":102,"line":135},[100,10163,139],{"emptyLinePlaceholder":138},[100,10165,10166,10168,10170,10172,10174,10176,10178,10181],{"class":102,"line":142},[100,10167,372],{"class":114},[100,10169,170],{"class":118},[100,10171,9271],{"class":178},[100,10173,290],{"class":118},[100,10175,9276],{"class":178},[100,10177,170],{"class":118},[100,10179,10180],{"class":122},"16",[100,10182,182],{"class":118},[14,10184,10185,10186,290],{},"Here you must use ",[17,10187,9552],{},[675,10189,10191],{"id":10190},"style-2-import-one-name-directly","Style 2: Import one name directly",[91,10193,10195],{"className":93,"code":10194,"language":95,"meta":96,"style":96},"from math import sqrt\n\nprint(sqrt(16))\n",[17,10196,10197,10210,10214],{"__ignoreMap":96},[100,10198,10199,10202,10205,10207],{"class":102,"line":103},[100,10200,10201],{"class":145},"from",[100,10203,10204],{"class":106}," math ",[100,10206,9259],{"class":145},[100,10208,10209],{"class":106}," sqrt\n",[100,10211,10212],{"class":102,"line":135},[100,10213,139],{"emptyLinePlaceholder":138},[100,10215,10216,10218,10220,10222,10224,10226],{"class":102,"line":142},[100,10217,372],{"class":114},[100,10219,170],{"class":118},[100,10221,9276],{"class":178},[100,10223,170],{"class":118},[100,10225,10180],{"class":122},[100,10227,182],{"class":118},[14,10229,10230,10231,10233,10234,290],{},"Here you use ",[17,10232,9276],{}," directly, not ",[17,10235,9552],{},[675,10237,10239],{"id":10238},"a-common-mistake","A common mistake",[91,10241,10243],{"className":93,"code":10242,"language":95,"meta":96,"style":96},"from math import sqrt\n\nprint(math.sqrt(16))\n",[17,10244,10245,10255,10259],{"__ignoreMap":96},[100,10246,10247,10249,10251,10253],{"class":102,"line":103},[100,10248,10201],{"class":145},[100,10250,10204],{"class":106},[100,10252,9259],{"class":145},[100,10254,10209],{"class":106},[100,10256,10257],{"class":102,"line":135},[100,10258,139],{"emptyLinePlaceholder":138},[100,10260,10261,10263,10265,10267,10269,10271,10273,10275],{"class":102,"line":142},[100,10262,372],{"class":114},[100,10264,170],{"class":118},[100,10266,9271],{"class":178},[100,10268,290],{"class":118},[100,10270,9276],{"class":178},[100,10272,170],{"class":118},[100,10274,10180],{"class":122},[100,10276,182],{"class":118},[14,10278,10279,10280,10282,10283,290],{},"This fails because you did ",[22,10281,1059],{}," import the name ",[17,10284,9271],{},[14,10286,10287],{},"Another common mistake:",[91,10289,10291],{"className":93,"code":10290,"language":95,"meta":96,"style":96},"import math\n\nprint(sqrt(16))\n",[17,10292,10293,10299,10303],{"__ignoreMap":96},[100,10294,10295,10297],{"class":102,"line":103},[100,10296,9259],{"class":145},[100,10298,9262],{"class":106},[100,10300,10301],{"class":102,"line":135},[100,10302,139],{"emptyLinePlaceholder":138},[100,10304,10305,10307,10309,10311,10313,10315],{"class":102,"line":142},[100,10306,372],{"class":114},[100,10308,170],{"class":118},[100,10310,9276],{"class":178},[100,10312,170],{"class":118},[100,10314,10180],{"class":122},[100,10316,182],{"class":118},[14,10318,10319,10320,10322],{},"This fails because you imported the module, not the name ",[17,10321,9276],{}," directly.",[14,10324,10325],{},"Make sure the way you call the name matches the way you imported it.",[77,10327,10329],{"id":10328},"fix-6-check-python-version-differences","Fix 6: Check Python version differences",[14,10331,10332],{},"Sometimes the attribute exists only in a different Python version.",[14,10334,10335],{},"This can happen when:",[40,10337,10338,10341,10344],{},[43,10339,10340],{},"you copy code from an older tutorial",[43,10342,10343],{},"you follow documentation for a newer Python version",[43,10345,10346],{},"a package changed its API between versions",[14,10348,10349],{},"Check your version with:",[91,10351,10355],{"className":10352,"code":10353,"language":10354,"meta":96,"style":96},"language-bash shiki shiki-themes material-theme-lighter github-light github-dark","python --version\n","bash",[17,10356,10357],{"__ignoreMap":96},[100,10358,10359,10362],{"class":102,"line":103},[100,10360,95],{"class":10361},"sbgvK",[100,10363,10365],{"class":10364},"stzsN"," --version\n",[14,10367,10368],{},"If example code does not match your environment, compare:",[40,10370,10371,10374,10377],{},[43,10372,10373],{},"your Python version",[43,10375,10376],{},"the tutorial's Python version",[43,10378,10379],{},"the package version, if it is not part of the standard library",[77,10381,10383],{"id":10382},"debugging-steps-in-order","Debugging steps in order",[14,10385,10386],{},"When you see this error, go through these steps:",[3282,10388,10389,10396,10401,10407,10412,10418,10421,10427],{},[43,10390,10391,10392,10395],{},"Read the exact ",[22,10393,10394],{},"module name"," in the error message",[43,10397,10391,10398,10400],{},[22,10399,9341],{}," that Python could not find",[43,10402,10403,10404],{},"Run ",[17,10405,10406],{},"dir(module_name)",[43,10408,5163,10409],{},[17,10410,10411],{},"module_name",[43,10413,5163,10414,10417],{},[17,10415,10416],{},"module_name.__file__"," if available",[43,10419,10420],{},"Look for local files with the same module name",[43,10422,10423,10424,10426],{},"Confirm that your ",[17,10425,9259],{}," statement matches how you use the name",[43,10428,10429],{},"Check your Python version if needed",[14,10431,10432],{},"Here are some useful commands:",[91,10434,10436],{"className":10352,"code":10435,"language":10354,"meta":96,"style":96},"python --version\npython -c \"import math; print(dir(math))\"\npython -c \"import json; print(json)\"\npython -c \"import json; print(getattr(json, '__file__', 'built-in module'))\"\n",[17,10437,10438,10444,10458,10471],{"__ignoreMap":96},[100,10439,10440,10442],{"class":102,"line":103},[100,10441,95],{"class":10361},[100,10443,10365],{"class":10364},[100,10445,10446,10448,10451,10453,10456],{"class":102,"line":135},[100,10447,95],{"class":10361},[100,10449,10450],{"class":10364}," -c",[100,10452,1708],{"class":205},[100,10454,10455],{"class":209},"import math; print(dir(math))",[100,10457,1714],{"class":205},[100,10459,10460,10462,10464,10466,10469],{"class":102,"line":142},[100,10461,95],{"class":10361},[100,10463,10450],{"class":10364},[100,10465,1708],{"class":205},[100,10467,10468],{"class":209},"import json; print(json)",[100,10470,1714],{"class":205},[100,10472,10473,10475,10477,10479,10482],{"class":102,"line":152},[100,10474,95],{"class":10361},[100,10476,10450],{"class":10364},[100,10478,1708],{"class":205},[100,10480,10481],{"class":209},"import json; print(getattr(json, '__file__', 'built-in module'))",[100,10483,1714],{"class":205},[77,10485,7117],{"id":7116},[14,10487,10488],{},"These are the most common specific causes:",[40,10490,10491,10494,10497,10504,10507],{},[43,10492,10493],{},"Typing the wrong function or attribute name",[43,10495,10496],{},"Using the wrong module for that function",[43,10498,10499,10500,3178,10502],{},"Local file name conflicts such as ",[17,10501,9856],{},[17,10503,9846],{},[43,10505,10506],{},"Incorrect import style",[43,10508,10509],{},"Version mismatch between your Python install and example code",[77,10511,1514],{"id":1513},[675,10513,10515],{"id":10514},"why-does-this-happen-even-though-i-installed-the-module","Why does this happen even though I installed the module?",[14,10517,10518],{},"The module may be installed correctly, but:",[40,10520,10521,10524,10527],{},[43,10522,10523],{},"the attribute name may be wrong",[43,10525,10526],{},"that attribute may not exist in your version",[43,10528,10529],{},"a local file with the same name may be hiding the real module",[675,10531,10533],{"id":10532},"how-do-i-know-whether-python-imported-the-wrong-file","How do I know whether Python imported the wrong file?",[14,10535,10536,10537,290],{},"Print the module object or ",[17,10538,10539],{},"module.__file__",[14,10541,1844],{},[91,10543,10545],{"className":93,"code":10544,"language":95,"meta":96,"style":96},"import json\n\nprint(json)\nprint(getattr(json, \"__file__\", \"built-in module\"))\n",[17,10546,10547,10553,10557,10567],{"__ignoreMap":96},[100,10548,10549,10551],{"class":102,"line":103},[100,10550,9259],{"class":145},[100,10552,9984],{"class":106},[100,10554,10555],{"class":102,"line":135},[100,10556,139],{"emptyLinePlaceholder":138},[100,10558,10559,10561,10563,10565],{"class":102,"line":142},[100,10560,372],{"class":114},[100,10562,170],{"class":118},[100,10564,9997],{"class":178},[100,10566,215],{"class":118},[100,10568,10569,10571,10573,10575,10577,10579,10581,10583,10585,10587,10589,10591,10593,10595],{"class":102,"line":152},[100,10570,372],{"class":114},[100,10572,170],{"class":118},[100,10574,10101],{"class":114},[100,10576,170],{"class":118},[100,10578,9997],{"class":178},[100,10580,126],{"class":118},[100,10582,1708],{"class":205},[100,10584,10041],{"class":209},[100,10586,206],{"class":205},[100,10588,126],{"class":118},[100,10590,1708],{"class":205},[100,10592,10120],{"class":209},[100,10594,206],{"class":205},[100,10596,182],{"class":118},[14,10598,10599],{},"If the path points to your project file, your file is shadowing the real module.",[675,10601,10603],{"id":10602},"what-is-an-attribute-in-this-error","What is an attribute in this error?",[14,10605,10606],{},"An attribute is a name inside an object.",[14,10608,10609],{},"For modules, an attribute can be:",[40,10611,10612,10614,10616,10618],{},[43,10613,9353],{},[43,10615,9359],{},[43,10617,9356],{},[43,10619,9362],{},[675,10621,10623],{"id":10622},"can-dir-help-fix-this-error","Can dir() help fix this error?",[14,10625,10626,10627,10629],{},"Yes. ",[17,10628,10406],{}," shows the names available on the module.",[14,10631,10632],{},"That helps you find:",[40,10634,10635,10638,10641],{},[43,10636,10637],{},"misspellings",[43,10639,10640],{},"wrong assumptions about the module",[43,10642,10643],{},"missing attributes",[77,10645,1554],{"id":1553},[40,10647,10648,10652,10657,10662,10667,10673,10679],{},[43,10649,10650],{},[295,10651,9381],{"href":7094},[43,10653,10654],{},[295,10655,10656],{"href":9960},"ImportError: No module named X (Fix)",[43,10658,10659],{},[295,10660,10661],{"href":9964},"ImportError: cannot import name (Fix)",[43,10663,10664],{},[295,10665,10666],{"href":10136},"How import works in Python",[43,10668,10669],{},[295,10670,1572,10671,1576],{"href":9567},[17,10672,4565],{},[43,10674,10675],{},[295,10676,1572,10677,1576],{"href":9793},[17,10678,9796],{},[43,10680,10681],{},[295,10682,10683],{"href":9827},"How to import a module in Python",[14,10685,10686,10687,3266,10690,10693],{},"If you keep hitting this error, inspect the import step by step. Once you know ",[22,10688,10689],{},"what Python imported",[22,10691,10692],{},"what names that module really contains",", the fix is usually easy to find.",[1589,10695,10696],{},"html pre.shiki code .sVHd0, html code.shiki .sVHd0{--shiki-light:#39ADB5;--shiki-light-font-style:italic;--shiki-default:#D73A49;--shiki-default-font-style:inherit;--shiki-dark:#F97583;--shiki-dark-font-style:inherit}html pre.shiki code .su5hD, html code.shiki .su5hD{--shiki-light:#90A4AE;--shiki-default:#24292E;--shiki-dark:#E1E4E8}html pre.shiki code .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 pre.shiki code .srdBf, html code.shiki .srdBf{--shiki-light:#F76D47;--shiki-default:#005CC5;--shiki-dark:#79B8FF}html pre.shiki code .sutJx, html code.shiki .sutJx{--shiki-light:#90A4AE;--shiki-light-font-style:italic;--shiki-default:#6A737D;--shiki-default-font-style:inherit;--shiki-dark:#6A737D;--shiki-dark-font-style:inherit}html .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 .smGrS, html code.shiki .smGrS{--shiki-light:#39ADB5;--shiki-default:#D73A49;--shiki-dark:#F97583}html pre.shiki code .s_hVV, html code.shiki .s_hVV{--shiki-light:#90A4AE;--shiki-default:#005CC5;--shiki-dark:#79B8FF}html pre.shiki code .sbgvK, html code.shiki .sbgvK{--shiki-light:#E2931D;--shiki-default:#6F42C1;--shiki-dark:#B392F0}html pre.shiki code .stzsN, html code.shiki .stzsN{--shiki-light:#91B859;--shiki-default:#005CC5;--shiki-dark:#79B8FF}",{"title":96,"searchDepth":135,"depth":135,"links":10698},[10699,10700,10701,10702,10703,10704,10705,10708,10709,10714,10715,10716,10717,10723],{"id":79,"depth":135,"text":80},{"id":5880,"depth":135,"text":5881},{"id":5944,"depth":135,"text":5945},{"id":9507,"depth":135,"text":9508},{"id":9534,"depth":135,"text":9535},{"id":9685,"depth":135,"text":9686},{"id":9832,"depth":135,"text":9833,"children":10706},[10707],{"id":6243,"depth":142,"text":6244},{"id":9968,"depth":135,"text":9969},{"id":10140,"depth":135,"text":10141,"children":10710},[10711,10712,10713],{"id":10147,"depth":142,"text":10148},{"id":10190,"depth":142,"text":10191},{"id":10238,"depth":142,"text":10239},{"id":10328,"depth":135,"text":10329},{"id":10382,"depth":135,"text":10383},{"id":7116,"depth":135,"text":7117},{"id":1513,"depth":135,"text":1514,"children":10718},[10719,10720,10721,10722],{"id":10514,"depth":142,"text":10515},{"id":10532,"depth":142,"text":10533},{"id":10602,"depth":142,"text":10603},{"id":10622,"depth":142,"text":10623},{"id":1553,"depth":135,"text":1554},"Master attributeerror module has no attribute fix in our comprehensive Python beginner guide.",{},"\u002Ferrors\u002Fattributeerror-module-has-no-attribute-fix",{"title":9234,"description":10724},"errors\u002Fattributeerror-module-has-no-attribute-fix","sJFANZcCTHCppFcZ45n3O72NTINIKP0uVUFhcuPzVNw",{"id":10731,"title":10732,"body":10733,"description":12878,"extension":1623,"meta":12879,"navigation":138,"path":7112,"seo":12880,"stem":12881,"__hash__":12882},"content\u002Ferrors\u002Fattributeerror-nonetype-object-has-no-attribute-fix.md","AttributeError: 'NoneType' object has no attribute (Fix)",{"type":7,"value":10734,"toc":12848},[10735,10740,10745,10761,10764,10781,10783,10851,10863,10865,10868,10874,10877,10904,10906,10933,10936,10956,10961,10963,10966,10996,10999,11028,11032,11038,11122,11124,11190,11193,11220,11223,11275,11277,11286,11293,11295,11297,11301,11304,11306,11337,11343,11354,11358,11363,11365,11428,11430,11438,11440,11501,11503,11512,11516,11521,11526,11601,11607,11609,11682,11684,11725,11732,11738,11807,11811,11817,11863,11868,11880,11934,11940,11943,11969,11971,12039,12041,12064,12067,12138,12140,12148,12154,12158,12161,12165,12168,12171,12174,12177,12235,12238,12240,12353,12356,12387,12391,12394,12427,12433,12436,12460,12463,12569,12578,12582,12585,12588,12591,12617,12621,12624,12646,12649,12673,12681,12684,12756,12765,12767,12771,12778,12782,12790,12794,12804,12808,12814,12816,12845],[10,10736,5728,10738,7375],{"id":10737},"attributeerror-nonetype-object-has-no-attribute-fix",[17,10739,4970],{},[14,10741,10742,10743,290],{},"This error happens when you try to use an attribute or method on ",[17,10744,930],{},[14,10746,10747,10748,10750,10751,10753,10754,5870,10756,5870,10758,10760],{},"In Python, ",[17,10749,930],{}," means “no value”. So if a variable is ",[17,10752,930],{},", you cannot call methods like ",[17,10755,6095],{},[17,10757,5873],{},[17,10759,6110],{},", or access attributes on it.",[14,10762,10763],{},"The fix is usually simple:",[40,10765,10766,10772,10775],{},[43,10767,10768,10769,10771],{},"find where ",[17,10770,930],{}," came from",[43,10773,10774],{},"make sure you have the right object before using it",[43,10776,10777,10778,10780],{},"add a check or default value if ",[17,10779,930],{}," is possible",[77,10782,80],{"id":79},[91,10784,10786],{"className":93,"code":10785,"language":95,"meta":96,"style":96},"name = get_name()\n\nif name is not None:\n    print(name.strip())\nelse:\n    print(\"No name returned\")\n",[17,10787,10788,10798,10802,10816,10830,10836],{"__ignoreMap":96},[100,10789,10790,10792,10794,10796],{"class":102,"line":103},[100,10791,1691],{"class":106},[100,10793,111],{"class":110},[100,10795,4697],{"class":178},[100,10797,4734],{"class":118},[100,10799,10800],{"class":102,"line":135},[100,10801,139],{"emptyLinePlaceholder":138},[100,10803,10804,10806,10808,10810,10812,10814],{"class":102,"line":142},[100,10805,2736],{"class":145},[100,10807,4915],{"class":106},[100,10809,4918],{"class":110},[100,10811,4921],{"class":110},[100,10813,866],{"class":158},[100,10815,149],{"class":118},[100,10817,10818,10820,10822,10824,10826,10828],{"class":102,"line":152},[100,10819,200],{"class":114},[100,10821,170],{"class":118},[100,10823,2853],{"class":178},[100,10825,290],{"class":118},[100,10827,6235],{"class":178},[100,10829,3370],{"class":118},[100,10831,10832,10834],{"class":102,"line":164},[100,10833,4944],{"class":145},[100,10835,149],{"class":118},[100,10837,10838,10840,10842,10844,10847,10849],{"class":102,"line":185},[100,10839,200],{"class":114},[100,10841,170],{"class":118},[100,10843,206],{"class":205},[100,10845,10846],{"class":209},"No name returned",[100,10848,206],{"class":205},[100,10850,215],{"class":118},[14,10852,10853,10854,10856,10857,5870,10859,5894,10861,290],{},"This error happens when you try to use a method or attribute on ",[17,10855,930],{},". Check the variable before calling methods like ",[17,10858,6095],{},[17,10860,5873],{},[17,10862,6110],{},[77,10864,5881],{"id":5880},[14,10866,10867],{},"Python expected an object that has a method or attribute.",[14,10869,10870,10871,10873],{},"But the value was ",[17,10872,930],{}," instead.",[14,10875,10876],{},"That is why code like this fails:",[91,10878,10880],{"className":93,"code":10879,"language":95,"meta":96,"style":96},"value = None\nprint(value.strip())\n",[17,10881,10882,10890],{"__ignoreMap":96},[100,10883,10884,10886,10888],{"class":102,"line":103},[100,10885,3318],{"class":106},[100,10887,111],{"class":110},[100,10889,4904],{"class":158},[100,10891,10892,10894,10896,10898,10900,10902],{"class":102,"line":135},[100,10893,372],{"class":114},[100,10895,170],{"class":118},[100,10897,757],{"class":178},[100,10899,290],{"class":118},[100,10901,6235],{"class":178},[100,10903,3370],{"class":118},[14,10905,218],{},[91,10907,10909],{"className":93,"code":10908,"language":95,"meta":96,"style":96},"AttributeError: 'NoneType' object has no attribute 'strip'\n",[17,10910,10911],{"__ignoreMap":96},[100,10912,10913,10915,10917,10919,10921,10923,10925,10927,10929,10931],{"class":102,"line":103},[100,10914,3738],{"class":191},[100,10916,89],{"class":118},[100,10918,1274],{"class":205},[100,10920,4665],{"class":209},[100,10922,1280],{"class":205},[100,10924,3914],{"class":191},[100,10926,3917],{"class":106},[100,10928,1280],{"class":205},[100,10930,6235],{"class":209},[100,10932,3925],{"class":205},[14,10934,10935],{},"Key idea:",[40,10937,10938,10943,10950],{},[43,10939,10940,10942],{},[17,10941,930],{}," is a special value that means “nothing” or “no result”",[43,10944,10945,10947,10948],{},[17,10946,4665],{}," is the type of ",[17,10949,930],{},[43,10951,10952,10953,10955],{},"methods like ",[17,10954,6095],{}," only work on real objects such as strings",[14,10957,10958,10959,290],{},"If you are new to Python errors, see ",[295,10960,1587],{"href":1749},[77,10962,7741],{"id":7740},[14,10964,10965],{},"This error usually means one of these things happened:",[40,10967,10968,10973,10982,10987,10993],{},[43,10969,10970,10971],{},"A function did not return a value, so Python used ",[17,10972,930],{},[43,10974,10975,10976],{},"You assigned the result of an in-place method like ",[295,10977,10979],{"href":10978},"\u002Freference\u002Fpython-list-sort-method\u002F",[17,10980,10981],{},"list.sort()",[43,10983,10984,10985],{},"A dictionary lookup or function returned ",[17,10986,930],{},[43,10988,10989,10990,10992],{},"A variable was set to ",[17,10991,930],{}," earlier in the program",[43,10994,10995],{},"User input, file data, or API data was missing",[14,10997,10998],{},"Here are some common causes:",[40,11000,11001,11004,11010,11020,11023],{},[43,11002,11003],{},"Calling a method on the result of a function that returns nothing",[43,11005,11006,11007,11009],{},"Using the result of ",[17,11008,10981],{}," as if it were a new list",[43,11011,11012,11013,11019],{},"Expecting ",[295,11014,11016],{"href":11015},"\u002Freference\u002Fpython-dictionary-get-method\u002F",[17,11017,11018],{},"dict.get()"," to always return a real value",[43,11021,11022],{},"Reading missing data from a file, API, or form",[43,11024,11025,11026],{},"Overwriting a valid variable with ",[17,11027,930],{},[77,11029,11031],{"id":11030},"example-that-causes-the-error","Example that causes the error",[14,11033,11034,11035,11037],{},"A very common cause is a function that has no ",[17,11036,1053],{}," statement.",[91,11039,11041],{"className":93,"code":11040,"language":95,"meta":96,"style":96},"def get_name():\n    print(\"Getting name...\")\n\nname = get_name()\n\nprint(name)\nprint(type(name))\nprint(name.strip())\n",[17,11042,11043,11051,11066,11070,11080,11084,11094,11108],{"__ignoreMap":96},[100,11044,11045,11047,11049],{"class":102,"line":103},[100,11046,1078],{"class":1077},[100,11048,4697],{"class":1081},[100,11050,1085],{"class":118},[100,11052,11053,11055,11057,11059,11062,11064],{"class":102,"line":135},[100,11054,200],{"class":114},[100,11056,170],{"class":118},[100,11058,206],{"class":205},[100,11060,11061],{"class":209},"Getting name...",[100,11063,206],{"class":205},[100,11065,215],{"class":118},[100,11067,11068],{"class":102,"line":142},[100,11069,139],{"emptyLinePlaceholder":138},[100,11071,11072,11074,11076,11078],{"class":102,"line":152},[100,11073,1691],{"class":106},[100,11075,111],{"class":110},[100,11077,4697],{"class":178},[100,11079,4734],{"class":118},[100,11081,11082],{"class":102,"line":164},[100,11083,139],{"emptyLinePlaceholder":138},[100,11085,11086,11088,11090,11092],{"class":102,"line":185},[100,11087,372],{"class":114},[100,11089,170],{"class":118},[100,11091,2853],{"class":178},[100,11093,215],{"class":118},[100,11095,11096,11098,11100,11102,11104,11106],{"class":102,"line":197},[100,11097,372],{"class":114},[100,11099,170],{"class":118},[100,11101,1250],{"class":191},[100,11103,170],{"class":118},[100,11105,2853],{"class":178},[100,11107,182],{"class":118},[100,11109,11110,11112,11114,11116,11118,11120],{"class":102,"line":771},[100,11111,372],{"class":114},[100,11113,170],{"class":118},[100,11115,2853],{"class":178},[100,11117,290],{"class":118},[100,11119,6235],{"class":178},[100,11121,3370],{"class":118},[14,11123,218],{},[91,11125,11127],{"className":93,"code":11126,"language":95,"meta":96,"style":96},"Getting name...\nNone\n\u003Cclass 'NoneType'>\nTraceback (most recent call last):\n  ...\nAttributeError: 'NoneType' object has no attribute 'strip'\n",[17,11128,11129,11136,11140,11154,11164,11168],{"__ignoreMap":96},[100,11130,11131,11134],{"class":102,"line":103},[100,11132,11133],{"class":106},"Getting name",[100,11135,2422],{"class":527},[100,11137,11138],{"class":102,"line":135},[100,11139,924],{"class":158},[100,11141,11142,11144,11146,11148,11150,11152],{"class":102,"line":142},[100,11143,2782],{"class":110},[100,11145,3417],{"class":1077},[100,11147,1274],{"class":205},[100,11149,4665],{"class":209},[100,11151,1280],{"class":205},[100,11153,1980],{"class":110},[100,11155,11156,11158,11160,11162],{"class":102,"line":152},[100,11157,514],{"class":178},[100,11159,170],{"class":118},[100,11161,519],{"class":178},[100,11163,522],{"class":118},[100,11165,11166],{"class":102,"line":164},[100,11167,528],{"class":527},[100,11169,11170,11172,11174,11176,11178,11180,11182,11184,11186,11188],{"class":102,"line":185},[100,11171,3738],{"class":191},[100,11173,89],{"class":118},[100,11175,1274],{"class":205},[100,11177,4665],{"class":209},[100,11179,1280],{"class":205},[100,11181,3914],{"class":191},[100,11183,3917],{"class":106},[100,11185,1280],{"class":205},[100,11187,6235],{"class":209},[100,11189,3925],{"class":205},[14,11191,11192],{},"What happened:",[40,11194,11195,11200,11203,11209],{},[43,11196,11197,11199],{},[17,11198,4805],{}," ran",[43,11201,11202],{},"the function did not return anything",[43,11204,11205,11206,11208],{},"Python returned ",[17,11207,930],{}," automatically",[43,11210,11211,11214,11215,11217,11218],{},[17,11212,11213],{},"name.strip()"," failed because ",[17,11216,2853],{}," was ",[17,11219,930],{},[14,11221,11222],{},"To fix it, return a real string:",[91,11224,11226],{"className":93,"code":11225,"language":95,"meta":96,"style":96},"def get_name():\n    return \"  Alice  \"\n\nname = get_name()\nprint(name.strip())\n",[17,11227,11228,11236,11247,11251,11261],{"__ignoreMap":96},[100,11229,11230,11232,11234],{"class":102,"line":103},[100,11231,1078],{"class":1077},[100,11233,4697],{"class":1081},[100,11235,1085],{"class":118},[100,11237,11238,11240,11242,11245],{"class":102,"line":135},[100,11239,2552],{"class":145},[100,11241,1708],{"class":205},[100,11243,11244],{"class":209},"  Alice  ",[100,11246,1714],{"class":205},[100,11248,11249],{"class":102,"line":142},[100,11250,139],{"emptyLinePlaceholder":138},[100,11252,11253,11255,11257,11259],{"class":102,"line":152},[100,11254,1691],{"class":106},[100,11256,111],{"class":110},[100,11258,4697],{"class":178},[100,11260,4734],{"class":118},[100,11262,11263,11265,11267,11269,11271,11273],{"class":102,"line":164},[100,11264,372],{"class":114},[100,11266,170],{"class":118},[100,11268,2853],{"class":178},[100,11270,290],{"class":118},[100,11272,6235],{"class":178},[100,11274,3370],{"class":118},[14,11276,218],{},[91,11278,11280],{"className":93,"code":11279,"language":95,"meta":96,"style":96},"Alice\n",[17,11281,11282],{"__ignoreMap":96},[100,11283,11284],{"class":102,"line":103},[100,11285,11279],{"class":106},[14,11287,11288,11289,290],{},"If you want a deeper explanation of this behavior, see ",[295,11290,11292],{"href":11291},"\u002Flearn\u002Freturn-values-in-python-functions\u002F","return values in Python functions",[77,11294,6244],{"id":6243},[14,11296,8116],{},[675,11298,11300],{"id":11299},"_1-find-where-the-variable-gets-its-value","1. Find where the variable gets its value",[14,11302,11303],{},"Look at the line that causes the error, then trace backward.",[14,11305,580],{},[91,11307,11309],{"className":93,"code":11308,"language":95,"meta":96,"style":96},"username = get_user_name()\nprint(username.upper())\n",[17,11310,11311,11322],{"__ignoreMap":96},[100,11312,11313,11315,11317,11320],{"class":102,"line":103},[100,11314,2686],{"class":106},[100,11316,111],{"class":110},[100,11318,11319],{"class":178}," get_user_name",[100,11321,4734],{"class":118},[100,11323,11324,11326,11328,11331,11333,11335],{"class":102,"line":135},[100,11325,372],{"class":114},[100,11327,170],{"class":118},[100,11329,11330],{"class":178},"username",[100,11332,290],{"class":118},[100,11334,4540],{"class":178},[100,11336,3370],{"class":118},[14,11338,11339,11340,11342],{},"Do not focus only on ",[17,11341,8734],{},". The real question is:",[14,11344,11345],{},[22,11346,11347,11348,11350,11351,11353],{},"Why is ",[17,11349,11330],{}," equal to ",[17,11352,930],{},"?",[675,11355,11357],{"id":11356},"_2-check-whether-a-function-needs-a-return-statement","2. Check whether a function needs a return statement",[14,11359,11360,11361,290],{},"If a function should give you a value, it must use ",[17,11362,1053],{},[14,11364,9709],{},[91,11366,11368],{"className":93,"code":11367,"language":95,"meta":96,"style":96},"def make_title(name):\n    name.title()\n\ntitle = make_title(\"python\")\nprint(title)\n",[17,11369,11370,11383,11395,11399,11418],{"__ignoreMap":96},[100,11371,11372,11374,11377,11379,11381],{"class":102,"line":103},[100,11373,1078],{"class":1077},[100,11375,11376],{"class":1081}," make_title",[100,11378,170],{"class":118},[100,11380,2853],{"class":2519},[100,11382,522],{"class":118},[100,11384,11385,11388,11390,11393],{"class":102,"line":135},[100,11386,11387],{"class":106},"    name",[100,11389,290],{"class":118},[100,11391,11392],{"class":178},"title",[100,11394,4734],{"class":118},[100,11396,11397],{"class":102,"line":142},[100,11398,139],{"emptyLinePlaceholder":138},[100,11400,11401,11404,11406,11408,11410,11412,11414,11416],{"class":102,"line":152},[100,11402,11403],{"class":106},"title ",[100,11405,111],{"class":110},[100,11407,11376],{"class":178},[100,11409,170],{"class":118},[100,11411,206],{"class":205},[100,11413,95],{"class":209},[100,11415,206],{"class":205},[100,11417,215],{"class":118},[100,11419,11420,11422,11424,11426],{"class":102,"line":164},[100,11421,372],{"class":114},[100,11423,170],{"class":118},[100,11425,11392],{"class":178},[100,11427,215],{"class":118},[14,11429,218],{},[91,11431,11432],{"className":93,"code":924,"language":95,"meta":96,"style":96},[17,11433,11434],{"__ignoreMap":96},[100,11435,11436],{"class":102,"line":103},[100,11437,924],{"class":158},[14,11439,9750],{},[91,11441,11443],{"className":93,"code":11442,"language":95,"meta":96,"style":96},"def make_title(name):\n    return name.title()\n\ntitle = make_title(\"python\")\nprint(title)\n",[17,11444,11445,11457,11469,11473,11491],{"__ignoreMap":96},[100,11446,11447,11449,11451,11453,11455],{"class":102,"line":103},[100,11448,1078],{"class":1077},[100,11450,11376],{"class":1081},[100,11452,170],{"class":118},[100,11454,2853],{"class":2519},[100,11456,522],{"class":118},[100,11458,11459,11461,11463,11465,11467],{"class":102,"line":135},[100,11460,2552],{"class":145},[100,11462,1703],{"class":106},[100,11464,290],{"class":118},[100,11466,11392],{"class":178},[100,11468,4734],{"class":118},[100,11470,11471],{"class":102,"line":142},[100,11472,139],{"emptyLinePlaceholder":138},[100,11474,11475,11477,11479,11481,11483,11485,11487,11489],{"class":102,"line":152},[100,11476,11403],{"class":106},[100,11478,111],{"class":110},[100,11480,11376],{"class":178},[100,11482,170],{"class":118},[100,11484,206],{"class":205},[100,11486,95],{"class":209},[100,11488,206],{"class":205},[100,11490,215],{"class":118},[100,11492,11493,11495,11497,11499],{"class":102,"line":164},[100,11494,372],{"class":114},[100,11496,170],{"class":118},[100,11498,11392],{"class":178},[100,11500,215],{"class":118},[14,11502,218],{},[91,11504,11506],{"className":93,"code":11505,"language":95,"meta":96,"style":96},"Python\n",[17,11507,11508],{"__ignoreMap":96},[100,11509,11510],{"class":102,"line":103},[100,11511,11505],{"class":106},[675,11513,11515],{"id":11514},"_3-do-not-assign-the-result-of-methods-that-change-objects-in-place","3. Do not assign the result of methods that change objects in place",[14,11517,11518,11519,290],{},"Some methods modify an object and return ",[17,11520,930],{},[14,11522,11523,11524,89],{},"A common example is ",[17,11525,10981],{},[91,11527,11529],{"className":93,"code":11528,"language":95,"meta":96,"style":96},"numbers = [3, 1, 2]\nresult = numbers.sort()\n\nprint(result)\nprint(result.append(4))\n",[17,11530,11531,11552,11568,11572,11583],{"__ignoreMap":96},[100,11532,11533,11535,11537,11539,11542,11544,11546,11548,11550],{"class":102,"line":103},[100,11534,107],{"class":106},[100,11536,111],{"class":110},[100,11538,594],{"class":118},[100,11540,11541],{"class":122},"3",[100,11543,126],{"class":118},[100,11545,6915],{"class":122},[100,11547,126],{"class":118},[100,11549,129],{"class":122},[100,11551,603],{"class":118},[100,11553,11554,11557,11559,11561,11563,11566],{"class":102,"line":135},[100,11555,11556],{"class":106},"result ",[100,11558,111],{"class":110},[100,11560,620],{"class":106},[100,11562,290],{"class":118},[100,11564,11565],{"class":178},"sort",[100,11567,4734],{"class":118},[100,11569,11570],{"class":102,"line":142},[100,11571,139],{"emptyLinePlaceholder":138},[100,11573,11574,11576,11578,11581],{"class":102,"line":152},[100,11575,372],{"class":114},[100,11577,170],{"class":118},[100,11579,11580],{"class":178},"result",[100,11582,215],{"class":118},[100,11584,11585,11587,11589,11591,11593,11595,11597,11599],{"class":102,"line":164},[100,11586,372],{"class":114},[100,11588,170],{"class":118},[100,11590,11580],{"class":178},[100,11592,290],{"class":118},[100,11594,4254],{"class":178},[100,11596,170],{"class":118},[100,11598,8322],{"class":122},[100,11600,182],{"class":118},[14,11602,7194,11603,2025,11605,290],{},[17,11604,11580],{},[17,11606,930],{},[14,11608,6031],{},[91,11610,11612],{"className":93,"code":11611,"language":95,"meta":96,"style":96},"numbers = [3, 1, 2]\nnumbers.sort()\n\nprint(numbers)\nnumbers.append(4)\nprint(numbers)\n",[17,11613,11614,11634,11644,11648,11658,11672],{"__ignoreMap":96},[100,11615,11616,11618,11620,11622,11624,11626,11628,11630,11632],{"class":102,"line":103},[100,11617,107],{"class":106},[100,11619,111],{"class":110},[100,11621,594],{"class":118},[100,11623,11541],{"class":122},[100,11625,126],{"class":118},[100,11627,6915],{"class":122},[100,11629,126],{"class":118},[100,11631,129],{"class":122},[100,11633,603],{"class":118},[100,11635,11636,11638,11640,11642],{"class":102,"line":135},[100,11637,179],{"class":106},[100,11639,290],{"class":118},[100,11641,11565],{"class":178},[100,11643,4734],{"class":118},[100,11645,11646],{"class":102,"line":142},[100,11647,139],{"emptyLinePlaceholder":138},[100,11649,11650,11652,11654,11656],{"class":102,"line":152},[100,11651,372],{"class":114},[100,11653,170],{"class":118},[100,11655,179],{"class":178},[100,11657,215],{"class":118},[100,11659,11660,11662,11664,11666,11668,11670],{"class":102,"line":164},[100,11661,179],{"class":106},[100,11663,290],{"class":118},[100,11665,4254],{"class":178},[100,11667,170],{"class":118},[100,11669,8322],{"class":122},[100,11671,215],{"class":118},[100,11673,11674,11676,11678,11680],{"class":102,"line":185},[100,11675,372],{"class":114},[100,11677,170],{"class":118},[100,11679,179],{"class":178},[100,11681,215],{"class":118},[14,11683,218],{},[91,11685,11687],{"className":93,"code":11686,"language":95,"meta":96,"style":96},"[1, 2, 3]\n[1, 2, 3, 4]\n",[17,11688,11689,11705],{"__ignoreMap":96},[100,11690,11691,11693,11695,11697,11699,11701,11703],{"class":102,"line":103},[100,11692,3166],{"class":118},[100,11694,123],{"class":122},[100,11696,126],{"class":118},[100,11698,129],{"class":122},[100,11700,126],{"class":118},[100,11702,1405],{"class":122},[100,11704,603],{"class":118},[100,11706,11707,11709,11711,11713,11715,11717,11719,11721,11723],{"class":102,"line":135},[100,11708,3166],{"class":118},[100,11710,123],{"class":122},[100,11712,126],{"class":118},[100,11714,129],{"class":122},[100,11716,126],{"class":118},[100,11718,1405],{"class":122},[100,11720,126],{"class":118},[100,11722,2584],{"class":122},[100,11724,603],{"class":118},[675,11726,11728,11729,11731],{"id":11727},"_4-use-an-if-check-before-accessing-attributes","4. Use an ",[17,11730,2736],{}," check before accessing attributes",[14,11733,11734,11735,11737],{},"If ",[17,11736,930],{}," is a valid possible value, check first:",[91,11739,11741],{"className":93,"code":11740,"language":95,"meta":96,"style":96},"email = None\n\nif email is not None:\n    print(email.strip())\nelse:\n    print(\"No email found\")\n",[17,11742,11743,11752,11756,11771,11786,11792],{"__ignoreMap":96},[100,11744,11745,11748,11750],{"class":102,"line":103},[100,11746,11747],{"class":106},"email ",[100,11749,111],{"class":110},[100,11751,4904],{"class":158},[100,11753,11754],{"class":102,"line":135},[100,11755,139],{"emptyLinePlaceholder":138},[100,11757,11758,11760,11763,11765,11767,11769],{"class":102,"line":142},[100,11759,2736],{"class":145},[100,11761,11762],{"class":106}," email ",[100,11764,4918],{"class":110},[100,11766,4921],{"class":110},[100,11768,866],{"class":158},[100,11770,149],{"class":118},[100,11772,11773,11775,11777,11780,11782,11784],{"class":102,"line":152},[100,11774,200],{"class":114},[100,11776,170],{"class":118},[100,11778,11779],{"class":178},"email",[100,11781,290],{"class":118},[100,11783,6235],{"class":178},[100,11785,3370],{"class":118},[100,11787,11788,11790],{"class":102,"line":164},[100,11789,4944],{"class":145},[100,11791,149],{"class":118},[100,11793,11794,11796,11798,11800,11803,11805],{"class":102,"line":185},[100,11795,200],{"class":114},[100,11797,170],{"class":118},[100,11799,206],{"class":205},[100,11801,11802],{"class":209},"No email found",[100,11804,206],{"class":205},[100,11806,215],{"class":118},[675,11808,11810],{"id":11809},"_5-give-the-variable-a-default-value-if-needed","5. Give the variable a default value if needed",[14,11812,11813,11814,11816],{},"Sometimes you want to replace ",[17,11815,930],{}," with a safe default.",[91,11818,11820],{"className":93,"code":11819,"language":95,"meta":96,"style":96},"name = None\nclean_name = name or \"\"\n\nprint(clean_name.strip())\n",[17,11821,11822,11830,11844,11848],{"__ignoreMap":96},[100,11823,11824,11826,11828],{"class":102,"line":103},[100,11825,1691],{"class":106},[100,11827,111],{"class":110},[100,11829,4904],{"class":158},[100,11831,11832,11835,11837,11839,11842],{"class":102,"line":135},[100,11833,11834],{"class":106},"clean_name ",[100,11836,111],{"class":110},[100,11838,4915],{"class":106},[100,11840,11841],{"class":110},"or",[100,11843,1696],{"class":205},[100,11845,11846],{"class":102,"line":142},[100,11847,139],{"emptyLinePlaceholder":138},[100,11849,11850,11852,11854,11857,11859,11861],{"class":102,"line":152},[100,11851,372],{"class":114},[100,11853,170],{"class":118},[100,11855,11856],{"class":178},"clean_name",[100,11858,290],{"class":118},[100,11860,6235],{"class":178},[100,11862,3370],{"class":118},[14,11864,2925,11865,11867],{},[17,11866,11856],{}," becomes an empty string.",[14,11869,11870,11871,3266,11873,11876,11877,11879],{},"Be careful with this pattern. It also replaces other false-like values such as ",[17,11872,2279],{},[17,11874,11875],{},"\"\"",". If you only want to handle ",[17,11878,930],{},", write it clearly:",[91,11881,11883],{"className":93,"code":11882,"language":95,"meta":96,"style":96},"name = None\nclean_name = \"\" if name is None else name\n\nprint(clean_name.strip())\n",[17,11884,11885,11893,11916,11920],{"__ignoreMap":96},[100,11886,11887,11889,11891],{"class":102,"line":103},[100,11888,1691],{"class":106},[100,11890,111],{"class":110},[100,11892,4904],{"class":158},[100,11894,11895,11897,11899,11902,11905,11907,11909,11911,11914],{"class":102,"line":135},[100,11896,11834],{"class":106},[100,11898,111],{"class":110},[100,11900,11901],{"class":205}," \"\"",[100,11903,11904],{"class":145}," if",[100,11906,4915],{"class":106},[100,11908,4918],{"class":110},[100,11910,866],{"class":158},[100,11912,11913],{"class":145}," else",[100,11915,2898],{"class":106},[100,11917,11918],{"class":102,"line":142},[100,11919,139],{"emptyLinePlaceholder":138},[100,11921,11922,11924,11926,11928,11930,11932],{"class":102,"line":152},[100,11923,372],{"class":114},[100,11925,170],{"class":118},[100,11927,11856],{"class":178},[100,11929,290],{"class":118},[100,11931,6235],{"class":178},[100,11933,3370],{"class":118},[77,11935,11937,11938],{"id":11936},"common-patterns-that-return-none","Common patterns that return ",[17,11939,930],{},[14,11941,11942],{},"These often lead to this error:",[40,11944,11945,11950,11954,11959,11964],{},[43,11946,11947,11948],{},"functions without ",[17,11949,1053],{},[43,11951,11952],{},[17,11953,10981],{},[43,11955,11956],{},[17,11957,11958],{},"list.append()",[43,11960,11961],{},[17,11962,11963],{},"list.extend()",[43,11965,11966],{},[17,11967,11968],{},"dict.update()",[14,11970,5500],{},[91,11972,11974],{"className":93,"code":11973,"language":95,"meta":96,"style":96},"items = [1, 2]\nresult = items.append(3)\n\nprint(result)\nprint(type(result))\n",[17,11975,11976,11992,12011,12015,12025],{"__ignoreMap":96},[100,11977,11978,11980,11982,11984,11986,11988,11990],{"class":102,"line":103},[100,11979,3095],{"class":106},[100,11981,111],{"class":110},[100,11983,594],{"class":118},[100,11985,123],{"class":122},[100,11987,126],{"class":118},[100,11989,129],{"class":122},[100,11991,603],{"class":118},[100,11993,11994,11996,11998,12001,12003,12005,12007,12009],{"class":102,"line":135},[100,11995,11556],{"class":106},[100,11997,111],{"class":110},[100,11999,12000],{"class":106}," items",[100,12002,290],{"class":118},[100,12004,4254],{"class":178},[100,12006,170],{"class":118},[100,12008,11541],{"class":122},[100,12010,215],{"class":118},[100,12012,12013],{"class":102,"line":142},[100,12014,139],{"emptyLinePlaceholder":138},[100,12016,12017,12019,12021,12023],{"class":102,"line":152},[100,12018,372],{"class":114},[100,12020,170],{"class":118},[100,12022,11580],{"class":178},[100,12024,215],{"class":118},[100,12026,12027,12029,12031,12033,12035,12037],{"class":102,"line":164},[100,12028,372],{"class":114},[100,12030,170],{"class":118},[100,12032,1250],{"class":191},[100,12034,170],{"class":118},[100,12036,11580],{"class":178},[100,12038,182],{"class":118},[14,12040,218],{},[91,12042,12044],{"className":93,"code":12043,"language":95,"meta":96,"style":96},"None\n\u003Cclass 'NoneType'>\n",[17,12045,12046,12050],{"__ignoreMap":96},[100,12047,12048],{"class":102,"line":103},[100,12049,924],{"class":158},[100,12051,12052,12054,12056,12058,12060,12062],{"class":102,"line":135},[100,12053,2782],{"class":110},[100,12055,3417],{"class":1077},[100,12057,1274],{"class":205},[100,12059,4665],{"class":209},[100,12061,1280],{"class":205},[100,12063,1980],{"class":110},[14,12065,12066],{},"Another example:",[91,12068,12070],{"className":93,"code":12069,"language":95,"meta":96,"style":96},"data = {\"a\": 1}\nresult = data.update({\"b\": 2})\n\nprint(result)\n",[17,12071,12072,12094,12124,12128],{"__ignoreMap":96},[100,12073,12074,12076,12078,12081,12083,12085,12087,12089,12091],{"class":102,"line":103},[100,12075,7909],{"class":106},[100,12077,111],{"class":110},[100,12079,12080],{"class":118}," {",[100,12082,206],{"class":205},[100,12084,295],{"class":209},[100,12086,206],{"class":205},[100,12088,89],{"class":118},[100,12090,6915],{"class":122},[100,12092,12093],{"class":118},"}\n",[100,12095,12096,12098,12100,12103,12105,12108,12111,12113,12115,12117,12119,12121],{"class":102,"line":135},[100,12097,11556],{"class":106},[100,12099,111],{"class":110},[100,12101,12102],{"class":106}," data",[100,12104,290],{"class":118},[100,12106,12107],{"class":178},"update",[100,12109,12110],{"class":118},"({",[100,12112,206],{"class":205},[100,12114,4027],{"class":209},[100,12116,206],{"class":205},[100,12118,89],{"class":118},[100,12120,129],{"class":122},[100,12122,12123],{"class":118},"})\n",[100,12125,12126],{"class":102,"line":142},[100,12127,139],{"emptyLinePlaceholder":138},[100,12129,12130,12132,12134,12136],{"class":102,"line":152},[100,12131,372],{"class":114},[100,12133,170],{"class":118},[100,12135,11580],{"class":178},[100,12137,215],{"class":118},[14,12139,218],{},[91,12141,12142],{"className":93,"code":924,"language":95,"meta":96,"style":96},[17,12143,12144],{"__ignoreMap":96},[100,12145,12146],{"class":102,"line":103},[100,12147,924],{"class":158},[14,12149,12150,12151,12153],{},"These methods change the original object directly. They do ",[22,12152,1059],{}," create and return a new object.",[77,12155,12157],{"id":12156},"step-by-step-debugging","Step-by-step debugging",[14,12159,12160],{},"When you see this error, use this simple process.",[675,12162,12164],{"id":12163},"read-the-full-traceback","Read the full traceback",[14,12166,12167],{},"The traceback shows the exact line that failed.",[14,12169,12170],{},"Start there first.",[675,12172,8126],{"id":12173},"print-the-variable-before-the-failing-line",[14,12175,12176],{},"Use small checks like these:",[91,12178,12180],{"className":93,"code":12179,"language":95,"meta":96,"style":96},"print(value)\nprint(type(value))\nprint(value is None)\nprint(repr(value))\n",[17,12181,12182,12192,12206,12220],{"__ignoreMap":96},[100,12183,12184,12186,12188,12190],{"class":102,"line":103},[100,12185,372],{"class":114},[100,12187,170],{"class":118},[100,12189,757],{"class":178},[100,12191,215],{"class":118},[100,12193,12194,12196,12198,12200,12202,12204],{"class":102,"line":135},[100,12195,372],{"class":114},[100,12197,170],{"class":118},[100,12199,1250],{"class":191},[100,12201,170],{"class":118},[100,12203,757],{"class":178},[100,12205,182],{"class":118},[100,12207,12208,12210,12212,12214,12216,12218],{"class":102,"line":142},[100,12209,372],{"class":114},[100,12211,170],{"class":118},[100,12213,3318],{"class":178},[100,12215,4918],{"class":145},[100,12217,866],{"class":158},[100,12219,215],{"class":118},[100,12221,12222,12224,12226,12229,12231,12233],{"class":102,"line":152},[100,12223,372],{"class":114},[100,12225,170],{"class":118},[100,12227,12228],{"class":114},"repr",[100,12230,170],{"class":118},[100,12232,757],{"class":178},[100,12234,182],{"class":118},[14,12236,12237],{},"These are especially useful when you are not sure what the variable contains.",[14,12239,1844],{},[91,12241,12243],{"className":93,"code":12242,"language":95,"meta":96,"style":96},"def get_message():\n    # imagine this came from another part of the program\n    return None\n\nmessage = get_message()\n\nprint(message)\nprint(type(message))\nprint(message is None)\nprint(repr(message))\n\nprint(message.strip())\n",[17,12244,12245,12254,12259,12265,12269,12279,12283,12293,12307,12321,12335,12339],{"__ignoreMap":96},[100,12246,12247,12249,12252],{"class":102,"line":103},[100,12248,1078],{"class":1077},[100,12250,12251],{"class":1081}," get_message",[100,12253,1085],{"class":118},[100,12255,12256],{"class":102,"line":135},[100,12257,12258],{"class":414},"    # imagine this came from another part of the program\n",[100,12260,12261,12263],{"class":102,"line":142},[100,12262,2552],{"class":145},[100,12264,4904],{"class":158},[100,12266,12267],{"class":102,"line":152},[100,12268,139],{"emptyLinePlaceholder":138},[100,12270,12271,12273,12275,12277],{"class":102,"line":164},[100,12272,6868],{"class":106},[100,12274,111],{"class":110},[100,12276,12251],{"class":178},[100,12278,4734],{"class":118},[100,12280,12281],{"class":102,"line":185},[100,12282,139],{"emptyLinePlaceholder":138},[100,12284,12285,12287,12289,12291],{"class":102,"line":197},[100,12286,372],{"class":114},[100,12288,170],{"class":118},[100,12290,1836],{"class":178},[100,12292,215],{"class":118},[100,12294,12295,12297,12299,12301,12303,12305],{"class":102,"line":771},[100,12296,372],{"class":114},[100,12298,170],{"class":118},[100,12300,1250],{"class":191},[100,12302,170],{"class":118},[100,12304,1836],{"class":178},[100,12306,182],{"class":118},[100,12308,12309,12311,12313,12315,12317,12319],{"class":102,"line":787},[100,12310,372],{"class":114},[100,12312,170],{"class":118},[100,12314,6868],{"class":178},[100,12316,4918],{"class":145},[100,12318,866],{"class":158},[100,12320,215],{"class":118},[100,12322,12323,12325,12327,12329,12331,12333],{"class":102,"line":5816},[100,12324,372],{"class":114},[100,12326,170],{"class":118},[100,12328,12228],{"class":114},[100,12330,170],{"class":118},[100,12332,1836],{"class":178},[100,12334,182],{"class":118},[100,12336,12337],{"class":102,"line":5833},[100,12338,139],{"emptyLinePlaceholder":138},[100,12340,12341,12343,12345,12347,12349,12351],{"class":102,"line":5848},[100,12342,372],{"class":114},[100,12344,170],{"class":118},[100,12346,1836],{"class":178},[100,12348,290],{"class":118},[100,12350,6235],{"class":178},[100,12352,3370],{"class":118},[14,12354,12355],{},"Output before the crash:",[91,12357,12359],{"className":93,"code":12358,"language":95,"meta":96,"style":96},"None\n\u003Cclass 'NoneType'>\nTrue\nNone\n",[17,12360,12361,12365,12379,12383],{"__ignoreMap":96},[100,12362,12363],{"class":102,"line":103},[100,12364,924],{"class":158},[100,12366,12367,12369,12371,12373,12375,12377],{"class":102,"line":135},[100,12368,2782],{"class":110},[100,12370,3417],{"class":1077},[100,12372,1274],{"class":205},[100,12374,4665],{"class":209},[100,12376,1280],{"class":205},[100,12378,1980],{"class":110},[100,12380,12381],{"class":102,"line":142},[100,12382,3431],{"class":158},[100,12384,12385],{"class":102,"line":152},[100,12386,924],{"class":158},[675,12388,12390],{"id":12389},"trace-backward-to-where-the-value-was-assigned","Trace backward to where the value was assigned",[14,12392,12393],{},"If this line fails:",[91,12395,12397],{"className":93,"code":12396,"language":95,"meta":96,"style":96},"print(profile.get(\"name\").strip())\n",[17,12398,12399],{"__ignoreMap":96},[100,12400,12401,12403,12405,12408,12410,12413,12415,12417,12419,12421,12423,12425],{"class":102,"line":103},[100,12402,372],{"class":114},[100,12404,170],{"class":118},[100,12406,12407],{"class":178},"profile",[100,12409,290],{"class":118},[100,12411,12412],{"class":178},"get",[100,12414,170],{"class":118},[100,12416,206],{"class":205},[100,12418,2853],{"class":209},[100,12420,206],{"class":205},[100,12422,5799],{"class":118},[100,12424,6235],{"class":178},[100,12426,3370],{"class":118},[14,12428,12429,12430,12432],{},"the problem may not be ",[17,12431,6095],{}," itself.",[14,12434,12435],{},"It may be:",[40,12437,12438,12444,12452],{},[43,12439,12440,2025,12442],{},[17,12441,12407],{},[17,12443,930],{},[43,12445,12446,12449,12450],{},[17,12447,12448],{},"profile.get(\"name\")"," returned ",[17,12451,930],{},[43,12453,9421,12454,12457,12458],{},[17,12455,12456],{},"\"name\""," key exists but its value is ",[17,12459,930],{},[14,12461,12462],{},"A safer version is:",[91,12464,12466],{"className":93,"code":12465,"language":95,"meta":96,"style":96},"profile = {\"name\": None}\n\nname = profile.get(\"name\")\n\nif name is not None:\n    print(name.strip())\nelse:\n    print(\"Name is missing\")\n",[17,12467,12468,12489,12493,12516,12520,12534,12548,12554],{"__ignoreMap":96},[100,12469,12470,12473,12475,12477,12479,12481,12483,12485,12487],{"class":102,"line":103},[100,12471,12472],{"class":106},"profile ",[100,12474,111],{"class":110},[100,12476,12080],{"class":118},[100,12478,206],{"class":205},[100,12480,2853],{"class":209},[100,12482,206],{"class":205},[100,12484,89],{"class":118},[100,12486,866],{"class":158},[100,12488,12093],{"class":118},[100,12490,12491],{"class":102,"line":135},[100,12492,139],{"emptyLinePlaceholder":138},[100,12494,12495,12497,12499,12502,12504,12506,12508,12510,12512,12514],{"class":102,"line":142},[100,12496,1691],{"class":106},[100,12498,111],{"class":110},[100,12500,12501],{"class":106}," profile",[100,12503,290],{"class":118},[100,12505,12412],{"class":178},[100,12507,170],{"class":118},[100,12509,206],{"class":205},[100,12511,2853],{"class":209},[100,12513,206],{"class":205},[100,12515,215],{"class":118},[100,12517,12518],{"class":102,"line":152},[100,12519,139],{"emptyLinePlaceholder":138},[100,12521,12522,12524,12526,12528,12530,12532],{"class":102,"line":164},[100,12523,2736],{"class":145},[100,12525,4915],{"class":106},[100,12527,4918],{"class":110},[100,12529,4921],{"class":110},[100,12531,866],{"class":158},[100,12533,149],{"class":118},[100,12535,12536,12538,12540,12542,12544,12546],{"class":102,"line":185},[100,12537,200],{"class":114},[100,12539,170],{"class":118},[100,12541,2853],{"class":178},[100,12543,290],{"class":118},[100,12545,6235],{"class":178},[100,12547,3370],{"class":118},[100,12549,12550,12552],{"class":102,"line":197},[100,12551,4944],{"class":145},[100,12553,149],{"class":118},[100,12555,12556,12558,12560,12562,12565,12567],{"class":102,"line":771},[100,12557,200],{"class":114},[100,12559,170],{"class":118},[100,12561,206],{"class":205},[100,12563,12564],{"class":209},"Name is missing",[100,12566,206],{"class":205},[100,12568,215],{"class":118},[14,12570,12571,12572,12574,12575,290],{},"If you need help understanding ",[17,12573,6110],{},", see ",[295,12576,12577],{"href":11015},"Python dictionary get() method",[675,12579,12581],{"id":12580},"check-return-values-from-functions-and-methods","Check return values from functions and methods",[14,12583,12584],{},"A lot of beginners assume every function returns something useful.",[14,12586,12587],{},"That is not always true.",[14,12589,12590],{},"If needed, print the result right away:",[91,12592,12594],{"className":93,"code":12593,"language":95,"meta":96,"style":96},"result = some_function()\nprint(result)\n",[17,12595,12596,12607],{"__ignoreMap":96},[100,12597,12598,12600,12602,12605],{"class":102,"line":103},[100,12599,11556],{"class":106},[100,12601,111],{"class":110},[100,12603,12604],{"class":178}," some_function",[100,12606,4734],{"class":118},[100,12608,12609,12611,12613,12615],{"class":102,"line":135},[100,12610,372],{"class":114},[100,12612,170],{"class":118},[100,12614,11580],{"class":178},[100,12616,215],{"class":118},[77,12618,12620],{"id":12619},"ways-to-prevent-the-error","Ways to prevent the error",[14,12622,12623],{},"You can avoid this error more often by using a few good habits:",[40,12625,12626,12629,12632,12637,12640],{},[43,12627,12628],{},"Return values clearly from functions",[43,12630,12631],{},"Use descriptive variable names",[43,12633,12634,12635],{},"Avoid chaining methods when a value may be ",[17,12636,930],{},[43,12638,12639],{},"Handle missing data early",[43,12641,12642,12643],{},"Use guard checks like ",[17,12644,12645],{},"if value is not None",[14,12647,12648],{},"For example, this is risky:",[91,12650,12652],{"className":93,"code":12651,"language":95,"meta":96,"style":96},"print(get_user_name().strip().title())\n",[17,12653,12654],{"__ignoreMap":96},[100,12655,12656,12658,12660,12663,12665,12667,12669,12671],{"class":102,"line":103},[100,12657,372],{"class":114},[100,12659,170],{"class":118},[100,12661,12662],{"class":178},"get_user_name",[100,12664,8886],{"class":118},[100,12666,6235],{"class":178},[100,12668,8886],{"class":118},[100,12670,11392],{"class":178},[100,12672,3370],{"class":118},[14,12674,11734,12675,551,12678,12680],{},[17,12676,12677],{},"get_user_name()",[17,12679,930],{},", the whole chain fails.",[14,12682,12683],{},"This is safer:",[91,12685,12687],{"className":93,"code":12686,"language":95,"meta":96,"style":96},"name = get_user_name()\n\nif name is not None:\n    print(name.strip().title())\nelse:\n    print(\"No user name available\")\n",[17,12688,12689,12699,12703,12717,12735,12741],{"__ignoreMap":96},[100,12690,12691,12693,12695,12697],{"class":102,"line":103},[100,12692,1691],{"class":106},[100,12694,111],{"class":110},[100,12696,11319],{"class":178},[100,12698,4734],{"class":118},[100,12700,12701],{"class":102,"line":135},[100,12702,139],{"emptyLinePlaceholder":138},[100,12704,12705,12707,12709,12711,12713,12715],{"class":102,"line":142},[100,12706,2736],{"class":145},[100,12708,4915],{"class":106},[100,12710,4918],{"class":110},[100,12712,4921],{"class":110},[100,12714,866],{"class":158},[100,12716,149],{"class":118},[100,12718,12719,12721,12723,12725,12727,12729,12731,12733],{"class":102,"line":152},[100,12720,200],{"class":114},[100,12722,170],{"class":118},[100,12724,2853],{"class":178},[100,12726,290],{"class":118},[100,12728,6235],{"class":178},[100,12730,8886],{"class":118},[100,12732,11392],{"class":178},[100,12734,3370],{"class":118},[100,12736,12737,12739],{"class":102,"line":164},[100,12738,4944],{"class":145},[100,12740,149],{"class":118},[100,12742,12743,12745,12747,12749,12752,12754],{"class":102,"line":185},[100,12744,200],{"class":114},[100,12746,170],{"class":118},[100,12748,206],{"class":205},[100,12750,12751],{"class":209},"No user name available",[100,12753,206],{"class":205},[100,12755,215],{"class":118},[14,12757,12758,12759,3266,12763,290],{},"You can also use exception handling, but in this case it is usually better to prevent the problem before it happens. If you want to learn more, see ",[295,12760,12762],{"href":12761},"\u002Flearn\u002Fusing-try-except-else-and-finally-in-python\u002F","using try, except, else, and finally in Python",[295,12764,1029],{"href":2824},[77,12766,1514],{"id":1513},[675,12768,12770],{"id":12769},"what-is-nonetype-in-python","What is NoneType in Python?",[14,12772,12773,10947,12775,12777],{},[17,12774,4665],{},[17,12776,930],{},". It represents the absence of a value.",[675,12779,12781],{"id":12780},"why-did-my-function-return-none","Why did my function return None?",[14,12783,12784,12785,12787,12788,254],{},"If a function has no ",[17,12786,1053],{}," statement, Python returns ",[17,12789,930],{},[675,12791,12793],{"id":12792},"why-does-listsort-lead-to-this-error-sometimes","Why does list.sort() lead to this error sometimes?",[14,12795,12796,12800,12801,12803],{},[295,12797,12798],{"href":10978},[17,12799,10981],{}," changes the list in place and returns ",[17,12802,930],{},". You should not assign its result to a new variable.",[675,12805,12807],{"id":12806},"should-i-use-try-except-to-fix-this","Should I use try-except to fix this?",[14,12809,12810,12811,12813],{},"Usually no. It is better to find why the value is ",[17,12812,930],{}," and handle it before accessing attributes.",[77,12815,1554],{"id":1553},[40,12817,12818,12822,12827,12831,12836,12841],{},[43,12819,12820],{},[295,12821,1587],{"href":1749},[43,12823,12824],{},[295,12825,7095],{"href":12826},"\u002Ferrors\u002Fattributeerror-object-has-no-attribute-fix\u002F",[43,12828,12829],{},[295,12830,8973],{"href":8972},[43,12832,12833],{},[295,12834,12835],{"href":11291},"Return values in Python functions",[43,12837,12838],{},[295,12839,12840],{"href":10978},"Python list sort() method",[43,12842,12843],{},[295,12844,12577],{"href":11015},[1589,12846,12847],{},"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 .slqww, html code.shiki .slqww{--shiki-light:#6182B8;--shiki-default:#24292E;--shiki-dark:#E1E4E8}html pre.shiki code .sP7_E, html code.shiki .sP7_E{--shiki-light:#39ADB5;--shiki-default:#24292E;--shiki-dark:#E1E4E8}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 .s39Yj, html code.shiki .s39Yj{--shiki-light:#39ADB5;--shiki-default:#005CC5;--shiki-dark:#79B8FF}html pre.shiki code .sptTA, html code.shiki .sptTA{--shiki-light:#6182B8;--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 .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 .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 .s_hVV, html code.shiki .s_hVV{--shiki-light:#90A4AE;--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 .srdBf, html code.shiki .srdBf{--shiki-light:#F76D47;--shiki-default:#005CC5;--shiki-dark:#79B8FF}html pre.shiki code .sutJx, html code.shiki .sutJx{--shiki-light:#90A4AE;--shiki-light-font-style:italic;--shiki-default:#6A737D;--shiki-default-font-style:inherit;--shiki-dark:#6A737D;--shiki-dark-font-style:inherit}",{"title":96,"searchDepth":135,"depth":135,"links":12849},[12850,12851,12852,12853,12854,12862,12864,12870,12871,12877],{"id":79,"depth":135,"text":80},{"id":5880,"depth":135,"text":5881},{"id":7740,"depth":135,"text":7741},{"id":11030,"depth":135,"text":11031},{"id":6243,"depth":135,"text":6244,"children":12855},[12856,12857,12858,12859,12861],{"id":11299,"depth":142,"text":11300},{"id":11356,"depth":142,"text":11357},{"id":11514,"depth":142,"text":11515},{"id":11727,"depth":142,"text":12860},"4. Use an if check before accessing attributes",{"id":11809,"depth":142,"text":11810},{"id":11936,"depth":135,"text":12863},"Common patterns that return None",{"id":12156,"depth":135,"text":12157,"children":12865},[12866,12867,12868,12869],{"id":12163,"depth":142,"text":12164},{"id":12173,"depth":142,"text":8126},{"id":12389,"depth":142,"text":12390},{"id":12580,"depth":142,"text":12581},{"id":12619,"depth":135,"text":12620},{"id":1513,"depth":135,"text":1514,"children":12872},[12873,12874,12875,12876],{"id":12769,"depth":142,"text":12770},{"id":12780,"depth":142,"text":12781},{"id":12792,"depth":142,"text":12793},{"id":12806,"depth":142,"text":12807},{"id":1553,"depth":135,"text":1554},"Master attributeerror nonetype object has no attribute fix in our comprehensive Python beginner guide.",{},{"title":10732,"description":12878},"errors\u002Fattributeerror-nonetype-object-has-no-attribute-fix","GXsU0J7twFxgVJKQnKbPzJz5dErRlHtM0SO3-yiWRQ8",{"id":12884,"title":9381,"body":12885,"description":14586,"extension":1623,"meta":14587,"navigation":138,"path":7094,"seo":14588,"stem":14589,"__hash__":14590},"content\u002Ferrors\u002Fattributeerror-object-has-no-attribute-fix.md",{"type":7,"value":12886,"toc":14552},[12887,12890,12893,12896,12904,12913,12920,12922,13019,13025,13027,13032,13035,13046,13048,13069,13072,13102,13104,13112,13117,13168,13170,13197,13199,13202,13221,13224,13252,13257,13259,13263,13315,13320,13328,13355,13362,13368,13438,13440,13471,13477,13481,13513,13521,13524,13526,13529,13533,13540,13591,13593,13611,13614,13618,13621,13658,13661,13667,13675,13706,13709,13713,13718,13721,13778,13780,13839,13841,13865,13869,13871,13918,13920,13995,13997,14030,14034,14036,14068,14070,14100,14102,14110,14114,14120,14125,14128,14148,14153,14159,14162,14164,14215,14219,14224,14230,14233,14235,14271,14276,14280,14284,14287,14289,14320,14326,14328,14358,14360,14369,14371,14374,14395,14397,14454,14459,14461,14463,14472,14476,14490,14494,14500,14504,14513,14515,14549],[10,12888,9381],{"id":12889},"attributeerror-object-has-no-attribute-fix",[14,12891,12892],{},"This error happens when Python finds the object before the dot, but cannot find the attribute or method after the dot.",[14,12894,12895],{},"In simple terms:",[40,12897,12898,12901],{},[43,12899,12900],{},"The variable exists",[43,12902,12903],{},"But that kind of object does not have the thing you tried to use",[14,12905,12906,12907,12909,12910,12912],{},"For example, a list has ",[17,12908,7659],{},", but an integer does not. A string has ",[17,12911,8734],{},", but a list does not.",[14,12914,12915,12916,290],{},"If you are not sure what value you actually have, start by checking its type with ",[295,12917,12918],{"href":4427},[17,12919,4430],{},[77,12921,80],{"id":79},[91,12923,12925],{"className":93,"code":12924,"language":95,"meta":96,"style":96},"name = \"Python\"\nprint(name.upper())   # correct: strings have upper()\n\nnumbers = [1, 2, 3]\nprint(numbers.append(4))  # correct: lists have append()\n\nvalue = None\n# print(value.append(1))  # wrong: None has no append()\n",[17,12926,12927,12940,12957,12961,12981,13002,13006,13014],{"__ignoreMap":96},[100,12928,12929,12931,12933,12935,12938],{"class":102,"line":103},[100,12930,1691],{"class":106},[100,12932,111],{"class":110},[100,12934,1708],{"class":205},[100,12936,12937],{"class":209},"Python",[100,12939,1714],{"class":205},[100,12941,12942,12944,12946,12948,12950,12952,12954],{"class":102,"line":135},[100,12943,372],{"class":114},[100,12945,170],{"class":118},[100,12947,2853],{"class":178},[100,12949,290],{"class":118},[100,12951,4540],{"class":178},[100,12953,6188],{"class":118},[100,12955,12956],{"class":414},"   # correct: strings have upper()\n",[100,12958,12959],{"class":102,"line":142},[100,12960,139],{"emptyLinePlaceholder":138},[100,12962,12963,12965,12967,12969,12971,12973,12975,12977,12979],{"class":102,"line":152},[100,12964,107],{"class":106},[100,12966,111],{"class":110},[100,12968,594],{"class":118},[100,12970,123],{"class":122},[100,12972,126],{"class":118},[100,12974,129],{"class":122},[100,12976,126],{"class":118},[100,12978,1405],{"class":122},[100,12980,603],{"class":118},[100,12982,12983,12985,12987,12989,12991,12993,12995,12997,12999],{"class":102,"line":164},[100,12984,372],{"class":114},[100,12986,170],{"class":118},[100,12988,179],{"class":178},[100,12990,290],{"class":118},[100,12992,4254],{"class":178},[100,12994,170],{"class":118},[100,12996,8322],{"class":122},[100,12998,411],{"class":118},[100,13000,13001],{"class":414},"  # correct: lists have append()\n",[100,13003,13004],{"class":102,"line":185},[100,13005,139],{"emptyLinePlaceholder":138},[100,13007,13008,13010,13012],{"class":102,"line":197},[100,13009,3318],{"class":106},[100,13011,111],{"class":110},[100,13013,4904],{"class":158},[100,13015,13016],{"class":102,"line":771},[100,13017,13018],{"class":414},"# print(value.append(1))  # wrong: None has no append()\n",[14,13020,13021,13022,13024],{},"Check the real type of the value with ",[17,13023,7052],{}," and confirm that the attribute or method exists for that type.",[77,13026,5881],{"id":5880},[14,13028,13029,13031],{},[17,13030,3738],{}," means Python could not find the attribute or method after the dot.",[14,13033,13034],{},"An attribute is something attached to an object, such as:",[40,13036,13037,13040,13043],{},[43,13038,13039],{},"A value",[43,13041,13042],{},"A method",[43,13044,13045],{},"A property",[14,13047,5500],{},[40,13049,13050,13056,13062],{},[43,13051,8085,13052,3266,13054],{},[17,13053,8734],{},[17,13055,8090],{},[43,13057,7656,13058,3266,13060],{},[17,13059,7659],{},[17,13061,8139],{},[43,13063,8093,13064,3266,13066],{},[17,13065,7891],{},[17,13067,13068],{},"get()",[14,13070,13071],{},"This works:",[91,13073,13074],{"className":93,"code":4513,"language":95,"meta":96,"style":96},[17,13075,13076,13088],{"__ignoreMap":96},[100,13077,13078,13080,13082,13084,13086],{"class":102,"line":103},[100,13079,4331],{"class":106},[100,13081,111],{"class":110},[100,13083,1708],{"class":205},[100,13085,3506],{"class":209},[100,13087,1714],{"class":205},[100,13089,13090,13092,13094,13096,13098,13100],{"class":102,"line":135},[100,13091,372],{"class":114},[100,13093,170],{"class":118},[100,13095,4468],{"class":178},[100,13097,290],{"class":118},[100,13099,4540],{"class":178},[100,13101,3370],{"class":118},[14,13103,218],{},[91,13105,13106],{"className":93,"code":4547,"language":95,"meta":96,"style":96},[17,13107,13108],{"__ignoreMap":96},[100,13109,13110],{"class":102,"line":103},[100,13111,4547],{"class":527},[14,13113,13114,13115,89],{},"But this fails because a list does not have ",[17,13116,8734],{},[91,13118,13120],{"className":93,"code":13119,"language":95,"meta":96,"style":96},"items = [\"a\", \"b\", \"c\"]\nprint(items.upper())\n",[17,13121,13122,13154],{"__ignoreMap":96},[100,13123,13124,13126,13128,13130,13132,13134,13136,13138,13140,13142,13144,13146,13148,13150,13152],{"class":102,"line":103},[100,13125,3095],{"class":106},[100,13127,111],{"class":110},[100,13129,594],{"class":118},[100,13131,206],{"class":205},[100,13133,295],{"class":209},[100,13135,206],{"class":205},[100,13137,126],{"class":118},[100,13139,1708],{"class":205},[100,13141,4027],{"class":209},[100,13143,206],{"class":205},[100,13145,126],{"class":118},[100,13147,1708],{"class":205},[100,13149,4036],{"class":209},[100,13151,206],{"class":205},[100,13153,603],{"class":118},[100,13155,13156,13158,13160,13162,13164,13166],{"class":102,"line":135},[100,13157,372],{"class":114},[100,13159,170],{"class":118},[100,13161,3146],{"class":178},[100,13163,290],{"class":118},[100,13165,4540],{"class":178},[100,13167,3370],{"class":118},[14,13169,218],{},[91,13171,13173],{"className":93,"code":13172,"language":95,"meta":96,"style":96},"AttributeError: 'list' object has no attribute 'upper'\n",[17,13174,13175],{"__ignoreMap":96},[100,13176,13177,13179,13181,13183,13185,13187,13189,13191,13193,13195],{"class":102,"line":103},[100,13178,3738],{"class":191},[100,13180,89],{"class":118},[100,13182,1274],{"class":205},[100,13184,1235],{"class":209},[100,13186,1280],{"class":205},[100,13188,3914],{"class":191},[100,13190,3917],{"class":106},[100,13192,1280],{"class":205},[100,13194,4540],{"class":209},[100,13196,3925],{"class":205},[77,13198,7741],{"id":7740},[14,13200,13201],{},"This error usually appears for one of these reasons:",[40,13203,13204,13206,13209,13215,13218],{},[43,13205,3953],{},[43,13207,13208],{},"A variable contains a different value than you expected",[43,13210,13211,13212,13214],{},"A function returned ",[17,13213,930],{},", but you treated it like a list, string, or dictionary",[43,13216,13217],{},"There is a spelling mistake in the attribute name",[43,13219,13220],{},"You expected a module, class, or object to provide something that it does not",[14,13222,13223],{},"Common examples:",[40,13225,13226,13229,13231,13241,13246,13249],{},[43,13227,13228],{},"Calling a list method on a string or tuple",[43,13230,3976],{},[43,13232,11006,13233,5870,13236,5894,13238,13240],{},[17,13234,13235],{},"print()",[17,13237,8139],{},[17,13239,7659],{}," as if it were a new object",[43,13242,13243,13244],{},"Trying to use methods on ",[17,13245,930],{},[43,13247,13248],{},"Misspelling a method name",[43,13250,13251],{},"Confusing module names, class names, and instance methods",[14,13253,13254,13255,290],{},"If you want a broader introduction to Python exceptions, see ",[295,13256,1587],{"href":1749},[77,13258,11031],{"id":11030},[675,13260,13262],{"id":13261},"using-a-string-method-on-a-list","Using a string method on a list",[91,13264,13266],{"className":93,"code":13265,"language":95,"meta":96,"style":96},"words = [\"python\", \"is\", \"fun\"]\nprint(words.upper())\n",[17,13267,13268,13301],{"__ignoreMap":96},[100,13269,13270,13272,13274,13276,13278,13280,13282,13284,13286,13288,13290,13292,13294,13297,13299],{"class":102,"line":103},[100,13271,7674],{"class":106},[100,13273,111],{"class":110},[100,13275,594],{"class":118},[100,13277,206],{"class":205},[100,13279,95],{"class":209},[100,13281,206],{"class":205},[100,13283,126],{"class":118},[100,13285,1708],{"class":205},[100,13287,4918],{"class":209},[100,13289,206],{"class":205},[100,13291,126],{"class":118},[100,13293,1708],{"class":205},[100,13295,13296],{"class":209},"fun",[100,13298,206],{"class":205},[100,13300,603],{"class":118},[100,13302,13303,13305,13307,13309,13311,13313],{"class":102,"line":135},[100,13304,372],{"class":114},[100,13306,170],{"class":118},[100,13308,7701],{"class":178},[100,13310,290],{"class":118},[100,13312,4540],{"class":178},[100,13314,3370],{"class":118},[14,13316,7194,13317,13319],{},[17,13318,8734],{}," is a string method, not a list method.",[675,13321,315,13323,13325,13326],{"id":13322},"calling-append-on-none",[17,13324,7659],{}," on ",[17,13327,930],{},[91,13329,13331],{"className":93,"code":13330,"language":95,"meta":96,"style":96},"value = None\nvalue.append(1)\n",[17,13332,13333,13341],{"__ignoreMap":96},[100,13334,13335,13337,13339],{"class":102,"line":103},[100,13336,3318],{"class":106},[100,13338,111],{"class":110},[100,13340,4904],{"class":158},[100,13342,13343,13345,13347,13349,13351,13353],{"class":102,"line":135},[100,13344,757],{"class":106},[100,13346,290],{"class":118},[100,13348,4254],{"class":178},[100,13350,170],{"class":118},[100,13352,123],{"class":122},[100,13354,215],{"class":118},[14,13356,7194,13357,4819,13359,13361],{},[17,13358,930],{},[17,13360,7659],{}," method.",[14,13363,13364,13365,13367],{},"A common reason is that a function returned ",[17,13366,930],{}," and you used it as if it were a list:",[91,13369,13370],{"className":93,"code":11528,"language":95,"meta":96,"style":96},[17,13371,13372,13392,13406,13410,13420],{"__ignoreMap":96},[100,13373,13374,13376,13378,13380,13382,13384,13386,13388,13390],{"class":102,"line":103},[100,13375,107],{"class":106},[100,13377,111],{"class":110},[100,13379,594],{"class":118},[100,13381,11541],{"class":122},[100,13383,126],{"class":118},[100,13385,6915],{"class":122},[100,13387,126],{"class":118},[100,13389,129],{"class":122},[100,13391,603],{"class":118},[100,13393,13394,13396,13398,13400,13402,13404],{"class":102,"line":135},[100,13395,11556],{"class":106},[100,13397,111],{"class":110},[100,13399,620],{"class":106},[100,13401,290],{"class":118},[100,13403,11565],{"class":178},[100,13405,4734],{"class":118},[100,13407,13408],{"class":102,"line":142},[100,13409,139],{"emptyLinePlaceholder":138},[100,13411,13412,13414,13416,13418],{"class":102,"line":152},[100,13413,372],{"class":114},[100,13415,170],{"class":118},[100,13417,11580],{"class":178},[100,13419,215],{"class":118},[100,13421,13422,13424,13426,13428,13430,13432,13434,13436],{"class":102,"line":164},[100,13423,372],{"class":114},[100,13425,170],{"class":118},[100,13427,11580],{"class":178},[100,13429,290],{"class":118},[100,13431,4254],{"class":178},[100,13433,170],{"class":118},[100,13435,8322],{"class":122},[100,13437,182],{"class":118},[14,13439,218],{},[91,13441,13443],{"className":93,"code":13442,"language":95,"meta":96,"style":96},"None\nAttributeError: 'NoneType' object has no attribute 'append'\n",[17,13444,13445,13449],{"__ignoreMap":96},[100,13446,13447],{"class":102,"line":103},[100,13448,924],{"class":158},[100,13450,13451,13453,13455,13457,13459,13461,13463,13465,13467,13469],{"class":102,"line":135},[100,13452,3738],{"class":191},[100,13454,89],{"class":118},[100,13456,1274],{"class":205},[100,13458,4665],{"class":209},[100,13460,1280],{"class":205},[100,13462,3914],{"class":191},[100,13464,3917],{"class":106},[100,13466,1280],{"class":205},[100,13468,4254],{"class":209},[100,13470,3925],{"class":205},[14,13472,13473,12800,13475,290],{},[17,13474,10981],{},[17,13476,930],{},[675,13478,13480],{"id":13479},"misspelling-an-attribute-name","Misspelling an attribute name",[91,13482,13484],{"className":93,"code":13483,"language":95,"meta":96,"style":96},"name = \"Python\"\nprint(name.uper())\n",[17,13485,13486,13498],{"__ignoreMap":96},[100,13487,13488,13490,13492,13494,13496],{"class":102,"line":103},[100,13489,1691],{"class":106},[100,13491,111],{"class":110},[100,13493,1708],{"class":205},[100,13495,12937],{"class":209},[100,13497,1714],{"class":205},[100,13499,13500,13502,13504,13506,13508,13511],{"class":102,"line":135},[100,13501,372],{"class":114},[100,13503,170],{"class":118},[100,13505,2853],{"class":178},[100,13507,290],{"class":118},[100,13509,13510],{"class":178},"uper",[100,13512,3370],{"class":118},[14,13514,13515,13516,9472,13518,290],{},"This fails because the method name is ",[17,13517,8734],{},[17,13519,13520],{},"uper()",[14,13522,13523],{},"Python method names are case-sensitive, so spelling and capitalization both matter.",[77,13525,6244],{"id":6243},[14,13527,13528],{},"Here are the most useful ways to fix this error.",[675,13530,13532],{"id":13531},"_1-check-the-object-type","1. Check the object type",[14,13534,241,13535,13539],{},[295,13536,13537],{"href":4427},[17,13538,4430],{}," to see what the value really is:",[91,13541,13543],{"className":93,"code":13542,"language":95,"meta":96,"style":96},"value = [\"a\", \"b\", \"c\"]\nprint(type(value))\n",[17,13544,13545,13577],{"__ignoreMap":96},[100,13546,13547,13549,13551,13553,13555,13557,13559,13561,13563,13565,13567,13569,13571,13573,13575],{"class":102,"line":103},[100,13548,3318],{"class":106},[100,13550,111],{"class":110},[100,13552,594],{"class":118},[100,13554,206],{"class":205},[100,13556,295],{"class":209},[100,13558,206],{"class":205},[100,13560,126],{"class":118},[100,13562,1708],{"class":205},[100,13564,4027],{"class":209},[100,13566,206],{"class":205},[100,13568,126],{"class":118},[100,13570,1708],{"class":205},[100,13572,4036],{"class":209},[100,13574,206],{"class":205},[100,13576,603],{"class":118},[100,13578,13579,13581,13583,13585,13587,13589],{"class":102,"line":135},[100,13580,372],{"class":114},[100,13582,170],{"class":118},[100,13584,1250],{"class":191},[100,13586,170],{"class":118},[100,13588,757],{"class":178},[100,13590,182],{"class":118},[14,13592,218],{},[91,13594,13595],{"className":93,"code":4189,"language":95,"meta":96,"style":96},[17,13596,13597],{"__ignoreMap":96},[100,13598,13599,13601,13603,13605,13607,13609],{"class":102,"line":103},[100,13600,2782],{"class":110},[100,13602,3417],{"class":1077},[100,13604,1274],{"class":205},[100,13606,1235],{"class":209},[100,13608,1280],{"class":205},[100,13610,1980],{"class":110},[14,13612,13613],{},"If the type is not what you expected, trace back to where that value came from.",[675,13615,13617],{"id":13616},"_2-print-the-value-before-the-failing-line","2. Print the value before the failing line",[14,13619,13620],{},"Sometimes the variable changed earlier in the program.",[91,13622,13624],{"className":93,"code":13623,"language":95,"meta":96,"style":96},"result = None\nprint(result)\nprint(type(result))\n",[17,13625,13626,13634,13644],{"__ignoreMap":96},[100,13627,13628,13630,13632],{"class":102,"line":103},[100,13629,11556],{"class":106},[100,13631,111],{"class":110},[100,13633,4904],{"class":158},[100,13635,13636,13638,13640,13642],{"class":102,"line":135},[100,13637,372],{"class":114},[100,13639,170],{"class":118},[100,13641,11580],{"class":178},[100,13643,215],{"class":118},[100,13645,13646,13648,13650,13652,13654,13656],{"class":102,"line":142},[100,13647,372],{"class":114},[100,13649,170],{"class":118},[100,13651,1250],{"class":191},[100,13653,170],{"class":118},[100,13655,11580],{"class":178},[100,13657,182],{"class":118},[14,13659,13660],{},"This helps you confirm what Python is actually working with.",[675,13662,13664,13665],{"id":13663},"_3-inspect-available-attributes-with-dir","3. Inspect available attributes with ",[17,13666,4565],{},[14,13668,13669,13670,13674],{},"You can use ",[295,13671,13672],{"href":4562},[17,13673,4565],{}," to list what an object supports:",[91,13676,13678],{"className":93,"code":13677,"language":95,"meta":96,"style":96},"text = \"hello\"\nprint(dir(text))\n",[17,13679,13680,13692],{"__ignoreMap":96},[100,13681,13682,13684,13686,13688,13690],{"class":102,"line":103},[100,13683,4331],{"class":106},[100,13685,111],{"class":110},[100,13687,1708],{"class":205},[100,13689,3506],{"class":209},[100,13691,1714],{"class":205},[100,13693,13694,13696,13698,13700,13702,13704],{"class":102,"line":135},[100,13695,372],{"class":114},[100,13697,170],{"class":118},[100,13699,3822],{"class":114},[100,13701,170],{"class":118},[100,13703,4468],{"class":178},[100,13705,182],{"class":118},[14,13707,13708],{},"This can help you check whether a method really exists.",[675,13710,13712],{"id":13711},"_4-check-function-return-values-before-chaining-methods","4. Check function return values before chaining methods",[14,13714,13715,13716,290],{},"Some methods return a new object. Others change the original object and return ",[17,13717,930],{},[14,13719,13720],{},"Bad example:",[91,13722,13724],{"className":93,"code":13723,"language":95,"meta":96,"style":96},"numbers = [3, 1, 2]\nresult = numbers.sort()\nprint(result.append(4))\n",[17,13725,13726,13746,13760],{"__ignoreMap":96},[100,13727,13728,13730,13732,13734,13736,13738,13740,13742,13744],{"class":102,"line":103},[100,13729,107],{"class":106},[100,13731,111],{"class":110},[100,13733,594],{"class":118},[100,13735,11541],{"class":122},[100,13737,126],{"class":118},[100,13739,6915],{"class":122},[100,13741,126],{"class":118},[100,13743,129],{"class":122},[100,13745,603],{"class":118},[100,13747,13748,13750,13752,13754,13756,13758],{"class":102,"line":135},[100,13749,11556],{"class":106},[100,13751,111],{"class":110},[100,13753,620],{"class":106},[100,13755,290],{"class":118},[100,13757,11565],{"class":178},[100,13759,4734],{"class":118},[100,13761,13762,13764,13766,13768,13770,13772,13774,13776],{"class":102,"line":142},[100,13763,372],{"class":114},[100,13765,170],{"class":118},[100,13767,11580],{"class":178},[100,13769,290],{"class":118},[100,13771,4254],{"class":178},[100,13773,170],{"class":118},[100,13775,8322],{"class":122},[100,13777,182],{"class":118},[14,13779,6031],{},[91,13781,13783],{"className":93,"code":13782,"language":95,"meta":96,"style":96},"numbers = [3, 1, 2]\nnumbers.sort()\nnumbers.append(4)\nprint(numbers)\n",[17,13784,13785,13805,13815,13829],{"__ignoreMap":96},[100,13786,13787,13789,13791,13793,13795,13797,13799,13801,13803],{"class":102,"line":103},[100,13788,107],{"class":106},[100,13790,111],{"class":110},[100,13792,594],{"class":118},[100,13794,11541],{"class":122},[100,13796,126],{"class":118},[100,13798,6915],{"class":122},[100,13800,126],{"class":118},[100,13802,129],{"class":122},[100,13804,603],{"class":118},[100,13806,13807,13809,13811,13813],{"class":102,"line":135},[100,13808,179],{"class":106},[100,13810,290],{"class":118},[100,13812,11565],{"class":178},[100,13814,4734],{"class":118},[100,13816,13817,13819,13821,13823,13825,13827],{"class":102,"line":142},[100,13818,179],{"class":106},[100,13820,290],{"class":118},[100,13822,4254],{"class":178},[100,13824,170],{"class":118},[100,13826,8322],{"class":122},[100,13828,215],{"class":118},[100,13830,13831,13833,13835,13837],{"class":102,"line":152},[100,13832,372],{"class":114},[100,13834,170],{"class":118},[100,13836,179],{"class":178},[100,13838,215],{"class":118},[14,13840,218],{},[91,13842,13843],{"className":93,"code":8343,"language":95,"meta":96,"style":96},[17,13844,13845],{"__ignoreMap":96},[100,13846,13847,13849,13851,13853,13855,13857,13859,13861,13863],{"class":102,"line":103},[100,13848,3166],{"class":118},[100,13850,123],{"class":122},[100,13852,126],{"class":118},[100,13854,129],{"class":122},[100,13856,126],{"class":118},[100,13858,1405],{"class":122},[100,13860,126],{"class":118},[100,13862,2584],{"class":122},[100,13864,603],{"class":118},[675,13866,13868],{"id":13867},"_5-use-the-right-method-for-the-object-type","5. Use the right method for the object type",[14,13870,9709],{},[91,13872,13874],{"className":93,"code":13873,"language":95,"meta":96,"style":96},"items = [\"a\", \"b\", \"c\"]\nitems.upper()\n",[17,13875,13876,13908],{"__ignoreMap":96},[100,13877,13878,13880,13882,13884,13886,13888,13890,13892,13894,13896,13898,13900,13902,13904,13906],{"class":102,"line":103},[100,13879,3095],{"class":106},[100,13881,111],{"class":110},[100,13883,594],{"class":118},[100,13885,206],{"class":205},[100,13887,295],{"class":209},[100,13889,206],{"class":205},[100,13891,126],{"class":118},[100,13893,1708],{"class":205},[100,13895,4027],{"class":209},[100,13897,206],{"class":205},[100,13899,126],{"class":118},[100,13901,1708],{"class":205},[100,13903,4036],{"class":209},[100,13905,206],{"class":205},[100,13907,603],{"class":118},[100,13909,13910,13912,13914,13916],{"class":102,"line":135},[100,13911,3146],{"class":106},[100,13913,290],{"class":118},[100,13915,4540],{"class":178},[100,13917,4734],{"class":118},[14,13919,9750],{},[91,13921,13923],{"className":93,"code":13922,"language":95,"meta":96,"style":96},"items = [\"a\", \"b\", \"c\"]\nupper_items = [item.upper() for item in items]\nprint(upper_items)\n",[17,13924,13925,13957,13984],{"__ignoreMap":96},[100,13926,13927,13929,13931,13933,13935,13937,13939,13941,13943,13945,13947,13949,13951,13953,13955],{"class":102,"line":103},[100,13928,3095],{"class":106},[100,13930,111],{"class":110},[100,13932,594],{"class":118},[100,13934,206],{"class":205},[100,13936,295],{"class":209},[100,13938,206],{"class":205},[100,13940,126],{"class":118},[100,13942,1708],{"class":205},[100,13944,4027],{"class":209},[100,13946,206],{"class":205},[100,13948,126],{"class":118},[100,13950,1708],{"class":205},[100,13952,4036],{"class":209},[100,13954,206],{"class":205},[100,13956,603],{"class":118},[100,13958,13959,13962,13964,13966,13968,13970,13972,13974,13976,13978,13980,13982],{"class":102,"line":135},[100,13960,13961],{"class":106},"upper_items ",[100,13963,111],{"class":110},[100,13965,594],{"class":118},[100,13967,7555],{"class":106},[100,13969,290],{"class":118},[100,13971,4540],{"class":178},[100,13973,7562],{"class":118},[100,13975,7565],{"class":145},[100,13977,7568],{"class":106},[100,13979,617],{"class":145},[100,13981,12000],{"class":106},[100,13983,603],{"class":118},[100,13985,13986,13988,13990,13993],{"class":102,"line":142},[100,13987,372],{"class":114},[100,13989,170],{"class":118},[100,13991,13992],{"class":178},"upper_items",[100,13994,215],{"class":118},[14,13996,218],{},[91,13998,14000],{"className":93,"code":13999,"language":95,"meta":96,"style":96},"['A', 'B', 'C']\n",[17,14001,14002],{"__ignoreMap":96},[100,14003,14004,14006,14008,14010,14012,14014,14016,14018,14020,14022,14024,14026,14028],{"class":102,"line":103},[100,14005,3166],{"class":118},[100,14007,1280],{"class":205},[100,14009,7414],{"class":209},[100,14011,1280],{"class":205},[100,14013,126],{"class":118},[100,14015,1274],{"class":205},[100,14017,7423],{"class":209},[100,14019,1280],{"class":205},[100,14021,126],{"class":118},[100,14023,1274],{"class":205},[100,14025,7432],{"class":209},[100,14027,1280],{"class":205},[100,14029,603],{"class":118},[675,14031,14033],{"id":14032},"_6-fix-spelling-mistakes","6. Fix spelling mistakes",[14,14035,9709],{},[91,14037,14039],{"className":93,"code":14038,"language":95,"meta":96,"style":96},"text = \"hello\"\nprint(text.uppr())\n",[17,14040,14041,14053],{"__ignoreMap":96},[100,14042,14043,14045,14047,14049,14051],{"class":102,"line":103},[100,14044,4331],{"class":106},[100,14046,111],{"class":110},[100,14048,1708],{"class":205},[100,14050,3506],{"class":209},[100,14052,1714],{"class":205},[100,14054,14055,14057,14059,14061,14063,14066],{"class":102,"line":135},[100,14056,372],{"class":114},[100,14058,170],{"class":118},[100,14060,4468],{"class":178},[100,14062,290],{"class":118},[100,14064,14065],{"class":178},"uppr",[100,14067,3370],{"class":118},[14,14069,9750],{},[91,14071,14072],{"className":93,"code":4513,"language":95,"meta":96,"style":96},[17,14073,14074,14086],{"__ignoreMap":96},[100,14075,14076,14078,14080,14082,14084],{"class":102,"line":103},[100,14077,4331],{"class":106},[100,14079,111],{"class":110},[100,14081,1708],{"class":205},[100,14083,3506],{"class":209},[100,14085,1714],{"class":205},[100,14087,14088,14090,14092,14094,14096,14098],{"class":102,"line":135},[100,14089,372],{"class":114},[100,14091,170],{"class":118},[100,14093,4468],{"class":178},[100,14095,290],{"class":118},[100,14097,4540],{"class":178},[100,14099,3370],{"class":118},[14,14101,218],{},[91,14103,14104],{"className":93,"code":4547,"language":95,"meta":96,"style":96},[17,14105,14106],{"__ignoreMap":96},[100,14107,14108],{"class":102,"line":103},[100,14109,4547],{"class":527},[77,14111,14113],{"id":14112},"common-beginner-cases","Common beginner cases",[675,14115,14117],{"id":14116},"attributeerror-nonetype-object-has-no-attribute",[17,14118,14119],{},"AttributeError: 'NoneType' object has no attribute ...",[14,14121,14122,14123,290],{},"This usually means your variable is ",[17,14124,930],{},[14,14126,14127],{},"Common causes:",[40,14129,14130,14133,14142],{},[43,14131,14132],{},"A function did not return a value",[43,14134,14135,14136,5870,14138,5894,14140],{},"You used the result of ",[17,14137,7659],{},[17,14139,8139],{},[17,14141,13235],{},[43,14143,14144,14145,14147],{},"A value was set to ",[17,14146,930],{}," earlier",[14,14149,14150,14151,290],{},"For a more focused guide, see ",[295,14152,7113],{"href":7112},[675,14154,14156],{"id":14155},"attributeerror-list-object-has-no-attribute",[17,14157,14158],{},"AttributeError: 'list' object has no attribute ...",[14,14160,14161],{},"This means you called a method that lists do not support.",[14,14163,1844],{},[91,14165,14167],{"className":93,"code":14166,"language":95,"meta":96,"style":96},"items = [\"a\", \"b\"]\nprint(items.split(\",\"))\n",[17,14168,14169,14193],{"__ignoreMap":96},[100,14170,14171,14173,14175,14177,14179,14181,14183,14185,14187,14189,14191],{"class":102,"line":103},[100,14172,3095],{"class":106},[100,14174,111],{"class":110},[100,14176,594],{"class":118},[100,14178,206],{"class":205},[100,14180,295],{"class":209},[100,14182,206],{"class":205},[100,14184,126],{"class":118},[100,14186,1708],{"class":205},[100,14188,4027],{"class":209},[100,14190,206],{"class":205},[100,14192,603],{"class":118},[100,14194,14195,14197,14199,14201,14203,14205,14207,14209,14211,14213],{"class":102,"line":135},[100,14196,372],{"class":114},[100,14198,170],{"class":118},[100,14200,3146],{"class":178},[100,14202,290],{"class":118},[100,14204,3922],{"class":178},[100,14206,170],{"class":118},[100,14208,206],{"class":205},[100,14210,126],{"class":209},[100,14212,206],{"class":205},[100,14214,182],{"class":118},[14,14216,14217,13319],{},[17,14218,8090],{},[14,14220,14221,14222,290],{},"See also ",[295,14223,7107],{"href":7106},[675,14225,14227],{"id":14226},"attributeerror-str-object-has-no-attribute",[17,14228,14229],{},"AttributeError: 'str' object has no attribute ...",[14,14231,14232],{},"This means you used a method that belongs to some other type.",[14,14234,1844],{},[91,14236,14238],{"className":93,"code":14237,"language":95,"meta":96,"style":96},"name = \"Python\"\nname.append(\"!\")\n",[17,14239,14240,14252],{"__ignoreMap":96},[100,14241,14242,14244,14246,14248,14250],{"class":102,"line":103},[100,14243,1691],{"class":106},[100,14245,111],{"class":110},[100,14247,1708],{"class":205},[100,14249,12937],{"class":209},[100,14251,1714],{"class":205},[100,14253,14254,14256,14258,14260,14262,14264,14267,14269],{"class":102,"line":135},[100,14255,2853],{"class":106},[100,14257,290],{"class":118},[100,14259,4254],{"class":178},[100,14261,170],{"class":118},[100,14263,206],{"class":205},[100,14265,14266],{"class":209},"!",[100,14268,206],{"class":205},[100,14270,215],{"class":118},[14,14272,14273,14274,290],{},"Strings do not have ",[17,14275,7659],{},[14,14277,14221,14278,290],{},[295,14279,7101],{"href":7100},[675,14281,14283],{"id":14282},"attributeerror-when-importing-a-module-and-calling-the-wrong-name","AttributeError when importing a module and calling the wrong name",[14,14285,14286],{},"Sometimes the object is a module, but the attribute name is wrong.",[14,14288,1844],{},[91,14290,14292],{"className":93,"code":14291,"language":95,"meta":96,"style":96},"import math\n\nprint(math.PI)\n",[17,14293,14294,14300,14304],{"__ignoreMap":96},[100,14295,14296,14298],{"class":102,"line":103},[100,14297,9259],{"class":145},[100,14299,9262],{"class":106},[100,14301,14302],{"class":102,"line":135},[100,14303,139],{"emptyLinePlaceholder":138},[100,14305,14306,14308,14310,14312,14314,14318],{"class":102,"line":142},[100,14307,372],{"class":114},[100,14309,170],{"class":118},[100,14311,9271],{"class":178},[100,14313,290],{"class":118},[100,14315,14317],{"class":14316},"swQdS","PI",[100,14319,215],{"class":118},[14,14321,14322,14323,290],{},"This fails because the correct name is ",[17,14324,14325],{},"math.pi",[14,14327,6031],{},[91,14329,14331],{"className":93,"code":14330,"language":95,"meta":96,"style":96},"import math\n\nprint(math.pi)\n",[17,14332,14333,14339,14343],{"__ignoreMap":96},[100,14334,14335,14337],{"class":102,"line":103},[100,14336,9259],{"class":145},[100,14338,9262],{"class":106},[100,14340,14341],{"class":102,"line":135},[100,14342,139],{"emptyLinePlaceholder":138},[100,14344,14345,14347,14349,14351,14353,14356],{"class":102,"line":142},[100,14346,372],{"class":114},[100,14348,170],{"class":118},[100,14350,9271],{"class":178},[100,14352,290],{"class":118},[100,14354,14355],{"class":2494},"pi",[100,14357,215],{"class":118},[14,14359,218],{},[91,14361,14363],{"className":93,"code":14362,"language":95,"meta":96,"style":96},"3.141592653589793\n",[17,14364,14365],{"__ignoreMap":96},[100,14366,14367],{"class":102,"line":103},[100,14368,14362],{"class":122},[77,14370,8550],{"id":8549},[14,14372,14373],{},"When you see this error, work through these steps:",[3282,14375,14376,14379,14382,14386,14389,14392],{},[43,14377,14378],{},"Find the line named in the traceback",[43,14380,14381],{},"Look at the value before the dot",[43,14383,5163,14384],{},[17,14385,5166],{},[43,14387,14388],{},"Check whether that type supports the attribute",[43,14390,14391],{},"Trace back where the value came from",[43,14393,14394],{},"Confirm the function or method returned what you expected",[14,14396,5295],{},[91,14398,14400],{"className":93,"code":14399,"language":95,"meta":96,"style":96},"print(value)\nprint(type(value))\nprint(dir(value))\nhelp(type(value))\n",[17,14401,14402,14412,14426,14440],{"__ignoreMap":96},[100,14403,14404,14406,14408,14410],{"class":102,"line":103},[100,14405,372],{"class":114},[100,14407,170],{"class":118},[100,14409,757],{"class":178},[100,14411,215],{"class":118},[100,14413,14414,14416,14418,14420,14422,14424],{"class":102,"line":135},[100,14415,372],{"class":114},[100,14417,170],{"class":118},[100,14419,1250],{"class":191},[100,14421,170],{"class":118},[100,14423,757],{"class":178},[100,14425,182],{"class":118},[100,14427,14428,14430,14432,14434,14436,14438],{"class":102,"line":142},[100,14429,372],{"class":114},[100,14431,170],{"class":118},[100,14433,3822],{"class":114},[100,14435,170],{"class":118},[100,14437,757],{"class":178},[100,14439,182],{"class":118},[100,14441,14442,14444,14446,14448,14450,14452],{"class":102,"line":152},[100,14443,2478],{"class":114},[100,14445,170],{"class":118},[100,14447,1250],{"class":191},[100,14449,170],{"class":118},[100,14451,757],{"class":178},[100,14453,182],{"class":118},[14,14455,14456,14457,290],{},"If you want a beginner-friendly process for debugging, read the ",[295,14458,7077],{"href":3852},[77,14460,1514],{"id":1513},[675,14462,5598],{"id":5597},[14,14464,14465,14466,3178,14469,290],{},"An attribute is something attached to an object, such as a variable or method. You access it with dot notation like ",[17,14467,14468],{},"obj.name",[17,14470,14471],{},"obj.method()",[675,14473,14475],{"id":14474},"why-do-i-get-this-error-with-none","Why do I get this error with None?",[14,14477,14478,14479,14481,14482,3178,14484,14486,14487,14489],{},"Because ",[17,14480,930],{}," is a special value, and many methods like ",[17,14483,7659],{},[17,14485,8090],{}," do not exist on it. Often a function returned ",[17,14488,930],{}," instead of the value you expected.",[675,14491,14493],{"id":14492},"how-do-i-know-which-methods-an-object-has","How do I know which methods an object has?",[14,14495,241,14496,14499],{},[17,14497,14498],{},"dir(obj)"," to list available attributes and methods, or check the official documentation for that object type.",[675,14501,14503],{"id":14502},"is-this-the-same-as-nameerror","Is this the same as NameError?",[14,14505,14506,14507,14509,14510,14512],{},"No. ",[17,14508,5616],{}," means Python does not know the variable name at all. ",[17,14511,3738],{}," means the variable exists, but the attribute after the dot does not.",[77,14514,1554],{"id":1553},[40,14516,14517,14521,14525,14529,14533,14537,14543],{},[43,14518,14519],{},[295,14520,8973],{"href":8972},[43,14522,14523],{},[295,14524,7107],{"href":7106},[43,14526,14527],{},[295,14528,7101],{"href":7100},[43,14530,14531],{},[295,14532,7113],{"href":7112},[43,14534,14535],{},[295,14536,1587],{"href":1749},[43,14538,14539],{},[295,14540,1572,14541,1576],{"href":4427},[17,14542,4430],{},[43,14544,14545],{},[295,14546,1572,14547,1576],{"href":4562},[17,14548,4565],{},[1589,14550,14551],{},"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 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 .srdBf, html code.shiki .srdBf{--shiki-light:#F76D47;--shiki-default:#005CC5;--shiki-dark:#79B8FF}html pre.shiki code .s39Yj, html code.shiki .s39Yj{--shiki-light:#39ADB5;--shiki-default:#005CC5;--shiki-dark:#79B8FF}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 .s_hVV, html code.shiki .s_hVV{--shiki-light:#90A4AE;--shiki-default:#005CC5;--shiki-dark:#79B8FF}html pre.shiki code .sZMiF, html code.shiki .sZMiF{--shiki-light:#E2931D;--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 .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 .swQdS, html code.shiki .swQdS{--shiki-light:#E53935;--shiki-default:#005CC5;--shiki-dark:#79B8FF}html pre.shiki code .skxfh, html code.shiki .skxfh{--shiki-light:#E53935;--shiki-default:#24292E;--shiki-dark:#E1E4E8}",{"title":96,"searchDepth":135,"depth":135,"links":14553},[14554,14555,14556,14557,14563,14572,14578,14579,14585],{"id":79,"depth":135,"text":80},{"id":5880,"depth":135,"text":5881},{"id":7740,"depth":135,"text":7741},{"id":11030,"depth":135,"text":11031,"children":14558},[14559,14560,14562],{"id":13261,"depth":142,"text":13262},{"id":13322,"depth":142,"text":14561},"Calling append() on None",{"id":13479,"depth":142,"text":13480},{"id":6243,"depth":135,"text":6244,"children":14564},[14565,14566,14567,14569,14570,14571],{"id":13531,"depth":142,"text":13532},{"id":13616,"depth":142,"text":13617},{"id":13663,"depth":142,"text":14568},"3. Inspect available attributes with dir()",{"id":13711,"depth":142,"text":13712},{"id":13867,"depth":142,"text":13868},{"id":14032,"depth":142,"text":14033},{"id":14112,"depth":135,"text":14113,"children":14573},[14574,14575,14576,14577],{"id":14116,"depth":142,"text":14119},{"id":14155,"depth":142,"text":14158},{"id":14226,"depth":142,"text":14229},{"id":14282,"depth":142,"text":14283},{"id":8549,"depth":135,"text":8550},{"id":1513,"depth":135,"text":1514,"children":14580},[14581,14582,14583,14584],{"id":5597,"depth":142,"text":5598},{"id":14474,"depth":142,"text":14475},{"id":14492,"depth":142,"text":14493},{"id":14502,"depth":142,"text":14503},{"id":1553,"depth":135,"text":1554},"Master attributeerror object has no attribute fix in our comprehensive Python beginner guide.",{},{"title":9381,"description":14586},"errors\u002Fattributeerror-object-has-no-attribute-fix","9lky8kZG5OC3E1Vfd73LfnyZlP_GC-XvDxf2bT1ycOQ",{"id":14592,"title":14593,"body":14594,"description":16151,"extension":1623,"meta":16152,"navigation":138,"path":7100,"seo":16153,"stem":16154,"__hash__":16155},"content\u002Ferrors\u002Fattributeerror-str-object-has-no-attribute-fix.md","AttributeError: 'str' object has no attribute (Fix)",{"type":7,"value":14595,"toc":16119},[14596,14601,14604,14607,14615,14618,14620,14740,14743,14817,14823,14825,14828,14831,14856,14859,14871,14874,14885,14887,14890,14911,14914,14952,14958,14977,14984,14986,14992,15027,15032,15035,15079,15084,15116,15120,15123,15129,15169,15174,15177,15248,15250,15259,15268,15275,15312,15323,15327,15361,15370,15373,15400,15402,15405,15448,15450,15546,15548,15622,15627,15629,15632,15655,15657,15728,15730,15758,15764,15767,15770,15787,15789,15823,15826,15853,15856,15908,15910,15918,15920,15923,15961,15964,15985,15987,15991,15997,16001,16006,16037,16048,16054,16059,16066,16073,16079,16086,16088,16116],[10,14597,5728,14599,7375],{"id":14598},"attributeerror-str-object-has-no-attribute-fix",[17,14600,5528],{},[14,14602,14603],{},"This error happens when your variable is a string, but your code uses a method or attribute that does not belong to Python strings.",[14,14605,14606],{},"In simple terms, Python is telling you:",[40,14608,14609,14612],{},[43,14610,14611],{},"“This value is text”",[43,14613,14614],{},"“The thing after the dot does not exist on text”",[14,14616,14617],{},"This often happens when you expected a list, dictionary, or custom object, but the variable is actually a string.",[77,14619,80],{"id":79},[91,14621,14623],{"className":93,"code":14622,"language":95,"meta":96,"style":96},"value = \"hello\"\n\n# Wrong: strings do not have append()\n# value.append(\"!\")\n\n# Fix 1: use string concatenation\nvalue = value + \"!\"\nprint(value)\n\n# Fix 2: convert to the right type before using list methods\nchars = list(value)\nchars.append(\"?\")\nprint(chars)\n",[17,14624,14625,14637,14641,14646,14651,14655,14660,14676,14686,14690,14695,14711,14730],{"__ignoreMap":96},[100,14626,14627,14629,14631,14633,14635],{"class":102,"line":103},[100,14628,3318],{"class":106},[100,14630,111],{"class":110},[100,14632,1708],{"class":205},[100,14634,3506],{"class":209},[100,14636,1714],{"class":205},[100,14638,14639],{"class":102,"line":135},[100,14640,139],{"emptyLinePlaceholder":138},[100,14642,14643],{"class":102,"line":142},[100,14644,14645],{"class":414},"# Wrong: strings do not have append()\n",[100,14647,14648],{"class":102,"line":152},[100,14649,14650],{"class":414},"# value.append(\"!\")\n",[100,14652,14653],{"class":102,"line":164},[100,14654,139],{"emptyLinePlaceholder":138},[100,14656,14657],{"class":102,"line":185},[100,14658,14659],{"class":414},"# Fix 1: use string concatenation\n",[100,14661,14662,14664,14666,14668,14670,14672,14674],{"class":102,"line":197},[100,14663,3318],{"class":106},[100,14665,111],{"class":110},[100,14667,989],{"class":106},[100,14669,6547],{"class":110},[100,14671,1708],{"class":205},[100,14673,14266],{"class":209},[100,14675,1714],{"class":205},[100,14677,14678,14680,14682,14684],{"class":102,"line":771},[100,14679,372],{"class":114},[100,14681,170],{"class":118},[100,14683,757],{"class":178},[100,14685,215],{"class":118},[100,14687,14688],{"class":102,"line":787},[100,14689,139],{"emptyLinePlaceholder":138},[100,14691,14692],{"class":102,"line":5816},[100,14693,14694],{"class":414},"# Fix 2: convert to the right type before using list methods\n",[100,14696,14697,14700,14702,14705,14707,14709],{"class":102,"line":5833},[100,14698,14699],{"class":106},"chars ",[100,14701,111],{"class":110},[100,14703,14704],{"class":191}," list",[100,14706,170],{"class":118},[100,14708,757],{"class":178},[100,14710,215],{"class":118},[100,14712,14713,14716,14718,14720,14722,14724,14726,14728],{"class":102,"line":5848},[100,14714,14715],{"class":106},"chars",[100,14717,290],{"class":118},[100,14719,4254],{"class":178},[100,14721,170],{"class":118},[100,14723,206],{"class":205},[100,14725,11353],{"class":209},[100,14727,206],{"class":205},[100,14729,215],{"class":118},[100,14731,14732,14734,14736,14738],{"class":102,"line":5855},[100,14733,372],{"class":114},[100,14735,170],{"class":118},[100,14737,14715],{"class":178},[100,14739,215],{"class":118},[14,14741,14742],{},"Expected output:",[91,14744,14746],{"className":93,"code":14745,"language":95,"meta":96,"style":96},"hello!\n['h', 'e', 'l', 'l', 'o', '!', '?']\n",[17,14747,14748,14753],{"__ignoreMap":96},[100,14749,14750],{"class":102,"line":103},[100,14751,14752],{"class":106},"hello!\n",[100,14754,14755,14757,14759,14762,14764,14766,14768,14771,14773,14775,14777,14780,14782,14784,14786,14788,14790,14792,14794,14797,14799,14801,14803,14805,14807,14809,14811,14813,14815],{"class":102,"line":135},[100,14756,3166],{"class":118},[100,14758,1280],{"class":205},[100,14760,14761],{"class":209},"h",[100,14763,1280],{"class":205},[100,14765,126],{"class":118},[100,14767,1274],{"class":205},[100,14769,14770],{"class":209},"e",[100,14772,1280],{"class":205},[100,14774,126],{"class":118},[100,14776,1274],{"class":205},[100,14778,14779],{"class":209},"l",[100,14781,1280],{"class":205},[100,14783,126],{"class":118},[100,14785,1274],{"class":205},[100,14787,14779],{"class":209},[100,14789,1280],{"class":205},[100,14791,126],{"class":118},[100,14793,1274],{"class":205},[100,14795,14796],{"class":209},"o",[100,14798,1280],{"class":205},[100,14800,126],{"class":118},[100,14802,1274],{"class":205},[100,14804,14266],{"class":209},[100,14806,1280],{"class":205},[100,14808,126],{"class":118},[100,14810,1274],{"class":205},[100,14812,11353],{"class":209},[100,14814,1280],{"class":205},[100,14816,603],{"class":118},[14,14818,14819,14820,14822],{},"First check the type of the variable with ",[17,14821,5166],{},". This error usually means you expected a list, dictionary, or custom object, but the variable is actually a string.",[77,14824,5881],{"id":5880},[14,14826,14827],{},"Python raises this error when you try to use an attribute or method that a string does not have.",[14,14829,14830],{},"An attribute is the part after the dot:",[91,14832,14834],{"className":93,"code":14833,"language":95,"meta":96,"style":96},"name.lower()\nname.append()\n",[17,14835,14836,14846],{"__ignoreMap":96},[100,14837,14838,14840,14842,14844],{"class":102,"line":103},[100,14839,2853],{"class":106},[100,14841,290],{"class":118},[100,14843,5802],{"class":178},[100,14845,4734],{"class":118},[100,14847,14848,14850,14852,14854],{"class":102,"line":135},[100,14849,2853],{"class":106},[100,14851,290],{"class":118},[100,14853,4254],{"class":178},[100,14855,4734],{"class":118},[14,14857,14858],{},"Here:",[40,14860,14861,14866],{},[43,14862,14863,14865],{},[17,14864,7394],{}," is a valid string method",[43,14867,14868,14870],{},[17,14869,7659],{}," is not a string method",[14,14872,14873],{},"Some methods belong to strings, but many belong to other types like lists or dictionaries.",[14,14875,14876,14877,3266,14880,290],{},"The main fix is to confirm the variable type and use a method that matches that type. If you need a refresher, see ",[295,14878,6080],{"href":14879},"\u002Flearn\u002Fpython-strings-explained-basics-and-examples\u002F",[295,14881,14882,14883,6354],{"href":4427},"the Python ",[17,14884,4430],{},[77,14886,7741],{"id":7740},[14,14888,14889],{},"Common reasons include:",[40,14891,14892,14895,14898,14901,14908],{},[43,14893,14894],{},"You thought the variable was a list, but it is actually a string.",[43,14896,14897],{},"You tried to call a dictionary method on text data.",[43,14899,14900],{},"You reassigned a variable and changed its type earlier in the code.",[43,14902,14903,14904,14907],{},"User input from ",[17,14905,14906],{},"input()"," is always a string unless you convert it.",[43,14909,14910],{},"Data read from a file or API may be text, not a Python object yet.",[14,14912,14913],{},"For example, this is a very common mistake:",[91,14915,14917],{"className":93,"code":14916,"language":95,"meta":96,"style":96},"value = input(\"Enter numbers: \")\nprint(type(value))\n",[17,14918,14919,14938],{"__ignoreMap":96},[100,14920,14921,14923,14925,14927,14929,14931,14934,14936],{"class":102,"line":103},[100,14922,3318],{"class":106},[100,14924,111],{"class":110},[100,14926,7221],{"class":114},[100,14928,170],{"class":118},[100,14930,206],{"class":205},[100,14932,14933],{"class":209},"Enter numbers: ",[100,14935,206],{"class":205},[100,14937,215],{"class":118},[100,14939,14940,14942,14944,14946,14948,14950],{"class":102,"line":135},[100,14941,372],{"class":114},[100,14943,170],{"class":118},[100,14945,1250],{"class":191},[100,14947,170],{"class":118},[100,14949,757],{"class":178},[100,14951,182],{"class":118},[14,14953,14954,14955,14957],{},"If the user types ",[17,14956,3002],{},", the result is still a string:",[91,14959,14961],{"className":93,"code":14960,"language":95,"meta":96,"style":96},"\u003Cclass 'str'>\n",[17,14962,14963],{"__ignoreMap":96},[100,14964,14965,14967,14969,14971,14973,14975],{"class":102,"line":103},[100,14966,2782],{"class":110},[100,14968,3417],{"class":1077},[100,14970,1274],{"class":205},[100,14972,3422],{"class":209},[100,14974,1280],{"class":205},[100,14976,1980],{"class":110},[14,14978,14979,14980,290],{},"If you need a number, convert it first. See ",[295,14981,14983],{"href":14982},"\u002Fhow-to\u002Fhow-to-convert-string-to-int-in-python\u002F","how to convert a string to int in Python",[77,14985,7772],{"id":7771},[675,14987,315,14989,14991],{"id":14988},"calling-append-on-a-string",[17,14990,7659],{}," on a string",[91,14993,14995],{"className":93,"code":14994,"language":95,"meta":96,"style":96},"text = \"hello\"\ntext.append(\"!\")\n",[17,14996,14997,15009],{"__ignoreMap":96},[100,14998,14999,15001,15003,15005,15007],{"class":102,"line":103},[100,15000,4331],{"class":106},[100,15002,111],{"class":110},[100,15004,1708],{"class":205},[100,15006,3506],{"class":209},[100,15008,1714],{"class":205},[100,15010,15011,15013,15015,15017,15019,15021,15023,15025],{"class":102,"line":135},[100,15012,4468],{"class":106},[100,15014,290],{"class":118},[100,15016,4254],{"class":178},[100,15018,170],{"class":118},[100,15020,206],{"class":205},[100,15022,14266],{"class":209},[100,15024,206],{"class":205},[100,15026,215],{"class":118},[14,15028,7194,15029,15031],{},[17,15030,7659],{}," is a list method, not a string method.",[14,15033,15034],{},"Fix:",[91,15036,15038],{"className":93,"code":15037,"language":95,"meta":96,"style":96},"text = \"hello\"\ntext = text + \"!\"\nprint(text)\n",[17,15039,15040,15052,15069],{"__ignoreMap":96},[100,15041,15042,15044,15046,15048,15050],{"class":102,"line":103},[100,15043,4331],{"class":106},[100,15045,111],{"class":110},[100,15047,1708],{"class":205},[100,15049,3506],{"class":209},[100,15051,1714],{"class":205},[100,15053,15054,15056,15058,15061,15063,15065,15067],{"class":102,"line":135},[100,15055,4331],{"class":106},[100,15057,111],{"class":110},[100,15059,15060],{"class":106}," text ",[100,15062,6547],{"class":110},[100,15064,1708],{"class":205},[100,15066,14266],{"class":209},[100,15068,1714],{"class":205},[100,15070,15071,15073,15075,15077],{"class":102,"line":142},[100,15072,372],{"class":114},[100,15074,170],{"class":118},[100,15076,4468],{"class":178},[100,15078,215],{"class":118},[675,15080,315,15082,14991],{"id":15081},"calling-keys-on-a-string",[17,15083,7891],{},[91,15085,15087],{"className":93,"code":15086,"language":95,"meta":96,"style":96},"data = \"name,age\"\nprint(data.keys())\n",[17,15088,15089,15102],{"__ignoreMap":96},[100,15090,15091,15093,15095,15097,15100],{"class":102,"line":103},[100,15092,7909],{"class":106},[100,15094,111],{"class":110},[100,15096,1708],{"class":205},[100,15098,15099],{"class":209},"name,age",[100,15101,1714],{"class":205},[100,15103,15104,15106,15108,15110,15112,15114],{"class":102,"line":135},[100,15105,372],{"class":114},[100,15107,170],{"class":118},[100,15109,7934],{"class":178},[100,15111,290],{"class":118},[100,15113,6185],{"class":178},[100,15115,3370],{"class":118},[14,15117,7194,15118,7946],{},[17,15119,7891],{},[14,15121,15122],{},"If your data is plain text, use string methods instead. If it should be structured data, convert or parse it first.",[675,15124,1357,15126,15128],{"id":15125},"using-get-on-a-string-instead-of-a-dictionary",[17,15127,13068],{}," on a string instead of a dictionary",[91,15130,15132],{"className":93,"code":15131,"language":95,"meta":96,"style":96},"data = '{\"name\": \"Ana\"}'\nprint(data.get(\"name\"))\n",[17,15133,15134,15147],{"__ignoreMap":96},[100,15135,15136,15138,15140,15142,15145],{"class":102,"line":103},[100,15137,7909],{"class":106},[100,15139,111],{"class":110},[100,15141,1274],{"class":205},[100,15143,15144],{"class":209},"{\"name\": \"Ana\"}",[100,15146,3925],{"class":205},[100,15148,15149,15151,15153,15155,15157,15159,15161,15163,15165,15167],{"class":102,"line":135},[100,15150,372],{"class":114},[100,15152,170],{"class":118},[100,15154,7934],{"class":178},[100,15156,290],{"class":118},[100,15158,12412],{"class":178},[100,15160,170],{"class":118},[100,15162,206],{"class":205},[100,15164,2853],{"class":209},[100,15166,206],{"class":205},[100,15168,182],{"class":118},[14,15170,7194,15171,15173],{},[17,15172,7934],{}," is still a string, not a dictionary.",[14,15175,15176],{},"Fix it by parsing the JSON text:",[91,15178,15180],{"className":93,"code":15179,"language":95,"meta":96,"style":96},"import json\n\ndata = '{\"name\": \"Ana\"}'\nparsed = json.loads(data)\nprint(parsed.get(\"name\"))\n",[17,15181,15182,15188,15192,15204,15225],{"__ignoreMap":96},[100,15183,15184,15186],{"class":102,"line":103},[100,15185,9259],{"class":145},[100,15187,9984],{"class":106},[100,15189,15190],{"class":102,"line":135},[100,15191,139],{"emptyLinePlaceholder":138},[100,15193,15194,15196,15198,15200,15202],{"class":102,"line":142},[100,15195,7909],{"class":106},[100,15197,111],{"class":110},[100,15199,1274],{"class":205},[100,15201,15144],{"class":209},[100,15203,3925],{"class":205},[100,15205,15206,15209,15211,15214,15216,15219,15221,15223],{"class":102,"line":152},[100,15207,15208],{"class":106},"parsed ",[100,15210,111],{"class":110},[100,15212,15213],{"class":106}," json",[100,15215,290],{"class":118},[100,15217,15218],{"class":178},"loads",[100,15220,170],{"class":118},[100,15222,7934],{"class":178},[100,15224,215],{"class":118},[100,15226,15227,15229,15231,15234,15236,15238,15240,15242,15244,15246],{"class":102,"line":164},[100,15228,372],{"class":114},[100,15230,170],{"class":118},[100,15232,15233],{"class":178},"parsed",[100,15235,290],{"class":118},[100,15237,12412],{"class":178},[100,15239,170],{"class":118},[100,15241,206],{"class":205},[100,15243,2853],{"class":209},[100,15245,206],{"class":205},[100,15247,182],{"class":118},[14,15249,14742],{},[91,15251,15253],{"className":93,"code":15252,"language":95,"meta":96,"style":96},"Ana\n",[17,15254,15255],{"__ignoreMap":96},[100,15256,15257],{"class":102,"line":103},[100,15258,15252],{"class":106},[14,15260,15261,15262,290],{},"If you are working with JSON text, see ",[295,15263,9421,15265,1576],{"href":15264},"\u002Fstandard-library\u002Fjson.loads-function-explained",[17,15266,15267],{},"json.loads()",[675,15269,315,15271,15274],{"id":15270},"calling-decode-on-a-string-in-python-3",[17,15272,15273],{},"decode()"," on a string in Python 3",[91,15276,15278],{"className":93,"code":15277,"language":95,"meta":96,"style":96},"text = \"hello\"\ntext.decode(\"utf-8\")\n",[17,15279,15280,15292],{"__ignoreMap":96},[100,15281,15282,15284,15286,15288,15290],{"class":102,"line":103},[100,15283,4331],{"class":106},[100,15285,111],{"class":110},[100,15287,1708],{"class":205},[100,15289,3506],{"class":209},[100,15291,1714],{"class":205},[100,15293,15294,15296,15298,15301,15303,15305,15308,15310],{"class":102,"line":135},[100,15295,4468],{"class":106},[100,15297,290],{"class":118},[100,15299,15300],{"class":178},"decode",[100,15302,170],{"class":118},[100,15304,206],{"class":205},[100,15306,15307],{"class":209},"utf-8",[100,15309,206],{"class":205},[100,15311,215],{"class":118},[14,15313,7194,15314,15316,15317,9472,15320,15322],{},[17,15315,15273],{}," is for ",[17,15318,15319],{},"bytes",[17,15321,3422],{}," in Python 3.",[675,15324,15326],{"id":15325},"trying-to-access-a-custom-object-attribute-on-plain-text","Trying to access a custom object attribute on plain text",[91,15328,15330],{"className":93,"code":15329,"language":95,"meta":96,"style":96},"user = \"Alice\"\nprint(user.name)\n",[17,15331,15332,15346],{"__ignoreMap":96},[100,15333,15334,15337,15339,15341,15344],{"class":102,"line":103},[100,15335,15336],{"class":106},"user ",[100,15338,111],{"class":110},[100,15340,1708],{"class":205},[100,15342,15343],{"class":209},"Alice",[100,15345,1714],{"class":205},[100,15347,15348,15350,15352,15355,15357,15359],{"class":102,"line":135},[100,15349,372],{"class":114},[100,15351,170],{"class":118},[100,15353,15354],{"class":178},"user",[100,15356,290],{"class":118},[100,15358,2853],{"class":2494},[100,15360,215],{"class":118},[14,15362,7194,15363,15365,15366,15369],{},[17,15364,15354],{}," is a string, not an object with a ",[17,15367,15368],{},".name"," attribute.",[14,15371,15372],{},"If you want the string itself, use it directly:",[91,15374,15376],{"className":93,"code":15375,"language":95,"meta":96,"style":96},"user = \"Alice\"\nprint(user)\n",[17,15377,15378,15390],{"__ignoreMap":96},[100,15379,15380,15382,15384,15386,15388],{"class":102,"line":103},[100,15381,15336],{"class":106},[100,15383,111],{"class":110},[100,15385,1708],{"class":205},[100,15387,15343],{"class":209},[100,15389,1714],{"class":205},[100,15391,15392,15394,15396,15398],{"class":102,"line":135},[100,15393,372],{"class":114},[100,15395,170],{"class":118},[100,15397,15354],{"class":178},[100,15399,215],{"class":118},[77,15401,6244],{"id":6243},[14,15403,15404],{},"Use these steps to solve the error:",[40,15406,15407,15410,15415,15424,15430,15443],{},[43,15408,15409],{},"Print the variable and its type before the failing line.",[43,15411,241,15412,15414],{},[17,15413,5283],{}," to confirm what the object really is.",[43,15416,15417,15418,3178,15421,15423],{},"If you need a list, convert with ",[17,15419,15420],{},"list(text)",[17,15422,8090],{}," depending on your goal.",[43,15425,15426,15427,15429],{},"If you need a dictionary, parse the string first, for example with ",[17,15428,15267],{}," when the text contains JSON.",[43,15431,15432,15433,5870,15435,5870,15437,5870,15439,5894,15441,290],{},"If you only need string operations, use valid string methods like ",[17,15434,7394],{},[17,15436,8734],{},[17,15438,8090],{},[17,15440,8744],{},[17,15442,8739],{},[43,15444,15445,15446,290],{},"Check earlier lines to see where the variable changed from another type to ",[17,15447,3422],{},[14,15449,1844],{},[91,15451,15453],{"className":93,"code":15452,"language":95,"meta":96,"style":96},"value = \"a,b,c\"\n\nprint(value)\nprint(type(value))\n\nitems = value.split(\",\")\nprint(items)\nprint(type(items))\n",[17,15454,15455,15468,15472,15482,15496,15500,15522,15532],{"__ignoreMap":96},[100,15456,15457,15459,15461,15463,15466],{"class":102,"line":103},[100,15458,3318],{"class":106},[100,15460,111],{"class":110},[100,15462,1708],{"class":205},[100,15464,15465],{"class":209},"a,b,c",[100,15467,1714],{"class":205},[100,15469,15470],{"class":102,"line":135},[100,15471,139],{"emptyLinePlaceholder":138},[100,15473,15474,15476,15478,15480],{"class":102,"line":142},[100,15475,372],{"class":114},[100,15477,170],{"class":118},[100,15479,757],{"class":178},[100,15481,215],{"class":118},[100,15483,15484,15486,15488,15490,15492,15494],{"class":102,"line":152},[100,15485,372],{"class":114},[100,15487,170],{"class":118},[100,15489,1250],{"class":191},[100,15491,170],{"class":118},[100,15493,757],{"class":178},[100,15495,182],{"class":118},[100,15497,15498],{"class":102,"line":164},[100,15499,139],{"emptyLinePlaceholder":138},[100,15501,15502,15504,15506,15508,15510,15512,15514,15516,15518,15520],{"class":102,"line":185},[100,15503,3095],{"class":106},[100,15505,111],{"class":110},[100,15507,6747],{"class":106},[100,15509,290],{"class":118},[100,15511,3922],{"class":178},[100,15513,170],{"class":118},[100,15515,206],{"class":205},[100,15517,126],{"class":209},[100,15519,206],{"class":205},[100,15521,215],{"class":118},[100,15523,15524,15526,15528,15530],{"class":102,"line":197},[100,15525,372],{"class":114},[100,15527,170],{"class":118},[100,15529,3146],{"class":178},[100,15531,215],{"class":118},[100,15533,15534,15536,15538,15540,15542,15544],{"class":102,"line":771},[100,15535,372],{"class":114},[100,15537,170],{"class":118},[100,15539,1250],{"class":191},[100,15541,170],{"class":118},[100,15543,3146],{"class":178},[100,15545,182],{"class":118},[14,15547,14742],{},[91,15549,15551],{"className":93,"code":15550,"language":95,"meta":96,"style":96},"a,b,c\n\u003Cclass 'str'>\n['a', 'b', 'c']\n\u003Cclass 'list'>\n",[17,15552,15553,15566,15580,15608],{"__ignoreMap":96},[100,15554,15555,15557,15559,15561,15563],{"class":102,"line":103},[100,15556,295],{"class":106},[100,15558,126],{"class":118},[100,15560,4027],{"class":106},[100,15562,126],{"class":118},[100,15564,15565],{"class":106},"c\n",[100,15567,15568,15570,15572,15574,15576,15578],{"class":102,"line":135},[100,15569,2782],{"class":110},[100,15571,3417],{"class":1077},[100,15573,1274],{"class":205},[100,15575,3422],{"class":209},[100,15577,1280],{"class":205},[100,15579,1980],{"class":110},[100,15581,15582,15584,15586,15588,15590,15592,15594,15596,15598,15600,15602,15604,15606],{"class":102,"line":142},[100,15583,3166],{"class":118},[100,15585,1280],{"class":205},[100,15587,295],{"class":209},[100,15589,1280],{"class":205},[100,15591,126],{"class":118},[100,15593,1274],{"class":205},[100,15595,4027],{"class":209},[100,15597,1280],{"class":205},[100,15599,126],{"class":118},[100,15601,1274],{"class":205},[100,15603,4036],{"class":209},[100,15605,1280],{"class":205},[100,15607,603],{"class":118},[100,15609,15610,15612,15614,15616,15618,15620],{"class":102,"line":152},[100,15611,2782],{"class":110},[100,15613,3417],{"class":1077},[100,15615,1274],{"class":205},[100,15617,1235],{"class":209},[100,15619,1280],{"class":205},[100,15621,1980],{"class":110},[14,15623,15624,15625,290],{},"If your goal is to break text into parts, see ",[295,15626,8960],{"href":8959},[77,15628,8550],{"id":8549},[14,15630,15631],{},"When you see this error, do this in order:",[3282,15633,15634,15637,15643,15649,15652],{},[43,15635,15636],{},"Look at the exact name after the dot in the error message.",[43,15638,15639,15640,290],{},"Check the variable value with ",[17,15641,15642],{},"print(variable)",[43,15644,15645,15646,290],{},"Check the type with ",[17,15647,15648],{},"print(type(variable))",[43,15650,15651],{},"Find where the variable was created or reassigned.",[43,15653,15654],{},"Replace the method with the correct one for strings, or convert the data to the correct type.",[14,15656,5295],{},[91,15658,15660],{"className":93,"code":15659,"language":95,"meta":96,"style":96},"print(value)\nprint(type(value))\nprint(isinstance(value, str))\nprint(dir(value))\nhelp(str)\n",[17,15661,15662,15672,15686,15704,15718],{"__ignoreMap":96},[100,15663,15664,15666,15668,15670],{"class":102,"line":103},[100,15665,372],{"class":114},[100,15667,170],{"class":118},[100,15669,757],{"class":178},[100,15671,215],{"class":118},[100,15673,15674,15676,15678,15680,15682,15684],{"class":102,"line":135},[100,15675,372],{"class":114},[100,15677,170],{"class":118},[100,15679,1250],{"class":191},[100,15681,170],{"class":118},[100,15683,757],{"class":178},[100,15685,182],{"class":118},[100,15687,15688,15690,15692,15694,15696,15698,15700,15702],{"class":102,"line":142},[100,15689,372],{"class":114},[100,15691,170],{"class":118},[100,15693,6994],{"class":114},[100,15695,170],{"class":118},[100,15697,757],{"class":178},[100,15699,126],{"class":118},[100,15701,2871],{"class":191},[100,15703,182],{"class":118},[100,15705,15706,15708,15710,15712,15714,15716],{"class":102,"line":152},[100,15707,372],{"class":114},[100,15709,170],{"class":118},[100,15711,3822],{"class":114},[100,15713,170],{"class":118},[100,15715,757],{"class":178},[100,15717,182],{"class":118},[100,15719,15720,15722,15724,15726],{"class":102,"line":164},[100,15721,2478],{"class":114},[100,15723,170],{"class":118},[100,15725,3422],{"class":191},[100,15727,215],{"class":118},[14,15729,8679],{},[40,15731,15732,15736,15741,15747,15752],{},[43,15733,15734,7047],{},[17,15735,7046],{},[43,15737,15738,15740],{},[17,15739,7052],{}," shows the real type",[43,15742,15743,15746],{},[17,15744,15745],{},"isinstance(value, str)"," checks if it is a string",[43,15748,15749,7071],{},[17,15750,15751],{},"dir(value)",[43,15753,15754,15757],{},[17,15755,15756],{},"help(str)"," shows documentation for strings",[77,15759,15761,15762,14991],{"id":15760},"special-case-decode-on-a-string","Special case: ",[17,15763,15273],{},[14,15765,15766],{},"This specific version is very common in Python 3.",[14,15768,15769],{},"Key point:",[40,15771,15772,15777],{},[43,15773,15774,15776],{},[17,15775,3422],{}," is already decoded text",[43,15778,15779,15781,15782,15784,15785],{},[17,15780,15273],{}," is used on ",[17,15783,15319],{},", not on ",[17,15786,3422],{},[14,15788,9709],{},[91,15790,15791],{"className":93,"code":15277,"language":95,"meta":96,"style":96},[17,15792,15793,15805],{"__ignoreMap":96},[100,15794,15795,15797,15799,15801,15803],{"class":102,"line":103},[100,15796,4331],{"class":106},[100,15798,111],{"class":110},[100,15800,1708],{"class":205},[100,15802,3506],{"class":209},[100,15804,1714],{"class":205},[100,15806,15807,15809,15811,15813,15815,15817,15819,15821],{"class":102,"line":135},[100,15808,4468],{"class":106},[100,15810,290],{"class":118},[100,15812,15300],{"class":178},[100,15814,170],{"class":118},[100,15816,206],{"class":205},[100,15818,15307],{"class":209},[100,15820,206],{"class":205},[100,15822,215],{"class":118},[14,15824,15825],{},"Right:",[91,15827,15829],{"className":93,"code":15828,"language":95,"meta":96,"style":96},"text = \"hello\"\nprint(text)\n",[17,15830,15831,15843],{"__ignoreMap":96},[100,15832,15833,15835,15837,15839,15841],{"class":102,"line":103},[100,15834,4331],{"class":106},[100,15836,111],{"class":110},[100,15838,1708],{"class":205},[100,15840,3506],{"class":209},[100,15842,1714],{"class":205},[100,15844,15845,15847,15849,15851],{"class":102,"line":135},[100,15846,372],{"class":114},[100,15848,170],{"class":118},[100,15850,4468],{"class":178},[100,15852,215],{"class":118},[14,15854,15855],{},"If you actually have bytes, decode them first:",[91,15857,15859],{"className":93,"code":15858,"language":95,"meta":96,"style":96},"data = b\"hello\"\ntext = data.decode(\"utf-8\")\nprint(text)\n",[17,15860,15861,15876,15898],{"__ignoreMap":96},[100,15862,15863,15865,15867,15870,15872,15874],{"class":102,"line":103},[100,15864,7909],{"class":106},[100,15866,111],{"class":110},[100,15868,15869],{"class":1077}," b",[100,15871,206],{"class":205},[100,15873,3506],{"class":209},[100,15875,1714],{"class":205},[100,15877,15878,15880,15882,15884,15886,15888,15890,15892,15894,15896],{"class":102,"line":135},[100,15879,4331],{"class":106},[100,15881,111],{"class":110},[100,15883,12102],{"class":106},[100,15885,290],{"class":118},[100,15887,15300],{"class":178},[100,15889,170],{"class":118},[100,15891,206],{"class":205},[100,15893,15307],{"class":209},[100,15895,206],{"class":205},[100,15897,215],{"class":118},[100,15899,15900,15902,15904,15906],{"class":102,"line":142},[100,15901,372],{"class":114},[100,15903,170],{"class":118},[100,15905,4468],{"class":178},[100,15907,215],{"class":118},[14,15909,14742],{},[91,15911,15912],{"className":93,"code":6651,"language":95,"meta":96,"style":96},[17,15913,15914],{"__ignoreMap":96},[100,15915,15916],{"class":102,"line":103},[100,15917,6651],{"class":106},[77,15919,7117],{"id":7116},[14,15921,15922],{},"These are some of the most common causes of this error:",[40,15924,15925,15932,15939,15945,15948,15951],{},[43,15926,15927,15928,3178,15930,14991],{},"Using list methods like ",[17,15929,7659],{},[17,15931,8134],{},[43,15933,15934,15935,3178,15937,14991],{},"Using dictionary methods like ",[17,15936,7891],{},[17,15938,13068],{},[43,15940,15941,15942,15944],{},"Assuming ",[17,15943,14906],{}," returns numbers or lists instead of text",[43,15946,15947],{},"Reading JSON or CSV as plain text and not parsing it",[43,15949,15950],{},"Reusing the same variable name for different types",[43,15952,1357,15953,13325,15955,15957,15958,15960],{},[17,15954,15273],{},[17,15956,3422],{}," instead of ",[17,15959,15319],{}," in Python 3",[14,15962,15963],{},"If the same problem happens with another type, you may also want to read:",[40,15965,15966,15972,15978],{},[43,15967,15968],{},[295,15969,5728,15970,7375],{"href":4118},[17,15971,4122],{},[43,15973,15974],{},[295,15975,5728,15976,7375],{"href":4967},[17,15977,4970],{},[43,15979,15980],{},[295,15981,5728,15982,7375],{"href":12826},[17,15983,15984],{},"'object' has no attribute",[77,15986,1514],{"id":1513},[675,15988,15990],{"id":15989},"why-does-python-say-a-string-has-no-attribute","Why does Python say a string has no attribute?",[14,15992,15993,15994,15996],{},"Because the method or attribute you used does not belong to the ",[17,15995,3422],{}," type. The variable is text, but your code treats it like another type.",[675,15998,16000],{"id":15999},"how-do-i-know-what-type-my-variable-is","How do I know what type my variable is?",[14,16002,241,16003,16005],{},[17,16004,5283],{},". For example:",[91,16007,16009],{"className":93,"code":16008,"language":95,"meta":96,"style":96},"value = \"hello\"\nprint(type(value))\n",[17,16010,16011,16023],{"__ignoreMap":96},[100,16012,16013,16015,16017,16019,16021],{"class":102,"line":103},[100,16014,3318],{"class":106},[100,16016,111],{"class":110},[100,16018,1708],{"class":205},[100,16020,3506],{"class":209},[100,16022,1714],{"class":205},[100,16024,16025,16027,16029,16031,16033,16035],{"class":102,"line":135},[100,16026,372],{"class":114},[100,16028,170],{"class":118},[100,16030,1250],{"class":191},[100,16032,170],{"class":118},[100,16034,757],{"class":178},[100,16036,182],{"class":118},[14,16038,16039,16040,5870,16042,5870,16044,16047],{},"This shows whether it is ",[17,16041,3422],{},[17,16043,1235],{},[17,16045,16046],{},"dict",", or something else.",[675,16049,9137,16051,16053],{"id":16050},"why-does-input-often-lead-to-this-error",[17,16052,14906],{}," often lead to this error?",[14,16055,14478,16056,16058],{},[17,16057,14906],{}," always returns a string. If you expect a number or another type, you must convert it first.",[675,16060,16062,16063,16065],{"id":16061},"can-i-use-append-on-a-string","Can I use ",[17,16064,7659],{}," on a string?",[14,16067,16068,16069,16072],{},"No. Strings are immutable. Use concatenation, ",[17,16070,16071],{},"join()",", or convert the string to a list if you need list behavior.",[675,16074,9137,16076,16078],{"id":16075},"why-does-decode-fail-on-a-string-in-python-3",[17,16077,15273],{}," fail on a string in Python 3?",[14,16080,14478,16081,15316,16083,16085],{},[17,16082,15273],{},[17,16084,15319],{},". A Python 3 string is already text, so it does not need decoding.",[77,16087,1554],{"id":1553},[40,16089,16090,16094,16100,16105,16110],{},[43,16091,16092],{},[295,16093,6080],{"href":14879},[43,16095,16096],{},[295,16097,1572,16098,1576],{"href":4427},[17,16099,4430],{},[43,16101,16102],{},[295,16103,16104],{"href":8959},"How to split a string in Python",[43,16106,16107],{},[295,16108,16109],{"href":14982},"How to convert string to int in Python",[43,16111,16112],{},[295,16113,16114,1576],{"href":15264},[17,16115,15267],{},[1589,16117,16118],{},"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 .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 .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 pre.shiki code .sZMiF, html code.shiki .sZMiF{--shiki-light:#E2931D;--shiki-default:#005CC5;--shiki-dark:#79B8FF}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 .sbsja, html code.shiki .sbsja{--shiki-light:#9C3EDA;--shiki-default:#D73A49;--shiki-dark:#F97583}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 .skxfh, html code.shiki .skxfh{--shiki-light:#E53935;--shiki-default:#24292E;--shiki-dark:#E1E4E8}",{"title":96,"searchDepth":135,"depth":135,"links":16120},[16121,16122,16123,16124,16135,16136,16137,16139,16140,16150],{"id":79,"depth":135,"text":80},{"id":5880,"depth":135,"text":5881},{"id":7740,"depth":135,"text":7741},{"id":7771,"depth":135,"text":7772,"children":16125},[16126,16128,16130,16132,16134],{"id":14988,"depth":142,"text":16127},"Calling append() on a string",{"id":15081,"depth":142,"text":16129},"Calling keys() on a string",{"id":15125,"depth":142,"text":16131},"Using get() on a string instead of a dictionary",{"id":15270,"depth":142,"text":16133},"Calling decode() on a string in Python 3",{"id":15325,"depth":142,"text":15326},{"id":6243,"depth":135,"text":6244},{"id":8549,"depth":135,"text":8550},{"id":15760,"depth":135,"text":16138},"Special case: decode() on a string",{"id":7116,"depth":135,"text":7117},{"id":1513,"depth":135,"text":1514,"children":16141},[16142,16143,16144,16146,16148],{"id":15989,"depth":142,"text":15990},{"id":15999,"depth":142,"text":16000},{"id":16050,"depth":142,"text":16145},"Why does input() often lead to this error?",{"id":16061,"depth":142,"text":16147},"Can I use append() on a string?",{"id":16075,"depth":142,"text":16149},"Why does decode() fail on a string in Python 3?",{"id":1553,"depth":135,"text":1554},"Master attributeerror str object has no attribute fix in our comprehensive Python beginner guide.",{},{"title":14593,"description":16151},"errors\u002Fattributeerror-str-object-has-no-attribute-fix","Vwszesyo5GNlWQlaNjXkRjN_yUwO5rkglt5JHCq4iys",{"id":16157,"title":16158,"body":16159,"description":18002,"extension":1623,"meta":18003,"navigation":138,"path":18004,"seo":18005,"stem":18006,"__hash__":18007},"content\u002Ferrors\u002Fcommon-python-errors-explained-beginner-guide.md","Common Python Errors Explained (Beginner Guide)",{"type":7,"value":16160,"toc":17978},[16161,16164,16167,16170,16184,16187,16191,16194,16259,16262,16266,16269,16283,16288,16292,16295,16317,16319,16359,16361,16431,16434,16453,16459,16463,16466,16468,16491,16494,16497,16509,16511,16537,16539,16553,16555,16580,16582,16602,16605,16619,16623,16626,16628,16639,16641,16652,16654,16679,16681,16708,16711,16722,16729,16733,16736,16738,16749,16752,16758,16760,16785,16787,16821,16823,16858,16860,16886,16889,16902,16905,16909,16912,16914,16927,16929,16935,16937,16968,16970,17006,17018,17025,17031,17035,17038,17040,17051,17053,17064,17067,17118,17120,17137,17140,17188,17190,17207,17210,17222,17225,17245,17249,17252,17254,17267,17269,17275,17277,17323,17325,17351,17356,17361,17365,17368,17370,17384,17386,17398,17400,17412,17414,17435,17437,17451,17458,17462,17465,17467,17481,17483,17510,17512,17569,17571,17612,17615,17622,17626,17629,17631,17645,17647,17669,17671,17691,17693,17707,17710,17714,17717,17739,17741,17823,17825,17859,17863,17866,17893,17896,17898,17902,17913,17917,17920,17924,17932,17936,17939,17943,17946,17948,17975],[10,16162,16158],{"id":16163},"common-python-errors-explained-beginner-guide",[14,16165,16166],{},"Python error messages can look confusing at first. This page gives you a simple map of the errors beginners see most often.",[14,16168,16169],{},"You will learn:",[40,16171,16172,16175,16178,16181],{},[43,16173,16174],{},"what common error types usually mean",[43,16176,16177],{},"how to read a traceback",[43,16179,16180],{},"how to tell syntax, type, value, name, index, and file errors apart",[43,16182,16183],{},"which specific error page to open next",[14,16185,16186],{},"This is an overview page. It is meant to help you identify the error category quickly, not replace detailed fix guides for each exact error.",[77,16188,16190],{"id":16189},"quick-way-to-identify-the-error","Quick way to identify the error",[14,16192,16193],{},"If you are not sure what error your code is raising, use this pattern to print the exact error type and message:",[91,16195,16197],{"className":93,"code":16196,"language":95,"meta":96,"style":96},"try:\n    # your code here\n    pass\nexcept Exception as e:\n    print(type(e).__name__)\n    print(e)\n",[17,16198,16199,16205,16210,16215,16230,16249],{"__ignoreMap":96},[100,16200,16201,16203],{"class":102,"line":103},[100,16202,146],{"class":145},[100,16204,149],{"class":118},[100,16206,16207],{"class":102,"line":135},[100,16208,16209],{"class":414},"    # your code here\n",[100,16211,16212],{"class":102,"line":142},[100,16213,16214],{"class":145},"    pass\n",[100,16216,16217,16219,16222,16225,16228],{"class":102,"line":152},[100,16218,188],{"class":145},[100,16220,16221],{"class":191}," Exception",[100,16223,16224],{"class":145}," as",[100,16226,16227],{"class":106}," e",[100,16229,149],{"class":118},[100,16231,16232,16234,16236,16238,16240,16242,16244,16247],{"class":102,"line":164},[100,16233,200],{"class":114},[100,16235,170],{"class":118},[100,16237,1250],{"class":191},[100,16239,170],{"class":118},[100,16241,14770],{"class":178},[100,16243,5799],{"class":118},[100,16245,16246],{"class":527},"__name__",[100,16248,215],{"class":118},[100,16250,16251,16253,16255,16257],{"class":102,"line":185},[100,16252,200],{"class":114},[100,16254,170],{"class":118},[100,16256,14770],{"class":178},[100,16258,215],{"class":118},[14,16260,16261],{},"Use this to see the exact error type and message before trying to fix it.",[77,16263,16265],{"id":16264},"what-this-page-is-for","What this page is for",[14,16267,16268],{},"This guide helps beginners:",[40,16270,16271,16274,16277,16280],{},[43,16272,16273],{},"get a simple overview of common Python errors",[43,16275,16276],{},"understand the difference between major error categories",[43,16278,16279],{},"move quickly to the right detailed fix page",[43,16281,16282],{},"avoid getting stuck in deep debugging too early",[14,16284,16285,16286,290],{},"If you want a broader introduction to exceptions, see ",[295,16287,1587],{"href":1749},[77,16289,16291],{"id":16290},"how-to-read-a-python-error","How to read a Python error",[14,16293,16294],{},"When Python shows an error, do this first:",[40,16296,16297,16304,16311,16314],{},[43,16298,16299,16300,16303],{},"Look at the ",[22,16301,16302],{},"last line"," first. It shows the error type and message.",[43,16305,16306,16307,16310],{},"Read the ",[22,16308,16309],{},"line number"," in the traceback.",[43,16312,16313],{},"Check the code on that line and the lines just above it.",[43,16315,16316],{},"Do not guess from the message alone. Confirm which variable or value caused it.",[14,16318,580],{},[91,16320,16322],{"className":93,"code":16321,"language":95,"meta":96,"style":96},"numbers = [10, 20, 30]\nprint(numbers[5])\n",[17,16323,16324,16345],{"__ignoreMap":96},[100,16325,16326,16328,16330,16332,16334,16336,16338,16340,16343],{"class":102,"line":103},[100,16327,107],{"class":106},[100,16329,111],{"class":110},[100,16331,594],{"class":118},[100,16333,356],{"class":122},[100,16335,126],{"class":118},[100,16337,361],{"class":122},[100,16339,126],{"class":118},[100,16341,16342],{"class":122}," 30",[100,16344,603],{"class":118},[100,16346,16347,16349,16351,16353,16355,16357],{"class":102,"line":135},[100,16348,372],{"class":114},[100,16350,170],{"class":118},[100,16352,179],{"class":178},[100,16354,3166],{"class":118},[100,16356,3557],{"class":122},[100,16358,132],{"class":118},[14,16360,218],{},[91,16362,16364],{"className":93,"code":16363,"language":95,"meta":96,"style":96},"Traceback (most recent call last):\n  File \"example.py\", line 2, in \u003Cmodule>\n    print(numbers[5])\nIndexError: list index out of range\n",[17,16365,16366,16376,16402,16416],{"__ignoreMap":96},[100,16367,16368,16370,16372,16374],{"class":102,"line":103},[100,16369,514],{"class":178},[100,16371,170],{"class":118},[100,16373,519],{"class":178},[100,16375,522],{"class":118},[100,16377,16378,16380,16382,16384,16386,16388,16390,16392,16394,16396,16398,16400],{"class":102,"line":135},[100,16379,1952],{"class":106},[100,16381,206],{"class":205},[100,16383,1957],{"class":209},[100,16385,206],{"class":205},[100,16387,126],{"class":118},[100,16389,1964],{"class":106},[100,16391,559],{"class":122},[100,16393,126],{"class":118},[100,16395,1971],{"class":110},[100,16397,1974],{"class":110},[100,16399,1977],{"class":106},[100,16401,1980],{"class":110},[100,16403,16404,16406,16408,16410,16412,16414],{"class":102,"line":142},[100,16405,200],{"class":114},[100,16407,170],{"class":118},[100,16409,179],{"class":178},[100,16411,3166],{"class":118},[100,16413,3557],{"class":122},[100,16415,132],{"class":118},[100,16417,16418,16421,16423,16425,16428],{"class":102,"line":152},[100,16419,16420],{"class":191},"IndexError",[100,16422,89],{"class":118},[100,16424,14704],{"class":191},[100,16426,16427],{"class":106}," index out of ",[100,16429,16430],{"class":114},"range\n",[14,16432,16433],{},"How to read it:",[40,16435,16436,16441,16447],{},[43,16437,16438,16440],{},[17,16439,16420],{}," is the error type",[43,16442,16443,16446],{},[17,16444,16445],{},"list index out of range"," is the message",[43,16448,16449,16452],{},[17,16450,16451],{},"line 2"," is where Python failed",[14,16454,16455,16456,16458],{},"If you are new to this process, a full ",[295,16457,7077],{"href":3852}," can help.",[77,16460,16462],{"id":16461},"syntax-errors","Syntax errors",[14,16464,16465],{},"Syntax errors happen when Python cannot understand the structure of your code.",[14,16467,14127],{},[40,16469,16470,16475,16478,16481],{},[43,16471,16472,16473],{},"missing ",[17,16474,89],{},[43,16476,16477],{},"missing quote",[43,16479,16480],{},"wrong indentation",[43,16482,16483,16484,5870,16486,5894,16488],{},"unmatched ",[17,16485,170],{},[17,16487,3166],{},[17,16489,16490],{},"{",[14,16492,16493],{},"These errors usually appear before your program fully runs.",[14,16495,16496],{},"Typical examples:",[40,16498,16499,16504],{},[43,16500,16501],{},[17,16502,16503],{},"SyntaxError",[43,16505,16506],{},[17,16507,16508],{},"IndentationError",[14,16510,1844],{},[91,16512,16514],{"className":93,"code":16513,"language":95,"meta":96,"style":96},"name = \"Sam\nprint(name)\n",[17,16515,16516,16527],{"__ignoreMap":96},[100,16517,16518,16520,16522,16524],{"class":102,"line":103},[100,16519,1691],{"class":106},[100,16521,111],{"class":110},[100,16523,1708],{"class":205},[100,16525,16526],{"class":209},"Sam\n",[100,16528,16529,16531,16533,16535],{"class":102,"line":135},[100,16530,372],{"class":114},[100,16532,170],{"class":118},[100,16534,2853],{"class":178},[100,16536,215],{"class":118},[14,16538,218],{},[91,16540,16542],{"className":93,"code":16541,"language":95,"meta":96,"style":96},"SyntaxError: unterminated string literal\n",[17,16543,16544],{"__ignoreMap":96},[100,16545,16546,16548,16550],{"class":102,"line":103},[100,16547,16503],{"class":191},[100,16549,89],{"class":118},[100,16551,16552],{"class":106}," unterminated string literal\n",[14,16554,12066],{},[91,16556,16558],{"className":93,"code":16557,"language":95,"meta":96,"style":96},"if True\n    print(\"Hello\")\n",[17,16559,16560,16566],{"__ignoreMap":96},[100,16561,16562,16564],{"class":102,"line":103},[100,16563,2736],{"class":145},[100,16565,3588],{"class":158},[100,16567,16568,16570,16572,16574,16576,16578],{"class":102,"line":135},[100,16569,200],{"class":114},[100,16571,170],{"class":118},[100,16573,206],{"class":205},[100,16575,7683],{"class":209},[100,16577,206],{"class":205},[100,16579,215],{"class":118},[14,16581,218],{},[91,16583,16585],{"className":93,"code":16584,"language":95,"meta":96,"style":96},"SyntaxError: expected ':'\n",[17,16586,16587],{"__ignoreMap":96},[100,16588,16589,16591,16593,16596,16598,16600],{"class":102,"line":103},[100,16590,16503],{"class":191},[100,16592,89],{"class":118},[100,16594,16595],{"class":106}," expected ",[100,16597,1280],{"class":205},[100,16599,89],{"class":209},[100,16601,3925],{"class":205},[14,16603,16604],{},"If your problem is about code structure, start with:",[40,16606,16607,16613],{},[43,16608,16609],{},[295,16610,16612],{"href":16611},"\u002Ferrors\u002Fsyntaxerror-invalid-syntax-fix\u002F","SyntaxError: invalid syntax",[43,16614,16615],{},[295,16616,16618],{"href":16617},"\u002Ferrors\u002Findentationerror-unexpected-indent-fix\u002F","IndentationError: unexpected indent",[77,16620,16622],{"id":16621},"name-and-variable-errors","Name and variable errors",[14,16624,16625],{},"These happen when Python cannot find a variable, or when a variable is used in the wrong scope.",[14,16627,14127],{},[40,16629,16630,16633,16636],{},[43,16631,16632],{},"misspelled variable names",[43,16634,16635],{},"using a variable before assigning it",[43,16637,16638],{},"scope problems inside functions",[14,16640,16496],{},[40,16642,16643,16647],{},[43,16644,16645],{},[17,16646,5616],{},[43,16648,16649],{},[17,16650,16651],{},"UnboundLocalError",[14,16653,1844],{},[91,16655,16657],{"className":93,"code":16656,"language":95,"meta":96,"style":96},"age = 25\nprint(ag)\n",[17,16658,16659,16668],{"__ignoreMap":96},[100,16660,16661,16663,16665],{"class":102,"line":103},[100,16662,1664],{"class":106},[100,16664,111],{"class":110},[100,16666,16667],{"class":122}," 25\n",[100,16669,16670,16672,16674,16677],{"class":102,"line":135},[100,16671,372],{"class":114},[100,16673,170],{"class":118},[100,16675,16676],{"class":178},"ag",[100,16678,215],{"class":118},[14,16680,218],{},[91,16682,16684],{"className":93,"code":16683,"language":95,"meta":96,"style":96},"NameError: name 'ag' is not defined\n",[17,16685,16686],{"__ignoreMap":96},[100,16687,16688,16690,16692,16694,16696,16698,16700,16703,16705],{"class":102,"line":103},[100,16689,5616],{"class":191},[100,16691,89],{"class":118},[100,16693,4915],{"class":106},[100,16695,1280],{"class":205},[100,16697,16676],{"class":209},[100,16699,1280],{"class":205},[100,16701,16702],{"class":110}," is",[100,16704,4921],{"class":110},[100,16706,16707],{"class":106}," defined\n",[14,16709,16710],{},"This often means:",[40,16712,16713,16716,16719],{},[43,16714,16715],{},"the name is misspelled",[43,16717,16718],{},"the variable was never created",[43,16720,16721],{},"the variable is not available where you are using it",[14,16723,16724,16725,290],{},"For a step-by-step fix, see ",[295,16726,16728],{"href":16727},"\u002Ferrors\u002Fnameerror-name-is-not-defined-fix\u002F","NameError: name is not defined",[77,16730,16732],{"id":16731},"type-errors","Type errors",[14,16734,16735],{},"Type errors happen when you use an operation with the wrong kind of value.",[14,16737,14127],{},[40,16739,16740,16743,16746],{},[43,16741,16742],{},"adding a string and an integer",[43,16744,16745],{},"calling a list like a function",[43,16747,16748],{},"using a string where Python expects an integer",[14,16750,16751],{},"Typical example:",[40,16753,16754],{},[43,16755,16756],{},[17,16757,3538],{},[14,16759,1844],{},[91,16761,16763],{"className":93,"code":16762,"language":95,"meta":96,"style":96},"print(\"Age: \" + 25)\n",[17,16764,16765],{"__ignoreMap":96},[100,16766,16767,16769,16771,16773,16776,16778,16780,16783],{"class":102,"line":103},[100,16768,372],{"class":114},[100,16770,170],{"class":118},[100,16772,206],{"class":205},[100,16774,16775],{"class":209},"Age: ",[100,16777,206],{"class":205},[100,16779,2895],{"class":110},[100,16781,16782],{"class":122}," 25",[100,16784,215],{"class":118},[14,16786,218],{},[91,16788,16790],{"className":93,"code":16789,"language":95,"meta":96,"style":96},"TypeError: can only concatenate str (not \"int\") to str\n",[17,16791,16792],{"__ignoreMap":96},[100,16793,16794,16796,16798,16801,16803,16806,16808,16810,16812,16814,16816,16818],{"class":102,"line":103},[100,16795,3538],{"class":191},[100,16797,89],{"class":118},[100,16799,16800],{"class":106}," can only concatenate ",[100,16802,3422],{"class":191},[100,16804,16805],{"class":118}," (",[100,16807,1059],{"class":145},[100,16809,1708],{"class":205},[100,16811,3499],{"class":209},[100,16813,206],{"class":205},[100,16815,6155],{"class":118},[100,16817,9932],{"class":106},[100,16819,16820],{"class":191},"str\n",[14,16822,12066],{},[91,16824,16826],{"className":93,"code":16825,"language":95,"meta":96,"style":96},"items = [1, 2, 3]\nitems(0)\n",[17,16827,16828,16848],{"__ignoreMap":96},[100,16829,16830,16832,16834,16836,16838,16840,16842,16844,16846],{"class":102,"line":103},[100,16831,3095],{"class":106},[100,16833,111],{"class":110},[100,16835,594],{"class":118},[100,16837,123],{"class":122},[100,16839,126],{"class":118},[100,16841,129],{"class":122},[100,16843,126],{"class":118},[100,16845,1405],{"class":122},[100,16847,603],{"class":118},[100,16849,16850,16852,16854,16856],{"class":102,"line":135},[100,16851,3146],{"class":178},[100,16853,170],{"class":118},[100,16855,2279],{"class":122},[100,16857,215],{"class":118},[14,16859,218],{},[91,16861,16863],{"className":93,"code":16862,"language":95,"meta":96,"style":96},"TypeError: 'list' object is not callable\n",[17,16864,16865],{"__ignoreMap":96},[100,16866,16867,16869,16871,16873,16875,16877,16879,16881,16883],{"class":102,"line":103},[100,16868,3538],{"class":191},[100,16870,89],{"class":118},[100,16872,1274],{"class":205},[100,16874,1235],{"class":209},[100,16876,1280],{"class":205},[100,16878,3914],{"class":191},[100,16880,16702],{"class":110},[100,16882,4921],{"class":110},[100,16884,16885],{"class":114}," callable\n",[14,16887,16888],{},"Type errors are often fixed by:",[40,16890,16891,16896,16899],{},[43,16892,16893,16894],{},"checking the value with ",[17,16895,4430],{},[43,16897,16898],{},"converting values when needed",[43,16900,16901],{},"making sure you are using the right object",[14,16903,16904],{},"A common beginner case is covered in TypeError: unsupported operand types for +.",[77,16906,16908],{"id":16907},"value-errors","Value errors",[14,16910,16911],{},"Value errors happen when the type is acceptable, but the actual value is not.",[14,16913,14127],{},[40,16915,16916,16921,16924],{},[43,16917,16918,16919],{},"converting non-numeric text with ",[17,16920,6119],{},[43,16922,16923],{},"unpacking the wrong number of values",[43,16925,16926],{},"invalid input format",[14,16928,16751],{},[40,16930,16931],{},[43,16932,16933],{},[17,16934,3483],{},[14,16936,1844],{},[91,16938,16940],{"className":93,"code":16939,"language":95,"meta":96,"style":96},"text = \"hello\"\nnumber = int(text)\n",[17,16941,16942,16954],{"__ignoreMap":96},[100,16943,16944,16946,16948,16950,16952],{"class":102,"line":103},[100,16945,4331],{"class":106},[100,16947,111],{"class":110},[100,16949,1708],{"class":205},[100,16951,3506],{"class":209},[100,16953,1714],{"class":205},[100,16955,16956,16958,16960,16962,16964,16966],{"class":102,"line":135},[100,16957,1901],{"class":106},[100,16959,111],{"class":110},[100,16961,3389],{"class":191},[100,16963,170],{"class":118},[100,16965,4468],{"class":178},[100,16967,215],{"class":118},[14,16969,218],{},[91,16971,16973],{"className":93,"code":16972,"language":95,"meta":96,"style":96},"ValueError: invalid literal for int() with base 10: 'hello'\n",[17,16974,16975],{"__ignoreMap":96},[100,16976,16977,16979,16981,16984,16986,16988,16990,16993,16996,16998,17000,17002,17004],{"class":102,"line":103},[100,16978,3483],{"class":191},[100,16980,89],{"class":118},[100,16982,16983],{"class":106}," invalid literal ",[100,16985,71],{"class":145},[100,16987,3389],{"class":191},[100,16989,7562],{"class":118},[100,16991,16992],{"class":145}," with",[100,16994,16995],{"class":106}," base ",[100,16997,356],{"class":122},[100,16999,89],{"class":118},[100,17001,1274],{"class":205},[100,17003,3506],{"class":209},[100,17005,3925],{"class":205},[14,17007,17008,17009,17011,17012,17014,17015,17017],{},"Here, ",[17,17010,4468],{}," is a string, and ",[17,17013,6119],{}," can accept strings. The problem is that ",[17,17016,3519],{}," is not a valid number.",[14,17019,17020,17021,290],{},"For this specific case, see ",[295,17022,17024],{"href":17023},"\u002Ferrors\u002Fvalueerror-invalid-literal-for-int-with-base-10-fix\u002F","ValueError: invalid literal for int() with base 10",[14,17026,17027,17028,290],{},"If you are unsure whether you have a type problem or a value problem, read ",[295,17029,17030],{"href":3269},"TypeError vs ValueError in Python",[77,17032,17034],{"id":17033},"index-and-key-errors","Index and key errors",[14,17036,17037],{},"These happen when you try to access data that does not exist.",[14,17039,14127],{},[40,17041,17042,17045,17048],{},[43,17043,17044],{},"using a list index that is too large",[43,17046,17047],{},"using a tuple index that is too large",[43,17049,17050],{},"asking for a dictionary key that is missing",[14,17052,16496],{},[40,17054,17055,17059],{},[43,17056,17057],{},[17,17058,16420],{},[43,17060,17061],{},[17,17062,17063],{},"KeyError",[14,17065,17066],{},"List example:",[91,17068,17070],{"className":93,"code":17069,"language":95,"meta":96,"style":96},"letters = [\"a\", \"b\", \"c\"]\nprint(letters[3])\n",[17,17071,17072,17104],{"__ignoreMap":96},[100,17073,17074,17076,17078,17080,17082,17084,17086,17088,17090,17092,17094,17096,17098,17100,17102],{"class":102,"line":103},[100,17075,7960],{"class":106},[100,17077,111],{"class":110},[100,17079,594],{"class":118},[100,17081,206],{"class":205},[100,17083,295],{"class":209},[100,17085,206],{"class":205},[100,17087,126],{"class":118},[100,17089,1708],{"class":205},[100,17091,4027],{"class":209},[100,17093,206],{"class":205},[100,17095,126],{"class":118},[100,17097,1708],{"class":205},[100,17099,4036],{"class":209},[100,17101,206],{"class":205},[100,17103,603],{"class":118},[100,17105,17106,17108,17110,17112,17114,17116],{"class":102,"line":135},[100,17107,372],{"class":114},[100,17109,170],{"class":118},[100,17111,7997],{"class":178},[100,17113,3166],{"class":118},[100,17115,11541],{"class":122},[100,17117,132],{"class":118},[14,17119,218],{},[91,17121,17123],{"className":93,"code":17122,"language":95,"meta":96,"style":96},"IndexError: list index out of range\n",[17,17124,17125],{"__ignoreMap":96},[100,17126,17127,17129,17131,17133,17135],{"class":102,"line":103},[100,17128,16420],{"class":191},[100,17130,89],{"class":118},[100,17132,14704],{"class":191},[100,17134,16427],{"class":106},[100,17136,16430],{"class":114},[14,17138,17139],{},"Dictionary example:",[91,17141,17143],{"className":93,"code":17142,"language":95,"meta":96,"style":96},"user = {\"name\": \"Mia\"}\nprint(user[\"age\"])\n",[17,17144,17145,17170],{"__ignoreMap":96},[100,17146,17147,17149,17151,17153,17155,17157,17159,17161,17163,17166,17168],{"class":102,"line":103},[100,17148,15336],{"class":106},[100,17150,111],{"class":110},[100,17152,12080],{"class":118},[100,17154,206],{"class":205},[100,17156,2853],{"class":209},[100,17158,206],{"class":205},[100,17160,89],{"class":118},[100,17162,1708],{"class":205},[100,17164,17165],{"class":209},"Mia",[100,17167,206],{"class":205},[100,17169,12093],{"class":118},[100,17171,17172,17174,17176,17178,17180,17182,17184,17186],{"class":102,"line":135},[100,17173,372],{"class":114},[100,17175,170],{"class":118},[100,17177,15354],{"class":178},[100,17179,3166],{"class":118},[100,17181,206],{"class":205},[100,17183,2770],{"class":209},[100,17185,206],{"class":205},[100,17187,132],{"class":118},[14,17189,218],{},[91,17191,17193],{"className":93,"code":17192,"language":95,"meta":96,"style":96},"KeyError: 'age'\n",[17,17194,17195],{"__ignoreMap":96},[100,17196,17197,17199,17201,17203,17205],{"class":102,"line":103},[100,17198,17063],{"class":191},[100,17200,89],{"class":118},[100,17202,1274],{"class":205},[100,17204,2770],{"class":209},[100,17206,3925],{"class":205},[14,17208,17209],{},"Use this rule:",[40,17211,17212,17217],{},[43,17213,17214,17216],{},[17,17215,16420],{}," is usually for lists and tuples",[43,17218,17219,17221],{},[17,17220,17063],{}," is usually for dictionaries",[14,17223,17224],{},"Detailed pages:",[40,17226,17227,17233,17239],{},[43,17228,17229],{},[295,17230,17232],{"href":17231},"\u002Ferrors\u002Findexerror-list-index-out-of-range-fix-explained\u002F","IndexError: list index out of range",[43,17234,17235],{},[295,17236,17238],{"href":17237},"\u002Ferrors\u002Fkeyerror-in-python-causes-and-fixes\u002F","KeyError in Python: causes and fixes",[43,17240,17241],{},[295,17242,17244],{"href":17243},"\u002Ferrors\u002Fkeyerror-vs-indexerror-in-python-explained\u002F","KeyError vs IndexError in Python",[77,17246,17248],{"id":17247},"attribute-errors","Attribute errors",[14,17250,17251],{},"Attribute errors happen when you try to use a method or attribute that an object does not have.",[14,17253,14127],{},[40,17255,17256,17259,17264],{},[43,17257,17258],{},"calling string methods on a list",[43,17260,17261,17262],{},"using methods on ",[17,17263,930],{},[43,17265,17266],{},"misspelling a method name",[14,17268,16751],{},[40,17270,17271],{},[43,17272,17273],{},[17,17274,3738],{},[14,17276,1844],{},[91,17278,17279],{"className":93,"code":13873,"language":95,"meta":96,"style":96},[17,17280,17281,17313],{"__ignoreMap":96},[100,17282,17283,17285,17287,17289,17291,17293,17295,17297,17299,17301,17303,17305,17307,17309,17311],{"class":102,"line":103},[100,17284,3095],{"class":106},[100,17286,111],{"class":110},[100,17288,594],{"class":118},[100,17290,206],{"class":205},[100,17292,295],{"class":209},[100,17294,206],{"class":205},[100,17296,126],{"class":118},[100,17298,1708],{"class":205},[100,17300,4027],{"class":209},[100,17302,206],{"class":205},[100,17304,126],{"class":118},[100,17306,1708],{"class":205},[100,17308,4036],{"class":209},[100,17310,206],{"class":205},[100,17312,603],{"class":118},[100,17314,17315,17317,17319,17321],{"class":102,"line":135},[100,17316,3146],{"class":106},[100,17318,290],{"class":118},[100,17320,4540],{"class":178},[100,17322,4734],{"class":118},[14,17324,218],{},[91,17326,17327],{"className":93,"code":13172,"language":95,"meta":96,"style":96},[17,17328,17329],{"__ignoreMap":96},[100,17330,17331,17333,17335,17337,17339,17341,17343,17345,17347,17349],{"class":102,"line":103},[100,17332,3738],{"class":191},[100,17334,89],{"class":118},[100,17336,1274],{"class":205},[100,17338,1235],{"class":209},[100,17340,1280],{"class":205},[100,17342,3914],{"class":191},[100,17344,3917],{"class":106},[100,17346,1280],{"class":205},[100,17348,4540],{"class":209},[100,17350,3925],{"class":205},[14,17352,17353,17354,13319],{},"In this example, ",[17,17355,4510],{},[14,17357,17358,17359,290],{},"This kind of error is explained in ",[295,17360,7095],{"href":12826},[77,17362,17364],{"id":17363},"import-and-module-errors","Import and module errors",[14,17366,17367],{},"These happen when Python cannot find a module, or cannot import something from it.",[14,17369,14127],{},[40,17371,17372,17375,17378,17381],{},[43,17373,17374],{},"a package is not installed",[43,17376,17377],{},"the module name is wrong",[43,17379,17380],{},"the file structure is wrong",[43,17382,17383],{},"a local file has the same name as a real module",[14,17385,16496],{},[40,17387,17388,17393],{},[43,17389,17390],{},[17,17391,17392],{},"ImportError",[43,17394,17395],{},[17,17396,17397],{},"ModuleNotFoundError",[14,17399,1844],{},[91,17401,17403],{"className":93,"code":17402,"language":95,"meta":96,"style":96},"import request\n",[17,17404,17405],{"__ignoreMap":96},[100,17406,17407,17409],{"class":102,"line":103},[100,17408,9259],{"class":145},[100,17410,17411],{"class":106}," request\n",[14,17413,218],{},[91,17415,17417],{"className":93,"code":17416,"language":95,"meta":96,"style":96},"ModuleNotFoundError: No module named 'request'\n",[17,17418,17419],{"__ignoreMap":96},[100,17420,17421,17423,17425,17428,17430,17433],{"class":102,"line":103},[100,17422,17397],{"class":191},[100,17424,89],{"class":118},[100,17426,17427],{"class":106}," No module named ",[100,17429,1280],{"class":205},[100,17431,17432],{"class":209},"request",[100,17434,3925],{"class":205},[14,17436,16710],{},[40,17438,17439,17442,17445],{},[43,17440,17441],{},"the module name is misspelled",[43,17443,17444],{},"the package is not installed",[43,17446,17447,17448],{},"you meant a different module, such as ",[17,17449,17450],{},"requests",[14,17452,17453,17454,290],{},"For help with this category, see ",[295,17455,17457],{"href":17456},"\u002Ferrors\u002Fmodulenotfounderror-no-module-named-x-fix\u002F","ModuleNotFoundError: No module named x",[77,17459,17461],{"id":17460},"file-and-operating-system-errors","File and operating system errors",[14,17463,17464],{},"These happen when your code works with files or folders and the path or permissions are wrong.",[14,17466,14127],{},[40,17468,17469,17472,17475,17478],{},[43,17470,17471],{},"wrong file path",[43,17473,17474],{},"file does not exist",[43,17476,17477],{},"no permission",[43,17479,17480],{},"using a directory path as if it were a file",[14,17482,16496],{},[40,17484,17485,17490,17495,17500,17505],{},[43,17486,17487],{},[17,17488,17489],{},"FileNotFoundError",[43,17491,17492],{},[17,17493,17494],{},"PermissionError",[43,17496,17497],{},[17,17498,17499],{},"IsADirectoryError",[43,17501,17502],{},[17,17503,17504],{},"NotADirectoryError",[43,17506,17507],{},[17,17508,17509],{},"OSError",[14,17511,1844],{},[91,17513,17515],{"className":93,"code":17514,"language":95,"meta":96,"style":96},"with open(\"missing_file.txt\", \"r\") as file:\n    data = file.read()\n",[17,17516,17517,17553],{"__ignoreMap":96},[100,17518,17519,17522,17525,17527,17529,17532,17534,17536,17538,17541,17543,17545,17547,17551],{"class":102,"line":103},[100,17520,17521],{"class":145},"with",[100,17523,17524],{"class":114}," open",[100,17526,170],{"class":118},[100,17528,206],{"class":205},[100,17530,17531],{"class":209},"missing_file.txt",[100,17533,206],{"class":205},[100,17535,126],{"class":118},[100,17537,1708],{"class":205},[100,17539,17540],{"class":209},"r",[100,17542,206],{"class":205},[100,17544,6155],{"class":118},[100,17546,16224],{"class":145},[100,17548,17550],{"class":17549},"sMMDD"," file",[100,17552,149],{"class":118},[100,17554,17555,17558,17560,17562,17564,17567],{"class":102,"line":135},[100,17556,17557],{"class":106},"    data ",[100,17559,111],{"class":110},[100,17561,17550],{"class":17549},[100,17563,290],{"class":118},[100,17565,17566],{"class":178},"read",[100,17568,4734],{"class":118},[14,17570,218],{},[91,17572,17574],{"className":93,"code":17573,"language":95,"meta":96,"style":96},"FileNotFoundError: [Errno 2] No such file or directory: 'missing_file.txt'\n",[17,17575,17576],{"__ignoreMap":96},[100,17577,17578,17580,17582,17584,17587,17589,17592,17595,17598,17601,17604,17606,17608,17610],{"class":102,"line":103},[100,17579,17489],{"class":191},[100,17581,89],{"class":118},[100,17583,594],{"class":118},[100,17585,17586],{"class":106},"Errno ",[100,17588,559],{"class":122},[100,17590,17591],{"class":118},"]",[100,17593,17594],{"class":106}," No such ",[100,17596,17597],{"class":17549},"file",[100,17599,17600],{"class":110}," or",[100,17602,17603],{"class":106}," directory",[100,17605,89],{"class":118},[100,17607,1274],{"class":205},[100,17609,17531],{"class":209},[100,17611,3925],{"class":205},[14,17613,17614],{},"This usually means the file path is wrong or the file is not where you think it is.",[14,17616,17617,17618,290],{},"For the most common file-path problem, see ",[295,17619,17621],{"href":17620},"\u002Ferrors\u002Ffilenotfounderror-errno-2-no-such-file-or-directory-fix\u002F","FileNotFoundError: Errno 2 No such file or directory",[77,17623,17625],{"id":17624},"math-and-runtime-errors","Math and runtime errors",[14,17627,17628],{},"These happen while the program is running.",[14,17630,14127],{},[40,17632,17633,17636,17639,17642],{},[43,17634,17635],{},"dividing by zero",[43,17637,17638],{},"recursion that goes too deep",[43,17640,17641],{},"invalid math input",[43,17643,17644],{},"internal runtime problems",[14,17646,16496],{},[40,17648,17649,17654,17659,17664],{},[43,17650,17651],{},[17,17652,17653],{},"ZeroDivisionError",[43,17655,17656],{},[17,17657,17658],{},"RecursionError",[43,17660,17661],{},[17,17662,17663],{},"RuntimeError",[43,17665,17666],{},[17,17667,17668],{},"OverflowError",[14,17670,1844],{},[91,17672,17674],{"className":93,"code":17673,"language":95,"meta":96,"style":96},"print(10 \u002F 0)\n",[17,17675,17676],{"__ignoreMap":96},[100,17677,17678,17680,17682,17684,17687,17689],{"class":102,"line":103},[100,17679,372],{"class":114},[100,17681,170],{"class":118},[100,17683,356],{"class":122},[100,17685,17686],{"class":110}," \u002F",[100,17688,1921],{"class":122},[100,17690,215],{"class":118},[14,17692,218],{},[91,17694,17696],{"className":93,"code":17695,"language":95,"meta":96,"style":96},"ZeroDivisionError: division by zero\n",[17,17697,17698],{"__ignoreMap":96},[100,17699,17700,17702,17704],{"class":102,"line":103},[100,17701,17653],{"class":191},[100,17703,89],{"class":118},[100,17705,17706],{"class":106}," division by zero\n",[14,17708,17709],{},"These errors usually mean your code is syntactically correct, but something went wrong during execution.",[77,17711,17713],{"id":17712},"a-simple-debugging-checklist","A simple debugging checklist",[14,17715,17716],{},"When you hit an error, use this checklist:",[40,17718,17719,17722,17725,17728,17733,17736],{},[43,17720,17721],{},"Read the full error name",[43,17723,17724],{},"Check the exact line number",[43,17726,17727],{},"Print important variables before the failing line",[43,17729,17730,17731],{},"Confirm value types with ",[17,17732,4430],{},[43,17734,17735],{},"Reduce the code to a small example",[43,17737,17738],{},"Open the specific error page for the exact message",[14,17740,5295],{},[91,17742,17744],{"className":93,"code":17743,"language":95,"meta":96,"style":96},"print(variable)\nprint(type(variable))\nprint(len(my_list))\nprint(my_dict.keys())\ndir(object_name)\nhelp(function_name)\n",[17,17745,17746,17757,17771,17786,17801,17812],{"__ignoreMap":96},[100,17747,17748,17750,17752,17755],{"class":102,"line":103},[100,17749,372],{"class":114},[100,17751,170],{"class":118},[100,17753,17754],{"class":178},"variable",[100,17756,215],{"class":118},[100,17758,17759,17761,17763,17765,17767,17769],{"class":102,"line":135},[100,17760,372],{"class":114},[100,17762,170],{"class":118},[100,17764,1250],{"class":191},[100,17766,170],{"class":118},[100,17768,17754],{"class":178},[100,17770,182],{"class":118},[100,17772,17773,17775,17777,17780,17782,17784],{"class":102,"line":142},[100,17774,372],{"class":114},[100,17776,170],{"class":118},[100,17778,17779],{"class":114},"len",[100,17781,170],{"class":118},[100,17783,7441],{"class":178},[100,17785,182],{"class":118},[100,17787,17788,17790,17792,17795,17797,17799],{"class":102,"line":152},[100,17789,372],{"class":114},[100,17791,170],{"class":118},[100,17793,17794],{"class":178},"my_dict",[100,17796,290],{"class":118},[100,17798,6185],{"class":178},[100,17800,3370],{"class":118},[100,17802,17803,17805,17807,17810],{"class":102,"line":164},[100,17804,3822],{"class":114},[100,17806,170],{"class":118},[100,17808,17809],{"class":178},"object_name",[100,17811,215],{"class":118},[100,17813,17814,17816,17818,17821],{"class":102,"line":185},[100,17815,2478],{"class":114},[100,17817,170],{"class":118},[100,17819,17820],{"class":178},"function_name",[100,17822,215],{"class":118},[14,17824,8679],{},[40,17826,17827,17831,17836,17842,17848,17853],{},[43,17828,17829,7047],{},[17,17830,15642],{},[43,17832,17833,17835],{},[17,17834,15648],{}," shows the data type",[43,17837,17838,17841],{},[17,17839,17840],{},"print(len(my_list))"," helps when checking indexes",[43,17843,17844,17847],{},[17,17845,17846],{},"print(my_dict.keys())"," shows available dictionary keys",[43,17849,17850,7071],{},[17,17851,17852],{},"dir(object_name)",[43,17854,17855,17858],{},[17,17856,17857],{},"help(function_name)"," shows built-in help",[77,17860,17862],{"id":17861},"common-causes-of-python-errors","Common causes of Python errors",[14,17864,17865],{},"Many beginner errors come from a small set of problems:",[40,17867,17868,17871,17873,17876,17879,17882,17884,17887],{},[43,17869,17870],{},"misspelled variable or function names",[43,17872,16480],{},[43,17874,17875],{},"using the wrong data type in an operation",[43,17877,17878],{},"trying to access an item that does not exist",[43,17880,17881],{},"bad user input",[43,17883,17471],{},[43,17885,17886],{},"module not installed",[43,17888,17889,17890,17892],{},"using ",[17,17891,930],{}," where a real value is expected",[14,17894,17895],{},"If you check these first, you will often find the problem much faster.",[77,17897,1514],{"id":1513},[675,17899,17901],{"id":17900},"what-is-the-most-common-python-error-for-beginners","What is the most common Python error for beginners?",[14,17903,17904,5870,17906,5870,17908,5629,17910,17912],{},[17,17905,16503],{},[17,17907,5616],{},[17,17909,3538],{},[17,17911,16420],{}," are some of the most common beginner errors.",[675,17914,17916],{"id":17915},"should-i-read-the-first-or-last-line-of-the-traceback-first","Should I read the first or last line of the traceback first?",[14,17918,17919],{},"Start with the last line because it shows the actual error type and message.",[675,17921,17923],{"id":17922},"what-is-the-difference-between-typeerror-and-valueerror","What is the difference between TypeError and ValueError?",[14,17925,17926,17928,17929,17931],{},[17,17927,3538],{}," means the kind of value is wrong. ",[17,17930,3483],{}," means the kind is acceptable, but the actual value is not.",[675,17933,17935],{"id":17934},"why-does-python-say-a-name-is-not-defined","Why does Python say a name is not defined?",[14,17937,17938],{},"Usually because the variable or function name is misspelled, not created yet, or not available in that scope.",[675,17940,17942],{"id":17941},"how-do-i-know-which-error-page-to-read-next","How do I know which error page to read next?",[14,17944,17945],{},"Match the exact error name and message from your traceback to the closest specific error page.",[77,17947,1554],{"id":1553},[40,17949,17950,17954,17959,17963,17967,17971],{},[43,17951,17952],{},[295,17953,1587],{"href":1749},[43,17955,17956],{},[295,17957,17958],{"href":3852},"How to debug Python code (beginner guide)",[43,17960,17961],{},[295,17962,16612],{"href":16611},[43,17964,17965],{},[295,17966,16728],{"href":16727},[43,17968,17969],{},[295,17970,17030],{"href":3269},[43,17972,17973],{},[295,17974,17244],{"href":17243},[1589,17976,17977],{},"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 .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 .sZMiF, html code.shiki .sZMiF{--shiki-light:#E2931D;--shiki-default:#005CC5;--shiki-dark:#79B8FF}html pre.shiki code .su5hD, html code.shiki .su5hD{--shiki-light:#90A4AE;--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 .s_hVV, html code.shiki .s_hVV{--shiki-light:#90A4AE;--shiki-default:#005CC5;--shiki-dark:#79B8FF}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 .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 .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 .s39Yj, html code.shiki .s39Yj{--shiki-light:#39ADB5;--shiki-default:#005CC5;--shiki-dark:#79B8FF}html pre.shiki code .sMMDD, html code.shiki .sMMDD{--shiki-light:#90A4AE;--shiki-default:#E36209;--shiki-dark:#FFAB70}",{"title":96,"searchDepth":135,"depth":135,"links":17979},[17980,17981,17982,17983,17984,17985,17986,17987,17988,17989,17990,17991,17992,17993,17994,18001],{"id":16189,"depth":135,"text":16190},{"id":16264,"depth":135,"text":16265},{"id":16290,"depth":135,"text":16291},{"id":16461,"depth":135,"text":16462},{"id":16621,"depth":135,"text":16622},{"id":16731,"depth":135,"text":16732},{"id":16907,"depth":135,"text":16908},{"id":17033,"depth":135,"text":17034},{"id":17247,"depth":135,"text":17248},{"id":17363,"depth":135,"text":17364},{"id":17460,"depth":135,"text":17461},{"id":17624,"depth":135,"text":17625},{"id":17712,"depth":135,"text":17713},{"id":17861,"depth":135,"text":17862},{"id":1513,"depth":135,"text":1514,"children":17995},[17996,17997,17998,17999,18000],{"id":17900,"depth":142,"text":17901},{"id":17915,"depth":142,"text":17916},{"id":17922,"depth":142,"text":17923},{"id":17934,"depth":142,"text":17935},{"id":17941,"depth":142,"text":17942},{"id":1553,"depth":135,"text":1554},"Master common python errors explained beginner guide in our comprehensive Python beginner guide.",{},"\u002Ferrors\u002Fcommon-python-errors-explained-beginner-guide",{"title":16158,"description":18002},"errors\u002Fcommon-python-errors-explained-beginner-guide","gtVfRUTZUfxH2i5Q-21YFdrznYzrfu0Wm1EJqgT4Nas",{"id":18009,"title":18010,"body":18011,"description":20689,"extension":1623,"meta":20690,"navigation":138,"path":20691,"seo":20692,"stem":20693,"__hash__":20694},"content\u002Ferrors\u002Ffilenotfounderror-errno-2-no-such-file-or-directory-fix.md","FileNotFoundError: [Errno 2] No such file or directory (Fix)",{"type":7,"value":18012,"toc":20646},[18013,18021,18027,18040,18054,18057,18221,18224,18226,18229,18232,18246,18249,18275,18287,18291,18296,18317,18319,18357,18359,18362,18435,18440,18553,18556,18591,18594,18596,18599,18603,18606,18609,18637,18643,18647,18650,18653,18678,18681,18685,18691,18722,18725,18737,18741,18744,18806,18809,18871,18875,18878,18882,18984,18989,19097,19105,19109,19112,19118,19186,19189,19331,19335,19338,19342,19404,19407,19411,19436,19440,19478,19483,19520,19528,19532,19588,19594,19598,19604,19608,19611,19615,19618,19620,19646,19649,19659,19663,19666,19668,19696,19699,19703,19706,19709,19714,19824,19827,19831,19834,19839,19844,19903,19907,20009,20015,20083,20087,20204,20206,20209,20236,20240,20243,20267,20304,20340,20376,20413,20415,20419,20425,20432,20435,20454,20458,20463,20476,20480,20483,20485,20576,20582,20584,20643],[10,18014,18016,18017,18020],{"id":18015},"filenotfounderror-errno-2-no-such-file-or-directory-fix","FileNotFoundError: ",[100,18018,18019],{},"Errno 2"," No such file or directory (Fix)",[14,18022,18023,18026],{},[17,18024,18025],{},"FileNotFoundError: [Errno 2] No such file or directory"," means Python could not find the file or folder path you gave it.",[14,18028,18029,18030,5870,18033,5894,18036,18039],{},"This error is very common when working with ",[17,18031,18032],{},"open()",[17,18034,18035],{},"os",[17,18037,18038],{},"pathlib",". In most cases, the problem is one of these:",[40,18041,18042,18045,18048,18051],{},[43,18043,18044],{},"The file name is wrong",[43,18046,18047],{},"The file is in a different folder",[43,18049,18050],{},"The path is relative, but Python is running from a different working directory",[43,18052,18053],{},"A folder in the path does not exist",[14,18055,18056],{},"If you want a fast way to check the path before opening the file, use this:",[91,18058,18060],{"className":93,"code":18059,"language":95,"meta":96,"style":96},"from pathlib import Path\n\nfile_path = Path(\"data.txt\")\n\nif file_path.exists():\n    with open(file_path, \"r\", encoding=\"utf-8\") as file:\n        content = file.read()\n        print(content)\nelse:\n    print(f\"File not found: {file_path.resolve()}\")\n",[17,18061,18062,18074,18078,18099,18103,18117,18159,18174,18185,18191],{"__ignoreMap":96},[100,18063,18064,18066,18069,18071],{"class":102,"line":103},[100,18065,10201],{"class":145},[100,18067,18068],{"class":106}," pathlib ",[100,18070,9259],{"class":145},[100,18072,18073],{"class":106}," Path\n",[100,18075,18076],{"class":102,"line":135},[100,18077,139],{"emptyLinePlaceholder":138},[100,18079,18080,18083,18085,18088,18090,18092,18095,18097],{"class":102,"line":142},[100,18081,18082],{"class":106},"file_path ",[100,18084,111],{"class":110},[100,18086,18087],{"class":178}," Path",[100,18089,170],{"class":118},[100,18091,206],{"class":205},[100,18093,18094],{"class":209},"data.txt",[100,18096,206],{"class":205},[100,18098,215],{"class":118},[100,18100,18101],{"class":102,"line":152},[100,18102,139],{"emptyLinePlaceholder":138},[100,18104,18105,18107,18110,18112,18115],{"class":102,"line":164},[100,18106,2736],{"class":145},[100,18108,18109],{"class":106}," file_path",[100,18111,290],{"class":118},[100,18113,18114],{"class":178},"exists",[100,18116,1085],{"class":118},[100,18118,18119,18122,18124,18126,18129,18131,18133,18135,18137,18139,18143,18145,18147,18149,18151,18153,18155,18157],{"class":102,"line":185},[100,18120,18121],{"class":145},"    with",[100,18123,17524],{"class":114},[100,18125,170],{"class":118},[100,18127,18128],{"class":178},"file_path",[100,18130,126],{"class":118},[100,18132,1708],{"class":205},[100,18134,17540],{"class":209},[100,18136,206],{"class":205},[100,18138,126],{"class":118},[100,18140,18142],{"class":18141},"s99_P"," encoding",[100,18144,111],{"class":110},[100,18146,206],{"class":205},[100,18148,15307],{"class":209},[100,18150,206],{"class":205},[100,18152,6155],{"class":118},[100,18154,16224],{"class":145},[100,18156,17550],{"class":17549},[100,18158,149],{"class":118},[100,18160,18161,18164,18166,18168,18170,18172],{"class":102,"line":197},[100,18162,18163],{"class":106},"        content ",[100,18165,111],{"class":110},[100,18167,17550],{"class":17549},[100,18169,290],{"class":118},[100,18171,17566],{"class":178},[100,18173,4734],{"class":118},[100,18175,18176,18178,18180,18183],{"class":102,"line":771},[100,18177,167],{"class":114},[100,18179,170],{"class":118},[100,18181,18182],{"class":178},"content",[100,18184,215],{"class":118},[100,18186,18187,18189],{"class":102,"line":787},[100,18188,4944],{"class":145},[100,18190,149],{"class":118},[100,18192,18193,18195,18197,18200,18203,18205,18207,18209,18212,18214,18217,18219],{"class":102,"line":5816},[100,18194,200],{"class":114},[100,18196,170],{"class":118},[100,18198,18199],{"class":1077},"f",[100,18201,18202],{"class":209},"\"File not found: ",[100,18204,16490],{"class":122},[100,18206,18128],{"class":178},[100,18208,290],{"class":118},[100,18210,18211],{"class":178},"resolve",[100,18213,7562],{"class":118},[100,18215,18216],{"class":122},"}",[100,18218,206],{"class":209},[100,18220,215],{"class":118},[14,18222,18223],{},"Use this when you are not sure whether the file exists. It checks the path before opening it and shows the full path for debugging.",[77,18225,5881],{"id":5880},[14,18227,18228],{},"This error means Python tried to access a path, but that path was not found.",[14,18230,18231],{},"That can happen when:",[40,18233,18234,18237,18240,18243],{},[43,18235,18236],{},"The file does not exist",[43,18238,18239],{},"The folder does not exist",[43,18241,18242],{},"The path points to the wrong location",[43,18244,18245],{},"You expected Python to run from one folder, but it is actually running from another",[14,18247,18248],{},"You will often see this error with:",[40,18250,18251,18255,18260,18265,18270],{},[43,18252,18253],{},[17,18254,18032],{},[43,18256,18257],{},[17,18258,18259],{},"os.listdir()",[43,18261,18262],{},[17,18263,18264],{},"os.remove()",[43,18266,18267],{},[17,18268,18269],{},"pathlib.Path.read_text()",[43,18271,18272],{},[17,18273,18274],{},"pathlib.Path.open()",[14,18276,18277,18278,3266,18283,290],{},"If you are new to file handling, it helps to first understand the ",[295,18279,1572,18281,6354],{"href":18280},"\u002Freference\u002Fpython-open-function-explained\u002F",[17,18282,18032],{},[295,18284,18286],{"href":18285},"\u002Flearn\u002Fworking-with-file-paths-in-python\u002F","working with file paths in Python",[77,18288,18290],{"id":18289},"when-this-error-usually-happens","When this error usually happens",[14,18292,18293,18295],{},[17,18294,17489],{}," usually appears in one of these situations:",[40,18297,18298,18301,18308,18311,18314],{},[43,18299,18300],{},"You try to open a file that does not exist",[43,18302,18303,18304,18307],{},"You use a relative path like ",[17,18305,18306],{},"\"data.txt\"",", but Python is running from a different folder",[43,18309,18310],{},"You misspell the file name or extension",[43,18312,18313],{},"You try to access a folder path that does not exist yet",[43,18315,18316],{},"You build the path incorrectly with slashes or manual string concatenation",[14,18318,13223],{},[40,18320,18321,18331,18340,18351],{},[43,18322,18323,18324,18327,18328],{},"Writing ",[17,18325,18326],{},"report.txt"," when the real file is ",[17,18329,18330],{},"reports.txt",[43,18332,1357,18333,18336,18337],{},[17,18334,18335],{},".txt"," when the file is really ",[17,18338,18339],{},".csv",[43,18341,18342,18343,18346,18347,18350],{},"Trying to write to ",[17,18344,18345],{},"output\u002Fresult.txt"," when the ",[17,18348,18349],{},"output"," folder does not exist",[43,18352,1357,18353,18356],{},[17,18354,18355],{},"folder\" + \"\u002F\" + filename"," and accidentally creating the wrong path",[77,18358,11031],{"id":11030},[14,18360,18361],{},"Here is a minimal example:",[91,18363,18365],{"className":93,"code":18364,"language":95,"meta":96,"style":96},"with open(\"missing.txt\", \"r\", encoding=\"utf-8\") as file:\n    content = file.read()\n    print(content)\n",[17,18366,18367,18410,18425],{"__ignoreMap":96},[100,18368,18369,18371,18373,18375,18377,18380,18382,18384,18386,18388,18390,18392,18394,18396,18398,18400,18402,18404,18406,18408],{"class":102,"line":103},[100,18370,17521],{"class":145},[100,18372,17524],{"class":114},[100,18374,170],{"class":118},[100,18376,206],{"class":205},[100,18378,18379],{"class":209},"missing.txt",[100,18381,206],{"class":205},[100,18383,126],{"class":118},[100,18385,1708],{"class":205},[100,18387,17540],{"class":209},[100,18389,206],{"class":205},[100,18391,126],{"class":118},[100,18393,18142],{"class":18141},[100,18395,111],{"class":110},[100,18397,206],{"class":205},[100,18399,15307],{"class":209},[100,18401,206],{"class":205},[100,18403,6155],{"class":118},[100,18405,16224],{"class":145},[100,18407,17550],{"class":17549},[100,18409,149],{"class":118},[100,18411,18412,18415,18417,18419,18421,18423],{"class":102,"line":135},[100,18413,18414],{"class":106},"    content ",[100,18416,111],{"class":110},[100,18418,17550],{"class":17549},[100,18420,290],{"class":118},[100,18422,17566],{"class":178},[100,18424,4734],{"class":118},[100,18426,18427,18429,18431,18433],{"class":102,"line":142},[100,18428,200],{"class":114},[100,18430,170],{"class":118},[100,18432,18182],{"class":178},[100,18434,215],{"class":118},[14,18436,11734,18437,18439],{},[17,18438,18379],{}," is not in the current working directory, Python raises:",[91,18441,18443],{"className":93,"code":18442,"language":95,"meta":96,"style":96},"Traceback (most recent call last):\n  File \"example.py\", line 1, in \u003Cmodule>\n    with open(\"missing.txt\", \"r\", encoding=\"utf-8\") as file:\nFileNotFoundError: [Errno 2] No such file or directory: 'missing.txt'\n",[17,18444,18445,18455,18481,18523],{"__ignoreMap":96},[100,18446,18447,18449,18451,18453],{"class":102,"line":103},[100,18448,514],{"class":178},[100,18450,170],{"class":118},[100,18452,519],{"class":178},[100,18454,522],{"class":118},[100,18456,18457,18459,18461,18463,18465,18467,18469,18471,18473,18475,18477,18479],{"class":102,"line":135},[100,18458,1952],{"class":106},[100,18460,206],{"class":205},[100,18462,1957],{"class":209},[100,18464,206],{"class":205},[100,18466,126],{"class":118},[100,18468,1964],{"class":106},[100,18470,123],{"class":122},[100,18472,126],{"class":118},[100,18474,1971],{"class":110},[100,18476,1974],{"class":110},[100,18478,1977],{"class":106},[100,18480,1980],{"class":110},[100,18482,18483,18485,18487,18489,18491,18493,18495,18497,18499,18501,18503,18505,18507,18509,18511,18513,18515,18517,18519,18521],{"class":102,"line":142},[100,18484,18121],{"class":145},[100,18486,17524],{"class":114},[100,18488,170],{"class":118},[100,18490,206],{"class":205},[100,18492,18379],{"class":209},[100,18494,206],{"class":205},[100,18496,126],{"class":118},[100,18498,1708],{"class":205},[100,18500,17540],{"class":209},[100,18502,206],{"class":205},[100,18504,126],{"class":118},[100,18506,18142],{"class":18141},[100,18508,111],{"class":110},[100,18510,206],{"class":205},[100,18512,15307],{"class":209},[100,18514,206],{"class":205},[100,18516,6155],{"class":118},[100,18518,16224],{"class":145},[100,18520,17550],{"class":17549},[100,18522,149],{"class":118},[100,18524,18525,18527,18529,18531,18533,18535,18537,18539,18541,18543,18545,18547,18549,18551],{"class":102,"line":152},[100,18526,17489],{"class":191},[100,18528,89],{"class":118},[100,18530,594],{"class":118},[100,18532,17586],{"class":106},[100,18534,559],{"class":122},[100,18536,17591],{"class":118},[100,18538,17594],{"class":106},[100,18540,17597],{"class":17549},[100,18542,17600],{"class":110},[100,18544,17603],{"class":106},[100,18546,89],{"class":118},[100,18548,1274],{"class":205},[100,18550,18379],{"class":209},[100,18552,3925],{"class":205},[14,18554,18555],{},"The important part is the last line:",[91,18557,18559],{"className":93,"code":18558,"language":95,"meta":96,"style":96},"FileNotFoundError: [Errno 2] No such file or directory: 'missing.txt'\n",[17,18560,18561],{"__ignoreMap":96},[100,18562,18563,18565,18567,18569,18571,18573,18575,18577,18579,18581,18583,18585,18587,18589],{"class":102,"line":103},[100,18564,17489],{"class":191},[100,18566,89],{"class":118},[100,18568,594],{"class":118},[100,18570,17586],{"class":106},[100,18572,559],{"class":122},[100,18574,17591],{"class":118},[100,18576,17594],{"class":106},[100,18578,17597],{"class":17549},[100,18580,17600],{"class":110},[100,18582,17603],{"class":106},[100,18584,89],{"class":118},[100,18586,1274],{"class":205},[100,18588,18379],{"class":209},[100,18590,3925],{"class":205},[14,18592,18593],{},"That tells you exactly which path Python failed to find.",[77,18595,6244],{"id":6243},[14,18597,18598],{},"Here are the fastest ways to fix this error.",[675,18600,18602],{"id":18601},"_1-check-that-the-file-really-exists","1. Check that the file really exists",[14,18604,18605],{},"Make sure the file is actually there.",[14,18607,18608],{},"For example, if your code uses:",[91,18610,18612],{"className":93,"code":18611,"language":95,"meta":96,"style":96},"open(\"data.txt\", \"r\")\n",[17,18613,18614],{"__ignoreMap":96},[100,18615,18616,18619,18621,18623,18625,18627,18629,18631,18633,18635],{"class":102,"line":103},[100,18617,18618],{"class":114},"open",[100,18620,170],{"class":118},[100,18622,206],{"class":205},[100,18624,18094],{"class":209},[100,18626,206],{"class":205},[100,18628,126],{"class":118},[100,18630,1708],{"class":205},[100,18632,17540],{"class":209},[100,18634,206],{"class":205},[100,18636,215],{"class":118},[14,18638,18639,18640,18642],{},"confirm that ",[17,18641,18094],{}," exists in the folder where Python is running.",[675,18644,18646],{"id":18645},"_2-check-the-file-name-extension-and-capitalization","2. Check the file name, extension, and capitalization",[14,18648,18649],{},"Small differences matter.",[14,18651,18652],{},"Check for mistakes like:",[40,18654,18655,18663,18670],{},[43,18656,18657,18659,18660],{},[17,18658,18094],{}," vs ",[17,18661,18662],{},"Data.txt",[43,18664,18665,18659,18668],{},[17,18666,18667],{},"report.csv",[17,18669,18326],{},[43,18671,18672,18659,18675],{},[17,18673,18674],{},"image.PNG",[17,18676,18677],{},"image.png",[14,18679,18680],{},"On some systems, capitalization matters.",[675,18682,18684],{"id":18683},"_3-print-the-current-working-directory","3. Print the current working directory",[14,18686,18687,18688,18690],{},"A relative path like ",[17,18689,18306],{}," depends on the current working directory, not always the script location.",[91,18692,18694],{"className":93,"code":18693,"language":95,"meta":96,"style":96},"import os\n\nprint(os.getcwd())\n",[17,18695,18696,18703,18707],{"__ignoreMap":96},[100,18697,18698,18700],{"class":102,"line":103},[100,18699,9259],{"class":145},[100,18701,18702],{"class":106}," os\n",[100,18704,18705],{"class":102,"line":135},[100,18706,139],{"emptyLinePlaceholder":138},[100,18708,18709,18711,18713,18715,18717,18720],{"class":102,"line":142},[100,18710,372],{"class":114},[100,18712,170],{"class":118},[100,18714,18035],{"class":178},[100,18716,290],{"class":118},[100,18718,18719],{"class":178},"getcwd",[100,18721,3370],{"class":118},[14,18723,18724],{},"If this folder is not where your file is, Python will not find it.",[14,18726,18727,18728,18730,18731,290],{},"If you are using the ",[17,18729,18035],{}," module, this is also a good time to review the ",[295,18732,1572,18734,18736],{"href":18733},"\u002Fstandard-library\u002Fpython-os-module-overview\u002F",[17,18735,18035],{}," module overview",[675,18738,18740],{"id":18739},"_4-use-the-correct-absolute-or-relative-path","4. Use the correct absolute or relative path",[14,18742,18743],{},"If the file is in a subfolder, include that folder:",[91,18745,18747],{"className":93,"code":18746,"language":95,"meta":96,"style":96},"with open(\"files\u002Fdata.txt\", \"r\", encoding=\"utf-8\") as file:\n    print(file.read())\n",[17,18748,18749,18792],{"__ignoreMap":96},[100,18750,18751,18753,18755,18757,18759,18762,18764,18766,18768,18770,18772,18774,18776,18778,18780,18782,18784,18786,18788,18790],{"class":102,"line":103},[100,18752,17521],{"class":145},[100,18754,17524],{"class":114},[100,18756,170],{"class":118},[100,18758,206],{"class":205},[100,18760,18761],{"class":209},"files\u002Fdata.txt",[100,18763,206],{"class":205},[100,18765,126],{"class":118},[100,18767,1708],{"class":205},[100,18769,17540],{"class":209},[100,18771,206],{"class":205},[100,18773,126],{"class":118},[100,18775,18142],{"class":18141},[100,18777,111],{"class":110},[100,18779,206],{"class":205},[100,18781,15307],{"class":209},[100,18783,206],{"class":205},[100,18785,6155],{"class":118},[100,18787,16224],{"class":145},[100,18789,17550],{"class":17549},[100,18791,149],{"class":118},[100,18793,18794,18796,18798,18800,18802,18804],{"class":102,"line":135},[100,18795,200],{"class":114},[100,18797,170],{"class":118},[100,18799,17597],{"class":17549},[100,18801,290],{"class":118},[100,18803,17566],{"class":178},[100,18805,3370],{"class":118},[14,18807,18808],{},"An absolute path also works:",[91,18810,18812],{"className":93,"code":18811,"language":95,"meta":96,"style":96},"with open(\"\u002Ffull\u002Fpath\u002Fto\u002Fdata.txt\", \"r\", encoding=\"utf-8\") as file:\n    print(file.read())\n",[17,18813,18814,18857],{"__ignoreMap":96},[100,18815,18816,18818,18820,18822,18824,18827,18829,18831,18833,18835,18837,18839,18841,18843,18845,18847,18849,18851,18853,18855],{"class":102,"line":103},[100,18817,17521],{"class":145},[100,18819,17524],{"class":114},[100,18821,170],{"class":118},[100,18823,206],{"class":205},[100,18825,18826],{"class":209},"\u002Ffull\u002Fpath\u002Fto\u002Fdata.txt",[100,18828,206],{"class":205},[100,18830,126],{"class":118},[100,18832,1708],{"class":205},[100,18834,17540],{"class":209},[100,18836,206],{"class":205},[100,18838,126],{"class":118},[100,18840,18142],{"class":18141},[100,18842,111],{"class":110},[100,18844,206],{"class":205},[100,18846,15307],{"class":209},[100,18848,206],{"class":205},[100,18850,6155],{"class":118},[100,18852,16224],{"class":145},[100,18854,17550],{"class":17549},[100,18856,149],{"class":118},[100,18858,18859,18861,18863,18865,18867,18869],{"class":102,"line":135},[100,18860,200],{"class":114},[100,18862,170],{"class":118},[100,18864,17597],{"class":17549},[100,18866,290],{"class":118},[100,18868,17566],{"class":178},[100,18870,3370],{"class":118},[675,18872,18874],{"id":18873},"_5-build-paths-safely","5. Build paths safely",[14,18876,18877],{},"Avoid building paths by hand when possible.",[14,18879,1357,18880,89],{},[17,18881,18038],{},[91,18883,18885],{"className":93,"code":18884,"language":95,"meta":96,"style":96},"from pathlib import Path\n\nfile_path = Path(\"files\") \u002F \"data.txt\"\n\nwith open(file_path, \"r\", encoding=\"utf-8\") as file:\n    print(file.read())\n",[17,18886,18887,18897,18901,18928,18932,18970],{"__ignoreMap":96},[100,18888,18889,18891,18893,18895],{"class":102,"line":103},[100,18890,10201],{"class":145},[100,18892,18068],{"class":106},[100,18894,9259],{"class":145},[100,18896,18073],{"class":106},[100,18898,18899],{"class":102,"line":135},[100,18900,139],{"emptyLinePlaceholder":138},[100,18902,18903,18905,18907,18909,18911,18913,18916,18918,18920,18922,18924,18926],{"class":102,"line":142},[100,18904,18082],{"class":106},[100,18906,111],{"class":110},[100,18908,18087],{"class":178},[100,18910,170],{"class":118},[100,18912,206],{"class":205},[100,18914,18915],{"class":209},"files",[100,18917,206],{"class":205},[100,18919,6155],{"class":118},[100,18921,17686],{"class":110},[100,18923,1708],{"class":205},[100,18925,18094],{"class":209},[100,18927,1714],{"class":205},[100,18929,18930],{"class":102,"line":152},[100,18931,139],{"emptyLinePlaceholder":138},[100,18933,18934,18936,18938,18940,18942,18944,18946,18948,18950,18952,18954,18956,18958,18960,18962,18964,18966,18968],{"class":102,"line":164},[100,18935,17521],{"class":145},[100,18937,17524],{"class":114},[100,18939,170],{"class":118},[100,18941,18128],{"class":178},[100,18943,126],{"class":118},[100,18945,1708],{"class":205},[100,18947,17540],{"class":209},[100,18949,206],{"class":205},[100,18951,126],{"class":118},[100,18953,18142],{"class":18141},[100,18955,111],{"class":110},[100,18957,206],{"class":205},[100,18959,15307],{"class":209},[100,18961,206],{"class":205},[100,18963,6155],{"class":118},[100,18965,16224],{"class":145},[100,18967,17550],{"class":17549},[100,18969,149],{"class":118},[100,18971,18972,18974,18976,18978,18980,18982],{"class":102,"line":185},[100,18973,200],{"class":114},[100,18975,170],{"class":118},[100,18977,17597],{"class":17549},[100,18979,290],{"class":118},[100,18981,17566],{"class":178},[100,18983,3370],{"class":118},[14,18985,1357,18986,89],{},[17,18987,18988],{},"os.path.join()",[91,18990,18992],{"className":93,"code":18991,"language":95,"meta":96,"style":96},"import os\n\nfile_path = os.path.join(\"files\", \"data.txt\")\n\nwith open(file_path, \"r\", encoding=\"utf-8\") as file:\n    print(file.read())\n",[17,18993,18994,19000,19004,19041,19045,19083],{"__ignoreMap":96},[100,18995,18996,18998],{"class":102,"line":103},[100,18997,9259],{"class":145},[100,18999,18702],{"class":106},[100,19001,19002],{"class":102,"line":135},[100,19003,139],{"emptyLinePlaceholder":138},[100,19005,19006,19008,19010,19013,19015,19018,19020,19023,19025,19027,19029,19031,19033,19035,19037,19039],{"class":102,"line":142},[100,19007,18082],{"class":106},[100,19009,111],{"class":110},[100,19011,19012],{"class":106}," os",[100,19014,290],{"class":118},[100,19016,19017],{"class":2494},"path",[100,19019,290],{"class":118},[100,19021,19022],{"class":178},"join",[100,19024,170],{"class":118},[100,19026,206],{"class":205},[100,19028,18915],{"class":209},[100,19030,206],{"class":205},[100,19032,126],{"class":118},[100,19034,1708],{"class":205},[100,19036,18094],{"class":209},[100,19038,206],{"class":205},[100,19040,215],{"class":118},[100,19042,19043],{"class":102,"line":152},[100,19044,139],{"emptyLinePlaceholder":138},[100,19046,19047,19049,19051,19053,19055,19057,19059,19061,19063,19065,19067,19069,19071,19073,19075,19077,19079,19081],{"class":102,"line":164},[100,19048,17521],{"class":145},[100,19050,17524],{"class":114},[100,19052,170],{"class":118},[100,19054,18128],{"class":178},[100,19056,126],{"class":118},[100,19058,1708],{"class":205},[100,19060,17540],{"class":209},[100,19062,206],{"class":205},[100,19064,126],{"class":118},[100,19066,18142],{"class":18141},[100,19068,111],{"class":110},[100,19070,206],{"class":205},[100,19072,15307],{"class":209},[100,19074,206],{"class":205},[100,19076,6155],{"class":118},[100,19078,16224],{"class":145},[100,19080,17550],{"class":17549},[100,19082,149],{"class":118},[100,19084,19085,19087,19089,19091,19093,19095],{"class":102,"line":185},[100,19086,200],{"class":114},[100,19088,170],{"class":118},[100,19090,17597],{"class":17549},[100,19092,290],{"class":118},[100,19094,17566],{"class":178},[100,19096,3370],{"class":118},[14,19098,19099,19100,290],{},"If you want a method-by-method explanation, see ",[295,19101,19103,9570],{"href":19102},"\u002Fstandard-library\u002Fos.path.join-function-explained",[17,19104,18988],{},[675,19106,19108],{"id":19107},"_6-create-the-folder-first-when-writing","6. Create the folder first when writing",[14,19110,19111],{},"If you write to a file inside a missing folder, Python cannot create the file until the folder exists.",[14,19113,19114,19115,19117],{},"This will fail if ",[17,19116,18349],{}," does not exist:",[91,19119,19121],{"className":93,"code":19120,"language":95,"meta":96,"style":96},"with open(\"output\u002Fresult.txt\", \"w\", encoding=\"utf-8\") as file:\n    file.write(\"Hello\")\n",[17,19122,19123,19166],{"__ignoreMap":96},[100,19124,19125,19127,19129,19131,19133,19135,19137,19139,19141,19144,19146,19148,19150,19152,19154,19156,19158,19160,19162,19164],{"class":102,"line":103},[100,19126,17521],{"class":145},[100,19128,17524],{"class":114},[100,19130,170],{"class":118},[100,19132,206],{"class":205},[100,19134,18345],{"class":209},[100,19136,206],{"class":205},[100,19138,126],{"class":118},[100,19140,1708],{"class":205},[100,19142,19143],{"class":209},"w",[100,19145,206],{"class":205},[100,19147,126],{"class":118},[100,19149,18142],{"class":18141},[100,19151,111],{"class":110},[100,19153,206],{"class":205},[100,19155,15307],{"class":209},[100,19157,206],{"class":205},[100,19159,6155],{"class":118},[100,19161,16224],{"class":145},[100,19163,17550],{"class":17549},[100,19165,149],{"class":118},[100,19167,19168,19171,19173,19176,19178,19180,19182,19184],{"class":102,"line":135},[100,19169,19170],{"class":17549},"    file",[100,19172,290],{"class":118},[100,19174,19175],{"class":178},"write",[100,19177,170],{"class":118},[100,19179,206],{"class":205},[100,19181,7683],{"class":209},[100,19183,206],{"class":205},[100,19185,215],{"class":118},[14,19187,19188],{},"Fix it like this:",[91,19190,19192],{"className":93,"code":19191,"language":95,"meta":96,"style":96},"from pathlib import Path\n\noutput_dir = Path(\"output\")\noutput_dir.mkdir(exist_ok=True)\n\nfile_path = output_dir \u002F \"result.txt\"\n\nwith open(file_path, \"w\", encoding=\"utf-8\") as file:\n    file.write(\"Hello\")\n",[17,19193,19194,19204,19208,19227,19249,19253,19271,19275,19313],{"__ignoreMap":96},[100,19195,19196,19198,19200,19202],{"class":102,"line":103},[100,19197,10201],{"class":145},[100,19199,18068],{"class":106},[100,19201,9259],{"class":145},[100,19203,18073],{"class":106},[100,19205,19206],{"class":102,"line":135},[100,19207,139],{"emptyLinePlaceholder":138},[100,19209,19210,19213,19215,19217,19219,19221,19223,19225],{"class":102,"line":142},[100,19211,19212],{"class":106},"output_dir ",[100,19214,111],{"class":110},[100,19216,18087],{"class":178},[100,19218,170],{"class":118},[100,19220,206],{"class":205},[100,19222,18349],{"class":209},[100,19224,206],{"class":205},[100,19226,215],{"class":118},[100,19228,19229,19232,19234,19237,19239,19242,19244,19247],{"class":102,"line":152},[100,19230,19231],{"class":106},"output_dir",[100,19233,290],{"class":118},[100,19235,19236],{"class":178},"mkdir",[100,19238,170],{"class":118},[100,19240,19241],{"class":18141},"exist_ok",[100,19243,111],{"class":110},[100,19245,19246],{"class":158},"True",[100,19248,215],{"class":118},[100,19250,19251],{"class":102,"line":164},[100,19252,139],{"emptyLinePlaceholder":138},[100,19254,19255,19257,19259,19262,19264,19266,19269],{"class":102,"line":185},[100,19256,18082],{"class":106},[100,19258,111],{"class":110},[100,19260,19261],{"class":106}," output_dir ",[100,19263,2558],{"class":110},[100,19265,1708],{"class":205},[100,19267,19268],{"class":209},"result.txt",[100,19270,1714],{"class":205},[100,19272,19273],{"class":102,"line":197},[100,19274,139],{"emptyLinePlaceholder":138},[100,19276,19277,19279,19281,19283,19285,19287,19289,19291,19293,19295,19297,19299,19301,19303,19305,19307,19309,19311],{"class":102,"line":771},[100,19278,17521],{"class":145},[100,19280,17524],{"class":114},[100,19282,170],{"class":118},[100,19284,18128],{"class":178},[100,19286,126],{"class":118},[100,19288,1708],{"class":205},[100,19290,19143],{"class":209},[100,19292,206],{"class":205},[100,19294,126],{"class":118},[100,19296,18142],{"class":18141},[100,19298,111],{"class":110},[100,19300,206],{"class":205},[100,19302,15307],{"class":209},[100,19304,206],{"class":205},[100,19306,6155],{"class":118},[100,19308,16224],{"class":145},[100,19310,17550],{"class":17549},[100,19312,149],{"class":118},[100,19314,19315,19317,19319,19321,19323,19325,19327,19329],{"class":102,"line":787},[100,19316,19170],{"class":17549},[100,19318,290],{"class":118},[100,19320,19175],{"class":178},[100,19322,170],{"class":118},[100,19324,206],{"class":205},[100,19326,7683],{"class":209},[100,19328,206],{"class":205},[100,19330,215],{"class":118},[77,19332,19334],{"id":19333},"debugging-steps-to-try","Debugging steps to try",[14,19336,19337],{},"When you are not sure what is wrong, do these checks in order.",[675,19339,19341],{"id":19340},"print-the-exact-path-your-code-is-using","Print the exact path your code is using",[91,19343,19345],{"className":93,"code":19344,"language":95,"meta":96,"style":96},"from pathlib import Path\n\npath = Path(\"data.txt\")\nprint(path)\nprint(path.resolve())\n",[17,19346,19347,19357,19361,19380,19390],{"__ignoreMap":96},[100,19348,19349,19351,19353,19355],{"class":102,"line":103},[100,19350,10201],{"class":145},[100,19352,18068],{"class":106},[100,19354,9259],{"class":145},[100,19356,18073],{"class":106},[100,19358,19359],{"class":102,"line":135},[100,19360,139],{"emptyLinePlaceholder":138},[100,19362,19363,19366,19368,19370,19372,19374,19376,19378],{"class":102,"line":142},[100,19364,19365],{"class":106},"path ",[100,19367,111],{"class":110},[100,19369,18087],{"class":178},[100,19371,170],{"class":118},[100,19373,206],{"class":205},[100,19375,18094],{"class":209},[100,19377,206],{"class":205},[100,19379,215],{"class":118},[100,19381,19382,19384,19386,19388],{"class":102,"line":152},[100,19383,372],{"class":114},[100,19385,170],{"class":118},[100,19387,19017],{"class":178},[100,19389,215],{"class":118},[100,19391,19392,19394,19396,19398,19400,19402],{"class":102,"line":164},[100,19393,372],{"class":114},[100,19395,170],{"class":118},[100,19397,19017],{"class":178},[100,19399,290],{"class":118},[100,19401,18211],{"class":178},[100,19403,3370],{"class":118},[14,19405,19406],{},"This shows both the path you wrote and the full resolved path.",[675,19408,19410],{"id":19409},"print-the-current-working-directory","Print the current working directory",[91,19412,19414],{"className":93,"code":19413,"language":95,"meta":96,"style":96},"import os\nprint(os.getcwd())\n",[17,19415,19416,19422],{"__ignoreMap":96},[100,19417,19418,19420],{"class":102,"line":103},[100,19419,9259],{"class":145},[100,19421,18702],{"class":106},[100,19423,19424,19426,19428,19430,19432,19434],{"class":102,"line":135},[100,19425,372],{"class":114},[100,19427,170],{"class":118},[100,19429,18035],{"class":178},[100,19431,290],{"class":118},[100,19433,18719],{"class":178},[100,19435,3370],{"class":118},[675,19437,19439],{"id":19438},"check-whether-the-file-exists","Check whether the file exists",[91,19441,19443],{"className":93,"code":19442,"language":95,"meta":96,"style":96},"from pathlib import Path\nprint(Path(\"data.txt\").exists())\n",[17,19444,19445,19455],{"__ignoreMap":96},[100,19446,19447,19449,19451,19453],{"class":102,"line":103},[100,19448,10201],{"class":145},[100,19450,18068],{"class":106},[100,19452,9259],{"class":145},[100,19454,18073],{"class":106},[100,19456,19457,19459,19461,19464,19466,19468,19470,19472,19474,19476],{"class":102,"line":135},[100,19458,372],{"class":114},[100,19460,170],{"class":118},[100,19462,19463],{"class":178},"Path",[100,19465,170],{"class":118},[100,19467,206],{"class":205},[100,19469,18094],{"class":209},[100,19471,206],{"class":205},[100,19473,5799],{"class":118},[100,19475,18114],{"class":178},[100,19477,3370],{"class":118},[14,19479,4424,19480,89],{},[17,19481,19482],{},"os.path.exists()",[91,19484,19486],{"className":93,"code":19485,"language":95,"meta":96,"style":96},"import os\nprint(os.path.exists(\"data.txt\"))\n",[17,19487,19488,19494],{"__ignoreMap":96},[100,19489,19490,19492],{"class":102,"line":103},[100,19491,9259],{"class":145},[100,19493,18702],{"class":106},[100,19495,19496,19498,19500,19502,19504,19506,19508,19510,19512,19514,19516,19518],{"class":102,"line":135},[100,19497,372],{"class":114},[100,19499,170],{"class":118},[100,19501,18035],{"class":178},[100,19503,290],{"class":118},[100,19505,19017],{"class":2494},[100,19507,290],{"class":118},[100,19509,18114],{"class":178},[100,19511,170],{"class":118},[100,19513,206],{"class":205},[100,19515,18094],{"class":209},[100,19517,206],{"class":205},[100,19519,182],{"class":118},[14,19521,19522,19523,290],{},"If you want more detail, see ",[295,19524,19526,9570],{"href":19525},"\u002Fstandard-library\u002Fos.path.exists-function-explained",[17,19527,19482],{},[675,19529,19531],{"id":19530},"if-writing-a-file-check-the-parent-folder","If writing a file, check the parent folder",[91,19533,19535],{"className":93,"code":19534,"language":95,"meta":96,"style":96},"from pathlib import Path\n\nfile_path = Path(\"output\u002Fresult.txt\")\nprint(file_path.parent.exists())\n",[17,19536,19537,19547,19551,19569],{"__ignoreMap":96},[100,19538,19539,19541,19543,19545],{"class":102,"line":103},[100,19540,10201],{"class":145},[100,19542,18068],{"class":106},[100,19544,9259],{"class":145},[100,19546,18073],{"class":106},[100,19548,19549],{"class":102,"line":135},[100,19550,139],{"emptyLinePlaceholder":138},[100,19552,19553,19555,19557,19559,19561,19563,19565,19567],{"class":102,"line":142},[100,19554,18082],{"class":106},[100,19556,111],{"class":110},[100,19558,18087],{"class":178},[100,19560,170],{"class":118},[100,19562,206],{"class":205},[100,19564,18345],{"class":209},[100,19566,206],{"class":205},[100,19568,215],{"class":118},[100,19570,19571,19573,19575,19577,19579,19582,19584,19586],{"class":102,"line":152},[100,19572,372],{"class":114},[100,19574,170],{"class":118},[100,19576,18128],{"class":178},[100,19578,290],{"class":118},[100,19580,19581],{"class":2494},"parent",[100,19583,290],{"class":118},[100,19585,18114],{"class":178},[100,19587,3370],{"class":118},[14,19589,19590,19591,19593],{},"If this prints ",[17,19592,1649],{},", create the folder before writing.",[675,19595,19597],{"id":19596},"avoid-guessing-where-python-is-running-from","Avoid guessing where Python is running from",[14,19599,19600,19601,290],{},"Do not assume Python runs from the same folder as your script. Check it directly with ",[17,19602,19603],{},"os.getcwd()",[77,19605,19607],{"id":19606},"relative-path-vs-absolute-path","Relative path vs absolute path",[14,19609,19610],{},"This causes a lot of confusion for beginners.",[675,19612,19614],{"id":19613},"relative-path","Relative path",[14,19616,19617],{},"A relative path depends on the current working directory.",[14,19619,1844],{},[91,19621,19622],{"className":93,"code":18611,"language":95,"meta":96,"style":96},[17,19623,19624],{"__ignoreMap":96},[100,19625,19626,19628,19630,19632,19634,19636,19638,19640,19642,19644],{"class":102,"line":103},[100,19627,18618],{"class":114},[100,19629,170],{"class":118},[100,19631,206],{"class":205},[100,19633,18094],{"class":209},[100,19635,206],{"class":205},[100,19637,126],{"class":118},[100,19639,1708],{"class":205},[100,19641,17540],{"class":209},[100,19643,206],{"class":205},[100,19645,215],{"class":118},[14,19647,19648],{},"This means:",[40,19650,19651,19656],{},[43,19652,19653,19654],{},"Look for ",[17,19655,18094],{},[43,19657,19658],{},"In the folder Python is currently running from",[675,19660,19662],{"id":19661},"absolute-path","Absolute path",[14,19664,19665],{},"An absolute path gives the full location of the file.",[14,19667,1844],{},[91,19669,19671],{"className":93,"code":19670,"language":95,"meta":96,"style":96},"open(\"\u002Fhome\u002Fuser\u002Fproject\u002Fdata.txt\", \"r\")\n",[17,19672,19673],{"__ignoreMap":96},[100,19674,19675,19677,19679,19681,19684,19686,19688,19690,19692,19694],{"class":102,"line":103},[100,19676,18618],{"class":114},[100,19678,170],{"class":118},[100,19680,206],{"class":205},[100,19682,19683],{"class":209},"\u002Fhome\u002Fuser\u002Fproject\u002Fdata.txt",[100,19685,206],{"class":205},[100,19687,126],{"class":118},[100,19689,1708],{"class":205},[100,19691,17540],{"class":209},[100,19693,206],{"class":205},[100,19695,215],{"class":118},[14,19697,19698],{},"This does not depend on the current working directory.",[675,19700,19702],{"id":19701},"important-beginner-mistake","Important beginner mistake",[14,19704,19705],{},"Many beginners think file paths are relative to the script file.",[14,19707,19708],{},"Often, they are relative to where the program was started instead.",[14,19710,19711,19712,89],{},"If your file is next to your script, you can build the path from ",[17,19713,10041],{},[91,19715,19717],{"className":93,"code":19716,"language":95,"meta":96,"style":96},"from pathlib import Path\n\nscript_folder = Path(__file__).parent\nfile_path = script_folder \u002F \"data.txt\"\n\nwith open(file_path, \"r\", encoding=\"utf-8\") as file:\n    print(file.read())\n",[17,19718,19719,19729,19733,19751,19768,19772,19810],{"__ignoreMap":96},[100,19720,19721,19723,19725,19727],{"class":102,"line":103},[100,19722,10201],{"class":145},[100,19724,18068],{"class":106},[100,19726,9259],{"class":145},[100,19728,18073],{"class":106},[100,19730,19731],{"class":102,"line":135},[100,19732,139],{"emptyLinePlaceholder":138},[100,19734,19735,19738,19740,19742,19744,19746,19748],{"class":102,"line":142},[100,19736,19737],{"class":106},"script_folder ",[100,19739,111],{"class":110},[100,19741,18087],{"class":178},[100,19743,170],{"class":118},[100,19745,10041],{"class":527},[100,19747,5799],{"class":118},[100,19749,19750],{"class":2494},"parent\n",[100,19752,19753,19755,19757,19760,19762,19764,19766],{"class":102,"line":152},[100,19754,18082],{"class":106},[100,19756,111],{"class":110},[100,19758,19759],{"class":106}," script_folder ",[100,19761,2558],{"class":110},[100,19763,1708],{"class":205},[100,19765,18094],{"class":209},[100,19767,1714],{"class":205},[100,19769,19770],{"class":102,"line":164},[100,19771,139],{"emptyLinePlaceholder":138},[100,19773,19774,19776,19778,19780,19782,19784,19786,19788,19790,19792,19794,19796,19798,19800,19802,19804,19806,19808],{"class":102,"line":185},[100,19775,17521],{"class":145},[100,19777,17524],{"class":114},[100,19779,170],{"class":118},[100,19781,18128],{"class":178},[100,19783,126],{"class":118},[100,19785,1708],{"class":205},[100,19787,17540],{"class":209},[100,19789,206],{"class":205},[100,19791,126],{"class":118},[100,19793,18142],{"class":18141},[100,19795,111],{"class":110},[100,19797,206],{"class":205},[100,19799,15307],{"class":209},[100,19801,206],{"class":205},[100,19803,6155],{"class":118},[100,19805,16224],{"class":145},[100,19807,17550],{"class":17549},[100,19809,149],{"class":118},[100,19811,19812,19814,19816,19818,19820,19822],{"class":102,"line":197},[100,19813,200],{"class":114},[100,19815,170],{"class":118},[100,19817,17597],{"class":17549},[100,19819,290],{"class":118},[100,19821,17566],{"class":178},[100,19823,3370],{"class":118},[14,19825,19826],{},"This is one of the safest ways to access files stored beside your script.",[77,19828,19830],{"id":19829},"how-to-prevent-this-error","How to prevent this error",[14,19832,19833],{},"You cannot avoid every missing-file case, but you can make your code much safer.",[675,19835,241,19837],{"id":19836},"use-pathlib",[17,19838,18038],{},[14,19840,19841,19843],{},[17,19842,18038],{}," makes paths easier to read and harder to build incorrectly.",[91,19845,19847],{"className":93,"code":19846,"language":95,"meta":96,"style":96},"from pathlib import Path\n\nfile_path = Path(\"data\") \u002F \"report.txt\"\nprint(file_path.exists())\n",[17,19848,19849,19859,19863,19889],{"__ignoreMap":96},[100,19850,19851,19853,19855,19857],{"class":102,"line":103},[100,19852,10201],{"class":145},[100,19854,18068],{"class":106},[100,19856,9259],{"class":145},[100,19858,18073],{"class":106},[100,19860,19861],{"class":102,"line":135},[100,19862,139],{"emptyLinePlaceholder":138},[100,19864,19865,19867,19869,19871,19873,19875,19877,19879,19881,19883,19885,19887],{"class":102,"line":142},[100,19866,18082],{"class":106},[100,19868,111],{"class":110},[100,19870,18087],{"class":178},[100,19872,170],{"class":118},[100,19874,206],{"class":205},[100,19876,7934],{"class":209},[100,19878,206],{"class":205},[100,19880,6155],{"class":118},[100,19882,17686],{"class":110},[100,19884,1708],{"class":205},[100,19886,18326],{"class":209},[100,19888,1714],{"class":205},[100,19890,19891,19893,19895,19897,19899,19901],{"class":102,"line":152},[100,19892,372],{"class":114},[100,19894,170],{"class":118},[100,19896,18128],{"class":178},[100,19898,290],{"class":118},[100,19900,18114],{"class":178},[100,19902,3370],{"class":118},[675,19904,19906],{"id":19905},"check-file-existence-before-reading","Check file existence before reading",[91,19908,19910],{"className":93,"code":19909,"language":95,"meta":96,"style":96},"from pathlib import Path\n\nfile_path = Path(\"data.txt\")\n\nif file_path.exists():\n    print(file_path.read_text(encoding=\"utf-8\"))\nelse:\n    print(\"File not found\")\n",[17,19911,19912,19922,19926,19944,19948,19960,19988,19994],{"__ignoreMap":96},[100,19913,19914,19916,19918,19920],{"class":102,"line":103},[100,19915,10201],{"class":145},[100,19917,18068],{"class":106},[100,19919,9259],{"class":145},[100,19921,18073],{"class":106},[100,19923,19924],{"class":102,"line":135},[100,19925,139],{"emptyLinePlaceholder":138},[100,19927,19928,19930,19932,19934,19936,19938,19940,19942],{"class":102,"line":142},[100,19929,18082],{"class":106},[100,19931,111],{"class":110},[100,19933,18087],{"class":178},[100,19935,170],{"class":118},[100,19937,206],{"class":205},[100,19939,18094],{"class":209},[100,19941,206],{"class":205},[100,19943,215],{"class":118},[100,19945,19946],{"class":102,"line":152},[100,19947,139],{"emptyLinePlaceholder":138},[100,19949,19950,19952,19954,19956,19958],{"class":102,"line":164},[100,19951,2736],{"class":145},[100,19953,18109],{"class":106},[100,19955,290],{"class":118},[100,19957,18114],{"class":178},[100,19959,1085],{"class":118},[100,19961,19962,19964,19966,19968,19970,19973,19975,19978,19980,19982,19984,19986],{"class":102,"line":185},[100,19963,200],{"class":114},[100,19965,170],{"class":118},[100,19967,18128],{"class":178},[100,19969,290],{"class":118},[100,19971,19972],{"class":178},"read_text",[100,19974,170],{"class":118},[100,19976,19977],{"class":18141},"encoding",[100,19979,111],{"class":110},[100,19981,206],{"class":205},[100,19983,15307],{"class":209},[100,19985,206],{"class":205},[100,19987,182],{"class":118},[100,19989,19990,19992],{"class":102,"line":197},[100,19991,4944],{"class":145},[100,19993,149],{"class":118},[100,19995,19996,19998,20000,20002,20005,20007],{"class":102,"line":771},[100,19997,200],{"class":114},[100,19999,170],{"class":118},[100,20001,206],{"class":205},[100,20003,20004],{"class":209},"File not found",[100,20006,206],{"class":205},[100,20008,215],{"class":118},[675,20010,241,20012,20014],{"id":20011},"use-__file__-when-the-file-is-next-to-your-script",[17,20013,10041],{}," when the file is next to your script",[91,20016,20018],{"className":93,"code":20017,"language":95,"meta":96,"style":96},"from pathlib import Path\n\nbase_dir = Path(__file__).parent\nfile_path = base_dir \u002F \"config.json\"\n\nprint(file_path)\n",[17,20019,20020,20030,20034,20051,20069,20073],{"__ignoreMap":96},[100,20021,20022,20024,20026,20028],{"class":102,"line":103},[100,20023,10201],{"class":145},[100,20025,18068],{"class":106},[100,20027,9259],{"class":145},[100,20029,18073],{"class":106},[100,20031,20032],{"class":102,"line":135},[100,20033,139],{"emptyLinePlaceholder":138},[100,20035,20036,20039,20041,20043,20045,20047,20049],{"class":102,"line":142},[100,20037,20038],{"class":106},"base_dir ",[100,20040,111],{"class":110},[100,20042,18087],{"class":178},[100,20044,170],{"class":118},[100,20046,10041],{"class":527},[100,20048,5799],{"class":118},[100,20050,19750],{"class":2494},[100,20052,20053,20055,20057,20060,20062,20064,20067],{"class":102,"line":152},[100,20054,18082],{"class":106},[100,20056,111],{"class":110},[100,20058,20059],{"class":106}," base_dir ",[100,20061,2558],{"class":110},[100,20063,1708],{"class":205},[100,20065,20066],{"class":209},"config.json",[100,20068,1714],{"class":205},[100,20070,20071],{"class":102,"line":164},[100,20072,139],{"emptyLinePlaceholder":138},[100,20074,20075,20077,20079,20081],{"class":102,"line":185},[100,20076,372],{"class":114},[100,20078,170],{"class":118},[100,20080,18128],{"class":178},[100,20082,215],{"class":118},[675,20084,20086],{"id":20085},"create-missing-folders-before-writing","Create missing folders before writing",[91,20088,20090],{"className":93,"code":20089,"language":95,"meta":96,"style":96},"from pathlib import Path\n\nfolder = Path(\"logs\")\nfolder.mkdir(exist_ok=True)\n\nlog_file = folder \u002F \"app.log\"\nlog_file.write_text(\"Started\\n\", encoding=\"utf-8\")\n",[17,20091,20092,20102,20106,20126,20145,20149,20168],{"__ignoreMap":96},[100,20093,20094,20096,20098,20100],{"class":102,"line":103},[100,20095,10201],{"class":145},[100,20097,18068],{"class":106},[100,20099,9259],{"class":145},[100,20101,18073],{"class":106},[100,20103,20104],{"class":102,"line":135},[100,20105,139],{"emptyLinePlaceholder":138},[100,20107,20108,20111,20113,20115,20117,20119,20122,20124],{"class":102,"line":142},[100,20109,20110],{"class":106},"folder ",[100,20112,111],{"class":110},[100,20114,18087],{"class":178},[100,20116,170],{"class":118},[100,20118,206],{"class":205},[100,20120,20121],{"class":209},"logs",[100,20123,206],{"class":205},[100,20125,215],{"class":118},[100,20127,20128,20131,20133,20135,20137,20139,20141,20143],{"class":102,"line":152},[100,20129,20130],{"class":106},"folder",[100,20132,290],{"class":118},[100,20134,19236],{"class":178},[100,20136,170],{"class":118},[100,20138,19241],{"class":18141},[100,20140,111],{"class":110},[100,20142,19246],{"class":158},[100,20144,215],{"class":118},[100,20146,20147],{"class":102,"line":164},[100,20148,139],{"emptyLinePlaceholder":138},[100,20150,20151,20154,20156,20159,20161,20163,20166],{"class":102,"line":185},[100,20152,20153],{"class":106},"log_file ",[100,20155,111],{"class":110},[100,20157,20158],{"class":106}," folder ",[100,20160,2558],{"class":110},[100,20162,1708],{"class":205},[100,20164,20165],{"class":209},"app.log",[100,20167,1714],{"class":205},[100,20169,20170,20173,20175,20178,20180,20182,20185,20188,20190,20192,20194,20196,20198,20200,20202],{"class":102,"line":197},[100,20171,20172],{"class":106},"log_file",[100,20174,290],{"class":118},[100,20176,20177],{"class":178},"write_text",[100,20179,170],{"class":118},[100,20181,206],{"class":205},[100,20183,20184],{"class":209},"Started",[100,20186,20187],{"class":527},"\\n",[100,20189,206],{"class":205},[100,20191,126],{"class":118},[100,20193,18142],{"class":18141},[100,20195,111],{"class":110},[100,20197,206],{"class":205},[100,20199,15307],{"class":209},[100,20201,206],{"class":205},[100,20203,215],{"class":118},[77,20205,1344],{"id":1343},[14,20207,20208],{},"Here are the most common reasons this error happens:",[40,20210,20211,20214,20221,20224,20227,20230,20233],{},[43,20212,20213],{},"Wrong file name",[43,20215,20216,20217,18659,20219],{},"Wrong file extension such as ",[17,20218,18335],{},[17,20220,18339],{},[43,20222,20223],{},"File is in a different folder",[43,20225,20226],{},"Current working directory is not the script folder",[43,20228,20229],{},"Missing parent directory when writing a file",[43,20231,20232],{},"Incorrect path separators or manually joined path strings",[43,20234,20235],{},"Capitalization mismatch on case-sensitive systems",[77,20237,20239],{"id":20238},"useful-debugging-commands","Useful debugging commands",[14,20241,20242],{},"These short commands can help you find the problem quickly:",[91,20244,20245],{"className":93,"code":19413,"language":95,"meta":96,"style":96},[17,20246,20247,20253],{"__ignoreMap":96},[100,20248,20249,20251],{"class":102,"line":103},[100,20250,9259],{"class":145},[100,20252,18702],{"class":106},[100,20254,20255,20257,20259,20261,20263,20265],{"class":102,"line":135},[100,20256,372],{"class":114},[100,20258,170],{"class":118},[100,20260,18035],{"class":178},[100,20262,290],{"class":118},[100,20264,18719],{"class":178},[100,20266,3370],{"class":118},[91,20268,20270],{"className":93,"code":20269,"language":95,"meta":96,"style":96},"from pathlib import Path\nprint(Path(\"data.txt\").resolve())\n",[17,20271,20272,20282],{"__ignoreMap":96},[100,20273,20274,20276,20278,20280],{"class":102,"line":103},[100,20275,10201],{"class":145},[100,20277,18068],{"class":106},[100,20279,9259],{"class":145},[100,20281,18073],{"class":106},[100,20283,20284,20286,20288,20290,20292,20294,20296,20298,20300,20302],{"class":102,"line":135},[100,20285,372],{"class":114},[100,20287,170],{"class":118},[100,20289,19463],{"class":178},[100,20291,170],{"class":118},[100,20293,206],{"class":205},[100,20295,18094],{"class":209},[100,20297,206],{"class":205},[100,20299,5799],{"class":118},[100,20301,18211],{"class":178},[100,20303,3370],{"class":118},[91,20305,20306],{"className":93,"code":19442,"language":95,"meta":96,"style":96},[17,20307,20308,20318],{"__ignoreMap":96},[100,20309,20310,20312,20314,20316],{"class":102,"line":103},[100,20311,10201],{"class":145},[100,20313,18068],{"class":106},[100,20315,9259],{"class":145},[100,20317,18073],{"class":106},[100,20319,20320,20322,20324,20326,20328,20330,20332,20334,20336,20338],{"class":102,"line":135},[100,20321,372],{"class":114},[100,20323,170],{"class":118},[100,20325,19463],{"class":178},[100,20327,170],{"class":118},[100,20329,206],{"class":205},[100,20331,18094],{"class":209},[100,20333,206],{"class":205},[100,20335,5799],{"class":118},[100,20337,18114],{"class":178},[100,20339,3370],{"class":118},[91,20341,20342],{"className":93,"code":19485,"language":95,"meta":96,"style":96},[17,20343,20344,20350],{"__ignoreMap":96},[100,20345,20346,20348],{"class":102,"line":103},[100,20347,9259],{"class":145},[100,20349,18702],{"class":106},[100,20351,20352,20354,20356,20358,20360,20362,20364,20366,20368,20370,20372,20374],{"class":102,"line":135},[100,20353,372],{"class":114},[100,20355,170],{"class":118},[100,20357,18035],{"class":178},[100,20359,290],{"class":118},[100,20361,19017],{"class":2494},[100,20363,290],{"class":118},[100,20365,18114],{"class":178},[100,20367,170],{"class":118},[100,20369,206],{"class":205},[100,20371,18094],{"class":209},[100,20373,206],{"class":205},[100,20375,182],{"class":118},[91,20377,20379],{"className":93,"code":20378,"language":95,"meta":96,"style":96},"from pathlib import Path\nprint(Path(\"output\").exists())\n",[17,20380,20381,20391],{"__ignoreMap":96},[100,20382,20383,20385,20387,20389],{"class":102,"line":103},[100,20384,10201],{"class":145},[100,20386,18068],{"class":106},[100,20388,9259],{"class":145},[100,20390,18073],{"class":106},[100,20392,20393,20395,20397,20399,20401,20403,20405,20407,20409,20411],{"class":102,"line":135},[100,20394,372],{"class":114},[100,20396,170],{"class":118},[100,20398,19463],{"class":178},[100,20400,170],{"class":118},[100,20402,206],{"class":205},[100,20404,18349],{"class":209},[100,20406,206],{"class":205},[100,20408,5799],{"class":118},[100,20410,18114],{"class":178},[100,20412,3370],{"class":118},[77,20414,1514],{"id":1513},[675,20416,20418],{"id":20417},"why-do-i-get-filenotfounderror-even-though-the-file-is-in-my-project-folder","Why do I get FileNotFoundError even though the file is in my project folder?",[14,20420,20421,20422,20424],{},"Your program may be running from a different working directory. Check ",[17,20423,19603],{}," and compare it to the real file location.",[675,20426,20428,20429,20431],{"id":20427},"does-open-create-a-file-if-it-does-not-exist","Does ",[17,20430,18032],{}," create a file if it does not exist?",[14,20433,20434],{},"It depends on the mode:",[40,20436,20437,20445,20451],{},[43,20438,20439,20442,20443],{},[17,20440,20441],{},"\"r\""," raises ",[17,20444,17489],{},[43,20446,20447,20450],{},[17,20448,20449],{},"\"w\""," creates the file if needed",[43,20452,20453],{},"But the parent folder must already exist",[675,20455,20457],{"id":20456},"what-is-the-difference-between-filenotfounderror-and-permissionerror","What is the difference between FileNotFoundError and PermissionError?",[14,20459,20460,20462],{},[17,20461,17489],{}," means Python cannot find the path.",[14,20464,20465,20467,20468,290],{},[17,20466,17494],{}," means the path exists, but Python is not allowed to access it. See ",[295,20469,20471,20472,20475],{"href":20470},"\u002Ferrors\u002Fpermissionerror-errno-13-permission-denied-fix\u002F","PermissionError: ",[100,20473,20474],{},"Errno 13"," Permission denied (Fix)",[675,20477,20479],{"id":20478},"should-i-use-try-except-for-this-error","Should I use try-except for this error?",[14,20481,20482],{},"Yes, if the file may genuinely be missing.",[14,20484,580],{},[91,20486,20488],{"className":93,"code":20487,"language":95,"meta":96,"style":96},"try:\n    with open(\"data.txt\", \"r\", encoding=\"utf-8\") as file:\n        print(file.read())\nexcept FileNotFoundError:\n    print(\"The file was not found.\")\n",[17,20489,20490,20496,20538,20552,20561],{"__ignoreMap":96},[100,20491,20492,20494],{"class":102,"line":103},[100,20493,146],{"class":145},[100,20495,149],{"class":118},[100,20497,20498,20500,20502,20504,20506,20508,20510,20512,20514,20516,20518,20520,20522,20524,20526,20528,20530,20532,20534,20536],{"class":102,"line":135},[100,20499,18121],{"class":145},[100,20501,17524],{"class":114},[100,20503,170],{"class":118},[100,20505,206],{"class":205},[100,20507,18094],{"class":209},[100,20509,206],{"class":205},[100,20511,126],{"class":118},[100,20513,1708],{"class":205},[100,20515,17540],{"class":209},[100,20517,206],{"class":205},[100,20519,126],{"class":118},[100,20521,18142],{"class":18141},[100,20523,111],{"class":110},[100,20525,206],{"class":205},[100,20527,15307],{"class":209},[100,20529,206],{"class":205},[100,20531,6155],{"class":118},[100,20533,16224],{"class":145},[100,20535,17550],{"class":17549},[100,20537,149],{"class":118},[100,20539,20540,20542,20544,20546,20548,20550],{"class":102,"line":142},[100,20541,167],{"class":114},[100,20543,170],{"class":118},[100,20545,17597],{"class":17549},[100,20547,290],{"class":118},[100,20549,17566],{"class":178},[100,20551,3370],{"class":118},[100,20553,20554,20556,20559],{"class":102,"line":152},[100,20555,188],{"class":145},[100,20557,20558],{"class":191}," FileNotFoundError",[100,20560,149],{"class":118},[100,20562,20563,20565,20567,20569,20572,20574],{"class":102,"line":164},[100,20564,200],{"class":114},[100,20566,170],{"class":118},[100,20568,206],{"class":205},[100,20570,20571],{"class":209},"The file was not found.",[100,20573,206],{"class":205},[100,20575,215],{"class":118},[14,20577,20578,20579,20581],{},"But do not use ",[17,20580,244],{}," as a substitute for checking your path logic. You should still find out why the path is wrong.",[77,20583,1554],{"id":1553},[40,20585,20586,20592,20597,20603,20609,20615,20621,20627,20637],{},[43,20587,20588],{},[295,20589,20591],{"href":20590},"\u002Fhow-to\u002Fhow-to-read-a-file-in-python\u002F","How to read a file in Python",[43,20593,20594],{},[295,20595,20596],{"href":18285},"Working with file paths in Python",[43,20598,20599],{},[295,20600,1572,20601,1576],{"href":18280},[17,20602,18032],{},[43,20604,20605],{},[295,20606,1572,20607,18736],{"href":18733},[17,20608,18035],{},[43,20610,20611],{},[295,20612,20613,1576],{"href":19525},[17,20614,19482],{},[43,20616,20617],{},[295,20618,20619,1576],{"href":19102},[17,20620,18988],{},[43,20622,20623],{},[295,20624,20471,20625,20475],{"href":20470},[100,20626,20474],{},[43,20628,20629],{},[295,20630,20632,20633,20636],{"href":20631},"\u002Ferrors\u002Fisadirectoryerror-errno-21-is-a-directory-fix\u002F","IsADirectoryError: ",[100,20634,20635],{},"Errno 21"," Is a directory (Fix)",[43,20638,20639],{},[295,20640,20642],{"href":20641},"\u002Ferrors\u002Ffilenotfounderror-in-python-causes-and-fixes\u002F","FileNotFoundError in Python: causes and fixes",[1589,20644,20645],{},"html pre.shiki code .sVHd0, html code.shiki .sVHd0{--shiki-light:#39ADB5;--shiki-light-font-style:italic;--shiki-default:#D73A49;--shiki-default-font-style:inherit;--shiki-dark:#F97583;--shiki-dark-font-style:inherit}html pre.shiki code .su5hD, html code.shiki .su5hD{--shiki-light:#90A4AE;--shiki-default:#24292E;--shiki-dark:#E1E4E8}html pre.shiki code .smGrS, html code.shiki .smGrS{--shiki-light:#39ADB5;--shiki-default:#D73A49;--shiki-dark:#F97583}html pre.shiki code .slqww, html code.shiki .slqww{--shiki-light:#6182B8;--shiki-default:#24292E;--shiki-dark:#E1E4E8}html pre.shiki code .sP7_E, html code.shiki .sP7_E{--shiki-light:#39ADB5;--shiki-default:#24292E;--shiki-dark:#E1E4E8}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 .s99_P, html code.shiki .s99_P{--shiki-light:#90A4AE;--shiki-light-font-style:italic;--shiki-default:#E36209;--shiki-default-font-style:inherit;--shiki-dark:#FFAB70;--shiki-dark-font-style:inherit}html pre.shiki code .sMMDD, html code.shiki .sMMDD{--shiki-light:#90A4AE;--shiki-default:#E36209;--shiki-dark:#FFAB70}html pre.shiki code .sbsja, html code.shiki .sbsja{--shiki-light:#9C3EDA;--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 .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 .skxfh, html code.shiki .skxfh{--shiki-light:#E53935;--shiki-default:#24292E;--shiki-dark:#E1E4E8}html pre.shiki code .s39Yj, html code.shiki .s39Yj{--shiki-light:#39ADB5;--shiki-default:#005CC5;--shiki-dark:#79B8FF}html pre.shiki code .s_hVV, html code.shiki .s_hVV{--shiki-light:#90A4AE;--shiki-default:#005CC5;--shiki-dark:#79B8FF}",{"title":96,"searchDepth":135,"depth":135,"links":20647},[20648,20649,20650,20651,20659,20666,20671,20679,20680,20681,20688],{"id":5880,"depth":135,"text":5881},{"id":18289,"depth":135,"text":18290},{"id":11030,"depth":135,"text":11031},{"id":6243,"depth":135,"text":6244,"children":20652},[20653,20654,20655,20656,20657,20658],{"id":18601,"depth":142,"text":18602},{"id":18645,"depth":142,"text":18646},{"id":18683,"depth":142,"text":18684},{"id":18739,"depth":142,"text":18740},{"id":18873,"depth":142,"text":18874},{"id":19107,"depth":142,"text":19108},{"id":19333,"depth":135,"text":19334,"children":20660},[20661,20662,20663,20664,20665],{"id":19340,"depth":142,"text":19341},{"id":19409,"depth":142,"text":19410},{"id":19438,"depth":142,"text":19439},{"id":19530,"depth":142,"text":19531},{"id":19596,"depth":142,"text":19597},{"id":19606,"depth":135,"text":19607,"children":20667},[20668,20669,20670],{"id":19613,"depth":142,"text":19614},{"id":19661,"depth":142,"text":19662},{"id":19701,"depth":142,"text":19702},{"id":19829,"depth":135,"text":19830,"children":20672},[20673,20675,20676,20678],{"id":19836,"depth":142,"text":20674},"Use pathlib",{"id":19905,"depth":142,"text":19906},{"id":20011,"depth":142,"text":20677},"Use __file__ when the file is next to your script",{"id":20085,"depth":142,"text":20086},{"id":1343,"depth":135,"text":1344},{"id":20238,"depth":135,"text":20239},{"id":1513,"depth":135,"text":1514,"children":20682},[20683,20684,20686,20687],{"id":20417,"depth":142,"text":20418},{"id":20427,"depth":142,"text":20685},"Does open() create a file if it does not exist?",{"id":20456,"depth":142,"text":20457},{"id":20478,"depth":142,"text":20479},{"id":1553,"depth":135,"text":1554},"Master filenotfounderror errno 2 no such file or directory fix in our comprehensive Python beginner guide.",{},"\u002Ferrors\u002Ffilenotfounderror-errno-2-no-such-file-or-directory-fix",{"title":18010,"description":20689},"errors\u002Ffilenotfounderror-errno-2-no-such-file-or-directory-fix","qJC1LyTxKs22SEV5n8fq35n0dJ58p_QYJ122KpTY23w",{"id":20696,"title":20697,"body":20698,"description":23201,"extension":1623,"meta":23202,"navigation":138,"path":23203,"seo":23204,"stem":23205,"__hash__":23206},"content\u002Ferrors\u002Ffilenotfounderror-in-python-causes-and-fixes.md","FileNotFoundError in Python: Causes and Fixes",{"type":7,"value":20699,"toc":23165},[20700,20703,20708,20719,20725,20727,20730,20876,20879,20895,20898,20900,20905,20908,20925,20928,20948,20952,20955,20975,20977,20979,21050,21055,21169,21172,21196,21202,21212,21216,21219,21222,21243,21246,21266,21271,21275,21280,21283,21295,21298,21358,21361,21423,21426,21521,21524,21548,21553,21583,21588,21592,21595,21603,21607,21707,21711,21815,21818,21829,21835,21839,21842,21846,21935,21939,22012,22018,22026,22116,22126,22130,22133,22137,22140,22202,22209,22213,22219,22284,22290,22296,22362,22365,22500,22503,22651,22659,22663,22669,22686,22688,22712,22740,22776,22812,22848,22874,22877,23006,23009,23013,23016,23030,23036,23040,23046,23049,23052,23059,23062,23065,23071,23074,23077,23083,23086,23089,23091,23095,23101,23105,23114,23118,23121,23125,23128,23132,23135,23137,23162],[10,20701,20697],{"id":20702},"filenotfounderror-in-python-causes-and-fixes",[14,20704,20705,20707],{},[17,20706,17489],{}," means Python could not find the file or folder you asked it to use.",[14,20709,20710,20711,5870,20713,5870,20716,20718],{},"This error often appears when you use ",[17,20712,18032],{},[17,20714,20715],{},"os.rename()",[17,20717,18264],{},", or other file operations. In most cases, the problem is not Python itself. The problem is the path.",[14,20720,20721,20722,10873],{},"A very common beginner mistake is thinking Python looks for files in the same folder as the script. Sometimes it does, but often Python uses the ",[22,20723,20724],{},"current working directory",[77,20726,80],{"id":79},[14,20728,20729],{},"If you want a fast way to avoid this error while debugging, check whether the file exists before opening it:",[91,20731,20732],{"className":93,"code":18059,"language":95,"meta":96,"style":96},[17,20733,20734,20744,20748,20766,20770,20782,20820,20834,20844,20850],{"__ignoreMap":96},[100,20735,20736,20738,20740,20742],{"class":102,"line":103},[100,20737,10201],{"class":145},[100,20739,18068],{"class":106},[100,20741,9259],{"class":145},[100,20743,18073],{"class":106},[100,20745,20746],{"class":102,"line":135},[100,20747,139],{"emptyLinePlaceholder":138},[100,20749,20750,20752,20754,20756,20758,20760,20762,20764],{"class":102,"line":142},[100,20751,18082],{"class":106},[100,20753,111],{"class":110},[100,20755,18087],{"class":178},[100,20757,170],{"class":118},[100,20759,206],{"class":205},[100,20761,18094],{"class":209},[100,20763,206],{"class":205},[100,20765,215],{"class":118},[100,20767,20768],{"class":102,"line":152},[100,20769,139],{"emptyLinePlaceholder":138},[100,20771,20772,20774,20776,20778,20780],{"class":102,"line":164},[100,20773,2736],{"class":145},[100,20775,18109],{"class":106},[100,20777,290],{"class":118},[100,20779,18114],{"class":178},[100,20781,1085],{"class":118},[100,20783,20784,20786,20788,20790,20792,20794,20796,20798,20800,20802,20804,20806,20808,20810,20812,20814,20816,20818],{"class":102,"line":185},[100,20785,18121],{"class":145},[100,20787,17524],{"class":114},[100,20789,170],{"class":118},[100,20791,18128],{"class":178},[100,20793,126],{"class":118},[100,20795,1708],{"class":205},[100,20797,17540],{"class":209},[100,20799,206],{"class":205},[100,20801,126],{"class":118},[100,20803,18142],{"class":18141},[100,20805,111],{"class":110},[100,20807,206],{"class":205},[100,20809,15307],{"class":209},[100,20811,206],{"class":205},[100,20813,6155],{"class":118},[100,20815,16224],{"class":145},[100,20817,17550],{"class":17549},[100,20819,149],{"class":118},[100,20821,20822,20824,20826,20828,20830,20832],{"class":102,"line":197},[100,20823,18163],{"class":106},[100,20825,111],{"class":110},[100,20827,17550],{"class":17549},[100,20829,290],{"class":118},[100,20831,17566],{"class":178},[100,20833,4734],{"class":118},[100,20835,20836,20838,20840,20842],{"class":102,"line":771},[100,20837,167],{"class":114},[100,20839,170],{"class":118},[100,20841,18182],{"class":178},[100,20843,215],{"class":118},[100,20845,20846,20848],{"class":102,"line":787},[100,20847,4944],{"class":145},[100,20849,149],{"class":118},[100,20851,20852,20854,20856,20858,20860,20862,20864,20866,20868,20870,20872,20874],{"class":102,"line":5816},[100,20853,200],{"class":114},[100,20855,170],{"class":118},[100,20857,18199],{"class":1077},[100,20859,18202],{"class":209},[100,20861,16490],{"class":122},[100,20863,18128],{"class":178},[100,20865,290],{"class":118},[100,20867,18211],{"class":178},[100,20869,7562],{"class":118},[100,20871,18216],{"class":122},[100,20873,206],{"class":209},[100,20875,215],{"class":118},[14,20877,20878],{},"This code:",[40,20880,20881,20886,20889,20892],{},[43,20882,20883,20884],{},"creates a path for ",[17,20885,18094],{},[43,20887,20888],{},"checks whether that file exists",[43,20890,20891],{},"reads it only if Python can find it",[43,20893,20894],{},"prints the full path when debugging",[14,20896,20897],{},"Use the correct path, check that the file really exists, and print the full path when debugging.",[77,20899,5881],{"id":5880},[14,20901,20902,20904],{},[17,20903,17489],{}," happens when Python cannot find the file or folder you asked for.",[14,20906,20907],{},"This commonly happens with:",[40,20909,20910,20914,20918,20922],{},[43,20911,20912],{},[17,20913,18032],{},[43,20915,20916],{},[17,20917,20715],{},[43,20919,20920],{},[17,20921,18264],{},[43,20923,20924],{},"other file and folder operations",[14,20926,20927],{},"Usually, one of these is true:",[40,20929,20930,20933,20936,20939,20942,20945],{},[43,20931,20932],{},"the file does not exist",[43,20934,20935],{},"the path is wrong",[43,20937,20938],{},"the code is running from a different folder than you expected",[43,20940,20941],{},"the file name or extension is misspelled",[43,20943,20944],{},"a folder in the path does not exist",[43,20946,20947],{},"the file was moved or deleted",[77,20949,20951],{"id":20950},"when-filenotfounderror-happens","When FileNotFoundError happens",[14,20953,20954],{},"You will usually see this error in situations like these:",[40,20956,20957,20960,20963,20966,20969,20972],{},[43,20958,20959],{},"Trying to open a file that does not exist",[43,20961,20962],{},"Using a relative path from the wrong folder",[43,20964,20965],{},"Misspelling the file name or extension",[43,20967,20968],{},"Using the wrong slash direction or an invalid path string",[43,20970,20971],{},"Trying to access a folder or drive that is not available",[43,20973,20974],{},"Using a path to a file that was moved, renamed, or deleted",[77,20976,11031],{"id":11030},[14,20978,1891],{},[91,20980,20982],{"className":93,"code":20981,"language":95,"meta":96,"style":96},"with open(\"data.txt\", \"r\", encoding=\"utf-8\") as file:\n    content = file.read()\n    print(content)\n",[17,20983,20984,21026,21040],{"__ignoreMap":96},[100,20985,20986,20988,20990,20992,20994,20996,20998,21000,21002,21004,21006,21008,21010,21012,21014,21016,21018,21020,21022,21024],{"class":102,"line":103},[100,20987,17521],{"class":145},[100,20989,17524],{"class":114},[100,20991,170],{"class":118},[100,20993,206],{"class":205},[100,20995,18094],{"class":209},[100,20997,206],{"class":205},[100,20999,126],{"class":118},[100,21001,1708],{"class":205},[100,21003,17540],{"class":209},[100,21005,206],{"class":205},[100,21007,126],{"class":118},[100,21009,18142],{"class":18141},[100,21011,111],{"class":110},[100,21013,206],{"class":205},[100,21015,15307],{"class":209},[100,21017,206],{"class":205},[100,21019,6155],{"class":118},[100,21021,16224],{"class":145},[100,21023,17550],{"class":17549},[100,21025,149],{"class":118},[100,21027,21028,21030,21032,21034,21036,21038],{"class":102,"line":135},[100,21029,18414],{"class":106},[100,21031,111],{"class":110},[100,21033,17550],{"class":17549},[100,21035,290],{"class":118},[100,21037,17566],{"class":178},[100,21039,4734],{"class":118},[100,21041,21042,21044,21046,21048],{"class":102,"line":142},[100,21043,200],{"class":114},[100,21045,170],{"class":118},[100,21047,18182],{"class":178},[100,21049,215],{"class":118},[14,21051,11734,21052,21054],{},[17,21053,18094],{}," is not in the location Python is searching, you may get an error like this:",[91,21056,21058],{"className":93,"code":21057,"language":95,"meta":96,"style":96},"Traceback (most recent call last):\n  File \"main.py\", line 1, in \u003Cmodule>\n    with open(\"data.txt\", \"r\", encoding=\"utf-8\") as file:\nFileNotFoundError: [Errno 2] No such file or directory: 'data.txt'\n",[17,21059,21060,21070,21097,21139],{"__ignoreMap":96},[100,21061,21062,21064,21066,21068],{"class":102,"line":103},[100,21063,514],{"class":178},[100,21065,170],{"class":118},[100,21067,519],{"class":178},[100,21069,522],{"class":118},[100,21071,21072,21074,21076,21079,21081,21083,21085,21087,21089,21091,21093,21095],{"class":102,"line":135},[100,21073,1952],{"class":106},[100,21075,206],{"class":205},[100,21077,21078],{"class":209},"main.py",[100,21080,206],{"class":205},[100,21082,126],{"class":118},[100,21084,1964],{"class":106},[100,21086,123],{"class":122},[100,21088,126],{"class":118},[100,21090,1971],{"class":110},[100,21092,1974],{"class":110},[100,21094,1977],{"class":106},[100,21096,1980],{"class":110},[100,21098,21099,21101,21103,21105,21107,21109,21111,21113,21115,21117,21119,21121,21123,21125,21127,21129,21131,21133,21135,21137],{"class":102,"line":142},[100,21100,18121],{"class":145},[100,21102,17524],{"class":114},[100,21104,170],{"class":118},[100,21106,206],{"class":205},[100,21108,18094],{"class":209},[100,21110,206],{"class":205},[100,21112,126],{"class":118},[100,21114,1708],{"class":205},[100,21116,17540],{"class":209},[100,21118,206],{"class":205},[100,21120,126],{"class":118},[100,21122,18142],{"class":18141},[100,21124,111],{"class":110},[100,21126,206],{"class":205},[100,21128,15307],{"class":209},[100,21130,206],{"class":205},[100,21132,6155],{"class":118},[100,21134,16224],{"class":145},[100,21136,17550],{"class":17549},[100,21138,149],{"class":118},[100,21140,21141,21143,21145,21147,21149,21151,21153,21155,21157,21159,21161,21163,21165,21167],{"class":102,"line":152},[100,21142,17489],{"class":191},[100,21144,89],{"class":118},[100,21146,594],{"class":118},[100,21148,17586],{"class":106},[100,21150,559],{"class":122},[100,21152,17591],{"class":118},[100,21154,17594],{"class":106},[100,21156,17597],{"class":17549},[100,21158,17600],{"class":110},[100,21160,17603],{"class":106},[100,21162,89],{"class":118},[100,21164,1274],{"class":205},[100,21166,18094],{"class":209},[100,21168,3925],{"class":205},[14,21170,21171],{},"The important part is this:",[91,21173,21175],{"className":93,"code":21174,"language":95,"meta":96,"style":96},"No such file or directory: 'data.txt'\n",[17,21176,21177],{"__ignoreMap":96},[100,21178,21179,21182,21184,21186,21188,21190,21192,21194],{"class":102,"line":103},[100,21180,21181],{"class":106},"No such ",[100,21183,17597],{"class":17549},[100,21185,17600],{"class":110},[100,21187,17603],{"class":106},[100,21189,89],{"class":118},[100,21191,1274],{"class":205},[100,21193,18094],{"class":209},[100,21195,3925],{"class":205},[14,21197,21198,21199,21201],{},"That tells you the exact path Python failed to find. In this example, Python was looking for a file named ",[17,21200,18094],{}," relative to the current working directory.",[14,21203,21204,21205,3266,21208,290],{},"If you are new to file handling, see ",[295,21206,21207],{"href":20590},"how to read a file in Python",[295,21209,1572,21210,9570],{"href":18280},[17,21211,18032],{},[77,21213,21215],{"id":21214},"fix-1-check-the-file-name-and-extension","Fix 1: Check the file name and extension",[14,21217,21218],{},"Start with the simplest check.",[14,21220,21221],{},"Make sure:",[40,21223,21224,21227,21237,21240],{},[43,21225,21226],{},"the file name matches exactly",[43,21228,21229,21230,5870,21232,5894,21234],{},"the extension is correct, such as ",[17,21231,18335],{},[17,21233,18339],{},[17,21235,21236],{},".json",[43,21238,21239],{},"there are no extra spaces",[43,21241,21242],{},"uppercase and lowercase letters match on systems where case matters",[14,21244,21245],{},"For example, these are different names:",[40,21247,21248,21252,21256,21261],{},[43,21249,21250],{},[17,21251,18094],{},[43,21253,21254],{},[17,21255,18662],{},[43,21257,21258],{},[17,21259,21260],{},"data.csv",[43,21262,21263],{},[17,21264,21265],{},"data.txt ",[14,21267,21268,21269,290],{},"A tiny difference is enough to cause ",[17,21270,17489],{},[77,21272,21274],{"id":21273},"fix-2-use-the-correct-path","Fix 2: Use the correct path",[14,21276,571,21277,21279],{},[22,21278,19017],{}," is the location of a file or folder.",[14,21281,21282],{},"There are two main kinds:",[40,21284,21285,21290],{},[43,21286,21287,21289],{},[22,21288,19614],{},": starts from the current working directory",[43,21291,21292,21294],{},[22,21293,19662],{},": gives the full location on your computer",[14,21296,21297],{},"Example of a relative path:",[91,21299,21300],{"className":93,"code":18746,"language":95,"meta":96,"style":96},[17,21301,21302,21344],{"__ignoreMap":96},[100,21303,21304,21306,21308,21310,21312,21314,21316,21318,21320,21322,21324,21326,21328,21330,21332,21334,21336,21338,21340,21342],{"class":102,"line":103},[100,21305,17521],{"class":145},[100,21307,17524],{"class":114},[100,21309,170],{"class":118},[100,21311,206],{"class":205},[100,21313,18761],{"class":209},[100,21315,206],{"class":205},[100,21317,126],{"class":118},[100,21319,1708],{"class":205},[100,21321,17540],{"class":209},[100,21323,206],{"class":205},[100,21325,126],{"class":118},[100,21327,18142],{"class":18141},[100,21329,111],{"class":110},[100,21331,206],{"class":205},[100,21333,15307],{"class":209},[100,21335,206],{"class":205},[100,21337,6155],{"class":118},[100,21339,16224],{"class":145},[100,21341,17550],{"class":17549},[100,21343,149],{"class":118},[100,21345,21346,21348,21350,21352,21354,21356],{"class":102,"line":135},[100,21347,200],{"class":114},[100,21349,170],{"class":118},[100,21351,17597],{"class":17549},[100,21353,290],{"class":118},[100,21355,17566],{"class":178},[100,21357,3370],{"class":118},[14,21359,21360],{},"Example of an absolute path:",[91,21362,21364],{"className":93,"code":21363,"language":95,"meta":96,"style":96},"with open(\"\u002Fhome\u002Fuser\u002Fproject\u002Ffiles\u002Fdata.txt\", \"r\", encoding=\"utf-8\") as file:\n    print(file.read())\n",[17,21365,21366,21409],{"__ignoreMap":96},[100,21367,21368,21370,21372,21374,21376,21379,21381,21383,21385,21387,21389,21391,21393,21395,21397,21399,21401,21403,21405,21407],{"class":102,"line":103},[100,21369,17521],{"class":145},[100,21371,17524],{"class":114},[100,21373,170],{"class":118},[100,21375,206],{"class":205},[100,21377,21378],{"class":209},"\u002Fhome\u002Fuser\u002Fproject\u002Ffiles\u002Fdata.txt",[100,21380,206],{"class":205},[100,21382,126],{"class":118},[100,21384,1708],{"class":205},[100,21386,17540],{"class":209},[100,21388,206],{"class":205},[100,21390,126],{"class":118},[100,21392,18142],{"class":18141},[100,21394,111],{"class":110},[100,21396,206],{"class":205},[100,21398,15307],{"class":209},[100,21400,206],{"class":205},[100,21402,6155],{"class":118},[100,21404,16224],{"class":145},[100,21406,17550],{"class":17549},[100,21408,149],{"class":118},[100,21410,21411,21413,21415,21417,21419,21421],{"class":102,"line":135},[100,21412,200],{"class":114},[100,21414,170],{"class":118},[100,21416,17597],{"class":17549},[100,21418,290],{"class":118},[100,21420,17566],{"class":178},[100,21422,3370],{"class":118},[14,21424,21425],{},"On Windows, an absolute path may look like this:",[91,21427,21429],{"className":93,"code":21428,"language":95,"meta":96,"style":96},"with open(r\"C:\\Users\\YourName\\Documents\\data.txt\", \"r\", encoding=\"utf-8\") as file:\n    print(file.read())\n",[17,21430,21431,21507],{"__ignoreMap":96},[100,21432,21433,21435,21437,21439,21441,21443,21447,21451,21454,21457,21460,21463,21466,21469,21472,21474,21477,21479,21481,21483,21485,21487,21489,21491,21493,21495,21497,21499,21501,21503,21505],{"class":102,"line":103},[100,21434,17521],{"class":145},[100,21436,17524],{"class":114},[100,21438,170],{"class":118},[100,21440,17540],{"class":1077},[100,21442,206],{"class":205},[100,21444,21446],{"class":21445},"sQRbd","C:",[100,21448,21450],{"class":21449},"sjYin","\\U",[100,21452,21453],{"class":21445},"sers",[100,21455,21456],{"class":21449},"\\Y",[100,21458,21459],{"class":21445},"ourName",[100,21461,21462],{"class":10364},"\\D",[100,21464,21465],{"class":21445},"ocuments",[100,21467,21468],{"class":10364},"\\d",[100,21470,21471],{"class":21445},"ata",[100,21473,290],{"class":10364},[100,21475,21476],{"class":21445},"txt",[100,21478,206],{"class":205},[100,21480,126],{"class":118},[100,21482,1708],{"class":205},[100,21484,17540],{"class":209},[100,21486,206],{"class":205},[100,21488,126],{"class":118},[100,21490,18142],{"class":18141},[100,21492,111],{"class":110},[100,21494,206],{"class":205},[100,21496,15307],{"class":209},[100,21498,206],{"class":205},[100,21500,6155],{"class":118},[100,21502,16224],{"class":145},[100,21504,17550],{"class":17549},[100,21506,149],{"class":118},[100,21508,21509,21511,21513,21515,21517,21519],{"class":102,"line":135},[100,21510,200],{"class":114},[100,21512,170],{"class":118},[100,21514,17597],{"class":17549},[100,21516,290],{"class":118},[100,21518,17566],{"class":178},[100,21520,3370],{"class":118},[14,21522,21523],{},"To see where Python is looking from, print the current working directory:",[91,21525,21526],{"className":93,"code":19413,"language":95,"meta":96,"style":96},[17,21527,21528,21534],{"__ignoreMap":96},[100,21529,21530,21532],{"class":102,"line":103},[100,21531,9259],{"class":145},[100,21533,18702],{"class":106},[100,21535,21536,21538,21540,21542,21544,21546],{"class":102,"line":135},[100,21537,372],{"class":114},[100,21539,170],{"class":118},[100,21541,18035],{"class":178},[100,21543,290],{"class":118},[100,21545,18719],{"class":178},[100,21547,3370],{"class":118},[14,21549,21550,21551,89],{},"Or with ",[17,21552,18038],{},[91,21554,21556],{"className":93,"code":21555,"language":95,"meta":96,"style":96},"from pathlib import Path\nprint(Path.cwd())\n",[17,21557,21558,21568],{"__ignoreMap":96},[100,21559,21560,21562,21564,21566],{"class":102,"line":103},[100,21561,10201],{"class":145},[100,21563,18068],{"class":106},[100,21565,9259],{"class":145},[100,21567,18073],{"class":106},[100,21569,21570,21572,21574,21576,21578,21581],{"class":102,"line":135},[100,21571,372],{"class":114},[100,21573,170],{"class":118},[100,21575,19463],{"class":178},[100,21577,290],{"class":118},[100,21579,21580],{"class":178},"cwd",[100,21582,3370],{"class":118},[14,21584,21585,21586,16458],{},"If this is confusing, a full guide to ",[295,21587,18286],{"href":18285},[77,21589,21591],{"id":21590},"fix-3-build-paths-safely","Fix 3: Build paths safely",[14,21593,21594],{},"Typing long paths by hand often causes mistakes.",[14,21596,21597,21598,3178,21601,290],{},"A better option is to use ",[17,21599,21600],{},"pathlib.Path",[17,21602,18988],{},[675,21604,21606],{"id":21605},"using-pathlib","Using pathlib",[91,21608,21609],{"className":93,"code":18884,"language":95,"meta":96,"style":96},[17,21610,21611,21621,21625,21651,21655,21693],{"__ignoreMap":96},[100,21612,21613,21615,21617,21619],{"class":102,"line":103},[100,21614,10201],{"class":145},[100,21616,18068],{"class":106},[100,21618,9259],{"class":145},[100,21620,18073],{"class":106},[100,21622,21623],{"class":102,"line":135},[100,21624,139],{"emptyLinePlaceholder":138},[100,21626,21627,21629,21631,21633,21635,21637,21639,21641,21643,21645,21647,21649],{"class":102,"line":142},[100,21628,18082],{"class":106},[100,21630,111],{"class":110},[100,21632,18087],{"class":178},[100,21634,170],{"class":118},[100,21636,206],{"class":205},[100,21638,18915],{"class":209},[100,21640,206],{"class":205},[100,21642,6155],{"class":118},[100,21644,17686],{"class":110},[100,21646,1708],{"class":205},[100,21648,18094],{"class":209},[100,21650,1714],{"class":205},[100,21652,21653],{"class":102,"line":152},[100,21654,139],{"emptyLinePlaceholder":138},[100,21656,21657,21659,21661,21663,21665,21667,21669,21671,21673,21675,21677,21679,21681,21683,21685,21687,21689,21691],{"class":102,"line":164},[100,21658,17521],{"class":145},[100,21660,17524],{"class":114},[100,21662,170],{"class":118},[100,21664,18128],{"class":178},[100,21666,126],{"class":118},[100,21668,1708],{"class":205},[100,21670,17540],{"class":209},[100,21672,206],{"class":205},[100,21674,126],{"class":118},[100,21676,18142],{"class":18141},[100,21678,111],{"class":110},[100,21680,206],{"class":205},[100,21682,15307],{"class":209},[100,21684,206],{"class":205},[100,21686,6155],{"class":118},[100,21688,16224],{"class":145},[100,21690,17550],{"class":17549},[100,21692,149],{"class":118},[100,21694,21695,21697,21699,21701,21703,21705],{"class":102,"line":185},[100,21696,200],{"class":114},[100,21698,170],{"class":118},[100,21700,17597],{"class":17549},[100,21702,290],{"class":118},[100,21704,17566],{"class":178},[100,21706,3370],{"class":118},[675,21708,21710],{"id":21709},"using-ospathjoin","Using os.path.join()",[91,21712,21713],{"className":93,"code":18991,"language":95,"meta":96,"style":96},[17,21714,21715,21721,21725,21759,21763,21801],{"__ignoreMap":96},[100,21716,21717,21719],{"class":102,"line":103},[100,21718,9259],{"class":145},[100,21720,18702],{"class":106},[100,21722,21723],{"class":102,"line":135},[100,21724,139],{"emptyLinePlaceholder":138},[100,21726,21727,21729,21731,21733,21735,21737,21739,21741,21743,21745,21747,21749,21751,21753,21755,21757],{"class":102,"line":142},[100,21728,18082],{"class":106},[100,21730,111],{"class":110},[100,21732,19012],{"class":106},[100,21734,290],{"class":118},[100,21736,19017],{"class":2494},[100,21738,290],{"class":118},[100,21740,19022],{"class":178},[100,21742,170],{"class":118},[100,21744,206],{"class":205},[100,21746,18915],{"class":209},[100,21748,206],{"class":205},[100,21750,126],{"class":118},[100,21752,1708],{"class":205},[100,21754,18094],{"class":209},[100,21756,206],{"class":205},[100,21758,215],{"class":118},[100,21760,21761],{"class":102,"line":152},[100,21762,139],{"emptyLinePlaceholder":138},[100,21764,21765,21767,21769,21771,21773,21775,21777,21779,21781,21783,21785,21787,21789,21791,21793,21795,21797,21799],{"class":102,"line":164},[100,21766,17521],{"class":145},[100,21768,17524],{"class":114},[100,21770,170],{"class":118},[100,21772,18128],{"class":178},[100,21774,126],{"class":118},[100,21776,1708],{"class":205},[100,21778,17540],{"class":209},[100,21780,206],{"class":205},[100,21782,126],{"class":118},[100,21784,18142],{"class":18141},[100,21786,111],{"class":110},[100,21788,206],{"class":205},[100,21790,15307],{"class":209},[100,21792,206],{"class":205},[100,21794,6155],{"class":118},[100,21796,16224],{"class":145},[100,21798,17550],{"class":17549},[100,21800,149],{"class":118},[100,21802,21803,21805,21807,21809,21811,21813],{"class":102,"line":185},[100,21804,200],{"class":114},[100,21806,170],{"class":118},[100,21808,17597],{"class":17549},[100,21810,290],{"class":118},[100,21812,17566],{"class":178},[100,21814,3370],{"class":118},[14,21816,21817],{},"These approaches help because they:",[40,21819,21820,21823,21826],{},[43,21821,21822],{},"reduce slash mistakes",[43,21824,21825],{},"make paths easier to read",[43,21827,21828],{},"make code easier to update later",[14,21830,21831,21832,290],{},"If you want to learn this style, see ",[295,21833,21834],{"href":19102},"os.path.join() explained",[77,21836,21838],{"id":21837},"fix-4-check-that-the-file-exists-before-using-it","Fix 4: Check that the file exists before using it",[14,21840,21841],{},"If a file may or may not exist, check first.",[675,21843,21845],{"id":21844},"with-pathlib","With pathlib",[91,21847,21849],{"className":93,"code":21848,"language":95,"meta":96,"style":96},"from pathlib import Path\n\nfile_path = Path(\"data.txt\")\n\nif file_path.exists():\n    print(\"File exists\")\nelse:\n    print(\"File does not exist\")\n",[17,21850,21851,21861,21865,21883,21887,21899,21914,21920],{"__ignoreMap":96},[100,21852,21853,21855,21857,21859],{"class":102,"line":103},[100,21854,10201],{"class":145},[100,21856,18068],{"class":106},[100,21858,9259],{"class":145},[100,21860,18073],{"class":106},[100,21862,21863],{"class":102,"line":135},[100,21864,139],{"emptyLinePlaceholder":138},[100,21866,21867,21869,21871,21873,21875,21877,21879,21881],{"class":102,"line":142},[100,21868,18082],{"class":106},[100,21870,111],{"class":110},[100,21872,18087],{"class":178},[100,21874,170],{"class":118},[100,21876,206],{"class":205},[100,21878,18094],{"class":209},[100,21880,206],{"class":205},[100,21882,215],{"class":118},[100,21884,21885],{"class":102,"line":152},[100,21886,139],{"emptyLinePlaceholder":138},[100,21888,21889,21891,21893,21895,21897],{"class":102,"line":164},[100,21890,2736],{"class":145},[100,21892,18109],{"class":106},[100,21894,290],{"class":118},[100,21896,18114],{"class":178},[100,21898,1085],{"class":118},[100,21900,21901,21903,21905,21907,21910,21912],{"class":102,"line":185},[100,21902,200],{"class":114},[100,21904,170],{"class":118},[100,21906,206],{"class":205},[100,21908,21909],{"class":209},"File exists",[100,21911,206],{"class":205},[100,21913,215],{"class":118},[100,21915,21916,21918],{"class":102,"line":197},[100,21917,4944],{"class":145},[100,21919,149],{"class":118},[100,21921,21922,21924,21926,21928,21931,21933],{"class":102,"line":771},[100,21923,200],{"class":114},[100,21925,170],{"class":118},[100,21927,206],{"class":205},[100,21929,21930],{"class":209},"File does not exist",[100,21932,206],{"class":205},[100,21934,215],{"class":118},[675,21936,21938],{"id":21937},"with-ospathexists","With os.path.exists()",[91,21940,21942],{"className":93,"code":21941,"language":95,"meta":96,"style":96},"import os\n\nif os.path.exists(\"data.txt\"):\n    print(\"File exists\")\nelse:\n    print(\"File does not exist\")\n",[17,21943,21944,21950,21954,21978,21992,21998],{"__ignoreMap":96},[100,21945,21946,21948],{"class":102,"line":103},[100,21947,9259],{"class":145},[100,21949,18702],{"class":106},[100,21951,21952],{"class":102,"line":135},[100,21953,139],{"emptyLinePlaceholder":138},[100,21955,21956,21958,21960,21962,21964,21966,21968,21970,21972,21974,21976],{"class":102,"line":142},[100,21957,2736],{"class":145},[100,21959,19012],{"class":106},[100,21961,290],{"class":118},[100,21963,19017],{"class":2494},[100,21965,290],{"class":118},[100,21967,18114],{"class":178},[100,21969,170],{"class":118},[100,21971,206],{"class":205},[100,21973,18094],{"class":209},[100,21975,206],{"class":205},[100,21977,522],{"class":118},[100,21979,21980,21982,21984,21986,21988,21990],{"class":102,"line":152},[100,21981,200],{"class":114},[100,21983,170],{"class":118},[100,21985,206],{"class":205},[100,21987,21909],{"class":209},[100,21989,206],{"class":205},[100,21991,215],{"class":118},[100,21993,21994,21996],{"class":102,"line":164},[100,21995,4944],{"class":145},[100,21997,149],{"class":118},[100,21999,22000,22002,22004,22006,22008,22010],{"class":102,"line":185},[100,22001,200],{"class":114},[100,22003,170],{"class":118},[100,22005,206],{"class":205},[100,22007,21930],{"class":209},[100,22009,206],{"class":205},[100,22011,215],{"class":118},[14,22013,22014,22015,22017],{},"This is useful, but it does ",[22,22016,1059],{}," replace good error handling. A file can still disappear between the check and the time you open it.",[14,22019,22020,22021,3266,22023,22025],{},"A safer pattern is to use ",[17,22022,146],{},[17,22024,188],{}," when needed:",[91,22027,22029],{"className":93,"code":22028,"language":95,"meta":96,"style":96},"try:\n    with open(\"data.txt\", \"r\", encoding=\"utf-8\") as file:\n        print(file.read())\nexcept FileNotFoundError:\n    print(\"Could not find data.txt\")\n",[17,22030,22031,22037,22079,22093,22101],{"__ignoreMap":96},[100,22032,22033,22035],{"class":102,"line":103},[100,22034,146],{"class":145},[100,22036,149],{"class":118},[100,22038,22039,22041,22043,22045,22047,22049,22051,22053,22055,22057,22059,22061,22063,22065,22067,22069,22071,22073,22075,22077],{"class":102,"line":135},[100,22040,18121],{"class":145},[100,22042,17524],{"class":114},[100,22044,170],{"class":118},[100,22046,206],{"class":205},[100,22048,18094],{"class":209},[100,22050,206],{"class":205},[100,22052,126],{"class":118},[100,22054,1708],{"class":205},[100,22056,17540],{"class":209},[100,22058,206],{"class":205},[100,22060,126],{"class":118},[100,22062,18142],{"class":18141},[100,22064,111],{"class":110},[100,22066,206],{"class":205},[100,22068,15307],{"class":209},[100,22070,206],{"class":205},[100,22072,6155],{"class":118},[100,22074,16224],{"class":145},[100,22076,17550],{"class":17549},[100,22078,149],{"class":118},[100,22080,22081,22083,22085,22087,22089,22091],{"class":102,"line":142},[100,22082,167],{"class":114},[100,22084,170],{"class":118},[100,22086,17597],{"class":17549},[100,22088,290],{"class":118},[100,22090,17566],{"class":178},[100,22092,3370],{"class":118},[100,22094,22095,22097,22099],{"class":102,"line":152},[100,22096,188],{"class":145},[100,22098,20558],{"class":191},[100,22100,149],{"class":118},[100,22102,22103,22105,22107,22109,22112,22114],{"class":102,"line":164},[100,22104,200],{"class":114},[100,22106,170],{"class":118},[100,22108,206],{"class":205},[100,22110,22111],{"class":209},"Could not find data.txt",[100,22113,206],{"class":205},[100,22115,215],{"class":118},[14,22117,22118,22119,3266,22123,290],{},"For more on existence checks, see ",[295,22120,22122],{"href":22121},"\u002Fhow-to\u002Fhow-to-check-if-a-file-exists-in-python\u002F","how to check if a file exists in Python",[295,22124,22125],{"href":19525},"os.path.exists() explained",[77,22127,22129],{"id":22128},"fix-5-create-the-file-or-folder-if-needed","Fix 5: Create the file or folder if needed",[14,22131,22132],{},"Sometimes the file is missing. Sometimes the folder is missing.",[675,22134,22136],{"id":22135},"if-you-want-to-read-a-file","If you want to read a file",[14,22138,22139],{},"The file must already exist.",[91,22141,22143],{"className":93,"code":22142,"language":95,"meta":96,"style":96},"with open(\"notes.txt\", \"r\", encoding=\"utf-8\") as file:\n    print(file.read())\n",[17,22144,22145,22188],{"__ignoreMap":96},[100,22146,22147,22149,22151,22153,22155,22158,22160,22162,22164,22166,22168,22170,22172,22174,22176,22178,22180,22182,22184,22186],{"class":102,"line":103},[100,22148,17521],{"class":145},[100,22150,17524],{"class":114},[100,22152,170],{"class":118},[100,22154,206],{"class":205},[100,22156,22157],{"class":209},"notes.txt",[100,22159,206],{"class":205},[100,22161,126],{"class":118},[100,22163,1708],{"class":205},[100,22165,17540],{"class":209},[100,22167,206],{"class":205},[100,22169,126],{"class":118},[100,22171,18142],{"class":18141},[100,22173,111],{"class":110},[100,22175,206],{"class":205},[100,22177,15307],{"class":209},[100,22179,206],{"class":205},[100,22181,6155],{"class":118},[100,22183,16224],{"class":145},[100,22185,17550],{"class":17549},[100,22187,149],{"class":118},[100,22189,22190,22192,22194,22196,22198,22200],{"class":102,"line":135},[100,22191,200],{"class":114},[100,22193,170],{"class":118},[100,22195,17597],{"class":17549},[100,22197,290],{"class":118},[100,22199,17566],{"class":178},[100,22201,3370],{"class":118},[14,22203,11734,22204,22206,22207,290],{},[17,22205,22157],{}," is missing, Python raises ",[17,22208,17489],{},[675,22210,22212],{"id":22211},"if-you-want-to-write-a-new-file","If you want to write a new file",[14,22214,22215,22218],{},[17,22216,22217],{},"open(..., \"w\")"," can create a new file if it does not already exist:",[91,22220,22222],{"className":93,"code":22221,"language":95,"meta":96,"style":96},"with open(\"notes.txt\", \"w\", encoding=\"utf-8\") as file:\n    file.write(\"Hello\")\n",[17,22223,22224,22266],{"__ignoreMap":96},[100,22225,22226,22228,22230,22232,22234,22236,22238,22240,22242,22244,22246,22248,22250,22252,22254,22256,22258,22260,22262,22264],{"class":102,"line":103},[100,22227,17521],{"class":145},[100,22229,17524],{"class":114},[100,22231,170],{"class":118},[100,22233,206],{"class":205},[100,22235,22157],{"class":209},[100,22237,206],{"class":205},[100,22239,126],{"class":118},[100,22241,1708],{"class":205},[100,22243,19143],{"class":209},[100,22245,206],{"class":205},[100,22247,126],{"class":118},[100,22249,18142],{"class":18141},[100,22251,111],{"class":110},[100,22253,206],{"class":205},[100,22255,15307],{"class":209},[100,22257,206],{"class":205},[100,22259,6155],{"class":118},[100,22261,16224],{"class":145},[100,22263,17550],{"class":17549},[100,22265,149],{"class":118},[100,22267,22268,22270,22272,22274,22276,22278,22280,22282],{"class":102,"line":135},[100,22269,19170],{"class":17549},[100,22271,290],{"class":118},[100,22273,19175],{"class":178},[100,22275,170],{"class":118},[100,22277,206],{"class":205},[100,22279,7683],{"class":209},[100,22281,206],{"class":205},[100,22283,215],{"class":118},[14,22285,22286,22287,290],{},"But this only works if the ",[22,22288,22289],{},"parent folder already exists",[14,22291,22292,22293,22295],{},"This will fail if the ",[17,22294,18349],{}," folder does not exist:",[91,22297,22299],{"className":93,"code":22298,"language":95,"meta":96,"style":96},"with open(\"output\u002Fnotes.txt\", \"w\", encoding=\"utf-8\") as file:\n    file.write(\"Hello\")\n",[17,22300,22301,22344],{"__ignoreMap":96},[100,22302,22303,22305,22307,22309,22311,22314,22316,22318,22320,22322,22324,22326,22328,22330,22332,22334,22336,22338,22340,22342],{"class":102,"line":103},[100,22304,17521],{"class":145},[100,22306,17524],{"class":114},[100,22308,170],{"class":118},[100,22310,206],{"class":205},[100,22312,22313],{"class":209},"output\u002Fnotes.txt",[100,22315,206],{"class":205},[100,22317,126],{"class":118},[100,22319,1708],{"class":205},[100,22321,19143],{"class":209},[100,22323,206],{"class":205},[100,22325,126],{"class":118},[100,22327,18142],{"class":18141},[100,22329,111],{"class":110},[100,22331,206],{"class":205},[100,22333,15307],{"class":209},[100,22335,206],{"class":205},[100,22337,6155],{"class":118},[100,22339,16224],{"class":145},[100,22341,17550],{"class":17549},[100,22343,149],{"class":118},[100,22345,22346,22348,22350,22352,22354,22356,22358,22360],{"class":102,"line":135},[100,22347,19170],{"class":17549},[100,22349,290],{"class":118},[100,22351,19175],{"class":178},[100,22353,170],{"class":118},[100,22355,206],{"class":205},[100,22357,7683],{"class":209},[100,22359,206],{"class":205},[100,22361,215],{"class":118},[14,22363,22364],{},"Create the folder first:",[91,22366,22368],{"className":93,"code":22367,"language":95,"meta":96,"style":96},"from pathlib import Path\n\nfolder = Path(\"output\")\nfolder.mkdir(exist_ok=True)\n\nfile_path = folder \u002F \"notes.txt\"\n\nwith open(file_path, \"w\", encoding=\"utf-8\") as file:\n    file.write(\"Hello\")\n",[17,22369,22370,22380,22384,22402,22420,22424,22440,22444,22482],{"__ignoreMap":96},[100,22371,22372,22374,22376,22378],{"class":102,"line":103},[100,22373,10201],{"class":145},[100,22375,18068],{"class":106},[100,22377,9259],{"class":145},[100,22379,18073],{"class":106},[100,22381,22382],{"class":102,"line":135},[100,22383,139],{"emptyLinePlaceholder":138},[100,22385,22386,22388,22390,22392,22394,22396,22398,22400],{"class":102,"line":142},[100,22387,20110],{"class":106},[100,22389,111],{"class":110},[100,22391,18087],{"class":178},[100,22393,170],{"class":118},[100,22395,206],{"class":205},[100,22397,18349],{"class":209},[100,22399,206],{"class":205},[100,22401,215],{"class":118},[100,22403,22404,22406,22408,22410,22412,22414,22416,22418],{"class":102,"line":152},[100,22405,20130],{"class":106},[100,22407,290],{"class":118},[100,22409,19236],{"class":178},[100,22411,170],{"class":118},[100,22413,19241],{"class":18141},[100,22415,111],{"class":110},[100,22417,19246],{"class":158},[100,22419,215],{"class":118},[100,22421,22422],{"class":102,"line":164},[100,22423,139],{"emptyLinePlaceholder":138},[100,22425,22426,22428,22430,22432,22434,22436,22438],{"class":102,"line":185},[100,22427,18082],{"class":106},[100,22429,111],{"class":110},[100,22431,20158],{"class":106},[100,22433,2558],{"class":110},[100,22435,1708],{"class":205},[100,22437,22157],{"class":209},[100,22439,1714],{"class":205},[100,22441,22442],{"class":102,"line":197},[100,22443,139],{"emptyLinePlaceholder":138},[100,22445,22446,22448,22450,22452,22454,22456,22458,22460,22462,22464,22466,22468,22470,22472,22474,22476,22478,22480],{"class":102,"line":771},[100,22447,17521],{"class":145},[100,22449,17524],{"class":114},[100,22451,170],{"class":118},[100,22453,18128],{"class":178},[100,22455,126],{"class":118},[100,22457,1708],{"class":205},[100,22459,19143],{"class":209},[100,22461,206],{"class":205},[100,22463,126],{"class":118},[100,22465,18142],{"class":18141},[100,22467,111],{"class":110},[100,22469,206],{"class":205},[100,22471,15307],{"class":209},[100,22473,206],{"class":205},[100,22475,6155],{"class":118},[100,22477,16224],{"class":145},[100,22479,17550],{"class":17549},[100,22481,149],{"class":118},[100,22483,22484,22486,22488,22490,22492,22494,22496,22498],{"class":102,"line":787},[100,22485,19170],{"class":17549},[100,22487,290],{"class":118},[100,22489,19175],{"class":178},[100,22491,170],{"class":118},[100,22493,206],{"class":205},[100,22495,7683],{"class":209},[100,22497,206],{"class":205},[100,22499,215],{"class":118},[14,22501,22502],{},"You can also create nested folders:",[91,22504,22506],{"className":93,"code":22505,"language":95,"meta":96,"style":96},"from pathlib import Path\n\nfolder = Path(\"reports\u002F2026\u002Fapril\")\nfolder.mkdir(parents=True, exist_ok=True)\n\nfile_path = folder \u002F \"summary.txt\"\n\nwith open(file_path, \"w\", encoding=\"utf-8\") as file:\n    file.write(\"Report created\")\n",[17,22507,22508,22518,22522,22541,22569,22573,22590,22594,22632],{"__ignoreMap":96},[100,22509,22510,22512,22514,22516],{"class":102,"line":103},[100,22511,10201],{"class":145},[100,22513,18068],{"class":106},[100,22515,9259],{"class":145},[100,22517,18073],{"class":106},[100,22519,22520],{"class":102,"line":135},[100,22521,139],{"emptyLinePlaceholder":138},[100,22523,22524,22526,22528,22530,22532,22534,22537,22539],{"class":102,"line":142},[100,22525,20110],{"class":106},[100,22527,111],{"class":110},[100,22529,18087],{"class":178},[100,22531,170],{"class":118},[100,22533,206],{"class":205},[100,22535,22536],{"class":209},"reports\u002F2026\u002Fapril",[100,22538,206],{"class":205},[100,22540,215],{"class":118},[100,22542,22543,22545,22547,22549,22551,22554,22556,22558,22560,22563,22565,22567],{"class":102,"line":152},[100,22544,20130],{"class":106},[100,22546,290],{"class":118},[100,22548,19236],{"class":178},[100,22550,170],{"class":118},[100,22552,22553],{"class":18141},"parents",[100,22555,111],{"class":110},[100,22557,19246],{"class":158},[100,22559,126],{"class":118},[100,22561,22562],{"class":18141}," exist_ok",[100,22564,111],{"class":110},[100,22566,19246],{"class":158},[100,22568,215],{"class":118},[100,22570,22571],{"class":102,"line":164},[100,22572,139],{"emptyLinePlaceholder":138},[100,22574,22575,22577,22579,22581,22583,22585,22588],{"class":102,"line":185},[100,22576,18082],{"class":106},[100,22578,111],{"class":110},[100,22580,20158],{"class":106},[100,22582,2558],{"class":110},[100,22584,1708],{"class":205},[100,22586,22587],{"class":209},"summary.txt",[100,22589,1714],{"class":205},[100,22591,22592],{"class":102,"line":197},[100,22593,139],{"emptyLinePlaceholder":138},[100,22595,22596,22598,22600,22602,22604,22606,22608,22610,22612,22614,22616,22618,22620,22622,22624,22626,22628,22630],{"class":102,"line":771},[100,22597,17521],{"class":145},[100,22599,17524],{"class":114},[100,22601,170],{"class":118},[100,22603,18128],{"class":178},[100,22605,126],{"class":118},[100,22607,1708],{"class":205},[100,22609,19143],{"class":209},[100,22611,206],{"class":205},[100,22613,126],{"class":118},[100,22615,18142],{"class":18141},[100,22617,111],{"class":110},[100,22619,206],{"class":205},[100,22621,15307],{"class":209},[100,22623,206],{"class":205},[100,22625,6155],{"class":118},[100,22627,16224],{"class":145},[100,22629,17550],{"class":17549},[100,22631,149],{"class":118},[100,22633,22634,22636,22638,22640,22642,22644,22647,22649],{"class":102,"line":787},[100,22635,19170],{"class":17549},[100,22637,290],{"class":118},[100,22639,19175],{"class":178},[100,22641,170],{"class":118},[100,22643,206],{"class":205},[100,22645,22646],{"class":209},"Report created",[100,22648,206],{"class":205},[100,22650,215],{"class":118},[14,22652,22653,22654,22658],{},"If you are learning the basics, ",[295,22655,22657],{"href":22656},"\u002Flearn\u002Fpython-file-handling-basics-read-and-write\u002F","Python file handling basics: read and write"," is a good next step.",[77,22660,22662],{"id":22661},"how-to-debug-step-by-step","How to debug step by step",[14,22664,22665,22666,22668],{},"When you get ",[17,22667,17489],{},", use this checklist:",[3282,22670,22671,22674,22677,22680,22683],{},[43,22672,22673],{},"Print the exact path variable before opening the file.",[43,22675,22676],{},"Print the current working directory.",[43,22678,22679],{},"Check whether the path is relative or absolute.",[43,22681,22682],{},"Confirm the file exists in that location.",[43,22684,22685],{},"Try a small test script with a known file.",[14,22687,5295],{},[91,22689,22690],{"className":93,"code":19413,"language":95,"meta":96,"style":96},[17,22691,22692,22698],{"__ignoreMap":96},[100,22693,22694,22696],{"class":102,"line":103},[100,22695,9259],{"class":145},[100,22697,18702],{"class":106},[100,22699,22700,22702,22704,22706,22708,22710],{"class":102,"line":135},[100,22701,372],{"class":114},[100,22703,170],{"class":118},[100,22705,18035],{"class":178},[100,22707,290],{"class":118},[100,22709,18719],{"class":178},[100,22711,3370],{"class":118},[91,22713,22714],{"className":93,"code":21555,"language":95,"meta":96,"style":96},[17,22715,22716,22726],{"__ignoreMap":96},[100,22717,22718,22720,22722,22724],{"class":102,"line":103},[100,22719,10201],{"class":145},[100,22721,18068],{"class":106},[100,22723,9259],{"class":145},[100,22725,18073],{"class":106},[100,22727,22728,22730,22732,22734,22736,22738],{"class":102,"line":135},[100,22729,372],{"class":114},[100,22731,170],{"class":118},[100,22733,19463],{"class":178},[100,22735,290],{"class":118},[100,22737,21580],{"class":178},[100,22739,3370],{"class":118},[91,22741,22742],{"className":93,"code":19442,"language":95,"meta":96,"style":96},[17,22743,22744,22754],{"__ignoreMap":96},[100,22745,22746,22748,22750,22752],{"class":102,"line":103},[100,22747,10201],{"class":145},[100,22749,18068],{"class":106},[100,22751,9259],{"class":145},[100,22753,18073],{"class":106},[100,22755,22756,22758,22760,22762,22764,22766,22768,22770,22772,22774],{"class":102,"line":135},[100,22757,372],{"class":114},[100,22759,170],{"class":118},[100,22761,19463],{"class":178},[100,22763,170],{"class":118},[100,22765,206],{"class":205},[100,22767,18094],{"class":209},[100,22769,206],{"class":205},[100,22771,5799],{"class":118},[100,22773,18114],{"class":178},[100,22775,3370],{"class":118},[91,22777,22778],{"className":93,"code":20269,"language":95,"meta":96,"style":96},[17,22779,22780,22790],{"__ignoreMap":96},[100,22781,22782,22784,22786,22788],{"class":102,"line":103},[100,22783,10201],{"class":145},[100,22785,18068],{"class":106},[100,22787,9259],{"class":145},[100,22789,18073],{"class":106},[100,22791,22792,22794,22796,22798,22800,22802,22804,22806,22808,22810],{"class":102,"line":135},[100,22793,372],{"class":114},[100,22795,170],{"class":118},[100,22797,19463],{"class":178},[100,22799,170],{"class":118},[100,22801,206],{"class":205},[100,22803,18094],{"class":209},[100,22805,206],{"class":205},[100,22807,5799],{"class":118},[100,22809,18211],{"class":178},[100,22811,3370],{"class":118},[91,22813,22814],{"className":93,"code":19485,"language":95,"meta":96,"style":96},[17,22815,22816,22822],{"__ignoreMap":96},[100,22817,22818,22820],{"class":102,"line":103},[100,22819,9259],{"class":145},[100,22821,18702],{"class":106},[100,22823,22824,22826,22828,22830,22832,22834,22836,22838,22840,22842,22844,22846],{"class":102,"line":135},[100,22825,372],{"class":114},[100,22827,170],{"class":118},[100,22829,18035],{"class":178},[100,22831,290],{"class":118},[100,22833,19017],{"class":2494},[100,22835,290],{"class":118},[100,22837,18114],{"class":178},[100,22839,170],{"class":118},[100,22841,206],{"class":205},[100,22843,18094],{"class":209},[100,22845,206],{"class":205},[100,22847,182],{"class":118},[91,22849,22851],{"className":93,"code":22850,"language":95,"meta":96,"style":96},"import os\nprint(os.listdir())\n",[17,22852,22853,22859],{"__ignoreMap":96},[100,22854,22855,22857],{"class":102,"line":103},[100,22856,9259],{"class":145},[100,22858,18702],{"class":106},[100,22860,22861,22863,22865,22867,22869,22872],{"class":102,"line":135},[100,22862,372],{"class":114},[100,22864,170],{"class":118},[100,22866,18035],{"class":178},[100,22868,290],{"class":118},[100,22870,22871],{"class":178},"listdir",[100,22873,3370],{"class":118},[14,22875,22876],{},"A simple debugging script:",[91,22878,22880],{"className":93,"code":22879,"language":95,"meta":96,"style":96},"from pathlib import Path\n\nfile_path = Path(\"data.txt\")\n\nprint(\"Current folder:\", Path.cwd())\nprint(\"Path given:\", file_path)\nprint(\"Full path:\", file_path.resolve())\nprint(\"Exists:\", file_path.exists())\n",[17,22881,22882,22892,22896,22914,22918,22941,22960,22983],{"__ignoreMap":96},[100,22883,22884,22886,22888,22890],{"class":102,"line":103},[100,22885,10201],{"class":145},[100,22887,18068],{"class":106},[100,22889,9259],{"class":145},[100,22891,18073],{"class":106},[100,22893,22894],{"class":102,"line":135},[100,22895,139],{"emptyLinePlaceholder":138},[100,22897,22898,22900,22902,22904,22906,22908,22910,22912],{"class":102,"line":142},[100,22899,18082],{"class":106},[100,22901,111],{"class":110},[100,22903,18087],{"class":178},[100,22905,170],{"class":118},[100,22907,206],{"class":205},[100,22909,18094],{"class":209},[100,22911,206],{"class":205},[100,22913,215],{"class":118},[100,22915,22916],{"class":102,"line":152},[100,22917,139],{"emptyLinePlaceholder":138},[100,22919,22920,22922,22924,22926,22929,22931,22933,22935,22937,22939],{"class":102,"line":164},[100,22921,372],{"class":114},[100,22923,170],{"class":118},[100,22925,206],{"class":205},[100,22927,22928],{"class":209},"Current folder:",[100,22930,206],{"class":205},[100,22932,126],{"class":118},[100,22934,18087],{"class":178},[100,22936,290],{"class":118},[100,22938,21580],{"class":178},[100,22940,3370],{"class":118},[100,22942,22943,22945,22947,22949,22952,22954,22956,22958],{"class":102,"line":185},[100,22944,372],{"class":114},[100,22946,170],{"class":118},[100,22948,206],{"class":205},[100,22950,22951],{"class":209},"Path given:",[100,22953,206],{"class":205},[100,22955,126],{"class":118},[100,22957,18109],{"class":178},[100,22959,215],{"class":118},[100,22961,22962,22964,22966,22968,22971,22973,22975,22977,22979,22981],{"class":102,"line":197},[100,22963,372],{"class":114},[100,22965,170],{"class":118},[100,22967,206],{"class":205},[100,22969,22970],{"class":209},"Full path:",[100,22972,206],{"class":205},[100,22974,126],{"class":118},[100,22976,18109],{"class":178},[100,22978,290],{"class":118},[100,22980,18211],{"class":178},[100,22982,3370],{"class":118},[100,22984,22985,22987,22989,22991,22994,22996,22998,23000,23002,23004],{"class":102,"line":771},[100,22986,372],{"class":114},[100,22988,170],{"class":118},[100,22990,206],{"class":205},[100,22992,22993],{"class":209},"Exists:",[100,22995,206],{"class":205},[100,22997,126],{"class":118},[100,22999,18109],{"class":178},[100,23001,290],{"class":118},[100,23003,18114],{"class":178},[100,23005,3370],{"class":118},[14,23007,23008],{},"This helps you see exactly where Python is looking.",[77,23010,23012],{"id":23011},"common-beginner-situations","Common beginner situations",[14,23014,23015],{},"These situations cause this error very often:",[40,23017,23018,23021,23024,23027],{},[43,23019,23020],{},"Running a script from an IDE where the working directory is different",[43,23022,23023],{},"Using notebook paths that are different from script paths",[43,23025,23026],{},"Saving the file in one folder but running code from another",[43,23028,23029],{},"Trying to open a file from the desktop without using the full path",[14,23031,23032,23033,23035],{},"For example, your script may be in one folder, but your IDE may run it from the project root. That means ",[17,23034,18306],{}," may not point where you think it does.",[77,23037,23039],{"id":23038},"related-errors-to-mention","Related errors to mention",[14,23041,23042,23043,23045],{},"Sometimes the problem is not ",[17,23044,17489],{},", but a different file-related error.",[675,23047,17494],{"id":23048},"permissionerror",[14,23050,23051],{},"This happens when the file exists, but Python is not allowed to access it.",[14,23053,23054,23055,290],{},"See ",[295,23056,23058],{"href":23057},"\u002Ferrors\u002Fpermissionerror-in-python-causes-and-fixes","PermissionError in Python: causes and fixes",[675,23060,17499],{"id":23061},"isadirectoryerror",[14,23063,23064],{},"This happens when you pass a folder path where Python expects a file path.",[14,23066,23054,23067,290],{},[295,23068,23070],{"href":23069},"\u002Ferrors\u002Fisadirectoryerror-errno-21-is-a-directory-fix","IsADirectoryError fix",[675,23072,17504],{"id":23073},"notadirectoryerror",[14,23075,23076],{},"This happens when part of the path is expected to be a folder, but it is not.",[14,23078,23054,23079,290],{},[295,23080,23082],{"href":23081},"\u002Ferrors\u002Fnotadirectoryerror-errno-20-not-a-directory-fix","NotADirectoryError fix",[675,23084,17509],{"id":23085},"oserror",[14,23087,23088],{},"This is a broader error for file system and operating system problems.",[77,23090,1514],{"id":1513},[675,23092,23094],{"id":23093},"why-do-i-get-filenotfounderror-when-the-file-is-in-the-same-folder","Why do I get FileNotFoundError when the file is in the same folder?",[14,23096,23097,23098,23100],{},"Python may be running from a different working directory. Check ",[17,23099,19603],{}," and compare it to the file location.",[675,23102,23104],{"id":23103},"can-open-create-a-missing-file","Can open() create a missing file?",[14,23106,23107,23108,3178,23110,23113],{},"Yes, with modes like ",[17,23109,20449],{},[17,23111,23112],{},"\"a\"",". But it cannot create missing folders in the path.",[675,23115,23117],{"id":23116},"what-is-the-difference-between-relative-and-absolute-paths","What is the difference between relative and absolute paths?",[14,23119,23120],{},"A relative path starts from the current working directory. An absolute path gives the full location from the root of the system.",[675,23122,23124],{"id":23123},"should-i-use-try-except-for-filenotfounderror","Should I use try-except for FileNotFoundError?",[14,23126,23127],{},"Yes, especially when a file may not exist. It lets your program fail gracefully and show a helpful message.",[675,23129,23131],{"id":23130},"why-does-my-path-work-on-one-computer-but-not-another","Why does my path work on one computer but not another?",[14,23133,23134],{},"The file location may be different, or the path format may not match the operating system.",[77,23136,1554],{"id":1553},[40,23138,23139,23143,23147,23153,23158],{},[43,23140,23141],{},[295,23142,20591],{"href":20590},[43,23144,23145],{},[295,23146,20596],{"href":18285},[43,23148,23149],{},[295,23150,1572,23151,1576],{"href":18280},[17,23152,18032],{},[43,23154,23155],{},[295,23156,23157],{"href":22121},"How to check if a file exists in Python",[43,23159,23160],{},[295,23161,22657],{"href":22656},[1589,23163,23164],{},"html pre.shiki code .sVHd0, html code.shiki .sVHd0{--shiki-light:#39ADB5;--shiki-light-font-style:italic;--shiki-default:#D73A49;--shiki-default-font-style:inherit;--shiki-dark:#F97583;--shiki-dark-font-style:inherit}html pre.shiki code .su5hD, html code.shiki .su5hD{--shiki-light:#90A4AE;--shiki-default:#24292E;--shiki-dark:#E1E4E8}html pre.shiki code .smGrS, html code.shiki .smGrS{--shiki-light:#39ADB5;--shiki-default:#D73A49;--shiki-dark:#F97583}html pre.shiki code .slqww, html code.shiki .slqww{--shiki-light:#6182B8;--shiki-default:#24292E;--shiki-dark:#E1E4E8}html pre.shiki code .sP7_E, html code.shiki .sP7_E{--shiki-light:#39ADB5;--shiki-default:#24292E;--shiki-dark:#E1E4E8}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 .s99_P, html code.shiki .s99_P{--shiki-light:#90A4AE;--shiki-light-font-style:italic;--shiki-default:#E36209;--shiki-default-font-style:inherit;--shiki-dark:#FFAB70;--shiki-dark-font-style:inherit}html pre.shiki code .sMMDD, html code.shiki .sMMDD{--shiki-light:#90A4AE;--shiki-default:#E36209;--shiki-dark:#FFAB70}html pre.shiki code .sbsja, html code.shiki .sbsja{--shiki-light:#9C3EDA;--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 .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 .sQRbd, html code.shiki .sQRbd{--shiki-light:#91B859;--shiki-default:#032F62;--shiki-dark:#DBEDFF}html pre.shiki code .sjYin, html code.shiki .sjYin{--shiki-light:#90A4AE;--shiki-light-font-weight:inherit;--shiki-default:#22863A;--shiki-default-font-weight:bold;--shiki-dark:#85E89D;--shiki-dark-font-weight:bold}html pre.shiki code .stzsN, html code.shiki .stzsN{--shiki-light:#91B859;--shiki-default:#005CC5;--shiki-dark:#79B8FF}html pre.shiki code .skxfh, html code.shiki .skxfh{--shiki-light:#E53935;--shiki-default:#24292E;--shiki-dark:#E1E4E8}html pre.shiki code .s39Yj, html code.shiki .s39Yj{--shiki-light:#39ADB5;--shiki-default:#005CC5;--shiki-dark:#79B8FF}",{"title":96,"searchDepth":135,"depth":135,"links":23166},[23167,23168,23169,23170,23171,23172,23173,23177,23181,23185,23186,23187,23193,23200],{"id":79,"depth":135,"text":80},{"id":5880,"depth":135,"text":5881},{"id":20950,"depth":135,"text":20951},{"id":11030,"depth":135,"text":11031},{"id":21214,"depth":135,"text":21215},{"id":21273,"depth":135,"text":21274},{"id":21590,"depth":135,"text":21591,"children":23174},[23175,23176],{"id":21605,"depth":142,"text":21606},{"id":21709,"depth":142,"text":21710},{"id":21837,"depth":135,"text":21838,"children":23178},[23179,23180],{"id":21844,"depth":142,"text":21845},{"id":21937,"depth":142,"text":21938},{"id":22128,"depth":135,"text":22129,"children":23182},[23183,23184],{"id":22135,"depth":142,"text":22136},{"id":22211,"depth":142,"text":22212},{"id":22661,"depth":135,"text":22662},{"id":23011,"depth":135,"text":23012},{"id":23038,"depth":135,"text":23039,"children":23188},[23189,23190,23191,23192],{"id":23048,"depth":142,"text":17494},{"id":23061,"depth":142,"text":17499},{"id":23073,"depth":142,"text":17504},{"id":23085,"depth":142,"text":17509},{"id":1513,"depth":135,"text":1514,"children":23194},[23195,23196,23197,23198,23199],{"id":23093,"depth":142,"text":23094},{"id":23103,"depth":142,"text":23104},{"id":23116,"depth":142,"text":23117},{"id":23123,"depth":142,"text":23124},{"id":23130,"depth":142,"text":23131},{"id":1553,"depth":135,"text":1554},"Master filenotfounderror in python causes and fixes in our comprehensive Python beginner guide.",{},"\u002Ferrors\u002Ffilenotfounderror-in-python-causes-and-fixes",{"title":20697,"description":23201},"errors\u002Ffilenotfounderror-in-python-causes-and-fixes","K3V59ZjpKj7y-uwaWJVVxKg6_GayWOl0RU9XLy4u5eo",{"id":23208,"title":23209,"body":23210,"description":24782,"extension":1623,"meta":24783,"navigation":138,"path":24784,"seo":24785,"stem":24786,"__hash__":24787},"content\u002Ferrors\u002Ffilenotfounderror-vs-permissionerror-explained.md","FileNotFoundError vs PermissionError Explained",{"type":7,"value":23211,"toc":24750},[23212,23215,23218,23221,23237,23240,23242,23245,23406,23412,23417,23421,23446,23449,23466,23472,23474,23479,23482,23499,23512,23519,23523,23528,23530,23547,23550,23554,23556,23617,23620,23654,23657,23659,23662,23680,23683,23806,23812,23816,23822,23888,23891,23922,23932,23935,23938,23955,23962,23964,23967,23971,23974,24034,24038,24041,24065,24069,24105,24109,24145,24153,24157,24160,24229,24238,24242,24248,24252,24255,24399,24403,24406,24410,24413,24430,24434,24437,24454,24457,24469,24473,24476,24500,24502,24522,24524,24669,24671,24675,24683,24687,24693,24697,24700,24704,24707,24709,24747],[10,23213,23209],{"id":23214},"filenotfounderror-vs-permissionerror-explained",[14,23216,23217],{},"These two Python errors look similar because both happen when you work with files or folders.",[14,23219,23220],{},"But they mean different things:",[40,23222,23223,23230],{},[43,23224,23225,23229],{},[22,23226,23227],{},[17,23228,17489],{}," means Python cannot find the path you gave.",[43,23231,23232,23236],{},[22,23233,23234],{},[17,23235,17494],{}," means Python found the path, but it is not allowed to access it.",[14,23238,23239],{},"This page helps you tell them apart quickly, understand why each one happens, and choose the right fix.",[77,23241,80],{"id":79},[14,23243,23244],{},"Use this pattern to separate a missing file problem from a permission problem:",[91,23246,23248],{"className":93,"code":23247,"language":95,"meta":96,"style":96},"from pathlib import Path\n\npath = Path(\"data.txt\")\n\nif not path.exists():\n    print(\"The file path is wrong or the file does not exist.\")\nelse:\n    try:\n        with open(path, \"r\", encoding=\"utf-8\") as file:\n            print(file.read())\n    except PermissionError:\n        print(\"The file exists, but Python does not have permission to open it.\")\n",[17,23249,23250,23260,23264,23282,23286,23301,23316,23322,23328,23367,23382,23391],{"__ignoreMap":96},[100,23251,23252,23254,23256,23258],{"class":102,"line":103},[100,23253,10201],{"class":145},[100,23255,18068],{"class":106},[100,23257,9259],{"class":145},[100,23259,18073],{"class":106},[100,23261,23262],{"class":102,"line":135},[100,23263,139],{"emptyLinePlaceholder":138},[100,23265,23266,23268,23270,23272,23274,23276,23278,23280],{"class":102,"line":142},[100,23267,19365],{"class":106},[100,23269,111],{"class":110},[100,23271,18087],{"class":178},[100,23273,170],{"class":118},[100,23275,206],{"class":205},[100,23277,18094],{"class":209},[100,23279,206],{"class":205},[100,23281,215],{"class":118},[100,23283,23284],{"class":102,"line":152},[100,23285,139],{"emptyLinePlaceholder":138},[100,23287,23288,23290,23292,23295,23297,23299],{"class":102,"line":164},[100,23289,2736],{"class":145},[100,23291,4921],{"class":110},[100,23293,23294],{"class":106}," path",[100,23296,290],{"class":118},[100,23298,18114],{"class":178},[100,23300,1085],{"class":118},[100,23302,23303,23305,23307,23309,23312,23314],{"class":102,"line":185},[100,23304,200],{"class":114},[100,23306,170],{"class":118},[100,23308,206],{"class":205},[100,23310,23311],{"class":209},"The file path is wrong or the file does not exist.",[100,23313,206],{"class":205},[100,23315,215],{"class":118},[100,23317,23318,23320],{"class":102,"line":197},[100,23319,4944],{"class":145},[100,23321,149],{"class":118},[100,23323,23324,23326],{"class":102,"line":771},[100,23325,730],{"class":145},[100,23327,149],{"class":118},[100,23329,23330,23333,23335,23337,23339,23341,23343,23345,23347,23349,23351,23353,23355,23357,23359,23361,23363,23365],{"class":102,"line":787},[100,23331,23332],{"class":145},"        with",[100,23334,17524],{"class":114},[100,23336,170],{"class":118},[100,23338,19017],{"class":178},[100,23340,126],{"class":118},[100,23342,1708],{"class":205},[100,23344,17540],{"class":209},[100,23346,206],{"class":205},[100,23348,126],{"class":118},[100,23350,18142],{"class":18141},[100,23352,111],{"class":110},[100,23354,206],{"class":205},[100,23356,15307],{"class":209},[100,23358,206],{"class":205},[100,23360,6155],{"class":118},[100,23362,16224],{"class":145},[100,23364,17550],{"class":17549},[100,23366,149],{"class":118},[100,23368,23369,23372,23374,23376,23378,23380],{"class":102,"line":5816},[100,23370,23371],{"class":114},"            print",[100,23373,170],{"class":118},[100,23375,17597],{"class":17549},[100,23377,290],{"class":118},[100,23379,17566],{"class":178},[100,23381,3370],{"class":118},[100,23383,23384,23386,23389],{"class":102,"line":5833},[100,23385,764],{"class":145},[100,23387,23388],{"class":191}," PermissionError",[100,23390,149],{"class":118},[100,23392,23393,23395,23397,23399,23402,23404],{"class":102,"line":5848},[100,23394,167],{"class":114},[100,23396,170],{"class":118},[100,23398,206],{"class":205},[100,23400,23401],{"class":209},"The file exists, but Python does not have permission to open it.",[100,23403,206],{"class":205},[100,23405,215],{"class":118},[14,23407,23408,23409,23411],{},"Use this approach before trying other fixes.",[9927,23410],{},"\nIt answers the first important question:",[14,23413,23414],{},[22,23415,23416],{},"Does the path exist at all?",[77,23418,23420],{"id":23419},"what-is-the-difference","What is the difference?",[40,23422,23423,23430,23437,23440],{},[43,23424,23425,23429],{},[22,23426,23427],{},[17,23428,17489],{}," means Python cannot find the file or folder at the path you gave.",[43,23431,23432,23436],{},[22,23433,23434],{},[17,23435,17494],{}," means the file or folder exists, but Python is not allowed to access it in the way you requested.",[43,23438,23439],{},"Both are file system errors, but they need different fixes.",[43,23441,23442,23443],{},"The fastest check is: ",[22,23444,23445],{},"does the path exist?",[14,23447,23448],{},"A simple way to think about it:",[40,23450,23451,23459],{},[43,23452,23453,23456,23457],{},[22,23454,23455],{},"Missing path"," → ",[17,23458,17489],{},[43,23460,23461,23456,23464],{},[22,23462,23463],{},"Blocked access",[17,23465,17494],{},[14,23467,23468,23469,290],{},"If you are not sure how file paths work, see ",[295,23470,18286],{"href":23471},"\u002Flearn\u002Fworking-with-file-paths-in-python",[77,23473,20951],{"id":20950},[14,23475,23476,23478],{},[17,23477,17489],{}," usually happens when Python looks for a path and cannot find it.",[14,23480,23481],{},"Common reasons:",[40,23483,23484,23487,23490,23493,23496],{},[43,23485,23486],{},"The filename is misspelled.",[43,23488,23489],{},"The file is in a different folder.",[43,23491,23492],{},"You used a relative path from the wrong working directory.",[43,23494,23495],{},"A parent directory in the path does not exist.",[43,23497,23498],{},"You are trying to open a file before it is created.",[14,23500,23501,23502,23505,23506,23508,23509,23511],{},"For example, if your script says ",[17,23503,23504],{},"open(\"data.txt\")",", Python looks for ",[17,23507,18094],{}," relative to the ",[22,23510,20724],{},", not always the folder where your script is saved.",[14,23513,23514,23515,290],{},"If you need to check that, see ",[295,23516,23518],{"href":23517},"\u002Fstandard-library\u002Fos.getcwd-function-explained","os.getcwd() explained",[77,23520,23522],{"id":23521},"when-permissionerror-happens","When PermissionError happens",[14,23524,23525,23527],{},[17,23526,17494],{}," happens when the path exists, but Python cannot use it the way you asked.",[14,23529,23481],{},[40,23531,23532,23535,23538,23541,23544],{},[43,23533,23534],{},"The file exists, but your user account cannot read or write it.",[43,23536,23537],{},"You tried to write to a read-only file or folder.",[43,23539,23540],{},"You opened a folder as if it were a file.",[43,23542,23543],{},"Another program or system rule is blocking access.",[43,23545,23546],{},"You are working in a protected location such as some system folders.",[14,23548,23549],{},"For example, trying to save a file in a protected system directory may fail even if that directory is real and spelled correctly.",[77,23551,23553],{"id":23552},"example-filenotfounderror","Example: FileNotFoundError",[14,23555,1891],{},[91,23557,23559],{"className":93,"code":23558,"language":95,"meta":96,"style":96},"with open(\"missing.txt\", \"r\", encoding=\"utf-8\") as file:\n    print(file.read())\n",[17,23560,23561,23603],{"__ignoreMap":96},[100,23562,23563,23565,23567,23569,23571,23573,23575,23577,23579,23581,23583,23585,23587,23589,23591,23593,23595,23597,23599,23601],{"class":102,"line":103},[100,23564,17521],{"class":145},[100,23566,17524],{"class":114},[100,23568,170],{"class":118},[100,23570,206],{"class":205},[100,23572,18379],{"class":209},[100,23574,206],{"class":205},[100,23576,126],{"class":118},[100,23578,1708],{"class":205},[100,23580,17540],{"class":209},[100,23582,206],{"class":205},[100,23584,126],{"class":118},[100,23586,18142],{"class":18141},[100,23588,111],{"class":110},[100,23590,206],{"class":205},[100,23592,15307],{"class":209},[100,23594,206],{"class":205},[100,23596,6155],{"class":118},[100,23598,16224],{"class":145},[100,23600,17550],{"class":17549},[100,23602,149],{"class":118},[100,23604,23605,23607,23609,23611,23613,23615],{"class":102,"line":135},[100,23606,200],{"class":114},[100,23608,170],{"class":118},[100,23610,17597],{"class":17549},[100,23612,290],{"class":118},[100,23614,17566],{"class":178},[100,23616,3370],{"class":118},[14,23618,23619],{},"Typical result:",[91,23621,23622],{"className":93,"code":18558,"language":95,"meta":96,"style":96},[17,23623,23624],{"__ignoreMap":96},[100,23625,23626,23628,23630,23632,23634,23636,23638,23640,23642,23644,23646,23648,23650,23652],{"class":102,"line":103},[100,23627,17489],{"class":191},[100,23629,89],{"class":118},[100,23631,594],{"class":118},[100,23633,17586],{"class":106},[100,23635,559],{"class":122},[100,23637,17591],{"class":118},[100,23639,17594],{"class":106},[100,23641,17597],{"class":17549},[100,23643,17600],{"class":110},[100,23645,17603],{"class":106},[100,23647,89],{"class":118},[100,23649,1274],{"class":205},[100,23651,18379],{"class":209},[100,23653,3925],{"class":205},[14,23655,23656],{},"Python fails before it can read anything because the file is not there.",[675,23658,6244],{"id":6243},[14,23660,23661],{},"Check these things:",[40,23663,23664,23667,23674,23677],{},[43,23665,23666],{},"Is the filename correct?",[43,23668,23669,23670,18659,23672,11353],{},"Is the extension correct, such as ",[17,23671,18335],{},[17,23673,18339],{},[43,23675,23676],{},"Is the file in the folder you think it is?",[43,23678,23679],{},"Are you running the script from a different directory?",[14,23681,23682],{},"This small debugging example can help:",[91,23684,23686],{"className":93,"code":23685,"language":95,"meta":96,"style":96},"import os\nfrom pathlib import Path\n\nfilename = \"missing.txt\"\n\nprint(\"Current working directory:\", os.getcwd())\nprint(\"Absolute path:\", Path(filename).resolve())\nprint(\"Exists:\", os.path.exists(filename))\n",[17,23687,23688,23694,23704,23708,23721,23725,23748,23776],{"__ignoreMap":96},[100,23689,23690,23692],{"class":102,"line":103},[100,23691,9259],{"class":145},[100,23693,18702],{"class":106},[100,23695,23696,23698,23700,23702],{"class":102,"line":135},[100,23697,10201],{"class":145},[100,23699,18068],{"class":106},[100,23701,9259],{"class":145},[100,23703,18073],{"class":106},[100,23705,23706],{"class":102,"line":142},[100,23707,139],{"emptyLinePlaceholder":138},[100,23709,23710,23713,23715,23717,23719],{"class":102,"line":152},[100,23711,23712],{"class":106},"filename ",[100,23714,111],{"class":110},[100,23716,1708],{"class":205},[100,23718,18379],{"class":209},[100,23720,1714],{"class":205},[100,23722,23723],{"class":102,"line":164},[100,23724,139],{"emptyLinePlaceholder":138},[100,23726,23727,23729,23731,23733,23736,23738,23740,23742,23744,23746],{"class":102,"line":185},[100,23728,372],{"class":114},[100,23730,170],{"class":118},[100,23732,206],{"class":205},[100,23734,23735],{"class":209},"Current working directory:",[100,23737,206],{"class":205},[100,23739,126],{"class":118},[100,23741,19012],{"class":178},[100,23743,290],{"class":118},[100,23745,18719],{"class":178},[100,23747,3370],{"class":118},[100,23749,23750,23752,23754,23756,23759,23761,23763,23765,23767,23770,23772,23774],{"class":102,"line":197},[100,23751,372],{"class":114},[100,23753,170],{"class":118},[100,23755,206],{"class":205},[100,23757,23758],{"class":209},"Absolute path:",[100,23760,206],{"class":205},[100,23762,126],{"class":118},[100,23764,18087],{"class":178},[100,23766,170],{"class":118},[100,23768,23769],{"class":178},"filename",[100,23771,5799],{"class":118},[100,23773,18211],{"class":178},[100,23775,3370],{"class":118},[100,23777,23778,23780,23782,23784,23786,23788,23790,23792,23794,23796,23798,23800,23802,23804],{"class":102,"line":771},[100,23779,372],{"class":114},[100,23781,170],{"class":118},[100,23783,206],{"class":205},[100,23785,22993],{"class":209},[100,23787,206],{"class":205},[100,23789,126],{"class":118},[100,23791,19012],{"class":178},[100,23793,290],{"class":118},[100,23795,19017],{"class":2494},[100,23797,290],{"class":118},[100,23799,18114],{"class":178},[100,23801,170],{"class":118},[100,23803,23769],{"class":178},[100,23805,182],{"class":118},[14,23807,23808,23809,290],{},"If you want a full guide for this error, see ",[295,23810,23811],{"href":23203},"FileNotFoundError: No such file or directory fix",[77,23813,23815],{"id":23814},"example-permissionerror","Example: PermissionError",[14,23817,23818,23819,23821],{},"Here is a simple example that may raise ",[17,23820,17494],{}," on some systems because the location is protected:",[91,23823,23825],{"className":93,"code":23824,"language":95,"meta":96,"style":96},"with open(\"\u002Froot\u002Fexample.txt\", \"w\", encoding=\"utf-8\") as file:\n    file.write(\"Hello\")\n",[17,23826,23827,23870],{"__ignoreMap":96},[100,23828,23829,23831,23833,23835,23837,23840,23842,23844,23846,23848,23850,23852,23854,23856,23858,23860,23862,23864,23866,23868],{"class":102,"line":103},[100,23830,17521],{"class":145},[100,23832,17524],{"class":114},[100,23834,170],{"class":118},[100,23836,206],{"class":205},[100,23838,23839],{"class":209},"\u002Froot\u002Fexample.txt",[100,23841,206],{"class":205},[100,23843,126],{"class":118},[100,23845,1708],{"class":205},[100,23847,19143],{"class":209},[100,23849,206],{"class":205},[100,23851,126],{"class":118},[100,23853,18142],{"class":18141},[100,23855,111],{"class":110},[100,23857,206],{"class":205},[100,23859,15307],{"class":209},[100,23861,206],{"class":205},[100,23863,6155],{"class":118},[100,23865,16224],{"class":145},[100,23867,17550],{"class":17549},[100,23869,149],{"class":118},[100,23871,23872,23874,23876,23878,23880,23882,23884,23886],{"class":102,"line":135},[100,23873,19170],{"class":17549},[100,23875,290],{"class":118},[100,23877,19175],{"class":178},[100,23879,170],{"class":118},[100,23881,206],{"class":205},[100,23883,7683],{"class":209},[100,23885,206],{"class":205},[100,23887,215],{"class":118},[14,23889,23890],{},"Possible result:",[91,23892,23894],{"className":93,"code":23893,"language":95,"meta":96,"style":96},"PermissionError: [Errno 13] Permission denied: '\u002Froot\u002Fexample.txt'\n",[17,23895,23896],{"__ignoreMap":96},[100,23897,23898,23900,23902,23904,23906,23909,23911,23914,23916,23918,23920],{"class":102,"line":103},[100,23899,17494],{"class":191},[100,23901,89],{"class":118},[100,23903,594],{"class":118},[100,23905,17586],{"class":106},[100,23907,23908],{"class":122},"13",[100,23910,17591],{"class":118},[100,23912,23913],{"class":106}," Permission denied",[100,23915,89],{"class":118},[100,23917,1274],{"class":205},[100,23919,23839],{"class":209},[100,23921,3925],{"class":205},[14,23923,23924,23925,23928,23929,23931],{},"In this case, the problem is not that ",[17,23926,23927],{},"\u002Froot"," is missing.",[9927,23930],{},"\nThe problem is that your Python program is not allowed to write there.",[675,23933,6244],{"id":23934},"how-to-fix-it-1",[14,23936,23937],{},"Possible fixes:",[40,23939,23940,23943,23946,23949,23952],{},[43,23941,23942],{},"Save the file somewhere you can access, such as your project folder.",[43,23944,23945],{},"Change the file path.",[43,23947,23948],{},"Change file or folder permissions if appropriate.",[43,23950,23951],{},"Close another program that may be locking the file.",[43,23953,23954],{},"Make sure you are using the correct file mode.",[14,23956,23957,23958,290],{},"If you need a full error-specific guide, see ",[295,23959,23961],{"href":23960},"\u002Ferrors\u002Fpermissionerror-errno-13-permission-denied-fix","PermissionError: Permission denied fix",[77,23963,22662],{"id":22661},[14,23965,23966],{},"When you are not sure which error you are dealing with, go in this order.",[675,23968,23970],{"id":23969},"_1-print-the-exact-path","1. Print the exact path",[14,23972,23973],{},"Make sure your code is using the path you expect.",[91,23975,23976],{"className":93,"code":19344,"language":95,"meta":96,"style":96},[17,23977,23978,23988,23992,24010,24020],{"__ignoreMap":96},[100,23979,23980,23982,23984,23986],{"class":102,"line":103},[100,23981,10201],{"class":145},[100,23983,18068],{"class":106},[100,23985,9259],{"class":145},[100,23987,18073],{"class":106},[100,23989,23990],{"class":102,"line":135},[100,23991,139],{"emptyLinePlaceholder":138},[100,23993,23994,23996,23998,24000,24002,24004,24006,24008],{"class":102,"line":142},[100,23995,19365],{"class":106},[100,23997,111],{"class":110},[100,23999,18087],{"class":178},[100,24001,170],{"class":118},[100,24003,206],{"class":205},[100,24005,18094],{"class":209},[100,24007,206],{"class":205},[100,24009,215],{"class":118},[100,24011,24012,24014,24016,24018],{"class":102,"line":152},[100,24013,372],{"class":114},[100,24015,170],{"class":118},[100,24017,19017],{"class":178},[100,24019,215],{"class":118},[100,24021,24022,24024,24026,24028,24030,24032],{"class":102,"line":164},[100,24023,372],{"class":114},[100,24025,170],{"class":118},[100,24027,19017],{"class":178},[100,24029,290],{"class":118},[100,24031,18211],{"class":178},[100,24033,3370],{"class":118},[675,24035,24037],{"id":24036},"_2-check-the-current-working-directory","2. Check the current working directory",[14,24039,24040],{},"Relative paths depend on where Python is running.",[91,24042,24043],{"className":93,"code":19413,"language":95,"meta":96,"style":96},[17,24044,24045,24051],{"__ignoreMap":96},[100,24046,24047,24049],{"class":102,"line":103},[100,24048,9259],{"class":145},[100,24050,18702],{"class":106},[100,24052,24053,24055,24057,24059,24061,24063],{"class":102,"line":135},[100,24054,372],{"class":114},[100,24056,170],{"class":118},[100,24058,18035],{"class":178},[100,24060,290],{"class":118},[100,24062,18719],{"class":178},[100,24064,3370],{"class":118},[675,24066,24068],{"id":24067},"_3-check-whether-the-path-exists","3. Check whether the path exists",[91,24070,24071],{"className":93,"code":19485,"language":95,"meta":96,"style":96},[17,24072,24073,24079],{"__ignoreMap":96},[100,24074,24075,24077],{"class":102,"line":103},[100,24076,9259],{"class":145},[100,24078,18702],{"class":106},[100,24080,24081,24083,24085,24087,24089,24091,24093,24095,24097,24099,24101,24103],{"class":102,"line":135},[100,24082,372],{"class":114},[100,24084,170],{"class":118},[100,24086,18035],{"class":178},[100,24088,290],{"class":118},[100,24090,19017],{"class":2494},[100,24092,290],{"class":118},[100,24094,18114],{"class":178},[100,24096,170],{"class":118},[100,24098,206],{"class":205},[100,24100,18094],{"class":209},[100,24102,206],{"class":205},[100,24104,182],{"class":118},[14,24106,4424,24107,89],{},[17,24108,18038],{},[91,24110,24111],{"className":93,"code":19442,"language":95,"meta":96,"style":96},[17,24112,24113,24123],{"__ignoreMap":96},[100,24114,24115,24117,24119,24121],{"class":102,"line":103},[100,24116,10201],{"class":145},[100,24118,18068],{"class":106},[100,24120,9259],{"class":145},[100,24122,18073],{"class":106},[100,24124,24125,24127,24129,24131,24133,24135,24137,24139,24141,24143],{"class":102,"line":135},[100,24126,372],{"class":114},[100,24128,170],{"class":118},[100,24130,19463],{"class":178},[100,24132,170],{"class":118},[100,24134,206],{"class":205},[100,24136,18094],{"class":209},[100,24138,206],{"class":205},[100,24140,5799],{"class":118},[100,24142,18114],{"class":178},[100,24144,3370],{"class":118},[14,24146,24147,24148,3266,24151,290],{},"For more on this, see ",[295,24149,22122],{"href":24150},"\u002Fhow-to\u002Fhow-to-check-if-a-file-exists-in-python",[295,24152,22125],{"href":19525},[675,24154,24156],{"id":24155},"_4-check-whether-it-is-a-file-or-a-directory","4. Check whether it is a file or a directory",[14,24158,24159],{},"Sometimes the path exists, but it points to a folder instead of a file.",[91,24161,24163],{"className":93,"code":24162,"language":95,"meta":96,"style":96},"import os\n\nprint(os.path.isfile(\"data.txt\"))\nprint(os.path.isdir(\"data.txt\"))\n",[17,24164,24165,24171,24175,24202],{"__ignoreMap":96},[100,24166,24167,24169],{"class":102,"line":103},[100,24168,9259],{"class":145},[100,24170,18702],{"class":106},[100,24172,24173],{"class":102,"line":135},[100,24174,139],{"emptyLinePlaceholder":138},[100,24176,24177,24179,24181,24183,24185,24187,24189,24192,24194,24196,24198,24200],{"class":102,"line":142},[100,24178,372],{"class":114},[100,24180,170],{"class":118},[100,24182,18035],{"class":178},[100,24184,290],{"class":118},[100,24186,19017],{"class":2494},[100,24188,290],{"class":118},[100,24190,24191],{"class":178},"isfile",[100,24193,170],{"class":118},[100,24195,206],{"class":205},[100,24197,18094],{"class":209},[100,24199,206],{"class":205},[100,24201,182],{"class":118},[100,24203,24204,24206,24208,24210,24212,24214,24216,24219,24221,24223,24225,24227],{"class":102,"line":152},[100,24205,372],{"class":114},[100,24207,170],{"class":118},[100,24209,18035],{"class":178},[100,24211,290],{"class":118},[100,24213,19017],{"class":2494},[100,24215,290],{"class":118},[100,24217,24218],{"class":178},"isdir",[100,24220,170],{"class":118},[100,24222,206],{"class":205},[100,24224,18094],{"class":209},[100,24226,206],{"class":205},[100,24228,182],{"class":118},[14,24230,24231,24232,24234,24235,290],{},"If you pass a directory to ",[17,24233,18032],{},", you may get a different error such as ",[295,24236,24237],{"href":23069},"IsADirectoryError: Is a directory fix",[675,24239,24241],{"id":24240},"_5-try-a-simpler-location","5. Try a simpler location",[14,24243,24244,24245,24247],{},"Try using a file in the same folder as your script or project.",[9927,24246],{},"\nThis removes many path and permission problems.",[675,24249,24251],{"id":24250},"_6-catch-the-errors-separately","6. Catch the errors separately",[14,24253,24254],{},"This gives clearer messages and makes debugging easier.",[91,24256,24258],{"className":93,"code":24257,"language":95,"meta":96,"style":96},"from pathlib import Path\n\npath = Path(\"data.txt\")\n\ntry:\n    with open(path, \"r\", encoding=\"utf-8\") as file:\n        print(file.read())\nexcept FileNotFoundError:\n    print(\"Python cannot find the file. Check the path and working directory.\")\nexcept PermissionError:\n    print(\"The file exists, but Python does not have permission to open it.\")\n",[17,24259,24260,24270,24274,24292,24296,24302,24340,24354,24362,24377,24385],{"__ignoreMap":96},[100,24261,24262,24264,24266,24268],{"class":102,"line":103},[100,24263,10201],{"class":145},[100,24265,18068],{"class":106},[100,24267,9259],{"class":145},[100,24269,18073],{"class":106},[100,24271,24272],{"class":102,"line":135},[100,24273,139],{"emptyLinePlaceholder":138},[100,24275,24276,24278,24280,24282,24284,24286,24288,24290],{"class":102,"line":142},[100,24277,19365],{"class":106},[100,24279,111],{"class":110},[100,24281,18087],{"class":178},[100,24283,170],{"class":118},[100,24285,206],{"class":205},[100,24287,18094],{"class":209},[100,24289,206],{"class":205},[100,24291,215],{"class":118},[100,24293,24294],{"class":102,"line":152},[100,24295,139],{"emptyLinePlaceholder":138},[100,24297,24298,24300],{"class":102,"line":164},[100,24299,146],{"class":145},[100,24301,149],{"class":118},[100,24303,24304,24306,24308,24310,24312,24314,24316,24318,24320,24322,24324,24326,24328,24330,24332,24334,24336,24338],{"class":102,"line":185},[100,24305,18121],{"class":145},[100,24307,17524],{"class":114},[100,24309,170],{"class":118},[100,24311,19017],{"class":178},[100,24313,126],{"class":118},[100,24315,1708],{"class":205},[100,24317,17540],{"class":209},[100,24319,206],{"class":205},[100,24321,126],{"class":118},[100,24323,18142],{"class":18141},[100,24325,111],{"class":110},[100,24327,206],{"class":205},[100,24329,15307],{"class":209},[100,24331,206],{"class":205},[100,24333,6155],{"class":118},[100,24335,16224],{"class":145},[100,24337,17550],{"class":17549},[100,24339,149],{"class":118},[100,24341,24342,24344,24346,24348,24350,24352],{"class":102,"line":197},[100,24343,167],{"class":114},[100,24345,170],{"class":118},[100,24347,17597],{"class":17549},[100,24349,290],{"class":118},[100,24351,17566],{"class":178},[100,24353,3370],{"class":118},[100,24355,24356,24358,24360],{"class":102,"line":771},[100,24357,188],{"class":145},[100,24359,20558],{"class":191},[100,24361,149],{"class":118},[100,24363,24364,24366,24368,24370,24373,24375],{"class":102,"line":787},[100,24365,200],{"class":114},[100,24367,170],{"class":118},[100,24369,206],{"class":205},[100,24371,24372],{"class":209},"Python cannot find the file. Check the path and working directory.",[100,24374,206],{"class":205},[100,24376,215],{"class":118},[100,24378,24379,24381,24383],{"class":102,"line":5816},[100,24380,188],{"class":145},[100,24382,23388],{"class":191},[100,24384,149],{"class":118},[100,24386,24387,24389,24391,24393,24395,24397],{"class":102,"line":5833},[100,24388,200],{"class":114},[100,24390,170],{"class":118},[100,24392,206],{"class":205},[100,24394,23401],{"class":209},[100,24396,206],{"class":205},[100,24398,215],{"class":118},[77,24400,24402],{"id":24401},"how-to-fix-the-right-error","How to fix the right error",[14,24404,24405],{},"Do not treat both errors as the same problem.",[675,24407,24409],{"id":24408},"fixing-filenotfounderror","Fixing FileNotFoundError",[14,24411,24412],{},"Use these fixes when the path does not exist:",[40,24414,24415,24418,24421,24424,24427],{},[43,24416,24417],{},"Correct the path",[43,24419,24420],{},"Correct the filename or extension",[43,24422,24423],{},"Create the file first",[43,24425,24426],{},"Create missing parent folders",[43,24428,24429],{},"Use an absolute path if needed",[675,24431,24433],{"id":24432},"fixing-permissionerror","Fixing PermissionError",[14,24435,24436],{},"Use these fixes when the path exists but access is blocked:",[40,24438,24439,24442,24445,24448,24451],{},[43,24440,24441],{},"Choose a file or folder you can access",[43,24443,24444],{},"Close programs that may be locking the file",[43,24446,24447],{},"Change file permissions if appropriate",[43,24449,24450],{},"Use the correct mode, such as read vs write",[43,24452,24453],{},"Avoid protected system locations unless necessary",[14,24455,24456],{},"If you are unsure, use this rule:",[3282,24458,24459,24464],{},[43,24460,24461],{},[22,24462,24463],{},"Check path existence first",[43,24465,24466],{},[22,24467,24468],{},"Handle permissions second",[77,24470,24472],{"id":24471},"common-beginner-confusion","Common beginner confusion",[14,24474,24475],{},"These mistakes are very common:",[40,24477,24478,24484,24487,24493],{},[43,24479,24480,24481,24483],{},"A wrong folder is ",[22,24482,1059],{}," a permission problem.",[43,24485,24486],{},"An existing file can still fail to open because of permissions.",[43,24488,24489,24490,24492],{},"A directory path passed to ",[17,24491,18032],{}," can raise a different error.",[43,24494,24495,24496,24499],{},"On Windows, using raw strings like ",[17,24497,24498],{},"r\"C:\\files\\data.txt\""," or forward slashes can help avoid path mistakes.",[14,24501,3971],{},[40,24503,24504,24507,24510,24513,24516,24519],{},[43,24505,24506],{},"Typing the wrong filename or extension",[43,24508,24509],{},"Running the script from a different folder than expected",[43,24511,24512],{},"Using a relative path that points to the wrong location",[43,24514,24515],{},"Trying to save to a protected directory",[43,24517,24518],{},"Trying to read or write a file without permission",[43,24520,24521],{},"Confusing a file path with a folder path",[14,24523,5295],{},[91,24525,24527],{"className":93,"code":24526,"language":95,"meta":96,"style":96},"import os; print(os.getcwd())\nimport os; print(os.path.exists('data.txt'))\nimport os; print(os.path.isfile('data.txt'))\nimport os; print(os.path.isdir('data.txt'))\nfrom pathlib import Path; print(Path('data.txt').resolve())\n",[17,24528,24529,24548,24578,24608,24638],{"__ignoreMap":96},[100,24530,24531,24533,24536,24538,24540,24542,24544,24546],{"class":102,"line":103},[100,24532,9259],{"class":145},[100,24534,24535],{"class":106}," os; ",[100,24537,372],{"class":114},[100,24539,170],{"class":118},[100,24541,18035],{"class":178},[100,24543,290],{"class":118},[100,24545,18719],{"class":178},[100,24547,3370],{"class":118},[100,24549,24550,24552,24554,24556,24558,24560,24562,24564,24566,24568,24570,24572,24574,24576],{"class":102,"line":135},[100,24551,9259],{"class":145},[100,24553,24535],{"class":106},[100,24555,372],{"class":114},[100,24557,170],{"class":118},[100,24559,18035],{"class":178},[100,24561,290],{"class":118},[100,24563,19017],{"class":2494},[100,24565,290],{"class":118},[100,24567,18114],{"class":178},[100,24569,170],{"class":118},[100,24571,1280],{"class":205},[100,24573,18094],{"class":209},[100,24575,1280],{"class":205},[100,24577,182],{"class":118},[100,24579,24580,24582,24584,24586,24588,24590,24592,24594,24596,24598,24600,24602,24604,24606],{"class":102,"line":142},[100,24581,9259],{"class":145},[100,24583,24535],{"class":106},[100,24585,372],{"class":114},[100,24587,170],{"class":118},[100,24589,18035],{"class":178},[100,24591,290],{"class":118},[100,24593,19017],{"class":2494},[100,24595,290],{"class":118},[100,24597,24191],{"class":178},[100,24599,170],{"class":118},[100,24601,1280],{"class":205},[100,24603,18094],{"class":209},[100,24605,1280],{"class":205},[100,24607,182],{"class":118},[100,24609,24610,24612,24614,24616,24618,24620,24622,24624,24626,24628,24630,24632,24634,24636],{"class":102,"line":152},[100,24611,9259],{"class":145},[100,24613,24535],{"class":106},[100,24615,372],{"class":114},[100,24617,170],{"class":118},[100,24619,18035],{"class":178},[100,24621,290],{"class":118},[100,24623,19017],{"class":2494},[100,24625,290],{"class":118},[100,24627,24218],{"class":178},[100,24629,170],{"class":118},[100,24631,1280],{"class":205},[100,24633,18094],{"class":209},[100,24635,1280],{"class":205},[100,24637,182],{"class":118},[100,24639,24640,24642,24644,24646,24649,24651,24653,24655,24657,24659,24661,24663,24665,24667],{"class":102,"line":164},[100,24641,10201],{"class":145},[100,24643,18068],{"class":106},[100,24645,9259],{"class":145},[100,24647,24648],{"class":106}," Path; ",[100,24650,372],{"class":114},[100,24652,170],{"class":118},[100,24654,19463],{"class":178},[100,24656,170],{"class":118},[100,24658,1280],{"class":205},[100,24660,18094],{"class":209},[100,24662,1280],{"class":205},[100,24664,5799],{"class":118},[100,24666,18211],{"class":178},[100,24668,3370],{"class":118},[77,24670,1514],{"id":1513},[675,24672,24674],{"id":24673},"how-do-i-know-if-it-is-filenotfounderror-or-permissionerror","How do I know if it is FileNotFoundError or PermissionError?",[14,24676,24677,24678,24680,24681,290],{},"First check whether the path exists. If it does not exist, it is usually ",[17,24679,17489],{},". If it exists but access is blocked, it is usually ",[17,24682,17494],{},[675,24684,24686],{"id":24685},"can-a-file-exist-and-still-raise-an-error","Can a file exist and still raise an error?",[14,24688,24689,24690,24692],{},"Yes. A file can exist but still raise ",[17,24691,17494],{}," if Python cannot read or write it.",[675,24694,24696],{"id":24695},"should-i-catch-both-errors","Should I catch both errors?",[14,24698,24699],{},"Yes, if your code works with files. Catching them separately gives clearer messages and helps you apply the correct fix.",[675,24701,24703],{"id":24702},"is-a-bad-relative-path-the-same-as-missing-file","Is a bad relative path the same as missing file?",[14,24705,24706],{},"Yes in practice. Python only sees that the file cannot be found from the current working directory.",[77,24708,1554],{"id":1553},[40,24710,24711,24715,24719,24723,24727,24731,24737,24742],{},[43,24712,24713],{},[295,24714,23811],{"href":23203},[43,24716,24717],{},[295,24718,23961],{"href":23960},[43,24720,24721],{},[295,24722,24237],{"href":23069},[43,24724,24725],{},[295,24726,23157],{"href":24150},[43,24728,24729],{},[295,24730,20596],{"href":23471},[43,24732,24733],{},[295,24734,24736],{"href":24735},"\u002Freference\u002Fpython-open-function-explained","Python open() function explained",[43,24738,24739],{},[295,24740,24741],{"href":19525},"os.path.exists() function explained",[43,24743,24744],{},[295,24745,24746],{"href":23517},"os.getcwd() function explained",[1589,24748,24749],{},"html pre.shiki code .sVHd0, html code.shiki .sVHd0{--shiki-light:#39ADB5;--shiki-light-font-style:italic;--shiki-default:#D73A49;--shiki-default-font-style:inherit;--shiki-dark:#F97583;--shiki-dark-font-style:inherit}html pre.shiki code .su5hD, html code.shiki .su5hD{--shiki-light:#90A4AE;--shiki-default:#24292E;--shiki-dark:#E1E4E8}html pre.shiki code .smGrS, html code.shiki .smGrS{--shiki-light:#39ADB5;--shiki-default:#D73A49;--shiki-dark:#F97583}html pre.shiki code .slqww, html code.shiki .slqww{--shiki-light:#6182B8;--shiki-default:#24292E;--shiki-dark:#E1E4E8}html pre.shiki code .sP7_E, html code.shiki .sP7_E{--shiki-light:#39ADB5;--shiki-default:#24292E;--shiki-dark:#E1E4E8}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 .s99_P, html code.shiki .s99_P{--shiki-light:#90A4AE;--shiki-light-font-style:italic;--shiki-default:#E36209;--shiki-default-font-style:inherit;--shiki-dark:#FFAB70;--shiki-dark-font-style:inherit}html pre.shiki code .sMMDD, html code.shiki .sMMDD{--shiki-light:#90A4AE;--shiki-default:#E36209;--shiki-dark:#FFAB70}html pre.shiki code .sZMiF, html code.shiki .sZMiF{--shiki-light:#E2931D;--shiki-default:#005CC5;--shiki-dark:#79B8FF}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 .srdBf, html code.shiki .srdBf{--shiki-light:#F76D47;--shiki-default:#005CC5;--shiki-dark:#79B8FF}html pre.shiki code .skxfh, html code.shiki .skxfh{--shiki-light:#E53935;--shiki-default:#24292E;--shiki-dark:#E1E4E8}",{"title":96,"searchDepth":135,"depth":135,"links":24751},[24752,24753,24754,24755,24756,24759,24762,24770,24774,24775,24781],{"id":79,"depth":135,"text":80},{"id":23419,"depth":135,"text":23420},{"id":20950,"depth":135,"text":20951},{"id":23521,"depth":135,"text":23522},{"id":23552,"depth":135,"text":23553,"children":24757},[24758],{"id":6243,"depth":142,"text":6244},{"id":23814,"depth":135,"text":23815,"children":24760},[24761],{"id":23934,"depth":142,"text":6244},{"id":22661,"depth":135,"text":22662,"children":24763},[24764,24765,24766,24767,24768,24769],{"id":23969,"depth":142,"text":23970},{"id":24036,"depth":142,"text":24037},{"id":24067,"depth":142,"text":24068},{"id":24155,"depth":142,"text":24156},{"id":24240,"depth":142,"text":24241},{"id":24250,"depth":142,"text":24251},{"id":24401,"depth":135,"text":24402,"children":24771},[24772,24773],{"id":24408,"depth":142,"text":24409},{"id":24432,"depth":142,"text":24433},{"id":24471,"depth":135,"text":24472},{"id":1513,"depth":135,"text":1514,"children":24776},[24777,24778,24779,24780],{"id":24673,"depth":142,"text":24674},{"id":24685,"depth":142,"text":24686},{"id":24695,"depth":142,"text":24696},{"id":24702,"depth":142,"text":24703},{"id":1553,"depth":135,"text":1554},"Master filenotfounderror vs permissionerror explained in our comprehensive Python beginner guide.",{},"\u002Ferrors\u002Ffilenotfounderror-vs-permissionerror-explained",{"title":23209,"description":24782},"errors\u002Ffilenotfounderror-vs-permissionerror-explained","n99ZDq_hE9Rw-_kd-6W3NJnLhy8qnGpQHlsh-WbcjNs",{"id":24789,"title":10661,"body":24790,"description":26052,"extension":1623,"meta":26053,"navigation":138,"path":9964,"seo":26054,"stem":26055,"__hash__":26056},"content\u002Ferrors\u002Fimporterror-cannot-import-name-fix.md",{"type":7,"value":24791,"toc":26024},[24792,24795,24804,24807,24824,24827,24829,24902,24905,24907,24912,24920,24923,24939,24948,24955,24959,24962,24976,24979,25011,25014,25028,25031,25063,25065,25074,25077,25088,25092,25095,25153,25162,25164,25167,25248,25253,25312,25314,25327,25334,25338,25341,25343,25349,25390,25396,25437,25440,25461,25464,25499,25502,25513,25520,25522,25525,25529,25532,25543,25547,25550,25553,25557,25582,25585,25588,25592,25617,25620,25624,25627,25645,25648,25654,25658,25661,25664,25666,25668,25687,25690,25729,25732,25747,25754,25756,25787,25789,25797,25799,25802,25854,25857,25879,25882,25914,25916,25920,25925,25930,25934,25937,25941,25943,25956,25960,25966,25968,25993,25995,26021],[10,24793,10661],{"id":24794},"importerror-cannot-import-name-fix",[14,24796,24797,24798,24803],{},"If you see the Python error ",[22,24799,24800],{},[17,24801,24802],{},"ImportError: cannot import name ...",", Python was able to find the module, but it could not find the specific function, class, or variable you asked for.",[14,24805,24806],{},"This usually happens because:",[40,24808,24809,24812,24815,24818,24821],{},[43,24810,24811],{},"the name is spelled wrong",[43,24813,24814],{},"you imported from the wrong module",[43,24816,24817],{},"the name is not available at the top level",[43,24819,24820],{},"there is a circular import",[43,24822,24823],{},"a local file is conflicting with a real module",[14,24825,24826],{},"This guide shows how to find the cause and fix it.",[77,24828,80],{"id":79},[91,24830,24832],{"className":93,"code":24831,"language":95,"meta":96,"style":96},"# Check that the name really exists in the module\nfrom mymodule import my_function\n\n# Alternative: import the module first, then inspect it\nimport mymodule\nprint(dir(mymodule))\n\n# Then access the name with dot notation if it exists\nmymodule.my_function()\n",[17,24833,24834,24839,24851,24855,24860,24867,24882,24886,24891],{"__ignoreMap":96},[100,24835,24836],{"class":102,"line":103},[100,24837,24838],{"class":414},"# Check that the name really exists in the module\n",[100,24840,24841,24843,24846,24848],{"class":102,"line":135},[100,24842,10201],{"class":145},[100,24844,24845],{"class":106}," mymodule ",[100,24847,9259],{"class":145},[100,24849,24850],{"class":106}," my_function\n",[100,24852,24853],{"class":102,"line":142},[100,24854,139],{"emptyLinePlaceholder":138},[100,24856,24857],{"class":102,"line":152},[100,24858,24859],{"class":414},"# Alternative: import the module first, then inspect it\n",[100,24861,24862,24864],{"class":102,"line":164},[100,24863,9259],{"class":145},[100,24865,24866],{"class":106}," mymodule\n",[100,24868,24869,24871,24873,24875,24877,24880],{"class":102,"line":185},[100,24870,372],{"class":114},[100,24872,170],{"class":118},[100,24874,3822],{"class":114},[100,24876,170],{"class":118},[100,24878,24879],{"class":178},"mymodule",[100,24881,182],{"class":118},[100,24883,24884],{"class":102,"line":197},[100,24885,139],{"emptyLinePlaceholder":138},[100,24887,24888],{"class":102,"line":771},[100,24889,24890],{"class":414},"# Then access the name with dot notation if it exists\n",[100,24892,24893,24895,24897,24900],{"class":102,"line":787},[100,24894,24879],{"class":106},[100,24896,290],{"class":118},[100,24898,24899],{"class":178},"my_function",[100,24901,4734],{"class":118},[14,24903,24904],{},"This error usually means the name does not exist, is spelled wrong, is defined in the wrong file, or there is a circular import.",[77,24906,5881],{"id":5880},[14,24908,24909,24911],{},[17,24910,9965],{}," means:",[40,24913,24914,24917],{},[43,24915,24916],{},"Python found the module",[43,24918,24919],{},"but Python could not find the specific name inside that module",[14,24921,24922],{},"For example, this can fail:",[91,24924,24926],{"className":93,"code":24925,"language":95,"meta":96,"style":96},"from math import square\n",[17,24927,24928],{"__ignoreMap":96},[100,24929,24930,24932,24934,24936],{"class":102,"line":103},[100,24931,10201],{"class":145},[100,24933,10204],{"class":106},[100,24935,9259],{"class":145},[100,24937,24938],{"class":106}," square\n",[14,24940,24941,24942,24944,24945,290],{},"because the ",[17,24943,9271],{}," module exists, but it does not provide a name called ",[17,24946,24947],{},"square",[14,24949,24950,24951,24954],{},"This is different from ",[295,24952,24953],{"href":17456},"ModuleNotFoundError: no module named x",", where Python cannot find the module at all.",[77,24956,24958],{"id":24957},"common-example-that-causes-the-error","Common example that causes the error",[14,24960,24961],{},"A simple example:",[91,24963,24964],{"className":93,"code":24925,"language":95,"meta":96,"style":96},[17,24965,24966],{"__ignoreMap":96},[100,24967,24968,24970,24972,24974],{"class":102,"line":103},[100,24969,10201],{"class":145},[100,24971,10204],{"class":106},[100,24973,9259],{"class":145},[100,24975,24938],{"class":106},[14,24977,24978],{},"Expected result:",[91,24980,24982],{"className":93,"code":24981,"language":95,"meta":96,"style":96},"ImportError: cannot import name 'square' from 'math'\n",[17,24983,24984],{"__ignoreMap":96},[100,24985,24986,24988,24990,24993,24995,24997,24999,25001,25003,25005,25007,25009],{"class":102,"line":103},[100,24987,17392],{"class":191},[100,24989,89],{"class":118},[100,24991,24992],{"class":106}," cannot ",[100,24994,9259],{"class":145},[100,24996,4915],{"class":106},[100,24998,1280],{"class":205},[100,25000,24947],{"class":209},[100,25002,1280],{"class":205},[100,25004,10023],{"class":145},[100,25006,1274],{"class":205},[100,25008,9271],{"class":209},[100,25010,3925],{"class":205},[14,25012,25013],{},"Why this fails:",[40,25015,25016,25021],{},[43,25017,25018,25020],{},[17,25019,9271],{}," is a real module",[43,25022,9427,25023,25025,25026],{},[17,25024,24947],{}," is not a function in ",[17,25027,9271],{},[14,25029,25030],{},"A correct version would be:",[91,25032,25033],{"className":93,"code":10151,"language":95,"meta":96,"style":96},[17,25034,25035,25041,25045],{"__ignoreMap":96},[100,25036,25037,25039],{"class":102,"line":103},[100,25038,9259],{"class":145},[100,25040,9262],{"class":106},[100,25042,25043],{"class":102,"line":135},[100,25044,139],{"emptyLinePlaceholder":138},[100,25046,25047,25049,25051,25053,25055,25057,25059,25061],{"class":102,"line":142},[100,25048,372],{"class":114},[100,25050,170],{"class":118},[100,25052,9271],{"class":178},[100,25054,290],{"class":118},[100,25056,9276],{"class":178},[100,25058,170],{"class":118},[100,25060,10180],{"class":122},[100,25062,182],{"class":118},[14,25064,218],{},[91,25066,25068],{"className":93,"code":25067,"language":95,"meta":96,"style":96},"4.0\n",[17,25069,25070],{"__ignoreMap":96},[100,25071,25072],{"class":102,"line":103},[100,25073,25067],{"class":122},[14,25075,25076],{},"Other common cases:",[40,25078,25079,25082,25085],{},[43,25080,25081],{},"you try to import a function that was renamed or deleted",[43,25083,25084],{},"you import from the wrong file with a similar name",[43,25086,25087],{},"you copy example code written for a different package version",[77,25089,25091],{"id":25090},"why-it-happens","Why it happens",[14,25093,25094],{},"Here are the most common reasons:",[40,25096,25097,25110,25118,25129,25137,25145],{},[43,25098,25099,25102,25104,25105,15957,25108,290],{},[22,25100,25101],{},"Misspelled name",[9927,25103],{},"\nYou wrote ",[17,25106,25107],{},"my_fuction",[17,25109,24899],{},[43,25111,25112,25115,25117],{},[22,25113,25114],{},"Wrong module",[9927,25116],{},"\nThe name exists, but in a different module.",[43,25119,25120,25123,25125,25126,25128],{},[22,25121,25122],{},"Name is not at the top level",[9927,25124],{},"\nThe value may only be created inside a function, loop, or ",[17,25127,2736],{}," block.",[43,25130,25131,25134,25136],{},[22,25132,25133],{},"Circular import",[9927,25135],{},"\nTwo files import from each other before loading is complete.",[43,25138,25139,25142,25144],{},[22,25140,25141],{},"Local file conflict",[9927,25143],{},"\nA file in your project has the same name as a standard library module or installed package.",[43,25146,25147,25150,25152],{},[22,25148,25149],{},"Package version changed",[9927,25151],{},"\nThe example you copied may use an older or newer API.",[14,25154,25155,25156,3266,25159,290],{},"If you are not comfortable with how imports work, see ",[295,25157,10137],{"href":25158},"\u002Flearn\u002Fhow-import-works-in-python\u002F",[295,25160,9828],{"href":25161},"\u002Fhow-to\u002Fhow-to-import-a-module-in-python\u002F",[77,25163,6244],{"id":6243},[14,25165,25166],{},"Try these fixes in order:",[3282,25168,25169,25179,25189,25201,25211,25228,25238],{},[43,25170,25171,25174],{},[22,25172,25173],{},"Check the spelling",[40,25175,25176],{},[43,25177,25178],{},"Compare the imported name with the real name in the module.",[43,25180,25181,25184],{},[22,25182,25183],{},"Open the module",[40,25185,25186],{},[43,25187,25188],{},"Confirm the function, class, or variable actually exists.",[43,25190,25191,25194],{},[22,25192,25193],{},"Import the module first",[40,25195,25196],{},[43,25197,25198,25199,290],{},"Then inspect it with ",[17,25200,4565],{},[43,25202,25203,25206],{},[22,25204,25205],{},"Make sure the module is the correct one",[40,25207,25208],{},[43,25209,25210],{},"You may be importing a different file than you think.",[43,25212,25213,25216],{},[22,25214,25215],{},"Rename conflicting local files",[40,25217,25218],{},[43,25219,25220,25221,5870,25223,5894,25225,25227],{},"Files like ",[17,25222,9846],{},[17,25224,9856],{},[17,25226,9861],{}," often cause problems.",[43,25229,25230,25233],{},[22,25231,25232],{},"Fix circular imports",[40,25234,25235],{},[43,25236,25237],{},"Move shared code into a third file, or move imports inside functions when appropriate.",[43,25239,25240,25243],{},[22,25241,25242],{},"Check package documentation",[40,25244,25245],{},[43,25246,25247],{},"The name may have moved or been removed in your installed version.",[14,25249,25250,25251,89],{},"Example using ",[17,25252,4565],{},[91,25254,25256],{"className":93,"code":25255,"language":95,"meta":96,"style":96},"import math\n\nprint(\"square\" in dir(math))\nprint(\"sqrt\" in dir(math))\n",[17,25257,25258,25264,25268,25290],{"__ignoreMap":96},[100,25259,25260,25262],{"class":102,"line":103},[100,25261,9259],{"class":145},[100,25263,9262],{"class":106},[100,25265,25266],{"class":102,"line":135},[100,25267,139],{"emptyLinePlaceholder":138},[100,25269,25270,25272,25274,25276,25278,25280,25282,25284,25286,25288],{"class":102,"line":142},[100,25271,372],{"class":114},[100,25273,170],{"class":118},[100,25275,206],{"class":205},[100,25277,24947],{"class":209},[100,25279,206],{"class":205},[100,25281,1971],{"class":145},[100,25283,4599],{"class":114},[100,25285,170],{"class":118},[100,25287,9271],{"class":178},[100,25289,182],{"class":118},[100,25291,25292,25294,25296,25298,25300,25302,25304,25306,25308,25310],{"class":102,"line":152},[100,25293,372],{"class":114},[100,25295,170],{"class":118},[100,25297,206],{"class":205},[100,25299,9276],{"class":209},[100,25301,206],{"class":205},[100,25303,1971],{"class":145},[100,25305,4599],{"class":114},[100,25307,170],{"class":118},[100,25309,9271],{"class":178},[100,25311,182],{"class":118},[14,25313,218],{},[91,25315,25317],{"className":93,"code":25316,"language":95,"meta":96,"style":96},"False\nTrue\n",[17,25318,25319,25323],{"__ignoreMap":96},[100,25320,25321],{"class":102,"line":103},[100,25322,2407],{"class":158},[100,25324,25325],{"class":102,"line":135},[100,25326,3431],{"class":158},[14,25328,25329,25330,290],{},"You can learn more about this with the ",[295,25331,1572,25332,1576],{"href":4562},[17,25333,4565],{},[77,25335,25337],{"id":25336},"circular-import-case","Circular import case",[14,25339,25340],{},"A circular import happens when two files depend on each other while Python is still loading them.",[14,25342,1844],{},[675,25344,25346],{"id":25345},"apy",[17,25347,25348],{},"a.py",[91,25350,25352],{"className":93,"code":25351,"language":95,"meta":96,"style":96},"from b import greet_b\n\ndef greet_a():\n    return \"Hello from A\"\n",[17,25353,25354,25366,25370,25379],{"__ignoreMap":96},[100,25355,25356,25358,25361,25363],{"class":102,"line":103},[100,25357,10201],{"class":145},[100,25359,25360],{"class":106}," b ",[100,25362,9259],{"class":145},[100,25364,25365],{"class":106}," greet_b\n",[100,25367,25368],{"class":102,"line":135},[100,25369,139],{"emptyLinePlaceholder":138},[100,25371,25372,25374,25377],{"class":102,"line":142},[100,25373,1078],{"class":1077},[100,25375,25376],{"class":1081}," greet_a",[100,25378,1085],{"class":118},[100,25380,25381,25383,25385,25388],{"class":102,"line":152},[100,25382,2552],{"class":145},[100,25384,1708],{"class":205},[100,25386,25387],{"class":209},"Hello from A",[100,25389,1714],{"class":205},[675,25391,25393],{"id":25392},"bpy",[17,25394,25395],{},"b.py",[91,25397,25399],{"className":93,"code":25398,"language":95,"meta":96,"style":96},"from a import greet_a\n\ndef greet_b():\n    return \"Hello from B\"\n",[17,25400,25401,25413,25417,25426],{"__ignoreMap":96},[100,25402,25403,25405,25408,25410],{"class":102,"line":103},[100,25404,10201],{"class":145},[100,25406,25407],{"class":106}," a ",[100,25409,9259],{"class":145},[100,25411,25412],{"class":106}," greet_a\n",[100,25414,25415],{"class":102,"line":135},[100,25416,139],{"emptyLinePlaceholder":138},[100,25418,25419,25421,25424],{"class":102,"line":142},[100,25420,1078],{"class":1077},[100,25422,25423],{"class":1081}," greet_b",[100,25425,1085],{"class":118},[100,25427,25428,25430,25432,25435],{"class":102,"line":152},[100,25429,2552],{"class":145},[100,25431,1708],{"class":205},[100,25433,25434],{"class":209},"Hello from B",[100,25436,1714],{"class":205},[14,25438,25439],{},"This setup can fail because:",[40,25441,25442,25449,25456],{},[43,25443,25444,25446,25447],{},[17,25445,25348],{}," starts importing ",[17,25448,25395],{},[43,25450,25451,25453,25454],{},[17,25452,25395],{}," then tries to import from ",[17,25455,25348],{},[43,25457,9427,25458,25460],{},[17,25459,25348],{}," has not finished loading yet",[14,25462,25463],{},"A common error looks like this:",[91,25465,25467],{"className":93,"code":25466,"language":95,"meta":96,"style":96},"ImportError: cannot import name 'greet_a' from partially initialized module 'a'\n",[17,25468,25469],{"__ignoreMap":96},[100,25470,25471,25473,25475,25477,25479,25481,25483,25486,25488,25490,25493,25495,25497],{"class":102,"line":103},[100,25472,17392],{"class":191},[100,25474,89],{"class":118},[100,25476,24992],{"class":106},[100,25478,9259],{"class":145},[100,25480,4915],{"class":106},[100,25482,1280],{"class":205},[100,25484,25485],{"class":209},"greet_a",[100,25487,1280],{"class":205},[100,25489,10023],{"class":145},[100,25491,25492],{"class":106}," partially initialized module ",[100,25494,1280],{"class":205},[100,25496,295],{"class":209},[100,25498,3925],{"class":205},[14,25500,25501],{},"Common fixes:",[40,25503,25504,25507,25510],{},[43,25505,25506],{},"move the import inside a function",[43,25508,25509],{},"move shared code into a third file",[43,25511,25512],{},"restructure the files so only one depends on the other",[14,25514,25515,25516,25519],{},"For example, moving shared code to ",[17,25517,25518],{},"common.py"," often fixes the problem.",[77,25521,22662],{"id":22661},[14,25523,25524],{},"Use this process when you are stuck.",[675,25526,25528],{"id":25527},"_1-read-the-full-traceback","1. Read the full traceback",[14,25530,25531],{},"Look for:",[40,25533,25534,25537,25540],{},[43,25535,25536],{},"the module name",[43,25538,25539],{},"the imported name",[43,25541,25542],{},"the file path Python is using",[675,25544,25546],{"id":25545},"_2-open-the-target-module","2. Open the target module",[14,25548,25549],{},"Search for the missing name.",[14,25551,25552],{},"Make sure it is really defined, and spelled the same way.",[675,25554,25556],{"id":25555},"_3-check-which-file-python-imported","3. Check which file Python imported",[91,25558,25560],{"className":93,"code":25559,"language":95,"meta":96,"style":96},"import mymodule\nprint(mymodule.__file__)\n",[17,25561,25562,25568],{"__ignoreMap":96},[100,25563,25564,25566],{"class":102,"line":103},[100,25565,9259],{"class":145},[100,25567,24866],{"class":106},[100,25569,25570,25572,25574,25576,25578,25580],{"class":102,"line":135},[100,25571,372],{"class":114},[100,25573,170],{"class":118},[100,25575,24879],{"class":178},[100,25577,290],{"class":118},[100,25579,10041],{"class":527},[100,25581,215],{"class":118},[14,25583,25584],{},"This shows the actual file path Python loaded.",[14,25586,25587],{},"That helps you catch problems where you are importing the wrong file.",[675,25589,25591],{"id":25590},"_4-list-the-names-in-the-module","4. List the names in the module",[91,25593,25595],{"className":93,"code":25594,"language":95,"meta":96,"style":96},"import mymodule\nprint(dir(mymodule))\n",[17,25596,25597,25603],{"__ignoreMap":96},[100,25598,25599,25601],{"class":102,"line":103},[100,25600,9259],{"class":145},[100,25602,24866],{"class":106},[100,25604,25605,25607,25609,25611,25613,25615],{"class":102,"line":135},[100,25606,372],{"class":114},[100,25608,170],{"class":118},[100,25610,3822],{"class":114},[100,25612,170],{"class":118},[100,25614,24879],{"class":178},[100,25616,182],{"class":118},[14,25618,25619],{},"If your name is missing from the list, Python cannot import it from that module.",[675,25621,25623],{"id":25622},"_5-look-for-local-filename-conflicts","5. Look for local filename conflicts",[14,25625,25626],{},"Check your project folder for files like:",[40,25628,25629,25633,25637,25641],{},[43,25630,25631],{},[17,25632,9846],{},[43,25634,25635],{},[17,25636,9856],{},[43,25638,25639],{},[17,25640,9861],{},[43,25642,25643],{},[17,25644,9851],{},[14,25646,25647],{},"These can shadow the real modules.",[14,25649,25650,25651,25653],{},"For example, if you create a file named ",[17,25652,9851],{},", then this import may not use the real standard library module.",[675,25655,25657],{"id":25656},"_6-check-for-circular-imports","6. Check for circular imports",[14,25659,25660],{},"Look for two files importing each other.",[14,25662,25663],{},"If file A imports from file B, and file B imports from file A, that is a strong sign of a circular import.",[77,25665,1344],{"id":1343},[14,25667,9001],{},[40,25669,25670,25673,25676,25678,25681,25684],{},[43,25671,25672],{},"Misspelled imported name",[43,25674,25675],{},"Wrong module path",[43,25677,25133],{},[43,25679,25680],{},"Local file name conflict",[43,25682,25683],{},"Outdated example code for a newer package version",[43,25685,25686],{},"Trying to import a name that is created only inside a function or condition",[14,25688,25689],{},"Here is an example of a name that cannot be imported because it is not defined at the top level:",[91,25691,25693],{"className":93,"code":25692,"language":95,"meta":96,"style":96},"# mymodule.py\n\ndef make_value():\n    hidden_value = 10\n    return hidden_value\n",[17,25694,25695,25700,25704,25713,25722],{"__ignoreMap":96},[100,25696,25697],{"class":102,"line":103},[100,25698,25699],{"class":414},"# mymodule.py\n",[100,25701,25702],{"class":102,"line":135},[100,25703,139],{"emptyLinePlaceholder":138},[100,25705,25706,25708,25711],{"class":102,"line":142},[100,25707,1078],{"class":1077},[100,25709,25710],{"class":1081}," make_value",[100,25712,1085],{"class":118},[100,25714,25715,25718,25720],{"class":102,"line":152},[100,25716,25717],{"class":106},"    hidden_value ",[100,25719,111],{"class":110},[100,25721,2181],{"class":122},[100,25723,25724,25726],{"class":102,"line":164},[100,25725,2552],{"class":145},[100,25727,25728],{"class":106}," hidden_value\n",[14,25730,25731],{},"This will fail:",[91,25733,25735],{"className":93,"code":25734,"language":95,"meta":96,"style":96},"from mymodule import hidden_value\n",[17,25736,25737],{"__ignoreMap":96},[100,25738,25739,25741,25743,25745],{"class":102,"line":103},[100,25740,10201],{"class":145},[100,25742,24845],{"class":106},[100,25744,9259],{"class":145},[100,25746,25728],{"class":106},[14,25748,25749,25750,25753],{},"because ",[17,25751,25752],{},"hidden_value"," only exists inside the function.",[14,25755,25030],{},[91,25757,25759],{"className":93,"code":25758,"language":95,"meta":96,"style":96},"from mymodule import make_value\n\nprint(make_value())\n",[17,25760,25761,25772,25776],{"__ignoreMap":96},[100,25762,25763,25765,25767,25769],{"class":102,"line":103},[100,25764,10201],{"class":145},[100,25766,24845],{"class":106},[100,25768,9259],{"class":145},[100,25770,25771],{"class":106}," make_value\n",[100,25773,25774],{"class":102,"line":135},[100,25775,139],{"emptyLinePlaceholder":138},[100,25777,25778,25780,25782,25785],{"class":102,"line":142},[100,25779,372],{"class":114},[100,25781,170],{"class":118},[100,25783,25784],{"class":178},"make_value",[100,25786,3370],{"class":118},[14,25788,218],{},[91,25790,25791],{"className":93,"code":3410,"language":95,"meta":96,"style":96},[17,25792,25793],{"__ignoreMap":96},[100,25794,25795],{"class":102,"line":103},[100,25796,3410],{"class":122},[77,25798,20239],{"id":20238},[14,25800,25801],{},"These commands are helpful when tracking down the problem:",[91,25803,25805],{"className":93,"code":25804,"language":95,"meta":96,"style":96},"import mymodule; print(mymodule.__file__)\nimport mymodule; print(dir(mymodule))\nhelp(mymodule)\n",[17,25806,25807,25826,25844],{"__ignoreMap":96},[100,25808,25809,25811,25814,25816,25818,25820,25822,25824],{"class":102,"line":103},[100,25810,9259],{"class":145},[100,25812,25813],{"class":106}," mymodule; ",[100,25815,372],{"class":114},[100,25817,170],{"class":118},[100,25819,24879],{"class":178},[100,25821,290],{"class":118},[100,25823,10041],{"class":527},[100,25825,215],{"class":118},[100,25827,25828,25830,25832,25834,25836,25838,25840,25842],{"class":102,"line":135},[100,25829,9259],{"class":145},[100,25831,25813],{"class":106},[100,25833,372],{"class":114},[100,25835,170],{"class":118},[100,25837,3822],{"class":114},[100,25839,170],{"class":118},[100,25841,24879],{"class":178},[100,25843,182],{"class":118},[100,25845,25846,25848,25850,25852],{"class":102,"line":142},[100,25847,2478],{"class":114},[100,25849,170],{"class":118},[100,25851,24879],{"class":178},[100,25853,215],{"class":118},[14,25855,25856],{},"From the command line:",[91,25858,25860],{"className":10352,"code":25859,"language":10354,"meta":96,"style":96},"python --version\npip show package_name\n",[17,25861,25862,25868],{"__ignoreMap":96},[100,25863,25864,25866],{"class":102,"line":103},[100,25865,95],{"class":10361},[100,25867,10365],{"class":10364},[100,25869,25870,25873,25876],{"class":102,"line":135},[100,25871,25872],{"class":10361},"pip",[100,25874,25875],{"class":209}," show",[100,25877,25878],{"class":209}," package_name\n",[14,25880,25881],{},"What they help with:",[40,25883,25884,25890,25896,25902,25908],{},[43,25885,25886,25889],{},[17,25887,25888],{},"mymodule.__file__"," shows which file Python imported",[43,25891,25892,25895],{},[17,25893,25894],{},"dir(mymodule)"," shows the names available in the module",[43,25897,25898,25901],{},[17,25899,25900],{},"help(mymodule)"," gives basic module information",[43,25903,25904,25907],{},[17,25905,25906],{},"python --version"," helps confirm your Python version",[43,25909,25910,25913],{},[17,25911,25912],{},"pip show package_name"," shows the installed package version",[77,25915,1514],{"id":1513},[675,25917,25919],{"id":25918},"what-is-the-difference-between-importerror-and-modulenotfounderror","What is the difference between ImportError and ModuleNotFoundError?",[14,25921,25922,25924],{},[17,25923,17397],{}," means Python cannot find the module.",[14,25926,25927,25929],{},[17,25928,9965],{}," means Python found the module but not the specific name inside it.",[675,25931,25933],{"id":25932},"can-circular-imports-cause-this-error","Can circular imports cause this error?",[14,25935,25936],{},"Yes. If two files import each other, one file may try to import a name before it is available.",[675,25938,25940],{"id":25939},"why-does-this-happen-even-though-the-name-is-in-the-file","Why does this happen even though the name is in the file?",[14,25942,23481],{},[40,25944,25945,25948,25951,25953],{},[43,25946,25947],{},"you are importing the wrong file",[43,25949,25950],{},"a local filename is conflicting with the real module",[43,25952,24820],{},[43,25954,25955],{},"the name is not available when the import runs",[675,25957,25959],{"id":25958},"how-do-i-check-what-names-a-module-provides","How do I check what names a module provides?",[14,25961,25962,25963,25965],{},"Import the module and use ",[17,25964,10406],{}," to list the available names.",[14,25967,1844],{},[91,25969,25971],{"className":93,"code":25970,"language":95,"meta":96,"style":96},"import math\nprint(dir(math))\n",[17,25972,25973,25979],{"__ignoreMap":96},[100,25974,25975,25977],{"class":102,"line":103},[100,25976,9259],{"class":145},[100,25978,9262],{"class":106},[100,25980,25981,25983,25985,25987,25989,25991],{"class":102,"line":135},[100,25982,372],{"class":114},[100,25984,170],{"class":118},[100,25986,3822],{"class":114},[100,25988,170],{"class":118},[100,25990,9271],{"class":178},[100,25992,182],{"class":118},[77,25994,1554],{"id":1553},[40,25996,25997,26003,26007,26011,26015],{},[43,25998,25999],{},[295,26000,26002],{"href":26001},"\u002Ferrors\u002Fimporterror-no-module-named-x-fix\u002F","ImportError: no module named x",[43,26004,26005],{},[295,26006,24953],{"href":17456},[43,26008,26009],{},[295,26010,10683],{"href":25161},[43,26012,26013],{},[295,26014,10666],{"href":25158},[43,26016,26017],{},[295,26018,1572,26019,1576],{"href":4562},[17,26020,4565],{},[1589,26022,26023],{},"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 .sVHd0, html code.shiki .sVHd0{--shiki-light:#39ADB5;--shiki-light-font-style:italic;--shiki-default:#D73A49;--shiki-default-font-style:inherit;--shiki-dark:#F97583;--shiki-dark-font-style:inherit}html pre.shiki code .su5hD, html code.shiki .su5hD{--shiki-light:#90A4AE;--shiki-default:#24292E;--shiki-dark:#E1E4E8}html pre.shiki code .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 .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 .srdBf, html code.shiki .srdBf{--shiki-light:#F76D47;--shiki-default:#005CC5;--shiki-dark:#79B8FF}html pre.shiki code .s39Yj, html code.shiki .s39Yj{--shiki-light:#39ADB5;--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 .s_hVV, html code.shiki .s_hVV{--shiki-light:#90A4AE;--shiki-default:#005CC5;--shiki-dark:#79B8FF}html pre.shiki code .smGrS, html code.shiki .smGrS{--shiki-light:#39ADB5;--shiki-default:#D73A49;--shiki-dark:#F97583}html pre.shiki code .sbgvK, html code.shiki .sbgvK{--shiki-light:#E2931D;--shiki-default:#6F42C1;--shiki-dark:#B392F0}html pre.shiki code .stzsN, html code.shiki .stzsN{--shiki-light:#91B859;--shiki-default:#005CC5;--shiki-dark:#79B8FF}",{"title":96,"searchDepth":135,"depth":135,"links":26025},[26026,26027,26028,26029,26030,26031,26035,26043,26044,26045,26051],{"id":79,"depth":135,"text":80},{"id":5880,"depth":135,"text":5881},{"id":24957,"depth":135,"text":24958},{"id":25090,"depth":135,"text":25091},{"id":6243,"depth":135,"text":6244},{"id":25336,"depth":135,"text":25337,"children":26032},[26033,26034],{"id":25345,"depth":142,"text":25348},{"id":25392,"depth":142,"text":25395},{"id":22661,"depth":135,"text":22662,"children":26036},[26037,26038,26039,26040,26041,26042],{"id":25527,"depth":142,"text":25528},{"id":25545,"depth":142,"text":25546},{"id":25555,"depth":142,"text":25556},{"id":25590,"depth":142,"text":25591},{"id":25622,"depth":142,"text":25623},{"id":25656,"depth":142,"text":25657},{"id":1343,"depth":135,"text":1344},{"id":20238,"depth":135,"text":20239},{"id":1513,"depth":135,"text":1514,"children":26046},[26047,26048,26049,26050],{"id":25918,"depth":142,"text":25919},{"id":25932,"depth":142,"text":25933},{"id":25939,"depth":142,"text":25940},{"id":25958,"depth":142,"text":25959},{"id":1553,"depth":135,"text":1554},"Master importerror cannot import name fix in our comprehensive Python beginner guide.",{},{"title":10661,"description":26052},"errors\u002Fimporterror-cannot-import-name-fix","HlrrpeNKvj7xry0nofi08giIbxmiyayxrRTv3iru4Xk",{"id":26058,"title":26059,"body":26060,"description":27528,"extension":1623,"meta":27529,"navigation":138,"path":27530,"seo":27531,"stem":27532,"__hash__":27533},"content\u002Ferrors\u002Fimporterror-in-python-causes-and-fixes.md","ImportError in Python: Causes and Fixes",{"type":7,"value":26061,"toc":27492},[26062,26065,26072,26074,26088,26095,26110,26113,26117,26120,26195,26198,26224,26227,26239,26243,26248,26250,26261,26263,26281,26286,26290,26296,26316,26319,26323,26326,26342,26352,26357,26390,26392,26401,26404,26412,26415,26421,26425,26428,26430,26444,26446,26462,26465,26480,26489,26492,26520,26525,26535,26538,26570,26573,26581,26585,26588,26591,26602,26605,26639,26642,26660,26668,26671,26674,26681,26685,26688,26694,26705,26708,26711,26730,26733,26745,26748,26759,26762,26766,26769,26772,26779,26784,26801,26804,26807,26821,26824,26835,26842,26851,26855,26858,26860,26864,26918,26922,26959,26962,26965,26975,26981,26985,26990,26992,26995,26997,27008,27012,27015,27023,27029,27035,27038,27056,27063,27067,27070,27084,27087,27091,27094,27138,27141,27167,27170,27172,27175,27180,27183,27186,27198,27202,27207,27210,27212,27226,27230,27235,27238,27240,27269,27273,27275,27280,27303,27305,27308,27364,27367,27387,27389,27391,27396,27401,27405,27408,27412,27415,27419,27425,27427,27452,27454,27489],[10,26063,26059],{"id":26064},"importerror-in-python-causes-and-fixes",[14,26066,26067,26069,26070,11037],{},[17,26068,17392],{}," means Python could not complete an ",[17,26071,9259],{},[14,26073,24806],{},[40,26075,26076,26079,26082,26085],{},[43,26077,26078],{},"the module is missing",[43,26080,26081],{},"the name you are trying to import does not exist",[43,26083,26084],{},"Python is using the wrong environment",[43,26086,26087],{},"your project files are named or arranged in a way that breaks imports",[14,26089,26090,26091,3266,26093,290],{},"Beginners often mix up ",[17,26092,17392],{},[17,26094,17397],{},[40,26096,26097,26102,26107],{},[43,26098,26099,26101],{},[17,26100,17397],{}," is more specific",[43,26103,26104,26106],{},[17,26105,17392],{}," is broader",[43,26108,26109],{},"Python may find the module, but still fail to import something from it",[14,26111,26112],{},"This page helps you check the most common causes step by step.",[77,26114,26116],{"id":26115},"quick-fix-checklist","Quick fix checklist",[14,26118,26119],{},"First confirm Python is running correctly, then check whether the module exists, the name is spelled correctly, and the import path is valid.",[91,26121,26123],{"className":93,"code":26122,"language":95,"meta":96,"style":96},"import sys\nprint(sys.version)\nprint(sys.path)\n\n# Check the exact import you are using\nimport math\nprint(math.sqrt(9))\n",[17,26124,26125,26132,26148,26162,26166,26171,26177],{"__ignoreMap":96},[100,26126,26127,26129],{"class":102,"line":103},[100,26128,9259],{"class":145},[100,26130,26131],{"class":106}," sys\n",[100,26133,26134,26136,26138,26141,26143,26146],{"class":102,"line":135},[100,26135,372],{"class":114},[100,26137,170],{"class":118},[100,26139,26140],{"class":178},"sys",[100,26142,290],{"class":118},[100,26144,26145],{"class":2494},"version",[100,26147,215],{"class":118},[100,26149,26150,26152,26154,26156,26158,26160],{"class":102,"line":142},[100,26151,372],{"class":114},[100,26153,170],{"class":118},[100,26155,26140],{"class":178},[100,26157,290],{"class":118},[100,26159,19017],{"class":2494},[100,26161,215],{"class":118},[100,26163,26164],{"class":102,"line":152},[100,26165,139],{"emptyLinePlaceholder":138},[100,26167,26168],{"class":102,"line":164},[100,26169,26170],{"class":414},"# Check the exact import you are using\n",[100,26172,26173,26175],{"class":102,"line":185},[100,26174,9259],{"class":145},[100,26176,9262],{"class":106},[100,26178,26179,26181,26183,26185,26187,26189,26191,26193],{"class":102,"line":197},[100,26180,372],{"class":114},[100,26182,170],{"class":118},[100,26184,9271],{"class":178},[100,26186,290],{"class":118},[100,26188,9276],{"class":178},[100,26190,170],{"class":118},[100,26192,9281],{"class":122},[100,26194,182],{"class":118},[14,26196,26197],{},"What this does:",[40,26199,26200,26206,26212,26218],{},[43,26201,26202,26205],{},[17,26203,26204],{},"sys.version"," shows which Python version is running",[43,26207,26208,26211],{},[17,26209,26210],{},"sys.path"," shows where Python looks for modules",[43,26213,26214,26217],{},[17,26215,26216],{},"import math"," checks that a standard library import works",[43,26219,26220,26223],{},[17,26221,26222],{},"math.sqrt(9)"," confirms the imported module is usable",[14,26225,26226],{},"If a simple import works but your real import fails, the problem is likely with:",[40,26228,26229,26231,26233,26236],{},[43,26230,25536],{},[43,26232,25539],{},[43,26234,26235],{},"the environment",[43,26237,26238],{},"your file or package structure",[77,26240,26242],{"id":26241},"what-importerror-means","What ImportError means",[14,26244,26245,26247],{},[17,26246,17392],{}," happens when Python cannot complete an import statement.",[14,26249,12895],{},[40,26251,26252,26255,26258],{},[43,26253,26254],{},"Python may fail to find a module",[43,26256,26257],{},"or Python may find the module but fail to load part of it",[43,26259,26260],{},"or Python may find the module but not the specific name you asked for",[14,26262,580],{},[40,26264,26265,26271],{},[43,26266,26267,26270],{},[17,26268,26269],{},"import some_module"," can fail if the module is not available",[43,26272,26273,26276,26277,26280],{},[17,26274,26275],{},"from some_module import thing"," can fail if ",[17,26278,26279],{},"thing"," does not exist",[14,26282,26283,26284,290],{},"If you want a broader explanation of imports, see ",[295,26285,10137],{"href":25158},[77,26287,26289],{"id":26288},"when-this-error-happens","When this error happens",[14,26291,26292,26293,26295],{},"You may see ",[17,26294,17392],{}," in situations like these:",[40,26297,26298,26301,26304,26307,26310,26313],{},[43,26299,26300],{},"importing a module that is missing",[43,26302,26303],{},"importing a name that does not exist inside a module",[43,26305,26306],{},"using the wrong Python environment",[43,26308,26309],{},"running a file from the wrong folder",[43,26311,26312],{},"circular imports between your own files",[43,26314,26315],{},"naming your file the same as a standard library module",[14,26317,26318],{},"These are the most common beginner cases.",[77,26320,26322],{"id":26321},"basic-example-that-causes-importerror","Basic example that causes ImportError",[14,26324,26325],{},"A common case is importing a name that is not available inside a module.",[91,26327,26329],{"className":93,"code":26328,"language":95,"meta":96,"style":96},"from math import square_root\n",[17,26330,26331],{"__ignoreMap":96},[100,26332,26333,26335,26337,26339],{"class":102,"line":103},[100,26334,10201],{"class":145},[100,26336,10204],{"class":106},[100,26338,9259],{"class":145},[100,26340,26341],{"class":106}," square_root\n",[14,26343,26344,26345,26347,26348,26351],{},"This fails because the ",[17,26346,9271],{}," module exists, but ",[17,26349,26350],{},"square_root"," is not a valid name in that module.",[14,26353,26354,26355,290],{},"The correct name is ",[17,26356,9276],{},[91,26358,26360],{"className":93,"code":26359,"language":95,"meta":96,"style":96},"from math import sqrt\n\nprint(sqrt(9))\n",[17,26361,26362,26372,26376],{"__ignoreMap":96},[100,26363,26364,26366,26368,26370],{"class":102,"line":103},[100,26365,10201],{"class":145},[100,26367,10204],{"class":106},[100,26369,9259],{"class":145},[100,26371,10209],{"class":106},[100,26373,26374],{"class":102,"line":135},[100,26375,139],{"emptyLinePlaceholder":138},[100,26377,26378,26380,26382,26384,26386,26388],{"class":102,"line":142},[100,26379,372],{"class":114},[100,26381,170],{"class":118},[100,26383,9276],{"class":178},[100,26385,170],{"class":118},[100,26387,9281],{"class":122},[100,26389,182],{"class":118},[14,26391,218],{},[91,26393,26395],{"className":93,"code":26394,"language":95,"meta":96,"style":96},"3.0\n",[17,26396,26397],{"__ignoreMap":96},[100,26398,26399],{"class":102,"line":103},[100,26400,26394],{"class":122},[14,26402,26403],{},"This is an important pattern:",[40,26405,26406,26409],{},[43,26407,26408],{},"Python can find the module",[43,26410,26411],{},"but Python cannot import the requested name",[14,26413,26414],{},"That is different from a missing module.",[14,26416,26417,26418,290],{},"For a more specific version of this problem, see ",[295,26419,9965],{"href":26420},"\u002Ferrors\u002Fimporterror-cannot-import-name-fix\u002F",[77,26422,26424],{"id":26423},"fix-1-check-the-module-and-name-carefully","Fix 1: Check the module and name carefully",[14,26426,26427],{},"Start with the simplest possibility: a typo.",[14,26429,23661],{},[40,26431,26432,26435,26438,26441],{},[43,26433,26434],{},"spelling",[43,26436,26437],{},"uppercase and lowercase letters",[43,26439,26440],{},"whether the function, class, or variable really exists",[43,26442,26443],{},"whether you are using the correct module",[14,26445,4442],{},[91,26447,26449],{"className":93,"code":26448,"language":95,"meta":96,"style":96},"from math import Sqrt\n",[17,26450,26451],{"__ignoreMap":96},[100,26452,26453,26455,26457,26459],{"class":102,"line":103},[100,26454,10201],{"class":145},[100,26456,10204],{"class":106},[100,26458,9259],{"class":145},[100,26460,26461],{"class":106}," Sqrt\n",[14,26463,26464],{},"This is correct:",[91,26466,26468],{"className":93,"code":26467,"language":95,"meta":96,"style":96},"from math import sqrt\n",[17,26469,26470],{"__ignoreMap":96},[100,26471,26472,26474,26476,26478],{"class":102,"line":103},[100,26473,10201],{"class":145},[100,26475,10204],{"class":106},[100,26477,9259],{"class":145},[100,26479,10209],{"class":106},[14,26481,26482,26483,3266,26486,26488],{},"Python names are case-sensitive. ",[17,26484,26485],{},"Sqrt",[17,26487,9276],{}," are different names.",[14,26490,26491],{},"If you are not sure what a module contains, import the whole module first:",[91,26493,26494],{"className":93,"code":9574,"language":95,"meta":96,"style":96},[17,26495,26496,26502,26506],{"__ignoreMap":96},[100,26497,26498,26500],{"class":102,"line":103},[100,26499,9259],{"class":145},[100,26501,9262],{"class":106},[100,26503,26504],{"class":102,"line":135},[100,26505,139],{"emptyLinePlaceholder":138},[100,26507,26508,26510,26512,26514,26516,26518],{"class":102,"line":142},[100,26509,372],{"class":114},[100,26511,170],{"class":118},[100,26513,3822],{"class":114},[100,26515,170],{"class":118},[100,26517,9271],{"class":178},[100,26519,182],{"class":118},[14,26521,26522,26524],{},[17,26523,4565],{}," shows the available names inside the module.",[14,26526,26527,26528,3266,26531,290],{},"You can also inspect help text with Python’s built-in tools. See ",[295,26529,26530],{"href":4562},"Python dir() explained",[295,26532,26534],{"href":26533},"\u002Freference\u002Fpython-help-function-explained\u002F","Python help() explained",[14,26536,26537],{},"A safe pattern is:",[91,26539,26540],{"className":93,"code":10151,"language":95,"meta":96,"style":96},[17,26541,26542,26548,26552],{"__ignoreMap":96},[100,26543,26544,26546],{"class":102,"line":103},[100,26545,9259],{"class":145},[100,26547,9262],{"class":106},[100,26549,26550],{"class":102,"line":135},[100,26551,139],{"emptyLinePlaceholder":138},[100,26553,26554,26556,26558,26560,26562,26564,26566,26568],{"class":102,"line":142},[100,26555,372],{"class":114},[100,26557,170],{"class":118},[100,26559,9271],{"class":178},[100,26561,290],{"class":118},[100,26563,9276],{"class":178},[100,26565,170],{"class":118},[100,26567,10180],{"class":122},[100,26569,182],{"class":118},[14,26571,26572],{},"This helps you confirm:",[40,26574,26575,26578],{},[43,26576,26577],{},"the module imports correctly",[43,26579,26580],{},"the member name is valid",[77,26582,26584],{"id":26583},"fix-2-check-your-environment-and-installed-packages","Fix 2: Check your environment and installed packages",[14,26586,26587],{},"Sometimes the package is installed, but not in the Python environment you are actually using.",[14,26589,26590],{},"This often happens when:",[40,26592,26593,26596,26599],{},[43,26594,26595],{},"your editor uses a different interpreter",[43,26597,26598],{},"your terminal uses a different Python installation",[43,26600,26601],{},"you forgot to activate a virtual environment",[14,26603,26604],{},"Useful checks:",[91,26606,26608],{"className":10352,"code":26607,"language":10354,"meta":96,"style":96},"python --version\npython -m pip --version\npython -m pip list\n",[17,26609,26610,26616,26628],{"__ignoreMap":96},[100,26611,26612,26614],{"class":102,"line":103},[100,26613,95],{"class":10361},[100,26615,10365],{"class":10364},[100,26617,26618,26620,26623,26626],{"class":102,"line":135},[100,26619,95],{"class":10361},[100,26621,26622],{"class":10364}," -m",[100,26624,26625],{"class":209}," pip",[100,26627,10365],{"class":10364},[100,26629,26630,26632,26634,26636],{"class":102,"line":142},[100,26631,95],{"class":10361},[100,26633,26622],{"class":10364},[100,26635,26625],{"class":209},[100,26637,26638],{"class":209}," list\n",[14,26640,26641],{},"A good habit is to install packages like this:",[91,26643,26645],{"className":10352,"code":26644,"language":10354,"meta":96,"style":96},"python -m pip install package_name\n",[17,26646,26647],{"__ignoreMap":96},[100,26648,26649,26651,26653,26655,26658],{"class":102,"line":103},[100,26650,95],{"class":10361},[100,26652,26622],{"class":10364},[100,26654,26625],{"class":209},[100,26656,26657],{"class":209}," install",[100,26659,25878],{"class":209},[14,26661,26662,26663,26665,26666,290],{},"This makes ",[17,26664,25872],{}," run with the same Python interpreter as ",[17,26667,95],{},[14,26669,26670],{},"If you are using a virtual environment, activate it before running your script.",[14,26672,26673],{},"If imports work in one editor but not another, they may be using different interpreters.",[14,26675,26676,26677,290],{},"If you need help with setup, see ",[295,26678,26680],{"href":26679},"\u002Fhow-to\u002Fhow-to-create-and-use-a-virtual-environment-in-python\u002F","how to create and use a virtual environment in Python",[77,26682,26684],{"id":26683},"fix-3-check-your-file-and-folder-names","Fix 3: Check your file and folder names",[14,26686,26687],{},"A local file can shadow a real module.",[14,26689,26690,26691,26693],{},"For example, if your file is named ",[17,26692,9846],{},", this can break code like:",[91,26695,26697],{"className":93,"code":26696,"language":95,"meta":96,"style":96},"import random\n",[17,26698,26699],{"__ignoreMap":96},[100,26700,26701,26703],{"class":102,"line":103},[100,26702,9259],{"class":145},[100,26704,9486],{"class":106},[14,26706,26707],{},"Python may import your local file instead of the standard library module.",[14,26709,26710],{},"Problem filenames include:",[40,26712,26713,26717,26721,26725],{},[43,26714,26715],{},[17,26716,9846],{},[43,26718,26719],{},[17,26720,9856],{},[43,26722,26723],{},[17,26724,9861],{},[43,26726,26727],{},[17,26728,26729],{},"sys.py",[14,26731,26732],{},"Rename the file to something else, such as:",[40,26734,26735,26740],{},[43,26736,26737],{},[17,26738,26739],{},"my_random_script.py",[43,26741,26742],{},[17,26743,26744],{},"json_example.py",[14,26746,26747],{},"After renaming, you may also need to remove cached files:",[40,26749,26750,26754],{},[43,26751,26752],{},[17,26753,9945],{},[43,26755,26756,26757,9951],{},"old ",[17,26758,9950],{},[14,26760,26761],{},"This is a very common beginner mistake.",[77,26763,26765],{"id":26764},"fix-4-check-package-structure-and-relative-imports","Fix 4: Check package structure and relative imports",[14,26767,26768],{},"Imports can fail if your files are run from the wrong location.",[14,26770,26771],{},"Imagine a project like this:",[91,26773,26777],{"className":26774,"code":26776,"language":4468,"meta":96},[26775],"language-text","project\u002F\n    app\u002F\n        __init__.py\n        main.py\n        utils.py\n",[17,26778,26776],{"__ignoreMap":96},[14,26780,11734,26781,26783],{},[17,26782,21078],{}," contains:",[91,26785,26787],{"className":93,"code":26786,"language":95,"meta":96,"style":96},"from app import utils\n",[17,26788,26789],{"__ignoreMap":96},[100,26790,26791,26793,26796,26798],{"class":102,"line":103},[100,26792,10201],{"class":145},[100,26794,26795],{"class":106}," app ",[100,26797,9259],{"class":145},[100,26799,26800],{"class":106}," utils\n",[14,26802,26803],{},"running the file from the correct place matters.",[14,26805,26806],{},"Often this works better from the project root:",[91,26808,26810],{"className":10352,"code":26809,"language":10354,"meta":96,"style":96},"python -m app.main\n",[17,26811,26812],{"__ignoreMap":96},[100,26813,26814,26816,26818],{"class":102,"line":103},[100,26815,95],{"class":10361},[100,26817,26622],{"class":10364},[100,26819,26820],{"class":209}," app.main\n",[14,26822,26823],{},"Why this helps:",[40,26825,26826,26829,26832],{},[43,26827,26828],{},"Python treats the code as part of a package",[43,26830,26831],{},"import paths are resolved more consistently",[43,26833,26834],{},"relative import problems are reduced",[14,26836,26837,26838,26841],{},"In some setups, an ",[17,26839,26840],{},"__init__.py"," file is also needed to mark a folder as a package.",[14,26843,26844,26845,3266,26847,290],{},"If you are learning the basics, see ",[295,26846,9828],{"href":25161},[295,26848,26850],{"href":26849},"\u002Fstandard-library\u002Fsys.path-explained","sys.path explained",[77,26852,26854],{"id":26853},"fix-5-look-for-circular-imports","Fix 5: Look for circular imports",[14,26856,26857],{},"A circular import happens when two files import each other.",[14,26859,1844],{},[675,26861,26862],{"id":25345},[17,26863,25348],{},[91,26865,26867],{"className":93,"code":26866,"language":95,"meta":96,"style":96},"from b import hello_from_b\n\ndef hello_from_a():\n    return \"Hello from A\"\n\nprint(hello_from_b())\n",[17,26868,26869,26880,26884,26893,26903,26907],{"__ignoreMap":96},[100,26870,26871,26873,26875,26877],{"class":102,"line":103},[100,26872,10201],{"class":145},[100,26874,25360],{"class":106},[100,26876,9259],{"class":145},[100,26878,26879],{"class":106}," hello_from_b\n",[100,26881,26882],{"class":102,"line":135},[100,26883,139],{"emptyLinePlaceholder":138},[100,26885,26886,26888,26891],{"class":102,"line":142},[100,26887,1078],{"class":1077},[100,26889,26890],{"class":1081}," hello_from_a",[100,26892,1085],{"class":118},[100,26894,26895,26897,26899,26901],{"class":102,"line":152},[100,26896,2552],{"class":145},[100,26898,1708],{"class":205},[100,26900,25387],{"class":209},[100,26902,1714],{"class":205},[100,26904,26905],{"class":102,"line":164},[100,26906,139],{"emptyLinePlaceholder":138},[100,26908,26909,26911,26913,26916],{"class":102,"line":185},[100,26910,372],{"class":114},[100,26912,170],{"class":118},[100,26914,26915],{"class":178},"hello_from_b",[100,26917,3370],{"class":118},[675,26919,26920],{"id":25392},[17,26921,25395],{},[91,26923,26925],{"className":93,"code":26924,"language":95,"meta":96,"style":96},"from a import hello_from_a\n\ndef hello_from_b():\n    return hello_from_a()\n",[17,26926,26927,26938,26942,26951],{"__ignoreMap":96},[100,26928,26929,26931,26933,26935],{"class":102,"line":103},[100,26930,10201],{"class":145},[100,26932,25407],{"class":106},[100,26934,9259],{"class":145},[100,26936,26937],{"class":106}," hello_from_a\n",[100,26939,26940],{"class":102,"line":135},[100,26941,139],{"emptyLinePlaceholder":138},[100,26943,26944,26946,26949],{"class":102,"line":142},[100,26945,1078],{"class":1077},[100,26947,26948],{"class":1081}," hello_from_b",[100,26950,1085],{"class":118},[100,26952,26953,26955,26957],{"class":102,"line":152},[100,26954,2552],{"class":145},[100,26956,26890],{"class":178},[100,26958,4734],{"class":118},[14,26960,26961],{},"This can fail because one module is only partly loaded when the other tries to use it.",[14,26963,26964],{},"Ways to fix circular imports:",[40,26966,26967,26969,26972],{},[43,26968,25509],{},[43,26970,26971],{},"delay an import until inside a function",[43,26973,26974],{},"redesign the module structure so files do not depend on each other directly",[14,26976,26977,26978,26980],{},"For example, moving shared code into ",[17,26979,25518],{}," is often the cleanest fix.",[77,26982,26984],{"id":26983},"how-to-debug-importerror-step-by-step","How to debug ImportError step by step",[14,26986,3277,26987,26989],{},[17,26988,17392],{},", use this checklist.",[675,26991,25528],{"id":25527},[14,26993,26994],{},"Do not stop at the last line.",[14,26996,25531],{},[40,26998,26999,27002,27005],{},[43,27000,27001],{},"the exact module name",[43,27003,27004],{},"the exact imported name",[43,27006,27007],{},"the file where the problem starts",[675,27009,27011],{"id":27010},"_2-decide-what-kind-of-import-problem-it-is","2. Decide what kind of import problem it is",[14,27013,27014],{},"Ask:",[40,27016,27017,27020],{},[43,27018,27019],{},"Is Python unable to find the module at all?",[43,27021,27022],{},"Or did Python find the module, but fail to import a name from it?",[14,27024,27025,27026,290],{},"If the module itself is missing, compare with ",[295,27027,27028],{"href":17456},"ModuleNotFoundError: No module named X",[675,27030,27032,27033],{"id":27031},"_3-print-syspath","3. Print ",[17,27034,26210],{},[14,27036,27037],{},"This shows where Python is searching for modules.",[91,27039,27041],{"className":10352,"code":27040,"language":10354,"meta":96,"style":96},"python -c \"import sys; print(sys.path)\"\n",[17,27042,27043],{"__ignoreMap":96},[100,27044,27045,27047,27049,27051,27054],{"class":102,"line":103},[100,27046,95],{"class":10361},[100,27048,10450],{"class":10364},[100,27050,1708],{"class":205},[100,27052,27053],{"class":209},"import sys; print(sys.path)",[100,27055,1714],{"class":205},[14,27057,27058,27059,290],{},"You can also learn more in the ",[295,27060,27062],{"href":27061},"\u002Fstandard-library\u002Fpython-sys-module-overview","Python sys module overview",[675,27064,27066],{"id":27065},"_4-check-for-conflicting-filenames","4. Check for conflicting filenames",[14,27068,27069],{},"Look in your current folder for files named like real modules:",[40,27071,27072,27076,27080],{},[43,27073,27074],{},[17,27075,9856],{},[43,27077,27078],{},[17,27079,9846],{},[43,27081,27082],{},[17,27083,9851],{},[14,27085,27086],{},"These can cause confusing import behavior.",[675,27088,27090],{"id":27089},"_5-test-the-import-in-a-very-small-file","5. Test the import in a very small file",[14,27092,27093],{},"Create a separate file with only the import:",[91,27095,27097],{"className":93,"code":27096,"language":95,"meta":96,"style":96},"import math\nprint(math.__file__)\nprint(math.sqrt(25))\n",[17,27098,27099,27105,27119],{"__ignoreMap":96},[100,27100,27101,27103],{"class":102,"line":103},[100,27102,9259],{"class":145},[100,27104,9262],{"class":106},[100,27106,27107,27109,27111,27113,27115,27117],{"class":102,"line":135},[100,27108,372],{"class":114},[100,27110,170],{"class":118},[100,27112,9271],{"class":178},[100,27114,290],{"class":118},[100,27116,10041],{"class":527},[100,27118,215],{"class":118},[100,27120,27121,27123,27125,27127,27129,27131,27133,27136],{"class":102,"line":142},[100,27122,372],{"class":114},[100,27124,170],{"class":118},[100,27126,9271],{"class":178},[100,27128,290],{"class":118},[100,27130,9276],{"class":178},[100,27132,170],{"class":118},[100,27134,27135],{"class":122},"25",[100,27137,182],{"class":118},[14,27139,27140],{},"Or for another module:",[91,27142,27144],{"className":93,"code":27143,"language":95,"meta":96,"style":96},"import module_name\nprint(module_name.__file__)\n",[17,27145,27146,27153],{"__ignoreMap":96},[100,27147,27148,27150],{"class":102,"line":103},[100,27149,9259],{"class":145},[100,27151,27152],{"class":106}," module_name\n",[100,27154,27155,27157,27159,27161,27163,27165],{"class":102,"line":135},[100,27156,372],{"class":114},[100,27158,170],{"class":118},[100,27160,10411],{"class":178},[100,27162,290],{"class":118},[100,27164,10041],{"class":527},[100,27166,215],{"class":118},[14,27168,27169],{},"This helps you see what Python is actually importing.",[77,27171,7081],{"id":7080},[14,27173,27174],{},"Some errors look similar but mean different things.",[675,27176,27178],{"id":27177},"modulenotfounderror",[17,27179,17397],{},[14,27181,27182],{},"Python cannot find the module at all.",[14,27184,27185],{},"Example idea:",[91,27187,27189],{"className":93,"code":27188,"language":95,"meta":96,"style":96},"import package_that_is_not_installed\n",[17,27190,27191],{"__ignoreMap":96},[100,27192,27193,27195],{"class":102,"line":103},[100,27194,9259],{"class":145},[100,27196,27197],{"class":106}," package_that_is_not_installed\n",[14,27199,23054,27200,290],{},[295,27201,27028],{"href":17456},[675,27203,27205],{"id":27204},"importerror-cannot-import-name",[17,27206,9965],{},[14,27208,27209],{},"Python found the module, but not the name you requested.",[14,27211,1844],{},[91,27213,27214],{"className":93,"code":26328,"language":95,"meta":96,"style":96},[17,27215,27216],{"__ignoreMap":96},[100,27217,27218,27220,27222,27224],{"class":102,"line":103},[100,27219,10201],{"class":145},[100,27221,10204],{"class":106},[100,27223,9259],{"class":145},[100,27225,26341],{"class":106},[14,27227,23054,27228,290],{},[295,27229,9965],{"href":26420},[675,27231,27233],{"id":27232},"attributeerror-module-has-no-attribute",[17,27234,9246],{},[14,27236,27237],{},"The import succeeded, but you used a name that does not exist after importing.",[14,27239,1844],{},[91,27241,27243],{"className":93,"code":27242,"language":95,"meta":96,"style":96},"import math\nprint(math.square_root(9))\n",[17,27244,27245,27251],{"__ignoreMap":96},[100,27246,27247,27249],{"class":102,"line":103},[100,27248,9259],{"class":145},[100,27250,9262],{"class":106},[100,27252,27253,27255,27257,27259,27261,27263,27265,27267],{"class":102,"line":135},[100,27254,372],{"class":114},[100,27256,170],{"class":118},[100,27258,9271],{"class":178},[100,27260,290],{"class":118},[100,27262,26350],{"class":178},[100,27264,170],{"class":118},[100,27266,9281],{"class":122},[100,27268,182],{"class":118},[14,27270,23054,27271,290],{},[295,27272,9246],{"href":5541},[77,27274,1344],{"id":1343},[14,27276,27277,27278,89],{},"Here are the most common reasons for ",[17,27279,17392],{},[40,27281,27282,27285,27288,27291,27294,27297,27300],{},[43,27283,27284],{},"misspelled module name",[43,27286,27287],{},"misspelled imported function or class name",[43,27289,27290],{},"package not installed",[43,27292,27293],{},"wrong virtual environment",[43,27295,27296],{},"local file shadows a real module",[43,27298,27299],{},"circular import between project files",[43,27301,27302],{},"incorrect package or relative import",[77,27304,20239],{"id":20238},[14,27306,27307],{},"These commands are often enough to find the problem:",[91,27309,27311],{"className":10352,"code":27310,"language":10354,"meta":96,"style":96},"python --version\npython -m pip --version\npython -m pip list\npython -c \"import sys; print(sys.path)\"\npython -c \"import module_name; print(module_name.__file__)\"\n",[17,27312,27313,27319,27329,27339,27351],{"__ignoreMap":96},[100,27314,27315,27317],{"class":102,"line":103},[100,27316,95],{"class":10361},[100,27318,10365],{"class":10364},[100,27320,27321,27323,27325,27327],{"class":102,"line":135},[100,27322,95],{"class":10361},[100,27324,26622],{"class":10364},[100,27326,26625],{"class":209},[100,27328,10365],{"class":10364},[100,27330,27331,27333,27335,27337],{"class":102,"line":142},[100,27332,95],{"class":10361},[100,27334,26622],{"class":10364},[100,27336,26625],{"class":209},[100,27338,26638],{"class":209},[100,27340,27341,27343,27345,27347,27349],{"class":102,"line":152},[100,27342,95],{"class":10361},[100,27344,10450],{"class":10364},[100,27346,1708],{"class":205},[100,27348,27053],{"class":209},[100,27350,1714],{"class":205},[100,27352,27353,27355,27357,27359,27362],{"class":102,"line":164},[100,27354,95],{"class":10361},[100,27356,10450],{"class":10364},[100,27358,1708],{"class":205},[100,27360,27361],{"class":209},"import module_name; print(module_name.__file__)",[100,27363,1714],{"class":205},[14,27365,27366],{},"What they help you check:",[40,27368,27369,27372,27378,27381,27384],{},[43,27370,27371],{},"which Python version is running",[43,27373,27374,27375,27377],{},"which ",[17,27376,25872],{}," belongs to that Python",[43,27379,27380],{},"which packages are installed",[43,27382,27383],{},"where Python searches for modules",[43,27385,27386],{},"which file Python actually imported",[77,27388,1514],{"id":1513},[675,27390,25919],{"id":25918},[14,27392,27393,27395],{},[17,27394,17397],{}," means Python could not find the module.",[14,27397,27398,27400],{},[17,27399,17392],{}," is broader and can also happen when a module is found but a requested name cannot be imported.",[675,27402,27404],{"id":27403},"why-does-my-import-work-in-one-editor-but-not-another","Why does my import work in one editor but not another?",[14,27406,27407],{},"The editors may be using different Python interpreters or virtual environments.",[675,27409,27411],{"id":27410},"can-my-filename-cause-importerror","Can my filename cause ImportError?",[14,27413,27414],{},"Yes. If your file is named like a real module, Python may import your file instead of the intended module.",[675,27416,27418],{"id":27417},"how-do-i-check-where-python-is-importing-from","How do I check where Python is importing from?",[14,27420,27421,27422,27424],{},"Import the module and print ",[17,27423,10539],{}," to see its location.",[14,27426,1844],{},[91,27428,27430],{"className":93,"code":27429,"language":95,"meta":96,"style":96},"import math\nprint(math.__file__)\n",[17,27431,27432,27438],{"__ignoreMap":96},[100,27433,27434,27436],{"class":102,"line":103},[100,27435,9259],{"class":145},[100,27437,9262],{"class":106},[100,27439,27440,27442,27444,27446,27448,27450],{"class":102,"line":135},[100,27441,372],{"class":114},[100,27443,170],{"class":118},[100,27445,9271],{"class":178},[100,27447,290],{"class":118},[100,27449,10041],{"class":527},[100,27451,215],{"class":118},[77,27453,1554],{"id":1553},[40,27455,27456,27460,27464,27468,27472,27477,27481,27485],{},[43,27457,27458],{},[295,27459,27028],{"href":17456},[43,27461,27462],{},[295,27463,9965],{"href":26420},[43,27465,27466],{},[295,27467,10666],{"href":25158},[43,27469,27470],{},[295,27471,10683],{"href":25161},[43,27473,27474],{},[295,27475,27476],{"href":26679},"How to create and use a virtual environment in Python",[43,27478,27479],{},[295,27480,26530],{"href":4562},[43,27482,27483],{},[295,27484,26850],{"href":26849},[43,27486,27487],{},[295,27488,9246],{"href":5541},[1589,27490,27491],{},"html pre.shiki code .sVHd0, html code.shiki .sVHd0{--shiki-light:#39ADB5;--shiki-light-font-style:italic;--shiki-default:#D73A49;--shiki-default-font-style:inherit;--shiki-dark:#F97583;--shiki-dark-font-style:inherit}html pre.shiki code .su5hD, html code.shiki .su5hD{--shiki-light:#90A4AE;--shiki-default:#24292E;--shiki-dark:#E1E4E8}html pre.shiki code .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 pre.shiki code .skxfh, html code.shiki .skxfh{--shiki-light:#E53935;--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 .srdBf, html code.shiki .srdBf{--shiki-light:#F76D47;--shiki-default:#005CC5;--shiki-dark:#79B8FF}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 .sbgvK, html code.shiki .sbgvK{--shiki-light:#E2931D;--shiki-default:#6F42C1;--shiki-dark:#B392F0}html pre.shiki code .stzsN, html code.shiki .stzsN{--shiki-light:#91B859;--shiki-default:#005CC5;--shiki-dark:#79B8FF}html pre.shiki code .s_sjI, html code.shiki .s_sjI{--shiki-light:#91B859;--shiki-default:#032F62;--shiki-dark:#9ECBFF}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 .sjJ54, html code.shiki .sjJ54{--shiki-light:#39ADB5;--shiki-default:#032F62;--shiki-dark:#9ECBFF}html pre.shiki code .s_hVV, html code.shiki .s_hVV{--shiki-light:#90A4AE;--shiki-default:#005CC5;--shiki-dark:#79B8FF}",{"title":96,"searchDepth":135,"depth":135,"links":27493},[27494,27495,27496,27497,27498,27499,27500,27501,27502,27506,27514,27519,27520,27521,27527],{"id":26115,"depth":135,"text":26116},{"id":26241,"depth":135,"text":26242},{"id":26288,"depth":135,"text":26289},{"id":26321,"depth":135,"text":26322},{"id":26423,"depth":135,"text":26424},{"id":26583,"depth":135,"text":26584},{"id":26683,"depth":135,"text":26684},{"id":26764,"depth":135,"text":26765},{"id":26853,"depth":135,"text":26854,"children":27503},[27504,27505],{"id":25345,"depth":142,"text":25348},{"id":25392,"depth":142,"text":25395},{"id":26983,"depth":135,"text":26984,"children":27507},[27508,27509,27510,27512,27513],{"id":25527,"depth":142,"text":25528},{"id":27010,"depth":142,"text":27011},{"id":27031,"depth":142,"text":27511},"3. Print sys.path",{"id":27065,"depth":142,"text":27066},{"id":27089,"depth":142,"text":27090},{"id":7080,"depth":135,"text":7081,"children":27515},[27516,27517,27518],{"id":27177,"depth":142,"text":17397},{"id":27204,"depth":142,"text":9965},{"id":27232,"depth":142,"text":9246},{"id":1343,"depth":135,"text":1344},{"id":20238,"depth":135,"text":20239},{"id":1513,"depth":135,"text":1514,"children":27522},[27523,27524,27525,27526],{"id":25918,"depth":142,"text":25919},{"id":27403,"depth":142,"text":27404},{"id":27410,"depth":142,"text":27411},{"id":27417,"depth":142,"text":27418},{"id":1553,"depth":135,"text":1554},"Master importerror in python causes and fixes in our comprehensive Python beginner guide.",{},"\u002Ferrors\u002Fimporterror-in-python-causes-and-fixes",{"title":26059,"description":27528},"errors\u002Fimporterror-in-python-causes-and-fixes","5oykHFp5vqern46CBvoAzW5dk1q3_NKR9xZfhcVoA-Y",{"id":27535,"title":10656,"body":27536,"description":28798,"extension":1623,"meta":28799,"navigation":138,"path":9960,"seo":28800,"stem":28801,"__hash__":28802},"content\u002Ferrors\u002Fimporterror-no-module-named-x-fix.md",{"type":7,"value":27537,"toc":28766},[27538,27541,27544,27547,27561,27564,27566,27582,27589,27591,27609,27616,27633,27636,27638,27643,27646,27659,27665,27669,27672,27683,27686,27700,27706,27711,27714,27716,27718,27740,27744,27747,27750,27766,27776,27778,27794,27797,27800,27817,27824,27828,27831,27833,27847,27849,27866,27869,27887,27889,27901,27904,27922,27925,27929,27932,27935,27938,27954,27957,27974,27977,27993,27998,28002,28005,28008,28014,28017,28027,28030,28033,28047,28050,28061,28066,28069,28075,28081,28091,28093,28113,28117,28120,28123,28129,28134,28165,28172,28175,28181,28183,28186,28190,28208,28211,28215,28231,28234,28243,28247,28262,28268,28272,28289,28291,28307,28311,28329,28331,28349,28352,28356,28360,28363,28419,28422,28427,28429,28445,28448,28452,28455,28466,28471,28473,28487,28490,28506,28510,28513,28519,28522,28533,28535,28549,28553,28556,28572,28575,28578,28605,28608,28612,28615,28621,28629,28635,28640,28642,28646,28649,28652,28678,28681,28683,28688,28693,28697,28700,28710,28716,28720,28726,28729,28731,28763],[10,27539,10656],{"id":27540},"importerror-no-module-named-x-fix",[14,27542,27543],{},"This error happens when Python tries to import a module but cannot find it.",[14,27545,27546],{},"In many cases, the fix is simple:",[40,27548,27549,27552,27555,27558],{},[43,27550,27551],{},"The package is not installed",[43,27553,27554],{},"It was installed in the wrong Python environment",[43,27556,27557],{},"The import name is wrong",[43,27559,27560],{},"Your own file name is blocking the real module",[14,27562,27563],{},"Start with the fastest checks first. That usually solves the problem quickly.",[77,27565,80],{"id":79},[91,27567,27568],{"className":10352,"code":26644,"language":10354,"meta":96,"style":96},[17,27569,27570],{"__ignoreMap":96},[100,27571,27572,27574,27576,27578,27580],{"class":102,"line":103},[100,27573,95],{"class":10361},[100,27575,26622],{"class":10364},[100,27577,26625],{"class":209},[100,27579,26657],{"class":209},[100,27581,25878],{"class":209},[14,27583,27584,27585,27588],{},"Use the same Python interpreter that runs your script. Replace ",[17,27586,27587],{},"package_name"," with the real package name.",[14,27590,580],{},[91,27592,27594],{"className":10352,"code":27593,"language":10354,"meta":96,"style":96},"python -m pip install requests\n",[17,27595,27596],{"__ignoreMap":96},[100,27597,27598,27600,27602,27604,27606],{"class":102,"line":103},[100,27599,95],{"class":10361},[100,27601,26622],{"class":10364},[100,27603,26625],{"class":209},[100,27605,26657],{"class":209},[100,27607,27608],{"class":209}," requests\n",[14,27610,27611,27612,27615],{},"If your system uses ",[17,27613,27614],{},"python3",", try:",[91,27617,27619],{"className":10352,"code":27618,"language":10354,"meta":96,"style":96},"python3 -m pip install requests\n",[17,27620,27621],{"__ignoreMap":96},[100,27622,27623,27625,27627,27629,27631],{"class":102,"line":103},[100,27624,27614],{"class":10361},[100,27626,26622],{"class":10364},[100,27628,26625],{"class":209},[100,27630,26657],{"class":209},[100,27632,27608],{"class":209},[27634,27635],"hr",{},[77,27637,5881],{"id":5880},[14,27639,27640,27642],{},[17,27641,9961],{}," means Python looked for a module and could not find it.",[14,27644,27645],{},"The missing module might be:",[40,27647,27648,27653,27656],{},[43,27649,27650,27651],{},"A third-party package such as ",[17,27652,17450],{},[43,27654,27655],{},"A local Python file you created",[43,27657,27658],{},"A module that exists in a different virtual environment",[14,27660,27661,27662,290],{},"This page focuses on the specific error message ",[22,27663,27664],{},"\"No module named X\"",[77,27666,27668],{"id":27667},"common-message-example","Common message example",[14,27670,27671],{},"A common example looks like this:",[91,27673,27675],{"className":93,"code":27674,"language":95,"meta":96,"style":96},"import requests\n",[17,27676,27677],{"__ignoreMap":96},[100,27678,27679,27681],{"class":102,"line":103},[100,27680,9259],{"class":145},[100,27682,27608],{"class":106},[14,27684,27685],{},"Possible error:",[91,27687,27689],{"className":93,"code":27688,"language":95,"meta":96,"style":96},"ImportError: No module named requests\n",[17,27690,27691],{"__ignoreMap":96},[100,27692,27693,27695,27697],{"class":102,"line":103},[100,27694,17392],{"class":191},[100,27696,89],{"class":118},[100,27698,27699],{"class":106}," No module named requests\n",[14,27701,27702,27703,290],{},"The important part is the name after ",[17,27704,27705],{},"named",[14,27707,27708,27709,290],{},"In this case, Python could not find ",[17,27710,17450],{},[14,27712,27713],{},"Check that exact name before changing anything else.",[77,27715,9508],{"id":9507},[14,27717,9511],{},[40,27719,27720,27722,27725,27728,27731,27734,27737],{},[43,27721,27551],{},[43,27723,27724],{},"The package was installed in a different Python version or virtual environment",[43,27726,27727],{},"The module name is spelled wrong",[43,27729,27730],{},"The installed package name and import name are different",[43,27732,27733],{},"Your file or folder name shadows the real module",[43,27735,27736],{},"You are running the script from the wrong folder",[43,27738,27739],{},"The module exists, but Python cannot see its location",[77,27741,27743],{"id":27742},"fix-1-install-the-missing-package","Fix 1: Install the missing package",[14,27745,27746],{},"If the missing module is a third-party package, install it first.",[14,27748,27749],{},"Use:",[91,27751,27752],{"className":10352,"code":26644,"language":10354,"meta":96,"style":96},[17,27753,27754],{"__ignoreMap":96},[100,27755,27756,27758,27760,27762,27764],{"class":102,"line":103},[100,27757,95],{"class":10361},[100,27759,26622],{"class":10364},[100,27761,26625],{"class":209},[100,27763,26657],{"class":209},[100,27765,25878],{"class":209},[14,27767,27768,27769,27772,27773,27775],{},"This is better than plain ",[17,27770,27771],{},"pip install"," because it helps ",[17,27774,25872],{}," use the same Python interpreter as your script.",[14,27777,1844],{},[91,27779,27780],{"className":10352,"code":27593,"language":10354,"meta":96,"style":96},[17,27781,27782],{"__ignoreMap":96},[100,27783,27784,27786,27788,27790,27792],{"class":102,"line":103},[100,27785,95],{"class":10361},[100,27787,26622],{"class":10364},[100,27789,26625],{"class":209},[100,27791,26657],{"class":209},[100,27793,27608],{"class":209},[14,27795,27796],{},"Then run your script again.",[14,27798,27799],{},"You can also check whether the package is already installed:",[91,27801,27803],{"className":10352,"code":27802,"language":10354,"meta":96,"style":96},"python -m pip show requests\n",[17,27804,27805],{"__ignoreMap":96},[100,27806,27807,27809,27811,27813,27815],{"class":102,"line":103},[100,27808,95],{"class":10361},[100,27810,26622],{"class":10364},[100,27812,26625],{"class":209},[100,27814,25875],{"class":209},[100,27816,27608],{"class":209},[14,27818,27819,27820,290],{},"If you are new to package installation, see ",[295,27821,27823],{"href":27822},"\u002Fhow-to\u002Fhow-to-install-a-python-package-with-pip\u002F","how to install a Python package with pip",[77,27825,27827],{"id":27826},"fix-2-check-the-import-name","Fix 2: Check the import name",[14,27829,27830],{},"Sometimes the package name and the import name are not the same.",[14,27832,1844],{},[40,27834,27835,27841],{},[43,27836,27837,27838],{},"Install with ",[17,27839,27840],{},"beautifulsoup4",[43,27842,27843,27844],{},"Import with ",[17,27845,27846],{},"bs4",[14,27848,26464],{},[91,27850,27852],{"className":93,"code":27851,"language":95,"meta":96,"style":96},"from bs4 import BeautifulSoup\n",[17,27853,27854],{"__ignoreMap":96},[100,27855,27856,27858,27861,27863],{"class":102,"line":103},[100,27857,10201],{"class":145},[100,27859,27860],{"class":106}," bs4 ",[100,27862,9259],{"class":145},[100,27864,27865],{"class":106}," BeautifulSoup\n",[14,27867,27868],{},"Install command:",[91,27870,27872],{"className":10352,"code":27871,"language":10354,"meta":96,"style":96},"python -m pip install beautifulsoup4\n",[17,27873,27874],{"__ignoreMap":96},[100,27875,27876,27878,27880,27882,27884],{"class":102,"line":103},[100,27877,95],{"class":10361},[100,27879,26622],{"class":10364},[100,27881,26625],{"class":209},[100,27883,26657],{"class":209},[100,27885,27886],{"class":209}," beautifulsoup4\n",[14,27888,12066],{},[91,27890,27892],{"className":93,"code":27891,"language":95,"meta":96,"style":96},"import PIL\n",[17,27893,27894],{"__ignoreMap":96},[100,27895,27896,27898],{"class":102,"line":103},[100,27897,9259],{"class":145},[100,27899,27900],{"class":527}," PIL\n",[14,27902,27903],{},"The package is installed with:",[91,27905,27907],{"className":10352,"code":27906,"language":10354,"meta":96,"style":96},"python -m pip install Pillow\n",[17,27908,27909],{"__ignoreMap":96},[100,27910,27911,27913,27915,27917,27919],{"class":102,"line":103},[100,27912,95],{"class":10361},[100,27914,26622],{"class":10364},[100,27916,26625],{"class":209},[100,27918,26657],{"class":209},[100,27920,27921],{"class":209}," Pillow\n",[14,27923,27924],{},"So if installation worked but the import still fails, check the package documentation and confirm the correct import name.",[77,27926,27928],{"id":27927},"fix-3-check-your-virtual-environment","Fix 3: Check your virtual environment",[14,27930,27931],{},"If you use a virtual environment, you must activate it before installing packages or running your script.",[14,27933,27934],{},"A package installed in one environment is not available in another.",[14,27936,27937],{},"Typical problem:",[40,27939,27940,27946,27949],{},[43,27941,27942,27943,27945],{},"You installed ",[17,27944,17450],{}," globally",[43,27947,27948],{},"Your script runs inside a virtual environment",[43,27950,27951,27952],{},"That virtual environment does not have ",[17,27953,17450],{},[14,27955,27956],{},"Example workflow:",[91,27958,27960],{"className":10352,"code":27959,"language":10354,"meta":96,"style":96},"python -m venv .venv\n",[17,27961,27962],{"__ignoreMap":96},[100,27963,27964,27966,27968,27971],{"class":102,"line":103},[100,27965,95],{"class":10361},[100,27967,26622],{"class":10364},[100,27969,27970],{"class":209}," venv",[100,27972,27973],{"class":209}," .venv\n",[14,27975,27976],{},"Activate it, then install:",[91,27978,27979],{"className":10352,"code":27593,"language":10354,"meta":96,"style":96},[17,27980,27981],{"__ignoreMap":96},[100,27982,27983,27985,27987,27989,27991],{"class":102,"line":103},[100,27984,95],{"class":10361},[100,27986,26622],{"class":10364},[100,27988,26625],{"class":209},[100,27990,26657],{"class":209},[100,27992,27608],{"class":209},[14,27994,27995,27996,290],{},"If virtual environments are confusing, read ",[295,27997,26680],{"href":26679},[77,27999,28001],{"id":28000},"fix-4-look-for-naming-conflicts","Fix 4: Look for naming conflicts",[14,28003,28004],{},"Your own file or folder can block Python from importing the real module.",[14,28006,28007],{},"For example, this file name is a problem:",[91,28009,28012],{"className":28010,"code":28011,"language":4468,"meta":96},[26775],"requests.py\n",[17,28013,28011],{"__ignoreMap":96},[14,28015,28016],{},"Then this code:",[91,28018,28019],{"className":93,"code":27674,"language":95,"meta":96,"style":96},[17,28020,28021],{"__ignoreMap":96},[100,28022,28023,28025],{"class":102,"line":103},[100,28024,9259],{"class":145},[100,28026,27608],{"class":106},[14,28028,28029],{},"may import your file instead of the real package, or fail with an import error.",[14,28031,28032],{},"Other common bad file names:",[40,28034,28035,28039,28043],{},[43,28036,28037],{},[17,28038,9856],{},[43,28040,28041],{},[17,28042,26729],{},[43,28044,28045],{},[17,28046,9846],{},[14,28048,28049],{},"Also check for:",[40,28051,28052,28055],{},[43,28053,28054],{},"A folder with the same name as the package",[43,28056,28057,28058,28060],{},"Old ",[17,28059,10130],{}," files after renaming a file",[14,28062,28063,28064,290],{},"If needed, rename your file and delete ",[17,28065,10130],{},[14,28067,28068],{},"Example problem:",[91,28070,28073],{"className":28071,"code":28072,"language":4468,"meta":96},[26775],"project\u002F\n├─ requests.py\n└─ app.py\n",[17,28074,28072],{"__ignoreMap":96},[14,28076,28077,28078,89],{},"Inside ",[17,28079,28080],{},"app.py",[91,28082,28083],{"className":93,"code":27674,"language":95,"meta":96,"style":96},[17,28084,28085],{"__ignoreMap":96},[100,28086,28087,28089],{"class":102,"line":103},[100,28088,9259],{"class":145},[100,28090,27608],{"class":106},[14,28092,15034],{},[40,28094,28095,28105,28111],{},[43,28096,28097,28098,28101,28102],{},"Rename ",[17,28099,28100],{},"requests.py"," to something like ",[17,28103,28104],{},"my_requests_test.py",[43,28106,28107,28108,28110],{},"Delete any ",[17,28109,10130],{}," folder",[43,28112,9954],{},[77,28114,28116],{"id":28115},"fix-5-check-local-files-and-folders","Fix 5: Check local files and folders",[14,28118,28119],{},"If you are importing your own module, make sure the file actually exists and is in a place Python can find.",[14,28121,28122],{},"Example folder:",[91,28124,28127],{"className":28125,"code":28126,"language":4468,"meta":96},[26775],"project\u002F\n├─ main.py\n└─ helpers.py\n",[17,28128,28126],{"__ignoreMap":96},[14,28130,28131,28132,89],{},"This works in ",[17,28133,21078],{},[91,28135,28137],{"className":93,"code":28136,"language":95,"meta":96,"style":96},"import helpers\n\nprint(\"Imported successfully\")\n",[17,28138,28139,28146,28150],{"__ignoreMap":96},[100,28140,28141,28143],{"class":102,"line":103},[100,28142,9259],{"class":145},[100,28144,28145],{"class":106}," helpers\n",[100,28147,28148],{"class":102,"line":135},[100,28149,139],{"emptyLinePlaceholder":138},[100,28151,28152,28154,28156,28158,28161,28163],{"class":102,"line":142},[100,28153,372],{"class":114},[100,28155,170],{"class":118},[100,28157,206],{"class":205},[100,28159,28160],{"class":209},"Imported successfully",[100,28162,206],{"class":205},[100,28164,215],{"class":118},[14,28166,28167,28168,28171],{},"But if ",[17,28169,28170],{},"helpers.py"," is missing, moved, or in another folder, Python may raise this error.",[14,28173,28174],{},"Also, imports can fail if you run the script from a different working directory than expected.",[14,28176,28177,28178,290],{},"If you want to understand why location matters, read ",[295,28179,28180],{"href":25158},"how imports work in Python",[77,28182,3274],{"id":3273},[14,28184,28185],{},"If the error is still not clear, these checks usually reveal the problem.",[675,28187,28189],{"id":28188},"check-which-python-runs-your-script","Check which Python runs your script",[91,28191,28193],{"className":10352,"code":28192,"language":10354,"meta":96,"style":96},"python -c \"import sys; print(sys.executable)\"\n",[17,28194,28195],{"__ignoreMap":96},[100,28196,28197,28199,28201,28203,28206],{"class":102,"line":103},[100,28198,95],{"class":10361},[100,28200,10450],{"class":10364},[100,28202,1708],{"class":205},[100,28204,28205],{"class":209},"import sys; print(sys.executable)",[100,28207,1714],{"class":205},[14,28209,28210],{},"This shows the exact Python interpreter being used.",[675,28212,28214],{"id":28213},"check-where-python-looks-for-modules","Check where Python looks for modules",[91,28216,28217],{"className":10352,"code":27040,"language":10354,"meta":96,"style":96},[17,28218,28219],{"__ignoreMap":96},[100,28220,28221,28223,28225,28227,28229],{"class":102,"line":103},[100,28222,95],{"class":10361},[100,28224,10450],{"class":10364},[100,28226,1708],{"class":205},[100,28228,27053],{"class":209},[100,28230,1714],{"class":205},[14,28232,28233],{},"This prints the list of folders Python searches for imports.",[14,28235,28236,28237,28239,28240,290],{},"You can learn more in ",[295,28238,26850],{"href":26849}," and the ",[295,28241,27062],{"href":28242},"\u002Fstandard-library\u002Fpython-sys-module-overview\u002F",[675,28244,28246],{"id":28245},"check-which-python-pip-uses","Check which Python pip uses",[91,28248,28250],{"className":10352,"code":28249,"language":10354,"meta":96,"style":96},"python -m pip --version\n",[17,28251,28252],{"__ignoreMap":96},[100,28253,28254,28256,28258,28260],{"class":102,"line":103},[100,28255,95],{"class":10361},[100,28257,26622],{"class":10364},[100,28259,26625],{"class":209},[100,28261,10365],{"class":10364},[14,28263,28264,28265,28267],{},"This helps confirm whether ",[17,28266,25872],{}," is connected to the same Python installation.",[675,28269,28271],{"id":28270},"check-whether-the-package-is-installed","Check whether the package is installed",[91,28273,28275],{"className":10352,"code":28274,"language":10354,"meta":96,"style":96},"python -m pip show package_name\n",[17,28276,28277],{"__ignoreMap":96},[100,28278,28279,28281,28283,28285,28287],{"class":102,"line":103},[100,28280,95],{"class":10361},[100,28282,26622],{"class":10364},[100,28284,26625],{"class":209},[100,28286,25875],{"class":209},[100,28288,25878],{"class":209},[14,28290,1844],{},[91,28292,28293],{"className":10352,"code":27802,"language":10354,"meta":96,"style":96},[17,28294,28295],{"__ignoreMap":96},[100,28296,28297,28299,28301,28303,28305],{"class":102,"line":103},[100,28298,95],{"class":10361},[100,28300,26622],{"class":10364},[100,28302,26625],{"class":209},[100,28304,25875],{"class":209},[100,28306,27608],{"class":209},[675,28308,28310],{"id":28309},"test-the-import-directly","Test the import directly",[91,28312,28314],{"className":10352,"code":28313,"language":10354,"meta":96,"style":96},"python -c \"import package_name\"\n",[17,28315,28316],{"__ignoreMap":96},[100,28317,28318,28320,28322,28324,28327],{"class":102,"line":103},[100,28319,95],{"class":10361},[100,28321,10450],{"class":10364},[100,28323,1708],{"class":205},[100,28325,28326],{"class":209},"import package_name",[100,28328,1714],{"class":205},[14,28330,1844],{},[91,28332,28334],{"className":10352,"code":28333,"language":10354,"meta":96,"style":96},"python -c \"import requests\"\n",[17,28335,28336],{"__ignoreMap":96},[100,28337,28338,28340,28342,28344,28347],{"class":102,"line":103},[100,28339,95],{"class":10361},[100,28341,10450],{"class":10364},[100,28343,1708],{"class":205},[100,28345,28346],{"class":209},"import requests",[100,28348,1714],{"class":205},[14,28350,28351],{},"If this command fails, the package still is not available to that Python interpreter.",[77,28353,28355],{"id":28354},"example-fixes-to-show","Example fixes to show",[675,28357,28359],{"id":28358},"example-1-missing-third-party-package","Example 1: Missing third-party package",[14,28361,28362],{},"Code:",[91,28364,28366],{"className":93,"code":28365,"language":95,"meta":96,"style":96},"import requests\n\nresponse = requests.get(\"https:\u002F\u002Fexample.com\")\nprint(response.status_code)\n",[17,28367,28368,28374,28378,28403],{"__ignoreMap":96},[100,28369,28370,28372],{"class":102,"line":103},[100,28371,9259],{"class":145},[100,28373,27608],{"class":106},[100,28375,28376],{"class":102,"line":135},[100,28377,139],{"emptyLinePlaceholder":138},[100,28379,28380,28383,28385,28388,28390,28392,28394,28396,28399,28401],{"class":102,"line":142},[100,28381,28382],{"class":106},"response ",[100,28384,111],{"class":110},[100,28386,28387],{"class":106}," requests",[100,28389,290],{"class":118},[100,28391,12412],{"class":178},[100,28393,170],{"class":118},[100,28395,206],{"class":205},[100,28397,28398],{"class":209},"https:\u002F\u002Fexample.com",[100,28400,206],{"class":205},[100,28402,215],{"class":118},[100,28404,28405,28407,28409,28412,28414,28417],{"class":102,"line":152},[100,28406,372],{"class":114},[100,28408,170],{"class":118},[100,28410,28411],{"class":178},"response",[100,28413,290],{"class":118},[100,28415,28416],{"class":2494},"status_code",[100,28418,215],{"class":118},[14,28420,28421],{},"Error:",[91,28423,28425],{"className":28424,"code":27688,"language":4468,"meta":96},[26775],[17,28426,27688],{"__ignoreMap":96},[14,28428,15034],{},[91,28430,28431],{"className":10352,"code":27593,"language":10354,"meta":96,"style":96},[17,28432,28433],{"__ignoreMap":96},[100,28434,28435,28437,28439,28441,28443],{"class":102,"line":103},[100,28436,95],{"class":10361},[100,28438,26622],{"class":10364},[100,28440,26625],{"class":209},[100,28442,26657],{"class":209},[100,28444,27608],{"class":209},[14,28446,28447],{},"Then run the script again.",[675,28449,28451],{"id":28450},"example-2-wrong-import-name","Example 2: Wrong import name",[14,28453,28454],{},"Incorrect:",[91,28456,28458],{"className":93,"code":28457,"language":95,"meta":96,"style":96},"import beautifulsoup4\n",[17,28459,28460],{"__ignoreMap":96},[100,28461,28462,28464],{"class":102,"line":103},[100,28463,9259],{"class":145},[100,28465,27886],{"class":106},[14,28467,28468,28469,290],{},"This fails because the import name is not ",[17,28470,27840],{},[14,28472,9750],{},[91,28474,28475],{"className":93,"code":27851,"language":95,"meta":96,"style":96},[17,28476,28477],{"__ignoreMap":96},[100,28478,28479,28481,28483,28485],{"class":102,"line":103},[100,28480,10201],{"class":145},[100,28482,27860],{"class":106},[100,28484,9259],{"class":145},[100,28486,27865],{"class":106},[14,28488,28489],{},"Install with:",[91,28491,28492],{"className":10352,"code":27871,"language":10354,"meta":96,"style":96},[17,28493,28494],{"__ignoreMap":96},[100,28495,28496,28498,28500,28502,28504],{"class":102,"line":103},[100,28497,95],{"class":10361},[100,28499,26622],{"class":10364},[100,28501,26625],{"class":209},[100,28503,26657],{"class":209},[100,28505,27886],{"class":209},[675,28507,28509],{"id":28508},"example-3-file-named-the-same-as-a-module","Example 3: File named the same as a module",[14,28511,28512],{},"Problem file:",[91,28514,28517],{"className":28515,"code":28516,"language":4468,"meta":96},[26775],"json.py\n",[17,28518,28516],{"__ignoreMap":96},[14,28520,28521],{},"Problem code:",[91,28523,28525],{"className":93,"code":28524,"language":95,"meta":96,"style":96},"import json\n",[17,28526,28527],{"__ignoreMap":96},[100,28528,28529,28531],{"class":102,"line":103},[100,28530,9259],{"class":145},[100,28532,9984],{"class":106},[14,28534,15034],{},[40,28536,28537,28542,28547],{},[43,28538,28097,28539,28541],{},[17,28540,9856],{}," to something else",[43,28543,28544,28545],{},"Delete ",[17,28546,10130],{},[43,28548,9954],{},[675,28550,28552],{"id":28551},"example-4-virtual-environment-mismatch","Example 4: Virtual environment mismatch",[14,28554,28555],{},"You install a package here:",[91,28557,28558],{"className":10352,"code":27593,"language":10354,"meta":96,"style":96},[17,28559,28560],{"__ignoreMap":96},[100,28561,28562,28564,28566,28568,28570],{"class":102,"line":103},[100,28563,95],{"class":10361},[100,28565,26622],{"class":10364},[100,28567,26625],{"class":209},[100,28569,26657],{"class":209},[100,28571,27608],{"class":209},[14,28573,28574],{},"But your editor runs a different interpreter.",[14,28576,28577],{},"Check both:",[91,28579,28581],{"className":10352,"code":28580,"language":10354,"meta":96,"style":96},"python -c \"import sys; print(sys.executable)\"\npython -m pip --version\n",[17,28582,28583,28595],{"__ignoreMap":96},[100,28584,28585,28587,28589,28591,28593],{"class":102,"line":103},[100,28586,95],{"class":10361},[100,28588,10450],{"class":10364},[100,28590,1708],{"class":205},[100,28592,28205],{"class":209},[100,28594,1714],{"class":205},[100,28596,28597,28599,28601,28603],{"class":102,"line":135},[100,28598,95],{"class":10361},[100,28600,26622],{"class":10364},[100,28602,26625],{"class":209},[100,28604,10365],{"class":10364},[14,28606,28607],{},"If they point to different Python installations, switch your editor or terminal to the correct environment.",[77,28609,28611],{"id":28610},"when-this-is-modulenotfounderror-instead","When this is ModuleNotFoundError instead",[14,28613,28614],{},"In modern Python versions, this problem often appears as:",[91,28616,28619],{"className":28617,"code":28618,"language":4468,"meta":96},[26775],"ModuleNotFoundError: No module named 'requests'\n",[17,28620,28618],{"__ignoreMap":96},[14,28622,28623,28625,28626,28628],{},[17,28624,17397],{}," is closely related to ",[17,28627,17392],{},", but it is more specific.",[14,28630,28631,28632,290],{},"If you see that version of the message, read ",[295,28633,28634],{"href":17456},"ModuleNotFoundError: No module named X (Fix)",[14,28636,28637,28638,290],{},"If your problem is different and Python finds the module but fails to import something from it, see ",[295,28639,10661],{"href":26420},[77,28641,1514],{"id":1513},[675,28643,28645],{"id":28644},"why-do-i-get-this-error-even-after-installing-the-package","Why do I get this error even after installing the package?",[14,28647,28648],{},"You probably installed it in a different Python version or virtual environment than the one running your script.",[14,28650,28651],{},"Check:",[91,28653,28654],{"className":10352,"code":28580,"language":10354,"meta":96,"style":96},[17,28655,28656,28668],{"__ignoreMap":96},[100,28657,28658,28660,28662,28664,28666],{"class":102,"line":103},[100,28659,95],{"class":10361},[100,28661,10450],{"class":10364},[100,28663,1708],{"class":205},[100,28665,28205],{"class":209},[100,28667,1714],{"class":205},[100,28669,28670,28672,28674,28676],{"class":102,"line":135},[100,28671,95],{"class":10361},[100,28673,26622],{"class":10364},[100,28675,26625],{"class":209},[100,28677,10365],{"class":10364},[14,28679,28680],{},"Those should match the same Python setup.",[675,28682,25919],{"id":25918},[14,28684,28685,28687],{},[17,28686,17397],{}," is a more specific import error. It is used when Python cannot find the module at all.",[14,28689,28690,28692],{},[17,28691,17392],{}," is broader and can happen for other import-related problems too.",[675,28694,28696],{"id":28695},"can-my-file-name-cause-this-error","Can my file name cause this error?",[14,28698,28699],{},"Yes.",[14,28701,28702,28703,5870,28705,5894,28707,28709],{},"A file such as ",[17,28704,9856],{},[17,28706,26729],{},[17,28708,28100],{}," can stop Python from importing the real module.",[14,28711,28712,28713,28715],{},"Rename the file, then remove any ",[17,28714,10130],{}," folders.",[675,28717,28719],{"id":28718},"should-i-use-pip-install-or-python-m-pip-install","Should I use pip install or python -m pip install?",[14,28721,241,28722,28725],{},[17,28723,28724],{},"python -m pip install"," when possible.",[14,28727,28728],{},"It is safer because it uses the selected Python interpreter, which helps avoid environment mismatches.",[77,28730,1554],{"id":1553},[40,28732,28733,28737,28742,28746,28751,28755,28759],{},[43,28734,28735],{},[295,28736,28634],{"href":17456},[43,28738,28739],{},[295,28740,28741],{"href":27822},"How to install a Python package with pip",[43,28743,28744],{},[295,28745,27476],{"href":26679},[43,28747,28748],{},[295,28749,28750],{"href":25158},"How imports work in Python",[43,28752,28753],{},[295,28754,27062],{"href":28242},[43,28756,28757],{},[295,28758,26850],{"href":26849},[43,28760,28761],{},[295,28762,10661],{"href":26420},[1589,28764,28765],{},"html pre.shiki code .sbgvK, html code.shiki .sbgvK{--shiki-light:#E2931D;--shiki-default:#6F42C1;--shiki-dark:#B392F0}html pre.shiki code .stzsN, html code.shiki .stzsN{--shiki-light:#91B859;--shiki-default:#005CC5;--shiki-dark:#79B8FF}html pre.shiki code .s_sjI, html code.shiki .s_sjI{--shiki-light:#91B859;--shiki-default:#032F62;--shiki-dark:#9ECBFF}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 .sVHd0, html code.shiki .sVHd0{--shiki-light:#39ADB5;--shiki-light-font-style:italic;--shiki-default:#D73A49;--shiki-default-font-style:inherit;--shiki-dark:#F97583;--shiki-dark-font-style:inherit}html pre.shiki code .su5hD, html code.shiki .su5hD{--shiki-light:#90A4AE;--shiki-default:#24292E;--shiki-dark:#E1E4E8}html pre.shiki code .sZMiF, html code.shiki .sZMiF{--shiki-light:#E2931D;--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 .s_hVV, html code.shiki .s_hVV{--shiki-light:#90A4AE;--shiki-default:#005CC5;--shiki-dark:#79B8FF}html pre.shiki code .sptTA, html code.shiki .sptTA{--shiki-light:#6182B8;--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 .smGrS, html code.shiki .smGrS{--shiki-light:#39ADB5;--shiki-default:#D73A49;--shiki-dark:#F97583}html pre.shiki code .slqww, html code.shiki .slqww{--shiki-light:#6182B8;--shiki-default:#24292E;--shiki-dark:#E1E4E8}html pre.shiki code .skxfh, html code.shiki .skxfh{--shiki-light:#E53935;--shiki-default:#24292E;--shiki-dark:#E1E4E8}",{"title":96,"searchDepth":135,"depth":135,"links":28767},[28768,28769,28770,28771,28772,28773,28774,28775,28776,28777,28784,28790,28791,28797],{"id":79,"depth":135,"text":80},{"id":5880,"depth":135,"text":5881},{"id":27667,"depth":135,"text":27668},{"id":9507,"depth":135,"text":9508},{"id":27742,"depth":135,"text":27743},{"id":27826,"depth":135,"text":27827},{"id":27927,"depth":135,"text":27928},{"id":28000,"depth":135,"text":28001},{"id":28115,"depth":135,"text":28116},{"id":3273,"depth":135,"text":3274,"children":28778},[28779,28780,28781,28782,28783],{"id":28188,"depth":142,"text":28189},{"id":28213,"depth":142,"text":28214},{"id":28245,"depth":142,"text":28246},{"id":28270,"depth":142,"text":28271},{"id":28309,"depth":142,"text":28310},{"id":28354,"depth":135,"text":28355,"children":28785},[28786,28787,28788,28789],{"id":28358,"depth":142,"text":28359},{"id":28450,"depth":142,"text":28451},{"id":28508,"depth":142,"text":28509},{"id":28551,"depth":142,"text":28552},{"id":28610,"depth":135,"text":28611},{"id":1513,"depth":135,"text":1514,"children":28792},[28793,28794,28795,28796],{"id":28644,"depth":142,"text":28645},{"id":25918,"depth":142,"text":25919},{"id":28695,"depth":142,"text":28696},{"id":28718,"depth":142,"text":28719},{"id":1553,"depth":135,"text":1554},"Master importerror no module named x fix in our comprehensive Python beginner guide.",{},{"title":10656,"description":28798},"errors\u002Fimporterror-no-module-named-x-fix","AYNGwrAAmeZaZYaDEv2JPrplw1344GD4pMs95ZtuDSw",{"id":28804,"title":28805,"body":28806,"description":29779,"extension":1623,"meta":29780,"navigation":138,"path":29781,"seo":29782,"stem":29783,"__hash__":29784},"content\u002Ferrors\u002Findentationerror-expected-an-indented-block-fix.md","IndentationError: expected an indented block (Fix)",{"type":7,"value":28807,"toc":29753},[28808,28811,28818,28835,28837,28865,28883,28885,28887,28915,28918,28921,28938,28941,28948,28950,28953,28988,28991,29018,29046,29084,29087,29089,29091,29117,29120,29140,29142,29169,29171,29180,29191,29193,29196,29213,29217,29220,29247,29250,29287,29289,29297,29301,29304,29306,29332,29341,29344,29384,29386,29394,29400,29405,29408,29411,29428,29430,29447,29450,29452,29469,29477,29479,29482,29499,29502,29514,29517,29533,29540,29544,29547,29552,29555,29557,29592,29598,29603,29606,29609,29617,29619,29622,29642,29645,29659,29662,29667,29684,29686,29690,29693,29697,29700,29707,29713,29717,29720,29722,29750],[10,28809,28805],{"id":28810},"indentationerror-expected-an-indented-block-fix",[14,28812,28813,28814,28817],{},"Python shows ",[17,28815,28816],{},"IndentationError: expected an indented block"," when it reaches a line that should contain a code block, but the next line is not indented.",[14,28819,28820,28821,5870,28823,5870,28825,5870,28827,5870,28829,5870,28831,5894,28833,290],{},"This usually happens after lines like ",[17,28822,2736],{},[17,28824,71],{},[17,28826,721],{},[17,28828,1078],{},[17,28830,3417],{},[17,28832,146],{},[17,28834,4944],{},[77,28836,80],{"id":79},[91,28838,28840],{"className":93,"code":28839,"language":95,"meta":96,"style":96},"if True:\n    print(\"This line is indented correctly\")\n",[17,28841,28842,28850],{"__ignoreMap":96},[100,28843,28844,28846,28848],{"class":102,"line":103},[100,28845,2736],{"class":145},[100,28847,159],{"class":158},[100,28849,149],{"class":118},[100,28851,28852,28854,28856,28858,28861,28863],{"class":102,"line":135},[100,28853,200],{"class":114},[100,28855,170],{"class":118},[100,28857,206],{"class":205},[100,28859,28860],{"class":209},"This line is indented correctly",[100,28862,206],{"class":205},[100,28864,215],{"class":118},[14,28866,28867,28868,5870,28870,5870,28872,5870,28874,5870,28876,5870,28878,5629,28880,28882],{},"Python expects an indented block after lines like ",[17,28869,2736],{},[17,28871,71],{},[17,28873,721],{},[17,28875,1078],{},[17,28877,3417],{},[17,28879,146],{},[17,28881,4944],{},". Add indentation, usually 4 spaces.",[77,28884,5881],{"id":5880},[14,28886,9328],{},[40,28888,28889,28892,28912],{},[43,28890,28891],{},"Python found a line that starts a block, but no indented code came after it",[43,28893,28894,28895,5870,28897,5870,28899,5870,28901,5870,28903,5870,28905,5870,28907,5870,28909,28911],{},"A block is a group of lines inside ",[17,28896,2736],{},[17,28898,71],{},[17,28900,721],{},[17,28902,1078],{},[17,28904,3417],{},[17,28906,146],{},[17,28908,188],{},[17,28910,4944],{},", and similar statements",[43,28913,28914],{},"Python uses indentation to define code structure",[14,28916,28917],{},"In Python, indentation is not optional. It is part of the syntax.",[14,28919,28920],{},"For example, after this line:",[91,28922,28924],{"className":93,"code":28923,"language":95,"meta":96,"style":96},"if age >= 18:\n",[17,28925,28926],{"__ignoreMap":96},[100,28927,28928,28930,28932,28934,28936],{"class":102,"line":103},[100,28929,2736],{"class":145},[100,28931,1676],{"class":106},[100,28933,1679],{"class":110},[100,28935,1869],{"class":122},[100,28937,149],{"class":118},[14,28939,28940],{},"Python expects the next line to be indented. If it is not, Python raises this error.",[14,28942,28943,28944,290],{},"If you are new to this, see ",[295,28945,28947],{"href":28946},"\u002Flearn\u002Fpython-indentation-rules-and-why-they-matter\u002F","Python indentation rules and why they matter",[77,28949,26289],{"id":26288},[14,28951,28952],{},"This error often appears:",[40,28954,28955,28961,28964,28972,28985],{},[43,28956,28957,28958,28960],{},"After an ",[17,28959,2736],{}," statement with no indented body",[43,28962,28963],{},"After a function definition with no indented body",[43,28965,28966,28967,3178,28969,28971],{},"After loops like ",[17,28968,71],{},[17,28970,721],{}," with no indented code",[43,28973,28974,28975,5870,28977,5870,28979,5894,28981,28984],{},"After ",[17,28976,146],{},[17,28978,188],{},[17,28980,4944],{},[17,28982,28983],{},"finally"," with no body",[43,28986,28987],{},"When indentation is missing because of a copy-paste or edit mistake",[14,28989,28990],{},"Here are a few examples of code that can trigger it:",[91,28992,28994],{"className":93,"code":28993,"language":95,"meta":96,"style":96},"if True:\nprint(\"Hello\")\n",[17,28995,28996,29004],{"__ignoreMap":96},[100,28997,28998,29000,29002],{"class":102,"line":103},[100,28999,2736],{"class":145},[100,29001,159],{"class":158},[100,29003,149],{"class":118},[100,29005,29006,29008,29010,29012,29014,29016],{"class":102,"line":135},[100,29007,372],{"class":114},[100,29009,170],{"class":118},[100,29011,206],{"class":205},[100,29013,7683],{"class":209},[100,29015,206],{"class":205},[100,29017,215],{"class":118},[91,29019,29021],{"className":93,"code":29020,"language":95,"meta":96,"style":96},"def greet():\nprint(\"Hi\")\n",[17,29022,29023,29031],{"__ignoreMap":96},[100,29024,29025,29027,29029],{"class":102,"line":103},[100,29026,1078],{"class":1077},[100,29028,2848],{"class":1081},[100,29030,1085],{"class":118},[100,29032,29033,29035,29037,29039,29042,29044],{"class":102,"line":135},[100,29034,372],{"class":114},[100,29036,170],{"class":118},[100,29038,206],{"class":205},[100,29040,29041],{"class":209},"Hi",[100,29043,206],{"class":205},[100,29045,215],{"class":118},[91,29047,29049],{"className":93,"code":29048,"language":95,"meta":96,"style":96},"for number in [1, 2, 3]:\nprint(number)\n",[17,29050,29051,29074],{"__ignoreMap":96},[100,29052,29053,29055,29057,29059,29061,29063,29065,29067,29069,29071],{"class":102,"line":103},[100,29054,71],{"class":145},[100,29056,614],{"class":106},[100,29058,617],{"class":145},[100,29060,594],{"class":118},[100,29062,123],{"class":122},[100,29064,126],{"class":118},[100,29066,129],{"class":122},[100,29068,126],{"class":118},[100,29070,1405],{"class":122},[100,29072,29073],{"class":118},"]:\n",[100,29075,29076,29078,29080,29082],{"class":102,"line":135},[100,29077,372],{"class":114},[100,29079,170],{"class":118},[100,29081,631],{"class":178},[100,29083,215],{"class":118},[14,29085,29086],{},"In each case, the line after the colon should be indented.",[77,29088,11031],{"id":11030},[14,29090,1891],{},[91,29092,29093],{"className":93,"code":28993,"language":95,"meta":96,"style":96},[17,29094,29095,29103],{"__ignoreMap":96},[100,29096,29097,29099,29101],{"class":102,"line":103},[100,29098,2736],{"class":145},[100,29100,159],{"class":158},[100,29102,149],{"class":118},[100,29104,29105,29107,29109,29111,29113,29115],{"class":102,"line":135},[100,29106,372],{"class":114},[100,29108,170],{"class":118},[100,29110,206],{"class":205},[100,29112,7683],{"class":209},[100,29114,206],{"class":205},[100,29116,215],{"class":118},[14,29118,29119],{},"The problem is the line after the colon.",[40,29121,29122,29128,29131,29134],{},[43,29123,29124,29127],{},[17,29125,29126],{},"if True:"," ends with a colon",[43,29129,29130],{},"The colon tells Python that a block is starting",[43,29132,29133],{},"The next line must be indented",[43,29135,29136,29139],{},[17,29137,29138],{},"print(\"Hello\")"," is not indented, so Python raises an error",[14,29141,6031],{},[91,29143,29145],{"className":93,"code":29144,"language":95,"meta":96,"style":96},"if True:\n    print(\"Hello\")\n",[17,29146,29147,29155],{"__ignoreMap":96},[100,29148,29149,29151,29153],{"class":102,"line":103},[100,29150,2736],{"class":145},[100,29152,159],{"class":158},[100,29154,149],{"class":118},[100,29156,29157,29159,29161,29163,29165,29167],{"class":102,"line":135},[100,29158,200],{"class":114},[100,29160,170],{"class":118},[100,29162,206],{"class":205},[100,29164,7683],{"class":209},[100,29166,206],{"class":205},[100,29168,215],{"class":118},[14,29170,218],{},[91,29172,29174],{"className":93,"code":29173,"language":95,"meta":96,"style":96},"Hello\n",[17,29175,29176],{"__ignoreMap":96},[100,29177,29178],{"class":102,"line":103},[100,29179,29173],{"class":106},[14,29181,29182,29183,3266,29187,290],{},"This same rule applies to ",[295,29184,29186],{"href":29185},"\u002Flearn\u002Fpython-if-statements-explained\u002F","Python if statements",[295,29188,29190],{"href":29189},"\u002Flearn\u002Fpython-functions-explained\u002F","Python functions",[77,29192,6244],{"id":6243},[14,29194,29195],{},"Use these steps:",[40,29197,29198,29201,29204,29210],{},[43,29199,29200],{},"Indent the next line with 4 spaces",[43,29202,29203],{},"Make sure all lines inside the block use the same indentation level",[43,29205,29206,29207],{},"If the block is intentionally empty, use ",[17,29208,29209],{},"pass",[43,29211,29212],{},"Check the line above the error, not only the line shown in the message",[675,29214,29216],{"id":29215},"example-fix","Example fix",[14,29218,29219],{},"Broken code:",[91,29221,29223],{"className":93,"code":29222,"language":95,"meta":96,"style":96},"def greet():\nprint(\"Hello\")\n",[17,29224,29225,29233],{"__ignoreMap":96},[100,29226,29227,29229,29231],{"class":102,"line":103},[100,29228,1078],{"class":1077},[100,29230,2848],{"class":1081},[100,29232,1085],{"class":118},[100,29234,29235,29237,29239,29241,29243,29245],{"class":102,"line":135},[100,29236,372],{"class":114},[100,29238,170],{"class":118},[100,29240,206],{"class":205},[100,29242,7683],{"class":209},[100,29244,206],{"class":205},[100,29246,215],{"class":118},[14,29248,29249],{},"Fixed code:",[91,29251,29253],{"className":93,"code":29252,"language":95,"meta":96,"style":96},"def greet():\n    print(\"Hello\")\n\ngreet()\n",[17,29254,29255,29263,29277,29281],{"__ignoreMap":96},[100,29256,29257,29259,29261],{"class":102,"line":103},[100,29258,1078],{"class":1077},[100,29260,2848],{"class":1081},[100,29262,1085],{"class":118},[100,29264,29265,29267,29269,29271,29273,29275],{"class":102,"line":135},[100,29266,200],{"class":114},[100,29268,170],{"class":118},[100,29270,206],{"class":205},[100,29272,7683],{"class":209},[100,29274,206],{"class":205},[100,29276,215],{"class":118},[100,29278,29279],{"class":102,"line":142},[100,29280,139],{"emptyLinePlaceholder":138},[100,29282,29283,29285],{"class":102,"line":152},[100,29284,2911],{"class":178},[100,29286,4734],{"class":118},[14,29288,218],{},[91,29290,29291],{"className":93,"code":29173,"language":95,"meta":96,"style":96},[17,29292,29293],{"__ignoreMap":96},[100,29294,29295],{"class":102,"line":103},[100,29296,29173],{"class":106},[675,29298,29300],{"id":29299},"check-the-line-above-the-error","Check the line above the error",[14,29302,29303],{},"Sometimes the error message points to a line that looks fine. The real issue is often the line before it.",[14,29305,1844],{},[91,29307,29309],{"className":93,"code":29308,"language":95,"meta":96,"style":96},"if True:\nname = \"Sam\"\n",[17,29310,29311,29319],{"__ignoreMap":96},[100,29312,29313,29315,29317],{"class":102,"line":103},[100,29314,2736],{"class":145},[100,29316,159],{"class":158},[100,29318,149],{"class":118},[100,29320,29321,29323,29325,29327,29330],{"class":102,"line":135},[100,29322,1691],{"class":106},[100,29324,111],{"class":110},[100,29326,1708],{"class":205},[100,29328,29329],{"class":209},"Sam",[100,29331,1714],{"class":205},[14,29333,29334,29335,29338,29339,25128],{},"Python complains because ",[17,29336,29337],{},"name = \"Sam\""," should be inside the ",[17,29340,2736],{},[14,29342,29343],{},"Fixed version:",[91,29345,29347],{"className":93,"code":29346,"language":95,"meta":96,"style":96},"if True:\n    name = \"Sam\"\n\nprint(name)\n",[17,29348,29349,29357,29370,29374],{"__ignoreMap":96},[100,29350,29351,29353,29355],{"class":102,"line":103},[100,29352,2736],{"class":145},[100,29354,159],{"class":158},[100,29356,149],{"class":118},[100,29358,29359,29362,29364,29366,29368],{"class":102,"line":135},[100,29360,29361],{"class":106},"    name ",[100,29363,111],{"class":110},[100,29365,1708],{"class":205},[100,29367,29329],{"class":209},[100,29369,1714],{"class":205},[100,29371,29372],{"class":102,"line":142},[100,29373,139],{"emptyLinePlaceholder":138},[100,29375,29376,29378,29380,29382],{"class":102,"line":152},[100,29377,372],{"class":114},[100,29379,170],{"class":118},[100,29381,2853],{"class":178},[100,29383,215],{"class":118},[14,29385,218],{},[91,29387,29388],{"className":93,"code":16526,"language":95,"meta":96,"style":96},[17,29389,29390],{"__ignoreMap":96},[100,29391,29392],{"class":102,"line":103},[100,29393,16526],{"class":106},[77,29395,241,29397,29399],{"id":29396},"use-pass-for-an-empty-block",[17,29398,29209],{}," for an empty block",[14,29401,29402,29404],{},[17,29403,29209],{}," is a placeholder that does nothing.",[14,29406,29407],{},"It is useful when you want to write the structure first and add code later.",[14,29409,29410],{},"Common places to use it:",[40,29412,29413,29417,29421,29425],{},[43,29414,28974,29415],{},[17,29416,1078],{},[43,29418,28974,29419],{},[17,29420,3417],{},[43,29422,28077,29423],{},[17,29424,2736],{},[43,29426,29427],{},"Inside loops during early drafting",[14,29429,1844],{},[91,29431,29433],{"className":93,"code":29432,"language":95,"meta":96,"style":96},"def greet():\n    pass\n",[17,29434,29435,29443],{"__ignoreMap":96},[100,29436,29437,29439,29441],{"class":102,"line":103},[100,29438,1078],{"class":1077},[100,29440,2848],{"class":1081},[100,29442,1085],{"class":118},[100,29444,29445],{"class":102,"line":135},[100,29446,16214],{"class":145},[14,29448,29449],{},"This code is valid, even though the function does nothing yet.",[14,29451,12066],{},[91,29453,29455],{"className":93,"code":29454,"language":95,"meta":96,"style":96},"if True:\n    pass\n",[17,29456,29457,29465],{"__ignoreMap":96},[100,29458,29459,29461,29463],{"class":102,"line":103},[100,29460,2736],{"class":145},[100,29462,159],{"class":158},[100,29464,149],{"class":118},[100,29466,29467],{"class":102,"line":135},[100,29468,16214],{"class":145},[14,29470,29471,29472,29474,29475,290],{},"Without ",[17,29473,29209],{},", Python would raise ",[17,29476,28816],{},[77,29478,1168],{"id":1167},[14,29480,29481],{},"When you see this error, check these things:",[40,29483,29484,29487,29490,29493,29496],{},[43,29485,29486],{},"Find the line that ends with a colon",[43,29488,29489],{},"Check whether the next line is indented",[43,29491,29492],{},"Use spaces consistently instead of mixing tabs and spaces",[43,29494,29495],{},"Make sure your editor did not remove indentation automatically",[43,29497,29498],{},"Run the file again after fixing one block at a time",[14,29500,29501],{},"You can also test your file from the command line:",[91,29503,29505],{"className":10352,"code":29504,"language":10354,"meta":96,"style":96},"python your_file.py\n",[17,29506,29507],{"__ignoreMap":96},[100,29508,29509,29511],{"class":102,"line":103},[100,29510,95],{"class":10361},[100,29512,29513],{"class":209}," your_file.py\n",[14,29515,29516],{},"Or check for syntax and indentation problems without running the program:",[91,29518,29520],{"className":10352,"code":29519,"language":10354,"meta":96,"style":96},"python -m py_compile your_file.py\n",[17,29521,29522],{"__ignoreMap":96},[100,29523,29524,29526,29528,29531],{"class":102,"line":103},[100,29525,95],{"class":10361},[100,29527,26622],{"class":10364},[100,29529,29530],{"class":209}," py_compile",[100,29532,29513],{"class":209},[14,29534,29535,29536,290],{},"If the problem is not missing indentation but inconsistent indentation, you may be dealing with ",[295,29537,29539],{"href":29538},"\u002Ferrors\u002Findentationerror-unindent-does-not-match-any-outer-indentation-level-fix","IndentationError: unindent does not match any outer indentation level",[77,29541,29543],{"id":29542},"related-indentation-problems","Related indentation problems",[14,29545,29546],{},"This error is not the same as other indentation errors.",[675,29548,29550],{"id":29549},"indentationerror-unexpected-indent",[17,29551,16618],{},[14,29553,29554],{},"This happens when a line is indented even though Python was not expecting a block there.",[14,29556,1844],{},[91,29558,29560],{"className":93,"code":29559,"language":95,"meta":96,"style":96},"print(\"Start\")\n    print(\"Too much indentation\")\n",[17,29561,29562,29577],{"__ignoreMap":96},[100,29563,29564,29566,29568,29570,29573,29575],{"class":102,"line":103},[100,29565,372],{"class":114},[100,29567,170],{"class":118},[100,29569,206],{"class":205},[100,29571,29572],{"class":209},"Start",[100,29574,206],{"class":205},[100,29576,215],{"class":118},[100,29578,29579,29581,29583,29585,29588,29590],{"class":102,"line":135},[100,29580,200],{"class":114},[100,29582,170],{"class":118},[100,29584,206],{"class":205},[100,29586,29587],{"class":209},"Too much indentation",[100,29589,206],{"class":205},[100,29591,215],{"class":118},[14,29593,23054,29594,29597],{},[295,29595,16618],{"href":29596},"\u002Ferrors\u002Findentationerror-unexpected-indent-fix"," for that case.",[675,29599,29601],{"id":29600},"indentationerror-unindent-does-not-match-any-outer-indentation-level",[17,29602,29539],{},[14,29604,29605],{},"This happens when indentation levels do not line up correctly, often because tabs and spaces were mixed.",[14,29607,29608],{},"All of these errors usually come from block structure or inconsistent whitespace.",[14,29610,29611,29612,29616],{},"Also check for ",[295,29613,29615],{"href":29614},"\u002Ferrors\u002Fsyntaxerror-missing-colon-fix","SyntaxError: missing colon",", because forgetting a colon can lead to confusion around block structure.",[77,29618,1344],{"id":1343},[14,29620,29621],{},"The most common causes are:",[40,29623,29624,29627,29633,29636,29639],{},[43,29625,29626],{},"Forgetting to indent after a colon",[43,29628,29629,29630,29632],{},"Leaving a function or ",[17,29631,2736],{}," block empty",[43,29634,29635],{},"Deleting the only line inside a block",[43,29637,29638],{},"Mixing tabs and spaces",[43,29640,29641],{},"Bad paste formatting from another editor or website",[14,29643,29644],{},"A common beginner mistake is writing the structure first:",[91,29646,29648],{"className":93,"code":29647,"language":95,"meta":96,"style":96},"def calculate_total():\n",[17,29649,29650],{"__ignoreMap":96},[100,29651,29652,29654,29657],{"class":102,"line":103},[100,29653,1078],{"class":1077},[100,29655,29656],{"class":1081}," calculate_total",[100,29658,1085],{"class":118},[14,29660,29661],{},"This is incomplete. Python expects an indented block after the function line.",[14,29663,29664,29665,89],{},"To make it valid for now, add ",[17,29666,29209],{},[91,29668,29670],{"className":93,"code":29669,"language":95,"meta":96,"style":96},"def calculate_total():\n    pass\n",[17,29671,29672,29680],{"__ignoreMap":96},[100,29673,29674,29676,29678],{"class":102,"line":103},[100,29675,1078],{"class":1077},[100,29677,29656],{"class":1081},[100,29679,1085],{"class":118},[100,29681,29682],{"class":102,"line":135},[100,29683,16214],{"class":145},[77,29685,1514],{"id":1513},[675,29687,29689],{"id":29688},"what-does-expected-an-indented-block-mean-in-python","What does \"expected an indented block\" mean in Python?",[14,29691,29692],{},"It means Python expected code inside a block, but the next line was not indented.",[675,29694,29696],{"id":29695},"how-many-spaces-should-i-use-for-indentation-in-python","How many spaces should I use for indentation in Python?",[14,29698,29699],{},"Use 4 spaces per indentation level. This is the standard Python style.",[675,29701,29703,29704,29706],{"id":29702},"can-i-leave-an-if-statement-or-function-empty","Can I leave an ",[17,29705,2736],{}," statement or function empty?",[14,29708,29709,29710,29712],{},"Yes, but you must use ",[17,29711,29209],{}," as a placeholder inside the block.",[675,29714,29716],{"id":29715},"why-does-the-error-point-to-a-line-that-looks-correct","Why does the error point to a line that looks correct?",[14,29718,29719],{},"The real problem is often the line above it, especially one that ends with a colon.",[77,29721,1554],{"id":1553},[40,29723,29724,29728,29732,29736,29740,29745],{},[43,29725,29726],{},[295,29727,28947],{"href":28946},[43,29729,29730],{},[295,29731,16618],{"href":29596},[43,29733,29734],{},[295,29735,29539],{"href":29538},[43,29737,29738],{},[295,29739,29615],{"href":29614},[43,29741,29742],{},[295,29743,29744],{"href":29185},"Python if statements explained",[43,29746,29747],{},[295,29748,29749],{"href":29189},"Python functions explained",[1589,29751,29752],{},"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 .s39Yj, html code.shiki .s39Yj{--shiki-light:#39ADB5;--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 .sptTA, html code.shiki .sptTA{--shiki-light:#6182B8;--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 .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 .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 .slqww, html code.shiki .slqww{--shiki-light:#6182B8;--shiki-default:#24292E;--shiki-dark:#E1E4E8}html pre.shiki code .sbgvK, html code.shiki .sbgvK{--shiki-light:#E2931D;--shiki-default:#6F42C1;--shiki-dark:#B392F0}html pre.shiki code .stzsN, html code.shiki .stzsN{--shiki-light:#91B859;--shiki-default:#005CC5;--shiki-dark:#79B8FF}",{"title":96,"searchDepth":135,"depth":135,"links":29754},[29755,29756,29757,29758,29759,29763,29765,29766,29770,29771,29778],{"id":79,"depth":135,"text":80},{"id":5880,"depth":135,"text":5881},{"id":26288,"depth":135,"text":26289},{"id":11030,"depth":135,"text":11031},{"id":6243,"depth":135,"text":6244,"children":29760},[29761,29762],{"id":29215,"depth":142,"text":29216},{"id":29299,"depth":142,"text":29300},{"id":29396,"depth":135,"text":29764},"Use pass for an empty block",{"id":1167,"depth":135,"text":1168},{"id":29542,"depth":135,"text":29543,"children":29767},[29768,29769],{"id":29549,"depth":142,"text":16618},{"id":29600,"depth":142,"text":29539},{"id":1343,"depth":135,"text":1344},{"id":1513,"depth":135,"text":1514,"children":29772},[29773,29774,29775,29777],{"id":29688,"depth":142,"text":29689},{"id":29695,"depth":142,"text":29696},{"id":29702,"depth":142,"text":29776},"Can I leave an if statement or function empty?",{"id":29715,"depth":142,"text":29716},{"id":1553,"depth":135,"text":1554},"Master indentationerror expected an indented block fix in our comprehensive Python beginner guide.",{},"\u002Ferrors\u002Findentationerror-expected-an-indented-block-fix",{"title":28805,"description":29779},"errors\u002Findentationerror-expected-an-indented-block-fix","ewu4j7SwWDdhzJ30pTEQiCnptuLDXPeLhE87GP7-gwA",{"id":29786,"title":29787,"body":29788,"description":30654,"extension":1623,"meta":30655,"navigation":138,"path":30656,"seo":30657,"stem":30658,"__hash__":30659},"content\u002Ferrors\u002Findentationerror-in-python-causes-and-fixes.md","IndentationError in Python: Causes and Fixes",{"type":7,"value":29789,"toc":30626},[29790,29793,29798,29800,29887,29893,29895,29898,29914,29919,29921,29932,29937,29941,29944,29946,29965,29972,29976,29981,30000,30003,30037,30041,30046,30050,30076,30085,30088,30102,30106,30132,30135,30140,30144,30147,30150,30199,30209,30212,30226,30229,30276,30279,30284,30288,30291,30295,30336,30339,30347,30350,30354,30394,30397,30400,30411,30416,30420,30423,30443,30446,30468,30470,30484,30491,30493,30496,30513,30516,30527,30534,30538,30543,30561,30564,30566,30570,30573,30577,30580,30584,30587,30591,30594,30596,30623],[10,29791,29787],{"id":29792},"indentationerror-in-python-causes-and-fixes",[14,29794,5525,29795,29797],{},[17,29796,16508],{}," by understanding what the error means, why Python requires indentation, and how to correct common spacing mistakes in your code.",[77,29799,80],{"id":79},[91,29801,29803],{"className":93,"code":29802,"language":95,"meta":96,"style":96},"if True:\n    print(\"Correct indentation\")\n\nfor i in range(3):\n    print(i)\n\ndef greet():\n    print(\"Hello\")\n",[17,29804,29805,29813,29828,29832,29850,29861,29865,29873],{"__ignoreMap":96},[100,29806,29807,29809,29811],{"class":102,"line":103},[100,29808,2736],{"class":145},[100,29810,159],{"class":158},[100,29812,149],{"class":118},[100,29814,29815,29817,29819,29821,29824,29826],{"class":102,"line":135},[100,29816,200],{"class":114},[100,29818,170],{"class":118},[100,29820,206],{"class":205},[100,29822,29823],{"class":209},"Correct indentation",[100,29825,206],{"class":205},[100,29827,215],{"class":118},[100,29829,29830],{"class":102,"line":142},[100,29831,139],{"emptyLinePlaceholder":138},[100,29833,29834,29836,29839,29841,29844,29846,29848],{"class":102,"line":152},[100,29835,71],{"class":145},[100,29837,29838],{"class":106}," i ",[100,29840,617],{"class":145},[100,29842,29843],{"class":114}," range",[100,29845,170],{"class":118},[100,29847,11541],{"class":122},[100,29849,522],{"class":118},[100,29851,29852,29854,29856,29859],{"class":102,"line":164},[100,29853,200],{"class":114},[100,29855,170],{"class":118},[100,29857,29858],{"class":178},"i",[100,29860,215],{"class":118},[100,29862,29863],{"class":102,"line":185},[100,29864,139],{"emptyLinePlaceholder":138},[100,29866,29867,29869,29871],{"class":102,"line":197},[100,29868,1078],{"class":1077},[100,29870,2848],{"class":1081},[100,29872,1085],{"class":118},[100,29874,29875,29877,29879,29881,29883,29885],{"class":102,"line":771},[100,29876,200],{"class":114},[100,29878,170],{"class":118},[100,29880,206],{"class":205},[100,29882,7683],{"class":209},[100,29884,206],{"class":205},[100,29886,215],{"class":118},[14,29888,29889,29890,29892],{},"After a line that ends with a colon (",[17,29891,89],{},"), indent the next block consistently. Use the same indentation style throughout the file.",[77,29894,5881],{"id":5880},[14,29896,29897],{},"Python uses indentation to define code blocks.",[14,29899,29900,29901,5870,29903,5870,29905,5870,29907,5870,29909,5870,29911,29913],{},"That means spaces at the start of a line are not just for formatting. They tell Python which lines belong inside an ",[17,29902,2736],{},[17,29904,71],{},[17,29906,721],{},[17,29908,1078],{},[17,29910,3417],{},[17,29912,146],{},", and similar statements.",[14,29915,1764,29916,29918],{},[17,29917,16508],{}," means the spacing at the start of one or more lines is invalid.",[14,29920,13223],{},[40,29922,29923,29926,29929],{},[43,29924,29925],{},"A line should be indented, but it is not",[43,29927,29928],{},"A line is indented when no block is open",[43,29930,29931],{},"One line uses different indentation from the rest of the block",[14,29933,28943,29934,290],{},[295,29935,28947],{"href":29936},"\u002Flearn\u002Fpython-indentation-rules-and-why-they-matter",[77,29938,29940],{"id":29939},"why-python-raises-indentationerror","Why Python raises IndentationError",[14,29942,29943],{},"Python raises this error when it cannot understand the structure of your code.",[14,29945,24806],{},[40,29947,29948,29953,29956,29959,29962],{},[43,29949,29950,29951,6155],{},"A block is required after a line ending with a colon (",[17,29952,89],{},[43,29954,29955],{},"The next line is not indented when it should be",[43,29957,29958],{},"A line is indented too much or too little",[43,29960,29961],{},"Tabs and spaces are mixed in the same file",[43,29963,29964],{},"One block uses inconsistent indentation levels",[14,29966,29967,29968,290],{},"Sometimes the real problem is on the line above the one shown in the error. For example, a missing colon can make the next indented line fail. See ",[295,29969,4119,29970],{"href":29614},[17,29971,29615],{},[77,29973,29975],{"id":29974},"common-situations-that-cause-it","Common situations that cause it",[14,29977,29978,29979,89],{},"These are the most common causes of ",[17,29980,16508],{},[40,29982,29983,29988,29991,29994,29997],{},[43,29984,29985,29986,9320],{},"Forgetting to indent code inside an ",[17,29987,2736],{},[43,29989,29990],{},"Forgetting to indent code inside a function",[43,29992,29993],{},"Pasting code from another source with mixed tabs and spaces",[43,29995,29996],{},"Adding extra spaces to a line that should align with nearby lines",[43,29998,29999],{},"Removing indentation from one line in a loop or conditional block",[14,30001,30002],{},"Other common causes include:",[40,30004,30005,30022,30025,30028,30031,30034],{},[43,30006,30007,30008,5870,30010,5870,30012,5870,30014,5870,30016,5870,30018,5894,30020],{},"Missing indentation after ",[17,30009,2736],{},[17,30011,71],{},[17,30013,721],{},[17,30015,1078],{},[17,30017,3417],{},[17,30019,146],{},[17,30021,188],{},[43,30023,30024],{},"Extra indentation on a line outside a block",[43,30026,30027],{},"Mixed tabs and spaces",[43,30029,30030],{},"Inconsistent indentation width in the same block",[43,30032,30033],{},"Copy-pasted code with hidden whitespace differences",[43,30035,30036],{},"Missing colon before the intended block",[77,30038,30040],{"id":30039},"example-missing-indentation","Example: missing indentation",[14,30042,30043,30044,290],{},"A very common error is forgetting to indent the line after a statement ending with ",[17,30045,89],{},[675,30047,30049],{"id":30048},"wrong-code","Wrong code",[91,30051,30052],{"className":93,"code":28993,"language":95,"meta":96,"style":96},[17,30053,30054,30062],{"__ignoreMap":96},[100,30055,30056,30058,30060],{"class":102,"line":103},[100,30057,2736],{"class":145},[100,30059,159],{"class":158},[100,30061,149],{"class":118},[100,30063,30064,30066,30068,30070,30072,30074],{"class":102,"line":135},[100,30065,372],{"class":114},[100,30067,170],{"class":118},[100,30069,206],{"class":205},[100,30071,7683],{"class":209},[100,30073,206],{"class":205},[100,30075,215],{"class":118},[14,30077,30078,30079,30081,30082,30084],{},"Python expects the ",[17,30080,29138],{}," line to belong to the ",[17,30083,2736],{}," block, but it is not indented.",[14,30086,30087],{},"This often causes:",[91,30089,30091],{"className":93,"code":30090,"language":95,"meta":96,"style":96},"IndentationError: expected an indented block\n",[17,30092,30093],{"__ignoreMap":96},[100,30094,30095,30097,30099],{"class":102,"line":103},[100,30096,16508],{"class":191},[100,30098,89],{"class":118},[100,30100,30101],{"class":106}," expected an indented block\n",[675,30103,30105],{"id":30104},"correct-code","Correct code",[91,30107,30108],{"className":93,"code":29144,"language":95,"meta":96,"style":96},[17,30109,30110,30118],{"__ignoreMap":96},[100,30111,30112,30114,30116],{"class":102,"line":103},[100,30113,2736],{"class":145},[100,30115,159],{"class":158},[100,30117,149],{"class":118},[100,30119,30120,30122,30124,30126,30128,30130],{"class":102,"line":135},[100,30121,200],{"class":114},[100,30123,170],{"class":118},[100,30125,206],{"class":205},[100,30127,7683],{"class":209},[100,30129,206],{"class":205},[100,30131,215],{"class":118},[14,30133,30134],{},"Use four spaces for the indented line.",[14,30136,30137,30138,290],{},"If you want a focused explanation of this version of the error, see ",[295,30139,28816],{"href":29781},[77,30141,30143],{"id":30142},"example-unexpected-indentation","Example: unexpected indentation",[14,30145,30146],{},"This happens when a line starts with spaces even though Python is not currently inside a block.",[675,30148,30049],{"id":30149},"wrong-code-1",[91,30151,30153],{"className":93,"code":30152,"language":95,"meta":96,"style":96},"print(\"Start\")\n    print(\"Oops\")\nprint(\"End\")\n",[17,30154,30155,30169,30184],{"__ignoreMap":96},[100,30156,30157,30159,30161,30163,30165,30167],{"class":102,"line":103},[100,30158,372],{"class":114},[100,30160,170],{"class":118},[100,30162,206],{"class":205},[100,30164,29572],{"class":209},[100,30166,206],{"class":205},[100,30168,215],{"class":118},[100,30170,30171,30173,30175,30177,30180,30182],{"class":102,"line":135},[100,30172,200],{"class":114},[100,30174,170],{"class":118},[100,30176,206],{"class":205},[100,30178,30179],{"class":209},"Oops",[100,30181,206],{"class":205},[100,30183,215],{"class":118},[100,30185,30186,30188,30190,30192,30195,30197],{"class":102,"line":142},[100,30187,372],{"class":114},[100,30189,170],{"class":118},[100,30191,206],{"class":205},[100,30193,30194],{"class":209},"End",[100,30196,206],{"class":205},[100,30198,215],{"class":118},[14,30200,30201,30202,5870,30204,5870,30206,30208],{},"The second line is indented, but there is no ",[17,30203,2736],{},[17,30205,71],{},[17,30207,1078],{},", or other block before it.",[14,30210,30211],{},"This can cause:",[91,30213,30215],{"className":93,"code":30214,"language":95,"meta":96,"style":96},"IndentationError: unexpected indent\n",[17,30216,30217],{"__ignoreMap":96},[100,30218,30219,30221,30223],{"class":102,"line":103},[100,30220,16508],{"class":191},[100,30222,89],{"class":118},[100,30224,30225],{"class":106}," unexpected indent\n",[675,30227,30105],{"id":30228},"correct-code-1",[91,30230,30232],{"className":93,"code":30231,"language":95,"meta":96,"style":96},"print(\"Start\")\nprint(\"Oops\")\nprint(\"End\")\n",[17,30233,30234,30248,30262],{"__ignoreMap":96},[100,30235,30236,30238,30240,30242,30244,30246],{"class":102,"line":103},[100,30237,372],{"class":114},[100,30239,170],{"class":118},[100,30241,206],{"class":205},[100,30243,29572],{"class":209},[100,30245,206],{"class":205},[100,30247,215],{"class":118},[100,30249,30250,30252,30254,30256,30258,30260],{"class":102,"line":135},[100,30251,372],{"class":114},[100,30253,170],{"class":118},[100,30255,206],{"class":205},[100,30257,30179],{"class":209},[100,30259,206],{"class":205},[100,30261,215],{"class":118},[100,30263,30264,30266,30268,30270,30272,30274],{"class":102,"line":142},[100,30265,372],{"class":114},[100,30267,170],{"class":118},[100,30269,206],{"class":205},[100,30271,30194],{"class":209},[100,30273,206],{"class":205},[100,30275,215],{"class":118},[14,30277,30278],{},"Remove the extra spaces so the line lines up correctly.",[14,30280,30281,30282,290],{},"For more help with this exact message, see ",[295,30283,16618],{"href":29596},[77,30285,30287],{"id":30286},"example-mixed-tabs-and-spaces","Example: mixed tabs and spaces",[14,30289,30290],{},"Code can look aligned on the screen but still fail if some lines use tabs and others use spaces.",[675,30292,30294],{"id":30293},"problem-example","Problem example",[91,30296,30298],{"className":93,"code":30297,"language":95,"meta":96,"style":96},"def greet():\n    print(\"Hello\")\n    print(\"World\")\n",[17,30299,30300,30308,30322],{"__ignoreMap":96},[100,30301,30302,30304,30306],{"class":102,"line":103},[100,30303,1078],{"class":1077},[100,30305,2848],{"class":1081},[100,30307,1085],{"class":118},[100,30309,30310,30312,30314,30316,30318,30320],{"class":102,"line":135},[100,30311,200],{"class":114},[100,30313,170],{"class":118},[100,30315,206],{"class":205},[100,30317,7683],{"class":209},[100,30319,206],{"class":205},[100,30321,215],{"class":118},[100,30323,30324,30326,30328,30330,30332,30334],{"class":102,"line":142},[100,30325,200],{"class":114},[100,30327,170],{"class":118},[100,30329,206],{"class":205},[100,30331,7692],{"class":209},[100,30333,206],{"class":205},[100,30335,215],{"class":118},[14,30337,30338],{},"In this example:",[40,30340,30341,30344],{},[43,30342,30343],{},"One indented line may use a tab",[43,30345,30346],{},"Another indented line uses spaces",[14,30348,30349],{},"Even if they look similar, Python treats them differently.",[675,30351,30353],{"id":30352},"better-version","Better version",[91,30355,30356],{"className":93,"code":30297,"language":95,"meta":96,"style":96},[17,30357,30358,30366,30380],{"__ignoreMap":96},[100,30359,30360,30362,30364],{"class":102,"line":103},[100,30361,1078],{"class":1077},[100,30363,2848],{"class":1081},[100,30365,1085],{"class":118},[100,30367,30368,30370,30372,30374,30376,30378],{"class":102,"line":135},[100,30369,200],{"class":114},[100,30371,170],{"class":118},[100,30373,206],{"class":205},[100,30375,7683],{"class":209},[100,30377,206],{"class":205},[100,30379,215],{"class":118},[100,30381,30382,30384,30386,30388,30390,30392],{"class":102,"line":142},[100,30383,200],{"class":114},[100,30385,170],{"class":118},[100,30387,206],{"class":205},[100,30389,7692],{"class":209},[100,30391,206],{"class":205},[100,30393,215],{"class":118},[14,30395,30396],{},"Use spaces consistently.",[14,30398,30399],{},"Helpful tips:",[40,30401,30402,30405,30408],{},[43,30403,30404],{},"Convert tabs to spaces in your editor",[43,30406,30407],{},"Turn on visible whitespace if your editor supports it",[43,30409,30410],{},"Use auto-formatting when possible",[14,30412,30413,30414,290],{},"If your indentation levels no longer line up correctly, you may also see ",[295,30415,29539],{"href":29538},[77,30417,30419],{"id":30418},"how-to-fix-indentationerror-step-by-step","How to fix IndentationError step by step",[14,30421,30422],{},"Use this checklist:",[3282,30424,30425,30428,30431,30434,30437,30440],{},[43,30426,30427],{},"Read the error line number carefully.",[43,30429,30430],{},"Check the line above it for a missing colon.",[43,30432,30433],{},"Check whether the next line should be indented.",[43,30435,30436],{},"Compare indentation with nearby lines in the same block.",[43,30438,30439],{},"Replace tabs with spaces.",[43,30441,30442],{},"Use one consistent indentation size, usually four spaces.",[14,30444,30445],{},"A simple debugging workflow:",[91,30447,30449],{"className":10352,"code":30448,"language":10354,"meta":96,"style":96},"python your_script.py\npython -m py_compile your_script.py\n",[17,30450,30451,30458],{"__ignoreMap":96},[100,30452,30453,30455],{"class":102,"line":103},[100,30454,95],{"class":10361},[100,30456,30457],{"class":209}," your_script.py\n",[100,30459,30460,30462,30464,30466],{"class":102,"line":135},[100,30461,95],{"class":10361},[100,30463,26622],{"class":10364},[100,30465,29530],{"class":209},[100,30467,30457],{"class":209},[14,30469,8679],{},[40,30471,30472,30478],{},[43,30473,30474,30477],{},[17,30475,30476],{},"python your_script.py"," runs the file and shows the error",[43,30479,30480,30483],{},[17,30481,30482],{},"python -m py_compile your_script.py"," checks whether Python can compile the file",[14,30485,30486,30487,30490],{},"If you are still stuck, a beginner-friendly ",[295,30488,30489],{"href":3852},"guide to debugging Python code"," can help you track down the problem.",[77,30492,19830],{"id":19829},[14,30494,30495],{},"You can avoid most indentation problems with a few habits:",[40,30497,30498,30501,30504,30507,30510],{},[43,30499,30500],{},"Use an editor that highlights indentation problems",[43,30502,30503],{},"Enable visible whitespace if available",[43,30505,30506],{},"Auto-format your code when possible",[43,30508,30509],{},"Do not mix tabs and spaces",[43,30511,30512],{},"Indent blocks immediately after typing a colon",[14,30514,30515],{},"A good general rule is:",[40,30517,30518,30524],{},[43,30519,241,30520,30523],{},[22,30521,30522],{},"four spaces"," for each indentation level",[43,30525,30526],{},"Keep all lines in the same block aligned exactly",[14,30528,30529,30530,290],{},"If you want to understand the bigger picture, read ",[295,30531,30533],{"href":30532},"\u002Flearn\u002Fpython-syntax-basics-explained","Python syntax basics explained",[77,30535,30537],{"id":30536},"related-errors","Related errors",[14,30539,30540,30542],{},[17,30541,16508],{}," often appears in one of these forms:",[40,30544,30545,30549,30553,30557],{},[43,30546,30547],{},[17,30548,16618],{},[43,30550,30551],{},[17,30552,28816],{},[43,30554,30555],{},[17,30556,29539],{},[43,30558,30559],{},[17,30560,29615],{},[14,30562,30563],{},"These messages are closely related because they all affect how Python reads the structure of your code.",[77,30565,1514],{"id":1513},[675,30567,30569],{"id":30568},"what-is-the-fastest-way-to-fix-indentationerror","What is the fastest way to fix IndentationError?",[14,30571,30572],{},"Check the error line and the line above it, then make sure the block uses consistent indentation with spaces.",[675,30574,30576],{"id":30575},"should-i-use-tabs-or-spaces-in-python","Should I use tabs or spaces in Python?",[14,30578,30579],{},"Use spaces. Four spaces per indentation level is the common standard.",[675,30581,30583],{"id":30582},"can-a-missing-colon-cause-indentationerror","Can a missing colon cause IndentationError?",[14,30585,30586],{},"Yes. If Python does not see the colon that starts a block, the next indented line may trigger an indentation-related error.",[675,30588,30590],{"id":30589},"why-does-copied-code-cause-indentation-problems","Why does copied code cause indentation problems?",[14,30592,30593],{},"Copied code may contain tabs, different spacing widths, or hidden whitespace that does not match your file.",[77,30595,1554],{"id":1553},[40,30597,30598,30602,30606,30610,30614,30618],{},[43,30599,30600],{},[295,30601,28947],{"href":29936},[43,30603,30604],{},[295,30605,16618],{"href":29596},[43,30607,30608],{},[295,30609,28816],{"href":29781},[43,30611,30612],{},[295,30613,29539],{"href":29538},[43,30615,30616],{},[295,30617,29615],{"href":29614},[43,30619,30620],{},[295,30621,30622],{"href":3852},"Debug Python code: beginner guide",[1589,30624,30625],{},"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 .s39Yj, html code.shiki .s39Yj{--shiki-light:#39ADB5;--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 .sptTA, html code.shiki .sptTA{--shiki-light:#6182B8;--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 .su5hD, html code.shiki .su5hD{--shiki-light:#90A4AE;--shiki-default:#24292E;--shiki-dark:#E1E4E8}html pre.shiki code .srdBf, html code.shiki .srdBf{--shiki-light:#F76D47;--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 .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 .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 .sbgvK, html code.shiki .sbgvK{--shiki-light:#E2931D;--shiki-default:#6F42C1;--shiki-dark:#B392F0}html pre.shiki code .stzsN, html code.shiki .stzsN{--shiki-light:#91B859;--shiki-default:#005CC5;--shiki-dark:#79B8FF}",{"title":96,"searchDepth":135,"depth":135,"links":30627},[30628,30629,30630,30631,30632,30636,30640,30644,30645,30646,30647,30653],{"id":79,"depth":135,"text":80},{"id":5880,"depth":135,"text":5881},{"id":29939,"depth":135,"text":29940},{"id":29974,"depth":135,"text":29975},{"id":30039,"depth":135,"text":30040,"children":30633},[30634,30635],{"id":30048,"depth":142,"text":30049},{"id":30104,"depth":142,"text":30105},{"id":30142,"depth":135,"text":30143,"children":30637},[30638,30639],{"id":30149,"depth":142,"text":30049},{"id":30228,"depth":142,"text":30105},{"id":30286,"depth":135,"text":30287,"children":30641},[30642,30643],{"id":30293,"depth":142,"text":30294},{"id":30352,"depth":142,"text":30353},{"id":30418,"depth":135,"text":30419},{"id":19829,"depth":135,"text":19830},{"id":30536,"depth":135,"text":30537},{"id":1513,"depth":135,"text":1514,"children":30648},[30649,30650,30651,30652],{"id":30568,"depth":142,"text":30569},{"id":30575,"depth":142,"text":30576},{"id":30582,"depth":142,"text":30583},{"id":30589,"depth":142,"text":30590},{"id":1553,"depth":135,"text":1554},"Master indentationerror in python causes and fixes in our comprehensive Python beginner guide.",{},"\u002Ferrors\u002Findentationerror-in-python-causes-and-fixes",{"title":29787,"description":30654},"errors\u002Findentationerror-in-python-causes-and-fixes","fDckGk-7edcqYr49oW3DKfYloRY7mLtX2epoRPiQya0",{"id":30661,"title":30662,"body":30663,"description":31723,"extension":1623,"meta":31724,"navigation":138,"path":29596,"seo":31725,"stem":31726,"__hash__":31727},"content\u002Ferrors\u002Findentationerror-unexpected-indent-fix.md","IndentationError: unexpected indent (Fix)",{"type":7,"value":30664,"toc":31699},[30665,30668,30676,30679,30690,30693,30695,30708,30714,30757,30759,30770,30775,30777,30779,30782,30788,30791,30802,30804,30807,30821,30824,30871,30874,30876,30881,30928,30931,30934,30952,30955,30959,30962,31009,31011,31017,31020,31061,31068,31070,31109,31111,31114,31130,31133,31150,31153,31186,31194,31202,31204,31207,31224,31227,31262,31264,31297,31304,31336,31342,31347,31349,31352,31391,31394,31405,31416,31419,31434,31437,31443,31447,31453,31456,31470,31473,31478,31480,31483,31488,31495,31497,31523,31529,31534,31538,31541,31543,31585,31588,31592,31597,31604,31606,31630,31634,31636,31640,31643,31647,31654,31656,31659,31663,31666,31668,31696],[10,30666,30662],{"id":30667},"indentationerror-unexpected-indent-fix",[14,30669,30670,30672,30673,290],{},[17,30671,16618],{}," means Python found spaces or tabs at the start of a line where it was ",[22,30674,30675],{},"not expecting a new block",[14,30677,30678],{},"This usually happens when:",[40,30680,30681,30684,30687],{},[43,30682,30683],{},"a top-level line starts with extra spaces",[43,30685,30686],{},"a line is indented even though the line above it did not start a block",[43,30688,30689],{},"copied code contains hidden tabs or spaces",[14,30691,30692],{},"The good news is that this error is usually fast to fix. Python often points to the exact line that caused the problem.",[77,30694,80],{"id":79},[14,30696,30697,30698,5870,30700,5870,30702,5870,30704,5894,30706,290],{},"Only indent a line when it belongs inside a block such as ",[17,30699,2736],{},[17,30701,71],{},[17,30703,721],{},[17,30705,1078],{},[17,30707,3417],{},[14,30709,30710,30711,290],{},"Use consistent spaces, usually ",[22,30712,30713],{},"4 spaces per level",[91,30715,30717],{"className":93,"code":30716,"language":95,"meta":96,"style":96},"if True:\n    print(\"This line is correctly indented\")\nprint(\"This line is not indented, so it is outside the if block\")\n",[17,30718,30719,30727,30742],{"__ignoreMap":96},[100,30720,30721,30723,30725],{"class":102,"line":103},[100,30722,2736],{"class":145},[100,30724,159],{"class":158},[100,30726,149],{"class":118},[100,30728,30729,30731,30733,30735,30738,30740],{"class":102,"line":135},[100,30730,200],{"class":114},[100,30732,170],{"class":118},[100,30734,206],{"class":205},[100,30736,30737],{"class":209},"This line is correctly indented",[100,30739,206],{"class":205},[100,30741,215],{"class":118},[100,30743,30744,30746,30748,30750,30753,30755],{"class":102,"line":142},[100,30745,372],{"class":114},[100,30747,170],{"class":118},[100,30749,206],{"class":205},[100,30751,30752],{"class":209},"This line is not indented, so it is outside the if block",[100,30754,206],{"class":205},[100,30756,215],{"class":118},[14,30758,30338],{},[40,30760,30761,30767],{},[43,30762,30763,30764,30766],{},"the second line is indented because it belongs to the ",[17,30765,2736],{}," block",[43,30768,30769],{},"the third line is not indented, so it runs outside the block",[14,30771,30772,30773,290],{},"If you want a refresher on how Python blocks work, see ",[295,30774,28947],{"href":28946},[77,30776,5881],{"id":5880},[14,30778,29897],{},[14,30780,30781],{},"That means spaces at the start of a line are not just for formatting. They change the structure of the program.",[14,30783,1764,30784,30787],{},[22,30785,30786],{},"unexpected indent"," means Python found extra leading spaces or tabs where a new block did not make sense.",[14,30789,30790],{},"In many cases:",[40,30792,30793,30796,30799],{},[43,30794,30795],{},"the error points to the exact problem line",[43,30797,30798],{},"the real cause may be on the line above it",[43,30800,30801],{},"a missing colon can make the next indented line fail",[77,30803,26289],{"id":26288},[14,30805,30806],{},"This error often appears in these situations:",[40,30808,30809,30812,30815,30818],{},[43,30810,30811],{},"A line is indented even though no block was started before it.",[43,30813,30814],{},"Top-level code starts with spaces by mistake.",[43,30816,30817],{},"A copied line includes hidden spaces or tabs.",[43,30819,30820],{},"The indentation level changes in a way that does not match the code structure.",[14,30822,30823],{},"Python expects indentation only after a block-starting line such as:",[40,30825,30826,30830,30834,30838,30842,30846,30850,30854,30858,30863,30867],{},[43,30827,30828],{},[17,30829,2736],{},[43,30831,30832],{},[17,30833,71],{},[43,30835,30836],{},[17,30837,721],{},[43,30839,30840],{},[17,30841,1078],{},[43,30843,30844],{},[17,30845,3417],{},[43,30847,30848],{},[17,30849,146],{},[43,30851,30852],{},[17,30853,188],{},[43,30855,30856],{},[17,30857,4944],{},[43,30859,30860],{},[17,30861,30862],{},"elif",[43,30864,30865],{},[17,30866,28983],{},[43,30868,30869],{},[17,30870,17521],{},[14,30872,30873],{},"If you indent after a normal statement, Python raises an error.",[77,30875,11031],{"id":11030},[14,30877,30878,30879,89],{},"Here is a simple script that causes ",[17,30880,16618],{},[91,30882,30884],{"className":93,"code":30883,"language":95,"meta":96,"style":96},"print(\"Start\")\n    print(\"Hello\")\nprint(\"End\")\n",[17,30885,30886,30900,30914],{"__ignoreMap":96},[100,30887,30888,30890,30892,30894,30896,30898],{"class":102,"line":103},[100,30889,372],{"class":114},[100,30891,170],{"class":118},[100,30893,206],{"class":205},[100,30895,29572],{"class":209},[100,30897,206],{"class":205},[100,30899,215],{"class":118},[100,30901,30902,30904,30906,30908,30910,30912],{"class":102,"line":135},[100,30903,200],{"class":114},[100,30905,170],{"class":118},[100,30907,206],{"class":205},[100,30909,7683],{"class":209},[100,30911,206],{"class":205},[100,30913,215],{"class":118},[100,30915,30916,30918,30920,30922,30924,30926],{"class":102,"line":142},[100,30917,372],{"class":114},[100,30919,170],{"class":118},[100,30921,206],{"class":205},[100,30923,30194],{"class":209},[100,30925,206],{"class":205},[100,30927,215],{"class":118},[14,30929,30930],{},"The second line starts with spaces, but there is no block above it.",[14,30932,30933],{},"Python sees:",[40,30935,30936,30942],{},[43,30937,30938,30939],{},"a normal top-level ",[17,30940,30941],{},"print(\"Start\")",[43,30943,30944,30945,5870,30947,5870,30949,30951],{},"then an indented line that does not belong to ",[17,30946,2736],{},[17,30948,71],{},[17,30950,1078],{},", or another block",[14,30953,30954],{},"That causes the error.",[675,30956,30958],{"id":30957},"correct-version","Correct version",[14,30960,30961],{},"Remove the extra indentation:",[91,30963,30965],{"className":93,"code":30964,"language":95,"meta":96,"style":96},"print(\"Start\")\nprint(\"Hello\")\nprint(\"End\")\n",[17,30966,30967,30981,30995],{"__ignoreMap":96},[100,30968,30969,30971,30973,30975,30977,30979],{"class":102,"line":103},[100,30970,372],{"class":114},[100,30972,170],{"class":118},[100,30974,206],{"class":205},[100,30976,29572],{"class":209},[100,30978,206],{"class":205},[100,30980,215],{"class":118},[100,30982,30983,30985,30987,30989,30991,30993],{"class":102,"line":135},[100,30984,372],{"class":114},[100,30986,170],{"class":118},[100,30988,206],{"class":205},[100,30990,7683],{"class":209},[100,30992,206],{"class":205},[100,30994,215],{"class":118},[100,30996,30997,30999,31001,31003,31005,31007],{"class":102,"line":142},[100,30998,372],{"class":114},[100,31000,170],{"class":118},[100,31002,206],{"class":205},[100,31004,30194],{"class":209},[100,31006,206],{"class":205},[100,31008,215],{"class":118},[14,31010,14742],{},[91,31012,31015],{"className":31013,"code":31014,"language":4468,"meta":96},[26775],"Start\nHello\nEnd\n",[17,31016,31014],{"__ignoreMap":96},[14,31018,31019],{},"Here is another common example:",[91,31021,31023],{"className":93,"code":31022,"language":95,"meta":96,"style":96},"name = \"Sam\"\n    age = 10\nprint(name, age)\n",[17,31024,31025,31037,31046],{"__ignoreMap":96},[100,31026,31027,31029,31031,31033,31035],{"class":102,"line":103},[100,31028,1691],{"class":106},[100,31030,111],{"class":110},[100,31032,1708],{"class":205},[100,31034,29329],{"class":209},[100,31036,1714],{"class":205},[100,31038,31039,31042,31044],{"class":102,"line":135},[100,31040,31041],{"class":106},"    age ",[100,31043,111],{"class":110},[100,31045,2181],{"class":122},[100,31047,31048,31050,31052,31054,31056,31059],{"class":102,"line":142},[100,31049,372],{"class":114},[100,31051,170],{"class":118},[100,31053,2853],{"class":178},[100,31055,126],{"class":118},[100,31057,31058],{"class":178}," age",[100,31060,215],{"class":118},[14,31062,31063,31064,31067],{},"This fails for the same reason: ",[17,31065,31066],{},"age = 10"," is indented for no valid reason.",[14,31069,6031],{},[91,31071,31073],{"className":93,"code":31072,"language":95,"meta":96,"style":96},"name = \"Sam\"\nage = 10\nprint(name, age)\n",[17,31074,31075,31087,31095],{"__ignoreMap":96},[100,31076,31077,31079,31081,31083,31085],{"class":102,"line":103},[100,31078,1691],{"class":106},[100,31080,111],{"class":110},[100,31082,1708],{"class":205},[100,31084,29329],{"class":209},[100,31086,1714],{"class":205},[100,31088,31089,31091,31093],{"class":102,"line":135},[100,31090,1664],{"class":106},[100,31092,111],{"class":110},[100,31094,2181],{"class":122},[100,31096,31097,31099,31101,31103,31105,31107],{"class":102,"line":142},[100,31098,372],{"class":114},[100,31100,170],{"class":118},[100,31102,2853],{"class":178},[100,31104,126],{"class":118},[100,31106,31058],{"class":178},[100,31108,215],{"class":118},[77,31110,1344],{"id":1343},[14,31112,31113],{},"The most common reasons for this error are:",[40,31115,31116,31119,31122,31125,31127],{},[43,31117,31118],{},"Accidentally pressing Tab or Space at the start of a line",[43,31120,31121],{},"Indenting after a line that does not end with a colon",[43,31123,31124],{},"Pasting code from a website or document",[43,31126,29638],{},[43,31128,31129],{},"Editing one line manually so it no longer matches nearby lines",[14,31131,31132],{},"More specifically, watch for these common mistakes:",[40,31134,31135,31138,31141,31144,31147],{},[43,31136,31137],{},"Extra spaces before a top-level statement",[43,31139,31140],{},"Indenting a line after a statement that does not create a block",[43,31142,31143],{},"Copy-pasted code with hidden indentation",[43,31145,31146],{},"Using tabs on one line and spaces on another",[43,31148,31149],{},"Missing colon on the previous line",[14,31151,31152],{},"A missing colon is especially important. For example:",[91,31154,31156],{"className":93,"code":31155,"language":95,"meta":96,"style":96},"if 5 > 3\n    print(\"Yes\")\n",[17,31157,31158,31171],{"__ignoreMap":96},[100,31159,31160,31162,31165,31168],{"class":102,"line":103},[100,31161,2736],{"class":145},[100,31163,31164],{"class":122}," 5",[100,31166,31167],{"class":110}," >",[100,31169,31170],{"class":122}," 3\n",[100,31172,31173,31175,31177,31179,31182,31184],{"class":102,"line":135},[100,31174,200],{"class":114},[100,31176,170],{"class":118},[100,31178,206],{"class":205},[100,31180,31181],{"class":209},"Yes",[100,31183,206],{"class":205},[100,31185,215],{"class":118},[14,31187,31188,31189,31191,31192,290],{},"This code is wrong because the ",[17,31190,2736],{}," line is missing ",[17,31193,89],{},[14,31195,31196,31197,31199,31200,29597],{},"Python may report a syntax problem on the ",[17,31198,2736],{}," line, or the next indented line may make the problem more confusing. See ",[295,31201,29615],{"href":29614},[77,31203,6244],{"id":6243},[14,31205,31206],{},"Use this simple process:",[3282,31208,31209,31212,31215,31218,31221],{},[43,31210,31211],{},"Go to the line shown in the traceback.",[43,31213,31214],{},"Check whether that line should really be inside a block.",[43,31216,31217],{},"If it should be top-level code, remove the extra spaces.",[43,31219,31220],{},"If it belongs in a block, check the line above it for a missing colon.",[43,31222,31223],{},"Convert tabs to spaces and keep indentation consistent.",[14,31225,31226],{},"Example of a missing block starter:",[91,31228,31230],{"className":93,"code":31229,"language":95,"meta":96,"style":96},"print(\"Checking value\")\n    print(\"This should not be indented\")\n",[17,31231,31232,31247],{"__ignoreMap":96},[100,31233,31234,31236,31238,31240,31243,31245],{"class":102,"line":103},[100,31235,372],{"class":114},[100,31237,170],{"class":118},[100,31239,206],{"class":205},[100,31241,31242],{"class":209},"Checking value",[100,31244,206],{"class":205},[100,31246,215],{"class":118},[100,31248,31249,31251,31253,31255,31258,31260],{"class":102,"line":135},[100,31250,200],{"class":114},[100,31252,170],{"class":118},[100,31254,206],{"class":205},[100,31256,31257],{"class":209},"This should not be indented",[100,31259,206],{"class":205},[100,31261,215],{"class":118},[14,31263,15034],{},[91,31265,31267],{"className":93,"code":31266,"language":95,"meta":96,"style":96},"print(\"Checking value\")\nprint(\"This should not be indented\")\n",[17,31268,31269,31283],{"__ignoreMap":96},[100,31270,31271,31273,31275,31277,31279,31281],{"class":102,"line":103},[100,31272,372],{"class":114},[100,31274,170],{"class":118},[100,31276,206],{"class":205},[100,31278,31242],{"class":209},[100,31280,206],{"class":205},[100,31282,215],{"class":118},[100,31284,31285,31287,31289,31291,31293,31295],{"class":102,"line":135},[100,31286,372],{"class":114},[100,31288,170],{"class":118},[100,31290,206],{"class":205},[100,31292,31257],{"class":209},[100,31294,206],{"class":205},[100,31296,215],{"class":118},[14,31298,31299,31300,31303],{},"Example where the line ",[22,31301,31302],{},"should"," be indented because it belongs to a block:",[91,31305,31307],{"className":93,"code":31306,"language":95,"meta":96,"style":96},"if 10 > 5:\n    print(\"10 is greater than 5\")\n",[17,31308,31309,31321],{"__ignoreMap":96},[100,31310,31311,31313,31315,31317,31319],{"class":102,"line":103},[100,31312,2736],{"class":145},[100,31314,6550],{"class":122},[100,31316,31167],{"class":110},[100,31318,31164],{"class":122},[100,31320,149],{"class":118},[100,31322,31323,31325,31327,31329,31332,31334],{"class":102,"line":135},[100,31324,200],{"class":114},[100,31326,170],{"class":118},[100,31328,206],{"class":205},[100,31330,31331],{"class":209},"10 is greater than 5",[100,31333,206],{"class":205},[100,31335,215],{"class":118},[14,31337,31338,31339,31341],{},"Here the indentation is correct because the ",[17,31340,2736],{}," statement starts a block.",[14,31343,31344,31345,290],{},"If Python says a block is missing instead of extra indentation, that is a different error. See ",[295,31346,28816],{"href":29781},[77,31348,1168],{"id":1167},[14,31350,31351],{},"When you see this error, check these things in order:",[40,31353,31354,31357,31382,31385,31388],{},[43,31355,31356],{},"Look at the error line and the line before it.",[43,31358,31359,31360,5870,31362,5870,31364,5870,31366,5870,31368,5870,31370,5870,31372,5870,31374,5870,31376,5870,31378,5629,31380,290],{},"Check for ",[17,31361,2736],{},[17,31363,71],{},[17,31365,721],{},[17,31367,1078],{},[17,31369,3417],{},[17,31371,146],{},[17,31373,188],{},[17,31375,4944],{},[17,31377,30862],{},[17,31379,28983],{},[17,31381,17521],{},[43,31383,31384],{},"Make sure block-starting lines end with a colon.",[43,31386,31387],{},"Use 4 spaces per indentation level.",[43,31389,31390],{},"Turn on visible whitespace in your editor if available.",[14,31392,31393],{},"You can also run your file from the terminal to see the traceback clearly:",[91,31395,31397],{"className":10352,"code":31396,"language":10354,"meta":96,"style":96},"python your_script.py\n",[17,31398,31399],{"__ignoreMap":96},[100,31400,31401,31403],{"class":102,"line":103},[100,31402,95],{"class":10361},[100,31404,30457],{"class":209},[91,31406,31408],{"className":10352,"code":31407,"language":10354,"meta":96,"style":96},"python3 your_script.py\n",[17,31409,31410],{"__ignoreMap":96},[100,31411,31412,31414],{"class":102,"line":103},[100,31413,27614],{"class":10361},[100,31415,30457],{"class":209},[14,31417,31418],{},"To check for syntax and indentation problems without running the script normally, you can use:",[91,31420,31422],{"className":10352,"code":31421,"language":10354,"meta":96,"style":96},"python -m py_compile your_script.py\n",[17,31423,31424],{"__ignoreMap":96},[100,31425,31426,31428,31430,31432],{"class":102,"line":103},[100,31427,95],{"class":10361},[100,31429,26622],{"class":10364},[100,31431,29530],{"class":209},[100,31433,30457],{"class":209},[14,31435,31436],{},"That command is useful when you only want Python to check the file for errors.",[14,31438,31439,31440,16458],{},"If you are new to reading tracebacks, ",[295,31441,31442],{"href":3852},"how to debug Python code as a beginner",[77,31444,31446],{"id":31445},"tabs-vs-spaces","Tabs vs spaces",[14,31448,31449,31450,290],{},"Python code is easier to manage when you use ",[22,31451,31452],{},"spaces only",[14,31454,31455],{},"For beginners, the best rule is simple:",[40,31457,31458,31464,31467],{},[43,31459,31460,31461],{},"use ",[22,31462,31463],{},"4 spaces",[43,31465,31466],{},"do not use tabs",[43,31468,31469],{},"keep every line in the same block aligned",[14,31471,31472],{},"Many code editors can automatically convert tabs to spaces. That helps prevent indentation problems.",[14,31474,31475,31476,290],{},"Mixing tabs and spaces can also cause a related error: ",[295,31477,29539],{"href":29538},[77,31479,7081],{"id":7080},[14,31481,31482],{},"This error is easy to confuse with other indentation and syntax errors.",[675,31484,31486],{"id":31485},"indentationerror-expected-an-indented-block",[17,31487,28816],{},[14,31489,31490,31491,31494],{},"This happens when indentation is ",[22,31492,31493],{},"missing",", not extra.",[14,31496,1844],{},[91,31498,31499],{"className":93,"code":28993,"language":95,"meta":96,"style":96},[17,31500,31501,31509],{"__ignoreMap":96},[100,31502,31503,31505,31507],{"class":102,"line":103},[100,31504,2736],{"class":145},[100,31506,159],{"class":158},[100,31508,149],{"class":118},[100,31510,31511,31513,31515,31517,31519,31521],{"class":102,"line":135},[100,31512,372],{"class":114},[100,31514,170],{"class":118},[100,31516,206],{"class":205},[100,31518,7683],{"class":209},[100,31520,206],{"class":205},[100,31522,215],{"class":118},[14,31524,31525,31526,31528],{},"Python expected the ",[17,31527,13235],{}," line to be indented.",[14,31530,31531,31532],{},"Related page: ",[295,31533,28816],{"href":29781},[675,31535,31536],{"id":29600},[17,31537,29539],{},[14,31539,31540],{},"This happens when indentation levels do not line up correctly.",[14,31542,1844],{},[91,31544,31546],{"className":93,"code":31545,"language":95,"meta":96,"style":96},"if True:\n    print(\"A\")\n  print(\"B\")\n",[17,31547,31548,31556,31570],{"__ignoreMap":96},[100,31549,31550,31552,31554],{"class":102,"line":103},[100,31551,2736],{"class":145},[100,31553,159],{"class":158},[100,31555,149],{"class":118},[100,31557,31558,31560,31562,31564,31566,31568],{"class":102,"line":135},[100,31559,200],{"class":114},[100,31561,170],{"class":118},[100,31563,206],{"class":205},[100,31565,7414],{"class":209},[100,31567,206],{"class":205},[100,31569,215],{"class":118},[100,31571,31572,31575,31577,31579,31581,31583],{"class":102,"line":142},[100,31573,31574],{"class":114},"  print",[100,31576,170],{"class":118},[100,31578,206],{"class":205},[100,31580,7423],{"class":209},[100,31582,206],{"class":205},[100,31584,215],{"class":118},[14,31586,31587],{},"The last line does not match the expected indentation structure.",[14,31589,31531,31590],{},[295,31591,29539],{"href":29538},[675,31593,31595],{"id":31594},"syntaxerror-missing-colon",[17,31596,29615],{},[14,31598,31599,31600,31603],{},"A missing colon on a line like ",[17,31601,31602],{},"if x == 1:"," can make the next indented line look wrong.",[14,31605,1844],{},[91,31607,31608],{"className":93,"code":16557,"language":95,"meta":96,"style":96},[17,31609,31610,31616],{"__ignoreMap":96},[100,31611,31612,31614],{"class":102,"line":103},[100,31613,2736],{"class":145},[100,31615,3588],{"class":158},[100,31617,31618,31620,31622,31624,31626,31628],{"class":102,"line":135},[100,31619,200],{"class":114},[100,31621,170],{"class":118},[100,31623,206],{"class":205},[100,31625,7683],{"class":209},[100,31627,206],{"class":205},[100,31629,215],{"class":118},[14,31631,31531,31632],{},[295,31633,29615],{"href":29614},[77,31635,1514],{"id":1513},[675,31637,31639],{"id":31638},"what-does-unexpected-indent-mean-in-python","What does unexpected indent mean in Python?",[14,31641,31642],{},"It means a line starts with spaces or tabs where Python was not expecting a new indented block.",[675,31644,31646],{"id":31645},"can-a-missing-colon-cause-this-error","Can a missing colon cause this error?",[14,31648,31649,31650,31653],{},"Yes. If a line like ",[17,31651,31652],{},"if x == 1"," is missing a colon, the next indented line may trigger an unexpected indent error.",[675,31655,30576],{"id":30575},[14,31657,31658],{},"Use spaces. The common style is 4 spaces for each indentation level.",[675,31660,31662],{"id":31661},"why-does-copied-code-sometimes-cause-indentation-errors","Why does copied code sometimes cause indentation errors?",[14,31664,31665],{},"Copied code may include tabs, non-standard spaces, or extra leading whitespace that is hard to see.",[77,31667,1554],{"id":1553},[40,31669,31670,31674,31679,31684,31688,31692],{},[43,31671,31672],{},[295,31673,28947],{"href":28946},[43,31675,31676],{},[295,31677,30533],{"href":31678},"\u002Flearn\u002Fpython-syntax-basics-explained\u002F",[43,31680,31681],{},[295,31682,31683],{"href":3852},"How to debug Python code as a beginner",[43,31685,31686],{},[295,31687,28816],{"href":29781},[43,31689,31690],{},[295,31691,29539],{"href":29538},[43,31693,31694],{},[295,31695,29615],{"href":29614},[1589,31697,31698],{},"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 .s39Yj, html code.shiki .s39Yj{--shiki-light:#39ADB5;--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 .sptTA, html code.shiki .sptTA{--shiki-light:#6182B8;--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 .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 .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 .slqww, html code.shiki .slqww{--shiki-light:#6182B8;--shiki-default:#24292E;--shiki-dark:#E1E4E8}html pre.shiki code .sbgvK, html code.shiki .sbgvK{--shiki-light:#E2931D;--shiki-default:#6F42C1;--shiki-dark:#B392F0}html pre.shiki code .stzsN, html code.shiki .stzsN{--shiki-light:#91B859;--shiki-default:#005CC5;--shiki-dark:#79B8FF}",{"title":96,"searchDepth":135,"depth":135,"links":31700},[31701,31702,31703,31704,31707,31708,31709,31710,31711,31716,31722],{"id":79,"depth":135,"text":80},{"id":5880,"depth":135,"text":5881},{"id":26288,"depth":135,"text":26289},{"id":11030,"depth":135,"text":11031,"children":31705},[31706],{"id":30957,"depth":142,"text":30958},{"id":1343,"depth":135,"text":1344},{"id":6243,"depth":135,"text":6244},{"id":1167,"depth":135,"text":1168},{"id":31445,"depth":135,"text":31446},{"id":7080,"depth":135,"text":7081,"children":31712},[31713,31714,31715],{"id":31485,"depth":142,"text":28816},{"id":29600,"depth":142,"text":29539},{"id":31594,"depth":142,"text":29615},{"id":1513,"depth":135,"text":1514,"children":31717},[31718,31719,31720,31721],{"id":31638,"depth":142,"text":31639},{"id":31645,"depth":142,"text":31646},{"id":30575,"depth":142,"text":30576},{"id":31661,"depth":142,"text":31662},{"id":1553,"depth":135,"text":1554},"Master indentationerror unexpected indent fix in our comprehensive Python beginner guide.",{},{"title":30662,"description":31723},"errors\u002Findentationerror-unexpected-indent-fix","Z416xcYcjm-LYA1MHM3j0SjAirOC_3-Pkdqc-BpGaIg",{"id":31729,"title":31730,"body":31731,"description":32613,"extension":1623,"meta":32614,"navigation":138,"path":29538,"seo":32615,"stem":32616,"__hash__":32617},"content\u002Ferrors\u002Findentationerror-unindent-does-not-match-any-outer-indentation-level-fix.md","IndentationError: unindent does not match any outer indentation level (Fix)",{"type":7,"value":31732,"toc":32594},[31733,31736,31742,31744,31751,31807,31809,31829,31834,31836,31839,31842,31844,31852,31855,31858,31899,31912,31914,31917,31949,31951,31967,31969,31972,32013,32016,32041,32044,32058,32061,32068,32071,32112,32115,32117,32120,32137,32140,32204,32207,32234,32237,32239,32242,32262,32265,32291,32294,32315,32321,32325,32328,32360,32363,32405,32408,32410,32450,32454,32457,32460,32474,32477,32488,32490,32493,32517,32530,32532,32536,32539,32543,32546,32550,32553,32557,32560,32564,32567,32569,32591],[10,31734,31730],{"id":31735},"indentationerror-unindent-does-not-match-any-outer-indentation-level-fix",[14,31737,31738,31739,31741],{},"Fix the Python error ",[17,31740,29539],{},". This error usually means one line is indented differently from the surrounding block. On this page, you will learn what the error means, why it happens, how to find the bad indentation, and how to fix it.",[77,31743,80],{"id":79},[14,31745,31746,31747,31750],{},"Use one indentation style consistently. For most beginners, the safest choice is ",[22,31748,31749],{},"4 spaces per indentation level",". Do not mix tabs and spaces.",[91,31752,31754],{"className":93,"code":31753,"language":95,"meta":96,"style":96},"if True:\n    print(\"Hello\")\n    print(\"World\")\nprint(\"Done\")\n",[17,31755,31756,31764,31778,31792],{"__ignoreMap":96},[100,31757,31758,31760,31762],{"class":102,"line":103},[100,31759,2736],{"class":145},[100,31761,159],{"class":158},[100,31763,149],{"class":118},[100,31765,31766,31768,31770,31772,31774,31776],{"class":102,"line":135},[100,31767,200],{"class":114},[100,31769,170],{"class":118},[100,31771,206],{"class":205},[100,31773,7683],{"class":209},[100,31775,206],{"class":205},[100,31777,215],{"class":118},[100,31779,31780,31782,31784,31786,31788,31790],{"class":102,"line":142},[100,31781,200],{"class":114},[100,31783,170],{"class":118},[100,31785,206],{"class":205},[100,31787,7692],{"class":209},[100,31789,206],{"class":205},[100,31791,215],{"class":118},[100,31793,31794,31796,31798,31800,31803,31805],{"class":102,"line":152},[100,31795,372],{"class":114},[100,31797,170],{"class":118},[100,31799,206],{"class":205},[100,31801,31802],{"class":209},"Done",[100,31804,206],{"class":205},[100,31806,215],{"class":118},[14,31808,30338],{},[40,31810,31811,31820,31826],{},[43,31812,31813,31814,31816,31817,31819],{},"Both ",[17,31815,13235],{}," lines inside the ",[17,31818,2736],{}," block use the same indentation.",[43,31821,31822,31825],{},[17,31823,31824],{},"print(\"Done\")"," is moved back to the left to end the block.",[43,31827,31828],{},"That unindent is valid because it matches an earlier indentation level.",[14,31830,31831,31832,290],{},"If you are not fully sure how Python blocks work, see ",[295,31833,28947],{"href":29936},[77,31835,5881],{"id":5880},[14,31837,31838],{},"Python uses indentation to group code into blocks.",[14,31840,31841],{},"This error appears when a line is unindented to a level that does not match any earlier block.",[14,31843,12895],{},[40,31845,31846,31849],{},[43,31847,31848],{},"the line starts too far left, or",[43,31850,31851],{},"it uses a different indentation pattern than Python expects.",[14,31853,31854],{},"Python does not use indentation just for style. It uses it to understand your program structure.",[14,31856,31857],{},"For example, in code like this:",[91,31859,31861],{"className":93,"code":31860,"language":95,"meta":96,"style":96},"if True:\n    print(\"A\")\nprint(\"B\")\n",[17,31862,31863,31871,31885],{"__ignoreMap":96},[100,31864,31865,31867,31869],{"class":102,"line":103},[100,31866,2736],{"class":145},[100,31868,159],{"class":158},[100,31870,149],{"class":118},[100,31872,31873,31875,31877,31879,31881,31883],{"class":102,"line":135},[100,31874,200],{"class":114},[100,31876,170],{"class":118},[100,31878,206],{"class":205},[100,31880,7414],{"class":209},[100,31882,206],{"class":205},[100,31884,215],{"class":118},[100,31886,31887,31889,31891,31893,31895,31897],{"class":102,"line":142},[100,31888,372],{"class":114},[100,31890,170],{"class":118},[100,31892,206],{"class":205},[100,31894,7423],{"class":209},[100,31896,206],{"class":205},[100,31898,215],{"class":118},[14,31900,31901,31904,31905,31907,31908,31911],{},[17,31902,31903],{},"print(\"A\")"," belongs to the ",[17,31906,2736],{}," block, and ",[17,31909,31910],{},"print(\"B\")"," does not.",[77,31913,25091],{"id":25090},[14,31915,31916],{},"This error often happens for one of these reasons:",[40,31918,31919,31922,31925,31928,31931],{},[43,31920,31921],{},"A line is moved back, but not to a valid previous indentation level.",[43,31923,31924],{},"Tabs and spaces are mixed in the same block.",[43,31926,31927],{},"One line uses 2 spaces and another uses 4 spaces.",[43,31929,31930],{},"Code was copied from another editor and kept hidden indentation characters.",[43,31932,31933,31934,5870,31936,5870,31938,5870,31940,5870,31942,5870,31944,5894,31946,31948],{},"A block inside ",[17,31935,2736],{},[17,31937,71],{},[17,31939,721],{},[17,31941,146],{},[17,31943,188],{},[17,31945,1078],{},[17,31947,3417],{}," is not aligned correctly.",[14,31950,3971],{},[40,31952,31953,31955,31958,31961,31964],{},[43,31954,29638],{},[43,31956,31957],{},"Using inconsistent numbers of spaces",[43,31959,31960],{},"Unindenting to the wrong level",[43,31962,31963],{},"Pasting code with hidden indentation characters",[43,31965,31966],{},"Editing nested blocks without rechecking alignment",[77,31968,11031],{"id":11030},[14,31970,31971],{},"Here is a small example with bad indentation:",[91,31973,31975],{"className":93,"code":31974,"language":95,"meta":96,"style":96},"if True:\n    print(\"Hello\")\n  print(\"World\")\n",[17,31976,31977,31985,31999],{"__ignoreMap":96},[100,31978,31979,31981,31983],{"class":102,"line":103},[100,31980,2736],{"class":145},[100,31982,159],{"class":158},[100,31984,149],{"class":118},[100,31986,31987,31989,31991,31993,31995,31997],{"class":102,"line":135},[100,31988,200],{"class":114},[100,31990,170],{"class":118},[100,31992,206],{"class":205},[100,31994,7683],{"class":209},[100,31996,206],{"class":205},[100,31998,215],{"class":118},[100,32000,32001,32003,32005,32007,32009,32011],{"class":102,"line":142},[100,32002,31574],{"class":114},[100,32004,170],{"class":118},[100,32006,206],{"class":205},[100,32008,7692],{"class":209},[100,32010,206],{"class":205},[100,32012,215],{"class":118},[14,32014,32015],{},"This can raise:",[91,32017,32019],{"className":93,"code":32018,"language":95,"meta":96,"style":96},"IndentationError: unindent does not match any outer indentation level\n",[17,32020,32021],{"__ignoreMap":96},[100,32022,32023,32025,32027,32030,32032,32035,32038],{"class":102,"line":103},[100,32024,16508],{"class":191},[100,32026,89],{"class":118},[100,32028,32029],{"class":106}," unindent does ",[100,32031,1059],{"class":110},[100,32033,32034],{"class":106}," match ",[100,32036,32037],{"class":114},"any",[100,32039,32040],{"class":106}," outer indentation level\n",[14,32042,32043],{},"Why?",[40,32045,32046,32052,32055],{},[43,32047,32048,32049,32051],{},"The second ",[17,32050,13235],{}," line is moved left.",[43,32053,32054],{},"But it does not return to a valid earlier indentation level.",[43,32056,32057],{},"It uses 2 spaces instead of matching the existing block structure.",[14,32059,32060],{},"A line like this can look almost correct in some editors. That is why indentation errors are frustrating for beginners.",[14,32062,32063,32064,32067],{},"Python checks the ",[22,32065,32066],{},"actual indentation characters",", not just how the code looks on screen.",[14,32069,32070],{},"Here is the corrected version:",[91,32072,32074],{"className":93,"code":32073,"language":95,"meta":96,"style":96},"if True:\n    print(\"Hello\")\n    print(\"World\")\n",[17,32075,32076,32084,32098],{"__ignoreMap":96},[100,32077,32078,32080,32082],{"class":102,"line":103},[100,32079,2736],{"class":145},[100,32081,159],{"class":158},[100,32083,149],{"class":118},[100,32085,32086,32088,32090,32092,32094,32096],{"class":102,"line":135},[100,32087,200],{"class":114},[100,32089,170],{"class":118},[100,32091,206],{"class":205},[100,32093,7683],{"class":209},[100,32095,206],{"class":205},[100,32097,215],{"class":118},[100,32099,32100,32102,32104,32106,32108,32110],{"class":102,"line":142},[100,32101,200],{"class":114},[100,32103,170],{"class":118},[100,32105,206],{"class":205},[100,32107,7692],{"class":209},[100,32109,206],{"class":205},[100,32111,215],{"class":118},[14,32113,32114],{},"Both lines now use the same indentation, so they are in the same block.",[77,32116,6244],{"id":6243},[14,32118,32119],{},"To fix this error:",[40,32121,32122,32125,32128,32131,32134],{},[43,32123,32124],{},"Make all lines in the same block use the same indentation.",[43,32126,32127],{},"Use 4 spaces for each indentation level.",[43,32129,32130],{},"Replace tabs with spaces in your editor.",[43,32132,32133],{},"Re-indent the whole block instead of fixing only one line.",[43,32135,32136],{},"Check the line above the error, because the real problem is often nearby.",[14,32138,32139],{},"Example with a nested block:",[91,32141,32143],{"className":93,"code":32142,"language":95,"meta":96,"style":96},"for i in range(2):\n    if i == 0:\n        print(\"zero\")\n    print(\"done with this loop\")\n",[17,32144,32145,32161,32174,32189],{"__ignoreMap":96},[100,32146,32147,32149,32151,32153,32155,32157,32159],{"class":102,"line":103},[100,32148,71],{"class":145},[100,32150,29838],{"class":106},[100,32152,617],{"class":145},[100,32154,29843],{"class":114},[100,32156,170],{"class":118},[100,32158,559],{"class":122},[100,32160,522],{"class":118},[100,32162,32163,32165,32167,32170,32172],{"class":102,"line":135},[100,32164,2777],{"class":145},[100,32166,29838],{"class":106},[100,32168,32169],{"class":110},"==",[100,32171,1921],{"class":122},[100,32173,149],{"class":118},[100,32175,32176,32178,32180,32182,32185,32187],{"class":102,"line":142},[100,32177,167],{"class":114},[100,32179,170],{"class":118},[100,32181,206],{"class":205},[100,32183,32184],{"class":209},"zero",[100,32186,206],{"class":205},[100,32188,215],{"class":118},[100,32190,32191,32193,32195,32197,32200,32202],{"class":102,"line":152},[100,32192,200],{"class":114},[100,32194,170],{"class":118},[100,32196,206],{"class":205},[100,32198,32199],{"class":209},"done with this loop",[100,32201,206],{"class":205},[100,32203,215],{"class":118},[14,32205,32206],{},"This works because:",[40,32208,32209,32218,32225],{},[43,32210,32211,32214,32215,32217],{},[17,32212,32213],{},"if i == 0:"," is inside the ",[17,32216,71],{}," loop.",[43,32219,32220,32214,32223,25128],{},[17,32221,32222],{},"print(\"zero\")",[17,32224,2736],{},[43,32226,32227,32230,32231,32233],{},[17,32228,32229],{},"print(\"done with this loop\")"," is unindented back to the ",[17,32232,71],{}," loop level, which is valid.",[14,32235,32236],{},"If one of those lines used a different number of spaces, Python could raise an indentation error.",[77,32238,8550],{"id":8549},[14,32240,32241],{},"When you see this error, use this checklist:",[3282,32243,32244,32247,32250,32253,32256,32259],{},[43,32245,32246],{},"Find the line number in the error message.",[43,32248,32249],{},"Look at the block above that line.",[43,32251,32252],{},"Compare the indentation of nearby lines that should be at the same level.",[43,32254,32255],{},"Delete the indentation on the bad line and type it again manually.",[43,32257,32258],{},"Turn on whitespace or invisible character display in your editor.",[43,32260,32261],{},"Convert all tabs to spaces if needed.",[14,32263,32264],{},"You can also run your script from the terminal:",[91,32266,32268],{"className":10352,"code":32267,"language":10354,"meta":96,"style":96},"python your_script.py\npython -tt your_script.py\npython3 your_script.py\n",[17,32269,32270,32276,32285],{"__ignoreMap":96},[100,32271,32272,32274],{"class":102,"line":103},[100,32273,95],{"class":10361},[100,32275,30457],{"class":209},[100,32277,32278,32280,32283],{"class":102,"line":135},[100,32279,95],{"class":10361},[100,32281,32282],{"class":10364}," -tt",[100,32284,30457],{"class":209},[100,32286,32287,32289],{"class":102,"line":142},[100,32288,27614],{"class":10361},[100,32290,30457],{"class":209},[14,32292,32293],{},"Notes:",[40,32295,32296,32301,32309],{},[43,32297,32298,32300],{},[17,32299,30476],{}," runs the script normally.",[43,32302,32303,32306,32307,290],{},[17,32304,32305],{},"python3 your_script.py"," is often used on systems where Python 3 is available as ",[17,32308,27614],{},[43,32310,32311,32314],{},[17,32312,32313],{},"python -tt your_script.py"," can help detect tab-related indentation problems more strictly in some environments.",[14,32316,32317,32318,290],{},"If you are working through several errors, a beginner-friendly next step is ",[295,32319,3853],{"href":32320},"\u002Fhow-to\u002Fhow-to-debug-python-code-beginner-guide",[77,32322,32324],{"id":32323},"common-places-beginners-see-this-error","Common places beginners see this error",[14,32326,32327],{},"Beginners often see this error:",[40,32329,32330,32336,32344,32349,32357],{},[43,32331,32332,32333,32335],{},"Inside nested ",[17,32334,2736],{}," statements",[43,32337,32338,32339,3178,32341,32343],{},"After a ",[17,32340,71],{},[17,32342,721],{}," loop",[43,32345,32346,32347],{},"Inside functions defined with ",[17,32348,1078],{},[43,32350,32351,32352,2193,32354,32356],{},"In ",[17,32353,146],{},[17,32355,188],{}," blocks",[43,32358,32359],{},"When pasting code from websites or chat apps",[14,32361,32362],{},"Example inside a function:",[91,32364,32366],{"className":93,"code":32365,"language":95,"meta":96,"style":96},"def greet():\n    print(\"Hello\")\n   print(\"World\")\n",[17,32367,32368,32376,32390],{"__ignoreMap":96},[100,32369,32370,32372,32374],{"class":102,"line":103},[100,32371,1078],{"class":1077},[100,32373,2848],{"class":1081},[100,32375,1085],{"class":118},[100,32377,32378,32380,32382,32384,32386,32388],{"class":102,"line":135},[100,32379,200],{"class":114},[100,32381,170],{"class":118},[100,32383,206],{"class":205},[100,32385,7683],{"class":209},[100,32387,206],{"class":205},[100,32389,215],{"class":118},[100,32391,32392,32395,32397,32399,32401,32403],{"class":102,"line":142},[100,32393,32394],{"class":114},"   print",[100,32396,170],{"class":118},[100,32398,206],{"class":205},[100,32400,7692],{"class":209},[100,32402,206],{"class":205},[100,32404,215],{"class":118},[14,32406,32407],{},"The second line is not aligned correctly with the first line in the function body.",[14,32409,6031],{},[91,32411,32412],{"className":93,"code":30297,"language":95,"meta":96,"style":96},[17,32413,32414,32422,32436],{"__ignoreMap":96},[100,32415,32416,32418,32420],{"class":102,"line":103},[100,32417,1078],{"class":1077},[100,32419,2848],{"class":1081},[100,32421,1085],{"class":118},[100,32423,32424,32426,32428,32430,32432,32434],{"class":102,"line":135},[100,32425,200],{"class":114},[100,32427,170],{"class":118},[100,32429,206],{"class":205},[100,32431,7683],{"class":209},[100,32433,206],{"class":205},[100,32435,215],{"class":118},[100,32437,32438,32440,32442,32444,32446,32448],{"class":102,"line":142},[100,32439,200],{"class":114},[100,32441,170],{"class":118},[100,32443,206],{"class":205},[100,32445,7692],{"class":209},[100,32447,206],{"class":205},[100,32449,215],{"class":118},[77,32451,32453],{"id":32452},"how-editors-can-help","How editors can help",[14,32455,32456],{},"Your editor can prevent many indentation problems.",[14,32458,32459],{},"Useful settings:",[40,32461,32462,32465,32468,32471],{},[43,32463,32464],{},"Enable visible whitespace",[43,32466,32467],{},"Set tab size to 4 spaces",[43,32469,32470],{},"Use auto-formatting if your editor supports it",[43,32472,32473],{},"Configure the editor to insert spaces when you press Tab",[14,32475,32476],{},"If you copy code from somewhere else, it is often safer to:",[40,32478,32479,32482,32485],{},[43,32480,32481],{},"select the whole block",[43,32483,32484],{},"remove the indentation",[43,32486,32487],{},"indent it again consistently",[77,32489,30537],{"id":30536},[14,32491,32492],{},"This error is closely related to other indentation problems:",[40,32494,32495,32502,32509],{},[43,32496,32497,32501],{},[295,32498,32499],{"href":29781},[17,32500,28816],{}," happens when Python needs an indented block but none is given.",[43,32503,32504,32508],{},[295,32505,32506],{"href":29596},[17,32507,16618],{}," happens when a line is indented but should not be.",[43,32510,32511,32516],{},[295,32512,32513],{"href":30656},[17,32514,32515],{},"IndentationError in Python: causes and fixes"," covers indentation problems more broadly.",[14,32518,32519,32520,5870,32522,5894,32524,32526,32527,32529],{},"Sometimes indentation problems appear after a syntax mistake higher up, such as a missing colon after ",[17,32521,2736],{},[17,32523,71],{},[17,32525,1078],{},". See ",[295,32528,29615],{"href":29614}," if that sounds familiar.",[77,32531,1514],{"id":1513},[675,32533,32535],{"id":32534},"what-does-unindent-mean-in-python","What does \"unindent\" mean in Python?",[14,32537,32538],{},"It means moving a line back to the left to end a block or return to an earlier indentation level.",[675,32540,32542],{"id":32541},"can-tabs-cause-this-error","Can tabs cause this error?",[14,32544,32545],{},"Yes. Mixing tabs and spaces is one of the most common causes.",[675,32547,32549],{"id":32548},"how-many-spaces-should-i-use-in-python","How many spaces should I use in Python?",[14,32551,32552],{},"Use 4 spaces for each indentation level. This is the standard and the easiest choice for beginners.",[675,32554,32556],{"id":32555},"why-does-the-error-point-to-one-line-when-the-problem-is-somewhere-else","Why does the error point to one line when the problem is somewhere else?",[14,32558,32559],{},"Python notices the mismatch when it reaches a line with invalid indentation, but the real cause may be in the block above it.",[675,32561,32563],{"id":32562},"how-do-i-stop-this-error-from-happening-again","How do I stop this error from happening again?",[14,32565,32566],{},"Set your editor to insert spaces instead of tabs, keep indentation consistent, and use auto-formatting when possible.",[77,32568,1554],{"id":1553},[40,32570,32571,32575,32579,32583,32587],{},[43,32572,32573],{},[295,32574,28947],{"href":29936},[43,32576,32577],{},[295,32578,28816],{"href":29781},[43,32580,32581],{},[295,32582,16618],{"href":29596},[43,32584,32585],{},[295,32586,32515],{"href":30656},[43,32588,32589],{},[295,32590,5690],{"href":32320},[1589,32592,32593],{},"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 .s39Yj, html code.shiki .s39Yj{--shiki-light:#39ADB5;--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 .sptTA, html code.shiki .sptTA{--shiki-light:#6182B8;--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 .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 .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 .sbgvK, html code.shiki .sbgvK{--shiki-light:#E2931D;--shiki-default:#6F42C1;--shiki-dark:#B392F0}html pre.shiki code .stzsN, html code.shiki .stzsN{--shiki-light:#91B859;--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}",{"title":96,"searchDepth":135,"depth":135,"links":32595},[32596,32597,32598,32599,32600,32601,32602,32603,32604,32605,32612],{"id":79,"depth":135,"text":80},{"id":5880,"depth":135,"text":5881},{"id":25090,"depth":135,"text":25091},{"id":11030,"depth":135,"text":11031},{"id":6243,"depth":135,"text":6244},{"id":8549,"depth":135,"text":8550},{"id":32323,"depth":135,"text":32324},{"id":32452,"depth":135,"text":32453},{"id":30536,"depth":135,"text":30537},{"id":1513,"depth":135,"text":1514,"children":32606},[32607,32608,32609,32610,32611],{"id":32534,"depth":142,"text":32535},{"id":32541,"depth":142,"text":32542},{"id":32548,"depth":142,"text":32549},{"id":32555,"depth":142,"text":32556},{"id":32562,"depth":142,"text":32563},{"id":1553,"depth":135,"text":1554},"Master indentationerror unindent does not match any outer indentation level fix in our comprehensive Python beginner guide.",{},{"title":31730,"description":32613},"errors\u002Findentationerror-unindent-does-not-match-any-outer-indentation-level-fix","ILTR5PLw9K9SvGzEdffK__iDU7xFDtGZ0C-0C2fCkwI",{"id":32619,"title":32620,"body":32621,"description":35052,"extension":1623,"meta":35053,"navigation":138,"path":35054,"seo":35055,"stem":35056,"__hash__":35057},"content\u002Ferrors\u002Findexerror-in-python-causes-and-fixes.md","IndexError in Python: Causes and Fixes",{"type":7,"value":32622,"toc":35013},[32623,32626,32631,32634,32645,32652,32655,32669,32677,32679,32682,32786,32788,32796,32805,32809,32814,32816,32827,32830,32838,32841,32934,32936,32955,32962,32967,32969,32974,32998,33001,33032,33036,33038,33093,33096,33119,33125,33128,33195,33201,33204,33237,33246,33250,33253,33257,33260,33364,33368,33375,33450,33453,33482,33485,33506,33510,33519,33521,33602,33604,33680,33682,33698,33703,33707,33715,33792,33794,33820,33823,33827,33830,33862,33865,33925,33929,33932,33936,33963,33966,33969,34029,34033,34113,34122,34124,34209,34213,34216,34283,34292,34299,34303,34369,34381,34385,34391,34395,34456,34460,34483,34487,34521,34525,34528,34607,34611,34614,34616,34707,34710,34724,34728,34731,34734,34739,34741,34784,34787,34792,34794,34842,34848,34851,34856,34858,34913,34919,34921,34925,34928,34932,34941,34945,34950,34954,34960,34964,34972,34974,35010],[10,32624,32620],{"id":32625},"indexerror-in-python-causes-and-fixes",[14,32627,32628,32630],{},[17,32629,16420],{}," means your code tried to use a position that does not exist in a sequence.",[14,32632,32633],{},"This usually happens with:",[40,32635,32636,32639,32642],{},[43,32637,32638],{},"lists",[43,32640,32641],{},"tuples",[43,32643,32644],{},"strings",[14,32646,32647,32648,9472,32650,290],{},"In Python, indexing starts at ",[17,32649,2279],{},[17,32651,123],{},[14,32653,32654],{},"So if a list has 3 items, the valid indexes are:",[40,32656,32657,32661,32665],{},[43,32658,32659],{},[17,32660,2279],{},[43,32662,32663],{},[17,32664,123],{},[43,32666,32667],{},[17,32668,559],{},[14,32670,32671,32672,32674,32675,290],{},"If you try to access index ",[17,32673,11541],{},", Python raises an ",[17,32676,16420],{},[77,32678,80],{"id":79},[14,32680,32681],{},"If you are not sure whether an index exists, check it before using it:",[91,32683,32685],{"className":93,"code":32684,"language":95,"meta":96,"style":96},"items = [\"a\", \"b\", \"c\"]\nindex = 2\n\nif 0 \u003C= index \u003C len(items):\n    print(items[index])\nelse:\n    print(\"Index is out of range\")\n",[17,32686,32687,32719,32727,32731,32751,32765,32771],{"__ignoreMap":96},[100,32688,32689,32691,32693,32695,32697,32699,32701,32703,32705,32707,32709,32711,32713,32715,32717],{"class":102,"line":103},[100,32690,3095],{"class":106},[100,32692,111],{"class":110},[100,32694,594],{"class":118},[100,32696,206],{"class":205},[100,32698,295],{"class":209},[100,32700,206],{"class":205},[100,32702,126],{"class":118},[100,32704,1708],{"class":205},[100,32706,4027],{"class":209},[100,32708,206],{"class":205},[100,32710,126],{"class":118},[100,32712,1708],{"class":205},[100,32714,4036],{"class":209},[100,32716,206],{"class":205},[100,32718,603],{"class":118},[100,32720,32721,32723,32725],{"class":102,"line":135},[100,32722,3116],{"class":106},[100,32724,111],{"class":110},[100,32726,3564],{"class":122},[100,32728,32729],{"class":102,"line":142},[100,32730,139],{"emptyLinePlaceholder":138},[100,32732,32733,32735,32737,32739,32741,32743,32745,32747,32749],{"class":102,"line":152},[100,32734,2736],{"class":145},[100,32736,1921],{"class":122},[100,32738,3133],{"class":110},[100,32740,3136],{"class":106},[100,32742,2782],{"class":110},[100,32744,3141],{"class":114},[100,32746,170],{"class":118},[100,32748,3146],{"class":178},[100,32750,522],{"class":118},[100,32752,32753,32755,32757,32759,32761,32763],{"class":102,"line":164},[100,32754,200],{"class":114},[100,32756,170],{"class":118},[100,32758,3146],{"class":178},[100,32760,3166],{"class":118},[100,32762,3169],{"class":178},[100,32764,132],{"class":118},[100,32766,32767,32769],{"class":102,"line":185},[100,32768,4944],{"class":145},[100,32770,149],{"class":118},[100,32772,32773,32775,32777,32779,32782,32784],{"class":102,"line":197},[100,32774,200],{"class":114},[100,32776,170],{"class":118},[100,32778,206],{"class":205},[100,32780,32781],{"class":209},"Index is out of range",[100,32783,206],{"class":205},[100,32785,215],{"class":118},[14,32787,218],{},[91,32789,32790],{"className":93,"code":15565,"language":95,"meta":96,"style":96},[17,32791,32792],{"__ignoreMap":96},[100,32793,32794],{"class":102,"line":103},[100,32795,15565],{"class":106},[14,32797,241,32798,32804],{},[295,32799,32801],{"href":32800},"\u002Freference\u002Fpython-len-function-explained\u002F",[17,32802,32803],{},"len()"," to make sure the index is inside the valid range.",[77,32806,32808],{"id":32807},"what-indexerror-means","What IndexError means",[14,32810,32811,32813],{},[17,32812,16420],{}," happens when you try to access a position that is outside the valid range of a sequence.",[14,32815,13223],{},[40,32817,32818,32821,32824],{},[43,32819,32820],{},"asking for an item past the end of a list",[43,32822,32823],{},"using a negative index that goes too far left",[43,32825,32826],{},"reading the first item from an empty list",[14,32828,32829],{},"It usually appears with:",[40,32831,32832,32834,32836],{},[43,32833,32638],{},[43,32835,32641],{},[43,32837,32644],{},[14,32839,32840],{},"Python uses zero-based indexing:",[91,32842,32844],{"className":93,"code":32843,"language":95,"meta":96,"style":96},"letters = [\"a\", \"b\", \"c\"]\n\nprint(letters[0])  # first item\nprint(letters[1])  # second item\nprint(letters[2])  # third item\n",[17,32845,32846,32878,32882,32900,32917],{"__ignoreMap":96},[100,32847,32848,32850,32852,32854,32856,32858,32860,32862,32864,32866,32868,32870,32872,32874,32876],{"class":102,"line":103},[100,32849,7960],{"class":106},[100,32851,111],{"class":110},[100,32853,594],{"class":118},[100,32855,206],{"class":205},[100,32857,295],{"class":209},[100,32859,206],{"class":205},[100,32861,126],{"class":118},[100,32863,1708],{"class":205},[100,32865,4027],{"class":209},[100,32867,206],{"class":205},[100,32869,126],{"class":118},[100,32871,1708],{"class":205},[100,32873,4036],{"class":209},[100,32875,206],{"class":205},[100,32877,603],{"class":118},[100,32879,32880],{"class":102,"line":135},[100,32881,139],{"emptyLinePlaceholder":138},[100,32883,32884,32886,32888,32890,32892,32894,32897],{"class":102,"line":142},[100,32885,372],{"class":114},[100,32887,170],{"class":118},[100,32889,7997],{"class":178},[100,32891,3166],{"class":118},[100,32893,2279],{"class":122},[100,32895,32896],{"class":118},"])",[100,32898,32899],{"class":414},"  # first item\n",[100,32901,32902,32904,32906,32908,32910,32912,32914],{"class":102,"line":152},[100,32903,372],{"class":114},[100,32905,170],{"class":118},[100,32907,7997],{"class":178},[100,32909,3166],{"class":118},[100,32911,123],{"class":122},[100,32913,32896],{"class":118},[100,32915,32916],{"class":414},"  # second item\n",[100,32918,32919,32921,32923,32925,32927,32929,32931],{"class":102,"line":164},[100,32920,372],{"class":114},[100,32922,170],{"class":118},[100,32924,7997],{"class":178},[100,32926,3166],{"class":118},[100,32928,559],{"class":122},[100,32930,32896],{"class":118},[100,32932,32933],{"class":414},"  # third item\n",[14,32935,218],{},[91,32937,32939],{"className":93,"code":32938,"language":95,"meta":96,"style":96},"a\nb\nc\n",[17,32940,32941,32946,32951],{"__ignoreMap":96},[100,32942,32943],{"class":102,"line":103},[100,32944,32945],{"class":106},"a\n",[100,32947,32948],{"class":102,"line":135},[100,32949,32950],{"class":106},"b\n",[100,32952,32953],{"class":102,"line":142},[100,32954,15565],{"class":106},[14,32956,32957,32958,32961],{},"There is no ",[17,32959,32960],{},"letters[3]"," here, because the list only has 3 items.",[14,32963,32964,32965,290],{},"If you need a beginner-friendly explanation of list positions, see ",[295,32966,8111],{"href":8110},[77,32968,18290],{"id":18289},[14,32970,32971,32973],{},[17,32972,16420],{}," often appears in these situations:",[40,32975,32976,32979,32982,32992,32995],{},[43,32977,32978],{},"Using an index that is too large",[43,32980,32981],{},"Using a negative index that goes past the start of the sequence",[43,32983,32984,32985,32991],{},"Looping with the wrong ",[295,32986,32988],{"href":32987},"\u002Freference\u002Fpython-range-function-explained\u002F",[17,32989,32990],{},"range()"," values",[43,32993,32994],{},"Removing items, then trying to access an old index",[43,32996,32997],{},"Reading user input and using it directly as an index",[14,32999,33000],{},"Here are a few common causes:",[40,33002,33003,33010,33016,33023,33029],{},[43,33004,33005,33006,33009],{},"Trying to access ",[17,33007,33008],{},"items[len(items)]"," instead of the last valid item",[43,33011,1357,33012,33015],{},[17,33013,33014],{},"range(len(items) + 1)"," in a loop",[43,33017,33018,33019,33022],{},"Accessing ",[17,33020,33021],{},"[0]"," on an empty list",[43,33024,33025,33026,33028],{},"Using a number from ",[17,33027,14906],{}," without checking it",[43,33030,33031],{},"Using indexes after the sequence size changed",[77,33033,33035],{"id":33034},"example-that-causes-indexerror","Example that causes IndexError",[14,33037,1891],{},[91,33039,33041],{"className":93,"code":33040,"language":95,"meta":96,"style":96},"items = [\"apple\", \"banana\", \"cherry\"]\n\nprint(items[3])\n",[17,33042,33043,33075,33079],{"__ignoreMap":96},[100,33044,33045,33047,33049,33051,33053,33055,33057,33059,33061,33063,33065,33067,33069,33071,33073],{"class":102,"line":103},[100,33046,3095],{"class":106},[100,33048,111],{"class":110},[100,33050,594],{"class":118},[100,33052,206],{"class":205},[100,33054,4148],{"class":209},[100,33056,206],{"class":205},[100,33058,126],{"class":118},[100,33060,1708],{"class":205},[100,33062,4157],{"class":209},[100,33064,206],{"class":205},[100,33066,126],{"class":118},[100,33068,1708],{"class":205},[100,33070,8949],{"class":209},[100,33072,206],{"class":205},[100,33074,603],{"class":118},[100,33076,33077],{"class":102,"line":135},[100,33078,139],{"emptyLinePlaceholder":138},[100,33080,33081,33083,33085,33087,33089,33091],{"class":102,"line":142},[100,33082,372],{"class":114},[100,33084,170],{"class":118},[100,33086,3146],{"class":178},[100,33088,3166],{"class":118},[100,33090,11541],{"class":122},[100,33092,132],{"class":118},[14,33094,33095],{},"This list has 3 items, so the valid indexes are:",[40,33097,33098,33105,33112],{},[43,33099,33100,23456,33102],{},[17,33101,2279],{},[17,33103,33104],{},"\"apple\"",[43,33106,33107,23456,33109],{},[17,33108,123],{},[17,33110,33111],{},"\"banana\"",[43,33113,33114,23456,33116],{},[17,33115,559],{},[17,33117,33118],{},"\"cherry\"",[14,33120,33121,33122,33124],{},"Index ",[17,33123,11541],{}," does not exist.",[14,33126,33127],{},"Python raises this error:",[91,33129,33131],{"className":93,"code":33130,"language":95,"meta":96,"style":96},"Traceback (most recent call last):\n  File \"example.py\", line 3, in \u003Cmodule>\n    print(items[3])\nIndexError: list index out of range\n",[17,33132,33133,33143,33169,33183],{"__ignoreMap":96},[100,33134,33135,33137,33139,33141],{"class":102,"line":103},[100,33136,514],{"class":178},[100,33138,170],{"class":118},[100,33140,519],{"class":178},[100,33142,522],{"class":118},[100,33144,33145,33147,33149,33151,33153,33155,33157,33159,33161,33163,33165,33167],{"class":102,"line":135},[100,33146,1952],{"class":106},[100,33148,206],{"class":205},[100,33150,1957],{"class":209},[100,33152,206],{"class":205},[100,33154,126],{"class":118},[100,33156,1964],{"class":106},[100,33158,11541],{"class":122},[100,33160,126],{"class":118},[100,33162,1971],{"class":110},[100,33164,1974],{"class":110},[100,33166,1977],{"class":106},[100,33168,1980],{"class":110},[100,33170,33171,33173,33175,33177,33179,33181],{"class":102,"line":142},[100,33172,200],{"class":114},[100,33174,170],{"class":118},[100,33176,3146],{"class":178},[100,33178,3166],{"class":118},[100,33180,11541],{"class":122},[100,33182,132],{"class":118},[100,33184,33185,33187,33189,33191,33193],{"class":102,"line":152},[100,33186,16420],{"class":191},[100,33188,89],{"class":118},[100,33190,14704],{"class":191},[100,33192,16427],{"class":106},[100,33194,16430],{"class":114},[14,33196,33197,33198,290],{},"If you are dealing with this exact message, see ",[295,33199,33200],{"href":17231},"IndexError: list index out of range fix explained",[14,33202,33203],{},"Strings can raise the same kind of error:",[91,33205,33207],{"className":93,"code":33206,"language":95,"meta":96,"style":96},"word = \"cat\"\nprint(word[3])\n",[17,33208,33209,33223],{"__ignoreMap":96},[100,33210,33211,33214,33216,33218,33221],{"class":102,"line":103},[100,33212,33213],{"class":106},"word ",[100,33215,111],{"class":110},[100,33217,1708],{"class":205},[100,33219,33220],{"class":209},"cat",[100,33222,1714],{"class":205},[100,33224,33225,33227,33229,33231,33233,33235],{"class":102,"line":135},[100,33226,372],{"class":114},[100,33228,170],{"class":118},[100,33230,8478],{"class":178},[100,33232,3166],{"class":118},[100,33234,11541],{"class":122},[100,33236,132],{"class":118},[14,33238,33239,33240,5870,33242,5629,33244,290],{},"This also fails because valid indexes are ",[17,33241,2279],{},[17,33243,123],{},[17,33245,559],{},[77,33247,33249],{"id":33248},"how-to-fix-indexerror","How to fix IndexError",[14,33251,33252],{},"There is no single fix for every case. The right solution depends on why the bad index happened.",[675,33254,33256],{"id":33255},"check-the-length-first-with-len","Check the length first with len()",[14,33258,33259],{},"If an index may or may not exist, check it first:",[91,33261,33263],{"className":93,"code":33262,"language":95,"meta":96,"style":96},"items = [\"a\", \"b\", \"c\"]\nindex = 1\n\nif 0 \u003C= index \u003C len(items):\n    print(items[index])\nelse:\n    print(\"Invalid index\")\n",[17,33264,33265,33297,33305,33309,33329,33343,33349],{"__ignoreMap":96},[100,33266,33267,33269,33271,33273,33275,33277,33279,33281,33283,33285,33287,33289,33291,33293,33295],{"class":102,"line":103},[100,33268,3095],{"class":106},[100,33270,111],{"class":110},[100,33272,594],{"class":118},[100,33274,206],{"class":205},[100,33276,295],{"class":209},[100,33278,206],{"class":205},[100,33280,126],{"class":118},[100,33282,1708],{"class":205},[100,33284,4027],{"class":209},[100,33286,206],{"class":205},[100,33288,126],{"class":118},[100,33290,1708],{"class":205},[100,33292,4036],{"class":209},[100,33294,206],{"class":205},[100,33296,603],{"class":118},[100,33298,33299,33301,33303],{"class":102,"line":135},[100,33300,3116],{"class":106},[100,33302,111],{"class":110},[100,33304,1093],{"class":122},[100,33306,33307],{"class":102,"line":142},[100,33308,139],{"emptyLinePlaceholder":138},[100,33310,33311,33313,33315,33317,33319,33321,33323,33325,33327],{"class":102,"line":152},[100,33312,2736],{"class":145},[100,33314,1921],{"class":122},[100,33316,3133],{"class":110},[100,33318,3136],{"class":106},[100,33320,2782],{"class":110},[100,33322,3141],{"class":114},[100,33324,170],{"class":118},[100,33326,3146],{"class":178},[100,33328,522],{"class":118},[100,33330,33331,33333,33335,33337,33339,33341],{"class":102,"line":164},[100,33332,200],{"class":114},[100,33334,170],{"class":118},[100,33336,3146],{"class":178},[100,33338,3166],{"class":118},[100,33340,3169],{"class":178},[100,33342,132],{"class":118},[100,33344,33345,33347],{"class":102,"line":185},[100,33346,4944],{"class":145},[100,33348,149],{"class":118},[100,33350,33351,33353,33355,33357,33360,33362],{"class":102,"line":197},[100,33352,200],{"class":114},[100,33354,170],{"class":118},[100,33356,206],{"class":205},[100,33358,33359],{"class":209},"Invalid index",[100,33361,206],{"class":205},[100,33363,215],{"class":118},[675,33365,33367],{"id":33366},"use-valid-index-values-only","Use valid index values only",[14,33369,33370,33371,33374],{},"A common mistake is using ",[17,33372,33373],{},"len(items)"," as an index.",[91,33376,33378],{"className":93,"code":33377,"language":95,"meta":96,"style":96},"items = [\"a\", \"b\", \"c\"]\n\nprint(len(items))   # 3\nprint(items[2])     # last valid item\n",[17,33379,33380,33412,33416,33433],{"__ignoreMap":96},[100,33381,33382,33384,33386,33388,33390,33392,33394,33396,33398,33400,33402,33404,33406,33408,33410],{"class":102,"line":103},[100,33383,3095],{"class":106},[100,33385,111],{"class":110},[100,33387,594],{"class":118},[100,33389,206],{"class":205},[100,33391,295],{"class":209},[100,33393,206],{"class":205},[100,33395,126],{"class":118},[100,33397,1708],{"class":205},[100,33399,4027],{"class":209},[100,33401,206],{"class":205},[100,33403,126],{"class":118},[100,33405,1708],{"class":205},[100,33407,4036],{"class":209},[100,33409,206],{"class":205},[100,33411,603],{"class":118},[100,33413,33414],{"class":102,"line":135},[100,33415,139],{"emptyLinePlaceholder":138},[100,33417,33418,33420,33422,33424,33426,33428,33430],{"class":102,"line":142},[100,33419,372],{"class":114},[100,33421,170],{"class":118},[100,33423,17779],{"class":114},[100,33425,170],{"class":118},[100,33427,3146],{"class":178},[100,33429,411],{"class":118},[100,33431,33432],{"class":414},"   # 3\n",[100,33434,33435,33437,33439,33441,33443,33445,33447],{"class":102,"line":152},[100,33436,372],{"class":114},[100,33438,170],{"class":118},[100,33440,3146],{"class":178},[100,33442,3166],{"class":118},[100,33444,559],{"class":122},[100,33446,32896],{"class":118},[100,33448,33449],{"class":414},"     # last valid item\n",[14,33451,33452],{},"If you want the last item, use:",[91,33454,33456],{"className":93,"code":33455,"language":95,"meta":96,"style":96},"print(items[len(items) - 1])\n",[17,33457,33458],{"__ignoreMap":96},[100,33459,33460,33462,33464,33466,33468,33470,33472,33474,33476,33478,33480],{"class":102,"line":103},[100,33461,372],{"class":114},[100,33463,170],{"class":118},[100,33465,3146],{"class":178},[100,33467,3166],{"class":118},[100,33469,17779],{"class":114},[100,33471,170],{"class":118},[100,33473,3146],{"class":178},[100,33475,6155],{"class":118},[100,33477,1906],{"class":110},[100,33479,6915],{"class":122},[100,33481,132],{"class":118},[14,33483,33484],{},"Or more simply:",[91,33486,33488],{"className":93,"code":33487,"language":95,"meta":96,"style":96},"print(items[-1])\n",[17,33489,33490],{"__ignoreMap":96},[100,33491,33492,33494,33496,33498,33500,33502,33504],{"class":102,"line":103},[100,33493,372],{"class":114},[100,33495,170],{"class":118},[100,33497,3146],{"class":178},[100,33499,3166],{"class":118},[100,33501,2193],{"class":110},[100,33503,123],{"class":122},[100,33505,132],{"class":118},[675,33507,33509],{"id":33508},"fix-loop-ranges","Fix loop ranges",[14,33511,33512,33513,33516,33517,290],{},"A loop should stop before ",[17,33514,33515],{},"len(sequence)",", not at ",[17,33518,33515],{},[14,33520,9709],{},[91,33522,33524],{"className":93,"code":33523,"language":95,"meta":96,"style":96},"items = [\"a\", \"b\", \"c\"]\n\nfor i in range(len(items) + 1):\n    print(items[i])\n",[17,33525,33526,33558,33562,33588],{"__ignoreMap":96},[100,33527,33528,33530,33532,33534,33536,33538,33540,33542,33544,33546,33548,33550,33552,33554,33556],{"class":102,"line":103},[100,33529,3095],{"class":106},[100,33531,111],{"class":110},[100,33533,594],{"class":118},[100,33535,206],{"class":205},[100,33537,295],{"class":209},[100,33539,206],{"class":205},[100,33541,126],{"class":118},[100,33543,1708],{"class":205},[100,33545,4027],{"class":209},[100,33547,206],{"class":205},[100,33549,126],{"class":118},[100,33551,1708],{"class":205},[100,33553,4036],{"class":209},[100,33555,206],{"class":205},[100,33557,603],{"class":118},[100,33559,33560],{"class":102,"line":135},[100,33561,139],{"emptyLinePlaceholder":138},[100,33563,33564,33566,33568,33570,33572,33574,33576,33578,33580,33582,33584,33586],{"class":102,"line":142},[100,33565,71],{"class":145},[100,33567,29838],{"class":106},[100,33569,617],{"class":145},[100,33571,29843],{"class":114},[100,33573,170],{"class":118},[100,33575,17779],{"class":114},[100,33577,170],{"class":118},[100,33579,3146],{"class":178},[100,33581,6155],{"class":118},[100,33583,2895],{"class":110},[100,33585,6915],{"class":122},[100,33587,522],{"class":118},[100,33589,33590,33592,33594,33596,33598,33600],{"class":102,"line":152},[100,33591,200],{"class":114},[100,33593,170],{"class":118},[100,33595,3146],{"class":178},[100,33597,3166],{"class":118},[100,33599,29858],{"class":178},[100,33601,132],{"class":118},[14,33603,9750],{},[91,33605,33607],{"className":93,"code":33606,"language":95,"meta":96,"style":96},"items = [\"a\", \"b\", \"c\"]\n\nfor i in range(len(items)):\n    print(items[i])\n",[17,33608,33609,33641,33645,33666],{"__ignoreMap":96},[100,33610,33611,33613,33615,33617,33619,33621,33623,33625,33627,33629,33631,33633,33635,33637,33639],{"class":102,"line":103},[100,33612,3095],{"class":106},[100,33614,111],{"class":110},[100,33616,594],{"class":118},[100,33618,206],{"class":205},[100,33620,295],{"class":209},[100,33622,206],{"class":205},[100,33624,126],{"class":118},[100,33626,1708],{"class":205},[100,33628,4027],{"class":209},[100,33630,206],{"class":205},[100,33632,126],{"class":118},[100,33634,1708],{"class":205},[100,33636,4036],{"class":209},[100,33638,206],{"class":205},[100,33640,603],{"class":118},[100,33642,33643],{"class":102,"line":135},[100,33644,139],{"emptyLinePlaceholder":138},[100,33646,33647,33649,33651,33653,33655,33657,33659,33661,33663],{"class":102,"line":142},[100,33648,71],{"class":145},[100,33650,29838],{"class":106},[100,33652,617],{"class":145},[100,33654,29843],{"class":114},[100,33656,170],{"class":118},[100,33658,17779],{"class":114},[100,33660,170],{"class":118},[100,33662,3146],{"class":178},[100,33664,33665],{"class":118},")):\n",[100,33667,33668,33670,33672,33674,33676,33678],{"class":102,"line":152},[100,33669,200],{"class":114},[100,33671,170],{"class":118},[100,33673,3146],{"class":178},[100,33675,3166],{"class":118},[100,33677,29858],{"class":178},[100,33679,132],{"class":118},[14,33681,218],{},[91,33683,33684],{"className":93,"code":32938,"language":95,"meta":96,"style":96},[17,33685,33686,33690,33694],{"__ignoreMap":96},[100,33687,33688],{"class":102,"line":103},[100,33689,32945],{"class":106},[100,33691,33692],{"class":102,"line":135},[100,33693,32950],{"class":106},[100,33695,33696],{"class":102,"line":142},[100,33697,15565],{"class":106},[14,33699,33700,33701,290],{},"If you want a simpler way to loop, see ",[295,33702,8546],{"href":8545},[675,33704,33706],{"id":33705},"use-enumerate-when-you-need-both-index-and-value","Use enumerate() when you need both index and value",[14,33708,33709,33710,89],{},"Instead of managing indexes manually, use ",[295,33711,33713],{"href":33712},"\u002Freference\u002Fpython-enumerate-function-explained\u002F",[17,33714,1575],{},[91,33716,33718],{"className":93,"code":33717,"language":95,"meta":96,"style":96},"items = [\"a\", \"b\", \"c\"]\n\nfor i, value in enumerate(items):\n    print(i, value)\n",[17,33719,33720,33752,33756,33778],{"__ignoreMap":96},[100,33721,33722,33724,33726,33728,33730,33732,33734,33736,33738,33740,33742,33744,33746,33748,33750],{"class":102,"line":103},[100,33723,3095],{"class":106},[100,33725,111],{"class":110},[100,33727,594],{"class":118},[100,33729,206],{"class":205},[100,33731,295],{"class":209},[100,33733,206],{"class":205},[100,33735,126],{"class":118},[100,33737,1708],{"class":205},[100,33739,4027],{"class":209},[100,33741,206],{"class":205},[100,33743,126],{"class":118},[100,33745,1708],{"class":205},[100,33747,4036],{"class":209},[100,33749,206],{"class":205},[100,33751,603],{"class":118},[100,33753,33754],{"class":102,"line":135},[100,33755,139],{"emptyLinePlaceholder":138},[100,33757,33758,33760,33763,33765,33767,33769,33772,33774,33776],{"class":102,"line":142},[100,33759,71],{"class":145},[100,33761,33762],{"class":106}," i",[100,33764,126],{"class":118},[100,33766,989],{"class":106},[100,33768,617],{"class":145},[100,33770,33771],{"class":114}," enumerate",[100,33773,170],{"class":118},[100,33775,3146],{"class":178},[100,33777,522],{"class":118},[100,33779,33780,33782,33784,33786,33788,33790],{"class":102,"line":152},[100,33781,200],{"class":114},[100,33783,170],{"class":118},[100,33785,29858],{"class":178},[100,33787,126],{"class":118},[100,33789,6747],{"class":178},[100,33791,215],{"class":118},[14,33793,218],{},[91,33795,33797],{"className":93,"code":33796,"language":95,"meta":96,"style":96},"0 a\n1 b\n2 c\n",[17,33798,33799,33806,33813],{"__ignoreMap":96},[100,33800,33801,33803],{"class":102,"line":103},[100,33802,2279],{"class":122},[100,33804,33805],{"class":106}," a\n",[100,33807,33808,33810],{"class":102,"line":135},[100,33809,123],{"class":122},[100,33811,33812],{"class":106}," b\n",[100,33814,33815,33817],{"class":102,"line":142},[100,33816,559],{"class":122},[100,33818,33819],{"class":106}," c\n",[14,33821,33822],{},"This is often safer than writing your own index logic.",[675,33824,33826],{"id":33825},"handle-empty-sequences-before-reading-the-first-item","Handle empty sequences before reading the first item",[14,33828,33829],{},"This is a very common beginner mistake:",[91,33831,33833],{"className":93,"code":33832,"language":95,"meta":96,"style":96},"items = []\n\nprint(items[0])\n",[17,33834,33835,33844,33848],{"__ignoreMap":96},[100,33836,33837,33839,33841],{"class":102,"line":103},[100,33838,3095],{"class":106},[100,33840,111],{"class":110},[100,33842,33843],{"class":118}," []\n",[100,33845,33846],{"class":102,"line":135},[100,33847,139],{"emptyLinePlaceholder":138},[100,33849,33850,33852,33854,33856,33858,33860],{"class":102,"line":142},[100,33851,372],{"class":114},[100,33853,170],{"class":118},[100,33855,3146],{"class":178},[100,33857,3166],{"class":118},[100,33859,2279],{"class":122},[100,33861,132],{"class":118},[14,33863,33864],{},"Fix it by checking whether the sequence has any items:",[91,33866,33868],{"className":93,"code":33867,"language":95,"meta":96,"style":96},"items = []\n\nif items:\n    print(items[0])\nelse:\n    print(\"The list is empty\")\n",[17,33869,33870,33878,33882,33890,33904,33910],{"__ignoreMap":96},[100,33871,33872,33874,33876],{"class":102,"line":103},[100,33873,3095],{"class":106},[100,33875,111],{"class":110},[100,33877,33843],{"class":118},[100,33879,33880],{"class":102,"line":135},[100,33881,139],{"emptyLinePlaceholder":138},[100,33883,33884,33886,33888],{"class":102,"line":142},[100,33885,2736],{"class":145},[100,33887,12000],{"class":106},[100,33889,149],{"class":118},[100,33891,33892,33894,33896,33898,33900,33902],{"class":102,"line":152},[100,33893,200],{"class":114},[100,33895,170],{"class":118},[100,33897,3146],{"class":178},[100,33899,3166],{"class":118},[100,33901,2279],{"class":122},[100,33903,132],{"class":118},[100,33905,33906,33908],{"class":102,"line":164},[100,33907,4944],{"class":145},[100,33909,149],{"class":118},[100,33911,33912,33914,33916,33918,33921,33923],{"class":102,"line":185},[100,33913,200],{"class":114},[100,33915,170],{"class":118},[100,33917,206],{"class":205},[100,33919,33920],{"class":209},"The list is empty",[100,33922,206],{"class":205},[100,33924,215],{"class":118},[77,33926,33928],{"id":33927},"common-indexerror-situations","Common IndexError situations",[14,33930,33931],{},"Here are some patterns that often cause this error.",[675,33933,33935],{"id":33934},"accessing-the-first-item-of-an-empty-list","Accessing the first item of an empty list",[91,33937,33939],{"className":93,"code":33938,"language":95,"meta":96,"style":96},"numbers = []\nprint(numbers[0])\n",[17,33940,33941,33949],{"__ignoreMap":96},[100,33942,33943,33945,33947],{"class":102,"line":103},[100,33944,107],{"class":106},[100,33946,111],{"class":110},[100,33948,33843],{"class":118},[100,33950,33951,33953,33955,33957,33959,33961],{"class":102,"line":135},[100,33952,372],{"class":114},[100,33954,170],{"class":118},[100,33956,179],{"class":178},[100,33958,3166],{"class":118},[100,33960,2279],{"class":122},[100,33962,132],{"class":118},[14,33964,33965],{},"This fails because there is no first item.",[14,33967,33968],{},"Safer version:",[91,33970,33972],{"className":93,"code":33971,"language":95,"meta":96,"style":96},"numbers = []\n\nif numbers:\n    print(numbers[0])\nelse:\n    print(\"No items found\")\n",[17,33973,33974,33982,33986,33994,34008,34014],{"__ignoreMap":96},[100,33975,33976,33978,33980],{"class":102,"line":103},[100,33977,107],{"class":106},[100,33979,111],{"class":110},[100,33981,33843],{"class":118},[100,33983,33984],{"class":102,"line":135},[100,33985,139],{"emptyLinePlaceholder":138},[100,33987,33988,33990,33992],{"class":102,"line":142},[100,33989,2736],{"class":145},[100,33991,620],{"class":106},[100,33993,149],{"class":118},[100,33995,33996,33998,34000,34002,34004,34006],{"class":102,"line":152},[100,33997,200],{"class":114},[100,33999,170],{"class":118},[100,34001,179],{"class":178},[100,34003,3166],{"class":118},[100,34005,2279],{"class":122},[100,34007,132],{"class":118},[100,34009,34010,34012],{"class":102,"line":164},[100,34011,4944],{"class":145},[100,34013,149],{"class":118},[100,34015,34016,34018,34020,34022,34025,34027],{"class":102,"line":185},[100,34017,200],{"class":114},[100,34019,170],{"class":118},[100,34021,206],{"class":205},[100,34023,34024],{"class":209},"No items found",[100,34026,206],{"class":205},[100,34028,215],{"class":118},[675,34030,34032],{"id":34031},"using-i-1-inside-a-loop-without-checking-the-end","Using i + 1 inside a loop without checking the end",[91,34034,34036],{"className":93,"code":34035,"language":95,"meta":96,"style":96},"items = [\"a\", \"b\", \"c\"]\n\nfor i in range(len(items)):\n    print(items[i + 1])\n",[17,34037,34038,34070,34074,34094],{"__ignoreMap":96},[100,34039,34040,34042,34044,34046,34048,34050,34052,34054,34056,34058,34060,34062,34064,34066,34068],{"class":102,"line":103},[100,34041,3095],{"class":106},[100,34043,111],{"class":110},[100,34045,594],{"class":118},[100,34047,206],{"class":205},[100,34049,295],{"class":209},[100,34051,206],{"class":205},[100,34053,126],{"class":118},[100,34055,1708],{"class":205},[100,34057,4027],{"class":209},[100,34059,206],{"class":205},[100,34061,126],{"class":118},[100,34063,1708],{"class":205},[100,34065,4036],{"class":209},[100,34067,206],{"class":205},[100,34069,603],{"class":118},[100,34071,34072],{"class":102,"line":135},[100,34073,139],{"emptyLinePlaceholder":138},[100,34075,34076,34078,34080,34082,34084,34086,34088,34090,34092],{"class":102,"line":142},[100,34077,71],{"class":145},[100,34079,29838],{"class":106},[100,34081,617],{"class":145},[100,34083,29843],{"class":114},[100,34085,170],{"class":118},[100,34087,17779],{"class":114},[100,34089,170],{"class":118},[100,34091,3146],{"class":178},[100,34093,33665],{"class":118},[100,34095,34096,34098,34100,34102,34104,34107,34109,34111],{"class":102,"line":152},[100,34097,200],{"class":114},[100,34099,170],{"class":118},[100,34101,3146],{"class":178},[100,34103,3166],{"class":118},[100,34105,34106],{"class":178},"i ",[100,34108,6547],{"class":110},[100,34110,6915],{"class":122},[100,34112,132],{"class":118},[14,34114,34115,34116,34119,34120,290],{},"This fails on the last loop because ",[17,34117,34118],{},"i + 1"," becomes ",[17,34121,11541],{},[14,34123,33968],{},[91,34125,34127],{"className":93,"code":34126,"language":95,"meta":96,"style":96},"items = [\"a\", \"b\", \"c\"]\n\nfor i in range(len(items) - 1):\n    print(items[i + 1])\n",[17,34128,34129,34161,34165,34191],{"__ignoreMap":96},[100,34130,34131,34133,34135,34137,34139,34141,34143,34145,34147,34149,34151,34153,34155,34157,34159],{"class":102,"line":103},[100,34132,3095],{"class":106},[100,34134,111],{"class":110},[100,34136,594],{"class":118},[100,34138,206],{"class":205},[100,34140,295],{"class":209},[100,34142,206],{"class":205},[100,34144,126],{"class":118},[100,34146,1708],{"class":205},[100,34148,4027],{"class":209},[100,34150,206],{"class":205},[100,34152,126],{"class":118},[100,34154,1708],{"class":205},[100,34156,4036],{"class":209},[100,34158,206],{"class":205},[100,34160,603],{"class":118},[100,34162,34163],{"class":102,"line":135},[100,34164,139],{"emptyLinePlaceholder":138},[100,34166,34167,34169,34171,34173,34175,34177,34179,34181,34183,34185,34187,34189],{"class":102,"line":142},[100,34168,71],{"class":145},[100,34170,29838],{"class":106},[100,34172,617],{"class":145},[100,34174,29843],{"class":114},[100,34176,170],{"class":118},[100,34178,17779],{"class":114},[100,34180,170],{"class":118},[100,34182,3146],{"class":178},[100,34184,6155],{"class":118},[100,34186,1906],{"class":110},[100,34188,6915],{"class":122},[100,34190,522],{"class":118},[100,34192,34193,34195,34197,34199,34201,34203,34205,34207],{"class":102,"line":152},[100,34194,200],{"class":114},[100,34196,170],{"class":118},[100,34198,3146],{"class":178},[100,34200,3166],{"class":118},[100,34202,34106],{"class":178},[100,34204,6547],{"class":110},[100,34206,6915],{"class":122},[100,34208,132],{"class":118},[675,34210,34212],{"id":34211},"mixing-up-item-values-and-item-indexes","Mixing up item values and item indexes",[14,34214,34215],{},"Sometimes a value is treated like an index by mistake:",[91,34217,34219],{"className":93,"code":34218,"language":95,"meta":96,"style":96},"items = [\"apple\", \"banana\", \"cherry\"]\nindex = \"banana\"\n\nprint(items[index])\n",[17,34220,34221,34253,34265,34269],{"__ignoreMap":96},[100,34222,34223,34225,34227,34229,34231,34233,34235,34237,34239,34241,34243,34245,34247,34249,34251],{"class":102,"line":103},[100,34224,3095],{"class":106},[100,34226,111],{"class":110},[100,34228,594],{"class":118},[100,34230,206],{"class":205},[100,34232,4148],{"class":209},[100,34234,206],{"class":205},[100,34236,126],{"class":118},[100,34238,1708],{"class":205},[100,34240,4157],{"class":209},[100,34242,206],{"class":205},[100,34244,126],{"class":118},[100,34246,1708],{"class":205},[100,34248,8949],{"class":209},[100,34250,206],{"class":205},[100,34252,603],{"class":118},[100,34254,34255,34257,34259,34261,34263],{"class":102,"line":135},[100,34256,3116],{"class":106},[100,34258,111],{"class":110},[100,34260,1708],{"class":205},[100,34262,4157],{"class":209},[100,34264,1714],{"class":205},[100,34266,34267],{"class":102,"line":142},[100,34268,139],{"emptyLinePlaceholder":138},[100,34270,34271,34273,34275,34277,34279,34281],{"class":102,"line":152},[100,34272,372],{"class":114},[100,34274,170],{"class":118},[100,34276,3146],{"class":178},[100,34278,3166],{"class":118},[100,34280,3169],{"class":178},[100,34282,132],{"class":118},[14,34284,34285,34286,34288,34289,34291],{},"This is not an ",[17,34287,16420],{}," — it raises a ",[17,34290,3538],{}," because indexes must be integers.",[14,34293,34294,34295,290],{},"If you want to find where an item appears, do not use the value as an index directly. See ",[295,34296,34298],{"href":34297},"\u002Fhow-to\u002Fhow-to-find-an-item-in-a-list-in-python\u002F","how to find an item in a list in Python",[675,34300,34302],{"id":34301},"using-list-positions-after-pop-or-remove-changed-the-list","Using list positions after pop() or remove() changed the list",[91,34304,34306],{"className":93,"code":34305,"language":95,"meta":96,"style":96},"items = [\"a\", \"b\", \"c\"]\n\nitems.pop()\nprint(items[2])\n",[17,34307,34308,34340,34344,34355],{"__ignoreMap":96},[100,34309,34310,34312,34314,34316,34318,34320,34322,34324,34326,34328,34330,34332,34334,34336,34338],{"class":102,"line":103},[100,34311,3095],{"class":106},[100,34313,111],{"class":110},[100,34315,594],{"class":118},[100,34317,206],{"class":205},[100,34319,295],{"class":209},[100,34321,206],{"class":205},[100,34323,126],{"class":118},[100,34325,1708],{"class":205},[100,34327,4027],{"class":209},[100,34329,206],{"class":205},[100,34331,126],{"class":118},[100,34333,1708],{"class":205},[100,34335,4036],{"class":209},[100,34337,206],{"class":205},[100,34339,603],{"class":118},[100,34341,34342],{"class":102,"line":135},[100,34343,139],{"emptyLinePlaceholder":138},[100,34345,34346,34348,34350,34353],{"class":102,"line":142},[100,34347,3146],{"class":106},[100,34349,290],{"class":118},[100,34351,34352],{"class":178},"pop",[100,34354,4734],{"class":118},[100,34356,34357,34359,34361,34363,34365,34367],{"class":102,"line":152},[100,34358,372],{"class":114},[100,34360,170],{"class":118},[100,34362,3146],{"class":178},[100,34364,3166],{"class":118},[100,34366,559],{"class":122},[100,34368,132],{"class":118},[14,34370,28974,34371,34373,34374,34377,34378,34380],{},[17,34372,8104],{},", the list becomes ",[17,34375,34376],{},"[\"a\", \"b\"]",", so index ",[17,34379,559],{}," no longer exists.",[77,34382,34384],{"id":34383},"step-by-step-debugging-tips","Step-by-step debugging tips",[14,34386,34387,34388,34390],{},"If you are not sure why ",[17,34389,16420],{}," is happening, check these things.",[675,34392,34394],{"id":34393},"_1-print-the-sequence-and-its-length","1. Print the sequence and its length",[91,34396,34398],{"className":93,"code":34397,"language":95,"meta":96,"style":96},"items = [\"a\", \"b\", \"c\"]\nprint(items)\nprint(len(items))\n",[17,34399,34400,34432,34442],{"__ignoreMap":96},[100,34401,34402,34404,34406,34408,34410,34412,34414,34416,34418,34420,34422,34424,34426,34428,34430],{"class":102,"line":103},[100,34403,3095],{"class":106},[100,34405,111],{"class":110},[100,34407,594],{"class":118},[100,34409,206],{"class":205},[100,34411,295],{"class":209},[100,34413,206],{"class":205},[100,34415,126],{"class":118},[100,34417,1708],{"class":205},[100,34419,4027],{"class":209},[100,34421,206],{"class":205},[100,34423,126],{"class":118},[100,34425,1708],{"class":205},[100,34427,4036],{"class":209},[100,34429,206],{"class":205},[100,34431,603],{"class":118},[100,34433,34434,34436,34438,34440],{"class":102,"line":135},[100,34435,372],{"class":114},[100,34437,170],{"class":118},[100,34439,3146],{"class":178},[100,34441,215],{"class":118},[100,34443,34444,34446,34448,34450,34452,34454],{"class":102,"line":142},[100,34445,372],{"class":114},[100,34447,170],{"class":118},[100,34449,17779],{"class":114},[100,34451,170],{"class":118},[100,34453,3146],{"class":178},[100,34455,182],{"class":118},[675,34457,34459],{"id":34458},"_2-print-the-index-value-before-the-failing-line","2. Print the index value before the failing line",[91,34461,34463],{"className":93,"code":34462,"language":95,"meta":96,"style":96},"index = 3\nprint(index)\n",[17,34464,34465,34473],{"__ignoreMap":96},[100,34466,34467,34469,34471],{"class":102,"line":103},[100,34468,3116],{"class":106},[100,34470,111],{"class":110},[100,34472,31170],{"class":122},[100,34474,34475,34477,34479,34481],{"class":102,"line":135},[100,34476,372],{"class":114},[100,34478,170],{"class":118},[100,34480,3169],{"class":178},[100,34482,215],{"class":118},[675,34484,34486],{"id":34485},"_3-check-whether-the-sequence-is-empty","3. Check whether the sequence is empty",[91,34488,34490],{"className":93,"code":34489,"language":95,"meta":96,"style":96},"items = []\nprint(len(items) == 0)\n",[17,34491,34492,34500],{"__ignoreMap":96},[100,34493,34494,34496,34498],{"class":102,"line":103},[100,34495,3095],{"class":106},[100,34497,111],{"class":110},[100,34499,33843],{"class":118},[100,34501,34502,34504,34506,34508,34510,34512,34514,34517,34519],{"class":102,"line":135},[100,34503,372],{"class":114},[100,34505,170],{"class":118},[100,34507,17779],{"class":114},[100,34509,170],{"class":118},[100,34511,3146],{"class":178},[100,34513,6155],{"class":118},[100,34515,34516],{"class":110}," ==",[100,34518,1921],{"class":122},[100,34520,215],{"class":118},[675,34522,34524],{"id":34523},"_4-check-loop-start-and-stop-values","4. Check loop start and stop values",[14,34526,34527],{},"If you use a loop with indexes, confirm that it stops at the right point.",[91,34529,34531],{"className":93,"code":34530,"language":95,"meta":96,"style":96},"items = [\"a\", \"b\", \"c\"]\n\nfor i in range(len(items)):\n    print(i, items[i])\n",[17,34532,34533,34565,34569,34589],{"__ignoreMap":96},[100,34534,34535,34537,34539,34541,34543,34545,34547,34549,34551,34553,34555,34557,34559,34561,34563],{"class":102,"line":103},[100,34536,3095],{"class":106},[100,34538,111],{"class":110},[100,34540,594],{"class":118},[100,34542,206],{"class":205},[100,34544,295],{"class":209},[100,34546,206],{"class":205},[100,34548,126],{"class":118},[100,34550,1708],{"class":205},[100,34552,4027],{"class":209},[100,34554,206],{"class":205},[100,34556,126],{"class":118},[100,34558,1708],{"class":205},[100,34560,4036],{"class":209},[100,34562,206],{"class":205},[100,34564,603],{"class":118},[100,34566,34567],{"class":102,"line":135},[100,34568,139],{"emptyLinePlaceholder":138},[100,34570,34571,34573,34575,34577,34579,34581,34583,34585,34587],{"class":102,"line":142},[100,34572,71],{"class":145},[100,34574,29838],{"class":106},[100,34576,617],{"class":145},[100,34578,29843],{"class":114},[100,34580,170],{"class":118},[100,34582,17779],{"class":114},[100,34584,170],{"class":118},[100,34586,3146],{"class":178},[100,34588,33665],{"class":118},[100,34590,34591,34593,34595,34597,34599,34601,34603,34605],{"class":102,"line":152},[100,34592,200],{"class":114},[100,34594,170],{"class":118},[100,34596,29858],{"class":178},[100,34598,126],{"class":118},[100,34600,12000],{"class":178},[100,34602,3166],{"class":118},[100,34604,29858],{"class":178},[100,34606,132],{"class":118},[675,34608,34610],{"id":34609},"_5-read-the-traceback-carefully","5. Read the traceback carefully",[14,34612,34613],{},"The traceback shows the exact line where the error happened. Start there.",[14,34615,5295],{},[91,34617,34619],{"className":93,"code":34618,"language":95,"meta":96,"style":96},"print(items)\nprint(len(items))\nprint(index)\nprint(type(items))\n\nfor i, value in enumerate(items):\n    print(i, value)\n",[17,34620,34621,34631,34645,34655,34669,34673,34693],{"__ignoreMap":96},[100,34622,34623,34625,34627,34629],{"class":102,"line":103},[100,34624,372],{"class":114},[100,34626,170],{"class":118},[100,34628,3146],{"class":178},[100,34630,215],{"class":118},[100,34632,34633,34635,34637,34639,34641,34643],{"class":102,"line":135},[100,34634,372],{"class":114},[100,34636,170],{"class":118},[100,34638,17779],{"class":114},[100,34640,170],{"class":118},[100,34642,3146],{"class":178},[100,34644,182],{"class":118},[100,34646,34647,34649,34651,34653],{"class":102,"line":142},[100,34648,372],{"class":114},[100,34650,170],{"class":118},[100,34652,3169],{"class":178},[100,34654,215],{"class":118},[100,34656,34657,34659,34661,34663,34665,34667],{"class":102,"line":152},[100,34658,372],{"class":114},[100,34660,170],{"class":118},[100,34662,1250],{"class":191},[100,34664,170],{"class":118},[100,34666,3146],{"class":178},[100,34668,182],{"class":118},[100,34670,34671],{"class":102,"line":164},[100,34672,139],{"emptyLinePlaceholder":138},[100,34674,34675,34677,34679,34681,34683,34685,34687,34689,34691],{"class":102,"line":185},[100,34676,71],{"class":145},[100,34678,33762],{"class":106},[100,34680,126],{"class":118},[100,34682,989],{"class":106},[100,34684,617],{"class":145},[100,34686,33771],{"class":114},[100,34688,170],{"class":118},[100,34690,3146],{"class":178},[100,34692,522],{"class":118},[100,34694,34695,34697,34699,34701,34703,34705],{"class":102,"line":197},[100,34696,200],{"class":114},[100,34698,170],{"class":118},[100,34700,29858],{"class":178},[100,34702,126],{"class":118},[100,34704,6747],{"class":178},[100,34706,215],{"class":118},[14,34708,34709],{},"These can help you answer:",[40,34711,34712,34715,34718,34721],{},[43,34713,34714],{},"How many items are in the sequence?",[43,34716,34717],{},"What index is the code trying to use?",[43,34719,34720],{},"Is the sequence empty?",[43,34722,34723],{},"Is the object really a list, tuple, or string?",[77,34725,34727],{"id":34726},"indexerror-vs-related-errors","IndexError vs related errors",[14,34729,34730],{},"Some Python errors look similar, but they mean different things.",[675,34732,16420],{"id":34733},"indexerror",[14,34735,34736,34738],{},[17,34737,16420],{}," means the position does not exist in a sequence.",[14,34740,1844],{},[91,34742,34744],{"className":93,"code":34743,"language":95,"meta":96,"style":96},"items = [\"a\", \"b\"]\nprint(items[2])\n",[17,34745,34746,34770],{"__ignoreMap":96},[100,34747,34748,34750,34752,34754,34756,34758,34760,34762,34764,34766,34768],{"class":102,"line":103},[100,34749,3095],{"class":106},[100,34751,111],{"class":110},[100,34753,594],{"class":118},[100,34755,206],{"class":205},[100,34757,295],{"class":209},[100,34759,206],{"class":205},[100,34761,126],{"class":118},[100,34763,1708],{"class":205},[100,34765,4027],{"class":209},[100,34767,206],{"class":205},[100,34769,603],{"class":118},[100,34771,34772,34774,34776,34778,34780,34782],{"class":102,"line":135},[100,34773,372],{"class":114},[100,34775,170],{"class":118},[100,34777,3146],{"class":178},[100,34779,3166],{"class":118},[100,34781,559],{"class":122},[100,34783,132],{"class":118},[675,34785,17063],{"id":34786},"keyerror",[14,34788,34789,34791],{},[17,34790,17063],{}," happens when a dictionary key does not exist.",[14,34793,1844],{},[91,34795,34797],{"className":93,"code":34796,"language":95,"meta":96,"style":96},"data = {\"name\": \"Ana\"}\nprint(data[\"age\"])\n",[17,34798,34799,34824],{"__ignoreMap":96},[100,34800,34801,34803,34805,34807,34809,34811,34813,34815,34817,34820,34822],{"class":102,"line":103},[100,34802,7909],{"class":106},[100,34804,111],{"class":110},[100,34806,12080],{"class":118},[100,34808,206],{"class":205},[100,34810,2853],{"class":209},[100,34812,206],{"class":205},[100,34814,89],{"class":118},[100,34816,1708],{"class":205},[100,34818,34819],{"class":209},"Ana",[100,34821,206],{"class":205},[100,34823,12093],{"class":118},[100,34825,34826,34828,34830,34832,34834,34836,34838,34840],{"class":102,"line":135},[100,34827,372],{"class":114},[100,34829,170],{"class":118},[100,34831,7934],{"class":178},[100,34833,3166],{"class":118},[100,34835,206],{"class":205},[100,34837,2770],{"class":209},[100,34839,206],{"class":205},[100,34841,132],{"class":118},[14,34843,34844,34845,290],{},"If you want a direct comparison, see ",[295,34846,34847],{"href":17243},"KeyError vs IndexError in Python explained",[675,34849,3538],{"id":34850},"typeerror",[14,34852,34853,34855],{},[17,34854,3538],{}," can happen if you use the wrong type as an index.",[14,34857,1844],{},[91,34859,34861],{"className":93,"code":34860,"language":95,"meta":96,"style":96},"items = [\"a\", \"b\", \"c\"]\nprint(items[\"1\"])\n",[17,34862,34863,34895],{"__ignoreMap":96},[100,34864,34865,34867,34869,34871,34873,34875,34877,34879,34881,34883,34885,34887,34889,34891,34893],{"class":102,"line":103},[100,34866,3095],{"class":106},[100,34868,111],{"class":110},[100,34870,594],{"class":118},[100,34872,206],{"class":205},[100,34874,295],{"class":209},[100,34876,206],{"class":205},[100,34878,126],{"class":118},[100,34880,1708],{"class":205},[100,34882,4027],{"class":209},[100,34884,206],{"class":205},[100,34886,126],{"class":118},[100,34888,1708],{"class":205},[100,34890,4036],{"class":209},[100,34892,206],{"class":205},[100,34894,603],{"class":118},[100,34896,34897,34899,34901,34903,34905,34907,34909,34911],{"class":102,"line":135},[100,34898,372],{"class":114},[100,34900,170],{"class":118},[100,34902,3146],{"class":178},[100,34904,3166],{"class":118},[100,34906,206],{"class":205},[100,34908,123],{"class":209},[100,34910,206],{"class":205},[100,34912,132],{"class":118},[14,34914,7194,34915,34918],{},[17,34916,34917],{},"\"1\""," is a string, not an integer.",[77,34920,1514],{"id":1513},[675,34922,34924],{"id":34923},"what-causes-indexerror-in-python","What causes IndexError in Python?",[14,34926,34927],{},"It happens when your code uses an index that is outside the valid range for a list, tuple, or string.",[675,34929,34931],{"id":34930},"what-is-a-valid-list-index","What is a valid list index?",[14,34933,34934,34935,5870,34937,5629,34939,290],{},"For a list with length 3, valid indexes are ",[17,34936,2279],{},[17,34938,123],{},[17,34940,559],{},[675,34942,34944],{"id":34943},"can-strings-raise-indexerror-too","Can strings raise IndexError too?",[14,34946,34947,34948,290],{},"Yes. Strings also use indexes, so accessing a position that does not exist raises ",[17,34949,16420],{},[675,34951,34953],{"id":34952},"how-do-i-avoid-indexerror-in-a-loop","How do I avoid IndexError in a loop?",[14,34955,34956,34957,34959],{},"Use correct ",[17,34958,32990],{}," limits, or loop directly over the items instead of hard-coding indexes.",[675,34961,34963],{"id":34962},"is-indexerror-the-same-as-keyerror","Is IndexError the same as KeyError?",[14,34965,14506,34966,34968,34969,34971],{},[17,34967,16420],{}," is for invalid positions in sequences. ",[17,34970,17063],{}," is for missing dictionary keys.",[77,34973,1554],{"id":1553},[40,34975,34976,34980,34986,34991,34996,35001,35005],{},[43,34977,34978],{},[295,34979,33200],{"href":17231},[43,34981,34982],{},[295,34983,34985],{"href":34984},"\u002Ferrors\u002Findexerror-tuple-index-out-of-range-fix\u002F","IndexError: tuple index out of range fix",[43,34987,34988],{},[295,34989,34990],{"href":32800},"Python len() function explained",[43,34992,34993],{},[295,34994,34995],{"href":32987},"Python range() function explained",[43,34997,34998],{},[295,34999,35000],{"href":33712},"Python enumerate() function explained",[43,35002,35003],{},[295,35004,9186],{"href":8545},[43,35006,35007],{},[295,35008,35009],{"href":8110},"Python lists explained: beginner guide",[1589,35011,35012],{},"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 .sP7_E, html code.shiki .sP7_E{--shiki-light:#39ADB5;--shiki-default:#24292E;--shiki-dark:#E1E4E8}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 .srdBf, html code.shiki .srdBf{--shiki-light:#F76D47;--shiki-default:#005CC5;--shiki-dark:#79B8FF}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 .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 .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 .sZMiF, html code.shiki .sZMiF{--shiki-light:#E2931D;--shiki-default:#005CC5;--shiki-dark:#79B8FF}",{"title":96,"searchDepth":135,"depth":135,"links":35014},[35015,35016,35017,35018,35019,35026,35032,35039,35044,35051],{"id":79,"depth":135,"text":80},{"id":32807,"depth":135,"text":32808},{"id":18289,"depth":135,"text":18290},{"id":33034,"depth":135,"text":33035},{"id":33248,"depth":135,"text":33249,"children":35020},[35021,35022,35023,35024,35025],{"id":33255,"depth":142,"text":33256},{"id":33366,"depth":142,"text":33367},{"id":33508,"depth":142,"text":33509},{"id":33705,"depth":142,"text":33706},{"id":33825,"depth":142,"text":33826},{"id":33927,"depth":135,"text":33928,"children":35027},[35028,35029,35030,35031],{"id":33934,"depth":142,"text":33935},{"id":34031,"depth":142,"text":34032},{"id":34211,"depth":142,"text":34212},{"id":34301,"depth":142,"text":34302},{"id":34383,"depth":135,"text":34384,"children":35033},[35034,35035,35036,35037,35038],{"id":34393,"depth":142,"text":34394},{"id":34458,"depth":142,"text":34459},{"id":34485,"depth":142,"text":34486},{"id":34523,"depth":142,"text":34524},{"id":34609,"depth":142,"text":34610},{"id":34726,"depth":135,"text":34727,"children":35040},[35041,35042,35043],{"id":34733,"depth":142,"text":16420},{"id":34786,"depth":142,"text":17063},{"id":34850,"depth":142,"text":3538},{"id":1513,"depth":135,"text":1514,"children":35045},[35046,35047,35048,35049,35050],{"id":34923,"depth":142,"text":34924},{"id":34930,"depth":142,"text":34931},{"id":34943,"depth":142,"text":34944},{"id":34952,"depth":142,"text":34953},{"id":34962,"depth":142,"text":34963},{"id":1553,"depth":135,"text":1554},"Master indexerror in python causes and fixes in our comprehensive Python beginner guide.",{},"\u002Ferrors\u002Findexerror-in-python-causes-and-fixes",{"title":32620,"description":35052},"errors\u002Findexerror-in-python-causes-and-fixes","O_zznq29dvEUBefEs7zTNpjNq2vRdqxGIwON_VqYRbA",{"id":35059,"title":35060,"body":35061,"description":37737,"extension":1623,"meta":37738,"navigation":138,"path":37739,"seo":37740,"stem":37741,"__hash__":37742},"content\u002Ferrors\u002Findexerror-list-index-out-of-range-fix-explained.md","IndexError: list index out of range (Fix Explained)",{"type":7,"value":35062,"toc":37680},[35063,35066,35071,35073,35087,35096,35098,35200,35204,35223,35227,35235,35240,35242,35245,35248,35289,35292,35315,35320,35324,35329,35351,35354,35360,35362,35365,35416,35419,35435,35439,35442,35456,35462,35474,35476,35479,35482,35520,35525,35529,35532,35544,35547,35588,35593,35599,35602,35682,35685,35703,35708,35715,35719,35746,35749,35753,35759,35816,35821,35823,35826,35833,35935,35938,35942,35945,36023,36027,36049,36054,36058,36061,36124,36127,36132,36139,36197,36207,36211,36214,36383,36386,36390,36393,36397,36460,36466,36560,36566,36570,36659,36663,36742,36745,36747,36750,36754,36769,36772,36776,36795,36801,36805,36820,36823,36827,36856,36861,36865,36871,36874,36919,36922,36961,36969,36973,36976,36980,37042,37045,37050,37124,37128,37150,37153,37157,37222,37226,37232,37287,37291,37324,37327,37329,37332,37361,37368,37380,37383,37439,37450,37452,37456,37468,37472,37480,37484,37487,37526,37530,37533,37535,37628,37631,37633,37677],[10,35064,35060],{"id":35065},"indexerror-list-index-out-of-range-fix-explained",[14,35067,35068,35070],{},[17,35069,17232],{}," means your code is trying to read a list position that does not exist.",[14,35072,30678],{},[40,35074,35075,35078,35081,35084],{},[43,35076,35077],{},"the index is too large",[43,35079,35080],{},"the list is empty",[43,35082,35083],{},"a loop goes one step too far",[43,35085,35086],{},"you expect more items than are really in the list",[14,35088,35089,35090,35095],{},"The good news is that this error is usually easy to fix. In most cases, you need to check the list length with ",[295,35091,35093],{"href":35092},"\u002Freference\u002Fpython-list-length-len\u002F",[17,35094,32803],{}," or use a safer looping pattern.",[77,35097,80],{"id":79},[91,35099,35100],{"className":93,"code":32684,"language":95,"meta":96,"style":96},[17,35101,35102,35134,35142,35146,35166,35180,35186],{"__ignoreMap":96},[100,35103,35104,35106,35108,35110,35112,35114,35116,35118,35120,35122,35124,35126,35128,35130,35132],{"class":102,"line":103},[100,35105,3095],{"class":106},[100,35107,111],{"class":110},[100,35109,594],{"class":118},[100,35111,206],{"class":205},[100,35113,295],{"class":209},[100,35115,206],{"class":205},[100,35117,126],{"class":118},[100,35119,1708],{"class":205},[100,35121,4027],{"class":209},[100,35123,206],{"class":205},[100,35125,126],{"class":118},[100,35127,1708],{"class":205},[100,35129,4036],{"class":209},[100,35131,206],{"class":205},[100,35133,603],{"class":118},[100,35135,35136,35138,35140],{"class":102,"line":135},[100,35137,3116],{"class":106},[100,35139,111],{"class":110},[100,35141,3564],{"class":122},[100,35143,35144],{"class":102,"line":142},[100,35145,139],{"emptyLinePlaceholder":138},[100,35147,35148,35150,35152,35154,35156,35158,35160,35162,35164],{"class":102,"line":152},[100,35149,2736],{"class":145},[100,35151,1921],{"class":122},[100,35153,3133],{"class":110},[100,35155,3136],{"class":106},[100,35157,2782],{"class":110},[100,35159,3141],{"class":114},[100,35161,170],{"class":118},[100,35163,3146],{"class":178},[100,35165,522],{"class":118},[100,35167,35168,35170,35172,35174,35176,35178],{"class":102,"line":164},[100,35169,200],{"class":114},[100,35171,170],{"class":118},[100,35173,3146],{"class":178},[100,35175,3166],{"class":118},[100,35177,3169],{"class":178},[100,35179,132],{"class":118},[100,35181,35182,35184],{"class":102,"line":185},[100,35183,4944],{"class":145},[100,35185,149],{"class":118},[100,35187,35188,35190,35192,35194,35196,35198],{"class":102,"line":197},[100,35189,200],{"class":114},[100,35191,170],{"class":118},[100,35193,206],{"class":205},[100,35195,32781],{"class":209},[100,35197,206],{"class":205},[100,35199,215],{"class":118},[14,35201,35202],{},[22,35203,26197],{},[40,35205,35206,35211,35217,35220],{},[43,35207,35208,35210],{},[17,35209,33373],{}," gets the number of items in the list",[43,35212,35213,35216],{},[17,35214,35215],{},"0 \u003C= index \u003C len(items)"," checks whether the index is valid",[43,35218,35219],{},"If the index is valid, Python reads the item",[43,35221,35222],{},"If not, the program avoids the error",[14,35224,35225],{},[22,35226,218],{},[91,35228,35229],{"className":93,"code":15565,"language":95,"meta":96,"style":96},[17,35230,35231],{"__ignoreMap":96},[100,35232,35233],{"class":102,"line":103},[100,35234,15565],{"class":106},[14,35236,241,35237,35239],{},[17,35238,32803],{}," to make sure the index exists before reading from the list.",[77,35241,5881],{"id":5880},[14,35243,35244],{},"A list index is a position in a list.",[14,35246,35247],{},"For example, in this list:",[91,35249,35251],{"className":93,"code":35250,"language":95,"meta":96,"style":96},"colors = [\"red\", \"green\", \"blue\"]\n",[17,35252,35253],{"__ignoreMap":96},[100,35254,35255,35258,35260,35262,35264,35267,35269,35271,35273,35276,35278,35280,35282,35285,35287],{"class":102,"line":103},[100,35256,35257],{"class":106},"colors ",[100,35259,111],{"class":110},[100,35261,594],{"class":118},[100,35263,206],{"class":205},[100,35265,35266],{"class":209},"red",[100,35268,206],{"class":205},[100,35270,126],{"class":118},[100,35272,1708],{"class":205},[100,35274,35275],{"class":209},"green",[100,35277,206],{"class":205},[100,35279,126],{"class":118},[100,35281,1708],{"class":205},[100,35283,35284],{"class":209},"blue",[100,35286,206],{"class":205},[100,35288,603],{"class":118},[14,35290,35291],{},"The indexes are:",[40,35293,35294,35301,35308],{},[43,35295,35296,23456,35298],{},[17,35297,2279],{},[17,35299,35300],{},"\"red\"",[43,35302,35303,23456,35305],{},[17,35304,123],{},[17,35306,35307],{},"\"green\"",[43,35309,35310,23456,35312],{},[17,35311,559],{},[17,35313,35314],{},"\"blue\"",[14,35316,3860,35317,35319],{},[17,35318,17232],{}," when you ask for a position that is not in the list.",[675,35321,35323],{"id":35322},"valid-list-indexes","Valid list indexes",[14,35325,35326,35327,89],{},"For a list with length ",[17,35328,11541],{},[40,35330,35331,35340],{},[43,35332,35333,35334,5870,35336,5629,35338],{},"valid positive indexes are ",[17,35335,2279],{},[17,35337,123],{},[17,35339,559],{},[43,35341,35342,35343,5870,35346,5629,35349],{},"valid negative indexes are ",[17,35344,35345],{},"-1",[17,35347,35348],{},"-2",[17,35350,5897],{},[14,35352,35353],{},"Anything outside that range will fail.",[14,35355,35356,35357,290],{},"If you need a refresher on list basics, see ",[295,35358,35359],{"href":8110},"Python lists explained for beginners",[77,35361,5945],{"id":5944},[14,35363,35364],{},"Here is a short list with 3 items:",[91,35366,35368],{"className":93,"code":35367,"language":95,"meta":96,"style":96},"items = [\"a\", \"b\", \"c\"]\nprint(items[3])\n",[17,35369,35370,35402],{"__ignoreMap":96},[100,35371,35372,35374,35376,35378,35380,35382,35384,35386,35388,35390,35392,35394,35396,35398,35400],{"class":102,"line":103},[100,35373,3095],{"class":106},[100,35375,111],{"class":110},[100,35377,594],{"class":118},[100,35379,206],{"class":205},[100,35381,295],{"class":209},[100,35383,206],{"class":205},[100,35385,126],{"class":118},[100,35387,1708],{"class":205},[100,35389,4027],{"class":209},[100,35391,206],{"class":205},[100,35393,126],{"class":118},[100,35395,1708],{"class":205},[100,35397,4036],{"class":209},[100,35399,206],{"class":205},[100,35401,603],{"class":118},[100,35403,35404,35406,35408,35410,35412,35414],{"class":102,"line":135},[100,35405,372],{"class":114},[100,35407,170],{"class":118},[100,35409,3146],{"class":178},[100,35411,3166],{"class":118},[100,35413,11541],{"class":122},[100,35415,132],{"class":118},[14,35417,35418],{},"This will raise:",[91,35420,35421],{"className":93,"code":17122,"language":95,"meta":96,"style":96},[17,35422,35423],{"__ignoreMap":96},[100,35424,35425,35427,35429,35431,35433],{"class":102,"line":103},[100,35426,16420],{"class":191},[100,35428,89],{"class":118},[100,35430,14704],{"class":191},[100,35432,16427],{"class":106},[100,35434,16430],{"class":114},[675,35436,35438],{"id":35437},"why-this-fails","Why this fails",[14,35440,35441],{},"The list has 3 items, but its valid indexes are:",[40,35443,35444,35448,35452],{},[43,35445,35446],{},[17,35447,2279],{},[43,35449,35450],{},[17,35451,123],{},[43,35453,35454],{},[17,35455,559],{},[14,35457,35458,35459,33124],{},"So ",[17,35460,35461],{},"items[3]",[14,35463,35464,35465,5870,35467,5629,35469,35471,35472,290],{},"A common beginner mistake is thinking that a 3-item list has indexes ",[17,35466,123],{},[17,35468,559],{},[17,35470,11541],{},". In Python, list indexes start at ",[17,35473,2279],{},[77,35475,25091],{"id":25090},[14,35477,35478],{},"This error can happen in several common situations.",[675,35480,32978],{"id":35481},"using-an-index-that-is-too-large",[91,35483,35484],{"className":93,"code":16321,"language":95,"meta":96,"style":96},[17,35485,35486,35506],{"__ignoreMap":96},[100,35487,35488,35490,35492,35494,35496,35498,35500,35502,35504],{"class":102,"line":103},[100,35489,107],{"class":106},[100,35491,111],{"class":110},[100,35493,594],{"class":118},[100,35495,356],{"class":122},[100,35497,126],{"class":118},[100,35499,361],{"class":122},[100,35501,126],{"class":118},[100,35503,16342],{"class":122},[100,35505,603],{"class":118},[100,35507,35508,35510,35512,35514,35516,35518],{"class":102,"line":135},[100,35509,372],{"class":114},[100,35511,170],{"class":118},[100,35513,179],{"class":178},[100,35515,3166],{"class":118},[100,35517,3557],{"class":122},[100,35519,132],{"class":118},[14,35521,33121,35522,35524],{},[17,35523,3557],{}," is outside the list.",[675,35526,35528],{"id":35527},"using-a-negative-index-that-goes-too-far","Using a negative index that goes too far",[14,35530,35531],{},"Negative indexes count from the end:",[40,35533,35534,35539],{},[43,35535,35536,35538],{},[17,35537,35345],{}," is the last item",[43,35540,35541,35543],{},[17,35542,35348],{}," is the second-to-last item",[14,35545,35546],{},"But this still has limits:",[91,35548,35550],{"className":93,"code":35549,"language":95,"meta":96,"style":96},"numbers = [10, 20, 30]\nprint(numbers[-4])\n",[17,35551,35552,35572],{"__ignoreMap":96},[100,35553,35554,35556,35558,35560,35562,35564,35566,35568,35570],{"class":102,"line":103},[100,35555,107],{"class":106},[100,35557,111],{"class":110},[100,35559,594],{"class":118},[100,35561,356],{"class":122},[100,35563,126],{"class":118},[100,35565,361],{"class":122},[100,35567,126],{"class":118},[100,35569,16342],{"class":122},[100,35571,603],{"class":118},[100,35573,35574,35576,35578,35580,35582,35584,35586],{"class":102,"line":135},[100,35575,372],{"class":114},[100,35577,170],{"class":118},[100,35579,179],{"class":178},[100,35581,3166],{"class":118},[100,35583,2193],{"class":110},[100,35585,8322],{"class":122},[100,35587,132],{"class":118},[14,35589,35590,35591,290],{},"This fails because the list only supports negative indexes down to ",[17,35592,5897],{},[675,35594,32984,35596,35598],{"id":35595},"looping-with-the-wrong-range-stop-value",[17,35597,32990],{}," stop value",[14,35600,35601],{},"This is very common:",[91,35603,35604],{"className":93,"code":33523,"language":95,"meta":96,"style":96},[17,35605,35606,35638,35642,35668],{"__ignoreMap":96},[100,35607,35608,35610,35612,35614,35616,35618,35620,35622,35624,35626,35628,35630,35632,35634,35636],{"class":102,"line":103},[100,35609,3095],{"class":106},[100,35611,111],{"class":110},[100,35613,594],{"class":118},[100,35615,206],{"class":205},[100,35617,295],{"class":209},[100,35619,206],{"class":205},[100,35621,126],{"class":118},[100,35623,1708],{"class":205},[100,35625,4027],{"class":209},[100,35627,206],{"class":205},[100,35629,126],{"class":118},[100,35631,1708],{"class":205},[100,35633,4036],{"class":209},[100,35635,206],{"class":205},[100,35637,603],{"class":118},[100,35639,35640],{"class":102,"line":135},[100,35641,139],{"emptyLinePlaceholder":138},[100,35643,35644,35646,35648,35650,35652,35654,35656,35658,35660,35662,35664,35666],{"class":102,"line":142},[100,35645,71],{"class":145},[100,35647,29838],{"class":106},[100,35649,617],{"class":145},[100,35651,29843],{"class":114},[100,35653,170],{"class":118},[100,35655,17779],{"class":114},[100,35657,170],{"class":118},[100,35659,3146],{"class":178},[100,35661,6155],{"class":118},[100,35663,2895],{"class":110},[100,35665,6915],{"class":122},[100,35667,522],{"class":118},[100,35669,35670,35672,35674,35676,35678,35680],{"class":102,"line":152},[100,35671,200],{"class":114},[100,35673,170],{"class":118},[100,35675,3146],{"class":178},[100,35677,3166],{"class":118},[100,35679,29858],{"class":178},[100,35681,132],{"class":118},[14,35683,35684],{},"This loop goes through:",[40,35686,35687,35691,35695,35699],{},[43,35688,35689],{},[17,35690,2279],{},[43,35692,35693],{},[17,35694,123],{},[43,35696,35697],{},[17,35698,559],{},[43,35700,35701],{},[17,35702,11541],{},[14,35704,33121,35705,35707],{},[17,35706,11541],{}," does not exist, so the loop crashes at the end.",[14,35709,35710,35711,290],{},"If you want to understand loop boundaries better, see ",[295,35712,14882,35713,1576],{"href":32987},[17,35714,32990],{},[675,35716,35718],{"id":35717},"accessing-a-list-before-checking-that-it-has-items","Accessing a list before checking that it has items",[91,35720,35722],{"className":93,"code":35721,"language":95,"meta":96,"style":96},"items = []\nprint(items[0])\n",[17,35723,35724,35732],{"__ignoreMap":96},[100,35725,35726,35728,35730],{"class":102,"line":103},[100,35727,3095],{"class":106},[100,35729,111],{"class":110},[100,35731,33843],{"class":118},[100,35733,35734,35736,35738,35740,35742,35744],{"class":102,"line":135},[100,35735,372],{"class":114},[100,35737,170],{"class":118},[100,35739,3146],{"class":178},[100,35741,3166],{"class":118},[100,35743,2279],{"class":122},[100,35745,132],{"class":118},[14,35747,35748],{},"This fails because the list is empty.",[675,35750,35752],{"id":35751},"assuming-input-always-has-enough-items","Assuming input always has enough items",[14,35754,35755,35756,35758],{},"Sometimes the list comes from user input, ",[17,35757,8090],{},", a file, or an API.",[91,35760,35762],{"className":93,"code":35761,"language":95,"meta":96,"style":96},"text = \"Alice\"\nparts = text.split(\",\")\n\nprint(parts[1])\n",[17,35763,35764,35776,35798,35802],{"__ignoreMap":96},[100,35765,35766,35768,35770,35772,35774],{"class":102,"line":103},[100,35767,4331],{"class":106},[100,35769,111],{"class":110},[100,35771,1708],{"class":205},[100,35773,15343],{"class":209},[100,35775,1714],{"class":205},[100,35777,35778,35780,35782,35784,35786,35788,35790,35792,35794,35796],{"class":102,"line":135},[100,35779,4345],{"class":106},[100,35781,111],{"class":110},[100,35783,4350],{"class":106},[100,35785,290],{"class":118},[100,35787,3922],{"class":178},[100,35789,170],{"class":118},[100,35791,206],{"class":205},[100,35793,126],{"class":209},[100,35795,206],{"class":205},[100,35797,215],{"class":118},[100,35799,35800],{"class":102,"line":142},[100,35801,139],{"emptyLinePlaceholder":138},[100,35803,35804,35806,35808,35810,35812,35814],{"class":102,"line":152},[100,35805,372],{"class":114},[100,35807,170],{"class":118},[100,35809,4373],{"class":178},[100,35811,3166],{"class":118},[100,35813,123],{"class":122},[100,35815,132],{"class":118},[14,35817,7194,35818,35820],{},[17,35819,4373],{}," only has one item.",[77,35822,6244],{"id":6243},[14,35824,35825],{},"There are several safe ways to fix this error.",[675,35827,35829,35830,35832],{"id":35828},"_1-check-the-list-length-with-len-before-indexing","1. Check the list length with ",[17,35831,32803],{}," before indexing",[91,35834,35835],{"className":93,"code":33262,"language":95,"meta":96,"style":96},[17,35836,35837,35869,35877,35881,35901,35915,35921],{"__ignoreMap":96},[100,35838,35839,35841,35843,35845,35847,35849,35851,35853,35855,35857,35859,35861,35863,35865,35867],{"class":102,"line":103},[100,35840,3095],{"class":106},[100,35842,111],{"class":110},[100,35844,594],{"class":118},[100,35846,206],{"class":205},[100,35848,295],{"class":209},[100,35850,206],{"class":205},[100,35852,126],{"class":118},[100,35854,1708],{"class":205},[100,35856,4027],{"class":209},[100,35858,206],{"class":205},[100,35860,126],{"class":118},[100,35862,1708],{"class":205},[100,35864,4036],{"class":209},[100,35866,206],{"class":205},[100,35868,603],{"class":118},[100,35870,35871,35873,35875],{"class":102,"line":135},[100,35872,3116],{"class":106},[100,35874,111],{"class":110},[100,35876,1093],{"class":122},[100,35878,35879],{"class":102,"line":142},[100,35880,139],{"emptyLinePlaceholder":138},[100,35882,35883,35885,35887,35889,35891,35893,35895,35897,35899],{"class":102,"line":152},[100,35884,2736],{"class":145},[100,35886,1921],{"class":122},[100,35888,3133],{"class":110},[100,35890,3136],{"class":106},[100,35892,2782],{"class":110},[100,35894,3141],{"class":114},[100,35896,170],{"class":118},[100,35898,3146],{"class":178},[100,35900,522],{"class":118},[100,35902,35903,35905,35907,35909,35911,35913],{"class":102,"line":164},[100,35904,200],{"class":114},[100,35906,170],{"class":118},[100,35908,3146],{"class":178},[100,35910,3166],{"class":118},[100,35912,3169],{"class":178},[100,35914,132],{"class":118},[100,35916,35917,35919],{"class":102,"line":185},[100,35918,4944],{"class":145},[100,35920,149],{"class":118},[100,35922,35923,35925,35927,35929,35931,35933],{"class":102,"line":197},[100,35924,200],{"class":114},[100,35926,170],{"class":118},[100,35928,206],{"class":205},[100,35930,33359],{"class":209},[100,35932,206],{"class":205},[100,35934,215],{"class":118},[14,35936,35937],{},"This is one of the clearest fixes for beginners.",[675,35939,35941],{"id":35940},"_2-use-the-correct-loop-range","2. Use the correct loop range",[14,35943,35944],{},"If you really need indexes, use:",[91,35946,35947],{"className":93,"code":34530,"language":95,"meta":96,"style":96},[17,35948,35949,35981,35985,36005],{"__ignoreMap":96},[100,35950,35951,35953,35955,35957,35959,35961,35963,35965,35967,35969,35971,35973,35975,35977,35979],{"class":102,"line":103},[100,35952,3095],{"class":106},[100,35954,111],{"class":110},[100,35956,594],{"class":118},[100,35958,206],{"class":205},[100,35960,295],{"class":209},[100,35962,206],{"class":205},[100,35964,126],{"class":118},[100,35966,1708],{"class":205},[100,35968,4027],{"class":209},[100,35970,206],{"class":205},[100,35972,126],{"class":118},[100,35974,1708],{"class":205},[100,35976,4036],{"class":209},[100,35978,206],{"class":205},[100,35980,603],{"class":118},[100,35982,35983],{"class":102,"line":135},[100,35984,139],{"emptyLinePlaceholder":138},[100,35986,35987,35989,35991,35993,35995,35997,35999,36001,36003],{"class":102,"line":142},[100,35988,71],{"class":145},[100,35990,29838],{"class":106},[100,35992,617],{"class":145},[100,35994,29843],{"class":114},[100,35996,170],{"class":118},[100,35998,17779],{"class":114},[100,36000,170],{"class":118},[100,36002,3146],{"class":178},[100,36004,33665],{"class":118},[100,36006,36007,36009,36011,36013,36015,36017,36019,36021],{"class":102,"line":152},[100,36008,200],{"class":114},[100,36010,170],{"class":118},[100,36012,29858],{"class":178},[100,36014,126],{"class":118},[100,36016,12000],{"class":178},[100,36018,3166],{"class":118},[100,36020,29858],{"class":178},[100,36022,132],{"class":118},[14,36024,36025],{},[22,36026,218],{},[91,36028,36029],{"className":93,"code":33796,"language":95,"meta":96,"style":96},[17,36030,36031,36037,36043],{"__ignoreMap":96},[100,36032,36033,36035],{"class":102,"line":103},[100,36034,2279],{"class":122},[100,36036,33805],{"class":106},[100,36038,36039,36041],{"class":102,"line":135},[100,36040,123],{"class":122},[100,36042,33812],{"class":106},[100,36044,36045,36047],{"class":102,"line":142},[100,36046,559],{"class":122},[100,36048,33819],{"class":106},[14,36050,36051,36052,290],{},"Notice that the loop stops before ",[17,36053,33373],{},[675,36055,36057],{"id":36056},"_3-prefer-direct-iteration-when-possible","3. Prefer direct iteration when possible",[14,36059,36060],{},"In many cases, you do not need indexes at all.",[91,36062,36064],{"className":93,"code":36063,"language":95,"meta":96,"style":96},"items = [\"a\", \"b\", \"c\"]\n\nfor item in items:\n    print(item)\n",[17,36065,36066,36098,36102,36114],{"__ignoreMap":96},[100,36067,36068,36070,36072,36074,36076,36078,36080,36082,36084,36086,36088,36090,36092,36094,36096],{"class":102,"line":103},[100,36069,3095],{"class":106},[100,36071,111],{"class":110},[100,36073,594],{"class":118},[100,36075,206],{"class":205},[100,36077,295],{"class":209},[100,36079,206],{"class":205},[100,36081,126],{"class":118},[100,36083,1708],{"class":205},[100,36085,4027],{"class":209},[100,36087,206],{"class":205},[100,36089,126],{"class":118},[100,36091,1708],{"class":205},[100,36093,4036],{"class":209},[100,36095,206],{"class":205},[100,36097,603],{"class":118},[100,36099,36100],{"class":102,"line":135},[100,36101,139],{"emptyLinePlaceholder":138},[100,36103,36104,36106,36108,36110,36112],{"class":102,"line":142},[100,36105,71],{"class":145},[100,36107,7568],{"class":106},[100,36109,617],{"class":145},[100,36111,12000],{"class":106},[100,36113,149],{"class":118},[100,36115,36116,36118,36120,36122],{"class":102,"line":152},[100,36117,200],{"class":114},[100,36119,170],{"class":118},[100,36121,7555],{"class":178},[100,36123,215],{"class":118},[14,36125,36126],{},"This is often safer and simpler.",[14,36128,36129,36130,290],{},"For more help, see ",[295,36131,8546],{"href":8545},[675,36133,36135,36136],{"id":36134},"_4-check-for-an-empty-list-before-using-my_list0","4. Check for an empty list before using ",[17,36137,36138],{},"my_list[0]",[91,36140,36141],{"className":93,"code":33867,"language":95,"meta":96,"style":96},[17,36142,36143,36151,36155,36163,36177,36183],{"__ignoreMap":96},[100,36144,36145,36147,36149],{"class":102,"line":103},[100,36146,3095],{"class":106},[100,36148,111],{"class":110},[100,36150,33843],{"class":118},[100,36152,36153],{"class":102,"line":135},[100,36154,139],{"emptyLinePlaceholder":138},[100,36156,36157,36159,36161],{"class":102,"line":142},[100,36158,2736],{"class":145},[100,36160,12000],{"class":106},[100,36162,149],{"class":118},[100,36164,36165,36167,36169,36171,36173,36175],{"class":102,"line":152},[100,36166,200],{"class":114},[100,36168,170],{"class":118},[100,36170,3146],{"class":178},[100,36172,3166],{"class":118},[100,36174,2279],{"class":122},[100,36176,132],{"class":118},[100,36178,36179,36181],{"class":102,"line":164},[100,36180,4944],{"class":145},[100,36182,149],{"class":118},[100,36184,36185,36187,36189,36191,36193,36195],{"class":102,"line":185},[100,36186,200],{"class":114},[100,36188,170],{"class":118},[100,36190,206],{"class":205},[100,36192,33920],{"class":209},[100,36194,206],{"class":205},[100,36196,215],{"class":118},[14,36198,36199,36200,36202,36203,36206],{},"In Python, an empty list is treated as ",[17,36201,1649],{},", so ",[17,36204,36205],{},"if items:"," is a simple emptiness check.",[675,36208,36210],{"id":36209},"_5-print-the-index-and-list-length-while-debugging","5. Print the index and list length while debugging",[14,36212,36213],{},"If you are not sure what is going wrong, print both values:",[91,36215,36217],{"className":93,"code":36216,"language":95,"meta":96,"style":96},"items = [\"a\", \"b\", \"c\"]\nindex = 3\n\nprint(\"List:\", items)\nprint(\"Length:\", len(items))\nprint(\"Index:\", index)\n\nif 0 \u003C= index \u003C len(items):\n    print(items[index])\nelse:\n    print(\"Index is out of range\")\n",[17,36218,36219,36251,36259,36263,36282,36305,36325,36329,36349,36363,36369],{"__ignoreMap":96},[100,36220,36221,36223,36225,36227,36229,36231,36233,36235,36237,36239,36241,36243,36245,36247,36249],{"class":102,"line":103},[100,36222,3095],{"class":106},[100,36224,111],{"class":110},[100,36226,594],{"class":118},[100,36228,206],{"class":205},[100,36230,295],{"class":209},[100,36232,206],{"class":205},[100,36234,126],{"class":118},[100,36236,1708],{"class":205},[100,36238,4027],{"class":209},[100,36240,206],{"class":205},[100,36242,126],{"class":118},[100,36244,1708],{"class":205},[100,36246,4036],{"class":209},[100,36248,206],{"class":205},[100,36250,603],{"class":118},[100,36252,36253,36255,36257],{"class":102,"line":135},[100,36254,3116],{"class":106},[100,36256,111],{"class":110},[100,36258,31170],{"class":122},[100,36260,36261],{"class":102,"line":142},[100,36262,139],{"emptyLinePlaceholder":138},[100,36264,36265,36267,36269,36271,36274,36276,36278,36280],{"class":102,"line":152},[100,36266,372],{"class":114},[100,36268,170],{"class":118},[100,36270,206],{"class":205},[100,36272,36273],{"class":209},"List:",[100,36275,206],{"class":205},[100,36277,126],{"class":118},[100,36279,12000],{"class":178},[100,36281,215],{"class":118},[100,36283,36284,36286,36288,36290,36293,36295,36297,36299,36301,36303],{"class":102,"line":164},[100,36285,372],{"class":114},[100,36287,170],{"class":118},[100,36289,206],{"class":205},[100,36291,36292],{"class":209},"Length:",[100,36294,206],{"class":205},[100,36296,126],{"class":118},[100,36298,3141],{"class":114},[100,36300,170],{"class":118},[100,36302,3146],{"class":178},[100,36304,182],{"class":118},[100,36306,36307,36309,36311,36313,36316,36318,36320,36323],{"class":102,"line":185},[100,36308,372],{"class":114},[100,36310,170],{"class":118},[100,36312,206],{"class":205},[100,36314,36315],{"class":209},"Index:",[100,36317,206],{"class":205},[100,36319,126],{"class":118},[100,36321,36322],{"class":178}," index",[100,36324,215],{"class":118},[100,36326,36327],{"class":102,"line":197},[100,36328,139],{"emptyLinePlaceholder":138},[100,36330,36331,36333,36335,36337,36339,36341,36343,36345,36347],{"class":102,"line":771},[100,36332,2736],{"class":145},[100,36334,1921],{"class":122},[100,36336,3133],{"class":110},[100,36338,3136],{"class":106},[100,36340,2782],{"class":110},[100,36342,3141],{"class":114},[100,36344,170],{"class":118},[100,36346,3146],{"class":178},[100,36348,522],{"class":118},[100,36350,36351,36353,36355,36357,36359,36361],{"class":102,"line":787},[100,36352,200],{"class":114},[100,36354,170],{"class":118},[100,36356,3146],{"class":178},[100,36358,3166],{"class":118},[100,36360,3169],{"class":178},[100,36362,132],{"class":118},[100,36364,36365,36367],{"class":102,"line":5816},[100,36366,4944],{"class":145},[100,36368,149],{"class":118},[100,36370,36371,36373,36375,36377,36379,36381],{"class":102,"line":5833},[100,36372,200],{"class":114},[100,36374,170],{"class":118},[100,36376,206],{"class":205},[100,36378,32781],{"class":209},[100,36380,206],{"class":205},[100,36382,215],{"class":118},[14,36384,36385],{},"This makes it easier to see the mismatch.",[77,36387,36389],{"id":36388},"common-real-examples","Common real examples",[14,36391,36392],{},"These are common ways beginners run into this error.",[675,36394,36396],{"id":36395},"reading-the-first-item-from-an-empty-list","Reading the first item from an empty list",[91,36398,36400],{"className":93,"code":36399,"language":95,"meta":96,"style":96},"results = []\n\nif results:\n    print(results[0])\nelse:\n    print(\"No results found\")\n",[17,36401,36402,36411,36415,36424,36439,36445],{"__ignoreMap":96},[100,36403,36404,36407,36409],{"class":102,"line":103},[100,36405,36406],{"class":106},"results ",[100,36408,111],{"class":110},[100,36410,33843],{"class":118},[100,36412,36413],{"class":102,"line":135},[100,36414,139],{"emptyLinePlaceholder":138},[100,36416,36417,36419,36422],{"class":102,"line":142},[100,36418,2736],{"class":145},[100,36420,36421],{"class":106}," results",[100,36423,149],{"class":118},[100,36425,36426,36428,36430,36433,36435,36437],{"class":102,"line":152},[100,36427,200],{"class":114},[100,36429,170],{"class":118},[100,36431,36432],{"class":178},"results",[100,36434,3166],{"class":118},[100,36436,2279],{"class":122},[100,36438,132],{"class":118},[100,36440,36441,36443],{"class":102,"line":164},[100,36442,4944],{"class":145},[100,36444,149],{"class":118},[100,36446,36447,36449,36451,36453,36456,36458],{"class":102,"line":185},[100,36448,200],{"class":114},[100,36450,170],{"class":118},[100,36452,206],{"class":205},[100,36454,36455],{"class":209},"No results found",[100,36457,206],{"class":205},[100,36459,215],{"class":118},[675,36461,1357,36463,36465],{"id":36462},"using-i-1-near-the-end-of-a-loop",[17,36464,34118],{}," near the end of a loop",[91,36467,36469],{"className":93,"code":36468,"language":95,"meta":96,"style":96},"items = [\"a\", \"b\", \"c\"]\n\nfor i in range(len(items) - 1):\n    print(items[i], items[i + 1])\n",[17,36470,36471,36503,36507,36533],{"__ignoreMap":96},[100,36472,36473,36475,36477,36479,36481,36483,36485,36487,36489,36491,36493,36495,36497,36499,36501],{"class":102,"line":103},[100,36474,3095],{"class":106},[100,36476,111],{"class":110},[100,36478,594],{"class":118},[100,36480,206],{"class":205},[100,36482,295],{"class":209},[100,36484,206],{"class":205},[100,36486,126],{"class":118},[100,36488,1708],{"class":205},[100,36490,4027],{"class":209},[100,36492,206],{"class":205},[100,36494,126],{"class":118},[100,36496,1708],{"class":205},[100,36498,4036],{"class":209},[100,36500,206],{"class":205},[100,36502,603],{"class":118},[100,36504,36505],{"class":102,"line":135},[100,36506,139],{"emptyLinePlaceholder":138},[100,36508,36509,36511,36513,36515,36517,36519,36521,36523,36525,36527,36529,36531],{"class":102,"line":142},[100,36510,71],{"class":145},[100,36512,29838],{"class":106},[100,36514,617],{"class":145},[100,36516,29843],{"class":114},[100,36518,170],{"class":118},[100,36520,17779],{"class":114},[100,36522,170],{"class":118},[100,36524,3146],{"class":178},[100,36526,6155],{"class":118},[100,36528,1906],{"class":110},[100,36530,6915],{"class":122},[100,36532,522],{"class":118},[100,36534,36535,36537,36539,36541,36543,36545,36548,36550,36552,36554,36556,36558],{"class":102,"line":152},[100,36536,200],{"class":114},[100,36538,170],{"class":118},[100,36540,3146],{"class":178},[100,36542,3166],{"class":118},[100,36544,29858],{"class":178},[100,36546,36547],{"class":118},"],",[100,36549,12000],{"class":178},[100,36551,3166],{"class":118},[100,36553,34106],{"class":178},[100,36555,6547],{"class":110},[100,36557,6915],{"class":122},[100,36559,132],{"class":118},[14,36561,36562,36563,36565],{},"This works because the loop stops before ",[17,36564,34118],{}," goes out of range.",[675,36567,36569],{"id":36568},"splitting-text-and-expecting-more-parts-than-exist","Splitting text and expecting more parts than exist",[91,36571,36573],{"className":93,"code":36572,"language":95,"meta":96,"style":96},"name = \"Alice\"\nparts = name.split()\n\nif len(parts) >= 2:\n    print(parts[1])\nelse:\n    print(\"There is no second word\")\n",[17,36574,36575,36587,36601,36605,36624,36638,36644],{"__ignoreMap":96},[100,36576,36577,36579,36581,36583,36585],{"class":102,"line":103},[100,36578,1691],{"class":106},[100,36580,111],{"class":110},[100,36582,1708],{"class":205},[100,36584,15343],{"class":209},[100,36586,1714],{"class":205},[100,36588,36589,36591,36593,36595,36597,36599],{"class":102,"line":135},[100,36590,4345],{"class":106},[100,36592,111],{"class":110},[100,36594,1703],{"class":106},[100,36596,290],{"class":118},[100,36598,3922],{"class":178},[100,36600,4734],{"class":118},[100,36602,36603],{"class":102,"line":142},[100,36604,139],{"emptyLinePlaceholder":138},[100,36606,36607,36609,36611,36613,36615,36617,36620,36622],{"class":102,"line":152},[100,36608,2736],{"class":145},[100,36610,3141],{"class":114},[100,36612,170],{"class":118},[100,36614,4373],{"class":178},[100,36616,6155],{"class":118},[100,36618,36619],{"class":110}," >=",[100,36621,129],{"class":122},[100,36623,149],{"class":118},[100,36625,36626,36628,36630,36632,36634,36636],{"class":102,"line":164},[100,36627,200],{"class":114},[100,36629,170],{"class":118},[100,36631,4373],{"class":178},[100,36633,3166],{"class":118},[100,36635,123],{"class":122},[100,36637,132],{"class":118},[100,36639,36640,36642],{"class":102,"line":185},[100,36641,4944],{"class":145},[100,36643,149],{"class":118},[100,36645,36646,36648,36650,36652,36655,36657],{"class":102,"line":197},[100,36647,200],{"class":114},[100,36649,170],{"class":118},[100,36651,206],{"class":205},[100,36653,36654],{"class":209},"There is no second word",[100,36656,206],{"class":205},[100,36658,215],{"class":118},[675,36660,36662],{"id":36661},"accessing-results-from-a-file-or-api-without-checking-length","Accessing results from a file or API without checking length",[91,36664,36666],{"className":93,"code":36665,"language":95,"meta":96,"style":96},"results = [\"first result\"]\n\nif len(results) >= 2:\n    print(results[1])\nelse:\n    print(\"There is no second result\")\n",[17,36667,36668,36685,36689,36707,36721,36727],{"__ignoreMap":96},[100,36669,36670,36672,36674,36676,36678,36681,36683],{"class":102,"line":103},[100,36671,36406],{"class":106},[100,36673,111],{"class":110},[100,36675,594],{"class":118},[100,36677,206],{"class":205},[100,36679,36680],{"class":209},"first result",[100,36682,206],{"class":205},[100,36684,603],{"class":118},[100,36686,36687],{"class":102,"line":135},[100,36688,139],{"emptyLinePlaceholder":138},[100,36690,36691,36693,36695,36697,36699,36701,36703,36705],{"class":102,"line":142},[100,36692,2736],{"class":145},[100,36694,3141],{"class":114},[100,36696,170],{"class":118},[100,36698,36432],{"class":178},[100,36700,6155],{"class":118},[100,36702,36619],{"class":110},[100,36704,129],{"class":122},[100,36706,149],{"class":118},[100,36708,36709,36711,36713,36715,36717,36719],{"class":102,"line":152},[100,36710,200],{"class":114},[100,36712,170],{"class":118},[100,36714,36432],{"class":178},[100,36716,3166],{"class":118},[100,36718,123],{"class":122},[100,36720,132],{"class":118},[100,36722,36723,36725],{"class":102,"line":164},[100,36724,4944],{"class":145},[100,36726,149],{"class":118},[100,36728,36729,36731,36733,36735,36738,36740],{"class":102,"line":185},[100,36730,200],{"class":114},[100,36732,170],{"class":118},[100,36734,206],{"class":205},[100,36736,36737],{"class":209},"There is no second result",[100,36739,206],{"class":205},[100,36741,215],{"class":118},[14,36743,36744],{},"When data comes from outside your program, always check its length first.",[77,36746,3274],{"id":3273},[14,36748,36749],{},"If you are getting this error and do not know why, try these steps.",[675,36751,36753],{"id":36752},"_1-print-the-list","1. Print the list",[91,36755,36757],{"className":93,"code":36756,"language":95,"meta":96,"style":96},"print(my_list)\n",[17,36758,36759],{"__ignoreMap":96},[100,36760,36761,36763,36765,36767],{"class":102,"line":103},[100,36762,372],{"class":114},[100,36764,170],{"class":118},[100,36766,7441],{"class":178},[100,36768,215],{"class":118},[14,36770,36771],{},"This shows what is actually in the list.",[675,36773,36775],{"id":36774},"_2-print-the-list-length","2. Print the list length",[91,36777,36779],{"className":93,"code":36778,"language":95,"meta":96,"style":96},"print(len(my_list))\n",[17,36780,36781],{"__ignoreMap":96},[100,36782,36783,36785,36787,36789,36791,36793],{"class":102,"line":103},[100,36784,372],{"class":114},[100,36786,170],{"class":118},[100,36788,17779],{"class":114},[100,36790,170],{"class":118},[100,36792,7441],{"class":178},[100,36794,182],{"class":118},[14,36796,36797,36798,290],{},"This tells you the highest valid positive index: ",[17,36799,36800],{},"len(my_list) - 1",[675,36802,36804],{"id":36803},"_3-print-the-index-value","3. Print the index value",[91,36806,36808],{"className":93,"code":36807,"language":95,"meta":96,"style":96},"print(index)\n",[17,36809,36810],{"__ignoreMap":96},[100,36811,36812,36814,36816,36818],{"class":102,"line":103},[100,36813,372],{"class":114},[100,36815,170],{"class":118},[100,36817,3169],{"class":178},[100,36819,215],{"class":118},[14,36821,36822],{},"Sometimes the index is not what you expected.",[675,36824,36826],{"id":36825},"_4-check-whether-the-list-is-empty","4. Check whether the list is empty",[91,36828,36830],{"className":93,"code":36829,"language":95,"meta":96,"style":96},"if not my_list:\n    print(\"The list is empty\")\n",[17,36831,36832,36842],{"__ignoreMap":96},[100,36833,36834,36836,36838,36840],{"class":102,"line":103},[100,36835,2736],{"class":145},[100,36837,4921],{"class":110},[100,36839,7573],{"class":106},[100,36841,149],{"class":118},[100,36843,36844,36846,36848,36850,36852,36854],{"class":102,"line":135},[100,36845,200],{"class":114},[100,36847,170],{"class":118},[100,36849,206],{"class":205},[100,36851,33920],{"class":209},[100,36853,206],{"class":205},[100,36855,215],{"class":118},[14,36857,36858,36859,290],{},"This is important before using ",[17,36860,36138],{},[675,36862,36864],{"id":36863},"_5-confirm-loop-boundaries","5. Confirm loop boundaries",[14,36866,36867,36868,36870],{},"If the error happens in a loop, check your ",[17,36869,32990],{}," carefully.",[14,36872,36873],{},"Bad:",[91,36875,36877],{"className":93,"code":36876,"language":95,"meta":96,"style":96},"for i in range(len(my_list) + 1):\n    print(my_list[i])\n",[17,36878,36879,36905],{"__ignoreMap":96},[100,36880,36881,36883,36885,36887,36889,36891,36893,36895,36897,36899,36901,36903],{"class":102,"line":103},[100,36882,71],{"class":145},[100,36884,29838],{"class":106},[100,36886,617],{"class":145},[100,36888,29843],{"class":114},[100,36890,170],{"class":118},[100,36892,17779],{"class":114},[100,36894,170],{"class":118},[100,36896,7441],{"class":178},[100,36898,6155],{"class":118},[100,36900,2895],{"class":110},[100,36902,6915],{"class":122},[100,36904,522],{"class":118},[100,36906,36907,36909,36911,36913,36915,36917],{"class":102,"line":135},[100,36908,200],{"class":114},[100,36910,170],{"class":118},[100,36912,7441],{"class":178},[100,36914,3166],{"class":118},[100,36916,29858],{"class":178},[100,36918,132],{"class":118},[14,36920,36921],{},"Good:",[91,36923,36925],{"className":93,"code":36924,"language":95,"meta":96,"style":96},"for i in range(len(my_list)):\n    print(my_list[i])\n",[17,36926,36927,36947],{"__ignoreMap":96},[100,36928,36929,36931,36933,36935,36937,36939,36941,36943,36945],{"class":102,"line":103},[100,36930,71],{"class":145},[100,36932,29838],{"class":106},[100,36934,617],{"class":145},[100,36936,29843],{"class":114},[100,36938,170],{"class":118},[100,36940,17779],{"class":114},[100,36942,170],{"class":118},[100,36944,7441],{"class":178},[100,36946,33665],{"class":118},[100,36948,36949,36951,36953,36955,36957,36959],{"class":102,"line":135},[100,36950,200],{"class":114},[100,36952,170],{"class":118},[100,36954,7441],{"class":178},[100,36956,3166],{"class":118},[100,36958,29858],{"class":178},[100,36960,132],{"class":118},[14,36962,36963,36964,36968],{},"If you need both the index and the value, ",[295,36965,36966],{"href":33712},[17,36967,1575],{}," is often a better choice.",[77,36970,36972],{"id":36971},"ways-to-avoid-this-error","Ways to avoid this error",[14,36974,36975],{},"These habits help prevent the problem before it happens.",[675,36977,36979],{"id":36978},"loop-over-items-instead-of-indexes","Loop over items instead of indexes",[91,36981,36982],{"className":93,"code":36063,"language":95,"meta":96,"style":96},[17,36983,36984,37016,37020,37032],{"__ignoreMap":96},[100,36985,36986,36988,36990,36992,36994,36996,36998,37000,37002,37004,37006,37008,37010,37012,37014],{"class":102,"line":103},[100,36987,3095],{"class":106},[100,36989,111],{"class":110},[100,36991,594],{"class":118},[100,36993,206],{"class":205},[100,36995,295],{"class":209},[100,36997,206],{"class":205},[100,36999,126],{"class":118},[100,37001,1708],{"class":205},[100,37003,4027],{"class":209},[100,37005,206],{"class":205},[100,37007,126],{"class":118},[100,37009,1708],{"class":205},[100,37011,4036],{"class":209},[100,37013,206],{"class":205},[100,37015,603],{"class":118},[100,37017,37018],{"class":102,"line":135},[100,37019,139],{"emptyLinePlaceholder":138},[100,37021,37022,37024,37026,37028,37030],{"class":102,"line":142},[100,37023,71],{"class":145},[100,37025,7568],{"class":106},[100,37027,617],{"class":145},[100,37029,12000],{"class":106},[100,37031,149],{"class":118},[100,37033,37034,37036,37038,37040],{"class":102,"line":152},[100,37035,200],{"class":114},[100,37037,170],{"class":118},[100,37039,7555],{"class":178},[100,37041,215],{"class":118},[14,37043,37044],{},"This avoids manual index mistakes.",[675,37046,241,37047,37049],{"id":33705},[17,37048,1575],{}," when you need both index and value",[91,37051,37052],{"className":93,"code":33717,"language":95,"meta":96,"style":96},[17,37053,37054,37086,37090,37110],{"__ignoreMap":96},[100,37055,37056,37058,37060,37062,37064,37066,37068,37070,37072,37074,37076,37078,37080,37082,37084],{"class":102,"line":103},[100,37057,3095],{"class":106},[100,37059,111],{"class":110},[100,37061,594],{"class":118},[100,37063,206],{"class":205},[100,37065,295],{"class":209},[100,37067,206],{"class":205},[100,37069,126],{"class":118},[100,37071,1708],{"class":205},[100,37073,4027],{"class":209},[100,37075,206],{"class":205},[100,37077,126],{"class":118},[100,37079,1708],{"class":205},[100,37081,4036],{"class":209},[100,37083,206],{"class":205},[100,37085,603],{"class":118},[100,37087,37088],{"class":102,"line":135},[100,37089,139],{"emptyLinePlaceholder":138},[100,37091,37092,37094,37096,37098,37100,37102,37104,37106,37108],{"class":102,"line":142},[100,37093,71],{"class":145},[100,37095,33762],{"class":106},[100,37097,126],{"class":118},[100,37099,989],{"class":106},[100,37101,617],{"class":145},[100,37103,33771],{"class":114},[100,37105,170],{"class":118},[100,37107,3146],{"class":178},[100,37109,522],{"class":118},[100,37111,37112,37114,37116,37118,37120,37122],{"class":102,"line":152},[100,37113,200],{"class":114},[100,37115,170],{"class":118},[100,37117,29858],{"class":178},[100,37119,126],{"class":118},[100,37121,6747],{"class":178},[100,37123,215],{"class":118},[14,37125,37126],{},[22,37127,218],{},[91,37129,37130],{"className":93,"code":33796,"language":95,"meta":96,"style":96},[17,37131,37132,37138,37144],{"__ignoreMap":96},[100,37133,37134,37136],{"class":102,"line":103},[100,37135,2279],{"class":122},[100,37137,33805],{"class":106},[100,37139,37140,37142],{"class":102,"line":135},[100,37141,123],{"class":122},[100,37143,33812],{"class":106},[100,37145,37146,37148],{"class":102,"line":142},[100,37147,559],{"class":122},[100,37149,33819],{"class":106},[14,37151,37152],{},"This is safer than managing the index yourself in many cases.",[675,37154,37156],{"id":37155},"check-list-length-before-reading-fixed-positions","Check list length before reading fixed positions",[91,37158,37160],{"className":93,"code":37159,"language":95,"meta":96,"style":96},"items = [\"apple\", \"banana\"]\n\nif len(items) > 1:\n    print(items[1])\n",[17,37161,37162,37186,37190,37208],{"__ignoreMap":96},[100,37163,37164,37166,37168,37170,37172,37174,37176,37178,37180,37182,37184],{"class":102,"line":103},[100,37165,3095],{"class":106},[100,37167,111],{"class":110},[100,37169,594],{"class":118},[100,37171,206],{"class":205},[100,37173,4148],{"class":209},[100,37175,206],{"class":205},[100,37177,126],{"class":118},[100,37179,1708],{"class":205},[100,37181,4157],{"class":209},[100,37183,206],{"class":205},[100,37185,603],{"class":118},[100,37187,37188],{"class":102,"line":135},[100,37189,139],{"emptyLinePlaceholder":138},[100,37191,37192,37194,37196,37198,37200,37202,37204,37206],{"class":102,"line":142},[100,37193,2736],{"class":145},[100,37195,3141],{"class":114},[100,37197,170],{"class":118},[100,37199,3146],{"class":178},[100,37201,6155],{"class":118},[100,37203,31167],{"class":110},[100,37205,6915],{"class":122},[100,37207,149],{"class":118},[100,37209,37210,37212,37214,37216,37218,37220],{"class":102,"line":152},[100,37211,200],{"class":114},[100,37213,170],{"class":118},[100,37215,3146],{"class":178},[100,37217,3166],{"class":118},[100,37219,123],{"class":122},[100,37221,132],{"class":118},[675,37223,37225],{"id":37224},"use-slicing-when-a-missing-range-should-not-crash-the-program","Use slicing when a missing range should not crash the program",[14,37227,37228,37229,37231],{},"Slicing is often safer because it does not raise ",[17,37230,16420],{}," when the end is too large.",[91,37233,37235],{"className":93,"code":37234,"language":95,"meta":96,"style":96},"items = [\"a\", \"b\", \"c\"]\nprint(items[0:5])\n",[17,37236,37237,37269],{"__ignoreMap":96},[100,37238,37239,37241,37243,37245,37247,37249,37251,37253,37255,37257,37259,37261,37263,37265,37267],{"class":102,"line":103},[100,37240,3095],{"class":106},[100,37242,111],{"class":110},[100,37244,594],{"class":118},[100,37246,206],{"class":205},[100,37248,295],{"class":209},[100,37250,206],{"class":205},[100,37252,126],{"class":118},[100,37254,1708],{"class":205},[100,37256,4027],{"class":209},[100,37258,206],{"class":205},[100,37260,126],{"class":118},[100,37262,1708],{"class":205},[100,37264,4036],{"class":209},[100,37266,206],{"class":205},[100,37268,603],{"class":118},[100,37270,37271,37273,37275,37277,37279,37281,37283,37285],{"class":102,"line":135},[100,37272,372],{"class":114},[100,37274,170],{"class":118},[100,37276,3146],{"class":178},[100,37278,3166],{"class":118},[100,37280,2279],{"class":122},[100,37282,89],{"class":118},[100,37284,3557],{"class":122},[100,37286,132],{"class":118},[14,37288,37289],{},[22,37290,218],{},[91,37292,37294],{"className":93,"code":37293,"language":95,"meta":96,"style":96},"['a', 'b', 'c']\n",[17,37295,37296],{"__ignoreMap":96},[100,37297,37298,37300,37302,37304,37306,37308,37310,37312,37314,37316,37318,37320,37322],{"class":102,"line":103},[100,37299,3166],{"class":118},[100,37301,1280],{"class":205},[100,37303,295],{"class":209},[100,37305,1280],{"class":205},[100,37307,126],{"class":118},[100,37309,1274],{"class":205},[100,37311,4027],{"class":209},[100,37313,1280],{"class":205},[100,37315,126],{"class":118},[100,37317,1274],{"class":205},[100,37319,4036],{"class":209},[100,37321,1280],{"class":205},[100,37323,603],{"class":118},[14,37325,37326],{},"That can be useful when you want a range of items without crashing.",[77,37328,7117],{"id":7116},[14,37330,37331],{},"These are some very common causes of this error:",[40,37333,37334,37339,37344,37350,37356],{},[43,37335,33005,37336],{},[17,37337,37338],{},"my_list[len(my_list)]",[43,37340,1357,37341],{},[17,37342,37343],{},"range(len(my_list) + 1)",[43,37345,37346,37347,37349],{},"Reading ",[17,37348,36138],{}," when the list is empty",[43,37351,37352,37353],{},"Using a negative index smaller than ",[17,37354,37355],{},"-len(my_list)",[43,37357,11012,37358,37360],{},[17,37359,8090],{}," to return more items than it did",[675,37362,37364,37365,37367],{"id":37363},"why-my_listlenmy_list-is-wrong","Why ",[17,37366,37338],{}," is wrong",[14,37369,37370,37371,37373,37374,5870,37376,5629,37378,290],{},"If a list has length ",[17,37372,11541],{},", the valid indexes are ",[17,37375,2279],{},[17,37377,123],{},[17,37379,559],{},[14,37381,37382],{},"So this is wrong:",[91,37384,37386],{"className":93,"code":37385,"language":95,"meta":96,"style":96},"my_list = [\"a\", \"b\", \"c\"]\nprint(my_list[len(my_list)])\n",[17,37387,37388,37420],{"__ignoreMap":96},[100,37389,37390,37392,37394,37396,37398,37400,37402,37404,37406,37408,37410,37412,37414,37416,37418],{"class":102,"line":103},[100,37391,7405],{"class":106},[100,37393,111],{"class":110},[100,37395,594],{"class":118},[100,37397,206],{"class":205},[100,37399,295],{"class":209},[100,37401,206],{"class":205},[100,37403,126],{"class":118},[100,37405,1708],{"class":205},[100,37407,4027],{"class":209},[100,37409,206],{"class":205},[100,37411,126],{"class":118},[100,37413,1708],{"class":205},[100,37415,4036],{"class":209},[100,37417,206],{"class":205},[100,37419,603],{"class":118},[100,37421,37422,37424,37426,37428,37430,37432,37434,37436],{"class":102,"line":135},[100,37423,372],{"class":114},[100,37425,170],{"class":118},[100,37427,7441],{"class":178},[100,37429,3166],{"class":118},[100,37431,17779],{"class":114},[100,37433,170],{"class":118},[100,37435,7441],{"class":178},[100,37437,37438],{"class":118},")])\n",[14,37440,14478,37441,2025,37444,37446,37447,37449],{},[17,37442,37443],{},"len(my_list)",[17,37445,11541],{},", and index ",[17,37448,11541],{}," is out of range.",[77,37451,1514],{"id":1513},[675,37453,37455],{"id":37454},"why-does-index-3-fail-in-a-list-with-3-items","Why does index 3 fail in a list with 3 items?",[14,37457,37458,37459,37461,37462,5870,37464,5629,37466,290],{},"Because list indexes start at ",[17,37460,2279],{},". A 3-item list has valid indexes ",[17,37463,2279],{},[17,37465,123],{},[17,37467,559],{},[675,37469,37471],{"id":37470},"can-negative-indexes-cause-this-error","Can negative indexes cause this error?",[14,37473,37474,37475,3178,37477,37479],{},"Yes. Negative indexes work only if they stay within the list length, such as ",[17,37476,35345],{},[17,37478,35348],{}," for a non-empty list.",[675,37481,37483],{"id":37482},"how-do-i-check-if-an-index-is-valid","How do I check if an index is valid?",[14,37485,37486],{},"Use a condition like this before accessing the item:",[91,37488,37490],{"className":93,"code":37489,"language":95,"meta":96,"style":96},"if 0 \u003C= index \u003C len(my_list):\n    print(my_list[index])\n",[17,37491,37492,37512],{"__ignoreMap":96},[100,37493,37494,37496,37498,37500,37502,37504,37506,37508,37510],{"class":102,"line":103},[100,37495,2736],{"class":145},[100,37497,1921],{"class":122},[100,37499,3133],{"class":110},[100,37501,3136],{"class":106},[100,37503,2782],{"class":110},[100,37505,3141],{"class":114},[100,37507,170],{"class":118},[100,37509,7441],{"class":178},[100,37511,522],{"class":118},[100,37513,37514,37516,37518,37520,37522,37524],{"class":102,"line":135},[100,37515,200],{"class":114},[100,37517,170],{"class":118},[100,37519,7441],{"class":178},[100,37521,3166],{"class":118},[100,37523,3169],{"class":178},[100,37525,132],{"class":118},[675,37527,37529],{"id":37528},"should-i-use-try-except-for-this","Should I use try-except for this?",[14,37531,37532],{},"You can, but for beginners it is often clearer to check the length first when missing items are expected.",[14,37534,580],{},[91,37536,37538],{"className":93,"code":37537,"language":95,"meta":96,"style":96},"items = [\"a\", \"b\", \"c\"]\nindex = 5\n\ntry:\n    print(items[index])\nexcept IndexError:\n    print(\"Index is out of range\")\n",[17,37539,37540,37572,37581,37585,37591,37605,37614],{"__ignoreMap":96},[100,37541,37542,37544,37546,37548,37550,37552,37554,37556,37558,37560,37562,37564,37566,37568,37570],{"class":102,"line":103},[100,37543,3095],{"class":106},[100,37545,111],{"class":110},[100,37547,594],{"class":118},[100,37549,206],{"class":205},[100,37551,295],{"class":209},[100,37553,206],{"class":205},[100,37555,126],{"class":118},[100,37557,1708],{"class":205},[100,37559,4027],{"class":209},[100,37561,206],{"class":205},[100,37563,126],{"class":118},[100,37565,1708],{"class":205},[100,37567,4036],{"class":209},[100,37569,206],{"class":205},[100,37571,603],{"class":118},[100,37573,37574,37576,37578],{"class":102,"line":135},[100,37575,3116],{"class":106},[100,37577,111],{"class":110},[100,37579,37580],{"class":122}," 5\n",[100,37582,37583],{"class":102,"line":142},[100,37584,139],{"emptyLinePlaceholder":138},[100,37586,37587,37589],{"class":102,"line":152},[100,37588,146],{"class":145},[100,37590,149],{"class":118},[100,37592,37593,37595,37597,37599,37601,37603],{"class":102,"line":164},[100,37594,200],{"class":114},[100,37596,170],{"class":118},[100,37598,3146],{"class":178},[100,37600,3166],{"class":118},[100,37602,3169],{"class":178},[100,37604,132],{"class":118},[100,37606,37607,37609,37612],{"class":102,"line":185},[100,37608,188],{"class":145},[100,37610,37611],{"class":191}," IndexError",[100,37613,149],{"class":118},[100,37615,37616,37618,37620,37622,37624,37626],{"class":102,"line":197},[100,37617,200],{"class":114},[100,37619,170],{"class":118},[100,37621,206],{"class":205},[100,37623,32781],{"class":209},[100,37625,206],{"class":205},[100,37627,215],{"class":118},[14,37629,37630],{},"This works, but if out-of-range access is a normal possibility, checking first is often easier to read.",[77,37632,1554],{"id":1553},[40,37634,37635,37639,37645,37651,37657,37661,37666,37672],{},[43,37636,37637],{},[295,37638,35359],{"href":8110},[43,37640,37641],{},[295,37642,1572,37643,9570],{"href":35092},[17,37644,32803],{},[43,37646,37647],{},[295,37648,1572,37649,1576],{"href":32987},[17,37650,32990],{},[43,37652,37653],{},[295,37654,1572,37655,1576],{"href":33712},[17,37656,1575],{},[43,37658,37659],{},[295,37660,9186],{"href":8545},[43,37662,37663],{},[295,37664,37665],{"href":34297},"How to find an item in a list in Python",[43,37667,37668],{},[295,37669,37671],{"href":37670},"\u002Ferrors\u002Findexerror-in-python-causes-and-fixes\u002F","IndexError in Python: causes and fixes",[43,37673,37674],{},[295,37675,37676],{"href":34984},"IndexError: tuple index out of range",[1589,37678,37679],{},"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 .sP7_E, html code.shiki .sP7_E{--shiki-light:#39ADB5;--shiki-default:#24292E;--shiki-dark:#E1E4E8}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 .srdBf, html code.shiki .srdBf{--shiki-light:#F76D47;--shiki-default:#005CC5;--shiki-dark:#79B8FF}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 .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}",{"title":96,"searchDepth":135,"depth":135,"links":37681},[37682,37683,37686,37689,37697,37706,37713,37720,37726,37730,37736],{"id":79,"depth":135,"text":80},{"id":5880,"depth":135,"text":5881,"children":37684},[37685],{"id":35322,"depth":142,"text":35323},{"id":5944,"depth":135,"text":5945,"children":37687},[37688],{"id":35437,"depth":142,"text":35438},{"id":25090,"depth":135,"text":25091,"children":37690},[37691,37692,37693,37695,37696],{"id":35481,"depth":142,"text":32978},{"id":35527,"depth":142,"text":35528},{"id":35595,"depth":142,"text":37694},"Looping with the wrong range() stop value",{"id":35717,"depth":142,"text":35718},{"id":35751,"depth":142,"text":35752},{"id":6243,"depth":135,"text":6244,"children":37698},[37699,37701,37702,37703,37705],{"id":35828,"depth":142,"text":37700},"1. Check the list length with len() before indexing",{"id":35940,"depth":142,"text":35941},{"id":36056,"depth":142,"text":36057},{"id":36134,"depth":142,"text":37704},"4. Check for an empty list before using my_list[0]",{"id":36209,"depth":142,"text":36210},{"id":36388,"depth":135,"text":36389,"children":37707},[37708,37709,37711,37712],{"id":36395,"depth":142,"text":36396},{"id":36462,"depth":142,"text":37710},"Using i + 1 near the end of a loop",{"id":36568,"depth":142,"text":36569},{"id":36661,"depth":142,"text":36662},{"id":3273,"depth":135,"text":3274,"children":37714},[37715,37716,37717,37718,37719],{"id":36752,"depth":142,"text":36753},{"id":36774,"depth":142,"text":36775},{"id":36803,"depth":142,"text":36804},{"id":36825,"depth":142,"text":36826},{"id":36863,"depth":142,"text":36864},{"id":36971,"depth":135,"text":36972,"children":37721},[37722,37723,37724,37725],{"id":36978,"depth":142,"text":36979},{"id":33705,"depth":142,"text":33706},{"id":37155,"depth":142,"text":37156},{"id":37224,"depth":142,"text":37225},{"id":7116,"depth":135,"text":7117,"children":37727},[37728],{"id":37363,"depth":142,"text":37729},"Why my_list[len(my_list)] is wrong",{"id":1513,"depth":135,"text":1514,"children":37731},[37732,37733,37734,37735],{"id":37454,"depth":142,"text":37455},{"id":37470,"depth":142,"text":37471},{"id":37482,"depth":142,"text":37483},{"id":37528,"depth":142,"text":37529},{"id":1553,"depth":135,"text":1554},"Master indexerror list index out of range fix explained in our comprehensive Python beginner guide.",{},"\u002Ferrors\u002Findexerror-list-index-out-of-range-fix-explained",{"title":35060,"description":37737},"errors\u002Findexerror-list-index-out-of-range-fix-explained","7-22twfR2SS-kHSxrQFurivHgUDk4xVC_dPiPBfOavA",{"id":37744,"title":37745,"body":37746,"description":39450,"extension":1623,"meta":39451,"navigation":138,"path":39452,"seo":39453,"stem":39454,"__hash__":39455},"content\u002Ferrors\u002Findexerror-tuple-index-out-of-range-fix.md","IndexError: tuple index out of range (Fix)",{"type":7,"value":37747,"toc":39425},[37748,37751,37756,37761,37763,37901,37913,37915,37918,37920,37941,37948,37950,37953,38008,38010,38028,38030,38050,38055,38062,38064,38067,38080,38083,38128,38131,38148,38155,38157,38161,38164,38239,38245,38248,38351,38354,38361,38365,38368,38434,38437,38441,38447,38523,38525,38551,38555,38557,38560,38574,38576,38654,38656,38741,38744,38773,38776,38793,38797,38800,38814,38816,38895,38898,38955,38957,38973,38977,38991,38994,39060,39063,39117,39119,39122,39145,39148,39217,39220,39241,39243,39306,39309,39360,39365,39367,39371,39376,39378,39381,39385,39388,39395,39398,39400,39422],[10,37749,37745],{"id":37750},"indexerror-tuple-index-out-of-range-fix",[14,37752,37753,37755],{},[17,37754,37676],{}," means your code is trying to read a tuple position that does not exist.",[14,37757,37758,37759,290],{},"This usually happens when you use an index that is too large, or when you forget that Python indexes start at ",[17,37760,2279],{},[77,37762,80],{"id":79},[91,37764,37766],{"className":93,"code":37765,"language":95,"meta":96,"style":96},"colors = (\"red\", \"green\", \"blue\")\n\n# Wrong: colors[3]  # IndexError\n\n# Correct: use a valid index\nprint(colors[2])\n\n# Or check length first\nindex = 3\nif index \u003C len(colors):\n    print(colors[index])\nelse:\n    print(\"Index out of range\")\n",[17,37767,37768,37800,37804,37809,37813,37818,37833,37837,37842,37850,37866,37880,37886],{"__ignoreMap":96},[100,37769,37770,37772,37774,37776,37778,37780,37782,37784,37786,37788,37790,37792,37794,37796,37798],{"class":102,"line":103},[100,37771,35257],{"class":106},[100,37773,111],{"class":110},[100,37775,16805],{"class":118},[100,37777,206],{"class":205},[100,37779,35266],{"class":209},[100,37781,206],{"class":205},[100,37783,126],{"class":118},[100,37785,1708],{"class":205},[100,37787,35275],{"class":209},[100,37789,206],{"class":205},[100,37791,126],{"class":118},[100,37793,1708],{"class":205},[100,37795,35284],{"class":209},[100,37797,206],{"class":205},[100,37799,215],{"class":118},[100,37801,37802],{"class":102,"line":135},[100,37803,139],{"emptyLinePlaceholder":138},[100,37805,37806],{"class":102,"line":142},[100,37807,37808],{"class":414},"# Wrong: colors[3]  # IndexError\n",[100,37810,37811],{"class":102,"line":152},[100,37812,139],{"emptyLinePlaceholder":138},[100,37814,37815],{"class":102,"line":164},[100,37816,37817],{"class":414},"# Correct: use a valid index\n",[100,37819,37820,37822,37824,37827,37829,37831],{"class":102,"line":185},[100,37821,372],{"class":114},[100,37823,170],{"class":118},[100,37825,37826],{"class":178},"colors",[100,37828,3166],{"class":118},[100,37830,559],{"class":122},[100,37832,132],{"class":118},[100,37834,37835],{"class":102,"line":197},[100,37836,139],{"emptyLinePlaceholder":138},[100,37838,37839],{"class":102,"line":771},[100,37840,37841],{"class":414},"# Or check length first\n",[100,37843,37844,37846,37848],{"class":102,"line":787},[100,37845,3116],{"class":106},[100,37847,111],{"class":110},[100,37849,31170],{"class":122},[100,37851,37852,37854,37856,37858,37860,37862,37864],{"class":102,"line":5816},[100,37853,2736],{"class":145},[100,37855,3136],{"class":106},[100,37857,2782],{"class":110},[100,37859,3141],{"class":114},[100,37861,170],{"class":118},[100,37863,37826],{"class":178},[100,37865,522],{"class":118},[100,37867,37868,37870,37872,37874,37876,37878],{"class":102,"line":5833},[100,37869,200],{"class":114},[100,37871,170],{"class":118},[100,37873,37826],{"class":178},[100,37875,3166],{"class":118},[100,37877,3169],{"class":178},[100,37879,132],{"class":118},[100,37881,37882,37884],{"class":102,"line":5848},[100,37883,4944],{"class":145},[100,37885,149],{"class":118},[100,37887,37888,37890,37892,37894,37897,37899],{"class":102,"line":5855},[100,37889,200],{"class":114},[100,37891,170],{"class":118},[100,37893,206],{"class":205},[100,37895,37896],{"class":209},"Index out of range",[100,37898,206],{"class":205},[100,37900,215],{"class":118},[14,37902,37903,37904,37906,37907,5870,37909,5629,37911,290],{},"Tuple indexes start at ",[17,37905,2279],{},". A tuple with 3 items has valid indexes ",[17,37908,2279],{},[17,37910,123],{},[17,37912,559],{},[77,37914,5881],{"id":5880},[14,37916,37917],{},"Python raises this error when you try to access a tuple index that is outside the tuple's valid range.",[14,37919,266],{},[40,37921,37922,37929,37938],{},[43,37923,37924,37925,9472,37927],{},"Tuple indexing starts at ",[17,37926,2279],{},[17,37928,123],{},[43,37930,37931,37932,5870,37934,5629,37936],{},"A tuple with 3 items has indexes ",[17,37933,2279],{},[17,37935,123],{},[17,37937,559],{},[43,37939,37940],{},"Negative indexes work from the end, but they must still be valid",[14,37942,37943,37944,290],{},"If you are new to tuples, see ",[295,37945,37947],{"href":37946},"\u002Flearn\u002Fpython-tuples-explained\u002F","Python tuples explained",[77,37949,11031],{"id":11030},[14,37951,37952],{},"Here is a small tuple with 3 items:",[91,37954,37956],{"className":93,"code":37955,"language":95,"meta":96,"style":96},"colors = (\"red\", \"green\", \"blue\")\n\nprint(colors[3])\n",[17,37957,37958,37990,37994],{"__ignoreMap":96},[100,37959,37960,37962,37964,37966,37968,37970,37972,37974,37976,37978,37980,37982,37984,37986,37988],{"class":102,"line":103},[100,37961,35257],{"class":106},[100,37963,111],{"class":110},[100,37965,16805],{"class":118},[100,37967,206],{"class":205},[100,37969,35266],{"class":209},[100,37971,206],{"class":205},[100,37973,126],{"class":118},[100,37975,1708],{"class":205},[100,37977,35275],{"class":209},[100,37979,206],{"class":205},[100,37981,126],{"class":118},[100,37983,1708],{"class":205},[100,37985,35284],{"class":209},[100,37987,206],{"class":205},[100,37989,215],{"class":118},[100,37991,37992],{"class":102,"line":135},[100,37993,139],{"emptyLinePlaceholder":138},[100,37995,37996,37998,38000,38002,38004,38006],{"class":102,"line":142},[100,37997,372],{"class":114},[100,37999,170],{"class":118},[100,38001,37826],{"class":178},[100,38003,3166],{"class":118},[100,38005,11541],{"class":122},[100,38007,132],{"class":118},[14,38009,218],{},[91,38011,38013],{"className":93,"code":38012,"language":95,"meta":96,"style":96},"IndexError: tuple index out of range\n",[17,38014,38015],{"__ignoreMap":96},[100,38016,38017,38019,38021,38024,38026],{"class":102,"line":103},[100,38018,16420],{"class":191},[100,38020,89],{"class":118},[100,38022,38023],{"class":191}," tuple",[100,38025,16427],{"class":106},[100,38027,16430],{"class":114},[14,38029,25013],{},[40,38031,38032,38035,38044],{},[43,38033,38034],{},"The tuple has 3 items",[43,38036,38037,38038,5870,38040,5629,38042],{},"The valid indexes are ",[17,38039,2279],{},[17,38041,123],{},[17,38043,559],{},[43,38045,38046,38049],{},[17,38047,38048],{},"colors[3]"," asks for a fourth item, but there is no fourth item",[14,38051,38052,38053,290],{},"The last valid index is ",[17,38054,559],{},[14,38056,38057,38058,290],{},"If you want a clearer explanation of positions in tuples, see ",[295,38059,38061],{"href":38060},"\u002Freference\u002Fpython-tuple-indexing-explained\u002F","Python tuple indexing explained",[77,38063,25091],{"id":25090},[14,38065,38066],{},"This error usually happens for one of these reasons:",[40,38068,38069,38071,38074,38077],{},[43,38070,32978],{},[43,38072,38073],{},"Using a negative index that goes past the start of the tuple",[43,38075,38076],{},"Assuming the tuple has more items than it really has",[43,38078,38079],{},"Using loop logic that goes one step too far",[14,38081,38082],{},"For example, this fails because the negative index is too small for the tuple:",[91,38084,38086],{"className":93,"code":38085,"language":95,"meta":96,"style":96},"numbers = (10, 20, 30)\n\nprint(numbers[-4])\n",[17,38087,38088,38108,38112],{"__ignoreMap":96},[100,38089,38090,38092,38094,38096,38098,38100,38102,38104,38106],{"class":102,"line":103},[100,38091,107],{"class":106},[100,38093,111],{"class":110},[100,38095,16805],{"class":118},[100,38097,356],{"class":122},[100,38099,126],{"class":118},[100,38101,361],{"class":122},[100,38103,126],{"class":118},[100,38105,16342],{"class":122},[100,38107,215],{"class":118},[100,38109,38110],{"class":102,"line":135},[100,38111,139],{"emptyLinePlaceholder":138},[100,38113,38114,38116,38118,38120,38122,38124,38126],{"class":102,"line":142},[100,38115,372],{"class":114},[100,38117,170],{"class":118},[100,38119,179],{"class":178},[100,38121,3166],{"class":118},[100,38123,2193],{"class":110},[100,38125,8322],{"class":122},[100,38127,132],{"class":118},[14,38129,38130],{},"A 3-item tuple supports these negative indexes:",[40,38132,38133,38138,38143],{},[43,38134,38135,38137],{},[17,38136,35345],{}," for the last item",[43,38139,38140,38142],{},[17,38141,35348],{}," for the second-to-last item",[43,38144,38145,38147],{},[17,38146,5897],{}," for the first item",[14,38149,38150,38151,38154],{},"But ",[17,38152,38153],{},"-4"," is outside the valid range.",[77,38156,6244],{"id":6243},[675,38158,38160],{"id":38159},"use-the-correct-index","Use the correct index",[14,38162,38163],{},"If you know the tuple size, use a valid index.",[91,38165,38167],{"className":93,"code":38166,"language":95,"meta":96,"style":96},"colors = (\"red\", \"green\", \"blue\")\n\nprint(colors[0])  # red\nprint(colors[2])  # blue\n",[17,38168,38169,38201,38205,38222],{"__ignoreMap":96},[100,38170,38171,38173,38175,38177,38179,38181,38183,38185,38187,38189,38191,38193,38195,38197,38199],{"class":102,"line":103},[100,38172,35257],{"class":106},[100,38174,111],{"class":110},[100,38176,16805],{"class":118},[100,38178,206],{"class":205},[100,38180,35266],{"class":209},[100,38182,206],{"class":205},[100,38184,126],{"class":118},[100,38186,1708],{"class":205},[100,38188,35275],{"class":209},[100,38190,206],{"class":205},[100,38192,126],{"class":118},[100,38194,1708],{"class":205},[100,38196,35284],{"class":209},[100,38198,206],{"class":205},[100,38200,215],{"class":118},[100,38202,38203],{"class":102,"line":135},[100,38204,139],{"emptyLinePlaceholder":138},[100,38206,38207,38209,38211,38213,38215,38217,38219],{"class":102,"line":142},[100,38208,372],{"class":114},[100,38210,170],{"class":118},[100,38212,37826],{"class":178},[100,38214,3166],{"class":118},[100,38216,2279],{"class":122},[100,38218,32896],{"class":118},[100,38220,38221],{"class":414},"  # red\n",[100,38223,38224,38226,38228,38230,38232,38234,38236],{"class":102,"line":152},[100,38225,372],{"class":114},[100,38227,170],{"class":118},[100,38229,37826],{"class":178},[100,38231,3166],{"class":118},[100,38233,559],{"class":122},[100,38235,32896],{"class":118},[100,38237,38238],{"class":414},"  # blue\n",[675,38240,38242,38243],{"id":38241},"check-the-tuple-length-with-len","Check the tuple length with ",[17,38244,32803],{},[14,38246,38247],{},"If the index may change, check it before accessing the tuple.",[91,38249,38251],{"className":93,"code":38250,"language":95,"meta":96,"style":96},"colors = (\"red\", \"green\", \"blue\")\nindex = 3\n\nif 0 \u003C= index \u003C len(colors):\n    print(colors[index])\nelse:\n    print(\"Index out of range\")\n",[17,38252,38253,38285,38293,38297,38317,38331,38337],{"__ignoreMap":96},[100,38254,38255,38257,38259,38261,38263,38265,38267,38269,38271,38273,38275,38277,38279,38281,38283],{"class":102,"line":103},[100,38256,35257],{"class":106},[100,38258,111],{"class":110},[100,38260,16805],{"class":118},[100,38262,206],{"class":205},[100,38264,35266],{"class":209},[100,38266,206],{"class":205},[100,38268,126],{"class":118},[100,38270,1708],{"class":205},[100,38272,35275],{"class":209},[100,38274,206],{"class":205},[100,38276,126],{"class":118},[100,38278,1708],{"class":205},[100,38280,35284],{"class":209},[100,38282,206],{"class":205},[100,38284,215],{"class":118},[100,38286,38287,38289,38291],{"class":102,"line":135},[100,38288,3116],{"class":106},[100,38290,111],{"class":110},[100,38292,31170],{"class":122},[100,38294,38295],{"class":102,"line":142},[100,38296,139],{"emptyLinePlaceholder":138},[100,38298,38299,38301,38303,38305,38307,38309,38311,38313,38315],{"class":102,"line":152},[100,38300,2736],{"class":145},[100,38302,1921],{"class":122},[100,38304,3133],{"class":110},[100,38306,3136],{"class":106},[100,38308,2782],{"class":110},[100,38310,3141],{"class":114},[100,38312,170],{"class":118},[100,38314,37826],{"class":178},[100,38316,522],{"class":118},[100,38318,38319,38321,38323,38325,38327,38329],{"class":102,"line":164},[100,38320,200],{"class":114},[100,38322,170],{"class":118},[100,38324,37826],{"class":178},[100,38326,3166],{"class":118},[100,38328,3169],{"class":178},[100,38330,132],{"class":118},[100,38332,38333,38335],{"class":102,"line":185},[100,38334,4944],{"class":145},[100,38336,149],{"class":118},[100,38338,38339,38341,38343,38345,38347,38349],{"class":102,"line":197},[100,38340,200],{"class":114},[100,38342,170],{"class":118},[100,38344,206],{"class":205},[100,38346,37896],{"class":209},[100,38348,206],{"class":205},[100,38350,215],{"class":118},[14,38352,38353],{},"This is safer when the tuple size is not guaranteed.",[14,38355,38356,38357,290],{},"If needed, read more about ",[295,38358,38360],{"href":38359},"\u002Freference\u002Fpython-tuple-length-len\u002F","Python tuple length with len()",[675,38362,38364],{"id":38363},"loop-over-the-tuple-directly","Loop over the tuple directly",[14,38366,38367],{},"If you want every item, you often do not need indexes at all.",[91,38369,38371],{"className":93,"code":38370,"language":95,"meta":96,"style":96},"colors = (\"red\", \"green\", \"blue\")\n\nfor color in colors:\n    print(color)\n",[17,38372,38373,38405,38409,38423],{"__ignoreMap":96},[100,38374,38375,38377,38379,38381,38383,38385,38387,38389,38391,38393,38395,38397,38399,38401,38403],{"class":102,"line":103},[100,38376,35257],{"class":106},[100,38378,111],{"class":110},[100,38380,16805],{"class":118},[100,38382,206],{"class":205},[100,38384,35266],{"class":209},[100,38386,206],{"class":205},[100,38388,126],{"class":118},[100,38390,1708],{"class":205},[100,38392,35275],{"class":209},[100,38394,206],{"class":205},[100,38396,126],{"class":118},[100,38398,1708],{"class":205},[100,38400,35284],{"class":209},[100,38402,206],{"class":205},[100,38404,215],{"class":118},[100,38406,38407],{"class":102,"line":135},[100,38408,139],{"emptyLinePlaceholder":138},[100,38410,38411,38413,38416,38418,38421],{"class":102,"line":142},[100,38412,71],{"class":145},[100,38414,38415],{"class":106}," color ",[100,38417,617],{"class":145},[100,38419,38420],{"class":106}," colors",[100,38422,149],{"class":118},[100,38424,38425,38427,38429,38432],{"class":102,"line":152},[100,38426,200],{"class":114},[100,38428,170],{"class":118},[100,38430,38431],{"class":178},"color",[100,38433,215],{"class":118},[14,38435,38436],{},"This avoids index mistakes completely.",[675,38438,241,38439,37049],{"id":33705},[17,38440,1575],{},[14,38442,38443,38444,38446],{},"If you need the position and the item, ",[17,38445,1575],{}," is safer than manual indexing.",[91,38448,38450],{"className":93,"code":38449,"language":95,"meta":96,"style":96},"colors = (\"red\", \"green\", \"blue\")\n\nfor i, color in enumerate(colors):\n    print(i, color)\n",[17,38451,38452,38484,38488,38508],{"__ignoreMap":96},[100,38453,38454,38456,38458,38460,38462,38464,38466,38468,38470,38472,38474,38476,38478,38480,38482],{"class":102,"line":103},[100,38455,35257],{"class":106},[100,38457,111],{"class":110},[100,38459,16805],{"class":118},[100,38461,206],{"class":205},[100,38463,35266],{"class":209},[100,38465,206],{"class":205},[100,38467,126],{"class":118},[100,38469,1708],{"class":205},[100,38471,35275],{"class":209},[100,38473,206],{"class":205},[100,38475,126],{"class":118},[100,38477,1708],{"class":205},[100,38479,35284],{"class":209},[100,38481,206],{"class":205},[100,38483,215],{"class":118},[100,38485,38486],{"class":102,"line":135},[100,38487,139],{"emptyLinePlaceholder":138},[100,38489,38490,38492,38494,38496,38498,38500,38502,38504,38506],{"class":102,"line":142},[100,38491,71],{"class":145},[100,38493,33762],{"class":106},[100,38495,126],{"class":118},[100,38497,38415],{"class":106},[100,38499,617],{"class":145},[100,38501,33771],{"class":114},[100,38503,170],{"class":118},[100,38505,37826],{"class":178},[100,38507,522],{"class":118},[100,38509,38510,38512,38514,38516,38518,38521],{"class":102,"line":152},[100,38511,200],{"class":114},[100,38513,170],{"class":118},[100,38515,29858],{"class":178},[100,38517,126],{"class":118},[100,38519,38520],{"class":178}," color",[100,38522,215],{"class":118},[14,38524,218],{},[91,38526,38528],{"className":93,"code":38527,"language":95,"meta":96,"style":96},"0 red\n1 green\n2 blue\n",[17,38529,38530,38537,38544],{"__ignoreMap":96},[100,38531,38532,38534],{"class":102,"line":103},[100,38533,2279],{"class":122},[100,38535,38536],{"class":106}," red\n",[100,38538,38539,38541],{"class":102,"line":135},[100,38540,123],{"class":122},[100,38542,38543],{"class":106}," green\n",[100,38545,38546,38548],{"class":102,"line":142},[100,38547,559],{"class":122},[100,38549,38550],{"class":106}," blue\n",[14,38552,14221,38553,290],{},[295,38554,35000],{"href":33712},[77,38556,3274],{"id":3273},[14,38558,38559],{},"When you see this error, check these things first:",[3282,38561,38562,38565,38568,38571],{},[43,38563,38564],{},"Print the tuple to see what it really contains",[43,38566,38567],{},"Print its length",[43,38569,38570],{},"Print the index value before the failing line",[43,38572,38573],{},"Confirm that your code is using 0-based indexing",[14,38575,5295],{},[91,38577,38579],{"className":93,"code":38578,"language":95,"meta":96,"style":96},"print(my_tuple)\nprint(len(my_tuple))\nprint(index)\n\nfor i, value in enumerate(my_tuple):\n    print(i, value)\n",[17,38580,38581,38592,38606,38616,38620,38640],{"__ignoreMap":96},[100,38582,38583,38585,38587,38590],{"class":102,"line":103},[100,38584,372],{"class":114},[100,38586,170],{"class":118},[100,38588,38589],{"class":178},"my_tuple",[100,38591,215],{"class":118},[100,38593,38594,38596,38598,38600,38602,38604],{"class":102,"line":135},[100,38595,372],{"class":114},[100,38597,170],{"class":118},[100,38599,17779],{"class":114},[100,38601,170],{"class":118},[100,38603,38589],{"class":178},[100,38605,182],{"class":118},[100,38607,38608,38610,38612,38614],{"class":102,"line":142},[100,38609,372],{"class":114},[100,38611,170],{"class":118},[100,38613,3169],{"class":178},[100,38615,215],{"class":118},[100,38617,38618],{"class":102,"line":152},[100,38619,139],{"emptyLinePlaceholder":138},[100,38621,38622,38624,38626,38628,38630,38632,38634,38636,38638],{"class":102,"line":164},[100,38623,71],{"class":145},[100,38625,33762],{"class":106},[100,38627,126],{"class":118},[100,38629,989],{"class":106},[100,38631,617],{"class":145},[100,38633,33771],{"class":114},[100,38635,170],{"class":118},[100,38637,38589],{"class":178},[100,38639,522],{"class":118},[100,38641,38642,38644,38646,38648,38650,38652],{"class":102,"line":185},[100,38643,200],{"class":114},[100,38645,170],{"class":118},[100,38647,29858],{"class":178},[100,38649,126],{"class":118},[100,38651,6747],{"class":178},[100,38653,215],{"class":118},[14,38655,1844],{},[91,38657,38659],{"className":93,"code":38658,"language":95,"meta":96,"style":96},"user = (\"Alice\", 25)\nindex = 2\n\nprint(user)\nprint(len(user))\nprint(index)\nprint(user[index])\n",[17,38660,38661,38681,38689,38693,38703,38717,38727],{"__ignoreMap":96},[100,38662,38663,38665,38667,38669,38671,38673,38675,38677,38679],{"class":102,"line":103},[100,38664,15336],{"class":106},[100,38666,111],{"class":110},[100,38668,16805],{"class":118},[100,38670,206],{"class":205},[100,38672,15343],{"class":209},[100,38674,206],{"class":205},[100,38676,126],{"class":118},[100,38678,16782],{"class":122},[100,38680,215],{"class":118},[100,38682,38683,38685,38687],{"class":102,"line":135},[100,38684,3116],{"class":106},[100,38686,111],{"class":110},[100,38688,3564],{"class":122},[100,38690,38691],{"class":102,"line":142},[100,38692,139],{"emptyLinePlaceholder":138},[100,38694,38695,38697,38699,38701],{"class":102,"line":152},[100,38696,372],{"class":114},[100,38698,170],{"class":118},[100,38700,15354],{"class":178},[100,38702,215],{"class":118},[100,38704,38705,38707,38709,38711,38713,38715],{"class":102,"line":164},[100,38706,372],{"class":114},[100,38708,170],{"class":118},[100,38710,17779],{"class":114},[100,38712,170],{"class":118},[100,38714,15354],{"class":178},[100,38716,182],{"class":118},[100,38718,38719,38721,38723,38725],{"class":102,"line":185},[100,38720,372],{"class":114},[100,38722,170],{"class":118},[100,38724,3169],{"class":178},[100,38726,215],{"class":118},[100,38728,38729,38731,38733,38735,38737,38739],{"class":102,"line":197},[100,38730,372],{"class":114},[100,38732,170],{"class":118},[100,38734,15354],{"class":178},[100,38736,3166],{"class":118},[100,38738,3169],{"class":178},[100,38740,132],{"class":118},[14,38742,38743],{},"Output before the error:",[91,38745,38747],{"className":93,"code":38746,"language":95,"meta":96,"style":96},"('Alice', 25)\n2\n2\n",[17,38748,38749,38765,38769],{"__ignoreMap":96},[100,38750,38751,38753,38755,38757,38759,38761,38763],{"class":102,"line":103},[100,38752,170],{"class":118},[100,38754,1280],{"class":205},[100,38756,15343],{"class":209},[100,38758,1280],{"class":205},[100,38760,126],{"class":118},[100,38762,16782],{"class":122},[100,38764,215],{"class":118},[100,38766,38767],{"class":102,"line":135},[100,38768,233],{"class":122},[100,38770,38771],{"class":102,"line":142},[100,38772,233],{"class":122},[14,38774,38775],{},"This tells you:",[40,38777,38778,38781,38788],{},[43,38779,38780],{},"The tuple has only 2 items",[43,38782,38783,38784,3266,38786],{},"Valid indexes are ",[17,38785,2279],{},[17,38787,123],{},[43,38789,33121,38790,38792],{},[17,38791,559],{}," is not valid",[77,38794,38796],{"id":38795},"special-case-negative-indexes","Special case: negative indexes",[14,38798,38799],{},"Negative indexes count from the end of the tuple:",[40,38801,38802,38808],{},[43,38803,38804,38807],{},[17,38805,38806],{},"my_tuple[-1]"," means the last item",[43,38809,38810,38813],{},[17,38811,38812],{},"my_tuple[-2]"," means the second-to-last item",[14,38815,1844],{},[91,38817,38819],{"className":93,"code":38818,"language":95,"meta":96,"style":96},"letters = (\"a\", \"b\", \"c\")\n\nprint(letters[-1])  # c\nprint(letters[-2])  # b\n",[17,38820,38821,38853,38857,38876],{"__ignoreMap":96},[100,38822,38823,38825,38827,38829,38831,38833,38835,38837,38839,38841,38843,38845,38847,38849,38851],{"class":102,"line":103},[100,38824,7960],{"class":106},[100,38826,111],{"class":110},[100,38828,16805],{"class":118},[100,38830,206],{"class":205},[100,38832,295],{"class":209},[100,38834,206],{"class":205},[100,38836,126],{"class":118},[100,38838,1708],{"class":205},[100,38840,4027],{"class":209},[100,38842,206],{"class":205},[100,38844,126],{"class":118},[100,38846,1708],{"class":205},[100,38848,4036],{"class":209},[100,38850,206],{"class":205},[100,38852,215],{"class":118},[100,38854,38855],{"class":102,"line":135},[100,38856,139],{"emptyLinePlaceholder":138},[100,38858,38859,38861,38863,38865,38867,38869,38871,38873],{"class":102,"line":142},[100,38860,372],{"class":114},[100,38862,170],{"class":118},[100,38864,7997],{"class":178},[100,38866,3166],{"class":118},[100,38868,2193],{"class":110},[100,38870,123],{"class":122},[100,38872,32896],{"class":118},[100,38874,38875],{"class":414},"  # c\n",[100,38877,38878,38880,38882,38884,38886,38888,38890,38892],{"class":102,"line":152},[100,38879,372],{"class":114},[100,38881,170],{"class":118},[100,38883,7997],{"class":178},[100,38885,3166],{"class":118},[100,38887,2193],{"class":110},[100,38889,559],{"class":122},[100,38891,32896],{"class":118},[100,38893,38894],{"class":414},"  # b\n",[14,38896,38897],{},"But negative indexes can still fail if the tuple is shorter than expected:",[91,38899,38901],{"className":93,"code":38900,"language":95,"meta":96,"style":96},"letters = (\"a\", \"b\", \"c\")\n\nprint(letters[-4])\n",[17,38902,38903,38935,38939],{"__ignoreMap":96},[100,38904,38905,38907,38909,38911,38913,38915,38917,38919,38921,38923,38925,38927,38929,38931,38933],{"class":102,"line":103},[100,38906,7960],{"class":106},[100,38908,111],{"class":110},[100,38910,16805],{"class":118},[100,38912,206],{"class":205},[100,38914,295],{"class":209},[100,38916,206],{"class":205},[100,38918,126],{"class":118},[100,38920,1708],{"class":205},[100,38922,4027],{"class":209},[100,38924,206],{"class":205},[100,38926,126],{"class":118},[100,38928,1708],{"class":205},[100,38930,4036],{"class":209},[100,38932,206],{"class":205},[100,38934,215],{"class":118},[100,38936,38937],{"class":102,"line":135},[100,38938,139],{"emptyLinePlaceholder":138},[100,38940,38941,38943,38945,38947,38949,38951,38953],{"class":102,"line":142},[100,38942,372],{"class":114},[100,38944,170],{"class":118},[100,38946,7997],{"class":178},[100,38948,3166],{"class":118},[100,38950,2193],{"class":110},[100,38952,8322],{"class":122},[100,38954,132],{"class":118},[14,38956,7710],{},[91,38958,38959],{"className":93,"code":38012,"language":95,"meta":96,"style":96},[17,38960,38961],{"__ignoreMap":96},[100,38962,38963,38965,38967,38969,38971],{"class":102,"line":103},[100,38964,16420],{"class":191},[100,38966,89],{"class":118},[100,38968,38023],{"class":191},[100,38970,16427],{"class":106},[100,38972,16430],{"class":114},[77,38974,38976],{"id":38975},"how-to-avoid-this-error-in-the-future","How to avoid this error in the future",[40,38978,38979,38982,38985,38988],{},[43,38980,38981],{},"Do not guess tuple positions",[43,38983,38984],{},"Check the structure of returned data before indexing",[43,38986,38987],{},"Prefer unpacking when you know the tuple size",[43,38989,38990],{},"Use loops for all items instead of hard-coded indexes",[14,38992,38993],{},"For example, unpacking is often clearer than manual indexing:",[91,38995,38997],{"className":93,"code":38996,"language":95,"meta":96,"style":96},"point = (10, 20)\n\nx, y = point\n\nprint(x)\nprint(y)\n",[17,38998,38999,39016,39020,39035,39039,39049],{"__ignoreMap":96},[100,39000,39001,39004,39006,39008,39010,39012,39014],{"class":102,"line":103},[100,39002,39003],{"class":106},"point ",[100,39005,111],{"class":110},[100,39007,16805],{"class":118},[100,39009,356],{"class":122},[100,39011,126],{"class":118},[100,39013,361],{"class":122},[100,39015,215],{"class":118},[100,39017,39018],{"class":102,"line":135},[100,39019,139],{"emptyLinePlaceholder":138},[100,39021,39022,39025,39027,39030,39032],{"class":102,"line":142},[100,39023,39024],{"class":106},"x",[100,39026,126],{"class":118},[100,39028,39029],{"class":106}," y ",[100,39031,111],{"class":110},[100,39033,39034],{"class":106}," point\n",[100,39036,39037],{"class":102,"line":152},[100,39038,139],{"emptyLinePlaceholder":138},[100,39040,39041,39043,39045,39047],{"class":102,"line":164},[100,39042,372],{"class":114},[100,39044,170],{"class":118},[100,39046,39024],{"class":178},[100,39048,215],{"class":118},[100,39050,39051,39053,39055,39058],{"class":102,"line":185},[100,39052,372],{"class":114},[100,39054,170],{"class":118},[100,39056,39057],{"class":178},"y",[100,39059,215],{"class":118},[14,39061,39062],{},"This is easier to read than:",[91,39064,39066],{"className":93,"code":39065,"language":95,"meta":96,"style":96},"point = (10, 20)\n\nprint(point[0])\nprint(point[1])\n",[17,39067,39068,39084,39088,39103],{"__ignoreMap":96},[100,39069,39070,39072,39074,39076,39078,39080,39082],{"class":102,"line":103},[100,39071,39003],{"class":106},[100,39073,111],{"class":110},[100,39075,16805],{"class":118},[100,39077,356],{"class":122},[100,39079,126],{"class":118},[100,39081,361],{"class":122},[100,39083,215],{"class":118},[100,39085,39086],{"class":102,"line":135},[100,39087,139],{"emptyLinePlaceholder":138},[100,39089,39090,39092,39094,39097,39099,39101],{"class":102,"line":142},[100,39091,372],{"class":114},[100,39093,170],{"class":118},[100,39095,39096],{"class":178},"point",[100,39098,3166],{"class":118},[100,39100,2279],{"class":122},[100,39102,132],{"class":118},[100,39104,39105,39107,39109,39111,39113,39115],{"class":102,"line":152},[100,39106,372],{"class":114},[100,39108,170],{"class":118},[100,39110,39096],{"class":178},[100,39112,3166],{"class":118},[100,39114,123],{"class":122},[100,39116,132],{"class":118},[77,39118,7117],{"id":7116},[14,39120,39121],{},"These are very common causes of this error:",[40,39123,39124,39130,39133,39139,39142],{},[43,39125,39126,39127,39129],{},"Accessing index ",[17,39128,11541],{}," in a tuple that only has 3 items",[43,39131,39132],{},"Using 1-based thinking instead of Python's 0-based indexing",[43,39134,39135,39136],{},"Looping with ",[17,39137,39138],{},"range(len(my_tuple) + 1)",[43,39140,39141],{},"Using a negative index smaller than the tuple allows",[43,39143,39144],{},"Reading function return values and assuming the tuple shape incorrectly",[14,39146,39147],{},"Here is a common loop mistake:",[91,39149,39151],{"className":93,"code":39150,"language":95,"meta":96,"style":96},"values = (10, 20, 30)\n\nfor i in range(len(values) + 1):\n    print(values[i])\n",[17,39152,39153,39173,39177,39203],{"__ignoreMap":96},[100,39154,39155,39157,39159,39161,39163,39165,39167,39169,39171],{"class":102,"line":103},[100,39156,4010],{"class":106},[100,39158,111],{"class":110},[100,39160,16805],{"class":118},[100,39162,356],{"class":122},[100,39164,126],{"class":118},[100,39166,361],{"class":122},[100,39168,126],{"class":118},[100,39170,16342],{"class":122},[100,39172,215],{"class":118},[100,39174,39175],{"class":102,"line":135},[100,39176,139],{"emptyLinePlaceholder":138},[100,39178,39179,39181,39183,39185,39187,39189,39191,39193,39195,39197,39199,39201],{"class":102,"line":142},[100,39180,71],{"class":145},[100,39182,29838],{"class":106},[100,39184,617],{"class":145},[100,39186,29843],{"class":114},[100,39188,170],{"class":118},[100,39190,17779],{"class":114},[100,39192,170],{"class":118},[100,39194,4049],{"class":178},[100,39196,6155],{"class":118},[100,39198,2895],{"class":110},[100,39200,6915],{"class":122},[100,39202,522],{"class":118},[100,39204,39205,39207,39209,39211,39213,39215],{"class":102,"line":152},[100,39206,200],{"class":114},[100,39208,170],{"class":118},[100,39210,4049],{"class":178},[100,39212,3166],{"class":118},[100,39214,29858],{"class":178},[100,39216,132],{"class":118},[14,39218,39219],{},"Why it fails:",[40,39221,39222,39229,39237],{},[43,39223,39224,2025,39227],{},[17,39225,39226],{},"len(values)",[17,39228,11541],{},[43,39230,39231,4812,39234],{},[17,39232,39233],{},"range(len(values) + 1)",[17,39235,39236],{},"0, 1, 2, 3",[43,39238,33121,39239,26280],{},[17,39240,11541],{},[14,39242,6031],{},[91,39244,39246],{"className":93,"code":39245,"language":95,"meta":96,"style":96},"values = (10, 20, 30)\n\nfor i in range(len(values)):\n    print(values[i])\n",[17,39247,39248,39268,39272,39292],{"__ignoreMap":96},[100,39249,39250,39252,39254,39256,39258,39260,39262,39264,39266],{"class":102,"line":103},[100,39251,4010],{"class":106},[100,39253,111],{"class":110},[100,39255,16805],{"class":118},[100,39257,356],{"class":122},[100,39259,126],{"class":118},[100,39261,361],{"class":122},[100,39263,126],{"class":118},[100,39265,16342],{"class":122},[100,39267,215],{"class":118},[100,39269,39270],{"class":102,"line":135},[100,39271,139],{"emptyLinePlaceholder":138},[100,39273,39274,39276,39278,39280,39282,39284,39286,39288,39290],{"class":102,"line":142},[100,39275,71],{"class":145},[100,39277,29838],{"class":106},[100,39279,617],{"class":145},[100,39281,29843],{"class":114},[100,39283,170],{"class":118},[100,39285,17779],{"class":114},[100,39287,170],{"class":118},[100,39289,4049],{"class":178},[100,39291,33665],{"class":118},[100,39293,39294,39296,39298,39300,39302,39304],{"class":102,"line":152},[100,39295,200],{"class":114},[100,39297,170],{"class":118},[100,39299,4049],{"class":178},[100,39301,3166],{"class":118},[100,39303,29858],{"class":178},[100,39305,132],{"class":118},[14,39307,39308],{},"Or even better:",[91,39310,39312],{"className":93,"code":39311,"language":95,"meta":96,"style":96},"values = (10, 20, 30)\n\nfor value in values:\n    print(value)\n",[17,39313,39314,39334,39338,39350],{"__ignoreMap":96},[100,39315,39316,39318,39320,39322,39324,39326,39328,39330,39332],{"class":102,"line":103},[100,39317,4010],{"class":106},[100,39319,111],{"class":110},[100,39321,16805],{"class":118},[100,39323,356],{"class":122},[100,39325,126],{"class":118},[100,39327,361],{"class":122},[100,39329,126],{"class":118},[100,39331,16342],{"class":122},[100,39333,215],{"class":118},[100,39335,39336],{"class":102,"line":135},[100,39337,139],{"emptyLinePlaceholder":138},[100,39339,39340,39342,39344,39346,39348],{"class":102,"line":142},[100,39341,71],{"class":145},[100,39343,989],{"class":106},[100,39345,617],{"class":145},[100,39347,32991],{"class":106},[100,39349,149],{"class":118},[100,39351,39352,39354,39356,39358],{"class":102,"line":152},[100,39353,200],{"class":114},[100,39355,170],{"class":118},[100,39357,757],{"class":178},[100,39359,215],{"class":118},[14,39361,39362,39363,290],{},"If you are seeing the same problem with a list, read ",[295,39364,17232],{"href":17231},[77,39366,1514],{"id":1513},[675,39368,39370],{"id":39369},"are-tuples-0-indexed-in-python","Are tuples 0-indexed in Python?",[14,39372,39373,39374,290],{},"Yes. The first item is at index ",[17,39375,2279],{},[675,39377,37471],{"id":37470},[14,39379,39380],{},"Yes. If the negative index goes beyond the start of the tuple, Python raises the same error.",[675,39382,39384],{"id":39383},"how-is-this-different-from-list-index-out-of-range","How is this different from list index out of range?",[14,39386,39387],{},"The cause is basically the same, but this error happens with tuples instead of lists.",[675,39389,39391,39392,39394],{"id":39390},"should-i-use-len-before-every-tuple-access","Should I use ",[17,39393,32803],{}," before every tuple access?",[14,39396,39397],{},"Not always. Use it when the tuple size may change or when the index is not guaranteed to be valid.",[77,39399,1554],{"id":1553},[40,39401,39402,39406,39410,39414,39418],{},[43,39403,39404],{},[295,39405,37947],{"href":37946},[43,39407,39408],{},[295,39409,38061],{"href":38060},[43,39411,39412],{},[295,39413,38360],{"href":38359},[43,39415,39416],{},[295,39417,35000],{"href":33712},[43,39419,39420],{},[295,39421,37671],{"href":37670},[1589,39423,39424],{},"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 .sP7_E, html code.shiki .sP7_E{--shiki-light:#39ADB5;--shiki-default:#24292E;--shiki-dark:#E1E4E8}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 .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 pre.shiki code .srdBf, html code.shiki .srdBf{--shiki-light:#F76D47;--shiki-default:#005CC5;--shiki-dark:#79B8FF}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 .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}",{"title":96,"searchDepth":135,"depth":135,"links":39426},[39427,39428,39429,39430,39431,39438,39439,39440,39441,39442,39449],{"id":79,"depth":135,"text":80},{"id":5880,"depth":135,"text":5881},{"id":11030,"depth":135,"text":11031},{"id":25090,"depth":135,"text":25091},{"id":6243,"depth":135,"text":6244,"children":39432},[39433,39434,39436,39437],{"id":38159,"depth":142,"text":38160},{"id":38241,"depth":142,"text":39435},"Check the tuple length with len()",{"id":38363,"depth":142,"text":38364},{"id":33705,"depth":142,"text":33706},{"id":3273,"depth":135,"text":3274},{"id":38795,"depth":135,"text":38796},{"id":38975,"depth":135,"text":38976},{"id":7116,"depth":135,"text":7117},{"id":1513,"depth":135,"text":1514,"children":39443},[39444,39445,39446,39447],{"id":39369,"depth":142,"text":39370},{"id":37470,"depth":142,"text":37471},{"id":39383,"depth":142,"text":39384},{"id":39390,"depth":142,"text":39448},"Should I use len() before every tuple access?",{"id":1553,"depth":135,"text":1554},"Master indexerror tuple index out of range fix in our comprehensive Python beginner guide.",{},"\u002Ferrors\u002Findexerror-tuple-index-out-of-range-fix",{"title":37745,"description":39450},"errors\u002Findexerror-tuple-index-out-of-range-fix","GZNZHOnCDdv_IXbjU7QS5BmYbUrSJAh-j2XHnGNahK4",{"id":39457,"title":39458,"body":39459,"description":41087,"extension":1623,"meta":41088,"navigation":138,"path":23069,"seo":41089,"stem":41090,"__hash__":41091},"content\u002Ferrors\u002Fisadirectoryerror-errno-21-is-a-directory-fix.md","IsADirectoryError: [Errno 21] Is a directory (Fix)",{"type":7,"value":39460,"toc":41066},[39461,39466,39479,39485,39487,39561,39569,39571,39573,39592,39595,39613,39616,39634,39639,39641,39643,39665,39667,39692,39694,39696,39745,39751,39782,39785,39788,39847,39855,39857,39860,39883,39886,39998,40001,40005,40008,40089,40092,40112,40114,40261,40266,40270,40273,40276,40367,40370,40382,40385,40555,40558,40575,40581,40583,40586,40588,40711,40714,40749,40752,40916,40919,40935,40937,40940,40962,40965,40991,40993,40997,41000,41004,41010,41014,41019,41023,41031,41035,41038,41040,41063],[10,39462,20632,39464,20636],{"id":39463},"isadirectoryerror-errno-21-is-a-directory-fix",[100,39465,20635],{},[14,39467,39468,39471,39472,39475,39476,290],{},[17,39469,39470],{},"IsADirectoryError: [Errno 21] Is a directory"," happens when your Python code tries to open, read, write, or otherwise use a ",[22,39473,39474],{},"folder path"," where Python expects a ",[22,39477,39478],{},"file path",[14,39480,39481,39482,39484],{},"This is a common beginner error when working with ",[17,39483,18032],{},", file paths, or folder loops. The fix is usually simple: check the path, confirm whether it points to a file or a directory, and use the right operation for that path.",[77,39486,80],{"id":79},[91,39488,39490],{"className":93,"code":39489,"language":95,"meta":96,"style":96},"path = \"data.txt\"  # use a file path, not a folder path\n\nwith open(path, \"r\") as file:\n    content = file.read()\n    print(content)\n",[17,39491,39492,39507,39511,39537,39551],{"__ignoreMap":96},[100,39493,39494,39496,39498,39500,39502,39504],{"class":102,"line":103},[100,39495,19365],{"class":106},[100,39497,111],{"class":110},[100,39499,1708],{"class":205},[100,39501,18094],{"class":209},[100,39503,206],{"class":205},[100,39505,39506],{"class":414},"  # use a file path, not a folder path\n",[100,39508,39509],{"class":102,"line":135},[100,39510,139],{"emptyLinePlaceholder":138},[100,39512,39513,39515,39517,39519,39521,39523,39525,39527,39529,39531,39533,39535],{"class":102,"line":142},[100,39514,17521],{"class":145},[100,39516,17524],{"class":114},[100,39518,170],{"class":118},[100,39520,19017],{"class":178},[100,39522,126],{"class":118},[100,39524,1708],{"class":205},[100,39526,17540],{"class":209},[100,39528,206],{"class":205},[100,39530,6155],{"class":118},[100,39532,16224],{"class":145},[100,39534,17550],{"class":17549},[100,39536,149],{"class":118},[100,39538,39539,39541,39543,39545,39547,39549],{"class":102,"line":152},[100,39540,18414],{"class":106},[100,39542,111],{"class":110},[100,39544,17550],{"class":17549},[100,39546,290],{"class":118},[100,39548,17566],{"class":178},[100,39550,4734],{"class":118},[100,39552,39553,39555,39557,39559],{"class":102,"line":164},[100,39554,200],{"class":114},[100,39556,170],{"class":118},[100,39558,18182],{"class":178},[100,39560,215],{"class":118},[14,39562,39563,39564,39566,39567,290],{},"This error usually means the path points to a ",[22,39565,20130],{}," instead of a ",[22,39568,17597],{},[77,39570,5881],{"id":5880},[14,39572,5884],{},[40,39574,39575,39578,39581,39589],{},[43,39576,39577],{},"It expected a file path",[43,39579,39580],{},"The path you gave points to a directory (folder)",[43,39582,39583,39584,5870,39586,39588],{},"This often happens with ",[17,39585,18032],{},[17,39587,18264],{},", or similar file operations",[43,39590,39591],{},"A directory and a file are different things in Python and in your operating system",[14,39593,39594],{},"For example, this path is a directory:",[91,39596,39598],{"className":93,"code":39597,"language":95,"meta":96,"style":96},"path = \"my_folder\"\n",[17,39599,39600],{"__ignoreMap":96},[100,39601,39602,39604,39606,39608,39611],{"class":102,"line":103},[100,39603,19365],{"class":106},[100,39605,111],{"class":110},[100,39607,1708],{"class":205},[100,39609,39610],{"class":209},"my_folder",[100,39612,1714],{"class":205},[14,39614,39615],{},"But this path is a file:",[91,39617,39619],{"className":93,"code":39618,"language":95,"meta":96,"style":96},"path = \"my_folder\u002Fdata.txt\"\n",[17,39620,39621],{"__ignoreMap":96},[100,39622,39623,39625,39627,39629,39632],{"class":102,"line":103},[100,39624,19365],{"class":106},[100,39626,111],{"class":110},[100,39628,1708],{"class":205},[100,39630,39631],{"class":209},"my_folder\u002Fdata.txt",[100,39633,1714],{"class":205},[14,39635,39636,39637,290],{},"If you want to learn more about how file paths work, see ",[295,39638,18286],{"href":18285},[77,39640,7741],{"id":7740},[14,39642,13201],{},[40,39644,39645,39650,39653,39656,39659],{},[43,39646,39647,39648],{},"You passed a folder name to ",[17,39649,18032],{},[43,39651,39652],{},"You built the path incorrectly",[43,39654,39655],{},"You used a variable that contains a directory path instead of a file path",[43,39657,39658],{},"You looped through directory names and tried to open each one as a file",[43,39660,39661,39662,39664],{},"You expected a file like ",[17,39663,18094],{}," but only used the parent folder path",[675,39666,1344],{"id":1343},[40,39668,39669,39674,39680,39683,39686],{},[43,39670,1357,39671,39673],{},[17,39672,18032],{}," with a folder path",[43,39675,39676,39677,39679],{},"Passing a directory to ",[17,39678,18264],{}," or another file-only function",[43,39681,39682],{},"Forgetting to include the file name in the path",[43,39684,39685],{},"Incorrectly joining paths in loops",[43,39687,39688,39689,39691],{},"Reading items from ",[17,39690,18259],{}," without checking their type",[77,39693,11031],{"id":11030},[14,39695,1891],{},[91,39697,39699],{"className":93,"code":39698,"language":95,"meta":96,"style":96},"with open(\"my_folder\", \"r\") as file:\n    content = file.read()\n",[17,39700,39701,39731],{"__ignoreMap":96},[100,39702,39703,39705,39707,39709,39711,39713,39715,39717,39719,39721,39723,39725,39727,39729],{"class":102,"line":103},[100,39704,17521],{"class":145},[100,39706,17524],{"class":114},[100,39708,170],{"class":118},[100,39710,206],{"class":205},[100,39712,39610],{"class":209},[100,39714,206],{"class":205},[100,39716,126],{"class":118},[100,39718,1708],{"class":205},[100,39720,17540],{"class":209},[100,39722,206],{"class":205},[100,39724,6155],{"class":118},[100,39726,16224],{"class":145},[100,39728,17550],{"class":17549},[100,39730,149],{"class":118},[100,39732,39733,39735,39737,39739,39741,39743],{"class":102,"line":135},[100,39734,18414],{"class":106},[100,39736,111],{"class":110},[100,39738,17550],{"class":17549},[100,39740,290],{"class":118},[100,39742,17566],{"class":178},[100,39744,4734],{"class":118},[14,39746,11734,39747,39750],{},[17,39748,39749],{},"\"my_folder\""," is a real folder, Python raises:",[91,39752,39754],{"className":93,"code":39753,"language":95,"meta":96,"style":96},"IsADirectoryError: [Errno 21] Is a directory: 'my_folder'\n",[17,39755,39756],{"__ignoreMap":96},[100,39757,39758,39760,39762,39764,39766,39769,39771,39774,39776,39778,39780],{"class":102,"line":103},[100,39759,17499],{"class":191},[100,39761,89],{"class":118},[100,39763,594],{"class":118},[100,39765,17586],{"class":106},[100,39767,39768],{"class":122},"21",[100,39770,17591],{"class":118},[100,39772,39773],{"class":106}," Is a directory",[100,39775,89],{"class":118},[100,39777,1274],{"class":205},[100,39779,39610],{"class":209},[100,39781,3925],{"class":205},[14,39783,39784],{},"Python cannot read a folder as if it were a text file.",[14,39786,39787],{},"The fix is to open a real file inside that folder:",[91,39789,39791],{"className":93,"code":39790,"language":95,"meta":96,"style":96},"with open(\"my_folder\u002Fdata.txt\", \"r\") as file:\n    content = file.read()\n    print(content)\n",[17,39792,39793,39823,39837],{"__ignoreMap":96},[100,39794,39795,39797,39799,39801,39803,39805,39807,39809,39811,39813,39815,39817,39819,39821],{"class":102,"line":103},[100,39796,17521],{"class":145},[100,39798,17524],{"class":114},[100,39800,170],{"class":118},[100,39802,206],{"class":205},[100,39804,39631],{"class":209},[100,39806,206],{"class":205},[100,39808,126],{"class":118},[100,39810,1708],{"class":205},[100,39812,17540],{"class":209},[100,39814,206],{"class":205},[100,39816,6155],{"class":118},[100,39818,16224],{"class":145},[100,39820,17550],{"class":17549},[100,39822,149],{"class":118},[100,39824,39825,39827,39829,39831,39833,39835],{"class":102,"line":135},[100,39826,18414],{"class":106},[100,39828,111],{"class":110},[100,39830,17550],{"class":17549},[100,39832,290],{"class":118},[100,39834,17566],{"class":178},[100,39836,4734],{"class":118},[100,39838,39839,39841,39843,39845],{"class":102,"line":142},[100,39840,200],{"class":114},[100,39842,170],{"class":118},[100,39844,18182],{"class":178},[100,39846,215],{"class":118},[14,39848,39849,39850,3178,39852,290],{},"If you need a refresher on file reading, see ",[295,39851,21207],{"href":20590},[295,39853,39854],{"href":24735},"the Python open() function explained",[77,39856,6244],{"id":6243},[14,39858,39859],{},"When you see this error, go through these checks:",[40,39861,39862,39865,39868,39874,39880],{},[43,39863,39864],{},"Check the exact path stored in your variable",[43,39866,39867],{},"Make sure the path includes the file name and extension",[43,39869,241,39870,39873],{},[17,39871,39872],{},"os.path.isfile()"," before opening a path",[43,39875,241,39876,39879],{},[17,39877,39878],{},"os.path.isdir()"," if you want to work with folders",[43,39881,39882],{},"List files in the folder first, then open one file at a time",[14,39884,39885],{},"A safe pattern looks like this:",[91,39887,39889],{"className":93,"code":39888,"language":95,"meta":96,"style":96},"import os\n\npath = \"my_folder\u002Fdata.txt\"\n\nif os.path.isfile(path):\n    with open(path, \"r\") as file:\n        print(file.read())\nelse:\n    print(\"This path is not a file.\")\n",[17,39890,39891,39897,39901,39913,39917,39937,39963,39977,39983],{"__ignoreMap":96},[100,39892,39893,39895],{"class":102,"line":103},[100,39894,9259],{"class":145},[100,39896,18702],{"class":106},[100,39898,39899],{"class":102,"line":135},[100,39900,139],{"emptyLinePlaceholder":138},[100,39902,39903,39905,39907,39909,39911],{"class":102,"line":142},[100,39904,19365],{"class":106},[100,39906,111],{"class":110},[100,39908,1708],{"class":205},[100,39910,39631],{"class":209},[100,39912,1714],{"class":205},[100,39914,39915],{"class":102,"line":152},[100,39916,139],{"emptyLinePlaceholder":138},[100,39918,39919,39921,39923,39925,39927,39929,39931,39933,39935],{"class":102,"line":164},[100,39920,2736],{"class":145},[100,39922,19012],{"class":106},[100,39924,290],{"class":118},[100,39926,19017],{"class":2494},[100,39928,290],{"class":118},[100,39930,24191],{"class":178},[100,39932,170],{"class":118},[100,39934,19017],{"class":178},[100,39936,522],{"class":118},[100,39938,39939,39941,39943,39945,39947,39949,39951,39953,39955,39957,39959,39961],{"class":102,"line":185},[100,39940,18121],{"class":145},[100,39942,17524],{"class":114},[100,39944,170],{"class":118},[100,39946,19017],{"class":178},[100,39948,126],{"class":118},[100,39950,1708],{"class":205},[100,39952,17540],{"class":209},[100,39954,206],{"class":205},[100,39956,6155],{"class":118},[100,39958,16224],{"class":145},[100,39960,17550],{"class":17549},[100,39962,149],{"class":118},[100,39964,39965,39967,39969,39971,39973,39975],{"class":102,"line":197},[100,39966,167],{"class":114},[100,39968,170],{"class":118},[100,39970,17597],{"class":17549},[100,39972,290],{"class":118},[100,39974,17566],{"class":178},[100,39976,3370],{"class":118},[100,39978,39979,39981],{"class":102,"line":771},[100,39980,4944],{"class":145},[100,39982,149],{"class":118},[100,39984,39985,39987,39989,39991,39994,39996],{"class":102,"line":787},[100,39986,200],{"class":114},[100,39988,170],{"class":118},[100,39990,206],{"class":205},[100,39992,39993],{"class":209},"This path is not a file.",[100,39995,206],{"class":205},[100,39997,215],{"class":118},[14,39999,40000],{},"This prevents your code from trying to open a directory as a file.",[77,40002,40004],{"id":40003},"fix-by-checking-file-vs-directory","Fix by checking file vs directory",[14,40006,40007],{},"When a path might be either a file or a folder, test it first.",[91,40009,40011],{"className":93,"code":40010,"language":95,"meta":96,"style":96},"import os\n\npath = \"my_folder\"\n\nprint(os.path.isfile(path))  # True if it is a file\nprint(os.path.isdir(path))   # True if it is a directory\n",[17,40012,40013,40019,40023,40035,40039,40064],{"__ignoreMap":96},[100,40014,40015,40017],{"class":102,"line":103},[100,40016,9259],{"class":145},[100,40018,18702],{"class":106},[100,40020,40021],{"class":102,"line":135},[100,40022,139],{"emptyLinePlaceholder":138},[100,40024,40025,40027,40029,40031,40033],{"class":102,"line":142},[100,40026,19365],{"class":106},[100,40028,111],{"class":110},[100,40030,1708],{"class":205},[100,40032,39610],{"class":209},[100,40034,1714],{"class":205},[100,40036,40037],{"class":102,"line":152},[100,40038,139],{"emptyLinePlaceholder":138},[100,40040,40041,40043,40045,40047,40049,40051,40053,40055,40057,40059,40061],{"class":102,"line":164},[100,40042,372],{"class":114},[100,40044,170],{"class":118},[100,40046,18035],{"class":178},[100,40048,290],{"class":118},[100,40050,19017],{"class":2494},[100,40052,290],{"class":118},[100,40054,24191],{"class":178},[100,40056,170],{"class":118},[100,40058,19017],{"class":178},[100,40060,411],{"class":118},[100,40062,40063],{"class":414},"  # True if it is a file\n",[100,40065,40066,40068,40070,40072,40074,40076,40078,40080,40082,40084,40086],{"class":102,"line":185},[100,40067,372],{"class":114},[100,40069,170],{"class":118},[100,40071,18035],{"class":178},[100,40073,290],{"class":118},[100,40075,19017],{"class":2494},[100,40077,290],{"class":118},[100,40079,24218],{"class":178},[100,40081,170],{"class":118},[100,40083,19017],{"class":178},[100,40085,411],{"class":118},[100,40087,40088],{"class":414},"   # True if it is a directory\n",[14,40090,40091],{},"This helps because:",[40,40093,40094,40100,40106,40109],{},[43,40095,40096,40099],{},[17,40097,40098],{},"os.path.isfile(path)"," confirms it is a file",[43,40101,40102,40105],{},[17,40103,40104],{},"os.path.isdir(path)"," confirms it is a directory",[43,40107,40108],{},"You can choose the correct operation",[43,40110,40111],{},"It is especially useful when paths come from user input or loops",[14,40113,1844],{},[91,40115,40117],{"className":93,"code":40116,"language":95,"meta":96,"style":96},"import os\n\npath = \"my_folder\"\n\nif os.path.isfile(path):\n    with open(path, \"r\") as file:\n        print(file.read())\nelif os.path.isdir(path):\n    print(\"This is a folder, not a file.\")\nelse:\n    print(\"The path does not exist.\")\n",[17,40118,40119,40125,40129,40141,40145,40165,40191,40205,40225,40240,40246],{"__ignoreMap":96},[100,40120,40121,40123],{"class":102,"line":103},[100,40122,9259],{"class":145},[100,40124,18702],{"class":106},[100,40126,40127],{"class":102,"line":135},[100,40128,139],{"emptyLinePlaceholder":138},[100,40130,40131,40133,40135,40137,40139],{"class":102,"line":142},[100,40132,19365],{"class":106},[100,40134,111],{"class":110},[100,40136,1708],{"class":205},[100,40138,39610],{"class":209},[100,40140,1714],{"class":205},[100,40142,40143],{"class":102,"line":152},[100,40144,139],{"emptyLinePlaceholder":138},[100,40146,40147,40149,40151,40153,40155,40157,40159,40161,40163],{"class":102,"line":164},[100,40148,2736],{"class":145},[100,40150,19012],{"class":106},[100,40152,290],{"class":118},[100,40154,19017],{"class":2494},[100,40156,290],{"class":118},[100,40158,24191],{"class":178},[100,40160,170],{"class":118},[100,40162,19017],{"class":178},[100,40164,522],{"class":118},[100,40166,40167,40169,40171,40173,40175,40177,40179,40181,40183,40185,40187,40189],{"class":102,"line":185},[100,40168,18121],{"class":145},[100,40170,17524],{"class":114},[100,40172,170],{"class":118},[100,40174,19017],{"class":178},[100,40176,126],{"class":118},[100,40178,1708],{"class":205},[100,40180,17540],{"class":209},[100,40182,206],{"class":205},[100,40184,6155],{"class":118},[100,40186,16224],{"class":145},[100,40188,17550],{"class":17549},[100,40190,149],{"class":118},[100,40192,40193,40195,40197,40199,40201,40203],{"class":102,"line":197},[100,40194,167],{"class":114},[100,40196,170],{"class":118},[100,40198,17597],{"class":17549},[100,40200,290],{"class":118},[100,40202,17566],{"class":178},[100,40204,3370],{"class":118},[100,40206,40207,40209,40211,40213,40215,40217,40219,40221,40223],{"class":102,"line":771},[100,40208,30862],{"class":145},[100,40210,19012],{"class":106},[100,40212,290],{"class":118},[100,40214,19017],{"class":2494},[100,40216,290],{"class":118},[100,40218,24218],{"class":178},[100,40220,170],{"class":118},[100,40222,19017],{"class":178},[100,40224,522],{"class":118},[100,40226,40227,40229,40231,40233,40236,40238],{"class":102,"line":787},[100,40228,200],{"class":114},[100,40230,170],{"class":118},[100,40232,206],{"class":205},[100,40234,40235],{"class":209},"This is a folder, not a file.",[100,40237,206],{"class":205},[100,40239,215],{"class":118},[100,40241,40242,40244],{"class":102,"line":5816},[100,40243,4944],{"class":145},[100,40245,149],{"class":118},[100,40247,40248,40250,40252,40254,40257,40259],{"class":102,"line":5833},[100,40249,200],{"class":114},[100,40251,170],{"class":118},[100,40253,206],{"class":205},[100,40255,40256],{"class":209},"The path does not exist.",[100,40258,206],{"class":205},[100,40260,215],{"class":118},[14,40262,40263,40264,290],{},"You can also check whether a path exists first with ",[295,40265,19482],{"href":19525},[77,40267,40269],{"id":40268},"fix-when-looping-through-folder-contents","Fix when looping through folder contents",[14,40271,40272],{},"This error often happens in loops.",[14,40274,40275],{},"A beginner might write code like this:",[91,40277,40279],{"className":93,"code":40278,"language":95,"meta":96,"style":96},"import os\n\nfolder = \"my_folder\"\n\nfor name in os.listdir(folder):\n    with open(name, \"r\") as file:\n        print(file.read())\n",[17,40280,40281,40287,40291,40303,40307,40327,40353],{"__ignoreMap":96},[100,40282,40283,40285],{"class":102,"line":103},[100,40284,9259],{"class":145},[100,40286,18702],{"class":106},[100,40288,40289],{"class":102,"line":135},[100,40290,139],{"emptyLinePlaceholder":138},[100,40292,40293,40295,40297,40299,40301],{"class":102,"line":142},[100,40294,20110],{"class":106},[100,40296,111],{"class":110},[100,40298,1708],{"class":205},[100,40300,39610],{"class":209},[100,40302,1714],{"class":205},[100,40304,40305],{"class":102,"line":152},[100,40306,139],{"emptyLinePlaceholder":138},[100,40308,40309,40311,40313,40315,40317,40319,40321,40323,40325],{"class":102,"line":164},[100,40310,71],{"class":145},[100,40312,4915],{"class":106},[100,40314,617],{"class":145},[100,40316,19012],{"class":106},[100,40318,290],{"class":118},[100,40320,22871],{"class":178},[100,40322,170],{"class":118},[100,40324,20130],{"class":178},[100,40326,522],{"class":118},[100,40328,40329,40331,40333,40335,40337,40339,40341,40343,40345,40347,40349,40351],{"class":102,"line":185},[100,40330,18121],{"class":145},[100,40332,17524],{"class":114},[100,40334,170],{"class":118},[100,40336,2853],{"class":178},[100,40338,126],{"class":118},[100,40340,1708],{"class":205},[100,40342,17540],{"class":209},[100,40344,206],{"class":205},[100,40346,6155],{"class":118},[100,40348,16224],{"class":145},[100,40350,17550],{"class":17549},[100,40352,149],{"class":118},[100,40354,40355,40357,40359,40361,40363,40365],{"class":102,"line":197},[100,40356,167],{"class":114},[100,40358,170],{"class":118},[100,40360,17597],{"class":17549},[100,40362,290],{"class":118},[100,40364,17566],{"class":178},[100,40366,3370],{"class":118},[14,40368,40369],{},"There are two problems here:",[40,40371,40372,40377],{},[43,40373,40374,40376],{},[17,40375,2853],{}," may be a directory, not a file",[43,40378,40379,40381],{},[17,40380,2853],{}," is not joined to the folder path",[14,40383,40384],{},"A better version is:",[91,40386,40388],{"className":93,"code":40387,"language":95,"meta":96,"style":96},"import os\n\nfolder = \"my_folder\"\n\nfor name in os.listdir(folder):\n    path = os.path.join(folder, name)\n\n    if os.path.isfile(path):\n        with open(path, \"r\") as file:\n            print(file.read())\n    else:\n        print(f\"Skipping directory: {path}\")\n",[17,40389,40390,40396,40400,40412,40416,40436,40463,40467,40487,40513,40527,40534],{"__ignoreMap":96},[100,40391,40392,40394],{"class":102,"line":103},[100,40393,9259],{"class":145},[100,40395,18702],{"class":106},[100,40397,40398],{"class":102,"line":135},[100,40399,139],{"emptyLinePlaceholder":138},[100,40401,40402,40404,40406,40408,40410],{"class":102,"line":142},[100,40403,20110],{"class":106},[100,40405,111],{"class":110},[100,40407,1708],{"class":205},[100,40409,39610],{"class":209},[100,40411,1714],{"class":205},[100,40413,40414],{"class":102,"line":152},[100,40415,139],{"emptyLinePlaceholder":138},[100,40417,40418,40420,40422,40424,40426,40428,40430,40432,40434],{"class":102,"line":164},[100,40419,71],{"class":145},[100,40421,4915],{"class":106},[100,40423,617],{"class":145},[100,40425,19012],{"class":106},[100,40427,290],{"class":118},[100,40429,22871],{"class":178},[100,40431,170],{"class":118},[100,40433,20130],{"class":178},[100,40435,522],{"class":118},[100,40437,40438,40441,40443,40445,40447,40449,40451,40453,40455,40457,40459,40461],{"class":102,"line":185},[100,40439,40440],{"class":106},"    path ",[100,40442,111],{"class":110},[100,40444,19012],{"class":106},[100,40446,290],{"class":118},[100,40448,19017],{"class":2494},[100,40450,290],{"class":118},[100,40452,19022],{"class":178},[100,40454,170],{"class":118},[100,40456,20130],{"class":178},[100,40458,126],{"class":118},[100,40460,1703],{"class":178},[100,40462,215],{"class":118},[100,40464,40465],{"class":102,"line":197},[100,40466,139],{"emptyLinePlaceholder":138},[100,40468,40469,40471,40473,40475,40477,40479,40481,40483,40485],{"class":102,"line":771},[100,40470,2777],{"class":145},[100,40472,19012],{"class":106},[100,40474,290],{"class":118},[100,40476,19017],{"class":2494},[100,40478,290],{"class":118},[100,40480,24191],{"class":178},[100,40482,170],{"class":118},[100,40484,19017],{"class":178},[100,40486,522],{"class":118},[100,40488,40489,40491,40493,40495,40497,40499,40501,40503,40505,40507,40509,40511],{"class":102,"line":787},[100,40490,23332],{"class":145},[100,40492,17524],{"class":114},[100,40494,170],{"class":118},[100,40496,19017],{"class":178},[100,40498,126],{"class":118},[100,40500,1708],{"class":205},[100,40502,17540],{"class":209},[100,40504,206],{"class":205},[100,40506,6155],{"class":118},[100,40508,16224],{"class":145},[100,40510,17550],{"class":17549},[100,40512,149],{"class":118},[100,40514,40515,40517,40519,40521,40523,40525],{"class":102,"line":5816},[100,40516,23371],{"class":114},[100,40518,170],{"class":118},[100,40520,17597],{"class":17549},[100,40522,290],{"class":118},[100,40524,17566],{"class":178},[100,40526,3370],{"class":118},[100,40528,40529,40532],{"class":102,"line":5833},[100,40530,40531],{"class":145},"    else",[100,40533,149],{"class":118},[100,40535,40536,40538,40540,40542,40545,40547,40549,40551,40553],{"class":102,"line":5848},[100,40537,167],{"class":114},[100,40539,170],{"class":118},[100,40541,18199],{"class":1077},[100,40543,40544],{"class":209},"\"Skipping directory: ",[100,40546,16490],{"class":122},[100,40548,19017],{"class":178},[100,40550,18216],{"class":122},[100,40552,206],{"class":209},[100,40554,215],{"class":118},[14,40556,40557],{},"This works better because:",[40,40559,40560,40563,40569,40572],{},[43,40561,40562],{},"Some folders contain subfolders as well as files",[43,40564,40565,40566,40568],{},"It does not assume every item from ",[17,40567,18259],{}," is a file",[43,40570,40571],{},"It joins the folder path and item name correctly",[43,40573,40574],{},"It skips directories or handles them separately",[14,40576,1160,40577,290],{},[295,40578,40580],{"href":40579},"\u002Fstandard-library\u002Fos.listdir-function-explained","os.listdir() explained",[77,40582,3274],{"id":3273},[14,40584,40585],{},"If you are not sure why the error is happening, check the path before the failing line.",[14,40587,5295],{},[91,40589,40591],{"className":93,"code":40590,"language":95,"meta":96,"style":96},"print(path)\n\nimport os\nprint(os.path.exists(path))\nprint(os.path.isfile(path))\nprint(os.path.isdir(path))\nprint(os.getcwd())\nprint(os.listdir(path))\n",[17,40592,40593,40603,40607,40613,40635,40657,40679,40693],{"__ignoreMap":96},[100,40594,40595,40597,40599,40601],{"class":102,"line":103},[100,40596,372],{"class":114},[100,40598,170],{"class":118},[100,40600,19017],{"class":178},[100,40602,215],{"class":118},[100,40604,40605],{"class":102,"line":135},[100,40606,139],{"emptyLinePlaceholder":138},[100,40608,40609,40611],{"class":102,"line":142},[100,40610,9259],{"class":145},[100,40612,18702],{"class":106},[100,40614,40615,40617,40619,40621,40623,40625,40627,40629,40631,40633],{"class":102,"line":152},[100,40616,372],{"class":114},[100,40618,170],{"class":118},[100,40620,18035],{"class":178},[100,40622,290],{"class":118},[100,40624,19017],{"class":2494},[100,40626,290],{"class":118},[100,40628,18114],{"class":178},[100,40630,170],{"class":118},[100,40632,19017],{"class":178},[100,40634,182],{"class":118},[100,40636,40637,40639,40641,40643,40645,40647,40649,40651,40653,40655],{"class":102,"line":164},[100,40638,372],{"class":114},[100,40640,170],{"class":118},[100,40642,18035],{"class":178},[100,40644,290],{"class":118},[100,40646,19017],{"class":2494},[100,40648,290],{"class":118},[100,40650,24191],{"class":178},[100,40652,170],{"class":118},[100,40654,19017],{"class":178},[100,40656,182],{"class":118},[100,40658,40659,40661,40663,40665,40667,40669,40671,40673,40675,40677],{"class":102,"line":185},[100,40660,372],{"class":114},[100,40662,170],{"class":118},[100,40664,18035],{"class":178},[100,40666,290],{"class":118},[100,40668,19017],{"class":2494},[100,40670,290],{"class":118},[100,40672,24218],{"class":178},[100,40674,170],{"class":118},[100,40676,19017],{"class":178},[100,40678,182],{"class":118},[100,40680,40681,40683,40685,40687,40689,40691],{"class":102,"line":197},[100,40682,372],{"class":114},[100,40684,170],{"class":118},[100,40686,18035],{"class":178},[100,40688,290],{"class":118},[100,40690,18719],{"class":178},[100,40692,3370],{"class":118},[100,40694,40695,40697,40699,40701,40703,40705,40707,40709],{"class":102,"line":771},[100,40696,372],{"class":114},[100,40698,170],{"class":118},[100,40700,18035],{"class":178},[100,40702,290],{"class":118},[100,40704,22871],{"class":178},[100,40706,170],{"class":118},[100,40708,19017],{"class":178},[100,40710,182],{"class":118},[14,40712,40713],{},"Here is what each one helps you find:",[40,40715,40716,40722,40728,40733,40738,40743],{},[43,40717,40718,40721],{},[17,40719,40720],{},"print(path)"," shows the exact value being used",[43,40723,40724,40727],{},[17,40725,40726],{},"os.path.exists(path)"," tells you whether the path exists",[43,40729,40730,40732],{},[17,40731,40098],{}," tells you whether it is a file",[43,40734,40735,40737],{},[17,40736,40104],{}," tells you whether it is a folder",[43,40739,40740,40742],{},[17,40741,19603],{}," shows the current working directory",[43,40744,40745,40748],{},[17,40746,40747],{},"os.listdir(path)"," shows the contents of a folder",[14,40750,40751],{},"A simple debugging example:",[91,40753,40755],{"className":93,"code":40754,"language":95,"meta":96,"style":96},"import os\n\npath = \"my_folder\"\n\nprint(\"Path:\", path)\nprint(\"Exists:\", os.path.exists(path))\nprint(\"Is file:\", os.path.isfile(path))\nprint(\"Is directory:\", os.path.isdir(path))\nprint(\"Current working directory:\", os.getcwd())\n",[17,40756,40757,40763,40767,40779,40783,40802,40832,40863,40894],{"__ignoreMap":96},[100,40758,40759,40761],{"class":102,"line":103},[100,40760,9259],{"class":145},[100,40762,18702],{"class":106},[100,40764,40765],{"class":102,"line":135},[100,40766,139],{"emptyLinePlaceholder":138},[100,40768,40769,40771,40773,40775,40777],{"class":102,"line":142},[100,40770,19365],{"class":106},[100,40772,111],{"class":110},[100,40774,1708],{"class":205},[100,40776,39610],{"class":209},[100,40778,1714],{"class":205},[100,40780,40781],{"class":102,"line":152},[100,40782,139],{"emptyLinePlaceholder":138},[100,40784,40785,40787,40789,40791,40794,40796,40798,40800],{"class":102,"line":164},[100,40786,372],{"class":114},[100,40788,170],{"class":118},[100,40790,206],{"class":205},[100,40792,40793],{"class":209},"Path:",[100,40795,206],{"class":205},[100,40797,126],{"class":118},[100,40799,23294],{"class":178},[100,40801,215],{"class":118},[100,40803,40804,40806,40808,40810,40812,40814,40816,40818,40820,40822,40824,40826,40828,40830],{"class":102,"line":185},[100,40805,372],{"class":114},[100,40807,170],{"class":118},[100,40809,206],{"class":205},[100,40811,22993],{"class":209},[100,40813,206],{"class":205},[100,40815,126],{"class":118},[100,40817,19012],{"class":178},[100,40819,290],{"class":118},[100,40821,19017],{"class":2494},[100,40823,290],{"class":118},[100,40825,18114],{"class":178},[100,40827,170],{"class":118},[100,40829,19017],{"class":178},[100,40831,182],{"class":118},[100,40833,40834,40836,40838,40840,40843,40845,40847,40849,40851,40853,40855,40857,40859,40861],{"class":102,"line":197},[100,40835,372],{"class":114},[100,40837,170],{"class":118},[100,40839,206],{"class":205},[100,40841,40842],{"class":209},"Is file:",[100,40844,206],{"class":205},[100,40846,126],{"class":118},[100,40848,19012],{"class":178},[100,40850,290],{"class":118},[100,40852,19017],{"class":2494},[100,40854,290],{"class":118},[100,40856,24191],{"class":178},[100,40858,170],{"class":118},[100,40860,19017],{"class":178},[100,40862,182],{"class":118},[100,40864,40865,40867,40869,40871,40874,40876,40878,40880,40882,40884,40886,40888,40890,40892],{"class":102,"line":771},[100,40866,372],{"class":114},[100,40868,170],{"class":118},[100,40870,206],{"class":205},[100,40872,40873],{"class":209},"Is directory:",[100,40875,206],{"class":205},[100,40877,126],{"class":118},[100,40879,19012],{"class":178},[100,40881,290],{"class":118},[100,40883,19017],{"class":2494},[100,40885,290],{"class":118},[100,40887,24218],{"class":178},[100,40889,170],{"class":118},[100,40891,19017],{"class":178},[100,40893,182],{"class":118},[100,40895,40896,40898,40900,40902,40904,40906,40908,40910,40912,40914],{"class":102,"line":787},[100,40897,372],{"class":114},[100,40899,170],{"class":118},[100,40901,206],{"class":205},[100,40903,23735],{"class":209},[100,40905,206],{"class":205},[100,40907,126],{"class":118},[100,40909,19012],{"class":178},[100,40911,290],{"class":118},[100,40913,18719],{"class":178},[100,40915,3370],{"class":118},[14,40917,40918],{},"Use these steps to look for:",[40,40920,40921,40924,40927,40932],{},[43,40922,40923],{},"Missing file names",[43,40925,40926],{},"Wrong path joins",[43,40928,40929,40930],{},"A folder path being used with ",[17,40931,18032],{},[43,40933,40934],{},"The script running in a different directory than you expected",[77,40936,30537],{"id":30536},[14,40938,40939],{},"This error is part of a larger group of file system errors.",[40,40941,40942,40947,40952,40957],{},[43,40943,40944,40946],{},[17,40945,17489],{}," happens when the path does not exist",[43,40948,40949,40951],{},[17,40950,17494],{}," happens when the path exists but access is denied",[43,40953,40954,40956],{},[17,40955,17504],{}," happens when code expects a folder but gets a file",[43,40958,40959,40961],{},[17,40960,17509],{}," is the broader error family that includes this error",[14,40963,40964],{},"Related pages:",[40,40966,40967,40975,40984],{},[43,40968,40969],{},[295,40970,18016,40972,40974],{"href":40971},"\u002Ferrors\u002Ffile-not-found-error-errno-2-no-such-file-or-directory-fix\u002F",[100,40973,18019],{}," No such file or directory",[43,40976,40977],{},[295,40978,40979,40980,40983],{"href":23081},"NotADirectoryError: ",[100,40981,40982],{},"Errno 20"," Not a directory",[43,40985,40986],{},[295,40987,20471,40989,23913],{"href":40988},"\u002Ferrors\u002Fpermission-error-errno-13-permission-denied-fix\u002F",[100,40990,20474],{},[77,40992,1514],{"id":1513},[675,40994,40996],{"id":40995},"what-does-isadirectoryerror-mean-in-python","What does IsADirectoryError mean in Python?",[14,40998,40999],{},"It means your code used a folder path where Python expected a file path.",[675,41001,41003],{"id":41002},"why-does-open-say-is-a-directory","Why does open() say Is a directory?",[14,41005,41006,41007,41009],{},"Because the path passed to ",[17,41008,18032],{}," points to a directory instead of a file.",[675,41011,41013],{"id":41012},"how-do-i-fix-errno-21-is-a-directory","How do I fix Errno 21 Is a directory?",[14,41015,41016,41017,290],{},"Use the full path to a real file, or check the path first with ",[17,41018,39872],{},[675,41020,41022],{"id":41021},"how-can-i-tell-if-a-path-is-a-file-or-folder","How can I tell if a path is a file or folder?",[14,41024,241,41025,41027,41028,41030],{},[17,41026,40098],{}," for files and ",[17,41029,40104],{}," for folders.",[675,41032,41034],{"id":41033},"can-oslistdir-cause-this-error","Can os.listdir() cause this error?",[14,41036,41037],{},"Yes. If you loop through items and try to open every item, some may be directories.",[77,41039,1554],{"id":1553},[40,41041,41042,41046,41050,41054,41059],{},[43,41043,41044],{},[295,41045,20591],{"href":20590},[43,41047,41048],{},[295,41049,20596],{"href":18285},[43,41051,41052],{},[295,41053,24741],{"href":19525},[43,41055,41056],{},[295,41057,41058],{"href":40579},"os.listdir() function explained",[43,41060,41061],{},[295,41062,24736],{"href":24735},[1589,41064,41065],{},"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 .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 .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 .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 pre.shiki code .sMMDD, html code.shiki .sMMDD{--shiki-light:#90A4AE;--shiki-default:#E36209;--shiki-dark:#FFAB70}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 .skxfh, html code.shiki .skxfh{--shiki-light:#E53935;--shiki-default:#24292E;--shiki-dark:#E1E4E8}html pre.shiki code .sbsja, html code.shiki .sbsja{--shiki-light:#9C3EDA;--shiki-default:#D73A49;--shiki-dark:#F97583}",{"title":96,"searchDepth":135,"depth":135,"links":41067},[41068,41069,41070,41073,41074,41075,41076,41077,41078,41079,41086],{"id":79,"depth":135,"text":80},{"id":5880,"depth":135,"text":5881},{"id":7740,"depth":135,"text":7741,"children":41071},[41072],{"id":1343,"depth":142,"text":1344},{"id":11030,"depth":135,"text":11031},{"id":6243,"depth":135,"text":6244},{"id":40003,"depth":135,"text":40004},{"id":40268,"depth":135,"text":40269},{"id":3273,"depth":135,"text":3274},{"id":30536,"depth":135,"text":30537},{"id":1513,"depth":135,"text":1514,"children":41080},[41081,41082,41083,41084,41085],{"id":40995,"depth":142,"text":40996},{"id":41002,"depth":142,"text":41003},{"id":41012,"depth":142,"text":41013},{"id":41021,"depth":142,"text":41022},{"id":41033,"depth":142,"text":41034},{"id":1553,"depth":135,"text":1554},"Master isadirectoryerror errno 21 is a directory fix in our comprehensive Python beginner guide.",{},{"title":39458,"description":41087},"errors\u002Fisadirectoryerror-errno-21-is-a-directory-fix","EVmqdrTqNmTJXJu9QIelnSD5wIp2RObh0jxA5Wjfbic",{"id":41093,"title":41094,"body":41095,"description":42295,"extension":1623,"meta":42296,"navigation":138,"path":42297,"seo":42298,"stem":42299,"__hash__":42300},"content\u002Ferrors\u002Fkeyboardinterrupt-exception-in-python-explained.md","KeyboardInterrupt Exception in Python Explained",{"type":7,"value":41096,"toc":42271},[41097,41100,41114,41124,41126,41132,41192,41195,41199,41204,41207,41226,41231,41235,41239,41262,41269,41273,41276,41304,41307,41315,41318,41373,41376,41382,41384,41400,41407,41411,41416,41427,41430,41434,41440,41443,41447,41452,41508,41514,41518,41521,41524,41552,41555,41626,41629,41633,41639,41739,41742,41746,41749,41808,41811,41815,41818,41821,41838,41843,41928,41930,41948,41954,41962,41968,41970,41975,42006,42009,42041,42043,42063,42068,42072,42077,42079,42099,42108,42115,42117,42119,42136,42138,42142,42145,42149,42158,42216,42220,42228,42232,42235,42237,42268],[10,41098,41094],{"id":41099},"keyboardinterrupt-exception-in-python-explained",[14,41101,41102,41105,41106,41109,41110,41113],{},[17,41103,41104],{},"KeyboardInterrupt"," is a Python exception that happens when ",[22,41107,41108],{},"you stop a running program from the keyboard",". In most cases, this means you pressed ",[22,41111,41112],{},"Ctrl+C"," in the terminal.",[14,41115,41116,41117,41119,41120,41123],{},"This page explains what ",[17,41118,41104],{}," means, when it appears, and how to handle it safely. It is usually ",[22,41121,41122],{},"not a bug in your code",". Instead, it means the program was interrupted by the user.",[77,41125,80],{"id":79},[14,41127,41128,41129,41131],{},"If you want your program to stop cleanly when the user presses Ctrl+C, wrap the code in a ",[17,41130,244],{}," block:",[91,41133,41135],{"className":93,"code":41134,"language":95,"meta":96,"style":96},"try:\n    while True:\n        print(\"Running...\")\nexcept KeyboardInterrupt:\n    print(\"\\nProgram stopped by user\")\n",[17,41136,41137,41143,41151,41166,41175],{"__ignoreMap":96},[100,41138,41139,41141],{"class":102,"line":103},[100,41140,146],{"class":145},[100,41142,149],{"class":118},[100,41144,41145,41147,41149],{"class":102,"line":135},[100,41146,155],{"class":145},[100,41148,159],{"class":158},[100,41150,149],{"class":118},[100,41152,41153,41155,41157,41159,41162,41164],{"class":102,"line":142},[100,41154,167],{"class":114},[100,41156,170],{"class":118},[100,41158,206],{"class":205},[100,41160,41161],{"class":209},"Running...",[100,41163,206],{"class":205},[100,41165,215],{"class":118},[100,41167,41168,41170,41173],{"class":102,"line":152},[100,41169,188],{"class":145},[100,41171,41172],{"class":191}," KeyboardInterrupt",[100,41174,149],{"class":118},[100,41176,41177,41179,41181,41183,41185,41188,41190],{"class":102,"line":164},[100,41178,200],{"class":114},[100,41180,170],{"class":118},[100,41182,206],{"class":205},[100,41184,20187],{"class":527},[100,41186,41187],{"class":209},"Program stopped by user",[100,41189,206],{"class":205},[100,41191,215],{"class":118},[14,41193,41194],{},"Use this when you want to avoid a long traceback and show a friendly exit message instead.",[77,41196,41198],{"id":41197},"what-keyboardinterrupt-means","What KeyboardInterrupt means",[14,41200,41201,41203],{},[17,41202,41104],{}," is a built-in exception in Python.",[14,41205,41206],{},"It means:",[40,41208,41209,41212,41217,41223],{},[43,41210,41211],{},"The program was interrupted from the keyboard",[43,41213,41214,41215],{},"This usually happens when you press ",[22,41216,41112],{},[43,41218,41219,41220],{},"It is ",[22,41221,41222],{},"not usually caused by a coding mistake",[43,41224,41225],{},"It often appears when a program is stuck in a loop, waiting for something, or running for a long time",[14,41227,41228,41229,290],{},"If you are new to exceptions, see ",[295,41230,1587],{"href":1586},[77,41232,41234],{"id":41233},"when-this-exception-happens","When this exception happens",[14,41236,26292,41237,26295],{},[17,41238,41104],{},[40,41240,41241,41246,41249,41252,41259],{},[43,41242,41243,41244,32343],{},"During an infinite ",[17,41245,721],{},[43,41247,41248],{},"While waiting for user input",[43,41250,41251],{},"During a long-running task",[43,41253,41254,41255,41258],{},"While ",[17,41256,41257],{},"time.sleep()"," is pausing the program",[43,41260,41261],{},"When a script is processing a lot of data and you stop it manually",[14,41263,41264,41265,290],{},"For example, a script may keep running until you interrupt it because it has no normal stop condition. This is common with ",[295,41266,41268],{"href":41267},"\u002Flearn\u002Fpython-while-loops-explained","Python while loops",[77,41270,41272],{"id":41271},"example-that-causes-keyboardinterrupt","Example that causes KeyboardInterrupt",[14,41274,41275],{},"Here is a simple program that keeps running forever:",[91,41277,41279],{"className":93,"code":41278,"language":95,"meta":96,"style":96},"while True:\n    print(\"Still running...\")\n",[17,41280,41281,41289],{"__ignoreMap":96},[100,41282,41283,41285,41287],{"class":102,"line":103},[100,41284,721],{"class":145},[100,41286,159],{"class":158},[100,41288,149],{"class":118},[100,41290,41291,41293,41295,41297,41300,41302],{"class":102,"line":135},[100,41292,200],{"class":114},[100,41294,170],{"class":118},[100,41296,206],{"class":205},[100,41298,41299],{"class":209},"Still running...",[100,41301,206],{"class":205},[100,41303,215],{"class":118},[14,41305,41306],{},"This loop has no end. If you run it in a terminal, it will continue printing until you stop it.",[14,41308,41309,41310,41312,41313,290],{},"A common way to stop it is to press ",[22,41311,41112],{},". When you do, Python may show a traceback ending with ",[17,41314,41104],{},[14,41316,41317],{},"A more realistic example uses a small delay:",[91,41319,41321],{"className":93,"code":41320,"language":95,"meta":96,"style":96},"import time\n\nwhile True:\n    print(\"Working...\")\n    time.sleep(1)\n",[17,41322,41323,41330,41334,41342,41357],{"__ignoreMap":96},[100,41324,41325,41327],{"class":102,"line":103},[100,41326,9259],{"class":145},[100,41328,41329],{"class":106}," time\n",[100,41331,41332],{"class":102,"line":135},[100,41333,139],{"emptyLinePlaceholder":138},[100,41335,41336,41338,41340],{"class":102,"line":142},[100,41337,721],{"class":145},[100,41339,159],{"class":158},[100,41341,149],{"class":118},[100,41343,41344,41346,41348,41350,41353,41355],{"class":102,"line":152},[100,41345,200],{"class":114},[100,41347,170],{"class":118},[100,41349,206],{"class":205},[100,41351,41352],{"class":209},"Working...",[100,41354,206],{"class":205},[100,41356,215],{"class":118},[100,41358,41359,41362,41364,41367,41369,41371],{"class":102,"line":164},[100,41360,41361],{"class":106},"    time",[100,41363,290],{"class":118},[100,41365,41366],{"class":178},"sleep",[100,41368,170],{"class":118},[100,41370,123],{"class":122},[100,41372,215],{"class":118},[14,41374,41375],{},"Possible output before interruption:",[91,41377,41380],{"className":41378,"code":41379,"language":4468,"meta":96},[26775],"Working...\nWorking...\nWorking...\n^CTraceback (most recent call last):\n  ...\nKeyboardInterrupt\n",[17,41381,41379],{"__ignoreMap":96},[14,41383,30338],{},[40,41385,41386,41391,41397],{},[43,41387,41388,41390],{},[17,41389,55],{}," creates an infinite loop",[43,41392,41393,41396],{},[17,41394,41395],{},"time.sleep(1)"," pauses for 1 second each time",[43,41398,41399],{},"Pressing Ctrl+C interrupts the program while it is running or sleeping",[14,41401,41402,41403,290],{},"If you want to understand that delay function better, see ",[295,41404,41406],{"href":41405},"\u002Fstandard-library\u002Ftime.sleep-function-explained","time.sleep() explained",[77,41408,41410],{"id":41409},"how-to-fix-or-handle-it","How to fix or handle it",[14,41412,41413,41415],{},[17,41414,41104],{}," usually does not need a “fix” in the same way as a normal error. Instead, you decide whether the program should:",[40,41417,41418,41421,41424],{},[43,41419,41420],{},"Stop immediately",[43,41422,41423],{},"Stop cleanly with a message",[43,41425,41426],{},"Save data before exiting",[14,41428,41429],{},"Here are the main ways to handle it.",[675,41431,41433],{"id":41432},"_1-press-ctrlc-only-when-you-want-to-stop-the-program","1. Press Ctrl+C only when you want to stop the program",[14,41435,41436,41437,41439],{},"If you manually interrupted the script, then ",[17,41438,41104],{}," is expected.",[14,41441,41442],{},"In that case, nothing may be wrong. You simply stopped the program on purpose.",[675,41444,41446],{"id":41445},"_2-catch-keyboardinterrupt-for-a-clean-exit","2. Catch KeyboardInterrupt for a clean exit",[14,41448,241,41449,41451],{},[17,41450,244],{}," if you want to show a friendly message instead of a traceback.",[91,41453,41454],{"className":93,"code":41134,"language":95,"meta":96,"style":96},[17,41455,41456,41462,41470,41484,41492],{"__ignoreMap":96},[100,41457,41458,41460],{"class":102,"line":103},[100,41459,146],{"class":145},[100,41461,149],{"class":118},[100,41463,41464,41466,41468],{"class":102,"line":135},[100,41465,155],{"class":145},[100,41467,159],{"class":158},[100,41469,149],{"class":118},[100,41471,41472,41474,41476,41478,41480,41482],{"class":102,"line":142},[100,41473,167],{"class":114},[100,41475,170],{"class":118},[100,41477,206],{"class":205},[100,41479,41161],{"class":209},[100,41481,206],{"class":205},[100,41483,215],{"class":118},[100,41485,41486,41488,41490],{"class":102,"line":152},[100,41487,188],{"class":145},[100,41489,41172],{"class":191},[100,41491,149],{"class":118},[100,41493,41494,41496,41498,41500,41502,41504,41506],{"class":102,"line":164},[100,41495,200],{"class":114},[100,41497,170],{"class":118},[100,41499,206],{"class":205},[100,41501,20187],{"class":527},[100,41503,41187],{"class":209},[100,41505,206],{"class":205},[100,41507,215],{"class":118},[14,41509,41510,41511,290],{},"This is a simple use of exception handling. If you need more help with the syntax, see ",[295,41512,12762],{"href":41513},"\u002Flearn\u002Fusing-try-except-else-and-finally-in-python",[675,41515,41517],{"id":41516},"_3-use-a-proper-loop-condition-when-possible","3. Use a proper loop condition when possible",[14,41519,41520],{},"Infinite loops are sometimes useful, but many programs should stop normally.",[14,41522,41523],{},"Instead of this:",[91,41525,41527],{"className":93,"code":41526,"language":95,"meta":96,"style":96},"while True:\n    print(\"Checking...\")\n",[17,41528,41529,41537],{"__ignoreMap":96},[100,41530,41531,41533,41535],{"class":102,"line":103},[100,41532,721],{"class":145},[100,41534,159],{"class":158},[100,41536,149],{"class":118},[100,41538,41539,41541,41543,41545,41548,41550],{"class":102,"line":135},[100,41540,200],{"class":114},[100,41542,170],{"class":118},[100,41544,206],{"class":205},[100,41546,41547],{"class":209},"Checking...",[100,41549,206],{"class":205},[100,41551,215],{"class":118},[14,41553,41554],{},"You can often use a condition:",[91,41556,41558],{"className":93,"code":41557,"language":95,"meta":96,"style":96},"count = 0\n\nwhile count \u003C 3:\n    print(\"Checking...\")\n    count += 1\n\nprint(\"Done\")\n",[17,41559,41560,41568,41572,41584,41598,41608,41612],{"__ignoreMap":96},[100,41561,41562,41564,41566],{"class":102,"line":103},[100,41563,2176],{"class":106},[100,41565,111],{"class":110},[100,41567,2069],{"class":122},[100,41569,41570],{"class":102,"line":135},[100,41571,139],{"emptyLinePlaceholder":138},[100,41573,41574,41576,41578,41580,41582],{"class":102,"line":142},[100,41575,721],{"class":145},[100,41577,2190],{"class":106},[100,41579,2782],{"class":110},[100,41581,1405],{"class":122},[100,41583,149],{"class":118},[100,41585,41586,41588,41590,41592,41594,41596],{"class":102,"line":152},[100,41587,200],{"class":114},[100,41589,170],{"class":118},[100,41591,206],{"class":205},[100,41593,41547],{"class":209},[100,41595,206],{"class":205},[100,41597,215],{"class":118},[100,41599,41600,41603,41606],{"class":102,"line":164},[100,41601,41602],{"class":106},"    count ",[100,41604,41605],{"class":110},"+=",[100,41607,1093],{"class":122},[100,41609,41610],{"class":102,"line":185},[100,41611,139],{"emptyLinePlaceholder":138},[100,41613,41614,41616,41618,41620,41622,41624],{"class":102,"line":197},[100,41615,372],{"class":114},[100,41617,170],{"class":118},[100,41619,206],{"class":205},[100,41621,31802],{"class":209},[100,41623,206],{"class":205},[100,41625,215],{"class":118},[14,41627,41628],{},"This version ends by itself and does not need manual interruption.",[675,41630,41632],{"id":41631},"_4-add-a-break-condition","4. Add a break condition",[14,41634,41635,41636,41638],{},"If you do need ",[17,41637,55],{},", add a way to exit the loop.",[91,41640,41642],{"className":93,"code":41641,"language":95,"meta":96,"style":96},"while True:\n    text = input(\"Type quit to stop: \")\n\n    if text == \"quit\":\n        break\n\n    print(\"You typed:\", text)\n\nprint(\"Program ended normally\")\n",[17,41643,41644,41652,41672,41676,41693,41697,41701,41720,41724],{"__ignoreMap":96},[100,41645,41646,41648,41650],{"class":102,"line":103},[100,41647,721],{"class":145},[100,41649,159],{"class":158},[100,41651,149],{"class":118},[100,41653,41654,41657,41659,41661,41663,41665,41668,41670],{"class":102,"line":135},[100,41655,41656],{"class":106},"    text ",[100,41658,111],{"class":110},[100,41660,7221],{"class":114},[100,41662,170],{"class":118},[100,41664,206],{"class":205},[100,41666,41667],{"class":209},"Type quit to stop: ",[100,41669,206],{"class":205},[100,41671,215],{"class":118},[100,41673,41674],{"class":102,"line":142},[100,41675,139],{"emptyLinePlaceholder":138},[100,41677,41678,41680,41682,41684,41686,41689,41691],{"class":102,"line":152},[100,41679,2777],{"class":145},[100,41681,15060],{"class":106},[100,41683,32169],{"class":110},[100,41685,1708],{"class":205},[100,41687,41688],{"class":209},"quit",[100,41690,206],{"class":205},[100,41692,149],{"class":118},[100,41694,41695],{"class":102,"line":164},[100,41696,790],{"class":145},[100,41698,41699],{"class":102,"line":185},[100,41700,139],{"emptyLinePlaceholder":138},[100,41702,41703,41705,41707,41709,41712,41714,41716,41718],{"class":102,"line":197},[100,41704,200],{"class":114},[100,41706,170],{"class":118},[100,41708,206],{"class":205},[100,41710,41711],{"class":209},"You typed:",[100,41713,206],{"class":205},[100,41715,126],{"class":118},[100,41717,4350],{"class":178},[100,41719,215],{"class":118},[100,41721,41722],{"class":102,"line":771},[100,41723,139],{"emptyLinePlaceholder":138},[100,41725,41726,41728,41730,41732,41735,41737],{"class":102,"line":787},[100,41727,372],{"class":114},[100,41729,170],{"class":118},[100,41731,206],{"class":205},[100,41733,41734],{"class":209},"Program ended normally",[100,41736,206],{"class":205},[100,41738,215],{"class":118},[14,41740,41741],{},"This lets the user stop the program without pressing Ctrl+C.",[675,41743,41745],{"id":41744},"_5-save-work-before-exiting-if-needed","5. Save work before exiting if needed",[14,41747,41748],{},"If your program writes to a file, stores data, or keeps a network connection open, you may want to clean up before exit.",[91,41750,41752],{"className":93,"code":41751,"language":95,"meta":96,"style":96},"try:\n    while True:\n        print(\"Program is running...\")\nexcept KeyboardInterrupt:\n    print(\"\\nSaving progress before exit...\")\n",[17,41753,41754,41760,41768,41783,41791],{"__ignoreMap":96},[100,41755,41756,41758],{"class":102,"line":103},[100,41757,146],{"class":145},[100,41759,149],{"class":118},[100,41761,41762,41764,41766],{"class":102,"line":135},[100,41763,155],{"class":145},[100,41765,159],{"class":158},[100,41767,149],{"class":118},[100,41769,41770,41772,41774,41776,41779,41781],{"class":102,"line":142},[100,41771,167],{"class":114},[100,41773,170],{"class":118},[100,41775,206],{"class":205},[100,41777,41778],{"class":209},"Program is running...",[100,41780,206],{"class":205},[100,41782,215],{"class":118},[100,41784,41785,41787,41789],{"class":102,"line":152},[100,41786,188],{"class":145},[100,41788,41172],{"class":191},[100,41790,149],{"class":118},[100,41792,41793,41795,41797,41799,41801,41804,41806],{"class":102,"line":164},[100,41794,200],{"class":114},[100,41796,170],{"class":118},[100,41798,206],{"class":205},[100,41800,20187],{"class":527},[100,41802,41803],{"class":209},"Saving progress before exit...",[100,41805,206],{"class":205},[100,41807,215],{"class":118},[14,41809,41810],{},"For some programs, a clean shutdown is important.",[77,41812,41814],{"id":41813},"how-to-exit-cleanly","How to exit cleanly",[14,41816,41817],{},"A clean exit means your program stops in a controlled way.",[14,41819,41820],{},"This is helpful when you want to:",[40,41822,41823,41826,41829,41832,41835],{},[43,41824,41825],{},"Print a friendly message",[43,41827,41828],{},"Close files",[43,41830,41831],{},"Close connections",[43,41833,41834],{},"Save progress",[43,41836,41837],{},"Always run cleanup code",[14,41839,41840,41841,89],{},"Here is a simple example with ",[17,41842,28983],{},[91,41844,41846],{"className":93,"code":41845,"language":95,"meta":96,"style":96},"try:\n    print(\"Program started\")\n    while True:\n        pass\nexcept KeyboardInterrupt:\n    print(\"\\nInterrupted by user\")\nfinally:\n    print(\"Cleanup code runs before exit\")\n",[17,41847,41848,41854,41869,41877,41882,41890,41907,41913],{"__ignoreMap":96},[100,41849,41850,41852],{"class":102,"line":103},[100,41851,146],{"class":145},[100,41853,149],{"class":118},[100,41855,41856,41858,41860,41862,41865,41867],{"class":102,"line":135},[100,41857,200],{"class":114},[100,41859,170],{"class":118},[100,41861,206],{"class":205},[100,41863,41864],{"class":209},"Program started",[100,41866,206],{"class":205},[100,41868,215],{"class":118},[100,41870,41871,41873,41875],{"class":102,"line":142},[100,41872,155],{"class":145},[100,41874,159],{"class":158},[100,41876,149],{"class":118},[100,41878,41879],{"class":102,"line":152},[100,41880,41881],{"class":145},"        pass\n",[100,41883,41884,41886,41888],{"class":102,"line":164},[100,41885,188],{"class":145},[100,41887,41172],{"class":191},[100,41889,149],{"class":118},[100,41891,41892,41894,41896,41898,41900,41903,41905],{"class":102,"line":185},[100,41893,200],{"class":114},[100,41895,170],{"class":118},[100,41897,206],{"class":205},[100,41899,20187],{"class":527},[100,41901,41902],{"class":209},"Interrupted by user",[100,41904,206],{"class":205},[100,41906,215],{"class":118},[100,41908,41909,41911],{"class":102,"line":197},[100,41910,28983],{"class":145},[100,41912,149],{"class":118},[100,41914,41915,41917,41919,41921,41924,41926],{"class":102,"line":771},[100,41916,200],{"class":114},[100,41918,170],{"class":118},[100,41920,206],{"class":205},[100,41922,41923],{"class":209},"Cleanup code runs before exit",[100,41925,206],{"class":205},[100,41927,215],{"class":118},[14,41929,26197],{},[40,41931,41932,41937,41943],{},[43,41933,41934,41936],{},[17,41935,146],{}," runs the main code",[43,41938,41939,41942],{},[17,41940,41941],{},"except KeyboardInterrupt"," handles Ctrl+C",[43,41944,41945,41947],{},[17,41946,28983],{}," runs whether an interruption happens or not",[14,41949,41950,41951,41953],{},"If something must always happen before the program ends, ",[17,41952,28983],{}," is a good choice.",[14,41955,41956,41957,41961],{},"In some programs, you may also choose to exit intentionally with ",[295,41958,41960],{"href":41959},"\u002Fstandard-library\u002Fsys.exit-function-explained","sys.exit()",", but that is different from a user pressing Ctrl+C.",[14,41963,41964,41965,41967],{},"Also, avoid catching ",[17,41966,41104],{}," and doing nothing unless you have a good reason. Hiding the interruption can make your program harder to stop and harder to debug.",[77,41969,3274],{"id":3273},[14,41971,1171,41972,41974],{},[17,41973,41104],{}," and are not sure why, try these steps:",[40,41976,41977,41983,41986,41989,41994,42001],{},[43,41978,41979,41980],{},"Check whether ",[22,41981,41982],{},"you pressed Ctrl+C",[43,41984,41985],{},"Look for infinite loops",[43,41987,41988],{},"Check for slow code that seems stuck",[43,41990,19653,41991,41993],{},[17,41992,14906],{}," calls waiting for user input",[43,41995,41996,41997,42000],{},"Check for long ",[17,41998,41999],{},"sleep()"," delays",[43,42002,6342,42003,42005],{},[17,42004,13235],{}," statements to see where the program stops",[14,42007,42008],{},"Helpful commands:",[91,42010,42012],{"className":10352,"code":42011,"language":10354,"meta":96,"style":96},"python script.py\npython -u script.py\npython -m pdb script.py\n",[17,42013,42014,42021,42030],{"__ignoreMap":96},[100,42015,42016,42018],{"class":102,"line":103},[100,42017,95],{"class":10361},[100,42019,42020],{"class":209}," script.py\n",[100,42022,42023,42025,42028],{"class":102,"line":135},[100,42024,95],{"class":10361},[100,42026,42027],{"class":10364}," -u",[100,42029,42020],{"class":209},[100,42031,42032,42034,42036,42039],{"class":102,"line":142},[100,42033,95],{"class":10361},[100,42035,26622],{"class":10364},[100,42037,42038],{"class":209}," pdb",[100,42040,42020],{"class":209},[14,42042,7039],{},[40,42044,42045,42051,42057],{},[43,42046,42047,42050],{},[17,42048,42049],{},"python script.py"," runs the script normally",[43,42052,42053,42056],{},[17,42054,42055],{},"python -u script.py"," runs Python in unbuffered mode, so output appears immediately",[43,42058,42059,42062],{},[17,42060,42061],{},"python -m pdb script.py"," starts the Python debugger",[14,42064,42065,42066,290],{},"If you want more help tracking problems step by step, see the ",[295,42067,7077],{"href":32320},[77,42069,42071],{"id":42070},"keyboardinterrupt-vs-other-exceptions","KeyboardInterrupt vs other exceptions",[14,42073,42074,42076],{},[17,42075,41104],{}," is different from many common Python exceptions.",[14,42078,580],{},[40,42080,42081,42089,42094],{},[43,42082,42083,42085,42086],{},[17,42084,41104],{}," usually happens because the ",[22,42087,42088],{},"user interrupted the program",[43,42090,42091,42093],{},[17,42092,3483],{}," happens when a value has the wrong form",[43,42095,42096,42098],{},[17,42097,17663],{}," happens when a program fails during execution for a different reason",[14,42100,35458,42101,42103,42104,42107],{},[17,42102,41104],{}," is usually ",[22,42105,42106],{},"about stopping a running script",", not about invalid code.",[14,42109,42110,42111,290],{},"If you are comparing exception types, you may also want to read about ",[295,42112,42114],{"href":42113},"\u002Ferrors\u002Fruntimeerror-in-python-causes-and-fixes","RuntimeError in Python",[77,42116,1344],{"id":1343},[14,42118,29621],{},[40,42120,42121,42124,42127,42133],{},[43,42122,42123],{},"Pressing Ctrl+C while a script is running",[43,42125,42126],{},"Manually stopping an infinite loop",[43,42128,42129,42130,42132],{},"Interrupting a long ",[17,42131,41257],{}," call",[43,42134,42135],{},"Stopping a script that is waiting for input or network activity",[77,42137,1514],{"id":1513},[675,42139,42141],{"id":42140},"is-keyboardinterrupt-an-error-in-my-code","Is KeyboardInterrupt an error in my code?",[14,42143,42144],{},"Usually no. It normally means the program was stopped by the user with Ctrl+C.",[675,42146,42148],{"id":42147},"how-do-i-stop-keyboardinterrupt-from-showing-a-traceback","How do I stop KeyboardInterrupt from showing a traceback?",[14,42150,42151,42152,42154,42155,42157],{},"Catch ",[17,42153,41104],{}," with ",[17,42156,244],{}," and print your own message before exiting.",[91,42159,42161],{"className":93,"code":42160,"language":95,"meta":96,"style":96},"try:\n    while True:\n        print(\"Running...\")\nexcept KeyboardInterrupt:\n    print(\"\\nStopped cleanly\")\n",[17,42162,42163,42169,42177,42191,42199],{"__ignoreMap":96},[100,42164,42165,42167],{"class":102,"line":103},[100,42166,146],{"class":145},[100,42168,149],{"class":118},[100,42170,42171,42173,42175],{"class":102,"line":135},[100,42172,155],{"class":145},[100,42174,159],{"class":158},[100,42176,149],{"class":118},[100,42178,42179,42181,42183,42185,42187,42189],{"class":102,"line":142},[100,42180,167],{"class":114},[100,42182,170],{"class":118},[100,42184,206],{"class":205},[100,42186,41161],{"class":209},[100,42188,206],{"class":205},[100,42190,215],{"class":118},[100,42192,42193,42195,42197],{"class":102,"line":152},[100,42194,188],{"class":145},[100,42196,41172],{"class":191},[100,42198,149],{"class":118},[100,42200,42201,42203,42205,42207,42209,42212,42214],{"class":102,"line":164},[100,42202,200],{"class":114},[100,42204,170],{"class":118},[100,42206,206],{"class":205},[100,42208,20187],{"class":527},[100,42210,42211],{"class":209},"Stopped cleanly",[100,42213,206],{"class":205},[100,42215,215],{"class":118},[675,42217,42219],{"id":42218},"does-keyboardinterrupt-happen-only-in-loops","Does KeyboardInterrupt happen only in loops?",[14,42221,42222,42223,5870,42225,42227],{},"No. It can happen during ",[17,42224,41366],{},[17,42226,6210],{},", file work, or other long-running operations if you interrupt the program.",[675,42229,42231],{"id":42230},"should-i-always-catch-keyboardinterrupt","Should I always catch KeyboardInterrupt?",[14,42233,42234],{},"Not always. Catch it when you want a clean shutdown or need to save data before exiting.",[77,42236,1554],{"id":1553},[40,42238,42239,42243,42248,42254,42259,42263],{},[43,42240,42241],{},[295,42242,1587],{"href":1586},[43,42244,42245],{},[295,42246,42247],{"href":41513},"Using try, except, else, and finally in Python",[43,42249,42250],{},[295,42251,42253],{"href":42252},"\u002Fhow-to\u002Fhow-to-exit-a-program-in-python","How to exit a program in Python",[43,42255,42256],{},[295,42257,42258],{"href":41267},"Python while loops explained",[43,42260,42261],{},[295,42262,41406],{"href":41405},[43,42264,42265],{},[295,42266,42267],{"href":32320},"Beginner guide to debugging Python code",[1589,42269,42270],{},"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 .sP7_E, html code.shiki .sP7_E{--shiki-light:#39ADB5;--shiki-default:#24292E;--shiki-dark:#E1E4E8}html pre.shiki code .s39Yj, html code.shiki .s39Yj{--shiki-light:#39ADB5;--shiki-default:#005CC5;--shiki-dark:#79B8FF}html pre.shiki code .sptTA, html code.shiki .sptTA{--shiki-light:#6182B8;--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 .sZMiF, html code.shiki .sZMiF{--shiki-light:#E2931D;--shiki-default:#005CC5;--shiki-dark:#79B8FF}html pre.shiki code .s_hVV, html code.shiki .s_hVV{--shiki-light:#90A4AE;--shiki-default:#005CC5;--shiki-dark:#79B8FF}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 .su5hD, html code.shiki .su5hD{--shiki-light:#90A4AE;--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 pre.shiki code .srdBf, html code.shiki .srdBf{--shiki-light:#F76D47;--shiki-default:#005CC5;--shiki-dark:#79B8FF}html pre.shiki code .smGrS, html code.shiki .smGrS{--shiki-light:#39ADB5;--shiki-default:#D73A49;--shiki-dark:#F97583}html pre.shiki code .sbgvK, html code.shiki .sbgvK{--shiki-light:#E2931D;--shiki-default:#6F42C1;--shiki-dark:#B392F0}html pre.shiki code .stzsN, html code.shiki .stzsN{--shiki-light:#91B859;--shiki-default:#005CC5;--shiki-dark:#79B8FF}",{"title":96,"searchDepth":135,"depth":135,"links":42272},[42273,42274,42275,42276,42277,42284,42285,42286,42287,42288,42294],{"id":79,"depth":135,"text":80},{"id":41197,"depth":135,"text":41198},{"id":41233,"depth":135,"text":41234},{"id":41271,"depth":135,"text":41272},{"id":41409,"depth":135,"text":41410,"children":42278},[42279,42280,42281,42282,42283],{"id":41432,"depth":142,"text":41433},{"id":41445,"depth":142,"text":41446},{"id":41516,"depth":142,"text":41517},{"id":41631,"depth":142,"text":41632},{"id":41744,"depth":142,"text":41745},{"id":41813,"depth":135,"text":41814},{"id":3273,"depth":135,"text":3274},{"id":42070,"depth":135,"text":42071},{"id":1343,"depth":135,"text":1344},{"id":1513,"depth":135,"text":1514,"children":42289},[42290,42291,42292,42293],{"id":42140,"depth":142,"text":42141},{"id":42147,"depth":142,"text":42148},{"id":42218,"depth":142,"text":42219},{"id":42230,"depth":142,"text":42231},{"id":1553,"depth":135,"text":1554},"Master keyboardinterrupt exception in python explained in our comprehensive Python beginner guide.",{},"\u002Ferrors\u002Fkeyboardinterrupt-exception-in-python-explained",{"title":41094,"description":42295},"errors\u002Fkeyboardinterrupt-exception-in-python-explained","i3ELmDwhDKOftfYUWsKdv818rrVPGjWeLAK5I6_pp8Y",{"id":42302,"title":42303,"body":42304,"description":43936,"extension":1623,"meta":43937,"navigation":138,"path":43938,"seo":43939,"stem":43940,"__hash__":43941},"content\u002Ferrors\u002Fkeyerror-in-python-causes-and-fixes.md","KeyError in Python: Causes and Fixes",{"type":7,"value":42305,"toc":43914},[42306,42309,42314,42317,42319,42475,42486,42490,42495,42501,42504,42508,42510,42609,42611,42646,42665,42672,42674,42678,42700,42703,42758,42766,42770,42773,42798,42801,42805,42810,42833,42835,42947,42949,42968,42971,42976,42980,42985,43074,43076,43091,43094,43101,43105,43110,43113,43127,43129,43212,43214,43237,43243,43248,43252,43258,43260,43322,43325,43339,43346,43350,43353,43356,43413,43418,43496,43498,43507,43510,43514,43517,43543,43545,43649,43651,43723,43725,43730,43754,43757,43816,43818,43826,43842,43846,43849,43853,43861,43865,43868,43870,43911],[10,42307,42303],{"id":42308},"keyerror-in-python-causes-and-fixes",[14,42310,571,42311,42313],{},[17,42312,17063],{}," in Python usually happens when you try to read a dictionary value using a key that does not exist.",[14,42315,42316],{},"This error is very common when working with dictionaries, JSON data, and API responses. The good news is that it is usually easy to fix once you know why it happens.",[77,42318,80],{"id":79},[91,42320,42322],{"className":93,"code":42321,"language":95,"meta":96,"style":96},"data = {\"name\": \"Ana\"}\n\n# Safe access\nprint(data.get(\"age\"))          # None\nprint(data.get(\"age\", 0))       # 0\n\n# Check before access\nif \"age\" in data:\n    print(data[\"age\"])\nelse:\n    print(\"Key not found\")\n",[17,42323,42324,42348,42352,42357,42382,42411,42415,42420,42436,42454,42460],{"__ignoreMap":96},[100,42325,42326,42328,42330,42332,42334,42336,42338,42340,42342,42344,42346],{"class":102,"line":103},[100,42327,7909],{"class":106},[100,42329,111],{"class":110},[100,42331,12080],{"class":118},[100,42333,206],{"class":205},[100,42335,2853],{"class":209},[100,42337,206],{"class":205},[100,42339,89],{"class":118},[100,42341,1708],{"class":205},[100,42343,34819],{"class":209},[100,42345,206],{"class":205},[100,42347,12093],{"class":118},[100,42349,42350],{"class":102,"line":135},[100,42351,139],{"emptyLinePlaceholder":138},[100,42353,42354],{"class":102,"line":142},[100,42355,42356],{"class":414},"# Safe access\n",[100,42358,42359,42361,42363,42365,42367,42369,42371,42373,42375,42377,42379],{"class":102,"line":152},[100,42360,372],{"class":114},[100,42362,170],{"class":118},[100,42364,7934],{"class":178},[100,42366,290],{"class":118},[100,42368,12412],{"class":178},[100,42370,170],{"class":118},[100,42372,206],{"class":205},[100,42374,2770],{"class":209},[100,42376,206],{"class":205},[100,42378,411],{"class":118},[100,42380,42381],{"class":414},"          # None\n",[100,42383,42384,42386,42388,42390,42392,42394,42396,42398,42400,42402,42404,42406,42408],{"class":102,"line":164},[100,42385,372],{"class":114},[100,42387,170],{"class":118},[100,42389,7934],{"class":178},[100,42391,290],{"class":118},[100,42393,12412],{"class":178},[100,42395,170],{"class":118},[100,42397,206],{"class":205},[100,42399,2770],{"class":209},[100,42401,206],{"class":205},[100,42403,126],{"class":118},[100,42405,1921],{"class":122},[100,42407,411],{"class":118},[100,42409,42410],{"class":414},"       # 0\n",[100,42412,42413],{"class":102,"line":185},[100,42414,139],{"emptyLinePlaceholder":138},[100,42416,42417],{"class":102,"line":197},[100,42418,42419],{"class":414},"# Check before access\n",[100,42421,42422,42424,42426,42428,42430,42432,42434],{"class":102,"line":771},[100,42423,2736],{"class":145},[100,42425,1708],{"class":205},[100,42427,2770],{"class":209},[100,42429,206],{"class":205},[100,42431,1971],{"class":110},[100,42433,12102],{"class":106},[100,42435,149],{"class":118},[100,42437,42438,42440,42442,42444,42446,42448,42450,42452],{"class":102,"line":787},[100,42439,200],{"class":114},[100,42441,170],{"class":118},[100,42443,7934],{"class":178},[100,42445,3166],{"class":118},[100,42447,206],{"class":205},[100,42449,2770],{"class":209},[100,42451,206],{"class":205},[100,42453,132],{"class":118},[100,42455,42456,42458],{"class":102,"line":5816},[100,42457,4944],{"class":145},[100,42459,149],{"class":118},[100,42461,42462,42464,42466,42468,42471,42473],{"class":102,"line":5833},[100,42463,200],{"class":114},[100,42465,170],{"class":118},[100,42467,206],{"class":205},[100,42469,42470],{"class":209},"Key not found",[100,42472,206],{"class":205},[100,42474,215],{"class":118},[14,42476,241,42477,42482,42483,42485],{},[295,42478,42480],{"href":42479},"\u002Freference\u002Fpython-dictionary-get-method",[17,42481,11018],{}," when a key may be missing, or check with ",[17,42484,617],{}," before using square brackets.",[77,42487,42489],{"id":42488},"what-keyerror-means","What KeyError means",[14,42491,571,42492,42494],{},[17,42493,17063],{}," happens when you try to access a dictionary key that does not exist.",[14,42496,42497,42498,290],{},"It is most common with square bracket access like ",[17,42499,42500],{},"data[\"missing_key\"]",[14,42502,42503],{},"Python raises the error because it cannot find that key in the dictionary.",[77,42505,42507],{"id":42506},"a-simple-example-that-causes-keyerror","A simple example that causes KeyError",[14,42509,5948],{},[91,42511,42513],{"className":93,"code":42512,"language":95,"meta":96,"style":96},"user = {\n    \"name\": \"Ana\",\n    \"country\": \"Spain\"\n}\n\nprint(user[\"name\"])\nprint(user[\"age\"])   # This line raises KeyError\n",[17,42514,42515,42524,42544,42562,42566,42570,42588],{"__ignoreMap":96},[100,42516,42517,42519,42521],{"class":102,"line":103},[100,42518,15336],{"class":106},[100,42520,111],{"class":110},[100,42522,42523],{"class":118}," {\n",[100,42525,42526,42529,42531,42533,42535,42537,42539,42541],{"class":102,"line":135},[100,42527,42528],{"class":205},"    \"",[100,42530,2853],{"class":209},[100,42532,206],{"class":205},[100,42534,89],{"class":118},[100,42536,1708],{"class":205},[100,42538,34819],{"class":209},[100,42540,206],{"class":205},[100,42542,42543],{"class":118},",\n",[100,42545,42546,42548,42551,42553,42555,42557,42560],{"class":102,"line":142},[100,42547,42528],{"class":205},[100,42549,42550],{"class":209},"country",[100,42552,206],{"class":205},[100,42554,89],{"class":118},[100,42556,1708],{"class":205},[100,42558,42559],{"class":209},"Spain",[100,42561,1714],{"class":205},[100,42563,42564],{"class":102,"line":152},[100,42565,12093],{"class":118},[100,42567,42568],{"class":102,"line":164},[100,42569,139],{"emptyLinePlaceholder":138},[100,42571,42572,42574,42576,42578,42580,42582,42584,42586],{"class":102,"line":185},[100,42573,372],{"class":114},[100,42575,170],{"class":118},[100,42577,15354],{"class":178},[100,42579,3166],{"class":118},[100,42581,206],{"class":205},[100,42583,2853],{"class":209},[100,42585,206],{"class":205},[100,42587,132],{"class":118},[100,42589,42590,42592,42594,42596,42598,42600,42602,42604,42606],{"class":102,"line":197},[100,42591,372],{"class":114},[100,42593,170],{"class":118},[100,42595,15354],{"class":178},[100,42597,3166],{"class":118},[100,42599,206],{"class":205},[100,42601,2770],{"class":209},[100,42603,206],{"class":205},[100,42605,32896],{"class":118},[100,42607,42608],{"class":414},"   # This line raises KeyError\n",[14,42610,218],{},[91,42612,42614],{"className":93,"code":42613,"language":95,"meta":96,"style":96},"Ana\nTraceback (most recent call last):\n  ...\nKeyError: 'age'\n",[17,42615,42616,42620,42630,42634],{"__ignoreMap":96},[100,42617,42618],{"class":102,"line":103},[100,42619,15252],{"class":106},[100,42621,42622,42624,42626,42628],{"class":102,"line":135},[100,42623,514],{"class":178},[100,42625,170],{"class":118},[100,42627,519],{"class":178},[100,42629,522],{"class":118},[100,42631,42632],{"class":102,"line":142},[100,42633,528],{"class":527},[100,42635,42636,42638,42640,42642,42644],{"class":102,"line":152},[100,42637,17063],{"class":191},[100,42639,89],{"class":118},[100,42641,1274],{"class":205},[100,42643,2770],{"class":209},[100,42645,3925],{"class":205},[14,42647,42648,42649,3266,42651,42654,42655,290,42658,42660,42661,42664],{},"The dictionary has ",[17,42650,12456],{},[17,42652,42653],{},"\"country\"",", but it does not have ",[17,42656,42657],{},"\"age\"",[9927,42659],{},"\nSo ",[17,42662,42663],{},"user[\"age\"]"," causes the error.",[14,42666,42667,42668,290],{},"If you need a refresher on dictionaries, see ",[295,42669,42671],{"href":42670},"\u002Flearn\u002Fpython-dictionaries-explained","Python dictionaries explained",[77,42673,3944],{"id":3943},[14,42675,42676,2137],{},[17,42677,17063],{},[40,42679,42680,42683,42686,42689,42692],{},[43,42681,42682],{},"The key was never added to the dictionary.",[43,42684,42685],{},"The key name is misspelled.",[43,42687,42688],{},"The key uses different uppercase or lowercase letters.",[43,42690,42691],{},"You expected input data to contain a key, but it does not.",[43,42693,42694,42695,3178,42697,290],{},"You removed the key earlier with ",[17,42696,8104],{},[17,42698,42699],{},"del",[14,42701,42702],{},"Here is a case-sensitive example:",[91,42704,42706],{"className":93,"code":42705,"language":95,"meta":96,"style":96},"data = {\"name\": \"Ana\"}\n\nprint(data[\"Name\"])   # KeyError\n",[17,42707,42708,42732,42736],{"__ignoreMap":96},[100,42709,42710,42712,42714,42716,42718,42720,42722,42724,42726,42728,42730],{"class":102,"line":103},[100,42711,7909],{"class":106},[100,42713,111],{"class":110},[100,42715,12080],{"class":118},[100,42717,206],{"class":205},[100,42719,2853],{"class":209},[100,42721,206],{"class":205},[100,42723,89],{"class":118},[100,42725,1708],{"class":205},[100,42727,34819],{"class":209},[100,42729,206],{"class":205},[100,42731,12093],{"class":118},[100,42733,42734],{"class":102,"line":135},[100,42735,139],{"emptyLinePlaceholder":138},[100,42737,42738,42740,42742,42744,42746,42748,42751,42753,42755],{"class":102,"line":142},[100,42739,372],{"class":114},[100,42741,170],{"class":118},[100,42743,7934],{"class":178},[100,42745,3166],{"class":118},[100,42747,206],{"class":205},[100,42749,42750],{"class":209},"Name",[100,42752,206],{"class":205},[100,42754,32896],{"class":118},[100,42756,42757],{"class":414},"   # KeyError\n",[14,42759,42760,3266,42762,42765],{},[17,42761,12456],{},[17,42763,42764],{},"\"Name\""," are different keys in Python.",[77,42767,42769],{"id":42768},"how-to-fix-keyerror","How to fix KeyError",[14,42771,42772],{},"There are several common fixes:",[40,42774,42775,42782,42789,42792],{},[43,42776,241,42777,42781],{},[295,42778,42779],{"href":42479},[17,42780,11018],{}," if the key may be missing.",[43,42783,42784,42785,42788],{},"Check with ",[17,42786,42787],{},"if key in dictionary"," before accessing it.",[43,42790,42791],{},"Add the missing key before reading it.",[43,42793,241,42794,42797],{},[17,42795,42796],{},"try-except KeyError"," when missing keys are expected and you want custom handling.",[14,42799,42800],{},"Choose the fix based on what your program should do when the key is not present.",[77,42802,42804],{"id":42803},"use-get-for-safe-dictionary-access","Use get() for safe dictionary access",[14,42806,42807,42809],{},[17,42808,13068],{}," is often the easiest fix for beginners.",[40,42811,42812,42819,42830],{},[43,42813,42814,551,42816,42818],{},[17,42815,13068],{},[17,42817,930],{}," by default if the key is missing.",[43,42820,42821,42822,42824,42825,3178,42827,290],{},"You can give ",[17,42823,13068],{}," a fallback value like ",[17,42826,2279],{},[17,42828,42829],{},"\"unknown\"",[43,42831,42832],{},"It avoids the error completely.",[14,42834,1844],{},[91,42836,42838],{"className":93,"code":42837,"language":95,"meta":96,"style":96},"student = {\"name\": \"Leo\"}\n\nprint(student.get(\"name\"))\nprint(student.get(\"grade\"))\nprint(student.get(\"grade\", \"Not assigned\"))\n",[17,42839,42840,42866,42870,42893,42916],{"__ignoreMap":96},[100,42841,42842,42845,42847,42849,42851,42853,42855,42857,42859,42862,42864],{"class":102,"line":103},[100,42843,42844],{"class":106},"student ",[100,42846,111],{"class":110},[100,42848,12080],{"class":118},[100,42850,206],{"class":205},[100,42852,2853],{"class":209},[100,42854,206],{"class":205},[100,42856,89],{"class":118},[100,42858,1708],{"class":205},[100,42860,42861],{"class":209},"Leo",[100,42863,206],{"class":205},[100,42865,12093],{"class":118},[100,42867,42868],{"class":102,"line":135},[100,42869,139],{"emptyLinePlaceholder":138},[100,42871,42872,42874,42876,42879,42881,42883,42885,42887,42889,42891],{"class":102,"line":142},[100,42873,372],{"class":114},[100,42875,170],{"class":118},[100,42877,42878],{"class":178},"student",[100,42880,290],{"class":118},[100,42882,12412],{"class":178},[100,42884,170],{"class":118},[100,42886,206],{"class":205},[100,42888,2853],{"class":209},[100,42890,206],{"class":205},[100,42892,182],{"class":118},[100,42894,42895,42897,42899,42901,42903,42905,42907,42909,42912,42914],{"class":102,"line":152},[100,42896,372],{"class":114},[100,42898,170],{"class":118},[100,42900,42878],{"class":178},[100,42902,290],{"class":118},[100,42904,12412],{"class":178},[100,42906,170],{"class":118},[100,42908,206],{"class":205},[100,42910,42911],{"class":209},"grade",[100,42913,206],{"class":205},[100,42915,182],{"class":118},[100,42917,42918,42920,42922,42924,42926,42928,42930,42932,42934,42936,42938,42940,42943,42945],{"class":102,"line":164},[100,42919,372],{"class":114},[100,42921,170],{"class":118},[100,42923,42878],{"class":178},[100,42925,290],{"class":118},[100,42927,12412],{"class":178},[100,42929,170],{"class":118},[100,42931,206],{"class":205},[100,42933,42911],{"class":209},[100,42935,206],{"class":205},[100,42937,126],{"class":118},[100,42939,1708],{"class":205},[100,42941,42942],{"class":209},"Not assigned",[100,42944,206],{"class":205},[100,42946,182],{"class":118},[14,42948,218],{},[91,42950,42952],{"className":93,"code":42951,"language":95,"meta":96,"style":96},"Leo\nNone\nNot assigned\n",[17,42953,42954,42959,42963],{"__ignoreMap":96},[100,42955,42956],{"class":102,"line":103},[100,42957,42958],{"class":106},"Leo\n",[100,42960,42961],{"class":102,"line":135},[100,42962,924],{"class":158},[100,42964,42965],{"class":102,"line":142},[100,42966,42967],{"class":106},"Not assigned\n",[14,42969,42970],{},"This is useful when a missing key is normal and should not stop the program.",[14,42972,42973,42974,290],{},"You can learn more on the ",[295,42975,12577],{"href":42479},[77,42977,42979],{"id":42978},"check-if-a-key-exists-first","Check if a key exists first",[14,42981,241,42982,42984],{},[17,42983,617],{}," when you want different logic for found and missing keys.",[91,42986,42988],{"className":93,"code":42987,"language":95,"meta":96,"style":96},"profile = {\"username\": \"mila\"}\n\nif \"email\" in profile:\n    print(profile[\"email\"])\nelse:\n    print(\"Email address is missing\")\n",[17,42989,42990,43015,43019,43035,43053,43059],{"__ignoreMap":96},[100,42991,42992,42994,42996,42998,43000,43002,43004,43006,43008,43011,43013],{"class":102,"line":103},[100,42993,12472],{"class":106},[100,42995,111],{"class":110},[100,42997,12080],{"class":118},[100,42999,206],{"class":205},[100,43001,11330],{"class":209},[100,43003,206],{"class":205},[100,43005,89],{"class":118},[100,43007,1708],{"class":205},[100,43009,43010],{"class":209},"mila",[100,43012,206],{"class":205},[100,43014,12093],{"class":118},[100,43016,43017],{"class":102,"line":135},[100,43018,139],{"emptyLinePlaceholder":138},[100,43020,43021,43023,43025,43027,43029,43031,43033],{"class":102,"line":142},[100,43022,2736],{"class":145},[100,43024,1708],{"class":205},[100,43026,11779],{"class":209},[100,43028,206],{"class":205},[100,43030,1971],{"class":110},[100,43032,12501],{"class":106},[100,43034,149],{"class":118},[100,43036,43037,43039,43041,43043,43045,43047,43049,43051],{"class":102,"line":152},[100,43038,200],{"class":114},[100,43040,170],{"class":118},[100,43042,12407],{"class":178},[100,43044,3166],{"class":118},[100,43046,206],{"class":205},[100,43048,11779],{"class":209},[100,43050,206],{"class":205},[100,43052,132],{"class":118},[100,43054,43055,43057],{"class":102,"line":164},[100,43056,4944],{"class":145},[100,43058,149],{"class":118},[100,43060,43061,43063,43065,43067,43070,43072],{"class":102,"line":185},[100,43062,200],{"class":114},[100,43064,170],{"class":118},[100,43066,206],{"class":205},[100,43068,43069],{"class":209},"Email address is missing",[100,43071,206],{"class":205},[100,43073,215],{"class":118},[14,43075,218],{},[91,43077,43079],{"className":93,"code":43078,"language":95,"meta":96,"style":96},"Email address is missing\n",[17,43080,43081],{"__ignoreMap":96},[100,43082,43083,43086,43088],{"class":102,"line":103},[100,43084,43085],{"class":106},"Email address ",[100,43087,4918],{"class":110},[100,43089,43090],{"class":106}," missing\n",[14,43092,43093],{},"This avoids the error before it happens.",[14,43095,43096,43097,290],{},"For more examples, see ",[295,43098,43100],{"href":43099},"\u002Fhow-to\u002Fhow-to-check-if-a-key-exists-in-a-dictionary-in-python","how to check if a key exists in a dictionary in Python",[77,43102,43104],{"id":43103},"handle-keyerror-with-try-and-except","Handle KeyError with try and except",[14,43106,241,43107,43109],{},[17,43108,244],{}," when missing keys are possible and you want to recover cleanly.",[14,43111,43112],{},"This is helpful for:",[40,43114,43115,43118,43121,43124],{},[43,43116,43117],{},"User input",[43,43119,43120],{},"Files",[43,43122,43123],{},"JSON data",[43,43125,43126],{},"API responses",[14,43128,1844],{},[91,43130,43132],{"className":93,"code":43131,"language":95,"meta":96,"style":96},"response = {\"status\": \"ok\"}\n\ntry:\n    print(response[\"message\"])\nexcept KeyError:\n    print(\"The 'message' field is missing\")\n",[17,43133,43134,43160,43164,43170,43188,43197],{"__ignoreMap":96},[100,43135,43136,43138,43140,43142,43144,43147,43149,43151,43153,43156,43158],{"class":102,"line":103},[100,43137,28382],{"class":106},[100,43139,111],{"class":110},[100,43141,12080],{"class":118},[100,43143,206],{"class":205},[100,43145,43146],{"class":209},"status",[100,43148,206],{"class":205},[100,43150,89],{"class":118},[100,43152,1708],{"class":205},[100,43154,43155],{"class":209},"ok",[100,43157,206],{"class":205},[100,43159,12093],{"class":118},[100,43161,43162],{"class":102,"line":135},[100,43163,139],{"emptyLinePlaceholder":138},[100,43165,43166,43168],{"class":102,"line":142},[100,43167,146],{"class":145},[100,43169,149],{"class":118},[100,43171,43172,43174,43176,43178,43180,43182,43184,43186],{"class":102,"line":152},[100,43173,200],{"class":114},[100,43175,170],{"class":118},[100,43177,28411],{"class":178},[100,43179,3166],{"class":118},[100,43181,206],{"class":205},[100,43183,1836],{"class":209},[100,43185,206],{"class":205},[100,43187,132],{"class":118},[100,43189,43190,43192,43195],{"class":102,"line":164},[100,43191,188],{"class":145},[100,43193,43194],{"class":191}," KeyError",[100,43196,149],{"class":118},[100,43198,43199,43201,43203,43205,43208,43210],{"class":102,"line":185},[100,43200,200],{"class":114},[100,43202,170],{"class":118},[100,43204,206],{"class":205},[100,43206,43207],{"class":209},"The 'message' field is missing",[100,43209,206],{"class":205},[100,43211,215],{"class":118},[14,43213,218],{},[91,43215,43217],{"className":93,"code":43216,"language":95,"meta":96,"style":96},"The 'message' field is missing\n",[17,43218,43219],{"__ignoreMap":96},[100,43220,43221,43224,43226,43228,43230,43233,43235],{"class":102,"line":103},[100,43222,43223],{"class":106},"The ",[100,43225,1280],{"class":205},[100,43227,1836],{"class":209},[100,43229,1280],{"class":205},[100,43231,43232],{"class":106}," field ",[100,43234,4918],{"class":110},[100,43236,43090],{"class":106},[14,43238,43239,43240,43242],{},"Keep the ",[17,43241,188],{}," block simple and specific.",[14,43244,43245,43246,290],{},"If you want to understand this pattern better, read ",[295,43247,1029],{"href":1028},[77,43249,43251],{"id":43250},"common-debugging-steps","Common debugging steps",[14,43253,43254,43255,43257],{},"When you get a ",[17,43256,17063],{},", check the actual data before changing your code.",[14,43259,5295],{},[91,43261,43263],{"className":93,"code":43262,"language":95,"meta":96,"style":96},"print(my_dict)\nprint(my_dict.keys())\nprint(\"name\" in my_dict)\nprint(type(my_dict))\n",[17,43264,43265,43275,43289,43308],{"__ignoreMap":96},[100,43266,43267,43269,43271,43273],{"class":102,"line":103},[100,43268,372],{"class":114},[100,43270,170],{"class":118},[100,43272,17794],{"class":178},[100,43274,215],{"class":118},[100,43276,43277,43279,43281,43283,43285,43287],{"class":102,"line":135},[100,43278,372],{"class":114},[100,43280,170],{"class":118},[100,43282,17794],{"class":178},[100,43284,290],{"class":118},[100,43286,6185],{"class":178},[100,43288,3370],{"class":118},[100,43290,43291,43293,43295,43297,43299,43301,43303,43306],{"class":102,"line":142},[100,43292,372],{"class":114},[100,43294,170],{"class":118},[100,43296,206],{"class":205},[100,43298,2853],{"class":209},[100,43300,206],{"class":205},[100,43302,1971],{"class":145},[100,43304,43305],{"class":178}," my_dict",[100,43307,215],{"class":118},[100,43309,43310,43312,43314,43316,43318,43320],{"class":102,"line":152},[100,43311,372],{"class":114},[100,43313,170],{"class":118},[100,43315,1250],{"class":191},[100,43317,170],{"class":118},[100,43319,17794],{"class":178},[100,43321,182],{"class":118},[14,43323,43324],{},"These checks help you answer questions like:",[40,43326,43327,43330,43333,43336],{},[43,43328,43329],{},"What keys are really present?",[43,43331,43332],{},"Is the key spelled correctly?",[43,43334,43335],{},"Is the capitalization correct?",[43,43337,43338],{},"Is the value actually a dictionary?",[14,43340,43341,43342,290],{},"You can also inspect keys directly with the ",[295,43343,43345],{"href":43344},"\u002Freference\u002Fpython-dictionary-keys-method","Python dictionary keys() method",[77,43347,43349],{"id":43348},"keyerror-with-json-and-api-data","KeyError with JSON and API data",[14,43351,43352],{},"JSON objects often become Python dictionaries after loading them.",[14,43354,43355],{},"A field may be present in one response but missing in another. That means code like this can fail:",[91,43357,43359],{"className":93,"code":43358,"language":95,"meta":96,"style":96},"data = {\n    \"user\": \"Ana\"\n}\n\nprint(data[\"email\"])   # KeyError\n",[17,43360,43361,43369,43385,43389,43393],{"__ignoreMap":96},[100,43362,43363,43365,43367],{"class":102,"line":103},[100,43364,7909],{"class":106},[100,43366,111],{"class":110},[100,43368,42523],{"class":118},[100,43370,43371,43373,43375,43377,43379,43381,43383],{"class":102,"line":135},[100,43372,42528],{"class":205},[100,43374,15354],{"class":209},[100,43376,206],{"class":205},[100,43378,89],{"class":118},[100,43380,1708],{"class":205},[100,43382,34819],{"class":209},[100,43384,1714],{"class":205},[100,43386,43387],{"class":102,"line":142},[100,43388,12093],{"class":118},[100,43390,43391],{"class":102,"line":152},[100,43392,139],{"emptyLinePlaceholder":138},[100,43394,43395,43397,43399,43401,43403,43405,43407,43409,43411],{"class":102,"line":164},[100,43396,372],{"class":114},[100,43398,170],{"class":118},[100,43400,7934],{"class":178},[100,43402,3166],{"class":118},[100,43404,206],{"class":205},[100,43406,11779],{"class":209},[100,43408,206],{"class":205},[100,43410,32896],{"class":118},[100,43412,42757],{"class":414},[14,43414,43415,43416,89],{},"A safer version uses ",[17,43417,13068],{},[91,43419,43421],{"className":93,"code":43420,"language":95,"meta":96,"style":96},"data = {\n    \"user\": \"Ana\"\n}\n\nemail = data.get(\"email\", \"No email provided\")\nprint(email)\n",[17,43422,43423,43431,43447,43451,43455,43486],{"__ignoreMap":96},[100,43424,43425,43427,43429],{"class":102,"line":103},[100,43426,7909],{"class":106},[100,43428,111],{"class":110},[100,43430,42523],{"class":118},[100,43432,43433,43435,43437,43439,43441,43443,43445],{"class":102,"line":135},[100,43434,42528],{"class":205},[100,43436,15354],{"class":209},[100,43438,206],{"class":205},[100,43440,89],{"class":118},[100,43442,1708],{"class":205},[100,43444,34819],{"class":209},[100,43446,1714],{"class":205},[100,43448,43449],{"class":102,"line":142},[100,43450,12093],{"class":118},[100,43452,43453],{"class":102,"line":152},[100,43454,139],{"emptyLinePlaceholder":138},[100,43456,43457,43459,43461,43463,43465,43467,43469,43471,43473,43475,43477,43479,43482,43484],{"class":102,"line":164},[100,43458,11747],{"class":106},[100,43460,111],{"class":110},[100,43462,12102],{"class":106},[100,43464,290],{"class":118},[100,43466,12412],{"class":178},[100,43468,170],{"class":118},[100,43470,206],{"class":205},[100,43472,11779],{"class":209},[100,43474,206],{"class":205},[100,43476,126],{"class":118},[100,43478,1708],{"class":205},[100,43480,43481],{"class":209},"No email provided",[100,43483,206],{"class":205},[100,43485,215],{"class":118},[100,43487,43488,43490,43492,43494],{"class":102,"line":185},[100,43489,372],{"class":114},[100,43491,170],{"class":118},[100,43493,11779],{"class":178},[100,43495,215],{"class":118},[14,43497,218],{},[91,43499,43501],{"className":93,"code":43500,"language":95,"meta":96,"style":96},"No email provided\n",[17,43502,43503],{"__ignoreMap":96},[100,43504,43505],{"class":102,"line":103},[100,43506,43500],{"class":106},[14,43508,43509],{},"This is a common pattern when working with external data, because you cannot always assume every field exists.",[77,43511,43513],{"id":43512},"related-dictionary-methods-to-know","Related dictionary methods to know",[14,43515,43516],{},"These methods are useful when working with dictionary keys:",[40,43518,43519,43526,43533,43538],{},[43,43520,43521,43525],{},[295,43522,43523],{"href":42479},[17,43524,13068],{}," for safe lookup",[43,43527,43528,43532],{},[295,43529,43530],{"href":43344},[17,43531,7891],{}," to inspect available keys",[43,43534,43535,43537],{},[17,43536,7898],{}," to loop through key-value pairs",[43,43539,43540,43542],{},[17,43541,8104],{}," to remove a key safely when used with a default value",[14,43544,1844],{},[91,43546,43548],{"className":93,"code":43547,"language":95,"meta":96,"style":96},"data = {\"name\": \"Ana\", \"age\": 25}\n\nprint(data.keys())\nprint(data.items())\nprint(data.pop(\"city\", \"Key not found\"))\n",[17,43549,43550,43586,43590,43604,43618],{"__ignoreMap":96},[100,43551,43552,43554,43556,43558,43560,43562,43564,43566,43568,43570,43572,43574,43576,43578,43580,43582,43584],{"class":102,"line":103},[100,43553,7909],{"class":106},[100,43555,111],{"class":110},[100,43557,12080],{"class":118},[100,43559,206],{"class":205},[100,43561,2853],{"class":209},[100,43563,206],{"class":205},[100,43565,89],{"class":118},[100,43567,1708],{"class":205},[100,43569,34819],{"class":209},[100,43571,206],{"class":205},[100,43573,126],{"class":118},[100,43575,1708],{"class":205},[100,43577,2770],{"class":209},[100,43579,206],{"class":205},[100,43581,89],{"class":118},[100,43583,16782],{"class":122},[100,43585,12093],{"class":118},[100,43587,43588],{"class":102,"line":135},[100,43589,139],{"emptyLinePlaceholder":138},[100,43591,43592,43594,43596,43598,43600,43602],{"class":102,"line":142},[100,43593,372],{"class":114},[100,43595,170],{"class":118},[100,43597,7934],{"class":178},[100,43599,290],{"class":118},[100,43601,6185],{"class":178},[100,43603,3370],{"class":118},[100,43605,43606,43608,43610,43612,43614,43616],{"class":102,"line":152},[100,43607,372],{"class":114},[100,43609,170],{"class":118},[100,43611,7934],{"class":178},[100,43613,290],{"class":118},[100,43615,3146],{"class":178},[100,43617,3370],{"class":118},[100,43619,43620,43622,43624,43626,43628,43630,43632,43634,43637,43639,43641,43643,43645,43647],{"class":102,"line":164},[100,43621,372],{"class":114},[100,43623,170],{"class":118},[100,43625,7934],{"class":178},[100,43627,290],{"class":118},[100,43629,34352],{"class":178},[100,43631,170],{"class":118},[100,43633,206],{"class":205},[100,43635,43636],{"class":209},"city",[100,43638,206],{"class":205},[100,43640,126],{"class":118},[100,43642,1708],{"class":205},[100,43644,42470],{"class":209},[100,43646,206],{"class":205},[100,43648,182],{"class":118},[14,43650,218],{},[91,43652,43654],{"className":93,"code":43653,"language":95,"meta":96,"style":96},"dict_keys(['name', 'age'])\ndict_items([('name', 25), ('age', 25)])\nKey not found\n",[17,43655,43656,43679,43713],{"__ignoreMap":96},[100,43657,43658,43661,43663,43665,43667,43669,43671,43673,43675,43677],{"class":102,"line":103},[100,43659,43660],{"class":178},"dict_keys",[100,43662,119],{"class":118},[100,43664,1280],{"class":205},[100,43666,2853],{"class":209},[100,43668,1280],{"class":205},[100,43670,126],{"class":118},[100,43672,1274],{"class":205},[100,43674,2770],{"class":209},[100,43676,1280],{"class":205},[100,43678,132],{"class":118},[100,43680,43681,43684,43687,43689,43691,43693,43695,43697,43699,43701,43703,43705,43707,43709,43711],{"class":102,"line":135},[100,43682,43683],{"class":178},"dict_items",[100,43685,43686],{"class":118},"([(",[100,43688,1280],{"class":205},[100,43690,2853],{"class":209},[100,43692,1280],{"class":205},[100,43694,126],{"class":118},[100,43696,16782],{"class":122},[100,43698,2874],{"class":118},[100,43700,16805],{"class":118},[100,43702,1280],{"class":205},[100,43704,2770],{"class":209},[100,43706,1280],{"class":205},[100,43708,126],{"class":118},[100,43710,16782],{"class":122},[100,43712,37438],{"class":118},[100,43714,43715,43718,43720],{"class":102,"line":142},[100,43716,43717],{"class":106},"Key ",[100,43719,1059],{"class":110},[100,43721,43722],{"class":106}," found\n",[77,43724,7117],{"id":7116},[14,43726,43727,43728,89],{},"These are some of the most common causes of ",[17,43729,17063],{},[40,43731,43732,43735,43738,43745,43748,43751],{},[43,43733,43734],{},"Accessing a missing dictionary key with square brackets",[43,43736,43737],{},"Misspelling the key name",[43,43739,43740,43741,15957,43743],{},"Using the wrong letter case, such as ",[17,43742,42764],{},[17,43744,12456],{},[43,43746,43747],{},"Expecting JSON or API data to always contain the same fields",[43,43749,43750],{},"Removing a key earlier in the program",[43,43752,43753],{},"Confusing dictionary keys with list indexes",[14,43755,43756],{},"Example of a misspelled key:",[91,43758,43760],{"className":93,"code":43759,"language":95,"meta":96,"style":96},"person = {\"first_name\": \"Liam\"}\n\nprint(person[\"frist_name\"])   # KeyError because the key is misspelled\n",[17,43761,43762,43789,43793],{"__ignoreMap":96},[100,43763,43764,43767,43769,43771,43773,43776,43778,43780,43782,43785,43787],{"class":102,"line":103},[100,43765,43766],{"class":106},"person ",[100,43768,111],{"class":110},[100,43770,12080],{"class":118},[100,43772,206],{"class":205},[100,43774,43775],{"class":209},"first_name",[100,43777,206],{"class":205},[100,43779,89],{"class":118},[100,43781,1708],{"class":205},[100,43783,43784],{"class":209},"Liam",[100,43786,206],{"class":205},[100,43788,12093],{"class":118},[100,43790,43791],{"class":102,"line":135},[100,43792,139],{"emptyLinePlaceholder":138},[100,43794,43795,43797,43799,43802,43804,43806,43809,43811,43813],{"class":102,"line":142},[100,43796,372],{"class":114},[100,43798,170],{"class":118},[100,43800,43801],{"class":178},"person",[100,43803,3166],{"class":118},[100,43805,206],{"class":205},[100,43807,43808],{"class":209},"frist_name",[100,43810,206],{"class":205},[100,43812,32896],{"class":118},[100,43814,43815],{"class":414},"   # KeyError because the key is misspelled\n",[77,43817,1514],{"id":1513},[675,43819,43821,43822,43825],{"id":43820},"what-is-the-difference-between-dictkey-and-dictgetkey","What is the difference between dict",[100,43823,43824],{},"key"," and dict.get(key)?",[14,43827,43828,20442,43831,43833,43834,43836,551,43839,43841],{},[17,43829,43830],{},"dict[key]",[17,43832,17063],{}," if the key is missing.",[9927,43835],{},[17,43837,43838],{},"dict.get(key)",[17,43840,930],{}," or a default value instead.",[675,43843,43845],{"id":43844},"does-keyerror-only-happen-with-dictionaries","Does KeyError only happen with dictionaries?",[14,43847,43848],{},"It mostly happens with dictionaries, but it can also appear in other mapping-like objects that use keys.",[675,43850,43852],{"id":43851},"how-do-i-avoid-keyerror-in-beginner-code","How do I avoid KeyError in beginner code?",[14,43854,241,43855,43857,43858,43860],{},[17,43856,13068],{}," for optional keys, or check with ",[17,43859,617],{}," before reading a value.",[675,43862,43864],{"id":43863},"why-do-i-get-keyerror-when-reading-json","Why do I get KeyError when reading JSON?",[14,43866,43867],{},"Some JSON data does not include every field every time. A missing field becomes a missing dictionary key in Python.",[77,43869,1554],{"id":1553},[40,43871,43872,43876,43880,43884,43889,43895,43899,43905],{},[43,43873,43874],{},[295,43875,12577],{"href":42479},[43,43877,43878],{},[295,43879,43345],{"href":43344},[43,43881,43882],{},[295,43883,42671],{"href":42670},[43,43885,43886],{},[295,43887,43888],{"href":43099},"How to check if a key exists in a dictionary in Python",[43,43890,43891],{},[295,43892,43894],{"href":43893},"\u002Fhow-to\u002Fhow-to-access-values-in-a-dictionary-in-python","How to access values in a dictionary in Python",[43,43896,43897],{},[295,43898,1581],{"href":1028},[43,43900,43901],{},[295,43902,43904],{"href":43903},"\u002Ferrors\u002Fkeyerror-key-not-found-in-dictionary-fix","KeyError: key not found in dictionary fix",[43,43906,43907],{},[295,43908,43910],{"href":43909},"\u002Ferrors\u002Fkeyerror-when-accessing-dictionary-values-fix","KeyError when accessing dictionary values fix",[1589,43912,43913],{},"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 .sP7_E, html code.shiki .sP7_E{--shiki-light:#39ADB5;--shiki-default:#24292E;--shiki-dark:#E1E4E8}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 .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 pre.shiki code .srdBf, html code.shiki .srdBf{--shiki-light:#F76D47;--shiki-default:#005CC5;--shiki-dark:#79B8FF}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 .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 .s_hVV, html code.shiki .s_hVV{--shiki-light:#90A4AE;--shiki-default:#005CC5;--shiki-dark:#79B8FF}html pre.shiki code .sZMiF, html code.shiki .sZMiF{--shiki-light:#E2931D;--shiki-default:#005CC5;--shiki-dark:#79B8FF}html pre.shiki code .s39Yj, html code.shiki .s39Yj{--shiki-light:#39ADB5;--shiki-default:#005CC5;--shiki-dark:#79B8FF}",{"title":96,"searchDepth":135,"depth":135,"links":43915},[43916,43917,43918,43919,43920,43921,43922,43923,43924,43925,43926,43927,43928,43935],{"id":79,"depth":135,"text":80},{"id":42488,"depth":135,"text":42489},{"id":42506,"depth":135,"text":42507},{"id":3943,"depth":135,"text":3944},{"id":42768,"depth":135,"text":42769},{"id":42803,"depth":135,"text":42804},{"id":42978,"depth":135,"text":42979},{"id":43103,"depth":135,"text":43104},{"id":43250,"depth":135,"text":43251},{"id":43348,"depth":135,"text":43349},{"id":43512,"depth":135,"text":43513},{"id":7116,"depth":135,"text":7117},{"id":1513,"depth":135,"text":1514,"children":43929},[43930,43932,43933,43934],{"id":43820,"depth":142,"text":43931},"What is the difference between dictkey and dict.get(key)?",{"id":43844,"depth":142,"text":43845},{"id":43851,"depth":142,"text":43852},{"id":43863,"depth":142,"text":43864},{"id":1553,"depth":135,"text":1554},"Master keyerror in python causes and fixes in our comprehensive Python beginner guide.",{},"\u002Ferrors\u002Fkeyerror-in-python-causes-and-fixes",{"title":42303,"description":43936},"errors\u002Fkeyerror-in-python-causes-and-fixes","K6E-dq09gBBkCmHDID_YCGW2pcqAarttNigrg-HgB1Y",{"id":43943,"title":43944,"body":43945,"description":45577,"extension":1623,"meta":45578,"navigation":138,"path":43903,"seo":45579,"stem":45580,"__hash__":45581},"content\u002Ferrors\u002Fkeyerror-key-not-found-in-dictionary-fix.md","KeyError: key not found in dictionary (Fix)",{"type":7,"value":43946,"toc":45557},[43947,43950,43955,43965,43968,43970,44105,44112,44114,44119,44124,44127,44132,44134,44136,44204,44206,44277,44286,44294,44296,44298,44316,44318,44335,44339,44345,44348,44435,44437,44450,44453,44458,44462,44468,44474,44476,44557,44559,44573,44576,44581,44585,44588,44591,44664,44666,44729,44736,44739,44870,44872,44953,44956,44966,44971,44975,44980,44983,45063,45065,45079,45085,45104,45113,45115,45118,45132,45134,45203,45206,45213,45220,45225,45228,45252,45254,45381,45383,45401,45407,45409,45413,45421,45425,45433,45439,45443,45448,45450,45505,45509,45517,45519,45545,45554],[10,43948,43944],{"id":43949},"keyerror-key-not-found-in-dictionary-fix",[14,43951,571,43952,43954],{},[17,43953,17063],{}," happens when you try to read a dictionary key that is not there.",[14,43956,43957,43958,43961,43962,43964],{},"This usually happens when you use square brackets, like ",[17,43959,43960],{},"data[\"age\"]",", but the dictionary does not contain ",[17,43963,42657],{},". Python raises an error because dictionary keys must match exactly.",[14,43966,43967],{},"This guide shows what the error means, why it happens, and simple ways to fix it.",[77,43969,80],{"id":79},[91,43971,43973],{"className":93,"code":43972,"language":95,"meta":96,"style":96},"data = {\"name\": \"Sam\"}\n\n# Safe access\nvalue = data.get(\"age\")\nprint(value)  # None\n\n# Or check first\nif \"age\" in data:\n    print(data[\"age\"])\nelse:\n    print(\"Key not found\")\n",[17,43974,43975,43999,44003,44007,44029,44042,44046,44051,44067,44085,44091],{"__ignoreMap":96},[100,43976,43977,43979,43981,43983,43985,43987,43989,43991,43993,43995,43997],{"class":102,"line":103},[100,43978,7909],{"class":106},[100,43980,111],{"class":110},[100,43982,12080],{"class":118},[100,43984,206],{"class":205},[100,43986,2853],{"class":209},[100,43988,206],{"class":205},[100,43990,89],{"class":118},[100,43992,1708],{"class":205},[100,43994,29329],{"class":209},[100,43996,206],{"class":205},[100,43998,12093],{"class":118},[100,44000,44001],{"class":102,"line":135},[100,44002,139],{"emptyLinePlaceholder":138},[100,44004,44005],{"class":102,"line":142},[100,44006,42356],{"class":414},[100,44008,44009,44011,44013,44015,44017,44019,44021,44023,44025,44027],{"class":102,"line":152},[100,44010,3318],{"class":106},[100,44012,111],{"class":110},[100,44014,12102],{"class":106},[100,44016,290],{"class":118},[100,44018,12412],{"class":178},[100,44020,170],{"class":118},[100,44022,206],{"class":205},[100,44024,2770],{"class":209},[100,44026,206],{"class":205},[100,44028,215],{"class":118},[100,44030,44031,44033,44035,44037,44039],{"class":102,"line":164},[100,44032,372],{"class":114},[100,44034,170],{"class":118},[100,44036,757],{"class":178},[100,44038,6155],{"class":118},[100,44040,44041],{"class":414},"  # None\n",[100,44043,44044],{"class":102,"line":185},[100,44045,139],{"emptyLinePlaceholder":138},[100,44047,44048],{"class":102,"line":197},[100,44049,44050],{"class":414},"# Or check first\n",[100,44052,44053,44055,44057,44059,44061,44063,44065],{"class":102,"line":771},[100,44054,2736],{"class":145},[100,44056,1708],{"class":205},[100,44058,2770],{"class":209},[100,44060,206],{"class":205},[100,44062,1971],{"class":110},[100,44064,12102],{"class":106},[100,44066,149],{"class":118},[100,44068,44069,44071,44073,44075,44077,44079,44081,44083],{"class":102,"line":787},[100,44070,200],{"class":114},[100,44072,170],{"class":118},[100,44074,7934],{"class":178},[100,44076,3166],{"class":118},[100,44078,206],{"class":205},[100,44080,2770],{"class":209},[100,44082,206],{"class":205},[100,44084,132],{"class":118},[100,44086,44087,44089],{"class":102,"line":5816},[100,44088,4944],{"class":145},[100,44090,149],{"class":118},[100,44092,44093,44095,44097,44099,44101,44103],{"class":102,"line":5833},[100,44094,200],{"class":114},[100,44096,170],{"class":118},[100,44098,206],{"class":205},[100,44100,42470],{"class":209},[100,44102,206],{"class":205},[100,44104,215],{"class":118},[14,44106,241,44107,44109,44110,42485],{},[17,44108,11018],{}," when a key might be missing, or check with ",[17,44111,617],{},[77,44113,5881],{"id":5880},[14,44115,571,44116,44118],{},[17,44117,17063],{}," happens when you ask for a dictionary key that does not exist.",[14,44120,44121,44122,290],{},"It usually appears when using square brackets like ",[17,44123,43960],{},[14,44125,44126],{},"Python stops because it cannot find that exact key.",[14,44128,44129,44130,290],{},"If you are new to dictionaries, see ",[295,44131,42671],{"href":42670},[77,44133,11031],{"id":11030},[14,44135,5948],{},[91,44137,44139],{"className":93,"code":44138,"language":95,"meta":96,"style":96},"data = {\"name\": \"Sam\", \"city\": \"Boston\"}\n\nprint(data[\"age\"])\n",[17,44140,44141,44182,44186],{"__ignoreMap":96},[100,44142,44143,44145,44147,44149,44151,44153,44155,44157,44159,44161,44163,44165,44167,44169,44171,44173,44175,44178,44180],{"class":102,"line":103},[100,44144,7909],{"class":106},[100,44146,111],{"class":110},[100,44148,12080],{"class":118},[100,44150,206],{"class":205},[100,44152,2853],{"class":209},[100,44154,206],{"class":205},[100,44156,89],{"class":118},[100,44158,1708],{"class":205},[100,44160,29329],{"class":209},[100,44162,206],{"class":205},[100,44164,126],{"class":118},[100,44166,1708],{"class":205},[100,44168,43636],{"class":209},[100,44170,206],{"class":205},[100,44172,89],{"class":118},[100,44174,1708],{"class":205},[100,44176,44177],{"class":209},"Boston",[100,44179,206],{"class":205},[100,44181,12093],{"class":118},[100,44183,44184],{"class":102,"line":135},[100,44185,139],{"emptyLinePlaceholder":138},[100,44187,44188,44190,44192,44194,44196,44198,44200,44202],{"class":102,"line":142},[100,44189,372],{"class":114},[100,44191,170],{"class":118},[100,44193,7934],{"class":178},[100,44195,3166],{"class":118},[100,44197,206],{"class":205},[100,44199,2770],{"class":209},[100,44201,206],{"class":205},[100,44203,132],{"class":118},[14,44205,218],{},[91,44207,44209],{"className":93,"code":44208,"language":95,"meta":96,"style":96},"Traceback (most recent call last):\n  File \"example.py\", line 3, in \u003Cmodule>\n    print(data[\"age\"])\nKeyError: 'age'\n",[17,44210,44211,44221,44247,44265],{"__ignoreMap":96},[100,44212,44213,44215,44217,44219],{"class":102,"line":103},[100,44214,514],{"class":178},[100,44216,170],{"class":118},[100,44218,519],{"class":178},[100,44220,522],{"class":118},[100,44222,44223,44225,44227,44229,44231,44233,44235,44237,44239,44241,44243,44245],{"class":102,"line":135},[100,44224,1952],{"class":106},[100,44226,206],{"class":205},[100,44228,1957],{"class":209},[100,44230,206],{"class":205},[100,44232,126],{"class":118},[100,44234,1964],{"class":106},[100,44236,11541],{"class":122},[100,44238,126],{"class":118},[100,44240,1971],{"class":110},[100,44242,1974],{"class":110},[100,44244,1977],{"class":106},[100,44246,1980],{"class":110},[100,44248,44249,44251,44253,44255,44257,44259,44261,44263],{"class":102,"line":142},[100,44250,200],{"class":114},[100,44252,170],{"class":118},[100,44254,7934],{"class":178},[100,44256,3166],{"class":118},[100,44258,206],{"class":205},[100,44260,2770],{"class":209},[100,44262,206],{"class":205},[100,44264,132],{"class":118},[100,44266,44267,44269,44271,44273,44275],{"class":102,"line":152},[100,44268,17063],{"class":191},[100,44270,89],{"class":118},[100,44272,1274],{"class":205},[100,44274,2770],{"class":209},[100,44276,3925],{"class":205},[14,44278,42648,44279,3266,44281,42654,44284,290],{},[17,44280,12456],{},[17,44282,44283],{},"\"city\"",[17,44285,42657],{},[14,44287,44288,44289,44291,44292,290],{},"Because of that, ",[17,44290,43960],{}," raises a ",[17,44293,17063],{},[77,44295,25091],{"id":25090},[14,44297,38066],{},[40,44299,44300,44303,44306,44313],{},[43,44301,44302],{},"The key is missing from the dictionary.",[43,44304,44305],{},"The key name is spelled differently.",[43,44307,44308,44309,18659,44311,290],{},"The key uses different capitalization, such as ",[17,44310,42764],{},[17,44312,12456],{},[43,44314,44315],{},"You expected data from a file or API, but that key was not included.",[14,44317,3971],{},[40,44319,44320,44323,44326,44329,44332],{},[43,44321,44322],{},"Accessing a missing key with square brackets",[43,44324,44325],{},"Typing the wrong key name",[43,44327,44328],{},"Using the wrong letter case in the key",[43,44330,44331],{},"Assuming API or JSON data always contains a key",[43,44333,44334],{},"Reading user input that does not match dictionary keys",[77,44336,44338],{"id":44337},"fix-1-check-if-the-key-exists-first","Fix 1: Check if the key exists first",[14,44340,241,44341,44344],{},[17,44342,44343],{},"if key in my_dict"," before reading the value.",[14,44346,44347],{},"This is clear and beginner-friendly. It is a good choice when you want different behavior if the key is missing.",[91,44349,44351],{"className":93,"code":44350,"language":95,"meta":96,"style":96},"data = {\"name\": \"Sam\"}\n\nif \"age\" in data:\n    print(data[\"age\"])\nelse:\n    print(\"Key not found\")\n",[17,44352,44353,44377,44381,44397,44415,44421],{"__ignoreMap":96},[100,44354,44355,44357,44359,44361,44363,44365,44367,44369,44371,44373,44375],{"class":102,"line":103},[100,44356,7909],{"class":106},[100,44358,111],{"class":110},[100,44360,12080],{"class":118},[100,44362,206],{"class":205},[100,44364,2853],{"class":209},[100,44366,206],{"class":205},[100,44368,89],{"class":118},[100,44370,1708],{"class":205},[100,44372,29329],{"class":209},[100,44374,206],{"class":205},[100,44376,12093],{"class":118},[100,44378,44379],{"class":102,"line":135},[100,44380,139],{"emptyLinePlaceholder":138},[100,44382,44383,44385,44387,44389,44391,44393,44395],{"class":102,"line":142},[100,44384,2736],{"class":145},[100,44386,1708],{"class":205},[100,44388,2770],{"class":209},[100,44390,206],{"class":205},[100,44392,1971],{"class":110},[100,44394,12102],{"class":106},[100,44396,149],{"class":118},[100,44398,44399,44401,44403,44405,44407,44409,44411,44413],{"class":102,"line":152},[100,44400,200],{"class":114},[100,44402,170],{"class":118},[100,44404,7934],{"class":178},[100,44406,3166],{"class":118},[100,44408,206],{"class":205},[100,44410,2770],{"class":209},[100,44412,206],{"class":205},[100,44414,132],{"class":118},[100,44416,44417,44419],{"class":102,"line":164},[100,44418,4944],{"class":145},[100,44420,149],{"class":118},[100,44422,44423,44425,44427,44429,44431,44433],{"class":102,"line":185},[100,44424,200],{"class":114},[100,44426,170],{"class":118},[100,44428,206],{"class":205},[100,44430,42470],{"class":209},[100,44432,206],{"class":205},[100,44434,215],{"class":118},[14,44436,218],{},[91,44438,44440],{"className":93,"code":44439,"language":95,"meta":96,"style":96},"Key not found\n",[17,44441,44442],{"__ignoreMap":96},[100,44443,44444,44446,44448],{"class":102,"line":103},[100,44445,43717],{"class":106},[100,44447,1059],{"class":110},[100,44449,43722],{"class":106},[14,44451,44452],{},"This approach makes your program explicit. You decide what should happen when the key is missing.",[14,44454,44455,44456,290],{},"For a full example, see ",[295,44457,43100],{"href":43099},[77,44459,44461],{"id":44460},"fix-2-use-get-for-safe-access","Fix 2: Use get() for safe access",[14,44463,44464,551,44466,43833],{},[17,44465,43838],{},[17,44467,930],{},[14,44469,44470,44471,290],{},"You can also give a default value, like ",[17,44472,44473],{},"dict.get(key, 0)",[14,44475,42970],{},[91,44477,44479],{"className":93,"code":44478,"language":95,"meta":96,"style":96},"data = {\"name\": \"Sam\"}\n\nprint(data.get(\"age\"))\nprint(data.get(\"age\", 0))\n",[17,44480,44481,44505,44509,44531],{"__ignoreMap":96},[100,44482,44483,44485,44487,44489,44491,44493,44495,44497,44499,44501,44503],{"class":102,"line":103},[100,44484,7909],{"class":106},[100,44486,111],{"class":110},[100,44488,12080],{"class":118},[100,44490,206],{"class":205},[100,44492,2853],{"class":209},[100,44494,206],{"class":205},[100,44496,89],{"class":118},[100,44498,1708],{"class":205},[100,44500,29329],{"class":209},[100,44502,206],{"class":205},[100,44504,12093],{"class":118},[100,44506,44507],{"class":102,"line":135},[100,44508,139],{"emptyLinePlaceholder":138},[100,44510,44511,44513,44515,44517,44519,44521,44523,44525,44527,44529],{"class":102,"line":142},[100,44512,372],{"class":114},[100,44514,170],{"class":118},[100,44516,7934],{"class":178},[100,44518,290],{"class":118},[100,44520,12412],{"class":178},[100,44522,170],{"class":118},[100,44524,206],{"class":205},[100,44526,2770],{"class":209},[100,44528,206],{"class":205},[100,44530,182],{"class":118},[100,44532,44533,44535,44537,44539,44541,44543,44545,44547,44549,44551,44553,44555],{"class":102,"line":152},[100,44534,372],{"class":114},[100,44536,170],{"class":118},[100,44538,7934],{"class":178},[100,44540,290],{"class":118},[100,44542,12412],{"class":178},[100,44544,170],{"class":118},[100,44546,206],{"class":205},[100,44548,2770],{"class":209},[100,44550,206],{"class":205},[100,44552,126],{"class":118},[100,44554,1921],{"class":122},[100,44556,182],{"class":118},[14,44558,218],{},[91,44560,44562],{"className":93,"code":44561,"language":95,"meta":96,"style":96},"None\n0\n",[17,44563,44564,44568],{"__ignoreMap":96},[100,44565,44566],{"class":102,"line":103},[100,44567,924],{"class":158},[100,44569,44570],{"class":102,"line":135},[100,44571,44572],{"class":122},"0\n",[14,44574,44575],{},"This is often the easiest fix for beginners.",[14,44577,44578,44579,290],{},"If you want to learn this method in more detail, see the ",[295,44580,12577],{"href":42479},[77,44582,44584],{"id":44583},"fix-3-inspect-the-dictionary-keys","Fix 3: Inspect the dictionary keys",[14,44586,44587],{},"Sometimes the problem is not that the key is missing. The real problem is that the key name is different from what you expected.",[14,44589,44590],{},"Print the dictionary or its keys to see what is actually available.",[91,44592,44594],{"className":93,"code":44593,"language":95,"meta":96,"style":96},"data = {\"Name\": \"Sam\", \"city\": \"Boston\"}\n\nprint(data)\nprint(data.keys())\n",[17,44595,44596,44636,44640,44650],{"__ignoreMap":96},[100,44597,44598,44600,44602,44604,44606,44608,44610,44612,44614,44616,44618,44620,44622,44624,44626,44628,44630,44632,44634],{"class":102,"line":103},[100,44599,7909],{"class":106},[100,44601,111],{"class":110},[100,44603,12080],{"class":118},[100,44605,206],{"class":205},[100,44607,42750],{"class":209},[100,44609,206],{"class":205},[100,44611,89],{"class":118},[100,44613,1708],{"class":205},[100,44615,29329],{"class":209},[100,44617,206],{"class":205},[100,44619,126],{"class":118},[100,44621,1708],{"class":205},[100,44623,43636],{"class":209},[100,44625,206],{"class":205},[100,44627,89],{"class":118},[100,44629,1708],{"class":205},[100,44631,44177],{"class":209},[100,44633,206],{"class":205},[100,44635,12093],{"class":118},[100,44637,44638],{"class":102,"line":135},[100,44639,139],{"emptyLinePlaceholder":138},[100,44641,44642,44644,44646,44648],{"class":102,"line":142},[100,44643,372],{"class":114},[100,44645,170],{"class":118},[100,44647,7934],{"class":178},[100,44649,215],{"class":118},[100,44651,44652,44654,44656,44658,44660,44662],{"class":102,"line":152},[100,44653,372],{"class":114},[100,44655,170],{"class":118},[100,44657,7934],{"class":178},[100,44659,290],{"class":118},[100,44661,6185],{"class":178},[100,44663,3370],{"class":118},[14,44665,218],{},[91,44667,44669],{"className":93,"code":44668,"language":95,"meta":96,"style":96},"{'Name': 'Sam', 'city': 'Boston'}\ndict_keys(['Name', 'city'])\n",[17,44670,44671,44707],{"__ignoreMap":96},[100,44672,44673,44675,44677,44679,44681,44683,44685,44687,44689,44691,44693,44695,44697,44699,44701,44703,44705],{"class":102,"line":103},[100,44674,16490],{"class":118},[100,44676,1280],{"class":205},[100,44678,42750],{"class":209},[100,44680,1280],{"class":205},[100,44682,89],{"class":118},[100,44684,1274],{"class":205},[100,44686,29329],{"class":209},[100,44688,1280],{"class":205},[100,44690,126],{"class":118},[100,44692,1274],{"class":205},[100,44694,43636],{"class":209},[100,44696,1280],{"class":205},[100,44698,89],{"class":118},[100,44700,1274],{"class":205},[100,44702,44177],{"class":209},[100,44704,1280],{"class":205},[100,44706,12093],{"class":118},[100,44708,44709,44711,44713,44715,44717,44719,44721,44723,44725,44727],{"class":102,"line":135},[100,44710,43660],{"class":178},[100,44712,119],{"class":118},[100,44714,1280],{"class":205},[100,44716,42750],{"class":209},[100,44718,1280],{"class":205},[100,44720,126],{"class":118},[100,44722,1274],{"class":205},[100,44724,43636],{"class":209},[100,44726,1280],{"class":205},[100,44728,132],{"class":118},[14,44730,44731,44732,9472,44734,290],{},"Now you can see that the key is ",[17,44733,42764],{},[17,44735,12456],{},[14,44737,44738],{},"You can also use these debugging checks:",[91,44740,44742],{"className":93,"code":44741,"language":95,"meta":96,"style":96},"my_dict = {\"Name\": \"Sam\", \"city\": \"Boston\"}\nkey_name = \"name\"\n\nprint(my_dict)\nprint(my_dict.keys())\nprint(\"wanted key:\", key_name)\nprint(\"exists:\", key_name in my_dict)\n",[17,44743,44744,44785,44798,44802,44812,44826,44846],{"__ignoreMap":96},[100,44745,44746,44749,44751,44753,44755,44757,44759,44761,44763,44765,44767,44769,44771,44773,44775,44777,44779,44781,44783],{"class":102,"line":103},[100,44747,44748],{"class":106},"my_dict ",[100,44750,111],{"class":110},[100,44752,12080],{"class":118},[100,44754,206],{"class":205},[100,44756,42750],{"class":209},[100,44758,206],{"class":205},[100,44760,89],{"class":118},[100,44762,1708],{"class":205},[100,44764,29329],{"class":209},[100,44766,206],{"class":205},[100,44768,126],{"class":118},[100,44770,1708],{"class":205},[100,44772,43636],{"class":209},[100,44774,206],{"class":205},[100,44776,89],{"class":118},[100,44778,1708],{"class":205},[100,44780,44177],{"class":209},[100,44782,206],{"class":205},[100,44784,12093],{"class":118},[100,44786,44787,44790,44792,44794,44796],{"class":102,"line":135},[100,44788,44789],{"class":106},"key_name ",[100,44791,111],{"class":110},[100,44793,1708],{"class":205},[100,44795,2853],{"class":209},[100,44797,1714],{"class":205},[100,44799,44800],{"class":102,"line":142},[100,44801,139],{"emptyLinePlaceholder":138},[100,44803,44804,44806,44808,44810],{"class":102,"line":152},[100,44805,372],{"class":114},[100,44807,170],{"class":118},[100,44809,17794],{"class":178},[100,44811,215],{"class":118},[100,44813,44814,44816,44818,44820,44822,44824],{"class":102,"line":164},[100,44815,372],{"class":114},[100,44817,170],{"class":118},[100,44819,17794],{"class":178},[100,44821,290],{"class":118},[100,44823,6185],{"class":178},[100,44825,3370],{"class":118},[100,44827,44828,44830,44832,44834,44837,44839,44841,44844],{"class":102,"line":185},[100,44829,372],{"class":114},[100,44831,170],{"class":118},[100,44833,206],{"class":205},[100,44835,44836],{"class":209},"wanted key:",[100,44838,206],{"class":205},[100,44840,126],{"class":118},[100,44842,44843],{"class":178}," key_name",[100,44845,215],{"class":118},[100,44847,44848,44850,44852,44854,44857,44859,44861,44864,44866,44868],{"class":102,"line":197},[100,44849,372],{"class":114},[100,44851,170],{"class":118},[100,44853,206],{"class":205},[100,44855,44856],{"class":209},"exists:",[100,44858,206],{"class":205},[100,44860,126],{"class":118},[100,44862,44863],{"class":178}," key_name ",[100,44865,617],{"class":145},[100,44867,43305],{"class":178},[100,44869,215],{"class":118},[14,44871,218],{},[91,44873,44875],{"className":93,"code":44874,"language":95,"meta":96,"style":96},"{'Name': 'Sam', 'city': 'Boston'}\ndict_keys(['Name', 'city'])\nwanted key: name\nexists: False\n",[17,44876,44877,44913,44935,44944],{"__ignoreMap":96},[100,44878,44879,44881,44883,44885,44887,44889,44891,44893,44895,44897,44899,44901,44903,44905,44907,44909,44911],{"class":102,"line":103},[100,44880,16490],{"class":118},[100,44882,1280],{"class":205},[100,44884,42750],{"class":209},[100,44886,1280],{"class":205},[100,44888,89],{"class":118},[100,44890,1274],{"class":205},[100,44892,29329],{"class":209},[100,44894,1280],{"class":205},[100,44896,126],{"class":118},[100,44898,1274],{"class":205},[100,44900,43636],{"class":209},[100,44902,1280],{"class":205},[100,44904,89],{"class":118},[100,44906,1274],{"class":205},[100,44908,44177],{"class":209},[100,44910,1280],{"class":205},[100,44912,12093],{"class":118},[100,44914,44915,44917,44919,44921,44923,44925,44927,44929,44931,44933],{"class":102,"line":135},[100,44916,43660],{"class":178},[100,44918,119],{"class":118},[100,44920,1280],{"class":205},[100,44922,42750],{"class":209},[100,44924,1280],{"class":205},[100,44926,126],{"class":118},[100,44928,1274],{"class":205},[100,44930,43636],{"class":209},[100,44932,1280],{"class":205},[100,44934,132],{"class":118},[100,44936,44937,44940,44942],{"class":102,"line":142},[100,44938,44939],{"class":106},"wanted key",[100,44941,89],{"class":118},[100,44943,2898],{"class":106},[100,44945,44946,44948,44950],{"class":102,"line":152},[100,44947,18114],{"class":106},[100,44949,89],{"class":118},[100,44951,44952],{"class":158}," False\n",[14,44954,44955],{},"This helps you find:",[40,44957,44958,44960,44963],{},[43,44959,9676],{},[43,44961,44962],{},"extra spaces",[43,44964,44965],{},"uppercase\u002Flowercase differences",[14,44967,44968,44969,290],{},"You can learn more about this on the ",[295,44970,43345],{"href":43344},[77,44972,44974],{"id":44973},"fix-4-handle-the-error-with-try-except","Fix 4: Handle the error with try-except",[14,44976,241,44977,44979],{},[17,44978,42796],{}," when missing keys are possible.",[14,44981,44982],{},"This is useful when reading uncertain external data, such as JSON, API responses, or user-provided values.",[91,44984,44986],{"className":93,"code":44985,"language":95,"meta":96,"style":96},"data = {\"name\": \"Sam\"}\n\ntry:\n    print(data[\"age\"])\nexcept KeyError:\n    print(\"The key was not found\")\n",[17,44987,44988,45012,45016,45022,45040,45048],{"__ignoreMap":96},[100,44989,44990,44992,44994,44996,44998,45000,45002,45004,45006,45008,45010],{"class":102,"line":103},[100,44991,7909],{"class":106},[100,44993,111],{"class":110},[100,44995,12080],{"class":118},[100,44997,206],{"class":205},[100,44999,2853],{"class":209},[100,45001,206],{"class":205},[100,45003,89],{"class":118},[100,45005,1708],{"class":205},[100,45007,29329],{"class":209},[100,45009,206],{"class":205},[100,45011,12093],{"class":118},[100,45013,45014],{"class":102,"line":135},[100,45015,139],{"emptyLinePlaceholder":138},[100,45017,45018,45020],{"class":102,"line":142},[100,45019,146],{"class":145},[100,45021,149],{"class":118},[100,45023,45024,45026,45028,45030,45032,45034,45036,45038],{"class":102,"line":152},[100,45025,200],{"class":114},[100,45027,170],{"class":118},[100,45029,7934],{"class":178},[100,45031,3166],{"class":118},[100,45033,206],{"class":205},[100,45035,2770],{"class":209},[100,45037,206],{"class":205},[100,45039,132],{"class":118},[100,45041,45042,45044,45046],{"class":102,"line":164},[100,45043,188],{"class":145},[100,45045,43194],{"class":191},[100,45047,149],{"class":118},[100,45049,45050,45052,45054,45056,45059,45061],{"class":102,"line":185},[100,45051,200],{"class":114},[100,45053,170],{"class":118},[100,45055,206],{"class":205},[100,45057,45058],{"class":209},"The key was not found",[100,45060,206],{"class":205},[100,45062,215],{"class":118},[14,45064,218],{},[91,45066,45068],{"className":93,"code":45067,"language":95,"meta":96,"style":96},"The key was not found\n",[17,45069,45070],{"__ignoreMap":96},[100,45071,45072,45075,45077],{"class":102,"line":103},[100,45073,45074],{"class":106},"The key was ",[100,45076,1059],{"class":110},[100,45078,43722],{"class":106},[14,45080,45081,45082,45084],{},"What ",[17,45083,188],{}," does:",[40,45086,45087,45092,45101],{},[43,45088,45089,45090],{},"Python tries the code inside ",[17,45091,146],{},[43,45093,45094,45095,45097,45098,45100],{},"If a ",[17,45096,17063],{}," happens, Python runs the ",[17,45099,188],{}," block instead",[43,45102,45103],{},"This prevents the program from crashing immediately",[14,45105,45106,45107,45109,45110,45112],{},"Keep this method simple. In many cases, ",[17,45108,13068],{}," or an ",[17,45111,617],{}," check is easier to read.",[77,45114,43251],{"id":43250},[14,45116,45117],{},"If you are not sure why the error is happening, try these steps:",[40,45119,45120,45123,45126,45129],{},[43,45121,45122],{},"Print the missing key value you are trying to use.",[43,45124,45125],{},"Print the dictionary keys before access.",[43,45127,45128],{},"Check spelling, spaces, and uppercase\u002Flowercase letters.",[43,45130,45131],{},"Confirm the dictionary was filled correctly before reading from it.",[14,45133,5295],{},[91,45135,45137],{"className":93,"code":45136,"language":95,"meta":96,"style":96},"print(my_dict)\nprint(my_dict.keys())\nprint(\"wanted key:\", key_name)\nprint(\"exists:\", key_name in my_dict)\n",[17,45138,45139,45149,45163,45181],{"__ignoreMap":96},[100,45140,45141,45143,45145,45147],{"class":102,"line":103},[100,45142,372],{"class":114},[100,45144,170],{"class":118},[100,45146,17794],{"class":178},[100,45148,215],{"class":118},[100,45150,45151,45153,45155,45157,45159,45161],{"class":102,"line":135},[100,45152,372],{"class":114},[100,45154,170],{"class":118},[100,45156,17794],{"class":178},[100,45158,290],{"class":118},[100,45160,6185],{"class":178},[100,45162,3370],{"class":118},[100,45164,45165,45167,45169,45171,45173,45175,45177,45179],{"class":102,"line":142},[100,45166,372],{"class":114},[100,45168,170],{"class":118},[100,45170,206],{"class":205},[100,45172,44836],{"class":209},[100,45174,206],{"class":205},[100,45176,126],{"class":118},[100,45178,44843],{"class":178},[100,45180,215],{"class":118},[100,45182,45183,45185,45187,45189,45191,45193,45195,45197,45199,45201],{"class":102,"line":152},[100,45184,372],{"class":114},[100,45186,170],{"class":118},[100,45188,206],{"class":205},[100,45190,44856],{"class":209},[100,45192,206],{"class":205},[100,45194,126],{"class":118},[100,45196,44863],{"class":178},[100,45198,617],{"class":145},[100,45200,43305],{"class":178},[100,45202,215],{"class":118},[14,45204,45205],{},"These checks often reveal the problem quickly.",[77,45207,45209,45210,45212],{"id":45208},"when-to-use-vs-get","When to use ",[100,45211],{}," vs get()",[14,45214,45215,45216,45219],{},"Use square brackets ",[17,45217,45218],{},"[]"," when the key must exist.",[14,45221,241,45222,45224],{},[17,45223,13068],{}," when the key may be missing.",[14,45226,45227],{},"The difference is important:",[40,45229,45230,45238,45246],{},[43,45231,45232,20442,45235,45237],{},[17,45233,45234],{},"my_dict[\"key\"]",[17,45236,17063],{}," if the key is missing",[43,45239,45240,551,45243,45245],{},[17,45241,45242],{},"my_dict.get(\"key\")",[17,45244,930],{}," instead",[43,45247,45248,45251],{},[17,45249,45250],{},"my_dict.get(\"key\", default_value)"," returns your default value",[14,45253,1844],{},[91,45255,45257],{"className":93,"code":45256,"language":95,"meta":96,"style":96},"data = {\"name\": \"Sam\"}\n\n# Use this when the key must exist\nprint(data[\"name\"])\n\n# Use this when the key may be missing\nprint(data.get(\"age\"))\n\n# Use this when you want a fallback value\nprint(data.get(\"age\", \"Unknown\"))\n",[17,45258,45259,45283,45287,45292,45310,45314,45319,45341,45345,45350],{"__ignoreMap":96},[100,45260,45261,45263,45265,45267,45269,45271,45273,45275,45277,45279,45281],{"class":102,"line":103},[100,45262,7909],{"class":106},[100,45264,111],{"class":110},[100,45266,12080],{"class":118},[100,45268,206],{"class":205},[100,45270,2853],{"class":209},[100,45272,206],{"class":205},[100,45274,89],{"class":118},[100,45276,1708],{"class":205},[100,45278,29329],{"class":209},[100,45280,206],{"class":205},[100,45282,12093],{"class":118},[100,45284,45285],{"class":102,"line":135},[100,45286,139],{"emptyLinePlaceholder":138},[100,45288,45289],{"class":102,"line":142},[100,45290,45291],{"class":414},"# Use this when the key must exist\n",[100,45293,45294,45296,45298,45300,45302,45304,45306,45308],{"class":102,"line":152},[100,45295,372],{"class":114},[100,45297,170],{"class":118},[100,45299,7934],{"class":178},[100,45301,3166],{"class":118},[100,45303,206],{"class":205},[100,45305,2853],{"class":209},[100,45307,206],{"class":205},[100,45309,132],{"class":118},[100,45311,45312],{"class":102,"line":164},[100,45313,139],{"emptyLinePlaceholder":138},[100,45315,45316],{"class":102,"line":185},[100,45317,45318],{"class":414},"# Use this when the key may be missing\n",[100,45320,45321,45323,45325,45327,45329,45331,45333,45335,45337,45339],{"class":102,"line":197},[100,45322,372],{"class":114},[100,45324,170],{"class":118},[100,45326,7934],{"class":178},[100,45328,290],{"class":118},[100,45330,12412],{"class":178},[100,45332,170],{"class":118},[100,45334,206],{"class":205},[100,45336,2770],{"class":209},[100,45338,206],{"class":205},[100,45340,182],{"class":118},[100,45342,45343],{"class":102,"line":771},[100,45344,139],{"emptyLinePlaceholder":138},[100,45346,45347],{"class":102,"line":787},[100,45348,45349],{"class":414},"# Use this when you want a fallback value\n",[100,45351,45352,45354,45356,45358,45360,45362,45364,45366,45368,45370,45372,45374,45377,45379],{"class":102,"line":5816},[100,45353,372],{"class":114},[100,45355,170],{"class":118},[100,45357,7934],{"class":178},[100,45359,290],{"class":118},[100,45361,12412],{"class":178},[100,45363,170],{"class":118},[100,45365,206],{"class":205},[100,45367,2770],{"class":209},[100,45369,206],{"class":205},[100,45371,126],{"class":118},[100,45373,1708],{"class":205},[100,45375,45376],{"class":209},"Unknown",[100,45378,206],{"class":205},[100,45380,182],{"class":118},[14,45382,218],{},[91,45384,45386],{"className":93,"code":45385,"language":95,"meta":96,"style":96},"Sam\nNone\nUnknown\n",[17,45387,45388,45392,45396],{"__ignoreMap":96},[100,45389,45390],{"class":102,"line":103},[100,45391,16526],{"class":106},[100,45393,45394],{"class":102,"line":135},[100,45395,924],{"class":158},[100,45397,45398],{"class":102,"line":142},[100,45399,45400],{"class":106},"Unknown\n",[14,45402,45403,45404,290],{},"If you want more practice, see ",[295,45405,45406],{"href":43893},"how to access values in a dictionary in Python",[77,45408,1514],{"id":1513},[675,45410,45412],{"id":45411},"why-does-get-not-raise-keyerror","Why does get() not raise KeyError?",[14,45414,14478,45415,45417,45418,45420],{},[17,45416,13068],{}," is designed for safe dictionary access. It returns ",[17,45419,930],{}," or a default value instead of stopping with an error.",[675,45422,45424],{"id":45423},"is-keyerror-the-same-as-indexerror","Is KeyError the same as IndexError?",[14,45426,14506,45427,45429,45430,45432],{},[17,45428,17063],{}," is for missing dictionary keys. ",[17,45431,16420],{}," is for invalid list or tuple positions.",[14,45434,45435,45436,290],{},"For a direct comparison, see ",[295,45437,34847],{"href":45438},"\u002Ferrors\u002Fkeyerror-vs-indexerror-in-python-explained",[675,45440,45442],{"id":45441},"how-do-i-give-a-default-value-if-a-key-is-missing","How do I give a default value if a key is missing?",[14,45444,241,45445,290],{},[17,45446,45447],{},"dict.get(\"key\", default_value)",[14,45449,1844],{},[91,45451,45453],{"className":93,"code":45452,"language":95,"meta":96,"style":96},"data = {\"name\": \"Sam\"}\nprint(data.get(\"age\", 0))\n",[17,45454,45455,45479],{"__ignoreMap":96},[100,45456,45457,45459,45461,45463,45465,45467,45469,45471,45473,45475,45477],{"class":102,"line":103},[100,45458,7909],{"class":106},[100,45460,111],{"class":110},[100,45462,12080],{"class":118},[100,45464,206],{"class":205},[100,45466,2853],{"class":209},[100,45468,206],{"class":205},[100,45470,89],{"class":118},[100,45472,1708],{"class":205},[100,45474,29329],{"class":209},[100,45476,206],{"class":205},[100,45478,12093],{"class":118},[100,45480,45481,45483,45485,45487,45489,45491,45493,45495,45497,45499,45501,45503],{"class":102,"line":135},[100,45482,372],{"class":114},[100,45484,170],{"class":118},[100,45486,7934],{"class":178},[100,45488,290],{"class":118},[100,45490,12412],{"class":178},[100,45492,170],{"class":118},[100,45494,206],{"class":205},[100,45496,2770],{"class":209},[100,45498,206],{"class":205},[100,45500,126],{"class":118},[100,45502,1921],{"class":122},[100,45504,182],{"class":118},[675,45506,45508],{"id":45507},"can-capitalization-cause-a-keyerror","Can capitalization cause a KeyError?",[14,45510,45511,45512,3266,45514,45516],{},"Yes. Dictionary keys must match exactly, so ",[17,45513,42764],{},[17,45515,12456],{}," are different keys.",[77,45518,1554],{"id":1553},[40,45520,45521,45525,45529,45533,45537,45541],{},[43,45522,45523],{},[295,45524,42671],{"href":42670},[43,45526,45527],{},[295,45528,12577],{"href":42479},[43,45530,45531],{},[295,45532,43345],{"href":43344},[43,45534,45535],{},[295,45536,43888],{"href":43099},[43,45538,45539],{},[295,45540,43894],{"href":43893},[43,45542,45543],{},[295,45544,17238],{"href":43938},[14,45546,45547,45548,45550,45551,45553],{},"Next step: practice safe dictionary access by using ",[17,45549,13068],{},", checking keys with ",[17,45552,617],{},", and learning a few basic dictionary methods.",[1589,45555,45556],{},"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 .sP7_E, html code.shiki .sP7_E{--shiki-light:#39ADB5;--shiki-default:#24292E;--shiki-dark:#E1E4E8}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 .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 .slqww, html code.shiki .slqww{--shiki-light:#6182B8;--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 .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 .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 .srdBf, html code.shiki .srdBf{--shiki-light:#F76D47;--shiki-default:#005CC5;--shiki-dark:#79B8FF}html pre.shiki code .sZMiF, html code.shiki .sZMiF{--shiki-light:#E2931D;--shiki-default:#005CC5;--shiki-dark:#79B8FF}html pre.shiki code .s39Yj, html code.shiki .s39Yj{--shiki-light:#39ADB5;--shiki-default:#005CC5;--shiki-dark:#79B8FF}",{"title":96,"searchDepth":135,"depth":135,"links":45558},[45559,45560,45561,45562,45563,45564,45565,45566,45567,45568,45570,45576],{"id":79,"depth":135,"text":80},{"id":5880,"depth":135,"text":5881},{"id":11030,"depth":135,"text":11031},{"id":25090,"depth":135,"text":25091},{"id":44337,"depth":135,"text":44338},{"id":44460,"depth":135,"text":44461},{"id":44583,"depth":135,"text":44584},{"id":44973,"depth":135,"text":44974},{"id":43250,"depth":135,"text":43251},{"id":45208,"depth":135,"text":45569},"When to use  vs get()",{"id":1513,"depth":135,"text":1514,"children":45571},[45572,45573,45574,45575],{"id":45411,"depth":142,"text":45412},{"id":45423,"depth":142,"text":45424},{"id":45441,"depth":142,"text":45442},{"id":45507,"depth":142,"text":45508},{"id":1553,"depth":135,"text":1554},"Master keyerror key not found in dictionary fix in our comprehensive Python beginner guide.",{},{"title":43944,"description":45577},"errors\u002Fkeyerror-key-not-found-in-dictionary-fix","ATYHVmq93Gy7qZqL9H4Jspv6qFrJ8mxaz1eQl8Rz8Ds",{"id":45583,"title":45584,"body":45585,"description":46845,"extension":1623,"meta":46846,"navigation":138,"path":46847,"seo":46848,"stem":46849,"__hash__":46850},"content\u002Ferrors\u002Fkeyerror-popitem-dictionary-is-empty-fix.md","KeyError: popitem(): dictionary is empty (Fix)",{"type":7,"value":45586,"toc":46821},[45587,45590,45600,45603,45605,45610,45677,45686,45688,45693,45696,45714,45718,45720,45726,45749,45751,45805,45807,45827,45836,45838,45840,45857,45859,45910,45913,45915,45918,45922,45925,46011,46013,46038,46043,46055,46059,46064,46133,46139,46143,46146,46223,46229,46233,46239,46241,46303,46312,46319,46323,46326,46339,46341,46428,46430,46438,46441,46443,46448,46450,46508,46511,46539,46542,46566,46569,46650,46652,46694,46696,46699,46727,46732,46737,46739,46746,46752,46758,46763,46767,46775,46779,46785,46787,46812,46818],[10,45588,45584],{"id":45589},"keyerror-popitem-dictionary-is-empty-fix",[14,45591,31738,45592,45595,45596,45599],{},[17,45593,45594],{},"\"KeyError: popitem(): dictionary is empty\"",". This error happens when you call ",[17,45597,45598],{},"popitem()"," on a dictionary that has no items left.",[14,45601,45602],{},"This page explains what the error means, shows a small example that causes it, and gives simple ways to avoid it.",[77,45604,80],{"id":79},[14,45606,45607,45608,89],{},"Check that the dictionary is not empty before calling ",[17,45609,45598],{},[91,45611,45613],{"className":93,"code":45612,"language":95,"meta":96,"style":96},"data = {}\n\nif data:\n    key, value = data.popitem()\nelse:\n    print(\"Dictionary is empty\")\n",[17,45614,45615,45624,45628,45636,45656,45662],{"__ignoreMap":96},[100,45616,45617,45619,45621],{"class":102,"line":103},[100,45618,7909],{"class":106},[100,45620,111],{"class":110},[100,45622,45623],{"class":118}," {}\n",[100,45625,45626],{"class":102,"line":135},[100,45627,139],{"emptyLinePlaceholder":138},[100,45629,45630,45632,45634],{"class":102,"line":142},[100,45631,2736],{"class":145},[100,45633,12102],{"class":106},[100,45635,149],{"class":118},[100,45637,45638,45641,45643,45645,45647,45649,45651,45654],{"class":102,"line":152},[100,45639,45640],{"class":106},"    key",[100,45642,126],{"class":118},[100,45644,989],{"class":106},[100,45646,111],{"class":110},[100,45648,12102],{"class":106},[100,45650,290],{"class":118},[100,45652,45653],{"class":178},"popitem",[100,45655,4734],{"class":118},[100,45657,45658,45660],{"class":102,"line":164},[100,45659,4944],{"class":145},[100,45661,149],{"class":118},[100,45663,45664,45666,45668,45670,45673,45675],{"class":102,"line":185},[100,45665,200],{"class":114},[100,45667,170],{"class":118},[100,45669,206],{"class":205},[100,45671,45672],{"class":209},"Dictionary is empty",[100,45674,206],{"class":205},[100,45676,215],{"class":118},[14,45678,45679,45680,36202,45682,45685],{},"An empty dictionary is ",[17,45681,1649],{},[17,45683,45684],{},"if data:"," is a simple way to avoid this error.",[77,45687,5881],{"id":5880},[14,45689,45690,45692],{},[17,45691,45598],{}," is a dictionary method that removes and returns one key-value pair.",[14,45694,45695],{},"If the dictionary is empty, there is nothing to remove. Python raises:",[91,45697,45699],{"className":93,"code":45698,"language":95,"meta":96,"style":96},"KeyError: 'popitem(): dictionary is empty'\n",[17,45700,45701],{"__ignoreMap":96},[100,45702,45703,45705,45707,45709,45712],{"class":102,"line":103},[100,45704,17063],{"class":191},[100,45706,89],{"class":118},[100,45708,1274],{"class":205},[100,45710,45711],{"class":209},"popitem(): dictionary is empty",[100,45713,3925],{"class":205},[14,45715,44129,45716,290],{},[295,45717,42671],{"href":42670},[77,45719,11031],{"id":11030},[14,45721,45722,45723,45725],{},"This code creates an empty dictionary and then calls ",[17,45724,45598],{}," on it:",[91,45727,45729],{"className":93,"code":45728,"language":95,"meta":96,"style":96},"data = {}\ndata.popitem()\n",[17,45730,45731,45739],{"__ignoreMap":96},[100,45732,45733,45735,45737],{"class":102,"line":103},[100,45734,7909],{"class":106},[100,45736,111],{"class":110},[100,45738,45623],{"class":118},[100,45740,45741,45743,45745,45747],{"class":102,"line":135},[100,45742,7934],{"class":106},[100,45744,290],{"class":118},[100,45746,45653],{"class":178},[100,45748,4734],{"class":118},[14,45750,218],{},[91,45752,45754],{"className":93,"code":45753,"language":95,"meta":96,"style":96},"Traceback (most recent call last):\n  File \"\u003Cstdin>\", line 2, in \u003Cmodule>\nKeyError: 'popitem(): dictionary is empty'\n",[17,45755,45756,45766,45793],{"__ignoreMap":96},[100,45757,45758,45760,45762,45764],{"class":102,"line":103},[100,45759,514],{"class":178},[100,45761,170],{"class":118},[100,45763,519],{"class":178},[100,45765,522],{"class":118},[100,45767,45768,45770,45772,45775,45777,45779,45781,45783,45785,45787,45789,45791],{"class":102,"line":135},[100,45769,1952],{"class":106},[100,45771,206],{"class":205},[100,45773,45774],{"class":209},"\u003Cstdin>",[100,45776,206],{"class":205},[100,45778,126],{"class":118},[100,45780,1964],{"class":106},[100,45782,559],{"class":122},[100,45784,126],{"class":118},[100,45786,1971],{"class":110},[100,45788,1974],{"class":110},[100,45790,1977],{"class":106},[100,45792,1980],{"class":110},[100,45794,45795,45797,45799,45801,45803],{"class":102,"line":142},[100,45796,17063],{"class":191},[100,45798,89],{"class":118},[100,45800,1274],{"class":205},[100,45802,45711],{"class":209},[100,45804,3925],{"class":205},[14,45806,25013],{},[40,45808,45809,45817,45822],{},[43,45810,45811,2025,45813,45816],{},[17,45812,7934],{},[17,45814,45815],{},"{}",", which means it has no key-value pairs",[43,45818,45819,45821],{},[17,45820,45598],{}," tries to remove one pair",[43,45823,45824,45825],{},"There is nothing to remove, so Python raises ",[17,45826,17063],{},[14,45828,45829,45830,290],{},"If you want to learn more about how this method works, see the ",[295,45831,45833,45834,4112],{"href":45832},"\u002Freference\u002Fpython-dictionary-popitem-method","Python dictionary ",[17,45835,45598],{},[77,45837,25091],{"id":25090},[14,45839,38066],{},[40,45841,45842,45845,45848,45854],{},[43,45843,45844],{},"The dictionary started empty",[43,45846,45847],{},"Items were removed earlier in the program",[43,45849,45850,45851,45853],{},"A loop kept calling ",[17,45852,45598],{}," after the dictionary became empty",[43,45855,45856],{},"You expected data from another function, but it returned an empty dictionary",[14,45858,39147],{},[91,45860,45862],{"className":93,"code":45861,"language":95,"meta":96,"style":96},"data = {\"a\": 1}\n\nwhile True:\n    print(data.popitem())\n",[17,45863,45864,45884,45888,45896],{"__ignoreMap":96},[100,45865,45866,45868,45870,45872,45874,45876,45878,45880,45882],{"class":102,"line":103},[100,45867,7909],{"class":106},[100,45869,111],{"class":110},[100,45871,12080],{"class":118},[100,45873,206],{"class":205},[100,45875,295],{"class":209},[100,45877,206],{"class":205},[100,45879,89],{"class":118},[100,45881,6915],{"class":122},[100,45883,12093],{"class":118},[100,45885,45886],{"class":102,"line":135},[100,45887,139],{"emptyLinePlaceholder":138},[100,45889,45890,45892,45894],{"class":102,"line":142},[100,45891,721],{"class":145},[100,45893,159],{"class":158},[100,45895,149],{"class":118},[100,45897,45898,45900,45902,45904,45906,45908],{"class":102,"line":152},[100,45899,200],{"class":114},[100,45901,170],{"class":118},[100,45903,7934],{"class":178},[100,45905,290],{"class":118},[100,45907,45653],{"class":178},[100,45909,3370],{"class":118},[14,45911,45912],{},"This removes the only item first. On the next loop, the dictionary is empty, so the error happens.",[77,45914,6244],{"id":6243},[14,45916,45917],{},"There are a few simple ways to fix this error.",[675,45919,45921],{"id":45920},"check-if-the-dictionary-has-items","Check if the dictionary has items",[14,45923,45924],{},"This is the clearest fix for beginners:",[91,45926,45928],{"className":93,"code":45927,"language":95,"meta":96,"style":96},"data = {\"name\": \"Alice\"}\n\nif data:\n    item = data.popitem()\n    print(item)\nelse:\n    print(\"Dictionary is empty\")\n",[17,45929,45930,45954,45958,45966,45981,45991,45997],{"__ignoreMap":96},[100,45931,45932,45934,45936,45938,45940,45942,45944,45946,45948,45950,45952],{"class":102,"line":103},[100,45933,7909],{"class":106},[100,45935,111],{"class":110},[100,45937,12080],{"class":118},[100,45939,206],{"class":205},[100,45941,2853],{"class":209},[100,45943,206],{"class":205},[100,45945,89],{"class":118},[100,45947,1708],{"class":205},[100,45949,15343],{"class":209},[100,45951,206],{"class":205},[100,45953,12093],{"class":118},[100,45955,45956],{"class":102,"line":135},[100,45957,139],{"emptyLinePlaceholder":138},[100,45959,45960,45962,45964],{"class":102,"line":142},[100,45961,2736],{"class":145},[100,45963,12102],{"class":106},[100,45965,149],{"class":118},[100,45967,45968,45971,45973,45975,45977,45979],{"class":102,"line":152},[100,45969,45970],{"class":106},"    item ",[100,45972,111],{"class":110},[100,45974,12102],{"class":106},[100,45976,290],{"class":118},[100,45978,45653],{"class":178},[100,45980,4734],{"class":118},[100,45982,45983,45985,45987,45989],{"class":102,"line":164},[100,45984,200],{"class":114},[100,45986,170],{"class":118},[100,45988,7555],{"class":178},[100,45990,215],{"class":118},[100,45992,45993,45995],{"class":102,"line":185},[100,45994,4944],{"class":145},[100,45996,149],{"class":118},[100,45998,45999,46001,46003,46005,46007,46009],{"class":102,"line":197},[100,46000,200],{"class":114},[100,46002,170],{"class":118},[100,46004,206],{"class":205},[100,46006,45672],{"class":209},[100,46008,206],{"class":205},[100,46010,215],{"class":118},[14,46012,218],{},[91,46014,46016],{"className":93,"code":46015,"language":95,"meta":96,"style":96},"('name', 'Alice')\n",[17,46017,46018],{"__ignoreMap":96},[100,46019,46020,46022,46024,46026,46028,46030,46032,46034,46036],{"class":102,"line":103},[100,46021,170],{"class":118},[100,46023,1280],{"class":205},[100,46025,2853],{"class":209},[100,46027,1280],{"class":205},[100,46029,126],{"class":118},[100,46031,1274],{"class":205},[100,46033,15343],{"class":209},[100,46035,1280],{"class":205},[100,46037,215],{"class":118},[14,46039,46040,46042],{},[17,46041,45684],{}," works because:",[40,46044,46045,46050],{},[43,46046,46047,46048],{},"non-empty dictionaries are ",[17,46049,19246],{},[43,46051,46052,46053],{},"empty dictionaries are ",[17,46054,1649],{},[675,46056,46058],{"id":46057},"use-an-explicit-length-check","Use an explicit length check",[14,46060,46061,46062,89],{},"If you want to be more explicit, use ",[17,46063,32803],{},[91,46065,46067],{"className":93,"code":46066,"language":95,"meta":96,"style":96},"data = {}\n\nif len(data) > 0:\n    print(data.popitem())\nelse:\n    print(\"Dictionary is empty\")\n",[17,46068,46069,46077,46081,46099,46113,46119],{"__ignoreMap":96},[100,46070,46071,46073,46075],{"class":102,"line":103},[100,46072,7909],{"class":106},[100,46074,111],{"class":110},[100,46076,45623],{"class":118},[100,46078,46079],{"class":102,"line":135},[100,46080,139],{"emptyLinePlaceholder":138},[100,46082,46083,46085,46087,46089,46091,46093,46095,46097],{"class":102,"line":142},[100,46084,2736],{"class":145},[100,46086,3141],{"class":114},[100,46088,170],{"class":118},[100,46090,7934],{"class":178},[100,46092,6155],{"class":118},[100,46094,31167],{"class":110},[100,46096,1921],{"class":122},[100,46098,149],{"class":118},[100,46100,46101,46103,46105,46107,46109,46111],{"class":102,"line":152},[100,46102,200],{"class":114},[100,46104,170],{"class":118},[100,46106,7934],{"class":178},[100,46108,290],{"class":118},[100,46110,45653],{"class":178},[100,46112,3370],{"class":118},[100,46114,46115,46117],{"class":102,"line":164},[100,46116,4944],{"class":145},[100,46118,149],{"class":118},[100,46120,46121,46123,46125,46127,46129,46131],{"class":102,"line":185},[100,46122,200],{"class":114},[100,46124,170],{"class":118},[100,46126,206],{"class":205},[100,46128,45672],{"class":209},[100,46130,206],{"class":205},[100,46132,215],{"class":118},[14,46134,46135,46136,46138],{},"This does the same thing, but ",[17,46137,45684],{}," is shorter and more common.",[675,46140,46142],{"id":46141},"use-try-except-when-empty-dictionaries-are-expected","Use try-except when empty dictionaries are expected",[14,46144,46145],{},"Sometimes an empty dictionary is normal in your program. In that case, you can catch the error:",[91,46147,46149],{"className":93,"code":46148,"language":95,"meta":96,"style":96},"data = {}\n\ntry:\n    key, value = data.popitem()\n    print(key, value)\nexcept KeyError:\n    print(\"Dictionary is empty\")\n",[17,46150,46151,46159,46163,46169,46187,46201,46209],{"__ignoreMap":96},[100,46152,46153,46155,46157],{"class":102,"line":103},[100,46154,7909],{"class":106},[100,46156,111],{"class":110},[100,46158,45623],{"class":118},[100,46160,46161],{"class":102,"line":135},[100,46162,139],{"emptyLinePlaceholder":138},[100,46164,46165,46167],{"class":102,"line":142},[100,46166,146],{"class":145},[100,46168,149],{"class":118},[100,46170,46171,46173,46175,46177,46179,46181,46183,46185],{"class":102,"line":152},[100,46172,45640],{"class":106},[100,46174,126],{"class":118},[100,46176,989],{"class":106},[100,46178,111],{"class":110},[100,46180,12102],{"class":106},[100,46182,290],{"class":118},[100,46184,45653],{"class":178},[100,46186,4734],{"class":118},[100,46188,46189,46191,46193,46195,46197,46199],{"class":102,"line":164},[100,46190,200],{"class":114},[100,46192,170],{"class":118},[100,46194,43824],{"class":178},[100,46196,126],{"class":118},[100,46198,6747],{"class":178},[100,46200,215],{"class":118},[100,46202,46203,46205,46207],{"class":102,"line":185},[100,46204,188],{"class":145},[100,46206,43194],{"class":191},[100,46208,149],{"class":118},[100,46210,46211,46213,46215,46217,46219,46221],{"class":102,"line":197},[100,46212,200],{"class":114},[100,46214,170],{"class":118},[100,46216,206],{"class":205},[100,46218,45672],{"class":209},[100,46220,206],{"class":205},[100,46222,215],{"class":118},[14,46224,46225,46226,46228],{},"This works, but for beginners, an ",[17,46227,2736],{}," check is usually easier to read.",[675,46230,46232],{"id":46231},"review-earlier-code","Review earlier code",[14,46234,46235,46236,46238],{},"Sometimes ",[17,46237,45598],{}," is not the real problem. The real issue is that the dictionary became empty earlier.",[14,46240,580],{},[91,46242,46244],{"className":93,"code":46243,"language":95,"meta":96,"style":96},"data = {\"x\": 1, \"y\": 2}\n\ndata.clear()\ndata.popitem()\n",[17,46245,46246,46278,46282,46293],{"__ignoreMap":96},[100,46247,46248,46250,46252,46254,46256,46258,46260,46262,46264,46266,46268,46270,46272,46274,46276],{"class":102,"line":103},[100,46249,7909],{"class":106},[100,46251,111],{"class":110},[100,46253,12080],{"class":118},[100,46255,206],{"class":205},[100,46257,39024],{"class":209},[100,46259,206],{"class":205},[100,46261,89],{"class":118},[100,46263,6915],{"class":122},[100,46265,126],{"class":118},[100,46267,1708],{"class":205},[100,46269,39057],{"class":209},[100,46271,206],{"class":205},[100,46273,89],{"class":118},[100,46275,129],{"class":122},[100,46277,12093],{"class":118},[100,46279,46280],{"class":102,"line":135},[100,46281,139],{"emptyLinePlaceholder":138},[100,46283,46284,46286,46288,46291],{"class":102,"line":142},[100,46285,7934],{"class":106},[100,46287,290],{"class":118},[100,46289,46290],{"class":178},"clear",[100,46292,4734],{"class":118},[100,46294,46295,46297,46299,46301],{"class":102,"line":152},[100,46296,7934],{"class":106},[100,46298,290],{"class":118},[100,46300,45653],{"class":178},[100,46302,4734],{"class":118},[14,46304,46305,46308,46309,46311],{},[17,46306,46307],{},"data.clear()"," removes everything, so ",[17,46310,45598],{}," fails after that.",[14,46313,46314,46315,290],{},"If you are removing a specific key instead of an arbitrary item, see ",[295,46316,46318],{"href":46317},"\u002Fhow-to\u002Fhow-to-remove-a-key-from-a-dictionary-in-python","how to remove a key from a dictionary in Python",[77,46320,46322],{"id":46321},"recommended-beginner-safe-pattern","Recommended beginner-safe pattern",[14,46324,46325],{},"The safest pattern is:",[3282,46327,46328,46331,46336],{},[43,46329,46330],{},"Check whether the dictionary is empty",[43,46332,241,46333,46335],{},[17,46334,45598],{}," only when there is at least one item",[43,46337,46338],{},"Handle the empty case with a message, return value, or different logic",[14,46340,1844],{},[91,46342,46344],{"className":93,"code":46343,"language":95,"meta":96,"style":96},"def remove_one_item(data):\n    if data:\n        return data.popitem()\n    return None\n\ninfo = {}\nresult = remove_one_item(info)\n\nprint(result)\n",[17,46345,46346,46359,46367,46380,46386,46390,46399,46414,46418],{"__ignoreMap":96},[100,46347,46348,46350,46353,46355,46357],{"class":102,"line":103},[100,46349,1078],{"class":1077},[100,46351,46352],{"class":1081}," remove_one_item",[100,46354,170],{"class":118},[100,46356,7934],{"class":2519},[100,46358,522],{"class":118},[100,46360,46361,46363,46365],{"class":102,"line":135},[100,46362,2777],{"class":145},[100,46364,12102],{"class":106},[100,46366,149],{"class":118},[100,46368,46369,46372,46374,46376,46378],{"class":102,"line":142},[100,46370,46371],{"class":145},"        return",[100,46373,12102],{"class":106},[100,46375,290],{"class":118},[100,46377,45653],{"class":178},[100,46379,4734],{"class":118},[100,46381,46382,46384],{"class":102,"line":152},[100,46383,2552],{"class":145},[100,46385,4904],{"class":158},[100,46387,46388],{"class":102,"line":164},[100,46389,139],{"emptyLinePlaceholder":138},[100,46391,46392,46395,46397],{"class":102,"line":185},[100,46393,46394],{"class":106},"info ",[100,46396,111],{"class":110},[100,46398,45623],{"class":118},[100,46400,46401,46403,46405,46407,46409,46412],{"class":102,"line":197},[100,46402,11556],{"class":106},[100,46404,111],{"class":110},[100,46406,46352],{"class":178},[100,46408,170],{"class":118},[100,46410,46411],{"class":178},"info",[100,46413,215],{"class":118},[100,46415,46416],{"class":102,"line":771},[100,46417,139],{"emptyLinePlaceholder":138},[100,46419,46420,46422,46424,46426],{"class":102,"line":787},[100,46421,372],{"class":114},[100,46423,170],{"class":118},[100,46425,11580],{"class":178},[100,46427,215],{"class":118},[14,46429,218],{},[91,46431,46432],{"className":93,"code":924,"language":95,"meta":96,"style":96},[17,46433,46434],{"__ignoreMap":96},[100,46435,46436],{"class":102,"line":103},[100,46437,924],{"class":158},[14,46439,46440],{},"This is usually clearer than relying on exceptions for normal program flow.",[77,46442,3274],{"id":3273},[14,46444,46445,46446,290],{},"If you are not sure why the error is happening, inspect the dictionary right before ",[17,46447,45598],{},[14,46449,26604],{},[91,46451,46453],{"className":93,"code":46452,"language":95,"meta":96,"style":96},"print(my_dict)\nprint(len(my_dict))\nprint(bool(my_dict))\nprint(my_dict.keys())\n",[17,46454,46455,46465,46479,46494],{"__ignoreMap":96},[100,46456,46457,46459,46461,46463],{"class":102,"line":103},[100,46458,372],{"class":114},[100,46460,170],{"class":118},[100,46462,17794],{"class":178},[100,46464,215],{"class":118},[100,46466,46467,46469,46471,46473,46475,46477],{"class":102,"line":135},[100,46468,372],{"class":114},[100,46470,170],{"class":118},[100,46472,17779],{"class":114},[100,46474,170],{"class":118},[100,46476,17794],{"class":178},[100,46478,182],{"class":118},[100,46480,46481,46483,46485,46488,46490,46492],{"class":102,"line":142},[100,46482,372],{"class":114},[100,46484,170],{"class":118},[100,46486,46487],{"class":191},"bool",[100,46489,170],{"class":118},[100,46491,17794],{"class":178},[100,46493,182],{"class":118},[100,46495,46496,46498,46500,46502,46504,46506],{"class":102,"line":152},[100,46497,372],{"class":114},[100,46499,170],{"class":118},[100,46501,17794],{"class":178},[100,46503,290],{"class":118},[100,46505,6185],{"class":178},[100,46507,3370],{"class":118},[14,46509,46510],{},"What these show:",[40,46512,46513,46519,46525,46534],{},[43,46514,46515,46518],{},[17,46516,46517],{},"print(my_dict)"," shows the full dictionary",[43,46520,46521,46524],{},[17,46522,46523],{},"print(len(my_dict))"," shows how many items it has",[43,46526,46527,46530,46531,46533],{},[17,46528,46529],{},"print(bool(my_dict))"," shows ",[17,46532,1649],{}," when it is empty",[43,46535,46536,46538],{},[17,46537,17846],{}," shows the current keys",[14,46540,46541],{},"You can also:",[40,46543,46544,46549,46552,46563],{},[43,46545,46546,46547],{},"Print the dictionary right before ",[17,46548,45598],{},[43,46550,46551],{},"Trace where items are removed earlier",[43,46553,41979,46554,5870,46557,46559,46560,46562],{},[17,46555,46556],{},"clear()",[17,46558,8104],{},", or another ",[17,46561,45598],{}," emptied it",[43,46564,46565],{},"If using a loop, make sure the loop stops when the dictionary becomes empty",[14,46567,46568],{},"A safe loop looks like this:",[91,46570,46572],{"className":93,"code":46571,"language":95,"meta":96,"style":96},"data = {\"a\": 1, \"b\": 2}\n\nwhile data:\n    print(data.popitem())\n\nprint(\"Done\")\n",[17,46573,46574,46606,46610,46618,46632,46636],{"__ignoreMap":96},[100,46575,46576,46578,46580,46582,46584,46586,46588,46590,46592,46594,46596,46598,46600,46602,46604],{"class":102,"line":103},[100,46577,7909],{"class":106},[100,46579,111],{"class":110},[100,46581,12080],{"class":118},[100,46583,206],{"class":205},[100,46585,295],{"class":209},[100,46587,206],{"class":205},[100,46589,89],{"class":118},[100,46591,6915],{"class":122},[100,46593,126],{"class":118},[100,46595,1708],{"class":205},[100,46597,4027],{"class":209},[100,46599,206],{"class":205},[100,46601,89],{"class":118},[100,46603,129],{"class":122},[100,46605,12093],{"class":118},[100,46607,46608],{"class":102,"line":135},[100,46609,139],{"emptyLinePlaceholder":138},[100,46611,46612,46614,46616],{"class":102,"line":142},[100,46613,721],{"class":145},[100,46615,12102],{"class":106},[100,46617,149],{"class":118},[100,46619,46620,46622,46624,46626,46628,46630],{"class":102,"line":152},[100,46621,200],{"class":114},[100,46623,170],{"class":118},[100,46625,7934],{"class":178},[100,46627,290],{"class":118},[100,46629,45653],{"class":178},[100,46631,3370],{"class":118},[100,46633,46634],{"class":102,"line":164},[100,46635,139],{"emptyLinePlaceholder":138},[100,46637,46638,46640,46642,46644,46646,46648],{"class":102,"line":185},[100,46639,372],{"class":114},[100,46641,170],{"class":118},[100,46643,206],{"class":205},[100,46645,31802],{"class":209},[100,46647,206],{"class":205},[100,46649,215],{"class":118},[14,46651,218],{},[91,46653,46655],{"className":93,"code":46654,"language":95,"meta":96,"style":96},"('b', 2)\n('a', 1)\nDone\n",[17,46656,46657,46673,46689],{"__ignoreMap":96},[100,46658,46659,46661,46663,46665,46667,46669,46671],{"class":102,"line":103},[100,46660,170],{"class":118},[100,46662,1280],{"class":205},[100,46664,4027],{"class":209},[100,46666,1280],{"class":205},[100,46668,126],{"class":118},[100,46670,129],{"class":122},[100,46672,215],{"class":118},[100,46674,46675,46677,46679,46681,46683,46685,46687],{"class":102,"line":135},[100,46676,170],{"class":118},[100,46678,1280],{"class":205},[100,46680,295],{"class":209},[100,46682,1280],{"class":205},[100,46684,126],{"class":118},[100,46686,6915],{"class":122},[100,46688,215],{"class":118},[100,46690,46691],{"class":102,"line":142},[100,46692,46693],{"class":106},"Done\n",[77,46695,7117],{"id":7116},[14,46697,46698],{},"These are common causes of this error:",[40,46700,46701,46707,46715,46724],{},[43,46702,315,46703,13325,46705],{},[17,46704,45598],{},[17,46706,45815],{},[43,46708,1357,46709,46711,46712,46714],{},[17,46710,45598],{}," inside a ",[17,46713,721],{}," loop without checking if items remain",[43,46716,46717,46718,5870,46720,5894,46722],{},"Removing all dictionary items earlier with ",[17,46719,46556],{},[17,46721,8104],{},[17,46723,45598],{},[43,46725,46726],{},"Assuming loaded or returned data is non-empty when it is actually empty",[14,46728,46729,46730,290],{},"If you are dealing with a broader dictionary key problem, see ",[295,46731,17238],{"href":43938},[14,46733,46734,46735,290],{},"If your code depends on checking for a key before removing or reading it, see ",[295,46736,43100],{"href":43099},[77,46738,1514],{"id":1513},[675,46740,46742,46743,46745],{"id":46741},"what-does-popitem-return","What does ",[17,46744,45598],{}," return?",[14,46747,46748,46749,290],{},"It returns one key-value pair as a tuple, like ",[17,46750,46751],{},"('name', 'Alice')",[675,46753,46755,46756,11353],{"id":46754},"why-is-this-a-keyerror","Why is this a ",[17,46757,17063],{},[14,46759,14478,46760,46762],{},[17,46761,45598],{}," tries to remove a dictionary key, but there is no key to remove.",[675,46764,46766],{"id":46765},"how-do-i-check-if-a-dictionary-is-empty","How do I check if a dictionary is empty?",[14,46768,241,46769,46772,46773,290],{},[17,46770,46771],{},"if my_dict:",". An empty dictionary is ",[17,46774,1649],{},[675,46776,46778],{"id":46777},"should-i-use-try-except-or-an-if-check","Should I use try-except or an if check?",[14,46780,46781,46782,46784],{},"For beginners, an ",[17,46783,2736],{}," check is usually simpler and easier to read.",[77,46786,1554],{"id":1553},[40,46788,46789,46793,46799,46804,46808],{},[43,46790,46791],{},[295,46792,42671],{"href":42670},[43,46794,46795],{},[295,46796,45833,46797,4112],{"href":45832},[17,46798,45598],{},[43,46800,46801],{},[295,46802,46803],{"href":46317},"How to remove a key from a dictionary in Python",[43,46805,46806],{},[295,46807,43888],{"href":43099},[43,46809,46810],{},[295,46811,17238],{"href":43938},[14,46813,46814,46815,46817],{},"To prevent this error next time, learn how dictionaries work and use ",[17,46816,45598],{}," only when you know the dictionary still has items.",[1589,46819,46820],{},"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 .sP7_E, html code.shiki .sP7_E{--shiki-light:#39ADB5;--shiki-default:#24292E;--shiki-dark:#E1E4E8}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 .slqww, html code.shiki .slqww{--shiki-light:#6182B8;--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 .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 .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 .s39Yj, html code.shiki .s39Yj{--shiki-light:#39ADB5;--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 .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":96,"searchDepth":135,"depth":135,"links":46822},[46823,46824,46825,46826,46827,46833,46834,46835,46836,46844],{"id":79,"depth":135,"text":80},{"id":5880,"depth":135,"text":5881},{"id":11030,"depth":135,"text":11031},{"id":25090,"depth":135,"text":25091},{"id":6243,"depth":135,"text":6244,"children":46828},[46829,46830,46831,46832],{"id":45920,"depth":142,"text":45921},{"id":46057,"depth":142,"text":46058},{"id":46141,"depth":142,"text":46142},{"id":46231,"depth":142,"text":46232},{"id":46321,"depth":135,"text":46322},{"id":3273,"depth":135,"text":3274},{"id":7116,"depth":135,"text":7117},{"id":1513,"depth":135,"text":1514,"children":46837},[46838,46840,46842,46843],{"id":46741,"depth":142,"text":46839},"What does popitem() return?",{"id":46754,"depth":142,"text":46841},"Why is this a KeyError?",{"id":46765,"depth":142,"text":46766},{"id":46777,"depth":142,"text":46778},{"id":1553,"depth":135,"text":1554},"Master keyerror popitem dictionary is empty fix in our comprehensive Python beginner guide.",{},"\u002Ferrors\u002Fkeyerror-popitem-dictionary-is-empty-fix",{"title":45584,"description":46845},"errors\u002Fkeyerror-popitem-dictionary-is-empty-fix","KuoZaFk4N1fx9ZUp2mVnm7POobapu3tBtzYJa-UU9fw",{"id":46852,"title":46853,"body":46854,"description":48729,"extension":1623,"meta":48730,"navigation":138,"path":45438,"seo":48731,"stem":48732,"__hash__":48733},"content\u002Ferrors\u002Fkeyerror-vs-indexerror-in-python-explained.md","KeyError vs IndexError in Python Explained",{"type":7,"value":46855,"toc":48702},[46856,46859,46866,46869,46886,46889,46891,47044,47049,47053,47083,47085,47103,47111,47115,47120,47123,47170,47177,47179,47193,47198,47202,47207,47210,47249,47255,47257,47271,47276,47280,47285,47349,47351,47368,47371,47386,47392,47459,47461,47469,47476,47479,47554,47556,47565,47570,47574,47678,47682,47773,47777,47781,47819,47821,47837,47839,47857,47861,47952,47956,47958,48017,48020,48023,48086,48089,48140,48143,48150,48154,48157,48184,48186,48267,48269,48302,48305,48317,48319,48322,48341,48343,48434,48437,48475,48479,48482,48495,48498,48527,48531,48606,48609,48627,48629,48633,48636,48640,48645,48649,48657,48663,48665,48671,48673,48699],[10,46857,46853],{"id":46858},"keyerror-vs-indexerror-in-python-explained",[14,46860,46861,3266,46863,46865],{},[17,46862,17063],{},[17,46864,16420],{}," both happen when your code tries to access something that is not available.",[14,46867,46868],{},"The difference is simple:",[40,46870,46871,46879],{},[43,46872,46873,46875,46876],{},[17,46874,17063],{}," is about a ",[22,46877,46878],{},"missing dictionary key",[43,46880,46881,46875,46883],{},[17,46882,16420],{},[22,46884,46885],{},"list or tuple position that does not exist",[14,46887,46888],{},"If you can quickly tell whether you are working with a dictionary or a sequence like a list, you can usually fix the problem fast.",[77,46890,80],{"id":79},[91,46892,46894],{"className":93,"code":46893,"language":95,"meta":96,"style":96},"data = {\"name\": \"Ana\"}\nitems = [10, 20, 30]\n\n# KeyError fix: check the key first or use get()\nprint(data.get(\"age\"))  # None\n\n# IndexError fix: check the index first\nindex = 5\nif 0 \u003C= index \u003C len(items):\n    print(items[index])\nelse:\n    print(\"Index out of range\")\n",[17,46895,46896,46920,46940,46944,46949,46973,46977,46982,46990,47010,47024,47030],{"__ignoreMap":96},[100,46897,46898,46900,46902,46904,46906,46908,46910,46912,46914,46916,46918],{"class":102,"line":103},[100,46899,7909],{"class":106},[100,46901,111],{"class":110},[100,46903,12080],{"class":118},[100,46905,206],{"class":205},[100,46907,2853],{"class":209},[100,46909,206],{"class":205},[100,46911,89],{"class":118},[100,46913,1708],{"class":205},[100,46915,34819],{"class":209},[100,46917,206],{"class":205},[100,46919,12093],{"class":118},[100,46921,46922,46924,46926,46928,46930,46932,46934,46936,46938],{"class":102,"line":135},[100,46923,3095],{"class":106},[100,46925,111],{"class":110},[100,46927,594],{"class":118},[100,46929,356],{"class":122},[100,46931,126],{"class":118},[100,46933,361],{"class":122},[100,46935,126],{"class":118},[100,46937,16342],{"class":122},[100,46939,603],{"class":118},[100,46941,46942],{"class":102,"line":142},[100,46943,139],{"emptyLinePlaceholder":138},[100,46945,46946],{"class":102,"line":152},[100,46947,46948],{"class":414},"# KeyError fix: check the key first or use get()\n",[100,46950,46951,46953,46955,46957,46959,46961,46963,46965,46967,46969,46971],{"class":102,"line":164},[100,46952,372],{"class":114},[100,46954,170],{"class":118},[100,46956,7934],{"class":178},[100,46958,290],{"class":118},[100,46960,12412],{"class":178},[100,46962,170],{"class":118},[100,46964,206],{"class":205},[100,46966,2770],{"class":209},[100,46968,206],{"class":205},[100,46970,411],{"class":118},[100,46972,44041],{"class":414},[100,46974,46975],{"class":102,"line":185},[100,46976,139],{"emptyLinePlaceholder":138},[100,46978,46979],{"class":102,"line":197},[100,46980,46981],{"class":414},"# IndexError fix: check the index first\n",[100,46983,46984,46986,46988],{"class":102,"line":771},[100,46985,3116],{"class":106},[100,46987,111],{"class":110},[100,46989,37580],{"class":122},[100,46991,46992,46994,46996,46998,47000,47002,47004,47006,47008],{"class":102,"line":787},[100,46993,2736],{"class":145},[100,46995,1921],{"class":122},[100,46997,3133],{"class":110},[100,46999,3136],{"class":106},[100,47001,2782],{"class":110},[100,47003,3141],{"class":114},[100,47005,170],{"class":118},[100,47007,3146],{"class":178},[100,47009,522],{"class":118},[100,47011,47012,47014,47016,47018,47020,47022],{"class":102,"line":5816},[100,47013,200],{"class":114},[100,47015,170],{"class":118},[100,47017,3146],{"class":178},[100,47019,3166],{"class":118},[100,47021,3169],{"class":178},[100,47023,132],{"class":118},[100,47025,47026,47028],{"class":102,"line":5833},[100,47027,4944],{"class":145},[100,47029,149],{"class":118},[100,47031,47032,47034,47036,47038,47040,47042],{"class":102,"line":5848},[100,47033,200],{"class":114},[100,47035,170],{"class":118},[100,47037,206],{"class":205},[100,47039,37896],{"class":209},[100,47041,206],{"class":205},[100,47043,215],{"class":118},[14,47045,241,47046,47048],{},[17,47047,11018],{}," for possibly missing dictionary keys. Check the length before using a list or tuple index.",[77,47050,47052],{"id":47051},"what-is-the-difference-between-keyerror-and-indexerror","What is the difference between KeyError and IndexError?",[40,47054,47055,47060,47065,47072],{},[43,47056,47057,47059],{},[17,47058,17063],{}," happens when you ask a dictionary for a key that does not exist.",[43,47061,47062,47064],{},[17,47063,16420],{}," happens when you ask a list or tuple for a position that does not exist.",[43,47066,571,47067,47069,47070,290],{},[22,47068,43824],{}," is a dictionary label like ",[17,47071,12456],{},[43,47073,1764,47074,47076,47077,5870,47079,5870,47081,290],{},[22,47075,3169],{}," is a numeric position like ",[17,47078,2279],{},[17,47080,123],{},[17,47082,559],{},[14,47084,580],{},[40,47086,47087,47095],{},[43,47088,47089,47092,47093],{},[17,47090,47091],{},"user[\"email\"]"," uses a ",[22,47094,43824],{},[43,47096,47097,47100,47101],{},[17,47098,47099],{},"items[2]"," uses an ",[22,47102,3169],{},[14,47104,8107,47105,3266,47108,290],{},[295,47106,42671],{"href":47107},"\u002Flearn\u002Fpython-dictionaries-explained\u002F",[295,47109,8111],{"href":47110},"\u002Flearn\u002Fpython-lists-explained-beginner-guide",[77,47112,47114],{"id":47113},"when-keyerror-happens","When KeyError happens",[14,47116,47117,47119],{},[17,47118,17063],{}," is most common with dictionaries.",[14,47121,47122],{},"It happens when code like this tries to read a missing key:",[91,47124,47126],{"className":93,"code":47125,"language":95,"meta":96,"style":96},"user = {\"name\": \"Ana\"}\nprint(user[\"email\"])\n",[17,47127,47128,47152],{"__ignoreMap":96},[100,47129,47130,47132,47134,47136,47138,47140,47142,47144,47146,47148,47150],{"class":102,"line":103},[100,47131,15336],{"class":106},[100,47133,111],{"class":110},[100,47135,12080],{"class":118},[100,47137,206],{"class":205},[100,47139,2853],{"class":209},[100,47141,206],{"class":205},[100,47143,89],{"class":118},[100,47145,1708],{"class":205},[100,47147,34819],{"class":209},[100,47149,206],{"class":205},[100,47151,12093],{"class":118},[100,47153,47154,47156,47158,47160,47162,47164,47166,47168],{"class":102,"line":135},[100,47155,372],{"class":114},[100,47157,170],{"class":118},[100,47159,15354],{"class":178},[100,47161,3166],{"class":118},[100,47163,206],{"class":205},[100,47165,11779],{"class":209},[100,47167,206],{"class":205},[100,47169,132],{"class":118},[14,47171,47172,47173,47176],{},"The dictionary exists, but ",[17,47174,47175],{},"\"email\""," is not inside it.",[14,47178,14127],{},[40,47180,47181,47184,47187,47190],{},[43,47182,47183],{},"Misspelled keys",[43,47185,47186],{},"Unexpected input data",[43,47188,47189],{},"Assuming JSON or API data always contains the same fields",[43,47191,47192],{},"Using the wrong object type",[14,47194,47195,47196,290],{},"If you want a full guide, see ",[295,47197,17238],{"href":17237},[77,47199,47201],{"id":47200},"when-indexerror-happens","When IndexError happens",[14,47203,47204,47206],{},[17,47205,16420],{}," is most common with lists and tuples.",[14,47208,47209],{},"It happens when code tries to access a position outside the valid range:",[91,47211,47213],{"className":93,"code":47212,"language":95,"meta":96,"style":96},"items = [10, 20, 30]\nprint(items[5])\n",[17,47214,47215,47235],{"__ignoreMap":96},[100,47216,47217,47219,47221,47223,47225,47227,47229,47231,47233],{"class":102,"line":103},[100,47218,3095],{"class":106},[100,47220,111],{"class":110},[100,47222,594],{"class":118},[100,47224,356],{"class":122},[100,47226,126],{"class":118},[100,47228,361],{"class":122},[100,47230,126],{"class":118},[100,47232,16342],{"class":122},[100,47234,603],{"class":118},[100,47236,47237,47239,47241,47243,47245,47247],{"class":102,"line":135},[100,47238,372],{"class":114},[100,47240,170],{"class":118},[100,47242,3146],{"class":178},[100,47244,3166],{"class":118},[100,47246,3557],{"class":122},[100,47248,132],{"class":118},[14,47250,47251,47252,47254],{},"The list exists, but index ",[17,47253,3557],{}," is too large.",[14,47256,14127],{},[40,47258,47259,47262,47265,47268],{},[43,47260,47261],{},"Loops that go too far",[43,47263,47264],{},"Hard-coded indexes",[43,47266,47267],{},"Empty lists",[43,47269,47270],{},"Assuming a list always has enough items",[14,47272,47273,47274,290],{},"For a deeper explanation, see ",[295,47275,37671],{"href":37670},[77,47277,47279],{"id":47278},"example-keyerror","Example: KeyError",[14,47281,47282,47283,89],{},"Here is a simple example that causes ",[17,47284,17063],{},[91,47286,47288],{"className":93,"code":47287,"language":95,"meta":96,"style":96},"user = {\"name\": \"Ana\", \"city\": \"Lima\"}\nprint(user[\"email\"])\n",[17,47289,47290,47331],{"__ignoreMap":96},[100,47291,47292,47294,47296,47298,47300,47302,47304,47306,47308,47310,47312,47314,47316,47318,47320,47322,47324,47327,47329],{"class":102,"line":103},[100,47293,15336],{"class":106},[100,47295,111],{"class":110},[100,47297,12080],{"class":118},[100,47299,206],{"class":205},[100,47301,2853],{"class":209},[100,47303,206],{"class":205},[100,47305,89],{"class":118},[100,47307,1708],{"class":205},[100,47309,34819],{"class":209},[100,47311,206],{"class":205},[100,47313,126],{"class":118},[100,47315,1708],{"class":205},[100,47317,43636],{"class":209},[100,47319,206],{"class":205},[100,47321,89],{"class":118},[100,47323,1708],{"class":205},[100,47325,47326],{"class":209},"Lima",[100,47328,206],{"class":205},[100,47330,12093],{"class":118},[100,47332,47333,47335,47337,47339,47341,47343,47345,47347],{"class":102,"line":135},[100,47334,372],{"class":114},[100,47336,170],{"class":118},[100,47338,15354],{"class":178},[100,47340,3166],{"class":118},[100,47342,206],{"class":205},[100,47344,11779],{"class":209},[100,47346,206],{"class":205},[100,47348,132],{"class":118},[14,47350,218],{},[91,47352,47354],{"className":93,"code":47353,"language":95,"meta":96,"style":96},"KeyError: 'email'\n",[17,47355,47356],{"__ignoreMap":96},[100,47357,47358,47360,47362,47364,47366],{"class":102,"line":103},[100,47359,17063],{"class":191},[100,47361,89],{"class":118},[100,47363,1274],{"class":205},[100,47365,11779],{"class":209},[100,47367,3925],{"class":205},[14,47369,47370],{},"Why it happens:",[40,47372,47373,47378,47383],{},[43,47374,47375,47377],{},[17,47376,15354],{}," is a dictionary",[43,47379,47380,47381],{},"The code asks for the key ",[17,47382,47175],{},[43,47384,47385],{},"That key is missing",[675,47387,47389,47390],{"id":47388},"fix-1-use-get","Fix 1: Use ",[17,47391,13068],{},[91,47393,47395],{"className":93,"code":47394,"language":95,"meta":96,"style":96},"user = {\"name\": \"Ana\", \"city\": \"Lima\"}\nprint(user.get(\"email\"))\n",[17,47396,47397,47437],{"__ignoreMap":96},[100,47398,47399,47401,47403,47405,47407,47409,47411,47413,47415,47417,47419,47421,47423,47425,47427,47429,47431,47433,47435],{"class":102,"line":103},[100,47400,15336],{"class":106},[100,47402,111],{"class":110},[100,47404,12080],{"class":118},[100,47406,206],{"class":205},[100,47408,2853],{"class":209},[100,47410,206],{"class":205},[100,47412,89],{"class":118},[100,47414,1708],{"class":205},[100,47416,34819],{"class":209},[100,47418,206],{"class":205},[100,47420,126],{"class":118},[100,47422,1708],{"class":205},[100,47424,43636],{"class":209},[100,47426,206],{"class":205},[100,47428,89],{"class":118},[100,47430,1708],{"class":205},[100,47432,47326],{"class":209},[100,47434,206],{"class":205},[100,47436,12093],{"class":118},[100,47438,47439,47441,47443,47445,47447,47449,47451,47453,47455,47457],{"class":102,"line":135},[100,47440,372],{"class":114},[100,47442,170],{"class":118},[100,47444,15354],{"class":178},[100,47446,290],{"class":118},[100,47448,12412],{"class":178},[100,47450,170],{"class":118},[100,47452,206],{"class":205},[100,47454,11779],{"class":209},[100,47456,206],{"class":205},[100,47458,182],{"class":118},[14,47460,218],{},[91,47462,47463],{"className":93,"code":924,"language":95,"meta":96,"style":96},[17,47464,47465],{"__ignoreMap":96},[100,47466,47467],{"class":102,"line":103},[100,47468,924],{"class":158},[14,47470,47471,551,47473,47475],{},[17,47472,13068],{},[17,47474,930],{}," instead of raising an error if the key is missing.",[14,47477,47478],{},"You can also give a default value:",[91,47480,47482],{"className":93,"code":47481,"language":95,"meta":96,"style":96},"user = {\"name\": \"Ana\", \"city\": \"Lima\"}\nprint(user.get(\"email\", \"No email found\"))\n",[17,47483,47484,47524],{"__ignoreMap":96},[100,47485,47486,47488,47490,47492,47494,47496,47498,47500,47502,47504,47506,47508,47510,47512,47514,47516,47518,47520,47522],{"class":102,"line":103},[100,47487,15336],{"class":106},[100,47489,111],{"class":110},[100,47491,12080],{"class":118},[100,47493,206],{"class":205},[100,47495,2853],{"class":209},[100,47497,206],{"class":205},[100,47499,89],{"class":118},[100,47501,1708],{"class":205},[100,47503,34819],{"class":209},[100,47505,206],{"class":205},[100,47507,126],{"class":118},[100,47509,1708],{"class":205},[100,47511,43636],{"class":209},[100,47513,206],{"class":205},[100,47515,89],{"class":118},[100,47517,1708],{"class":205},[100,47519,47326],{"class":209},[100,47521,206],{"class":205},[100,47523,12093],{"class":118},[100,47525,47526,47528,47530,47532,47534,47536,47538,47540,47542,47544,47546,47548,47550,47552],{"class":102,"line":135},[100,47527,372],{"class":114},[100,47529,170],{"class":118},[100,47531,15354],{"class":178},[100,47533,290],{"class":118},[100,47535,12412],{"class":178},[100,47537,170],{"class":118},[100,47539,206],{"class":205},[100,47541,11779],{"class":209},[100,47543,206],{"class":205},[100,47545,126],{"class":118},[100,47547,1708],{"class":205},[100,47549,11802],{"class":209},[100,47551,206],{"class":205},[100,47553,182],{"class":118},[14,47555,218],{},[91,47557,47559],{"className":93,"code":47558,"language":95,"meta":96,"style":96},"No email found\n",[17,47560,47561],{"__ignoreMap":96},[100,47562,47563],{"class":102,"line":103},[100,47564,47558],{"class":106},[14,47566,23054,47567,47569],{},[295,47568,12577],{"href":11015}," for more examples.",[675,47571,47573],{"id":47572},"fix-2-check-the-key-first","Fix 2: Check the key first",[91,47575,47577],{"className":93,"code":47576,"language":95,"meta":96,"style":96},"user = {\"name\": \"Ana\", \"city\": \"Lima\"}\n\nif \"email\" in user:\n    print(user[\"email\"])\nelse:\n    print(\"Key not found\")\n",[17,47578,47579,47619,47623,47640,47658,47664],{"__ignoreMap":96},[100,47580,47581,47583,47585,47587,47589,47591,47593,47595,47597,47599,47601,47603,47605,47607,47609,47611,47613,47615,47617],{"class":102,"line":103},[100,47582,15336],{"class":106},[100,47584,111],{"class":110},[100,47586,12080],{"class":118},[100,47588,206],{"class":205},[100,47590,2853],{"class":209},[100,47592,206],{"class":205},[100,47594,89],{"class":118},[100,47596,1708],{"class":205},[100,47598,34819],{"class":209},[100,47600,206],{"class":205},[100,47602,126],{"class":118},[100,47604,1708],{"class":205},[100,47606,43636],{"class":209},[100,47608,206],{"class":205},[100,47610,89],{"class":118},[100,47612,1708],{"class":205},[100,47614,47326],{"class":209},[100,47616,206],{"class":205},[100,47618,12093],{"class":118},[100,47620,47621],{"class":102,"line":135},[100,47622,139],{"emptyLinePlaceholder":138},[100,47624,47625,47627,47629,47631,47633,47635,47638],{"class":102,"line":142},[100,47626,2736],{"class":145},[100,47628,1708],{"class":205},[100,47630,11779],{"class":209},[100,47632,206],{"class":205},[100,47634,1971],{"class":110},[100,47636,47637],{"class":106}," user",[100,47639,149],{"class":118},[100,47641,47642,47644,47646,47648,47650,47652,47654,47656],{"class":102,"line":152},[100,47643,200],{"class":114},[100,47645,170],{"class":118},[100,47647,15354],{"class":178},[100,47649,3166],{"class":118},[100,47651,206],{"class":205},[100,47653,11779],{"class":209},[100,47655,206],{"class":205},[100,47657,132],{"class":118},[100,47659,47660,47662],{"class":102,"line":164},[100,47661,4944],{"class":145},[100,47663,149],{"class":118},[100,47665,47666,47668,47670,47672,47674,47676],{"class":102,"line":185},[100,47667,200],{"class":114},[100,47669,170],{"class":118},[100,47671,206],{"class":205},[100,47673,42470],{"class":209},[100,47675,206],{"class":205},[100,47677,215],{"class":118},[675,47679,47681],{"id":47680},"fix-3-add-the-missing-key","Fix 3: Add the missing key",[91,47683,47685],{"className":93,"code":47684,"language":95,"meta":96,"style":96},"user = {\"name\": \"Ana\", \"city\": \"Lima\"}\nuser[\"email\"] = \"ana@example.com\"\n\nprint(user[\"email\"])\n",[17,47686,47687,47727,47751,47755],{"__ignoreMap":96},[100,47688,47689,47691,47693,47695,47697,47699,47701,47703,47705,47707,47709,47711,47713,47715,47717,47719,47721,47723,47725],{"class":102,"line":103},[100,47690,15336],{"class":106},[100,47692,111],{"class":110},[100,47694,12080],{"class":118},[100,47696,206],{"class":205},[100,47698,2853],{"class":209},[100,47700,206],{"class":205},[100,47702,89],{"class":118},[100,47704,1708],{"class":205},[100,47706,34819],{"class":209},[100,47708,206],{"class":205},[100,47710,126],{"class":118},[100,47712,1708],{"class":205},[100,47714,43636],{"class":209},[100,47716,206],{"class":205},[100,47718,89],{"class":118},[100,47720,1708],{"class":205},[100,47722,47326],{"class":209},[100,47724,206],{"class":205},[100,47726,12093],{"class":118},[100,47728,47729,47731,47733,47735,47737,47739,47741,47744,47746,47749],{"class":102,"line":135},[100,47730,15354],{"class":106},[100,47732,3166],{"class":118},[100,47734,206],{"class":205},[100,47736,11779],{"class":209},[100,47738,206],{"class":205},[100,47740,17591],{"class":118},[100,47742,47743],{"class":110}," =",[100,47745,1708],{"class":205},[100,47747,47748],{"class":209},"ana@example.com",[100,47750,1714],{"class":205},[100,47752,47753],{"class":102,"line":142},[100,47754,139],{"emptyLinePlaceholder":138},[100,47756,47757,47759,47761,47763,47765,47767,47769,47771],{"class":102,"line":152},[100,47758,372],{"class":114},[100,47760,170],{"class":118},[100,47762,15354],{"class":178},[100,47764,3166],{"class":118},[100,47766,206],{"class":205},[100,47768,11779],{"class":209},[100,47770,206],{"class":205},[100,47772,132],{"class":118},[77,47774,47776],{"id":47775},"example-indexerror","Example: IndexError",[14,47778,47282,47779,89],{},[17,47780,16420],{},[91,47782,47783],{"className":93,"code":47212,"language":95,"meta":96,"style":96},[17,47784,47785,47805],{"__ignoreMap":96},[100,47786,47787,47789,47791,47793,47795,47797,47799,47801,47803],{"class":102,"line":103},[100,47788,3095],{"class":106},[100,47790,111],{"class":110},[100,47792,594],{"class":118},[100,47794,356],{"class":122},[100,47796,126],{"class":118},[100,47798,361],{"class":122},[100,47800,126],{"class":118},[100,47802,16342],{"class":122},[100,47804,603],{"class":118},[100,47806,47807,47809,47811,47813,47815,47817],{"class":102,"line":135},[100,47808,372],{"class":114},[100,47810,170],{"class":118},[100,47812,3146],{"class":178},[100,47814,3166],{"class":118},[100,47816,3557],{"class":122},[100,47818,132],{"class":118},[14,47820,218],{},[91,47822,47823],{"className":93,"code":17122,"language":95,"meta":96,"style":96},[17,47824,47825],{"__ignoreMap":96},[100,47826,47827,47829,47831,47833,47835],{"class":102,"line":103},[100,47828,16420],{"class":191},[100,47830,89],{"class":118},[100,47832,14704],{"class":191},[100,47834,16427],{"class":106},[100,47836,16430],{"class":114},[14,47838,47370],{},[40,47840,47841,47845,47853],{},[43,47842,47843,4101],{},[17,47844,3146],{},[43,47846,38783,47847,5870,47849,5629,47851],{},[17,47848,2279],{},[17,47850,123],{},[17,47852,559],{},[43,47854,33121,47855,26280],{},[17,47856,3557],{},[675,47858,47860],{"id":47859},"fix-1-check-the-index-before-using-it","Fix 1: Check the index before using it",[91,47862,47864],{"className":93,"code":47863,"language":95,"meta":96,"style":96},"items = [10, 20, 30]\nindex = 5\n\nif 0 \u003C= index \u003C len(items):\n    print(items[index])\nelse:\n    print(\"Index out of range\")\n",[17,47865,47866,47886,47894,47898,47918,47932,47938],{"__ignoreMap":96},[100,47867,47868,47870,47872,47874,47876,47878,47880,47882,47884],{"class":102,"line":103},[100,47869,3095],{"class":106},[100,47871,111],{"class":110},[100,47873,594],{"class":118},[100,47875,356],{"class":122},[100,47877,126],{"class":118},[100,47879,361],{"class":122},[100,47881,126],{"class":118},[100,47883,16342],{"class":122},[100,47885,603],{"class":118},[100,47887,47888,47890,47892],{"class":102,"line":135},[100,47889,3116],{"class":106},[100,47891,111],{"class":110},[100,47893,37580],{"class":122},[100,47895,47896],{"class":102,"line":142},[100,47897,139],{"emptyLinePlaceholder":138},[100,47899,47900,47902,47904,47906,47908,47910,47912,47914,47916],{"class":102,"line":152},[100,47901,2736],{"class":145},[100,47903,1921],{"class":122},[100,47905,3133],{"class":110},[100,47907,3136],{"class":106},[100,47909,2782],{"class":110},[100,47911,3141],{"class":114},[100,47913,170],{"class":118},[100,47915,3146],{"class":178},[100,47917,522],{"class":118},[100,47919,47920,47922,47924,47926,47928,47930],{"class":102,"line":164},[100,47921,200],{"class":114},[100,47923,170],{"class":118},[100,47925,3146],{"class":178},[100,47927,3166],{"class":118},[100,47929,3169],{"class":178},[100,47931,132],{"class":118},[100,47933,47934,47936],{"class":102,"line":185},[100,47935,4944],{"class":145},[100,47937,149],{"class":118},[100,47939,47940,47942,47944,47946,47948,47950],{"class":102,"line":197},[100,47941,200],{"class":114},[100,47943,170],{"class":118},[100,47945,206],{"class":205},[100,47947,37896],{"class":209},[100,47949,206],{"class":205},[100,47951,215],{"class":118},[675,47953,47955],{"id":47954},"fix-2-change-loop-logic","Fix 2: Change loop logic",[14,47957,13720],{},[91,47959,47961],{"className":93,"code":47960,"language":95,"meta":96,"style":96},"items = [10, 20, 30]\n\nfor i in range(5):\n    print(items[i])\n",[17,47962,47963,47983,47987,48003],{"__ignoreMap":96},[100,47964,47965,47967,47969,47971,47973,47975,47977,47979,47981],{"class":102,"line":103},[100,47966,3095],{"class":106},[100,47968,111],{"class":110},[100,47970,594],{"class":118},[100,47972,356],{"class":122},[100,47974,126],{"class":118},[100,47976,361],{"class":122},[100,47978,126],{"class":118},[100,47980,16342],{"class":122},[100,47982,603],{"class":118},[100,47984,47985],{"class":102,"line":135},[100,47986,139],{"emptyLinePlaceholder":138},[100,47988,47989,47991,47993,47995,47997,47999,48001],{"class":102,"line":142},[100,47990,71],{"class":145},[100,47992,29838],{"class":106},[100,47994,617],{"class":145},[100,47996,29843],{"class":114},[100,47998,170],{"class":118},[100,48000,3557],{"class":122},[100,48002,522],{"class":118},[100,48004,48005,48007,48009,48011,48013,48015],{"class":102,"line":152},[100,48006,200],{"class":114},[100,48008,170],{"class":118},[100,48010,3146],{"class":178},[100,48012,3166],{"class":118},[100,48014,29858],{"class":178},[100,48016,132],{"class":118},[14,48018,48019],{},"This fails because the loop tries indexes that do not exist.",[14,48021,48022],{},"Better:",[91,48024,48026],{"className":93,"code":48025,"language":95,"meta":96,"style":96},"items = [10, 20, 30]\n\nfor i in range(len(items)):\n    print(items[i])\n",[17,48027,48028,48048,48052,48072],{"__ignoreMap":96},[100,48029,48030,48032,48034,48036,48038,48040,48042,48044,48046],{"class":102,"line":103},[100,48031,3095],{"class":106},[100,48033,111],{"class":110},[100,48035,594],{"class":118},[100,48037,356],{"class":122},[100,48039,126],{"class":118},[100,48041,361],{"class":122},[100,48043,126],{"class":118},[100,48045,16342],{"class":122},[100,48047,603],{"class":118},[100,48049,48050],{"class":102,"line":135},[100,48051,139],{"emptyLinePlaceholder":138},[100,48053,48054,48056,48058,48060,48062,48064,48066,48068,48070],{"class":102,"line":142},[100,48055,71],{"class":145},[100,48057,29838],{"class":106},[100,48059,617],{"class":145},[100,48061,29843],{"class":114},[100,48063,170],{"class":118},[100,48065,17779],{"class":114},[100,48067,170],{"class":118},[100,48069,3146],{"class":178},[100,48071,33665],{"class":118},[100,48073,48074,48076,48078,48080,48082,48084],{"class":102,"line":152},[100,48075,200],{"class":114},[100,48077,170],{"class":118},[100,48079,3146],{"class":178},[100,48081,3166],{"class":118},[100,48083,29858],{"class":178},[100,48085,132],{"class":118},[14,48087,48088],{},"Best for beginners:",[91,48090,48092],{"className":93,"code":48091,"language":95,"meta":96,"style":96},"items = [10, 20, 30]\n\nfor item in items:\n    print(item)\n",[17,48093,48094,48114,48118,48130],{"__ignoreMap":96},[100,48095,48096,48098,48100,48102,48104,48106,48108,48110,48112],{"class":102,"line":103},[100,48097,3095],{"class":106},[100,48099,111],{"class":110},[100,48101,594],{"class":118},[100,48103,356],{"class":122},[100,48105,126],{"class":118},[100,48107,361],{"class":122},[100,48109,126],{"class":118},[100,48111,16342],{"class":122},[100,48113,603],{"class":118},[100,48115,48116],{"class":102,"line":135},[100,48117,139],{"emptyLinePlaceholder":138},[100,48119,48120,48122,48124,48126,48128],{"class":102,"line":142},[100,48121,71],{"class":145},[100,48123,7568],{"class":106},[100,48125,617],{"class":145},[100,48127,12000],{"class":106},[100,48129,149],{"class":118},[100,48131,48132,48134,48136,48138],{"class":102,"line":152},[100,48133,200],{"class":114},[100,48135,170],{"class":118},[100,48137,7555],{"class":178},[100,48139,215],{"class":118},[14,48141,48142],{},"Looping directly over the list avoids many index mistakes.",[14,48144,48145,48146,290],{},"If needed, review ",[295,48147,9421,48148,1576],{"href":32800},[17,48149,32803],{},[77,48151,48153],{"id":48152},"how-to-choose-the-right-fix","How to choose the right fix",[14,48155,48156],{},"When you see the error, first look at the object you are accessing.",[40,48158,48159,48168,48176,48179],{},[43,48160,48161,48162,48165,48166],{},"If the code uses square brackets on a ",[22,48163,48164],{},"dictionary",", check the ",[22,48167,43824],{},[43,48169,48161,48170,48165,48173],{},[22,48171,48172],{},"list or tuple",[22,48174,48175],{},"position",[43,48177,48178],{},"Print the object to confirm what it contains",[43,48180,241,48181,48183],{},[17,48182,4430],{}," if you are not sure what kind of object you have",[14,48185,1844],{},[91,48187,48189],{"className":93,"code":48188,"language":95,"meta":96,"style":96},"data = {\"name\": \"Ana\"}\nitems = [10, 20, 30]\n\nprint(type(data))\nprint(type(items))\n",[17,48190,48191,48215,48235,48239,48253],{"__ignoreMap":96},[100,48192,48193,48195,48197,48199,48201,48203,48205,48207,48209,48211,48213],{"class":102,"line":103},[100,48194,7909],{"class":106},[100,48196,111],{"class":110},[100,48198,12080],{"class":118},[100,48200,206],{"class":205},[100,48202,2853],{"class":209},[100,48204,206],{"class":205},[100,48206,89],{"class":118},[100,48208,1708],{"class":205},[100,48210,34819],{"class":209},[100,48212,206],{"class":205},[100,48214,12093],{"class":118},[100,48216,48217,48219,48221,48223,48225,48227,48229,48231,48233],{"class":102,"line":135},[100,48218,3095],{"class":106},[100,48220,111],{"class":110},[100,48222,594],{"class":118},[100,48224,356],{"class":122},[100,48226,126],{"class":118},[100,48228,361],{"class":122},[100,48230,126],{"class":118},[100,48232,16342],{"class":122},[100,48234,603],{"class":118},[100,48236,48237],{"class":102,"line":142},[100,48238,139],{"emptyLinePlaceholder":138},[100,48240,48241,48243,48245,48247,48249,48251],{"class":102,"line":152},[100,48242,372],{"class":114},[100,48244,170],{"class":118},[100,48246,1250],{"class":191},[100,48248,170],{"class":118},[100,48250,7934],{"class":178},[100,48252,182],{"class":118},[100,48254,48255,48257,48259,48261,48263,48265],{"class":102,"line":164},[100,48256,372],{"class":114},[100,48258,170],{"class":118},[100,48260,1250],{"class":191},[100,48262,170],{"class":118},[100,48264,3146],{"class":178},[100,48266,182],{"class":118},[14,48268,218],{},[91,48270,48272],{"className":93,"code":48271,"language":95,"meta":96,"style":96},"\u003Cclass 'dict'>\n\u003Cclass 'list'>\n",[17,48273,48274,48288],{"__ignoreMap":96},[100,48275,48276,48278,48280,48282,48284,48286],{"class":102,"line":103},[100,48277,2782],{"class":110},[100,48279,3417],{"class":1077},[100,48281,1274],{"class":205},[100,48283,16046],{"class":209},[100,48285,1280],{"class":205},[100,48287,1980],{"class":110},[100,48289,48290,48292,48294,48296,48298,48300],{"class":102,"line":135},[100,48291,2782],{"class":110},[100,48293,3417],{"class":1077},[100,48295,1274],{"class":205},[100,48297,1235],{"class":209},[100,48299,1280],{"class":205},[100,48301,1980],{"class":110},[14,48303,48304],{},"A good rule:",[40,48306,48307,48312],{},[43,48308,48309,48310],{},"Dictionary + missing label = ",[17,48311,17063],{},[43,48313,48314,48315],{},"List or tuple + bad numeric position = ",[17,48316,16420],{},[77,48318,6924],{"id":6923},[14,48320,48321],{},"If you are not sure what is wrong, use these steps:",[3282,48323,48324,48329,48332,48335,48338],{},[43,48325,16306,48326,48328],{},[22,48327,16302],{}," of the traceback first",[43,48330,48331],{},"Find the variable being accessed",[43,48333,48334],{},"Check whether it is a dictionary, list, or tuple",[43,48336,48337],{},"Inspect the available keys or valid indexes",[43,48339,48340],{},"Check for empty data structures",[14,48342,5295],{},[91,48344,48346],{"className":93,"code":48345,"language":95,"meta":96,"style":96},"print(type(data))\nprint(data)\nprint(data.keys())\n\nprint(len(items))\nprint(index)\nprint(list(enumerate(items)))\n",[17,48347,48348,48362,48372,48386,48390,48404,48414],{"__ignoreMap":96},[100,48349,48350,48352,48354,48356,48358,48360],{"class":102,"line":103},[100,48351,372],{"class":114},[100,48353,170],{"class":118},[100,48355,1250],{"class":191},[100,48357,170],{"class":118},[100,48359,7934],{"class":178},[100,48361,182],{"class":118},[100,48363,48364,48366,48368,48370],{"class":102,"line":135},[100,48365,372],{"class":114},[100,48367,170],{"class":118},[100,48369,7934],{"class":178},[100,48371,215],{"class":118},[100,48373,48374,48376,48378,48380,48382,48384],{"class":102,"line":142},[100,48375,372],{"class":114},[100,48377,170],{"class":118},[100,48379,7934],{"class":178},[100,48381,290],{"class":118},[100,48383,6185],{"class":178},[100,48385,3370],{"class":118},[100,48387,48388],{"class":102,"line":152},[100,48389,139],{"emptyLinePlaceholder":138},[100,48391,48392,48394,48396,48398,48400,48402],{"class":102,"line":164},[100,48393,372],{"class":114},[100,48395,170],{"class":118},[100,48397,17779],{"class":114},[100,48399,170],{"class":118},[100,48401,3146],{"class":178},[100,48403,182],{"class":118},[100,48405,48406,48408,48410,48412],{"class":102,"line":185},[100,48407,372],{"class":114},[100,48409,170],{"class":118},[100,48411,3169],{"class":178},[100,48413,215],{"class":118},[100,48415,48416,48418,48420,48422,48424,48427,48429,48431],{"class":102,"line":197},[100,48417,372],{"class":114},[100,48419,170],{"class":118},[100,48421,1235],{"class":191},[100,48423,170],{"class":118},[100,48425,48426],{"class":114},"enumerate",[100,48428,170],{"class":118},[100,48430,3146],{"class":178},[100,48432,48433],{"class":118},")))\n",[14,48435,48436],{},"What these help you see:",[40,48438,48439,48445,48451,48457,48463,48469],{},[43,48440,48441,48444],{},[17,48442,48443],{},"print(type(data))"," shows the object type",[43,48446,48447,48450],{},[17,48448,48449],{},"print(data)"," shows the current contents",[43,48452,48453,48456],{},[17,48454,48455],{},"print(data.keys())"," shows dictionary keys",[43,48458,48459,48462],{},[17,48460,48461],{},"print(len(items))"," shows how many items are in the list",[43,48464,48465,48468],{},[17,48466,48467],{},"print(index)"," shows the index you are trying to use",[43,48470,48471,48474],{},[17,48472,48473],{},"print(list(enumerate(items)))"," shows each item with its index",[77,48476,48478],{"id":48477},"common-causes-beginners-mix-up","Common causes beginners mix up",[14,48480,48481],{},"These mistakes often cause confusion:",[40,48483,48484,48487,48490,48493],{},[43,48485,48486],{},"Treating a dictionary like a list",[43,48488,48489],{},"Treating a list like a dictionary",[43,48491,48492],{},"Assuming API or JSON data always contains the same keys",[43,48494,47270],{},[14,48496,48497],{},"Other common causes:",[40,48499,48500,48505,48508,48511,48520,48524],{},[43,48501,1357,48502,48504],{},[17,48503,42500],{}," on a dictionary",[43,48506,48507],{},"Misspelling a dictionary key",[43,48509,48510],{},"Expecting JSON data to contain a key that is not present",[43,48512,1357,48513,48516,48517],{},[17,48514,48515],{},"list[index]"," with an index larger than ",[17,48518,48519],{},"len(list) - 1",[43,48521,39126,48522,33022],{},[17,48523,2279],{},[43,48525,48526],{},"Using the wrong data type for the operation",[77,48528,48530],{"id":48529},"quick-comparison-table","Quick comparison table",[48532,48533,48534,48556],"table",{},[48535,48536,48537],"thead",{},[48538,48539,48540,48544,48547,48550,48553],"tr",{},[48541,48542,48543],"th",{},"Error",[48541,48545,48546],{},"Usually happens with",[48541,48548,48549],{},"What is missing",[48541,48551,48552],{},"Example",[48541,48554,48555],{},"Common fix",[48557,48558,48559,48584],"tbody",{},[48538,48560,48561,48566,48569,48572,48576],{},[48562,48563,48564],"td",{},[17,48565,17063],{},[48562,48567,48568],{},"Dictionary",[48562,48570,48571],{},"Key",[48562,48573,48574],{},[17,48575,47091],{},[48562,48577,241,48578,48580,48581],{},[17,48579,13068],{}," or check ",[17,48582,48583],{},"if key in dict",[48538,48585,48586,48590,48593,48596,48601],{},[48562,48587,48588],{},[17,48589,16420],{},[48562,48591,48592],{},"List or tuple",[48562,48594,48595],{},"Valid position",[48562,48597,48598],{},[17,48599,48600],{},"items[5]",[48562,48602,48603,48604],{},"Check bounds with ",[17,48605,32803],{},[14,48607,48608],{},"Another quick way to remember it:",[40,48610,48611,48618],{},[43,48612,48613,48615,48616],{},[17,48614,17063],{}," uses labels like ",[17,48617,12456],{},[43,48619,48620,48622,48623,3178,48625],{},[17,48621,16420],{}," uses positions like ",[17,48624,2279],{},[17,48626,11541],{},[77,48628,1514],{"id":1513},[675,48630,48632],{"id":48631},"is-keyerror-only-for-dictionaries","Is KeyError only for dictionaries?",[14,48634,48635],{},"Mostly yes for beginner use. It usually appears when a dictionary key is missing.",[675,48637,48639],{"id":48638},"does-indexerror-happen-with-dictionaries","Does IndexError happen with dictionaries?",[14,48641,14506,48642,48644],{},[17,48643,16420],{}," is for sequences like lists and tuples, not dictionary keys.",[675,48646,48648],{"id":48647},"should-i-use-get-to-avoid-keyerror","Should I use get() to avoid KeyError?",[14,48650,48651,48652,48654,48655,290],{},"Yes, if a missing key is acceptable. ",[17,48653,13068],{}," returns a default value instead of raising ",[17,48656,17063],{},[14,48658,48659,48660,290],{},"If you want examples, see ",[295,48661,43100],{"href":48662},"\u002Fhow-to\u002Fhow-to-check-if-a-key-exists-in-a-dictionary-in-python\u002F",[675,48664,34953],{"id":34952},[14,48666,48667,48668,290],{},"Loop directly over the list when possible, or make sure the index stays below ",[17,48669,48670],{},"len(list)",[77,48672,1554],{"id":1553},[40,48674,48675,48679,48683,48687,48691,48695],{},[43,48676,48677],{},[295,48678,17238],{"href":17237},[43,48680,48681],{},[295,48682,37671],{"href":37670},[43,48684,48685],{},[295,48686,43888],{"href":48662},[43,48688,48689],{},[295,48690,12577],{"href":11015},[43,48692,48693],{},[295,48694,42671],{"href":47107},[43,48696,48697],{},[295,48698,8111],{"href":47110},[1589,48700,48701],{},"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 .sP7_E, html code.shiki .sP7_E{--shiki-light:#39ADB5;--shiki-default:#24292E;--shiki-dark:#E1E4E8}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 .srdBf, html code.shiki .srdBf{--shiki-light:#F76D47;--shiki-default:#005CC5;--shiki-dark:#79B8FF}html pre.shiki code .sutJx, html code.shiki .sutJx{--shiki-light:#90A4AE;--shiki-light-font-style:italic;--shiki-default:#6A737D;--shiki-default-font-style:inherit;--shiki-dark:#6A737D;--shiki-dark-font-style:inherit}html pre.shiki code .sptTA, html code.shiki .sptTA{--shiki-light:#6182B8;--shiki-default:#005CC5;--shiki-dark:#79B8FF}html pre.shiki code .slqww, html code.shiki .slqww{--shiki-light:#6182B8;--shiki-default:#24292E;--shiki-dark:#E1E4E8}html 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 .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 .s39Yj, html code.shiki .s39Yj{--shiki-light:#39ADB5;--shiki-default:#005CC5;--shiki-dark:#79B8FF}html pre.shiki code .sbsja, html code.shiki .sbsja{--shiki-light:#9C3EDA;--shiki-default:#D73A49;--shiki-dark:#F97583}",{"title":96,"searchDepth":135,"depth":135,"links":48703},[48704,48705,48706,48707,48708,48714,48718,48719,48720,48721,48722,48728],{"id":79,"depth":135,"text":80},{"id":47051,"depth":135,"text":47052},{"id":47113,"depth":135,"text":47114},{"id":47200,"depth":135,"text":47201},{"id":47278,"depth":135,"text":47279,"children":48709},[48710,48712,48713],{"id":47388,"depth":142,"text":48711},"Fix 1: Use get()",{"id":47572,"depth":142,"text":47573},{"id":47680,"depth":142,"text":47681},{"id":47775,"depth":135,"text":47776,"children":48715},[48716,48717],{"id":47859,"depth":142,"text":47860},{"id":47954,"depth":142,"text":47955},{"id":48152,"depth":135,"text":48153},{"id":6923,"depth":135,"text":6924},{"id":48477,"depth":135,"text":48478},{"id":48529,"depth":135,"text":48530},{"id":1513,"depth":135,"text":1514,"children":48723},[48724,48725,48726,48727],{"id":48631,"depth":142,"text":48632},{"id":48638,"depth":142,"text":48639},{"id":48647,"depth":142,"text":48648},{"id":34952,"depth":142,"text":34953},{"id":1553,"depth":135,"text":1554},"Master keyerror vs indexerror in python explained in our comprehensive Python beginner guide.",{},{"title":46853,"description":48729},"errors\u002Fkeyerror-vs-indexerror-in-python-explained","8llLg-kM4wvHGGk0ot9MrRYCDVuH6sy_1wezHkr-dK0",{"id":48735,"title":48736,"body":48737,"description":50709,"extension":1623,"meta":50710,"navigation":138,"path":43909,"seo":50711,"stem":50712,"__hash__":50713},"content\u002Ferrors\u002Fkeyerror-when-accessing-dictionary-values-fix.md","KeyError when accessing dictionary values (Fix)",{"type":7,"value":48738,"toc":50681},[48739,48742,48747,48753,48756,48758,48891,48897,48899,48904,48906,48952,48954,48970,48972,48987,48991,48993,48995,49012,49015,49111,49116,49118,49120,49171,49173,49189,49191,49212,49217,49221,49226,49292,49294,49302,49305,49374,49376,49384,49386,49409,49412,49417,49421,49424,49511,49513,49525,49528,49531,49542,49546,49550,49555,49634,49636,49648,49651,49662,49667,49670,49757,49759,49767,49771,49774,49776,49852,49855,49887,49889,49999,50001,50072,50080,50087,50089,50092,50096,50161,50170,50174,50282,50286,50369,50373,50458,50461,50508,50510,50523,50531,50533,50536,50554,50556,50561,50586,50588,50598,50609,50616,50622,50629,50634,50638,50641,50643,50666,50678],[10,48740,48736],{"id":48741},"keyerror-when-accessing-dictionary-values-fix",[14,48743,571,48744,48746],{},[17,48745,17063],{}," happens when you try to access a dictionary value using a key that does not exist.",[14,48748,48749,48750,48752],{},"This usually happens with square bracket access like ",[17,48751,43960],{},". If the key is missing, Python stops and raises an error.",[14,48754,48755],{},"This page shows what the error means, why it happens, and how to fix it safely.",[77,48757,80],{"id":79},[91,48759,48761],{"className":93,"code":48760,"language":95,"meta":96,"style":96},"data = {\"name\": \"Ana\"}\n\n# Safe access\nvalue = data.get(\"age\")\nprint(value)  # None\n\n# Or check first\nif \"age\" in data:\n    print(data[\"age\"])\nelse:\n    print(\"Key not found\")\n",[17,48762,48763,48787,48791,48795,48817,48829,48833,48837,48853,48871,48877],{"__ignoreMap":96},[100,48764,48765,48767,48769,48771,48773,48775,48777,48779,48781,48783,48785],{"class":102,"line":103},[100,48766,7909],{"class":106},[100,48768,111],{"class":110},[100,48770,12080],{"class":118},[100,48772,206],{"class":205},[100,48774,2853],{"class":209},[100,48776,206],{"class":205},[100,48778,89],{"class":118},[100,48780,1708],{"class":205},[100,48782,34819],{"class":209},[100,48784,206],{"class":205},[100,48786,12093],{"class":118},[100,48788,48789],{"class":102,"line":135},[100,48790,139],{"emptyLinePlaceholder":138},[100,48792,48793],{"class":102,"line":142},[100,48794,42356],{"class":414},[100,48796,48797,48799,48801,48803,48805,48807,48809,48811,48813,48815],{"class":102,"line":152},[100,48798,3318],{"class":106},[100,48800,111],{"class":110},[100,48802,12102],{"class":106},[100,48804,290],{"class":118},[100,48806,12412],{"class":178},[100,48808,170],{"class":118},[100,48810,206],{"class":205},[100,48812,2770],{"class":209},[100,48814,206],{"class":205},[100,48816,215],{"class":118},[100,48818,48819,48821,48823,48825,48827],{"class":102,"line":164},[100,48820,372],{"class":114},[100,48822,170],{"class":118},[100,48824,757],{"class":178},[100,48826,6155],{"class":118},[100,48828,44041],{"class":414},[100,48830,48831],{"class":102,"line":185},[100,48832,139],{"emptyLinePlaceholder":138},[100,48834,48835],{"class":102,"line":197},[100,48836,44050],{"class":414},[100,48838,48839,48841,48843,48845,48847,48849,48851],{"class":102,"line":771},[100,48840,2736],{"class":145},[100,48842,1708],{"class":205},[100,48844,2770],{"class":209},[100,48846,206],{"class":205},[100,48848,1971],{"class":110},[100,48850,12102],{"class":106},[100,48852,149],{"class":118},[100,48854,48855,48857,48859,48861,48863,48865,48867,48869],{"class":102,"line":787},[100,48856,200],{"class":114},[100,48858,170],{"class":118},[100,48860,7934],{"class":178},[100,48862,3166],{"class":118},[100,48864,206],{"class":205},[100,48866,2770],{"class":209},[100,48868,206],{"class":205},[100,48870,132],{"class":118},[100,48872,48873,48875],{"class":102,"line":5816},[100,48874,4944],{"class":145},[100,48876,149],{"class":118},[100,48878,48879,48881,48883,48885,48887,48889],{"class":102,"line":5833},[100,48880,200],{"class":114},[100,48882,170],{"class":118},[100,48884,206],{"class":205},[100,48886,42470],{"class":209},[100,48888,206],{"class":205},[100,48890,215],{"class":118},[14,48892,241,48893,44109,48895,42485],{},[17,48894,11018],{},[17,48896,617],{},[77,48898,5881],{"id":5880},[14,48900,571,48901,48903],{},[17,48902,17063],{}," means Python could not find the key in the dictionary.",[14,48905,580],{},[91,48907,48908],{"className":93,"code":34796,"language":95,"meta":96,"style":96},[17,48909,48910,48934],{"__ignoreMap":96},[100,48911,48912,48914,48916,48918,48920,48922,48924,48926,48928,48930,48932],{"class":102,"line":103},[100,48913,7909],{"class":106},[100,48915,111],{"class":110},[100,48917,12080],{"class":118},[100,48919,206],{"class":205},[100,48921,2853],{"class":209},[100,48923,206],{"class":205},[100,48925,89],{"class":118},[100,48927,1708],{"class":205},[100,48929,34819],{"class":209},[100,48931,206],{"class":205},[100,48933,12093],{"class":118},[100,48935,48936,48938,48940,48942,48944,48946,48948,48950],{"class":102,"line":135},[100,48937,372],{"class":114},[100,48939,170],{"class":118},[100,48941,7934],{"class":178},[100,48943,3166],{"class":118},[100,48945,206],{"class":205},[100,48947,2770],{"class":209},[100,48949,206],{"class":205},[100,48951,132],{"class":118},[14,48953,7710],{},[91,48955,48956],{"className":93,"code":17192,"language":95,"meta":96,"style":96},[17,48957,48958],{"__ignoreMap":96},[100,48959,48960,48962,48964,48966,48968],{"class":102,"line":103},[100,48961,17063],{"class":191},[100,48963,89],{"class":118},[100,48965,1274],{"class":205},[100,48967,2770],{"class":209},[100,48969,3925],{"class":205},[14,48971,32043],{},[40,48973,48974,48979,48984],{},[43,48975,48976,48977,290],{},"The dictionary only has the key ",[17,48978,12456],{},[43,48980,48981,48982,290],{},"You asked for ",[17,48983,42657],{},[43,48985,48986],{},"Dictionary keys must match exactly.",[14,48988,44129,48989,290],{},[295,48990,42671],{"href":42670},[77,48992,25091],{"id":25090},[14,48994,14889],{},[40,48996,48997,49000,49003,49006],{},[43,48998,48999],{},"The key does not exist in the dictionary.",[43,49001,49002],{},"The key name has different spelling or capitalization.",[43,49004,49005],{},"You expected data from a file, API, or user input, but the key is missing.",[43,49007,42694,49008,3178,49010,290],{},[17,49009,8104],{},[17,49011,42699],{},[14,49013,49014],{},"A few examples:",[91,49016,49018],{"className":93,"code":49017,"language":95,"meta":96,"style":96},"data = {\"name\": \"Ana\"}\n\nprint(data[\"Name\"])   # different capitalization\nprint(data[\" name\"])  # leading space\nprint(data[\"age\"])    # missing key\n",[17,49019,49020,49044,49048,49069,49090],{"__ignoreMap":96},[100,49021,49022,49024,49026,49028,49030,49032,49034,49036,49038,49040,49042],{"class":102,"line":103},[100,49023,7909],{"class":106},[100,49025,111],{"class":110},[100,49027,12080],{"class":118},[100,49029,206],{"class":205},[100,49031,2853],{"class":209},[100,49033,206],{"class":205},[100,49035,89],{"class":118},[100,49037,1708],{"class":205},[100,49039,34819],{"class":209},[100,49041,206],{"class":205},[100,49043,12093],{"class":118},[100,49045,49046],{"class":102,"line":135},[100,49047,139],{"emptyLinePlaceholder":138},[100,49049,49050,49052,49054,49056,49058,49060,49062,49064,49066],{"class":102,"line":142},[100,49051,372],{"class":114},[100,49053,170],{"class":118},[100,49055,7934],{"class":178},[100,49057,3166],{"class":118},[100,49059,206],{"class":205},[100,49061,42750],{"class":209},[100,49063,206],{"class":205},[100,49065,32896],{"class":118},[100,49067,49068],{"class":414},"   # different capitalization\n",[100,49070,49071,49073,49075,49077,49079,49081,49083,49085,49087],{"class":102,"line":152},[100,49072,372],{"class":114},[100,49074,170],{"class":118},[100,49076,7934],{"class":178},[100,49078,3166],{"class":118},[100,49080,206],{"class":205},[100,49082,1703],{"class":209},[100,49084,206],{"class":205},[100,49086,32896],{"class":118},[100,49088,49089],{"class":414},"  # leading space\n",[100,49091,49092,49094,49096,49098,49100,49102,49104,49106,49108],{"class":102,"line":164},[100,49093,372],{"class":114},[100,49095,170],{"class":118},[100,49097,7934],{"class":178},[100,49099,3166],{"class":118},[100,49101,206],{"class":205},[100,49103,2770],{"class":209},[100,49105,206],{"class":205},[100,49107,32896],{"class":118},[100,49109,49110],{"class":414},"    # missing key\n",[14,49112,49113,49114,290],{},"All of these can raise ",[17,49115,17063],{},[77,49117,11031],{"id":11030},[14,49119,1891],{},[91,49121,49123],{"className":93,"code":49122,"language":95,"meta":96,"style":96},"person = {\"name\": \"Ana\"}\n\nprint(person[\"age\"])\n",[17,49124,49125,49149,49153],{"__ignoreMap":96},[100,49126,49127,49129,49131,49133,49135,49137,49139,49141,49143,49145,49147],{"class":102,"line":103},[100,49128,43766],{"class":106},[100,49130,111],{"class":110},[100,49132,12080],{"class":118},[100,49134,206],{"class":205},[100,49136,2853],{"class":209},[100,49138,206],{"class":205},[100,49140,89],{"class":118},[100,49142,1708],{"class":205},[100,49144,34819],{"class":209},[100,49146,206],{"class":205},[100,49148,12093],{"class":118},[100,49150,49151],{"class":102,"line":135},[100,49152,139],{"emptyLinePlaceholder":138},[100,49154,49155,49157,49159,49161,49163,49165,49167,49169],{"class":102,"line":142},[100,49156,372],{"class":114},[100,49158,170],{"class":118},[100,49160,43801],{"class":178},[100,49162,3166],{"class":118},[100,49164,206],{"class":205},[100,49166,2770],{"class":209},[100,49168,206],{"class":205},[100,49170,132],{"class":118},[14,49172,218],{},[91,49174,49175],{"className":93,"code":17192,"language":95,"meta":96,"style":96},[17,49176,49177],{"__ignoreMap":96},[100,49178,49179,49181,49183,49185,49187],{"class":102,"line":103},[100,49180,17063],{"class":191},[100,49182,89],{"class":118},[100,49184,1274],{"class":205},[100,49186,2770],{"class":209},[100,49188,3925],{"class":205},[14,49190,536],{},[40,49192,49193,49200,49205],{},[43,49194,49195,49197,49198],{},[17,49196,43801],{}," contains one key: ",[17,49199,12456],{},[43,49201,49202,49203],{},"The code tries to read ",[17,49204,42657],{},[43,49206,3860,49207,3516,49209,49211],{},[17,49208,17063],{},[17,49210,42657],{}," is not present",[14,49213,49214,49215,290],{},"If you want to learn the normal way to read dictionary values, see ",[295,49216,45406],{"href":43893},[77,49218,49220],{"id":49219},"fix-1-use-get-for-optional-keys","Fix 1: Use get() for optional keys",[14,49222,241,49223,49225],{},[17,49224,6110],{}," when a key might be missing.",[91,49227,49229],{"className":93,"code":49228,"language":95,"meta":96,"style":96},"person = {\"name\": \"Ana\"}\n\nage = person.get(\"age\")\nprint(age)\n",[17,49230,49231,49255,49259,49282],{"__ignoreMap":96},[100,49232,49233,49235,49237,49239,49241,49243,49245,49247,49249,49251,49253],{"class":102,"line":103},[100,49234,43766],{"class":106},[100,49236,111],{"class":110},[100,49238,12080],{"class":118},[100,49240,206],{"class":205},[100,49242,2853],{"class":209},[100,49244,206],{"class":205},[100,49246,89],{"class":118},[100,49248,1708],{"class":205},[100,49250,34819],{"class":209},[100,49252,206],{"class":205},[100,49254,12093],{"class":118},[100,49256,49257],{"class":102,"line":135},[100,49258,139],{"emptyLinePlaceholder":138},[100,49260,49261,49263,49265,49268,49270,49272,49274,49276,49278,49280],{"class":102,"line":142},[100,49262,1664],{"class":106},[100,49264,111],{"class":110},[100,49266,49267],{"class":106}," person",[100,49269,290],{"class":118},[100,49271,12412],{"class":178},[100,49273,170],{"class":118},[100,49275,206],{"class":205},[100,49277,2770],{"class":209},[100,49279,206],{"class":205},[100,49281,215],{"class":118},[100,49283,49284,49286,49288,49290],{"class":102,"line":152},[100,49285,372],{"class":114},[100,49287,170],{"class":118},[100,49289,2770],{"class":178},[100,49291,215],{"class":118},[14,49293,218],{},[91,49295,49296],{"className":93,"code":924,"language":95,"meta":96,"style":96},[17,49297,49298],{"__ignoreMap":96},[100,49299,49300],{"class":102,"line":103},[100,49301,924],{"class":158},[14,49303,49304],{},"You can also provide a default value:",[91,49306,49308],{"className":93,"code":49307,"language":95,"meta":96,"style":96},"person = {\"name\": \"Ana\"}\n\nage = person.get(\"age\", 0)\nprint(age)\n",[17,49309,49310,49334,49338,49364],{"__ignoreMap":96},[100,49311,49312,49314,49316,49318,49320,49322,49324,49326,49328,49330,49332],{"class":102,"line":103},[100,49313,43766],{"class":106},[100,49315,111],{"class":110},[100,49317,12080],{"class":118},[100,49319,206],{"class":205},[100,49321,2853],{"class":209},[100,49323,206],{"class":205},[100,49325,89],{"class":118},[100,49327,1708],{"class":205},[100,49329,34819],{"class":209},[100,49331,206],{"class":205},[100,49333,12093],{"class":118},[100,49335,49336],{"class":102,"line":135},[100,49337,139],{"emptyLinePlaceholder":138},[100,49339,49340,49342,49344,49346,49348,49350,49352,49354,49356,49358,49360,49362],{"class":102,"line":142},[100,49341,1664],{"class":106},[100,49343,111],{"class":110},[100,49345,49267],{"class":106},[100,49347,290],{"class":118},[100,49349,12412],{"class":178},[100,49351,170],{"class":118},[100,49353,206],{"class":205},[100,49355,2770],{"class":209},[100,49357,206],{"class":205},[100,49359,126],{"class":118},[100,49361,1921],{"class":122},[100,49363,215],{"class":118},[100,49365,49366,49368,49370,49372],{"class":102,"line":152},[100,49367,372],{"class":114},[100,49369,170],{"class":118},[100,49371,2770],{"class":178},[100,49373,215],{"class":118},[14,49375,218],{},[91,49377,49378],{"className":93,"code":44572,"language":95,"meta":96,"style":96},[17,49379,49380],{"__ignoreMap":96},[100,49381,49382],{"class":102,"line":103},[100,49383,44572],{"class":122},[14,49385,26823],{},[40,49387,49388,49395,49402],{},[43,49389,49390,20442,49393],{},[17,49391,49392],{},"person[\"age\"]",[17,49394,17063],{},[43,49396,49397,551,49400,45245],{},[17,49398,49399],{},"person.get(\"age\")",[17,49401,930],{},[43,49403,49404,551,49407,45237],{},[17,49405,49406],{},"person.get(\"age\", 0)",[17,49408,2279],{},[14,49410,49411],{},"This is useful when missing data is expected.",[14,49413,49414,49415,290],{},"If you want more detail, see the ",[295,49416,12577],{"href":42479},[77,49418,49420],{"id":49419},"fix-2-check-if-the-key-exists-first","Fix 2: Check if the key exists first",[14,49422,49423],{},"You can check before reading the value:",[91,49425,49427],{"className":93,"code":49426,"language":95,"meta":96,"style":96},"person = {\"name\": \"Ana\"}\n\nif \"age\" in person:\n    print(person[\"age\"])\nelse:\n    print(\"Key not found\")\n",[17,49428,49429,49453,49457,49473,49491,49497],{"__ignoreMap":96},[100,49430,49431,49433,49435,49437,49439,49441,49443,49445,49447,49449,49451],{"class":102,"line":103},[100,49432,43766],{"class":106},[100,49434,111],{"class":110},[100,49436,12080],{"class":118},[100,49438,206],{"class":205},[100,49440,2853],{"class":209},[100,49442,206],{"class":205},[100,49444,89],{"class":118},[100,49446,1708],{"class":205},[100,49448,34819],{"class":209},[100,49450,206],{"class":205},[100,49452,12093],{"class":118},[100,49454,49455],{"class":102,"line":135},[100,49456,139],{"emptyLinePlaceholder":138},[100,49458,49459,49461,49463,49465,49467,49469,49471],{"class":102,"line":142},[100,49460,2736],{"class":145},[100,49462,1708],{"class":205},[100,49464,2770],{"class":209},[100,49466,206],{"class":205},[100,49468,1971],{"class":110},[100,49470,49267],{"class":106},[100,49472,149],{"class":118},[100,49474,49475,49477,49479,49481,49483,49485,49487,49489],{"class":102,"line":152},[100,49476,200],{"class":114},[100,49478,170],{"class":118},[100,49480,43801],{"class":178},[100,49482,3166],{"class":118},[100,49484,206],{"class":205},[100,49486,2770],{"class":209},[100,49488,206],{"class":205},[100,49490,132],{"class":118},[100,49492,49493,49495],{"class":102,"line":164},[100,49494,4944],{"class":145},[100,49496,149],{"class":118},[100,49498,49499,49501,49503,49505,49507,49509],{"class":102,"line":185},[100,49500,200],{"class":114},[100,49502,170],{"class":118},[100,49504,206],{"class":205},[100,49506,42470],{"class":209},[100,49508,206],{"class":205},[100,49510,215],{"class":118},[14,49512,218],{},[91,49514,49515],{"className":93,"code":44439,"language":95,"meta":96,"style":96},[17,49516,49517],{"__ignoreMap":96},[100,49518,49519,49521,49523],{"class":102,"line":103},[100,49520,43717],{"class":106},[100,49522,1059],{"class":110},[100,49524,43722],{"class":106},[14,49526,49527],{},"This is a clear and beginner-friendly approach.",[14,49529,49530],{},"Use this when:",[40,49532,49533,49536,49539],{},[43,49534,49535],{},"You want different behavior for missing keys",[43,49537,49538],{},"You want to print a message",[43,49540,49541],{},"You want to avoid an error before it happens",[14,49543,44455,49544,290],{},[295,49545,43100],{"href":43099},[77,49547,49549],{"id":49548},"fix-3-handle-the-error-with-try-except","Fix 3: Handle the error with try-except",[14,49551,49552,49553,89],{},"You can catch the error with ",[17,49554,244],{},[91,49556,49558],{"className":93,"code":49557,"language":95,"meta":96,"style":96},"person = {\"name\": \"Ana\"}\n\ntry:\n    print(person[\"age\"])\nexcept KeyError:\n    print(\"The key was not found\")\n",[17,49559,49560,49584,49588,49594,49612,49620],{"__ignoreMap":96},[100,49561,49562,49564,49566,49568,49570,49572,49574,49576,49578,49580,49582],{"class":102,"line":103},[100,49563,43766],{"class":106},[100,49565,111],{"class":110},[100,49567,12080],{"class":118},[100,49569,206],{"class":205},[100,49571,2853],{"class":209},[100,49573,206],{"class":205},[100,49575,89],{"class":118},[100,49577,1708],{"class":205},[100,49579,34819],{"class":209},[100,49581,206],{"class":205},[100,49583,12093],{"class":118},[100,49585,49586],{"class":102,"line":135},[100,49587,139],{"emptyLinePlaceholder":138},[100,49589,49590,49592],{"class":102,"line":142},[100,49591,146],{"class":145},[100,49593,149],{"class":118},[100,49595,49596,49598,49600,49602,49604,49606,49608,49610],{"class":102,"line":152},[100,49597,200],{"class":114},[100,49599,170],{"class":118},[100,49601,43801],{"class":178},[100,49603,3166],{"class":118},[100,49605,206],{"class":205},[100,49607,2770],{"class":209},[100,49609,206],{"class":205},[100,49611,132],{"class":118},[100,49613,49614,49616,49618],{"class":102,"line":164},[100,49615,188],{"class":145},[100,49617,43194],{"class":191},[100,49619,149],{"class":118},[100,49621,49622,49624,49626,49628,49630,49632],{"class":102,"line":185},[100,49623,200],{"class":114},[100,49625,170],{"class":118},[100,49627,206],{"class":205},[100,49629,45058],{"class":209},[100,49631,206],{"class":205},[100,49633,215],{"class":118},[14,49635,218],{},[91,49637,49638],{"className":93,"code":45067,"language":95,"meta":96,"style":96},[17,49639,49640],{"__ignoreMap":96},[100,49641,49642,49644,49646],{"class":102,"line":103},[100,49643,45074],{"class":106},[100,49645,1059],{"class":110},[100,49647,43722],{"class":106},[14,49649,49650],{},"This is useful when:",[40,49652,49653,49656,49659],{},[43,49654,49655],{},"The key might be missing",[43,49657,49658],{},"You want to handle the problem without stopping the program",[43,49660,49661],{},"The missing key is part of normal program flow",[14,49663,43239,49664,49666],{},[17,49665,146],{}," block small. That makes debugging easier.",[14,49668,49669],{},"You can also use a fallback value:",[91,49671,49673],{"className":93,"code":49672,"language":95,"meta":96,"style":96},"person = {\"name\": \"Ana\"}\n\ntry:\n    age = person[\"age\"]\nexcept KeyError:\n    age = 0\n\nprint(age)\n",[17,49674,49675,49699,49703,49709,49727,49735,49743,49747],{"__ignoreMap":96},[100,49676,49677,49679,49681,49683,49685,49687,49689,49691,49693,49695,49697],{"class":102,"line":103},[100,49678,43766],{"class":106},[100,49680,111],{"class":110},[100,49682,12080],{"class":118},[100,49684,206],{"class":205},[100,49686,2853],{"class":209},[100,49688,206],{"class":205},[100,49690,89],{"class":118},[100,49692,1708],{"class":205},[100,49694,34819],{"class":209},[100,49696,206],{"class":205},[100,49698,12093],{"class":118},[100,49700,49701],{"class":102,"line":135},[100,49702,139],{"emptyLinePlaceholder":138},[100,49704,49705,49707],{"class":102,"line":142},[100,49706,146],{"class":145},[100,49708,149],{"class":118},[100,49710,49711,49713,49715,49717,49719,49721,49723,49725],{"class":102,"line":152},[100,49712,31041],{"class":106},[100,49714,111],{"class":110},[100,49716,49267],{"class":106},[100,49718,3166],{"class":118},[100,49720,206],{"class":205},[100,49722,2770],{"class":209},[100,49724,206],{"class":205},[100,49726,603],{"class":118},[100,49728,49729,49731,49733],{"class":102,"line":164},[100,49730,188],{"class":145},[100,49732,43194],{"class":191},[100,49734,149],{"class":118},[100,49736,49737,49739,49741],{"class":102,"line":185},[100,49738,31041],{"class":106},[100,49740,111],{"class":110},[100,49742,2069],{"class":122},[100,49744,49745],{"class":102,"line":197},[100,49746,139],{"emptyLinePlaceholder":138},[100,49748,49749,49751,49753,49755],{"class":102,"line":771},[100,49750,372],{"class":114},[100,49752,170],{"class":118},[100,49754,2770],{"class":178},[100,49756,215],{"class":118},[14,49758,218],{},[91,49760,49761],{"className":93,"code":44572,"language":95,"meta":96,"style":96},[17,49762,49763],{"__ignoreMap":96},[100,49764,49765],{"class":102,"line":103},[100,49766,44572],{"class":122},[77,49768,49770],{"id":49769},"how-to-debug-a-keyerror","How to debug a KeyError",[14,49772,49773],{},"If you are not sure why the error happens, inspect the dictionary first.",[14,49775,5295],{},[91,49777,49779],{"className":93,"code":49778,"language":95,"meta":96,"style":96},"print(data)\nprint(data.keys())\nprint(\"age\" in data)\nprint(repr(user_key))\nprint(type(user_key))\n",[17,49780,49781,49791,49805,49823,49838],{"__ignoreMap":96},[100,49782,49783,49785,49787,49789],{"class":102,"line":103},[100,49784,372],{"class":114},[100,49786,170],{"class":118},[100,49788,7934],{"class":178},[100,49790,215],{"class":118},[100,49792,49793,49795,49797,49799,49801,49803],{"class":102,"line":135},[100,49794,372],{"class":114},[100,49796,170],{"class":118},[100,49798,7934],{"class":178},[100,49800,290],{"class":118},[100,49802,6185],{"class":178},[100,49804,3370],{"class":118},[100,49806,49807,49809,49811,49813,49815,49817,49819,49821],{"class":102,"line":142},[100,49808,372],{"class":114},[100,49810,170],{"class":118},[100,49812,206],{"class":205},[100,49814,2770],{"class":209},[100,49816,206],{"class":205},[100,49818,1971],{"class":145},[100,49820,12102],{"class":178},[100,49822,215],{"class":118},[100,49824,49825,49827,49829,49831,49833,49836],{"class":102,"line":152},[100,49826,372],{"class":114},[100,49828,170],{"class":118},[100,49830,12228],{"class":114},[100,49832,170],{"class":118},[100,49834,49835],{"class":178},"user_key",[100,49837,182],{"class":118},[100,49839,49840,49842,49844,49846,49848,49850],{"class":102,"line":164},[100,49841,372],{"class":114},[100,49843,170],{"class":118},[100,49845,1250],{"class":191},[100,49847,170],{"class":118},[100,49849,49835],{"class":178},[100,49851,182],{"class":118},[14,49853,49854],{},"What these help you check:",[40,49856,49857,49861,49866,49872,49881],{},[43,49858,49859,46518],{},[17,49860,48449],{},[43,49862,49863,49865],{},[17,49864,48455],{}," shows the available keys",[43,49867,49868,49871],{},[17,49869,49870],{},"print(\"age\" in data)"," checks whether a specific key exists",[43,49873,49874,49877,49878],{},[17,49875,49876],{},"print(repr(user_key))"," helps reveal hidden spaces like ",[17,49879,49880],{},"\"age \"",[43,49882,49883,49886],{},[17,49884,49885],{},"print(type(user_key))"," shows whether the key is a string, integer, or something else",[14,49888,1844],{},[91,49890,49892],{"className":93,"code":49891,"language":95,"meta":96,"style":96},"data = {\"name\": \"Ana\", \"age \": 25}\nuser_key = \"age\"\n\nprint(data)\nprint(data.keys())\nprint(repr(user_key))\nprint(user_key in data)\n",[17,49893,49894,49930,49943,49947,49957,49971,49985],{"__ignoreMap":96},[100,49895,49896,49898,49900,49902,49904,49906,49908,49910,49912,49914,49916,49918,49920,49922,49924,49926,49928],{"class":102,"line":103},[100,49897,7909],{"class":106},[100,49899,111],{"class":110},[100,49901,12080],{"class":118},[100,49903,206],{"class":205},[100,49905,2853],{"class":209},[100,49907,206],{"class":205},[100,49909,89],{"class":118},[100,49911,1708],{"class":205},[100,49913,34819],{"class":209},[100,49915,206],{"class":205},[100,49917,126],{"class":118},[100,49919,1708],{"class":205},[100,49921,1664],{"class":209},[100,49923,206],{"class":205},[100,49925,89],{"class":118},[100,49927,16782],{"class":122},[100,49929,12093],{"class":118},[100,49931,49932,49935,49937,49939,49941],{"class":102,"line":135},[100,49933,49934],{"class":106},"user_key ",[100,49936,111],{"class":110},[100,49938,1708],{"class":205},[100,49940,2770],{"class":209},[100,49942,1714],{"class":205},[100,49944,49945],{"class":102,"line":142},[100,49946,139],{"emptyLinePlaceholder":138},[100,49948,49949,49951,49953,49955],{"class":102,"line":152},[100,49950,372],{"class":114},[100,49952,170],{"class":118},[100,49954,7934],{"class":178},[100,49956,215],{"class":118},[100,49958,49959,49961,49963,49965,49967,49969],{"class":102,"line":164},[100,49960,372],{"class":114},[100,49962,170],{"class":118},[100,49964,7934],{"class":178},[100,49966,290],{"class":118},[100,49968,6185],{"class":178},[100,49970,3370],{"class":118},[100,49972,49973,49975,49977,49979,49981,49983],{"class":102,"line":185},[100,49974,372],{"class":114},[100,49976,170],{"class":118},[100,49978,12228],{"class":114},[100,49980,170],{"class":118},[100,49982,49835],{"class":178},[100,49984,182],{"class":118},[100,49986,49987,49989,49991,49993,49995,49997],{"class":102,"line":197},[100,49988,372],{"class":114},[100,49990,170],{"class":118},[100,49992,49934],{"class":178},[100,49994,617],{"class":145},[100,49996,12102],{"class":178},[100,49998,215],{"class":118},[14,50000,218],{},[91,50002,50004],{"className":93,"code":50003,"language":95,"meta":96,"style":96},"{'name': 'Ana', 'age ': 25}\ndict_keys(['name', 'age '])\n'age'\nFalse\n",[17,50005,50006,50038,50060,50068],{"__ignoreMap":96},[100,50007,50008,50010,50012,50014,50016,50018,50020,50022,50024,50026,50028,50030,50032,50034,50036],{"class":102,"line":103},[100,50009,16490],{"class":118},[100,50011,1280],{"class":205},[100,50013,2853],{"class":209},[100,50015,1280],{"class":205},[100,50017,89],{"class":118},[100,50019,1274],{"class":205},[100,50021,34819],{"class":209},[100,50023,1280],{"class":205},[100,50025,126],{"class":118},[100,50027,1274],{"class":205},[100,50029,1664],{"class":209},[100,50031,1280],{"class":205},[100,50033,89],{"class":118},[100,50035,16782],{"class":122},[100,50037,12093],{"class":118},[100,50039,50040,50042,50044,50046,50048,50050,50052,50054,50056,50058],{"class":102,"line":135},[100,50041,43660],{"class":178},[100,50043,119],{"class":118},[100,50045,1280],{"class":205},[100,50047,2853],{"class":209},[100,50049,1280],{"class":205},[100,50051,126],{"class":118},[100,50053,1274],{"class":205},[100,50055,1664],{"class":209},[100,50057,1280],{"class":205},[100,50059,132],{"class":118},[100,50061,50062,50064,50066],{"class":102,"line":142},[100,50063,1280],{"class":3553},[100,50065,2770],{"class":3556},[100,50067,3925],{"class":3553},[100,50069,50070],{"class":102,"line":152},[100,50071,2407],{"class":158},[14,50073,50074,50075,50077,50078,290],{},"Here the real key is ",[17,50076,49880],{}," with a trailing space, not ",[17,50079,42657],{},[14,50081,50082,50083,290],{},"If your dictionary came from JSON, inspect the full data before assuming a field exists. See ",[295,50084,50086],{"href":50085},"\u002Fhow-to\u002Fhow-to-parse-json-in-python","how to parse JSON in Python",[77,50088,36389],{"id":36388},[14,50090,50091],{},"These are very common beginner cases:",[675,50093,50095],{"id":50094},"reading-json-data-and-assuming-a-field-always-exists","Reading JSON data and assuming a field always exists",[91,50097,50099],{"className":93,"code":50098,"language":95,"meta":96,"style":96},"data = {\"name\": \"Ana\"}\n\nemail = data.get(\"email\")\nprint(email)\n",[17,50100,50101,50125,50129,50151],{"__ignoreMap":96},[100,50102,50103,50105,50107,50109,50111,50113,50115,50117,50119,50121,50123],{"class":102,"line":103},[100,50104,7909],{"class":106},[100,50106,111],{"class":110},[100,50108,12080],{"class":118},[100,50110,206],{"class":205},[100,50112,2853],{"class":209},[100,50114,206],{"class":205},[100,50116,89],{"class":118},[100,50118,1708],{"class":205},[100,50120,34819],{"class":209},[100,50122,206],{"class":205},[100,50124,12093],{"class":118},[100,50126,50127],{"class":102,"line":135},[100,50128,139],{"emptyLinePlaceholder":138},[100,50130,50131,50133,50135,50137,50139,50141,50143,50145,50147,50149],{"class":102,"line":142},[100,50132,11747],{"class":106},[100,50134,111],{"class":110},[100,50136,12102],{"class":106},[100,50138,290],{"class":118},[100,50140,12412],{"class":178},[100,50142,170],{"class":118},[100,50144,206],{"class":205},[100,50146,11779],{"class":209},[100,50148,206],{"class":205},[100,50150,215],{"class":118},[100,50152,50153,50155,50157,50159],{"class":102,"line":152},[100,50154,372],{"class":114},[100,50156,170],{"class":118},[100,50158,11779],{"class":178},[100,50160,215],{"class":118},[14,50162,50163,50164,50167,50168,290],{},"If you used ",[17,50165,50166],{},"data[\"email\"]",", this would raise ",[17,50169,17063],{},[675,50171,50173],{"id":50172},"using-user-input-as-a-dictionary-key-without-checking-it","Using user input as a dictionary key without checking it",[91,50175,50177],{"className":93,"code":50176,"language":95,"meta":96,"style":96},"prices = {\"apple\": 2, \"banana\": 3}\n\nitem = \"orange\"\n\nif item in prices:\n    print(prices[item])\nelse:\n    print(\"Item not found\")\n",[17,50178,50179,50212,50216,50229,50233,50246,50261,50267],{"__ignoreMap":96},[100,50180,50181,50184,50186,50188,50190,50192,50194,50196,50198,50200,50202,50204,50206,50208,50210],{"class":102,"line":103},[100,50182,50183],{"class":106},"prices ",[100,50185,111],{"class":110},[100,50187,12080],{"class":118},[100,50189,206],{"class":205},[100,50191,4148],{"class":209},[100,50193,206],{"class":205},[100,50195,89],{"class":118},[100,50197,129],{"class":122},[100,50199,126],{"class":118},[100,50201,1708],{"class":205},[100,50203,4157],{"class":209},[100,50205,206],{"class":205},[100,50207,89],{"class":118},[100,50209,1405],{"class":122},[100,50211,12093],{"class":118},[100,50213,50214],{"class":102,"line":135},[100,50215,139],{"emptyLinePlaceholder":138},[100,50217,50218,50221,50223,50225,50227],{"class":102,"line":142},[100,50219,50220],{"class":106},"item ",[100,50222,111],{"class":110},[100,50224,1708],{"class":205},[100,50226,4166],{"class":209},[100,50228,1714],{"class":205},[100,50230,50231],{"class":102,"line":152},[100,50232,139],{"emptyLinePlaceholder":138},[100,50234,50235,50237,50239,50241,50244],{"class":102,"line":164},[100,50236,2736],{"class":145},[100,50238,7568],{"class":106},[100,50240,617],{"class":110},[100,50242,50243],{"class":106}," prices",[100,50245,149],{"class":118},[100,50247,50248,50250,50252,50255,50257,50259],{"class":102,"line":185},[100,50249,200],{"class":114},[100,50251,170],{"class":118},[100,50253,50254],{"class":178},"prices",[100,50256,3166],{"class":118},[100,50258,7555],{"class":178},[100,50260,132],{"class":118},[100,50262,50263,50265],{"class":102,"line":197},[100,50264,4944],{"class":145},[100,50266,149],{"class":118},[100,50268,50269,50271,50273,50275,50278,50280],{"class":102,"line":771},[100,50270,200],{"class":114},[100,50272,170],{"class":118},[100,50274,206],{"class":205},[100,50276,50277],{"class":209},"Item not found",[100,50279,206],{"class":205},[100,50281,215],{"class":118},[675,50283,50285],{"id":50284},"looking-up-a-key-after-removing-it","Looking up a key after removing it",[91,50287,50289],{"className":93,"code":50288,"language":95,"meta":96,"style":96},"data = {\"name\": \"Ana\", \"age\": 20}\n\ndel data[\"age\"]\nprint(data.get(\"age\"))\n",[17,50290,50291,50327,50331,50347],{"__ignoreMap":96},[100,50292,50293,50295,50297,50299,50301,50303,50305,50307,50309,50311,50313,50315,50317,50319,50321,50323,50325],{"class":102,"line":103},[100,50294,7909],{"class":106},[100,50296,111],{"class":110},[100,50298,12080],{"class":118},[100,50300,206],{"class":205},[100,50302,2853],{"class":209},[100,50304,206],{"class":205},[100,50306,89],{"class":118},[100,50308,1708],{"class":205},[100,50310,34819],{"class":209},[100,50312,206],{"class":205},[100,50314,126],{"class":118},[100,50316,1708],{"class":205},[100,50318,2770],{"class":209},[100,50320,206],{"class":205},[100,50322,89],{"class":118},[100,50324,361],{"class":122},[100,50326,12093],{"class":118},[100,50328,50329],{"class":102,"line":135},[100,50330,139],{"emptyLinePlaceholder":138},[100,50332,50333,50335,50337,50339,50341,50343,50345],{"class":102,"line":142},[100,50334,42699],{"class":145},[100,50336,12102],{"class":106},[100,50338,3166],{"class":118},[100,50340,206],{"class":205},[100,50342,2770],{"class":209},[100,50344,206],{"class":205},[100,50346,603],{"class":118},[100,50348,50349,50351,50353,50355,50357,50359,50361,50363,50365,50367],{"class":102,"line":152},[100,50350,372],{"class":114},[100,50352,170],{"class":118},[100,50354,7934],{"class":178},[100,50356,290],{"class":118},[100,50358,12412],{"class":178},[100,50360,170],{"class":118},[100,50362,206],{"class":205},[100,50364,2770],{"class":209},[100,50366,206],{"class":205},[100,50368,182],{"class":118},[675,50370,50372],{"id":50371},"confusing-string-keys-and-integer-keys","Confusing string keys and integer keys",[91,50374,50376],{"className":93,"code":50375,"language":95,"meta":96,"style":96},"data = {\"1\": \"one\", 2: \"two\"}\n\nprint(data[\"1\"])  # works\nprint(data[2])    # works\n",[17,50377,50378,50416,50420,50441],{"__ignoreMap":96},[100,50379,50380,50382,50384,50386,50388,50390,50392,50394,50396,50399,50401,50403,50405,50407,50409,50412,50414],{"class":102,"line":103},[100,50381,7909],{"class":106},[100,50383,111],{"class":110},[100,50385,12080],{"class":118},[100,50387,206],{"class":205},[100,50389,123],{"class":209},[100,50391,206],{"class":205},[100,50393,89],{"class":118},[100,50395,1708],{"class":205},[100,50397,50398],{"class":209},"one",[100,50400,206],{"class":205},[100,50402,126],{"class":118},[100,50404,129],{"class":122},[100,50406,89],{"class":118},[100,50408,1708],{"class":205},[100,50410,50411],{"class":209},"two",[100,50413,206],{"class":205},[100,50415,12093],{"class":118},[100,50417,50418],{"class":102,"line":135},[100,50419,139],{"emptyLinePlaceholder":138},[100,50421,50422,50424,50426,50428,50430,50432,50434,50436,50438],{"class":102,"line":142},[100,50423,372],{"class":114},[100,50425,170],{"class":118},[100,50427,7934],{"class":178},[100,50429,3166],{"class":118},[100,50431,206],{"class":205},[100,50433,123],{"class":209},[100,50435,206],{"class":205},[100,50437,32896],{"class":118},[100,50439,50440],{"class":414},"  # works\n",[100,50442,50443,50445,50447,50449,50451,50453,50455],{"class":102,"line":152},[100,50444,372],{"class":114},[100,50446,170],{"class":118},[100,50448,7934],{"class":178},[100,50450,3166],{"class":118},[100,50452,559],{"class":122},[100,50454,32896],{"class":118},[100,50456,50457],{"class":414},"    # works\n",[14,50459,50460],{},"But this would fail:",[91,50462,50464],{"className":93,"code":50463,"language":95,"meta":96,"style":96},"data = {\"1\": \"one\"}\n\nprint(data[1])\n",[17,50465,50466,50490,50494],{"__ignoreMap":96},[100,50467,50468,50470,50472,50474,50476,50478,50480,50482,50484,50486,50488],{"class":102,"line":103},[100,50469,7909],{"class":106},[100,50471,111],{"class":110},[100,50473,12080],{"class":118},[100,50475,206],{"class":205},[100,50477,123],{"class":209},[100,50479,206],{"class":205},[100,50481,89],{"class":118},[100,50483,1708],{"class":205},[100,50485,50398],{"class":209},[100,50487,206],{"class":205},[100,50489,12093],{"class":118},[100,50491,50492],{"class":102,"line":135},[100,50493,139],{"emptyLinePlaceholder":138},[100,50495,50496,50498,50500,50502,50504,50506],{"class":102,"line":142},[100,50497,372],{"class":114},[100,50499,170],{"class":118},[100,50501,7934],{"class":178},[100,50503,3166],{"class":118},[100,50505,123],{"class":122},[100,50507,132],{"class":118},[14,50509,218],{},[91,50511,50513],{"className":93,"code":50512,"language":95,"meta":96,"style":96},"KeyError: 1\n",[17,50514,50515],{"__ignoreMap":96},[100,50516,50517,50519,50521],{"class":102,"line":103},[100,50518,17063],{"class":191},[100,50520,89],{"class":118},[100,50522,1093],{"class":122},[14,50524,50525,50526,50528,50529,45516],{},"The string ",[17,50527,34917],{}," and the integer ",[17,50530,123],{},[77,50532,30537],{"id":30536},[14,50534,50535],{},"Some related pages may also help:",[40,50537,50538,50543,50549],{},[43,50539,50540,50541],{},"A broader explanation of this exception: ",[295,50542,17238],{"href":43938},[43,50544,50545,50546],{},"A closely related dictionary problem: ",[295,50547,50548],{"href":43903},"KeyError: key not found in dictionary (fix)",[43,50550,50551,50552],{},"If you are working with dictionary methods, you may also need ",[295,50553,12577],{"href":42479},[77,50555,7117],{"id":7116},[14,50557,50558,50559,89],{},"These are frequent causes of ",[17,50560,17063],{},[40,50562,50563,50568,50575,50578,50581,50584],{},[43,50564,1357,50565,50567],{},[17,50566,42500],{}," when the key is not in the dictionary",[43,50569,50570,50571,15957,50573],{},"Wrong capitalization, such as ",[17,50572,42764],{},[17,50574,12456],{},[43,50576,50577],{},"Leading or trailing spaces in the key",[43,50579,50580],{},"Expecting a key from JSON or API data that is not always included",[43,50582,50583],{},"Mixing string keys and integer keys",[43,50585,43750],{},[77,50587,1514],{"id":1513},[675,50589,50591,50592,3266,50595,11353],{"id":50590},"what-is-the-difference-between-datakey-and-datagetkey","What is the difference between ",[17,50593,50594],{},"data[\"key\"]",[17,50596,50597],{},"data.get(\"key\")",[14,50599,50600,50601,50603,50604,551,50606,50608],{},"Square brackets raise ",[17,50602,17063],{}," if the key is missing. ",[17,50605,13068],{},[17,50607,930],{}," or a default value.",[675,50610,50612,50613,50615],{"id":50611},"should-i-always-use-get-instead-of-square-brackets","Should I always use ",[17,50614,13068],{}," instead of square brackets?",[14,50617,50618,50619,50621],{},"No. Use square brackets when the key must exist. Use ",[17,50620,13068],{}," when the key might be missing.",[675,50623,50625,50626,50628],{"id":50624},"can-keyerror-happen-with-json-data","Can ",[17,50627,17063],{}," happen with JSON data?",[14,50630,50631,50632,290],{},"Yes. JSON objects become Python dictionaries, so missing fields can cause ",[17,50633,17063],{},[675,50635,50637],{"id":50636},"why-does-my-key-look-correct-but-still-fail","Why does my key look correct but still fail?",[14,50639,50640],{},"Check capitalization, spaces, and whether the key is a string or a number.",[77,50642,1554],{"id":1553},[40,50644,50645,50649,50653,50657,50661],{},[43,50646,50647],{},[295,50648,42671],{"href":42670},[43,50650,50651],{},[295,50652,12577],{"href":42479},[43,50654,50655],{},[295,50656,43894],{"href":43893},[43,50658,50659],{},[295,50660,43888],{"href":43099},[43,50662,50663],{},[295,50664,50665],{"href":50085},"How to parse JSON in Python",[14,50667,50668,50669,50671,50672,50674,50675,50677],{},"Once you fix this error, the next step is learning how to safely read dictionary data in everyday code. Using ",[17,50670,6110],{},", checking with ",[17,50673,617],{},", and inspecting real input data will help you avoid ",[17,50676,17063],{}," in the future.",[1589,50679,50680],{},"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 .sP7_E, html code.shiki .sP7_E{--shiki-light:#39ADB5;--shiki-default:#24292E;--shiki-dark:#E1E4E8}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 .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 .slqww, html code.shiki .slqww{--shiki-light:#6182B8;--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 .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 .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 .s39Yj, html code.shiki .s39Yj{--shiki-light:#39ADB5;--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 .s2W-s, html code.shiki .s2W-s{--shiki-light:#39ADB5;--shiki-light-font-style:italic;--shiki-default:#032F62;--shiki-default-font-style:inherit;--shiki-dark:#9ECBFF;--shiki-dark-font-style:inherit}html pre.shiki code .sithA, html code.shiki .sithA{--shiki-light:#90A4AE;--shiki-light-font-style:italic;--shiki-default:#032F62;--shiki-default-font-style:inherit;--shiki-dark:#9ECBFF;--shiki-dark-font-style:inherit}",{"title":96,"searchDepth":135,"depth":135,"links":50682},[50683,50684,50685,50686,50687,50688,50689,50690,50691,50697,50698,50699,50708],{"id":79,"depth":135,"text":80},{"id":5880,"depth":135,"text":5881},{"id":25090,"depth":135,"text":25091},{"id":11030,"depth":135,"text":11031},{"id":49219,"depth":135,"text":49220},{"id":49419,"depth":135,"text":49420},{"id":49548,"depth":135,"text":49549},{"id":49769,"depth":135,"text":49770},{"id":36388,"depth":135,"text":36389,"children":50692},[50693,50694,50695,50696],{"id":50094,"depth":142,"text":50095},{"id":50172,"depth":142,"text":50173},{"id":50284,"depth":142,"text":50285},{"id":50371,"depth":142,"text":50372},{"id":30536,"depth":135,"text":30537},{"id":7116,"depth":135,"text":7117},{"id":1513,"depth":135,"text":1514,"children":50700},[50701,50703,50705,50707],{"id":50590,"depth":142,"text":50702},"What is the difference between data[\"key\"] and data.get(\"key\")?",{"id":50611,"depth":142,"text":50704},"Should I always use get() instead of square brackets?",{"id":50624,"depth":142,"text":50706},"Can KeyError happen with JSON data?",{"id":50636,"depth":142,"text":50637},{"id":1553,"depth":135,"text":1554},"Master keyerror when accessing dictionary values fix in our comprehensive Python beginner guide.",{},{"title":48736,"description":50709},"errors\u002Fkeyerror-when-accessing-dictionary-values-fix","kFzHN8ifAxDhsLspxm3F_8C06rqcc5ZDCBPWaz7bBj4",{"id":50715,"title":50716,"body":50717,"description":52159,"extension":1623,"meta":52160,"navigation":138,"path":52161,"seo":52162,"stem":52163,"__hash__":52164},"content\u002Ferrors\u002Fmemoryerror-in-python-causes-and-fixes.md","MemoryError in Python: Causes and Fixes",{"type":7,"value":50718,"toc":52130},[50719,50722,50728,50731,50733,50736,50793,50802,50808,50812,50817,50819,50832,50835,50849,50851,50854,50874,50877,50912,50967,51016,51019,51023,51026,51053,51060,51067,51070,51135,51137,51154,51159,51161,51165,51168,51205,51208,51288,51293,51297,51300,51302,51351,51354,51416,51425,51429,51432,51560,51563,51567,51570,51623,51626,51630,51633,51635,51650,51652,51657,51661,51664,51678,51682,51685,51762,51764,51788,51792,51795,51869,51871,51900,51903,51907,51910,51937,51940,51978,51981,51985,51988,51991,51995,51998,52001,52012,52015,52017,52022,52045,52048,52059,52061,52065,52068,52072,52078,52082,52085,52089,52092,52094,52121,52127],[10,50720,50716],{"id":50721},"memoryerror-in-python-causes-and-fixes",[14,50723,50724,50727],{},[17,50725,50726],{},"MemoryError"," means Python tried to create or grow an object, but there was not enough memory available.",[14,50729,50730],{},"This usually happens when a program creates very large lists, strings, dictionaries, or reads too much data into memory at once. The fix is often not just “use more RAM.” In many cases, the better solution is to change how the program handles data.",[77,50732,80],{"id":79},[14,50734,50735],{},"A common fix is to avoid creating very large lists when you only need to loop through values.",[91,50737,50739],{"className":93,"code":50738,"language":95,"meta":96,"style":96},"numbers = range(10_000_000)\n# Better than: numbers = list(range(10_000_000))\n\nfor n in numbers:\n    if n > 5:\n        break\n",[17,50740,50741,50756,50761,50765,50777,50789],{"__ignoreMap":96},[100,50742,50743,50745,50747,50749,50751,50754],{"class":102,"line":103},[100,50744,107],{"class":106},[100,50746,111],{"class":110},[100,50748,29843],{"class":114},[100,50750,170],{"class":118},[100,50752,50753],{"class":122},"10_000_000",[100,50755,215],{"class":118},[100,50757,50758],{"class":102,"line":135},[100,50759,50760],{"class":414},"# Better than: numbers = list(range(10_000_000))\n",[100,50762,50763],{"class":102,"line":142},[100,50764,139],{"emptyLinePlaceholder":138},[100,50766,50767,50769,50771,50773,50775],{"class":102,"line":152},[100,50768,71],{"class":145},[100,50770,1418],{"class":106},[100,50772,617],{"class":145},[100,50774,620],{"class":106},[100,50776,149],{"class":118},[100,50778,50779,50781,50783,50785,50787],{"class":102,"line":164},[100,50780,2777],{"class":145},[100,50782,1418],{"class":106},[100,50784,1918],{"class":110},[100,50786,31164],{"class":122},[100,50788,149],{"class":118},[100,50790,50791],{"class":102,"line":185},[100,50792,790],{"class":145},[14,50794,50795,50798,50799,50801],{},[17,50796,50797],{},"range(10_000_000)"," is memory-efficient because it does ",[22,50800,1059],{}," store all ten million numbers in a list.",[14,50803,50804,50805,290],{},"If you want to understand this idea better, see ",[295,50806,1164],{"href":50807},"\u002Flearn\u002Fgenerators-in-python-explained\u002F",[77,50809,50811],{"id":50810},"what-memoryerror-means","What MemoryError means",[14,50813,50814,50816],{},[17,50815,50726],{}," happens when Python cannot get enough memory for an object.",[14,50818,12895],{},[40,50820,50821,50824,50827],{},[43,50822,50823],{},"Your code asked Python to store too much data",[43,50825,50826],{},"Python could not fit that data into available memory",[43,50828,50829,50830],{},"The program stopped with a ",[17,50831,50726],{},[14,50833,50834],{},"This often appears when:",[40,50836,50837,50840,50843,50846],{},[43,50838,50839],{},"Creating a huge list",[43,50841,50842],{},"Building a very large string",[43,50844,50845],{},"Loading a large file all at once",[43,50847,50848],{},"Growing a data structure in a loop for too long",[77,50850,29975],{"id":29974},[14,50852,50853],{},"Some common causes are:",[40,50855,50856,50862,50865,50868,50871],{},[43,50857,50858,50859],{},"Building a huge list with ",[17,50860,50861],{},"list(range(...))",[43,50863,50864],{},"Reading a very large file all at once",[43,50866,50867],{},"Creating many copies of large objects",[43,50869,50870],{},"Using nested lists or dictionaries that grow too much",[43,50872,50873],{},"Infinite loops that keep appending data",[14,50875,50876],{},"Here are some typical examples:",[91,50878,50880],{"className":93,"code":50879,"language":95,"meta":96,"style":96},"# Huge list\nnumbers = list(range(10**8))\n",[17,50881,50882,50887],{"__ignoreMap":96},[100,50883,50884],{"class":102,"line":103},[100,50885,50886],{"class":414},"# Huge list\n",[100,50888,50889,50891,50893,50895,50897,50900,50902,50904,50907,50910],{"class":102,"line":135},[100,50890,107],{"class":106},[100,50892,111],{"class":110},[100,50894,14704],{"class":191},[100,50896,170],{"class":118},[100,50898,50899],{"class":114},"range",[100,50901,170],{"class":118},[100,50903,356],{"class":122},[100,50905,50906],{"class":110},"**",[100,50908,50909],{"class":122},"8",[100,50911,182],{"class":118},[91,50913,50915],{"className":93,"code":50914,"language":95,"meta":96,"style":96},"# Reading the whole file into memory\nwith open(\"big_file.txt\", \"r\") as file:\n    data = file.read()\n",[17,50916,50917,50922,50953],{"__ignoreMap":96},[100,50918,50919],{"class":102,"line":103},[100,50920,50921],{"class":414},"# Reading the whole file into memory\n",[100,50923,50924,50926,50928,50930,50932,50935,50937,50939,50941,50943,50945,50947,50949,50951],{"class":102,"line":135},[100,50925,17521],{"class":145},[100,50927,17524],{"class":114},[100,50929,170],{"class":118},[100,50931,206],{"class":205},[100,50933,50934],{"class":209},"big_file.txt",[100,50936,206],{"class":205},[100,50938,126],{"class":118},[100,50940,1708],{"class":205},[100,50942,17540],{"class":209},[100,50944,206],{"class":205},[100,50946,6155],{"class":118},[100,50948,16224],{"class":145},[100,50950,17550],{"class":17549},[100,50952,149],{"class":118},[100,50954,50955,50957,50959,50961,50963,50965],{"class":102,"line":142},[100,50956,17557],{"class":106},[100,50958,111],{"class":110},[100,50960,17550],{"class":17549},[100,50962,290],{"class":118},[100,50964,17566],{"class":178},[100,50966,4734],{"class":118},[91,50968,50970],{"className":93,"code":50969,"language":95,"meta":96,"style":96},"# Infinite growth\nitems = []\n\nwhile True:\n    items.append(\"hello\")\n",[17,50971,50972,50977,50985,50989,50997],{"__ignoreMap":96},[100,50973,50974],{"class":102,"line":103},[100,50975,50976],{"class":414},"# Infinite growth\n",[100,50978,50979,50981,50983],{"class":102,"line":135},[100,50980,3095],{"class":106},[100,50982,111],{"class":110},[100,50984,33843],{"class":118},[100,50986,50987],{"class":102,"line":142},[100,50988,139],{"emptyLinePlaceholder":138},[100,50990,50991,50993,50995],{"class":102,"line":152},[100,50992,721],{"class":145},[100,50994,159],{"class":158},[100,50996,149],{"class":118},[100,50998,50999,51002,51004,51006,51008,51010,51012,51014],{"class":102,"line":164},[100,51000,51001],{"class":106},"    items",[100,51003,290],{"class":118},[100,51005,4254],{"class":178},[100,51007,170],{"class":118},[100,51009,206],{"class":205},[100,51011,3506],{"class":209},[100,51013,206],{"class":205},[100,51015,215],{"class":118},[14,51017,51018],{},"In each case, memory use keeps growing until Python can no longer continue.",[77,51020,51022],{"id":51021},"example-that-causes-memoryerror","Example that causes MemoryError",[14,51024,51025],{},"A classic example is trying to create a very large list:",[91,51027,51029],{"className":93,"code":51028,"language":95,"meta":96,"style":96},"numbers = list(range(10**10))\n",[17,51030,51031],{"__ignoreMap":96},[100,51032,51033,51035,51037,51039,51041,51043,51045,51047,51049,51051],{"class":102,"line":103},[100,51034,107],{"class":106},[100,51036,111],{"class":110},[100,51038,14704],{"class":191},[100,51040,170],{"class":118},[100,51042,50899],{"class":114},[100,51044,170],{"class":118},[100,51046,356],{"class":122},[100,51048,50906],{"class":110},[100,51050,356],{"class":122},[100,51052,182],{"class":118},[14,51054,51055,51056,51059],{},"This is a problem because Python tries to store ",[22,51057,51058],{},"every value"," in memory at the same time.",[14,51061,51062,51063,51066],{},"For a very large number like ",[17,51064,51065],{},"10**10",", that can require far more RAM than most computers have.",[14,51068,51069],{},"By comparison, this is much lighter:",[91,51071,51073],{"className":93,"code":51072,"language":95,"meta":96,"style":96},"numbers = range(10**10)\nprint(numbers[0])\nprint(numbers[1])\nprint(numbers[2])\n",[17,51074,51075,51093,51107,51121],{"__ignoreMap":96},[100,51076,51077,51079,51081,51083,51085,51087,51089,51091],{"class":102,"line":103},[100,51078,107],{"class":106},[100,51080,111],{"class":110},[100,51082,29843],{"class":114},[100,51084,170],{"class":118},[100,51086,356],{"class":122},[100,51088,50906],{"class":110},[100,51090,356],{"class":122},[100,51092,215],{"class":118},[100,51094,51095,51097,51099,51101,51103,51105],{"class":102,"line":135},[100,51096,372],{"class":114},[100,51098,170],{"class":118},[100,51100,179],{"class":178},[100,51102,3166],{"class":118},[100,51104,2279],{"class":122},[100,51106,132],{"class":118},[100,51108,51109,51111,51113,51115,51117,51119],{"class":102,"line":142},[100,51110,372],{"class":114},[100,51112,170],{"class":118},[100,51114,179],{"class":178},[100,51116,3166],{"class":118},[100,51118,123],{"class":122},[100,51120,132],{"class":118},[100,51122,51123,51125,51127,51129,51131,51133],{"class":102,"line":152},[100,51124,372],{"class":114},[100,51126,170],{"class":118},[100,51128,179],{"class":178},[100,51130,3166],{"class":118},[100,51132,559],{"class":122},[100,51134,132],{"class":118},[14,51136,14742],{},[91,51138,51140],{"className":93,"code":51139,"language":95,"meta":96,"style":96},"0\n1\n2\n",[17,51141,51142,51146,51150],{"__ignoreMap":96},[100,51143,51144],{"class":102,"line":103},[100,51145,44572],{"class":122},[100,51147,51148],{"class":102,"line":135},[100,51149,228],{"class":122},[100,51151,51152],{"class":102,"line":142},[100,51153,233],{"class":122},[14,51155,51156,51158],{},[17,51157,32990],{}," does not build the full list in memory. It produces values as needed.",[77,51160,6244],{"id":6243},[675,51162,51164],{"id":51163},"use-iterators-or-generators-instead-of-large-lists","Use iterators or generators instead of large lists",[14,51166,51167],{},"If you only need to loop through values, avoid creating a full list.",[91,51169,51171],{"className":93,"code":51170,"language":95,"meta":96,"style":96},"for number in range(10_000_000):\n    if number > 5:\n        break\n",[17,51172,51173,51189,51201],{"__ignoreMap":96},[100,51174,51175,51177,51179,51181,51183,51185,51187],{"class":102,"line":103},[100,51176,71],{"class":145},[100,51178,614],{"class":106},[100,51180,617],{"class":145},[100,51182,29843],{"class":114},[100,51184,170],{"class":118},[100,51186,50753],{"class":122},[100,51188,522],{"class":118},[100,51190,51191,51193,51195,51197,51199],{"class":102,"line":135},[100,51192,2777],{"class":145},[100,51194,614],{"class":106},[100,51196,1918],{"class":110},[100,51198,31164],{"class":122},[100,51200,149],{"class":118},[100,51202,51203],{"class":102,"line":142},[100,51204,790],{"class":145},[14,51206,51207],{},"You can also use a generator expression:",[91,51209,51211],{"className":93,"code":51210,"language":95,"meta":96,"style":96},"squares = (n * n for n in range(1_000_000))\n\nfor value in squares:\n    print(value)\n    if value > 100:\n        break\n",[17,51212,51213,51244,51248,51261,51271,51284],{"__ignoreMap":96},[100,51214,51215,51218,51220,51222,51225,51227,51229,51231,51233,51235,51237,51239,51242],{"class":102,"line":103},[100,51216,51217],{"class":106},"squares ",[100,51219,111],{"class":110},[100,51221,16805],{"class":118},[100,51223,51224],{"class":106},"n ",[100,51226,3252],{"class":110},[100,51228,1418],{"class":106},[100,51230,71],{"class":145},[100,51232,1418],{"class":106},[100,51234,617],{"class":145},[100,51236,29843],{"class":114},[100,51238,170],{"class":118},[100,51240,51241],{"class":122},"1_000_000",[100,51243,182],{"class":118},[100,51245,51246],{"class":102,"line":135},[100,51247,139],{"emptyLinePlaceholder":138},[100,51249,51250,51252,51254,51256,51259],{"class":102,"line":142},[100,51251,71],{"class":145},[100,51253,989],{"class":106},[100,51255,617],{"class":145},[100,51257,51258],{"class":106}," squares",[100,51260,149],{"class":118},[100,51262,51263,51265,51267,51269],{"class":102,"line":152},[100,51264,200],{"class":114},[100,51266,170],{"class":118},[100,51268,757],{"class":178},[100,51270,215],{"class":118},[100,51272,51273,51275,51277,51279,51282],{"class":102,"line":164},[100,51274,2777],{"class":145},[100,51276,989],{"class":106},[100,51278,1918],{"class":110},[100,51280,51281],{"class":122}," 100",[100,51283,149],{"class":118},[100,51285,51286],{"class":102,"line":185},[100,51287,790],{"class":145},[14,51289,51290,51291,290],{},"This creates values one at a time instead of storing them all. For more on this idea, see ",[295,51292,1164],{"href":50807},[675,51294,51296],{"id":51295},"process-files-line-by-line-instead-of-reading-the-whole-file","Process files line by line instead of reading the whole file",[14,51298,51299],{},"This is a very common fix.",[14,51301,41523],{},[91,51303,51305],{"className":93,"code":51304,"language":95,"meta":96,"style":96},"with open(\"big_file.txt\", \"r\") as file:\n    data = file.read()\n",[17,51306,51307,51337],{"__ignoreMap":96},[100,51308,51309,51311,51313,51315,51317,51319,51321,51323,51325,51327,51329,51331,51333,51335],{"class":102,"line":103},[100,51310,17521],{"class":145},[100,51312,17524],{"class":114},[100,51314,170],{"class":118},[100,51316,206],{"class":205},[100,51318,50934],{"class":209},[100,51320,206],{"class":205},[100,51322,126],{"class":118},[100,51324,1708],{"class":205},[100,51326,17540],{"class":209},[100,51328,206],{"class":205},[100,51330,6155],{"class":118},[100,51332,16224],{"class":145},[100,51334,17550],{"class":17549},[100,51336,149],{"class":118},[100,51338,51339,51341,51343,51345,51347,51349],{"class":102,"line":135},[100,51340,17557],{"class":106},[100,51342,111],{"class":110},[100,51344,17550],{"class":17549},[100,51346,290],{"class":118},[100,51348,17566],{"class":178},[100,51350,4734],{"class":118},[14,51352,51353],{},"Use this:",[91,51355,51357],{"className":93,"code":51356,"language":95,"meta":96,"style":96},"with open(\"big_file.txt\", \"r\") as file:\n    for line in file:\n        print(line.strip())\n",[17,51358,51359,51389,51402],{"__ignoreMap":96},[100,51360,51361,51363,51365,51367,51369,51371,51373,51375,51377,51379,51381,51383,51385,51387],{"class":102,"line":103},[100,51362,17521],{"class":145},[100,51364,17524],{"class":114},[100,51366,170],{"class":118},[100,51368,206],{"class":205},[100,51370,50934],{"class":209},[100,51372,206],{"class":205},[100,51374,126],{"class":118},[100,51376,1708],{"class":205},[100,51378,17540],{"class":209},[100,51380,206],{"class":205},[100,51382,6155],{"class":118},[100,51384,16224],{"class":145},[100,51386,17550],{"class":17549},[100,51388,149],{"class":118},[100,51390,51391,51394,51396,51398,51400],{"class":102,"line":135},[100,51392,51393],{"class":145},"    for",[100,51395,1964],{"class":106},[100,51397,617],{"class":145},[100,51399,17550],{"class":17549},[100,51401,149],{"class":118},[100,51403,51404,51406,51408,51410,51412,51414],{"class":102,"line":142},[100,51405,167],{"class":114},[100,51407,170],{"class":118},[100,51409,102],{"class":178},[100,51411,290],{"class":118},[100,51413,6235],{"class":178},[100,51415,3370],{"class":118},[14,51417,51418,51419,3266,51423,290],{},"This reads one line at a time, which uses much less memory. See ",[295,51420,51422],{"href":51421},"\u002Fhow-to\u002Fhow-to-read-a-file-line-by-line-in-python\u002F","how to read a file line by line in Python",[295,51424,22657],{"href":22656},[675,51426,51428],{"id":51427},"break-work-into-smaller-chunks","Break work into smaller chunks",[14,51430,51431],{},"If your input is large, process part of it, then continue.",[91,51433,51435],{"className":93,"code":51434,"language":95,"meta":96,"style":96},"numbers = range(1_000_000)\nchunk_size = 100_000\n\nfor start in range(0, 1_000_000, chunk_size):\n    end = start + chunk_size\n    chunk = range(start, end)\n    print(f\"Processing {start} to {end - 1}\")\n",[17,51436,51437,51451,51461,51465,51492,51506,51527],{"__ignoreMap":96},[100,51438,51439,51441,51443,51445,51447,51449],{"class":102,"line":103},[100,51440,107],{"class":106},[100,51442,111],{"class":110},[100,51444,29843],{"class":114},[100,51446,170],{"class":118},[100,51448,51241],{"class":122},[100,51450,215],{"class":118},[100,51452,51453,51456,51458],{"class":102,"line":135},[100,51454,51455],{"class":106},"chunk_size ",[100,51457,111],{"class":110},[100,51459,51460],{"class":122}," 100_000\n",[100,51462,51463],{"class":102,"line":142},[100,51464,139],{"emptyLinePlaceholder":138},[100,51466,51467,51469,51472,51474,51476,51478,51480,51482,51485,51487,51490],{"class":102,"line":152},[100,51468,71],{"class":145},[100,51470,51471],{"class":106}," start ",[100,51473,617],{"class":145},[100,51475,29843],{"class":114},[100,51477,170],{"class":118},[100,51479,2279],{"class":122},[100,51481,126],{"class":118},[100,51483,51484],{"class":122}," 1_000_000",[100,51486,126],{"class":118},[100,51488,51489],{"class":178}," chunk_size",[100,51491,522],{"class":118},[100,51493,51494,51497,51499,51501,51503],{"class":102,"line":164},[100,51495,51496],{"class":106},"    end ",[100,51498,111],{"class":110},[100,51500,51471],{"class":106},[100,51502,6547],{"class":110},[100,51504,51505],{"class":106}," chunk_size\n",[100,51507,51508,51511,51513,51515,51517,51520,51522,51525],{"class":102,"line":185},[100,51509,51510],{"class":106},"    chunk ",[100,51512,111],{"class":110},[100,51514,29843],{"class":114},[100,51516,170],{"class":118},[100,51518,51519],{"class":178},"start",[100,51521,126],{"class":118},[100,51523,51524],{"class":178}," end",[100,51526,215],{"class":118},[100,51528,51529,51531,51533,51535,51538,51540,51542,51544,51546,51548,51551,51553,51556,51558],{"class":102,"line":197},[100,51530,200],{"class":114},[100,51532,170],{"class":118},[100,51534,18199],{"class":1077},[100,51536,51537],{"class":209},"\"Processing ",[100,51539,16490],{"class":122},[100,51541,51519],{"class":178},[100,51543,18216],{"class":122},[100,51545,9932],{"class":209},[100,51547,16490],{"class":122},[100,51549,51550],{"class":178},"end ",[100,51552,2193],{"class":110},[100,51554,51555],{"class":122}," 1}",[100,51557,206],{"class":209},[100,51559,215],{"class":118},[14,51561,51562],{},"This approach is useful when working with large datasets.",[675,51564,51566],{"id":51565},"remove-data-you-no-longer-need","Remove data you no longer need",[14,51568,51569],{},"If a large object is finished, do not keep it around.",[91,51571,51573],{"className":93,"code":51572,"language":95,"meta":96,"style":96},"data = list(range(1_000_000))\nprint(len(data))\n\n# Finished using data\ndel data\n",[17,51574,51575,51593,51607,51611,51616],{"__ignoreMap":96},[100,51576,51577,51579,51581,51583,51585,51587,51589,51591],{"class":102,"line":103},[100,51578,7909],{"class":106},[100,51580,111],{"class":110},[100,51582,14704],{"class":191},[100,51584,170],{"class":118},[100,51586,50899],{"class":114},[100,51588,170],{"class":118},[100,51590,51241],{"class":122},[100,51592,182],{"class":118},[100,51594,51595,51597,51599,51601,51603,51605],{"class":102,"line":135},[100,51596,372],{"class":114},[100,51598,170],{"class":118},[100,51600,17779],{"class":114},[100,51602,170],{"class":118},[100,51604,7934],{"class":178},[100,51606,182],{"class":118},[100,51608,51609],{"class":102,"line":142},[100,51610,139],{"emptyLinePlaceholder":138},[100,51612,51613],{"class":102,"line":152},[100,51614,51615],{"class":414},"# Finished using data\n",[100,51617,51618,51620],{"class":102,"line":164},[100,51619,42699],{"class":145},[100,51621,51622],{"class":106}," data\n",[14,51624,51625],{},"Deleting variables can help in some cases, especially when large objects are no longer needed.",[675,51627,51629],{"id":51628},"use-more-memory-efficient-data-structures-when-possible","Use more memory-efficient data structures when possible",[14,51631,51632],{},"Sometimes the problem is not just the amount of data, but how you store it.",[14,51634,580],{},[40,51636,51637,51644,51647],{},[43,51638,241,51639,15957,51641,51643],{},[17,51640,32990],{},[17,51642,50861],{}," when possible",[43,51645,51646],{},"Avoid unnecessary copies",[43,51648,51649],{},"Avoid building large nested structures unless you really need them",[77,51651,3274],{"id":3273},[14,51653,43254,51654,51656],{},[17,51655,50726],{},", try these steps.",[675,51658,51660],{"id":51659},"_1-find-the-line-where-large-objects-are-created","1. Find the line where large objects are created",[14,51662,51663],{},"Look for lines that:",[40,51665,51666,51669,51672,51675],{},[43,51667,51668],{},"Build large lists",[43,51670,51671],{},"Read full files",[43,51673,51674],{},"Copy data",[43,51676,51677],{},"Append in loops",[675,51679,51681],{"id":51680},"_2-print-sizes-or-lengths-as-data-grows","2. Print sizes or lengths as data grows",[14,51683,51684],{},"These simple checks can help:",[91,51686,51688],{"className":93,"code":51687,"language":95,"meta":96,"style":96},"print(len(my_list))\nprint(type(data))\nprint(my_list[:5])\n\nimport sys\nprint(sys.getsizeof(my_list))\n",[17,51689,51690,51704,51718,51733,51737,51743],{"__ignoreMap":96},[100,51691,51692,51694,51696,51698,51700,51702],{"class":102,"line":103},[100,51693,372],{"class":114},[100,51695,170],{"class":118},[100,51697,17779],{"class":114},[100,51699,170],{"class":118},[100,51701,7441],{"class":178},[100,51703,182],{"class":118},[100,51705,51706,51708,51710,51712,51714,51716],{"class":102,"line":135},[100,51707,372],{"class":114},[100,51709,170],{"class":118},[100,51711,1250],{"class":191},[100,51713,170],{"class":118},[100,51715,7934],{"class":178},[100,51717,182],{"class":118},[100,51719,51720,51722,51724,51726,51729,51731],{"class":102,"line":142},[100,51721,372],{"class":114},[100,51723,170],{"class":118},[100,51725,7441],{"class":178},[100,51727,51728],{"class":118},"[:",[100,51730,3557],{"class":122},[100,51732,132],{"class":118},[100,51734,51735],{"class":102,"line":152},[100,51736,139],{"emptyLinePlaceholder":138},[100,51738,51739,51741],{"class":102,"line":164},[100,51740,9259],{"class":145},[100,51742,26131],{"class":106},[100,51744,51745,51747,51749,51751,51753,51756,51758,51760],{"class":102,"line":185},[100,51746,372],{"class":114},[100,51748,170],{"class":118},[100,51750,26140],{"class":178},[100,51752,290],{"class":118},[100,51754,51755],{"class":178},"getsizeof",[100,51757,170],{"class":118},[100,51759,7441],{"class":178},[100,51761,182],{"class":118},[14,51763,8679],{},[40,51765,51766,51770,51776,51782],{},[43,51767,51768,48462],{},[17,51769,37443],{},[43,51771,51772,51775],{},[17,51773,51774],{},"type(data)"," confirms what kind of object you are using",[43,51777,51778,51781],{},[17,51779,51780],{},"my_list[:5]"," shows the first few items without printing everything",[43,51783,51784,51787],{},[17,51785,51786],{},"sys.getsizeof(my_list)"," shows the size of the container object in bytes",[675,51789,51791],{"id":51790},"_3-check-for-loops-that-append-forever","3. Check for loops that append forever",[14,51793,51794],{},"A loop may be adding data without stopping.",[91,51796,51798],{"className":93,"code":51797,"language":95,"meta":96,"style":96},"items = []\ncount = 0\n\nwhile count \u003C 5:\n    items.append(count)\n    count += 1\n\nprint(items)\n",[17,51799,51800,51808,51816,51820,51832,51847,51855,51859],{"__ignoreMap":96},[100,51801,51802,51804,51806],{"class":102,"line":103},[100,51803,3095],{"class":106},[100,51805,111],{"class":110},[100,51807,33843],{"class":118},[100,51809,51810,51812,51814],{"class":102,"line":135},[100,51811,2176],{"class":106},[100,51813,111],{"class":110},[100,51815,2069],{"class":122},[100,51817,51818],{"class":102,"line":142},[100,51819,139],{"emptyLinePlaceholder":138},[100,51821,51822,51824,51826,51828,51830],{"class":102,"line":152},[100,51823,721],{"class":145},[100,51825,2190],{"class":106},[100,51827,2782],{"class":110},[100,51829,31164],{"class":122},[100,51831,149],{"class":118},[100,51833,51834,51836,51838,51840,51842,51845],{"class":102,"line":164},[100,51835,51001],{"class":106},[100,51837,290],{"class":118},[100,51839,4254],{"class":178},[100,51841,170],{"class":118},[100,51843,51844],{"class":178},"count",[100,51846,215],{"class":118},[100,51848,51849,51851,51853],{"class":102,"line":185},[100,51850,41602],{"class":106},[100,51852,41605],{"class":110},[100,51854,1093],{"class":122},[100,51856,51857],{"class":102,"line":197},[100,51858,139],{"emptyLinePlaceholder":138},[100,51860,51861,51863,51865,51867],{"class":102,"line":771},[100,51862,372],{"class":114},[100,51864,170],{"class":118},[100,51866,3146],{"class":178},[100,51868,215],{"class":118},[14,51870,14742],{},[91,51872,51874],{"className":93,"code":51873,"language":95,"meta":96,"style":96},"[0, 1, 2, 3, 4]\n",[17,51875,51876],{"__ignoreMap":96},[100,51877,51878,51880,51882,51884,51886,51888,51890,51892,51894,51896,51898],{"class":102,"line":103},[100,51879,3166],{"class":118},[100,51881,2279],{"class":122},[100,51883,126],{"class":118},[100,51885,6915],{"class":122},[100,51887,126],{"class":118},[100,51889,129],{"class":122},[100,51891,126],{"class":118},[100,51893,1405],{"class":122},[100,51895,126],{"class":118},[100,51897,2584],{"class":122},[100,51899,603],{"class":118},[14,51901,51902],{},"If you forget to update the loop condition, the list may keep growing.",[675,51904,51906],{"id":51905},"_4-test-with-a-much-smaller-input-first","4. Test with a much smaller input first",[14,51908,51909],{},"If this fails:",[91,51911,51913],{"className":93,"code":51912,"language":95,"meta":96,"style":96},"numbers = list(range(10**9))\n",[17,51914,51915],{"__ignoreMap":96},[100,51916,51917,51919,51921,51923,51925,51927,51929,51931,51933,51935],{"class":102,"line":103},[100,51918,107],{"class":106},[100,51920,111],{"class":110},[100,51922,14704],{"class":191},[100,51924,170],{"class":118},[100,51926,50899],{"class":114},[100,51928,170],{"class":118},[100,51930,356],{"class":122},[100,51932,50906],{"class":110},[100,51934,9281],{"class":122},[100,51936,182],{"class":118},[14,51938,51939],{},"Try a much smaller version:",[91,51941,51943],{"className":93,"code":51942,"language":95,"meta":96,"style":96},"numbers = list(range(1000))\nprint(len(numbers))\n",[17,51944,51945,51964],{"__ignoreMap":96},[100,51946,51947,51949,51951,51953,51955,51957,51959,51962],{"class":102,"line":103},[100,51948,107],{"class":106},[100,51950,111],{"class":110},[100,51952,14704],{"class":191},[100,51954,170],{"class":118},[100,51956,50899],{"class":114},[100,51958,170],{"class":118},[100,51960,51961],{"class":122},"1000",[100,51963,182],{"class":118},[100,51965,51966,51968,51970,51972,51974,51976],{"class":102,"line":135},[100,51967,372],{"class":114},[100,51969,170],{"class":118},[100,51971,17779],{"class":114},[100,51973,170],{"class":118},[100,51975,179],{"class":178},[100,51977,182],{"class":118},[14,51979,51980],{},"If the small version works, the problem is likely memory usage, not syntax.",[675,51982,51984],{"id":51983},"_5-replace-eager-loading-with-streaming-or-chunking","5. Replace eager loading with streaming or chunking",[14,51986,51987],{},"“Eager loading” means loading everything at once. A better approach is often to process data as it comes in.",[14,51989,51990],{},"This is especially useful when working with files, CSV data, or API results.",[77,51992,51994],{"id":51993},"when-the-problem-is-system-memory","When the problem is system memory",[14,51996,51997],{},"Sometimes the program is simply asking for more memory than the machine can provide.",[14,51999,52000],{},"A few things to know:",[40,52002,52003,52006,52009],{},[43,52004,52005],{},"Closing other programs may help",[43,52007,52008],{},"Very large datasets may require a different algorithm, not just more RAM",[43,52010,52011],{},"A 32-bit Python installation can hit memory limits sooner than 64-bit Python",[14,52013,52014],{},"So even if your code is correct, it may still fail if the approach needs too much memory.",[77,52016,1344],{"id":1343},[14,52018,52019,52020,89],{},"Here are the most common reasons beginners see ",[17,52021,50726],{},[40,52023,52024,52030,52036,52039,52042],{},[43,52025,52026,52027,52029],{},"Creating a huge list instead of using ",[17,52028,32990],{}," or a generator",[43,52031,1357,52032,52035],{},[17,52033,52034],{},"file.read()"," on a very large file",[43,52037,52038],{},"Keeping unnecessary copies of data",[43,52040,52041],{},"Appending inside an infinite or very long loop",[43,52043,52044],{},"Loading a large JSON or CSV file fully into memory",[14,52046,52047],{},"A good habit is to ask:",[40,52049,52050,52053,52056],{},[43,52051,52052],{},"Do I need all this data at once?",[43,52054,52055],{},"Can I process one item at a time?",[43,52057,52058],{},"Can I stop storing old data?",[77,52060,1514],{"id":1513},[675,52062,52064],{"id":52063},"is-memoryerror-the-same-as-my-computer-running-out-of-memory","Is MemoryError the same as my computer running out of memory?",[14,52066,52067],{},"Usually yes, or Python reached the memory limit available to the process.",[675,52069,52071],{"id":52070},"can-i-fix-memoryerror-by-using-range-instead-of-listrange","Can I fix MemoryError by using range() instead of list(range())?",[14,52073,52074,52075,52077],{},"Often yes, because ",[17,52076,32990],{}," does not store all numbers in memory at once.",[675,52079,52081],{"id":52080},"why-does-reading-a-file-cause-memoryerror","Why does reading a file cause MemoryError?",[14,52083,52084],{},"If you read the whole file at once, Python may try to store too much data in RAM.",[675,52086,52088],{"id":52087},"will-deleting-variables-help","Will deleting variables help?",[14,52090,52091],{},"Sometimes. It can help if large objects are no longer needed, but better program design is usually the real fix.",[77,52093,1554],{"id":1553},[40,52095,52096,52100,52105,52109,52115],{},[43,52097,52098],{},[295,52099,1566],{"href":50807},[43,52101,52102],{},[295,52103,52104],{"href":51421},"How to read a file line by line in Python",[43,52106,52107],{},[295,52108,22657],{"href":22656},[43,52110,52111],{},[295,52112,52114],{"href":52113},"\u002Ferrors\u002Foverflowerror-numerical-result-out-of-range-fix\u002F","OverflowError: numerical result out of range",[43,52116,52117],{},[295,52118,52120],{"href":52119},"\u002Ferrors\u002Fruntimeerror-in-python-causes-and-fixes\u002F","RuntimeError in Python: causes and fixes",[14,52122,52123,52124,52126],{},"Learning to fix ",[17,52125,50726],{}," is a good step toward writing more memory-efficient Python code.",[1589,52128,52129],{},"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 .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 .srdBf, html code.shiki .srdBf{--shiki-light:#F76D47;--shiki-default:#005CC5;--shiki-dark:#79B8FF}html pre.shiki code .sutJx, html code.shiki .sutJx{--shiki-light:#90A4AE;--shiki-light-font-style:italic;--shiki-default:#6A737D;--shiki-default-font-style:inherit;--shiki-dark:#6A737D;--shiki-dark-font-style:inherit}html pre.shiki code .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 .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 .sMMDD, html code.shiki .sMMDD{--shiki-light:#90A4AE;--shiki-default:#E36209;--shiki-dark:#FFAB70}html pre.shiki code .slqww, html code.shiki .slqww{--shiki-light:#6182B8;--shiki-default:#24292E;--shiki-dark:#E1E4E8}html pre.shiki code .s39Yj, html code.shiki .s39Yj{--shiki-light:#39ADB5;--shiki-default:#005CC5;--shiki-dark:#79B8FF}html pre.shiki code .sbsja, html code.shiki .sbsja{--shiki-light:#9C3EDA;--shiki-default:#D73A49;--shiki-dark:#F97583}",{"title":96,"searchDepth":135,"depth":135,"links":52131},[52132,52133,52134,52135,52136,52143,52150,52151,52152,52158],{"id":79,"depth":135,"text":80},{"id":50810,"depth":135,"text":50811},{"id":29974,"depth":135,"text":29975},{"id":51021,"depth":135,"text":51022},{"id":6243,"depth":135,"text":6244,"children":52137},[52138,52139,52140,52141,52142],{"id":51163,"depth":142,"text":51164},{"id":51295,"depth":142,"text":51296},{"id":51427,"depth":142,"text":51428},{"id":51565,"depth":142,"text":51566},{"id":51628,"depth":142,"text":51629},{"id":3273,"depth":135,"text":3274,"children":52144},[52145,52146,52147,52148,52149],{"id":51659,"depth":142,"text":51660},{"id":51680,"depth":142,"text":51681},{"id":51790,"depth":142,"text":51791},{"id":51905,"depth":142,"text":51906},{"id":51983,"depth":142,"text":51984},{"id":51993,"depth":135,"text":51994},{"id":1343,"depth":135,"text":1344},{"id":1513,"depth":135,"text":1514,"children":52153},[52154,52155,52156,52157],{"id":52063,"depth":142,"text":52064},{"id":52070,"depth":142,"text":52071},{"id":52080,"depth":142,"text":52081},{"id":52087,"depth":142,"text":52088},{"id":1553,"depth":135,"text":1554},"Master memoryerror in python causes and fixes in our comprehensive Python beginner guide.",{},"\u002Ferrors\u002Fmemoryerror-in-python-causes-and-fixes",{"title":50716,"description":52159},"errors\u002Fmemoryerror-in-python-causes-and-fixes","7SkL0-JAUnj0IyhSudFZ9ydNZAmkPoeHut9zueIXTTc",{"id":52166,"title":28634,"body":52167,"description":53206,"extension":1623,"meta":53207,"navigation":138,"path":53208,"seo":53209,"stem":53210,"__hash__":53211},"content\u002Ferrors\u002Fmodulenotfounderror-no-module-named-x-fix.md",{"type":7,"value":52168,"toc":53174},[52169,52172,52178,52180,52193,52196,52198,52214,52220,52223,52225,52228,52238,52241,52244,52254,52259,52262,52273,52276,52280,52282,52306,52310,52313,52317,52320,52326,52332,52336,52345,52361,52364,52380,52385,52389,52392,52408,52411,52414,52418,52421,52426,52430,52433,52435,52453,52457,52460,52465,52475,52478,52482,52485,52488,52492,52495,52498,52519,52522,52528,52531,52543,52552,52556,52559,52562,52565,52576,52578,52594,52597,52600,52609,52612,52625,52628,52644,52647,52651,52654,52657,52659,52674,52676,52694,52697,52715,52717,52733,52735,52747,52750,52755,52759,52762,52764,52769,52794,52800,52802,52820,52825,52829,52832,52887,52890,52922,52929,52933,52936,52939,52945,52949,52981,52987,53011,53013,53018,53021,53038,53043,53045,53048,53085,53087,53091,53097,53099,53107,53111,53116,53121,53125,53128,53132,53135,53137,53171],[10,52170,28634],{"id":52171},"modulenotfounderror-no-module-named-x-fix",[14,52173,52174,52177],{},[17,52175,52176],{},"ModuleNotFoundError: No module named 'X'"," means Python tried to import a module but could not find it.",[14,52179,24806],{},[40,52181,52182,52184,52187,52190],{},[43,52183,17444],{},[43,52185,52186],{},"it is installed in the wrong Python environment",[43,52188,52189],{},"the import name is wrong",[43,52191,52192],{},"a local file is hiding the real package",[14,52194,52195],{},"This guide helps you find the cause quickly and fix it step by step.",[77,52197,80],{"id":79},[91,52199,52200],{"className":10352,"code":27593,"language":10354,"meta":96,"style":96},[17,52201,52202],{"__ignoreMap":96},[100,52203,52204,52206,52208,52210,52212],{"class":102,"line":103},[100,52205,95],{"class":10361},[100,52207,26622],{"class":10364},[100,52209,26625],{"class":209},[100,52211,26657],{"class":209},[100,52213,27608],{"class":209},[14,52215,52216,52217,52219],{},"Replace ",[17,52218,17450],{}," with the missing module name.",[14,52221,52222],{},"If the package is already installed, check that your terminal, IDE, and virtual environment are all using the same Python interpreter.",[77,52224,5881],{"id":5880},[14,52226,52227],{},"When Python runs an import like this:",[91,52229,52230],{"className":93,"code":27674,"language":95,"meta":96,"style":96},[17,52231,52232],{"__ignoreMap":96},[100,52233,52234,52236],{"class":102,"line":103},[100,52235,9259],{"class":145},[100,52237,27608],{"class":106},[14,52239,52240],{},"it searches for that module in places listed in Python’s import path.",[14,52242,52243],{},"If Python cannot find it, you get an error like:",[91,52245,52246],{"className":93,"code":27674,"language":95,"meta":96,"style":96},[17,52247,52248],{"__ignoreMap":96},[100,52249,52250,52252],{"class":102,"line":103},[100,52251,9259],{"class":145},[100,52253,27608],{"class":106},[91,52255,52257],{"className":52256,"code":28618,"language":4468,"meta":96},[26775],[17,52258,28618],{"__ignoreMap":96},[14,52260,52261],{},"In simple terms, Python is saying:",[40,52263,52264,52267,52270],{},[43,52265,52266],{},"“I looked for this module”",[43,52268,52269],{},"“I could not find it”",[43,52271,52272],{},"“The name I could not find is the important clue”",[14,52274,52275],{},"Start by reading the missing module name exactly as shown in the error.",[77,52277,52279],{"id":52278},"common-reasons-it-happens","Common reasons it happens",[14,52281,29621],{},[40,52283,52284,52286,52289,52292,52294,52297,52300,52303],{},[43,52285,27551],{},[43,52287,52288],{},"The package is installed in a different Python version",[43,52290,52291],{},"A virtual environment is not activated",[43,52293,27557],{},[43,52295,52296],{},"The package name and import name are different",[43,52298,52299],{},"Your IDE is using a different interpreter",[43,52301,52302],{},"A local file name is conflicting with the real package",[43,52304,52305],{},"Your project structure is wrong for local modules",[77,52307,52309],{"id":52308},"how-to-fix-it-step-by-step","How to fix it step by step",[14,52311,52312],{},"Use this checklist in order.",[675,52314,52316],{"id":52315},"_1-read-the-missing-module-name-carefully","1. Read the missing module name carefully",[14,52318,52319],{},"If the error says:",[91,52321,52324],{"className":52322,"code":52323,"language":4468,"meta":96},[26775],"ModuleNotFoundError: No module named 'bs4'\n",[17,52325,52323],{"__ignoreMap":96},[14,52327,52328,52329,52331],{},"then the missing import name is ",[17,52330,27846],{},", not necessarily the package name you install.",[675,52333,52335],{"id":52334},"_2-install-the-package","2. Install the package",[14,52337,241,52338,52341,52342,52344],{},[17,52339,52340],{},"python -m pip install ..."," so ",[17,52343,25872],{}," runs with the same Python interpreter:",[91,52346,52347],{"className":10352,"code":27593,"language":10354,"meta":96,"style":96},[17,52348,52349],{"__ignoreMap":96},[100,52350,52351,52353,52355,52357,52359],{"class":102,"line":103},[100,52352,95],{"class":10361},[100,52354,26622],{"class":10364},[100,52356,26625],{"class":209},[100,52358,26657],{"class":209},[100,52360,27608],{"class":209},[14,52362,52363],{},"On some systems, you may need:",[91,52365,52366],{"className":10352,"code":27618,"language":10354,"meta":96,"style":96},[17,52367,52368],{"__ignoreMap":96},[100,52369,52370,52372,52374,52376,52378],{"class":102,"line":103},[100,52371,27614],{"class":10361},[100,52373,26622],{"class":10364},[100,52375,26625],{"class":209},[100,52377,26657],{"class":209},[100,52379,27608],{"class":209},[14,52381,52382,52383,290],{},"If you are not sure how installation works, see ",[295,52384,27823],{"href":27822},[675,52386,52388],{"id":52387},"_3-check-which-python-is-running-your-script","3. Check which Python is running your script",[14,52390,52391],{},"Run:",[91,52393,52394],{"className":10352,"code":28192,"language":10354,"meta":96,"style":96},[17,52395,52396],{"__ignoreMap":96},[100,52397,52398,52400,52402,52404,52406],{"class":102,"line":103},[100,52399,95],{"class":10361},[100,52401,10450],{"class":10364},[100,52403,1708],{"class":205},[100,52405,28205],{"class":209},[100,52407,1714],{"class":205},[14,52409,52410],{},"This shows the exact Python executable being used.",[14,52412,52413],{},"If your package was installed somewhere else, Python will still say the module is missing.",[675,52415,52417],{"id":52416},"_4-check-whether-a-virtual-environment-should-be-active","4. Check whether a virtual environment should be active",[14,52419,52420],{},"If your project uses a virtual environment, activate it before installing packages or running code.",[14,52422,52423,52424,290],{},"If you need help with that, see ",[295,52425,26680],{"href":26679},[675,52427,52429],{"id":52428},"_5-make-sure-the-import-name-is-correct","5. Make sure the import name is correct",[14,52431,52432],{},"A package can have one install name and a different import name.",[14,52434,580],{},[40,52436,52437,52447],{},[43,52438,52439,52440,52443,52444],{},"install ",[17,52441,52442],{},"pillow"," but import ",[17,52445,52446],{},"PIL",[43,52448,52439,52449,52443,52451],{},[17,52450,27840],{},[17,52452,27846],{},[675,52454,52456],{"id":52455},"_6-look-for-local-file-name-conflicts","6. Look for local file name conflicts",[14,52458,52459],{},"A file in your project can hide the real package.",[14,52461,26690,52462,52464],{},[17,52463,28100],{},", this code can break:",[91,52466,52467],{"className":93,"code":27674,"language":95,"meta":96,"style":96},[17,52468,52469],{"__ignoreMap":96},[100,52470,52471,52473],{"class":102,"line":103},[100,52472,9259],{"class":145},[100,52474,27608],{"class":106},[14,52476,52477],{},"Rename the file and try again.",[675,52479,52481],{"id":52480},"_7-restart-your-terminal-or-ide","7. Restart your terminal or IDE",[14,52483,52484],{},"After installing a package, your editor or terminal session may still be using old settings.",[14,52486,52487],{},"Restart it and run the code again.",[77,52489,52491],{"id":52490},"check-your-python-and-pip-paths","Check your Python and pip paths",[14,52493,52494],{},"A very common problem is installing a package with one Python but running code with another.",[14,52496,52497],{},"Run these commands:",[91,52499,52501],{"className":10352,"code":52500,"language":10354,"meta":96,"style":96},"python --version\npython -m pip --version\n",[17,52502,52503,52509],{"__ignoreMap":96},[100,52504,52505,52507],{"class":102,"line":103},[100,52506,95],{"class":10361},[100,52508,10365],{"class":10364},[100,52510,52511,52513,52515,52517],{"class":102,"line":135},[100,52512,95],{"class":10361},[100,52514,26622],{"class":10364},[100,52516,26625],{"class":209},[100,52518,10365],{"class":10364},[14,52520,52521],{},"Example output:",[91,52523,52526],{"className":52524,"code":52525,"language":4468,"meta":96},[26775],"Python 3.11.6\npip 23.2.1 from \u002Fpath\u002Fto\u002Fpython3.11\u002Fsite-packages\u002Fpip (python 3.11)\n",[17,52527,52525],{"__ignoreMap":96},[14,52529,52530],{},"This helps you confirm that:",[40,52532,52533,52538],{},[43,52534,52535,52537],{},[17,52536,95],{}," points to the version you expect",[43,52539,52540,52542],{},[17,52541,25872],{}," belongs to that same Python",[14,52544,1357,52545,52548,52549,52551],{},[17,52546,52547],{},"python -m pip"," is safer than plain ",[17,52550,25872],{}," because it reduces the chance of installing into the wrong interpreter.",[77,52553,52555],{"id":52554},"virtual-environment-issues","Virtual environment issues",[14,52557,52558],{},"A virtual environment is an isolated Python environment for one project.",[14,52560,52561],{},"If a package is installed inside a virtual environment, it is usually only available when that environment is active.",[14,52563,52564],{},"Common problems:",[40,52566,52567,52570,52573],{},[43,52568,52569],{},"You installed the package in a virtual environment, but the environment is not active",[43,52571,52572],{},"Your terminal uses one environment, but your IDE uses another",[43,52574,52575],{},"You created the environment but installed packages globally by mistake",[14,52577,27956],{},[91,52579,52581],{"className":10352,"code":52580,"language":10354,"meta":96,"style":96},"python -m venv venv\n",[17,52582,52583],{"__ignoreMap":96},[100,52584,52585,52587,52589,52591],{"class":102,"line":103},[100,52586,95],{"class":10361},[100,52588,26622],{"class":10364},[100,52590,27970],{"class":209},[100,52592,52593],{"class":209}," venv\n",[14,52595,52596],{},"Activate it.",[14,52598,52599],{},"On Windows:",[91,52601,52603],{"className":10352,"code":52602,"language":10354,"meta":96,"style":96},"venv\\Scripts\\activate\n",[17,52604,52605],{"__ignoreMap":96},[100,52606,52607],{"class":102,"line":103},[100,52608,52602],{"class":10361},[14,52610,52611],{},"On macOS or Linux:",[91,52613,52615],{"className":10352,"code":52614,"language":10354,"meta":96,"style":96},"source venv\u002Fbin\u002Factivate\n",[17,52616,52617],{"__ignoreMap":96},[100,52618,52619,52622],{"class":102,"line":103},[100,52620,52621],{"class":114},"source",[100,52623,52624],{"class":209}," venv\u002Fbin\u002Factivate\n",[14,52626,52627],{},"Then install the package:",[91,52629,52630],{"className":10352,"code":27593,"language":10354,"meta":96,"style":96},[17,52631,52632],{"__ignoreMap":96},[100,52633,52634,52636,52638,52640,52642],{"class":102,"line":103},[100,52635,95],{"class":10361},[100,52637,26622],{"class":10364},[100,52639,26625],{"class":209},[100,52641,26657],{"class":209},[100,52643,27608],{"class":209},[14,52645,52646],{},"If imports still fail, your editor may be using the wrong interpreter.",[77,52648,52650],{"id":52649},"import-name-vs-package-name","Import name vs package name",[14,52652,52653],{},"This causes many beginner mistakes.",[14,52655,52656],{},"The name you install is not always the name you import.",[14,52658,5500],{},[40,52660,52661,52668],{},[43,52662,52439,52663,52665,52666],{},[17,52664,52442],{}," → import ",[17,52667,52446],{},[43,52669,52439,52670,52665,52672],{},[17,52671,27840],{},[17,52673,27846],{},[14,52675,1844],{},[91,52677,52679],{"className":10352,"code":52678,"language":10354,"meta":96,"style":96},"python -m pip install pillow\n",[17,52680,52681],{"__ignoreMap":96},[100,52682,52683,52685,52687,52689,52691],{"class":102,"line":103},[100,52684,95],{"class":10361},[100,52686,26622],{"class":10364},[100,52688,26625],{"class":209},[100,52690,26657],{"class":209},[100,52692,52693],{"class":209}," pillow\n",[14,52695,52696],{},"Then in Python:",[91,52698,52700],{"className":93,"code":52699,"language":95,"meta":96,"style":96},"from PIL import Image\n",[17,52701,52702],{"__ignoreMap":96},[100,52703,52704,52706,52709,52712],{"class":102,"line":103},[100,52705,10201],{"class":145},[100,52707,52708],{"class":527}," PIL",[100,52710,52711],{"class":145}," import",[100,52713,52714],{"class":106}," Image\n",[14,52716,12066],{},[91,52718,52719],{"className":10352,"code":27871,"language":10354,"meta":96,"style":96},[17,52720,52721],{"__ignoreMap":96},[100,52722,52723,52725,52727,52729,52731],{"class":102,"line":103},[100,52724,95],{"class":10361},[100,52726,26622],{"class":10364},[100,52728,26625],{"class":209},[100,52730,26657],{"class":209},[100,52732,27886],{"class":209},[14,52734,52696],{},[91,52736,52738],{"className":93,"code":52737,"language":95,"meta":96,"style":96},"import bs4\n",[17,52739,52740],{"__ignoreMap":96},[100,52741,52742,52744],{"class":102,"line":103},[100,52743,9259],{"class":145},[100,52745,52746],{"class":106}," bs4\n",[14,52748,52749],{},"If the install name and import name do not match, check the package documentation.",[14,52751,52752,52753,290],{},"If you want to understand imports better, see ",[295,52754,10137],{"href":25158},[77,52756,52758],{"id":52757},"local-file-name-conflicts","Local file name conflicts",[14,52760,52761],{},"Python checks your project files before some installed packages. Because of that, a local file or folder can shadow a real package.",[675,52763,30294],{"id":30293},[14,52765,52766,52767,89],{},"Suppose your project contains a file named ",[17,52768,28100],{},[91,52770,52772],{"className":93,"code":52771,"language":95,"meta":96,"style":96},"import requests\nprint(\"Hello\")\n",[17,52773,52774,52780],{"__ignoreMap":96},[100,52775,52776,52778],{"class":102,"line":103},[100,52777,9259],{"class":145},[100,52779,27608],{"class":106},[100,52781,52782,52784,52786,52788,52790,52792],{"class":102,"line":135},[100,52783,372],{"class":114},[100,52785,170],{"class":118},[100,52787,206],{"class":205},[100,52789,7683],{"class":209},[100,52791,206],{"class":205},[100,52793,215],{"class":118},[14,52795,52796,52797,52799],{},"This can confuse Python because it may try to import your local file instead of the real ",[17,52798,17450],{}," package.",[675,52801,6244],{"id":6243},[40,52803,52804,52810,52815],{},[43,52805,52806,52807],{},"Rename the file to something else, like ",[17,52808,52809],{},"api_test.py",[43,52811,52812,52813],{},"Also check for folders named ",[17,52814,17450],{},[43,52816,52817,52818],{},"Delete generated cache files if needed, such as ",[17,52819,10130],{},[14,52821,52822,52823,290],{},"This kind of conflict can also lead to related errors like ",[295,52824,9246],{"href":5541},[77,52826,52828],{"id":52827},"debugging-commands-to-try","Debugging commands to try",[14,52830,52831],{},"These commands help you see what Python is using.",[91,52833,52835],{"className":10352,"code":52834,"language":10354,"meta":96,"style":96},"python --version\npython -m pip --version\npython -m pip list\npython -c \"import sys; print(sys.executable)\"\npython -c \"import sys; print(sys.path)\"\n",[17,52836,52837,52843,52853,52863,52875],{"__ignoreMap":96},[100,52838,52839,52841],{"class":102,"line":103},[100,52840,95],{"class":10361},[100,52842,10365],{"class":10364},[100,52844,52845,52847,52849,52851],{"class":102,"line":135},[100,52846,95],{"class":10361},[100,52848,26622],{"class":10364},[100,52850,26625],{"class":209},[100,52852,10365],{"class":10364},[100,52854,52855,52857,52859,52861],{"class":102,"line":142},[100,52856,95],{"class":10361},[100,52858,26622],{"class":10364},[100,52860,26625],{"class":209},[100,52862,26638],{"class":209},[100,52864,52865,52867,52869,52871,52873],{"class":102,"line":152},[100,52866,95],{"class":10361},[100,52868,10450],{"class":10364},[100,52870,1708],{"class":205},[100,52872,28205],{"class":209},[100,52874,1714],{"class":205},[100,52876,52877,52879,52881,52883,52885],{"class":102,"line":164},[100,52878,95],{"class":10361},[100,52880,10450],{"class":10364},[100,52882,1708],{"class":205},[100,52884,27053],{"class":209},[100,52886,1714],{"class":205},[14,52888,52889],{},"What they tell you:",[40,52891,52892,52897,52906,52912,52918],{},[43,52893,52894,52896],{},[17,52895,25906],{}," shows the Python version",[43,52898,52899,52902,52903,52905],{},[17,52900,52901],{},"python -m pip --version"," shows which Python ",[17,52904,25872],{}," belongs to",[43,52907,52908,52911],{},[17,52909,52910],{},"python -m pip list"," shows installed packages",[43,52913,52914,52917],{},[17,52915,52916],{},"sys.executable"," shows the exact Python executable",[43,52919,52920,26211],{},[17,52921,26210],{},[14,52923,52924,52925,28239,52927,290],{},"If you want to learn more about the search path, see ",[295,52926,26850],{"href":26849},[295,52928,27062],{"href":28242},[77,52930,52932],{"id":52931},"when-the-module-is-your-own-file","When the module is your own file",[14,52934,52935],{},"Sometimes the missing module is not a package from the internet. It is your own Python file.",[14,52937,52938],{},"Example project:",[91,52940,52943],{"className":52941,"code":52942,"language":4468,"meta":96},[26775],"project\u002F\n    main.py\n    helper.py\n",[17,52944,52942],{"__ignoreMap":96},[14,52946,11734,52947,26783],{},[17,52948,21078],{},[91,52950,52952],{"className":93,"code":52951,"language":95,"meta":96,"style":96},"import helper\n\nprint(helper.say_hello())\n",[17,52953,52954,52961,52965],{"__ignoreMap":96},[100,52955,52956,52958],{"class":102,"line":103},[100,52957,9259],{"class":145},[100,52959,52960],{"class":106}," helper\n",[100,52962,52963],{"class":102,"line":135},[100,52964,139],{"emptyLinePlaceholder":138},[100,52966,52967,52969,52971,52974,52976,52979],{"class":102,"line":142},[100,52968,372],{"class":114},[100,52970,170],{"class":118},[100,52972,52973],{"class":178},"helper",[100,52975,290],{"class":118},[100,52977,52978],{"class":178},"say_hello",[100,52980,3370],{"class":118},[14,52982,2318,52983,52986],{},[17,52984,52985],{},"helper.py"," must exist and contain the function:",[91,52988,52990],{"className":93,"code":52989,"language":95,"meta":96,"style":96},"def say_hello():\n    return \"Hello\"\n",[17,52991,52992,53001],{"__ignoreMap":96},[100,52993,52994,52996,52999],{"class":102,"line":103},[100,52995,1078],{"class":1077},[100,52997,52998],{"class":1081}," say_hello",[100,53000,1085],{"class":118},[100,53002,53003,53005,53007,53009],{"class":102,"line":135},[100,53004,2552],{"class":145},[100,53006,1708],{"class":205},[100,53008,7683],{"class":209},[100,53010,1714],{"class":205},[14,53012,14742],{},[91,53014,53016],{"className":53015,"code":29173,"language":4468,"meta":96},[26775],[17,53017,29173],{"__ignoreMap":96},[14,53019,53020],{},"Check these points:",[40,53022,53023,53026,53029,53032,53035],{},[43,53024,53025],{},"The file is in the correct folder",[43,53027,53028],{},"The spelling matches exactly",[43,53030,53031],{},"The letter case matches exactly",[43,53033,53034],{},"The filename does not contain spaces or invalid characters",[43,53036,53037],{},"If importing from a folder, the project structure is correct",[14,53039,53040,53041,290],{},"If Python finds the module but not the thing inside it, that is a different problem. See ",[295,53042,9965],{"href":26420},[77,53044,30537],{"id":30536},[14,53046,53047],{},"Some import errors look similar but mean different things.",[40,53049,53050,53057,53066,53077],{},[43,53051,53052,53054,53056],{},[17,53053,52176],{},[9927,53055],{},"\nPython could not find the module at all.",[43,53058,53059,53061,53063,53064,290],{},[17,53060,24802],{},[9927,53062],{},"\nPython found the module, but not the specific name inside it. See ",[295,53065,9965],{"href":26420},[43,53067,53068,53070,53072,53073,290],{},[17,53069,9961],{},[9927,53071],{},"\nThis is an older style message for similar import problems. See ",[295,53074,53076],{"href":53075},"\u002Ferrors\u002Fimporterror-in-python-causes-and-fixes\u002F","ImportError in Python: causes and fixes",[43,53078,53079,53082,53084],{},[17,53080,53081],{},"AttributeError: module has no attribute ...",[9927,53083],{},"\nThis can happen after a wrong import or a naming conflict.",[77,53086,1514],{"id":1513},[675,53088,53090],{"id":53089},"why-does-pip-say-the-package-is-installed-but-python-still-cannot-import-it","Why does pip say the package is installed but Python still cannot import it?",[14,53092,53093,53094,53096],{},"This usually means ",[17,53095,25872],{}," installed the package into a different Python interpreter or virtual environment than the one running your code.",[675,53098,28719],{"id":28718},[14,53100,241,53101,53103,53104,53106],{},[17,53102,28724],{}," because it makes sure ",[17,53105,25872],{}," runs for the same Python interpreter.",[675,53108,53110],{"id":53109},"what-is-the-difference-between-modulenotfounderror-and-importerror","What is the difference between ModuleNotFoundError and ImportError?",[14,53112,53113,53115],{},[17,53114,17397],{}," means Python could not find the module at all.",[14,53117,53118,53120],{},[17,53119,17392],{}," is broader. It can also mean Python found the module but failed during the import.",[675,53122,53124],{"id":53123},"why-does-this-happen-in-vs-code-or-pycharm-only","Why does this happen in VS Code or PyCharm only?",[14,53126,53127],{},"Your editor may be using a different interpreter or virtual environment from your terminal.",[675,53129,53131],{"id":53130},"can-a-file-in-my-project-cause-this-error","Can a file in my project cause this error?",[14,53133,53134],{},"Yes. A local file or folder with the same name as a package can block the real import.",[77,53136,1554],{"id":1553},[40,53138,53139,53143,53147,53151,53155,53159,53163,53167],{},[43,53140,53141],{},[295,53142,28741],{"href":27822},[43,53144,53145],{},[295,53146,27476],{"href":26679},[43,53148,53149],{},[295,53150,10666],{"href":25158},[43,53152,53153],{},[295,53154,9965],{"href":26420},[43,53156,53157],{},[295,53158,53076],{"href":53075},[43,53160,53161],{},[295,53162,9246],{"href":5541},[43,53164,53165],{},[295,53166,27062],{"href":28242},[43,53168,53169],{},[295,53170,26850],{"href":26849},[1589,53172,53173],{},"html pre.shiki code .sbgvK, html code.shiki .sbgvK{--shiki-light:#E2931D;--shiki-default:#6F42C1;--shiki-dark:#B392F0}html pre.shiki code .stzsN, html code.shiki .stzsN{--shiki-light:#91B859;--shiki-default:#005CC5;--shiki-dark:#79B8FF}html pre.shiki code .s_sjI, html code.shiki .s_sjI{--shiki-light:#91B859;--shiki-default:#032F62;--shiki-dark:#9ECBFF}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 .sVHd0, html code.shiki .sVHd0{--shiki-light:#39ADB5;--shiki-light-font-style:italic;--shiki-default:#D73A49;--shiki-default-font-style:inherit;--shiki-dark:#F97583;--shiki-dark-font-style:inherit}html pre.shiki code .su5hD, html code.shiki .su5hD{--shiki-light:#90A4AE;--shiki-default:#24292E;--shiki-dark:#E1E4E8}html pre.shiki code .sjJ54, html code.shiki .sjJ54{--shiki-light:#39ADB5;--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 .s_hVV, html code.shiki .s_hVV{--shiki-light:#90A4AE;--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 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}",{"title":96,"searchDepth":135,"depth":135,"links":53175},[53176,53177,53178,53179,53188,53189,53190,53191,53195,53196,53197,53198,53205],{"id":79,"depth":135,"text":80},{"id":5880,"depth":135,"text":5881},{"id":52278,"depth":135,"text":52279},{"id":52308,"depth":135,"text":52309,"children":53180},[53181,53182,53183,53184,53185,53186,53187],{"id":52315,"depth":142,"text":52316},{"id":52334,"depth":142,"text":52335},{"id":52387,"depth":142,"text":52388},{"id":52416,"depth":142,"text":52417},{"id":52428,"depth":142,"text":52429},{"id":52455,"depth":142,"text":52456},{"id":52480,"depth":142,"text":52481},{"id":52490,"depth":135,"text":52491},{"id":52554,"depth":135,"text":52555},{"id":52649,"depth":135,"text":52650},{"id":52757,"depth":135,"text":52758,"children":53192},[53193,53194],{"id":30293,"depth":142,"text":30294},{"id":6243,"depth":142,"text":6244},{"id":52827,"depth":135,"text":52828},{"id":52931,"depth":135,"text":52932},{"id":30536,"depth":135,"text":30537},{"id":1513,"depth":135,"text":1514,"children":53199},[53200,53201,53202,53203,53204],{"id":53089,"depth":142,"text":53090},{"id":28718,"depth":142,"text":28719},{"id":53109,"depth":142,"text":53110},{"id":53123,"depth":142,"text":53124},{"id":53130,"depth":142,"text":53131},{"id":1553,"depth":135,"text":1554},"Master modulenotfounderror no module named x fix in our comprehensive Python beginner guide.",{},"\u002Ferrors\u002Fmodulenotfounderror-no-module-named-x-fix",{"title":28634,"description":53206},"errors\u002Fmodulenotfounderror-no-module-named-x-fix","k0bNJiOB0WlwB-3ujG6cHAPovgq4VomryELMSEeWIPQ",{"id":53213,"title":53214,"body":53215,"description":54243,"extension":1623,"meta":54244,"navigation":138,"path":54245,"seo":54246,"stem":54247,"__hash__":54248},"content\u002Ferrors\u002Fnameerror-global-name-not-defined-fix.md","NameError: global name not defined (Fix)",{"type":7,"value":53216,"toc":54220},[53217,53220,53226,53228,53255,53260,53262,53265,53269,53283,53286,53311,53318,53320,53322,53339,53346,53348,53353,53357,53381,53383,53408,53418,53422,53445,53447,53472,53474,53507,53509,53518,53525,53529,53557,53559,53584,53592,53594,53597,53614,53617,53640,53643,53687,53690,53717,53721,53727,53738,53742,53788,53790,53814,53816,53832,53835,53891,53893,53901,53910,53912,53914,53931,53933,53971,53974,54028,54042,54045,54055,54060,54062,54064,54084,54087,54111,54114,54139,54146,54148,54152,54157,54161,54166,54170,54173,54177,54186,54188,54217],[10,53218,53214],{"id":53219},"nameerror-global-name-not-defined-fix",[14,53221,31738,53222,53225],{},[22,53223,53224],{},"\"NameError: global name not defined\""," by finding the missing or misspelled name and making sure it exists before you use it.",[77,53227,80],{"id":79},[91,53229,53231],{"className":93,"code":53230,"language":95,"meta":96,"style":96},"message = \"Hello\"\nprint(message)\n",[17,53232,53233,53245],{"__ignoreMap":96},[100,53234,53235,53237,53239,53241,53243],{"class":102,"line":103},[100,53236,6868],{"class":106},[100,53238,111],{"class":110},[100,53240,1708],{"class":205},[100,53242,7683],{"class":209},[100,53244,1714],{"class":205},[100,53246,53247,53249,53251,53253],{"class":102,"line":135},[100,53248,372],{"class":114},[100,53250,170],{"class":118},[100,53252,1836],{"class":178},[100,53254,215],{"class":118},[14,53256,571,53257,53259],{},[17,53258,5616],{}," happens when Python cannot find a variable, function, or module name. Define it first, spell it correctly, and use the right scope.",[77,53261,5881],{"id":5880},[14,53263,53264],{},"Python raises this error when it tries to use a name that does not exist in the current scope.",[14,53266,571,53267,9348],{},[22,53268,2853],{},[40,53270,53271,53274,53277,53280],{},[43,53272,53273],{},"A variable",[43,53275,53276],{},"A function",[43,53278,53279],{},"A class",[43,53281,53282],{},"An imported module",[14,53284,53285],{},"You will often see this in modern Python 3 as:",[91,53287,53289],{"className":93,"code":53288,"language":95,"meta":96,"style":96},"NameError: name 'message' is not defined\n",[17,53290,53291],{"__ignoreMap":96},[100,53292,53293,53295,53297,53299,53301,53303,53305,53307,53309],{"class":102,"line":103},[100,53294,5616],{"class":191},[100,53296,89],{"class":118},[100,53298,4915],{"class":106},[100,53300,1280],{"class":205},[100,53302,1836],{"class":209},[100,53304,1280],{"class":205},[100,53306,16702],{"class":110},[100,53308,4921],{"class":110},[100,53310,16707],{"class":106},[14,53312,53313,53314,53317],{},"Older Python 2 wording sometimes says ",[22,53315,53316],{},"\"global name not defined\"",". The meaning is the same.",[77,53319,25091],{"id":25090},[14,53321,38066],{},[40,53323,53324,53327,53330,53333,53336],{},[43,53325,53326],{},"You used a variable before assigning a value to it",[43,53328,53329],{},"You misspelled the name",[43,53331,53332],{},"You used a name outside the function or block where it was created",[43,53334,53335],{},"You forgot to import a module or function",[43,53337,53338],{},"You expected a global variable to exist, but it was never defined",[14,53340,53341,53342,290],{},"If you are new to variables, see ",[295,53343,53345],{"href":53344},"\u002Flearn\u002Fpython-variables-explained-for-beginners","Python variables explained for beginners",[77,53347,11031],{"id":11030},[14,53349,53350,53351,290],{},"Here are common examples that trigger ",[17,53352,5616],{},[675,53354,53356],{"id":53355},"using-a-variable-before-assigning-it","Using a variable before assigning it",[91,53358,53360],{"className":93,"code":53359,"language":95,"meta":96,"style":96},"print(total)\ntotal = 10\n",[17,53361,53362,53372],{"__ignoreMap":96},[100,53363,53364,53366,53368,53370],{"class":102,"line":103},[100,53365,372],{"class":114},[100,53367,170],{"class":118},[100,53369,2520],{"class":178},[100,53371,215],{"class":118},[100,53373,53374,53377,53379],{"class":102,"line":135},[100,53375,53376],{"class":106},"total ",[100,53378,111],{"class":110},[100,53380,2181],{"class":122},[14,53382,218],{},[91,53384,53386],{"className":93,"code":53385,"language":95,"meta":96,"style":96},"NameError: name 'total' is not defined\n",[17,53387,53388],{"__ignoreMap":96},[100,53389,53390,53392,53394,53396,53398,53400,53402,53404,53406],{"class":102,"line":103},[100,53391,5616],{"class":191},[100,53393,89],{"class":118},[100,53395,4915],{"class":106},[100,53397,1280],{"class":205},[100,53399,2520],{"class":209},[100,53401,1280],{"class":205},[100,53403,16702],{"class":110},[100,53405,4921],{"class":110},[100,53407,16707],{"class":106},[14,53409,53410,53411,53414,53415,53417],{},"Python reads code from top to bottom. At the moment ",[17,53412,53413],{},"print(total)"," runs, ",[17,53416,2520],{}," does not exist yet.",[675,53419,53421],{"id":53420},"forgetting-to-import-a-module","Forgetting to import a module",[91,53423,53425],{"className":93,"code":53424,"language":95,"meta":96,"style":96},"print(math.sqrt(4))\n",[17,53426,53427],{"__ignoreMap":96},[100,53428,53429,53431,53433,53435,53437,53439,53441,53443],{"class":102,"line":103},[100,53430,372],{"class":114},[100,53432,170],{"class":118},[100,53434,9271],{"class":178},[100,53436,290],{"class":118},[100,53438,9276],{"class":178},[100,53440,170],{"class":118},[100,53442,8322],{"class":122},[100,53444,182],{"class":118},[14,53446,218],{},[91,53448,53450],{"className":93,"code":53449,"language":95,"meta":96,"style":96},"NameError: name 'math' is not defined\n",[17,53451,53452],{"__ignoreMap":96},[100,53453,53454,53456,53458,53460,53462,53464,53466,53468,53470],{"class":102,"line":103},[100,53455,5616],{"class":191},[100,53457,89],{"class":118},[100,53459,4915],{"class":106},[100,53461,1280],{"class":205},[100,53463,9271],{"class":209},[100,53465,1280],{"class":205},[100,53467,16702],{"class":110},[100,53469,4921],{"class":110},[100,53471,16707],{"class":106},[14,53473,15034],{},[91,53475,53477],{"className":93,"code":53476,"language":95,"meta":96,"style":96},"import math\n\nprint(math.sqrt(4))\n",[17,53478,53479,53485,53489],{"__ignoreMap":96},[100,53480,53481,53483],{"class":102,"line":103},[100,53482,9259],{"class":145},[100,53484,9262],{"class":106},[100,53486,53487],{"class":102,"line":135},[100,53488,139],{"emptyLinePlaceholder":138},[100,53490,53491,53493,53495,53497,53499,53501,53503,53505],{"class":102,"line":142},[100,53492,372],{"class":114},[100,53494,170],{"class":118},[100,53496,9271],{"class":178},[100,53498,290],{"class":118},[100,53500,9276],{"class":178},[100,53502,170],{"class":118},[100,53504,8322],{"class":122},[100,53506,182],{"class":118},[14,53508,218],{},[91,53510,53512],{"className":93,"code":53511,"language":95,"meta":96,"style":96},"2.0\n",[17,53513,53514],{"__ignoreMap":96},[100,53515,53516],{"class":102,"line":103},[100,53517,53511],{"class":122},[14,53519,53520,53521,290],{},"If imports are confusing, read ",[295,53522,53524],{"href":53523},"\u002Flearn\u002Fpython-modules-explained","Python modules explained",[675,53526,53528],{"id":53527},"using-the-wrong-variable-name","Using the wrong variable name",[91,53530,53532],{"className":93,"code":53531,"language":95,"meta":96,"style":96},"user_name = \"Maya\"\nprint(username)\n",[17,53533,53534,53547],{"__ignoreMap":96},[100,53535,53536,53539,53541,53543,53545],{"class":102,"line":103},[100,53537,53538],{"class":106},"user_name ",[100,53540,111],{"class":110},[100,53542,1708],{"class":205},[100,53544,2918],{"class":209},[100,53546,1714],{"class":205},[100,53548,53549,53551,53553,53555],{"class":102,"line":135},[100,53550,372],{"class":114},[100,53552,170],{"class":118},[100,53554,11330],{"class":178},[100,53556,215],{"class":118},[14,53558,218],{},[91,53560,53562],{"className":93,"code":53561,"language":95,"meta":96,"style":96},"NameError: name 'username' is not defined\n",[17,53563,53564],{"__ignoreMap":96},[100,53565,53566,53568,53570,53572,53574,53576,53578,53580,53582],{"class":102,"line":103},[100,53567,5616],{"class":191},[100,53569,89],{"class":118},[100,53571,4915],{"class":106},[100,53573,1280],{"class":205},[100,53575,11330],{"class":209},[100,53577,1280],{"class":205},[100,53579,16702],{"class":110},[100,53581,4921],{"class":110},[100,53583,16707],{"class":106},[14,53585,53586,53587,9472,53590,290],{},"The variable is named ",[17,53588,53589],{},"user_name",[17,53591,11330],{},[77,53593,6244],{"id":6243},[14,53595,53596],{},"Use these steps to fix the error:",[40,53598,53599,53602,53605,53608,53611],{},[43,53600,53601],{},"Define the variable before you use it",[43,53603,53604],{},"Check spelling and letter case carefully",[43,53606,53607],{},"Make sure the name exists in the right scope",[43,53609,53610],{},"Import the module or function before calling it",[43,53612,53613],{},"Read the traceback line number to find where the missing name is used",[14,53615,53616],{},"Example of fixing the order:",[91,53618,53620],{"className":93,"code":53619,"language":95,"meta":96,"style":96},"total = 10\nprint(total)\n",[17,53621,53622,53630],{"__ignoreMap":96},[100,53623,53624,53626,53628],{"class":102,"line":103},[100,53625,53376],{"class":106},[100,53627,111],{"class":110},[100,53629,2181],{"class":122},[100,53631,53632,53634,53636,53638],{"class":102,"line":135},[100,53633,372],{"class":114},[100,53635,170],{"class":118},[100,53637,2520],{"class":178},[100,53639,215],{"class":118},[14,53641,53642],{},"Example of fixing an import:",[91,53644,53646],{"className":93,"code":53645,"language":95,"meta":96,"style":96},"import math\n\nresult = math.sqrt(9)\nprint(result)\n",[17,53647,53648,53654,53658,53677],{"__ignoreMap":96},[100,53649,53650,53652],{"class":102,"line":103},[100,53651,9259],{"class":145},[100,53653,9262],{"class":106},[100,53655,53656],{"class":102,"line":135},[100,53657,139],{"emptyLinePlaceholder":138},[100,53659,53660,53662,53664,53667,53669,53671,53673,53675],{"class":102,"line":142},[100,53661,11556],{"class":106},[100,53663,111],{"class":110},[100,53665,53666],{"class":106}," math",[100,53668,290],{"class":118},[100,53670,9276],{"class":178},[100,53672,170],{"class":118},[100,53674,9281],{"class":122},[100,53676,215],{"class":118},[100,53678,53679,53681,53683,53685],{"class":102,"line":152},[100,53680,372],{"class":114},[100,53682,170],{"class":118},[100,53684,11580],{"class":178},[100,53686,215],{"class":118},[14,53688,53689],{},"Example of fixing a typo:",[91,53691,53693],{"className":93,"code":53692,"language":95,"meta":96,"style":96},"username = \"Maya\"\nprint(username)\n",[17,53694,53695,53707],{"__ignoreMap":96},[100,53696,53697,53699,53701,53703,53705],{"class":102,"line":103},[100,53698,2686],{"class":106},[100,53700,111],{"class":110},[100,53702,1708],{"class":205},[100,53704,2918],{"class":209},[100,53706,1714],{"class":205},[100,53708,53709,53711,53713,53715],{"class":102,"line":135},[100,53710,372],{"class":114},[100,53712,170],{"class":118},[100,53714,11330],{"class":178},[100,53716,215],{"class":118},[77,53718,53720],{"id":53719},"check-scope-problems","Check scope problems",[14,53722,53723,53724,290],{},"Scope means ",[22,53725,53726],{},"where a name is available in your program",[40,53728,53729,53732,53735],{},[43,53730,53731],{},"A variable created inside a function usually cannot be used outside that function",[43,53733,53734],{},"A variable created outside a function can usually be read inside the function",[43,53736,53737],{},"If you assign to a name inside a function, Python treats it as local unless handled differently",[675,53739,53741],{"id":53740},"local-variable-used-outside-a-function","Local variable used outside a function",[91,53743,53745],{"className":93,"code":53744,"language":95,"meta":96,"style":96},"def greet():\n    message = \"Hello\"\n\ngreet()\nprint(message)\n",[17,53746,53747,53755,53768,53772,53778],{"__ignoreMap":96},[100,53748,53749,53751,53753],{"class":102,"line":103},[100,53750,1078],{"class":1077},[100,53752,2848],{"class":1081},[100,53754,1085],{"class":118},[100,53756,53757,53760,53762,53764,53766],{"class":102,"line":135},[100,53758,53759],{"class":106},"    message ",[100,53761,111],{"class":110},[100,53763,1708],{"class":205},[100,53765,7683],{"class":209},[100,53767,1714],{"class":205},[100,53769,53770],{"class":102,"line":142},[100,53771,139],{"emptyLinePlaceholder":138},[100,53773,53774,53776],{"class":102,"line":152},[100,53775,2911],{"class":178},[100,53777,4734],{"class":118},[100,53779,53780,53782,53784,53786],{"class":102,"line":164},[100,53781,372],{"class":114},[100,53783,170],{"class":118},[100,53785,1836],{"class":178},[100,53787,215],{"class":118},[14,53789,218],{},[91,53791,53792],{"className":93,"code":53288,"language":95,"meta":96,"style":96},[17,53793,53794],{"__ignoreMap":96},[100,53795,53796,53798,53800,53802,53804,53806,53808,53810,53812],{"class":102,"line":103},[100,53797,5616],{"class":191},[100,53799,89],{"class":118},[100,53801,4915],{"class":106},[100,53803,1280],{"class":205},[100,53805,1836],{"class":209},[100,53807,1280],{"class":205},[100,53809,16702],{"class":110},[100,53811,4921],{"class":110},[100,53813,16707],{"class":106},[14,53815,4094],{},[40,53817,53818,53826,53829],{},[43,53819,53820,53822,53823],{},[17,53821,1836],{}," was created inside ",[17,53824,53825],{},"greet()",[43,53827,53828],{},"It only exists while that function runs",[43,53830,53831],{},"Outside the function, Python cannot find it",[14,53833,53834],{},"Fix it by returning the value:",[91,53836,53838],{"className":93,"code":53837,"language":95,"meta":96,"style":96},"def greet():\n    message = \"Hello\"\n    return message\n\nmessage = greet()\nprint(message)\n",[17,53839,53840,53848,53860,53867,53871,53881],{"__ignoreMap":96},[100,53841,53842,53844,53846],{"class":102,"line":103},[100,53843,1078],{"class":1077},[100,53845,2848],{"class":1081},[100,53847,1085],{"class":118},[100,53849,53850,53852,53854,53856,53858],{"class":102,"line":135},[100,53851,53759],{"class":106},[100,53853,111],{"class":110},[100,53855,1708],{"class":205},[100,53857,7683],{"class":209},[100,53859,1714],{"class":205},[100,53861,53862,53864],{"class":102,"line":142},[100,53863,2552],{"class":145},[100,53865,53866],{"class":106}," message\n",[100,53868,53869],{"class":102,"line":152},[100,53870,139],{"emptyLinePlaceholder":138},[100,53872,53873,53875,53877,53879],{"class":102,"line":164},[100,53874,6868],{"class":106},[100,53876,111],{"class":110},[100,53878,2848],{"class":178},[100,53880,4734],{"class":118},[100,53882,53883,53885,53887,53889],{"class":102,"line":185},[100,53884,372],{"class":114},[100,53886,170],{"class":118},[100,53888,1836],{"class":178},[100,53890,215],{"class":118},[14,53892,218],{},[91,53894,53895],{"className":93,"code":29173,"language":95,"meta":96,"style":96},[17,53896,53897],{"__ignoreMap":96},[100,53898,53899],{"class":102,"line":103},[100,53900,29173],{"class":106},[14,53902,53903,53904,3266,53906,290],{},"If you want a fuller explanation, see ",[295,53905,53345],{"href":53344},[295,53907,53909],{"href":53908},"\u002Ferrors\u002Funboundlocalerror-local-variable-referenced-before-assignment-fix","UnboundLocalError: local variable referenced before assignment",[77,53911,3274],{"id":3273},[14,53913,14373],{},[3282,53915,53916,53919,53922,53925,53928],{},[43,53917,53918],{},"Read the exact missing name in the error message",[43,53920,53921],{},"Go to the line shown in the traceback",[43,53923,53924],{},"Search earlier in the file for where that name should be defined",[43,53926,53927],{},"Check for typos, missing imports, and wrong indentation",[43,53929,53930],{},"Print nearby values if needed to confirm your program flow",[14,53932,5295],{},[91,53934,53936],{"className":93,"code":53935,"language":95,"meta":96,"style":96},"print(variable_name)\nprint(locals())\nprint(globals())\n",[17,53937,53938,53949,53960],{"__ignoreMap":96},[100,53939,53940,53942,53944,53947],{"class":102,"line":103},[100,53941,372],{"class":114},[100,53943,170],{"class":118},[100,53945,53946],{"class":178},"variable_name",[100,53948,215],{"class":118},[100,53950,53951,53953,53955,53958],{"class":102,"line":135},[100,53952,372],{"class":114},[100,53954,170],{"class":118},[100,53956,53957],{"class":114},"locals",[100,53959,3370],{"class":118},[100,53961,53962,53964,53966,53969],{"class":102,"line":142},[100,53963,372],{"class":114},[100,53965,170],{"class":118},[100,53967,53968],{"class":114},"globals",[100,53970,3370],{"class":118},[14,53972,53973],{},"For example, if you think a variable should exist inside a function:",[91,53975,53977],{"className":93,"code":53976,"language":95,"meta":96,"style":96},"def test():\n    value = 5\n    print(locals())\n\ntest()\nprint(globals())\n",[17,53978,53979,53988,53997,54007,54011,54018],{"__ignoreMap":96},[100,53980,53981,53983,53986],{"class":102,"line":103},[100,53982,1078],{"class":1077},[100,53984,53985],{"class":1081}," test",[100,53987,1085],{"class":118},[100,53989,53990,53993,53995],{"class":102,"line":135},[100,53991,53992],{"class":106},"    value ",[100,53994,111],{"class":110},[100,53996,37580],{"class":122},[100,53998,53999,54001,54003,54005],{"class":102,"line":142},[100,54000,200],{"class":114},[100,54002,170],{"class":118},[100,54004,53957],{"class":114},[100,54006,3370],{"class":118},[100,54008,54009],{"class":102,"line":152},[100,54010,139],{"emptyLinePlaceholder":138},[100,54012,54013,54016],{"class":102,"line":164},[100,54014,54015],{"class":178},"test",[100,54017,4734],{"class":118},[100,54019,54020,54022,54024,54026],{"class":102,"line":185},[100,54021,372],{"class":114},[100,54023,170],{"class":118},[100,54025,53968],{"class":114},[100,54027,3370],{"class":118},[40,54029,54030,54036],{},[43,54031,54032,54035],{},[17,54033,54034],{},"locals()"," shows names available in the current local scope",[43,54037,54038,54041],{},[17,54039,54040],{},"globals()"," shows names available at the top level of the file",[14,54043,54044],{},"You can also run your script from the terminal to see the full traceback:",[91,54046,54047],{"className":10352,"code":31396,"language":10354,"meta":96,"style":96},[17,54048,54049],{"__ignoreMap":96},[100,54050,54051,54053],{"class":102,"line":103},[100,54052,95],{"class":10361},[100,54054,30457],{"class":209},[14,54056,54057,54058,290],{},"If you want a step-by-step debugging process, read ",[295,54059,5690],{"href":32320},[77,54061,7117],{"id":7116},[14,54063,9001],{},[40,54065,54066,54069,54072,54075,54078,54081],{},[43,54067,54068],{},"Using a variable before assignment",[43,54070,54071],{},"Misspelled variable or function name",[43,54073,54074],{},"Wrong uppercase or lowercase letters",[43,54076,54077],{},"Forgotten import statement",[43,54079,54080],{},"Trying to use a local variable outside its function",[43,54082,54083],{},"Assuming a global variable already exists",[14,54085,54086],{},"Remember that Python is case-sensitive. These are different names:",[91,54088,54090],{"className":93,"code":54089,"language":95,"meta":96,"style":96},"count = 5\nprint(Count)\n",[17,54091,54092,54100],{"__ignoreMap":96},[100,54093,54094,54096,54098],{"class":102,"line":103},[100,54095,2176],{"class":106},[100,54097,111],{"class":110},[100,54099,37580],{"class":122},[100,54101,54102,54104,54106,54109],{"class":102,"line":135},[100,54103,372],{"class":114},[100,54105,170],{"class":118},[100,54107,54108],{"class":178},"Count",[100,54110,215],{"class":118},[14,54112,54113],{},"This causes:",[91,54115,54117],{"className":93,"code":54116,"language":95,"meta":96,"style":96},"NameError: name 'Count' is not defined\n",[17,54118,54119],{"__ignoreMap":96},[100,54120,54121,54123,54125,54127,54129,54131,54133,54135,54137],{"class":102,"line":103},[100,54122,5616],{"class":191},[100,54124,89],{"class":118},[100,54126,4915],{"class":106},[100,54128,1280],{"class":205},[100,54130,54108],{"class":209},[100,54132,1280],{"class":205},[100,54134,16702],{"class":110},[100,54136,4921],{"class":110},[100,54138,16707],{"class":106},[14,54140,14478,54141,3266,54143,54145],{},[17,54142,51844],{},[17,54144,54108],{}," are not the same.",[77,54147,1514],{"id":1513},[675,54149,54151],{"id":54150},"is-this-the-same-as-nameerror-name-is-not-defined","Is this the same as NameError: name is not defined?",[14,54153,10626,54154,54156],{},[22,54155,53316],{}," is older wording. Both mean Python cannot find a name.",[675,54158,54160],{"id":54159},"can-a-typo-really-cause-this-error","Can a typo really cause this error?",[14,54162,54163,54164,290],{},"Yes. Even a small spelling difference or wrong capitalization can cause ",[17,54165,5616],{},[675,54167,54169],{"id":54168},"why-does-this-happen-inside-a-function","Why does this happen inside a function?",[14,54171,54172],{},"The function may be using a name that was never passed in, never defined, or only exists in another scope.",[675,54174,54176],{"id":54175},"how-do-i-fix-a-missing-module-name","How do I fix a missing module name?",[14,54178,54179,54180,54182,54183,290],{},"Import the module first, such as ",[17,54181,26216],{},", before using ",[17,54184,54185],{},"math.sqrt()",[77,54187,1554],{"id":1553},[40,54189,54190,54195,54201,54205,54209,54213],{},[43,54191,54192],{},[295,54193,16728],{"href":54194},"\u002Ferrors\u002Fnameerror-name-is-not-defined-fix",[43,54196,54197],{},[295,54198,54200],{"href":54199},"\u002Ferrors\u002Fnameerror-in-python-causes-and-fixes","NameError in Python: causes and fixes",[43,54202,54203],{},[295,54204,53909],{"href":53908},[43,54206,54207],{},[295,54208,53345],{"href":53344},[43,54210,54211],{},[295,54212,53524],{"href":53523},[43,54214,54215],{},[295,54216,5690],{"href":32320},[1589,54218,54219],{},"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 .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 .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 .sbgvK, html code.shiki .sbgvK{--shiki-light:#E2931D;--shiki-default:#6F42C1;--shiki-dark:#B392F0}",{"title":96,"searchDepth":135,"depth":135,"links":54221},[54222,54223,54224,54225,54230,54231,54234,54235,54236,54242],{"id":79,"depth":135,"text":80},{"id":5880,"depth":135,"text":5881},{"id":25090,"depth":135,"text":25091},{"id":11030,"depth":135,"text":11031,"children":54226},[54227,54228,54229],{"id":53355,"depth":142,"text":53356},{"id":53420,"depth":142,"text":53421},{"id":53527,"depth":142,"text":53528},{"id":6243,"depth":135,"text":6244},{"id":53719,"depth":135,"text":53720,"children":54232},[54233],{"id":53740,"depth":142,"text":53741},{"id":3273,"depth":135,"text":3274},{"id":7116,"depth":135,"text":7117},{"id":1513,"depth":135,"text":1514,"children":54237},[54238,54239,54240,54241],{"id":54150,"depth":142,"text":54151},{"id":54159,"depth":142,"text":54160},{"id":54168,"depth":142,"text":54169},{"id":54175,"depth":142,"text":54176},{"id":1553,"depth":135,"text":1554},"Master nameerror global name not defined fix in our comprehensive Python beginner guide.",{},"\u002Ferrors\u002Fnameerror-global-name-not-defined-fix",{"title":53214,"description":54243},"errors\u002Fnameerror-global-name-not-defined-fix","uQ9fIY2nfzB8DkQ7dBUfB2l33D59SL5mpxRKar-qCCA",{"id":54250,"title":54251,"body":54252,"description":55483,"extension":1623,"meta":55484,"navigation":138,"path":54199,"seo":55485,"stem":55486,"__hash__":55487},"content\u002Ferrors\u002Fnameerror-in-python-causes-and-fixes.md","NameError in Python: Causes and Fixes",{"type":7,"value":54253,"toc":55450},[54254,54257,54262,54265,54275,54280,54282,54306,54311,54314,54328,54330,54335,54338,54352,54355,54358,54373,54375,54399,54405,54409,54412,54436,54438,54509,54515,54518,54520,54525,54547,54551,54579,54581,54606,54614,54618,54633,54635,54660,54663,54682,54686,54709,54711,54735,54741,54750,54754,54756,54775,54778,54792,54796,54800,54802,54831,54833,54860,54866,54868,54893,54895,54918,54927,54929,54953,54955,54982,54991,54993,55016,55018,55051,55053,55062,55066,55069,55074,55079,55081,55096,55103,55108,55113,55115,55170,55176,55183,55185,55189,55206,55209,55219,55222,55267,55270,55295,55303,55305,55311,55330,55333,55353,55361,55371,55373,55377,55385,55389,55397,55401,55406,55412,55416,55419,55421,55447],[10,54255,54251],{"id":54256},"nameerror-in-python-causes-and-fixes",[14,54258,571,54259,54261],{},[17,54260,5616],{}," in Python means you used a name that Python cannot find.",[14,54263,54264],{},"That name might be:",[40,54266,54267,54269,54271,54273],{},[43,54268,53273],{},[43,54270,53276],{},[43,54272,53279],{},[43,54274,53282],{},[14,54276,54277,54278,290],{},"This error is common for beginners because Python only knows names that were already created, assigned, or imported. If the name does not exist in the current scope, Python raises ",[17,54279,5616],{},[77,54281,80],{"id":79},[91,54283,54285],{"className":93,"code":54284,"language":95,"meta":96,"style":96},"x = 10\nprint(x)\n",[17,54286,54287,54296],{"__ignoreMap":96},[100,54288,54289,54292,54294],{"class":102,"line":103},[100,54290,54291],{"class":106},"x ",[100,54293,111],{"class":110},[100,54295,2181],{"class":122},[100,54297,54298,54300,54302,54304],{"class":102,"line":135},[100,54299,372],{"class":114},[100,54301,170],{"class":118},[100,54303,39024],{"class":178},[100,54305,215],{"class":118},[14,54307,571,54308,54310],{},[17,54309,5616],{}," happens when Python sees a variable, function, or module name that does not exist in the current scope.",[14,54312,54313],{},"Check these first:",[40,54315,54316,54319,54322,54325],{},[43,54317,54318],{},"Is the name spelled correctly?",[43,54320,54321],{},"Was it defined before this line?",[43,54323,54324],{},"Did you forget to import it?",[43,54326,54327],{},"Did you mean to write a string with quotes?",[77,54329,5881],{"id":5880},[14,54331,54332,54334],{},[17,54333,5616],{}," means Python cannot find the name you used.",[14,54336,54337],{},"For example, Python will raise this error if you try to use:",[40,54339,54340,54343,54346,54349],{},[43,54341,54342],{},"A variable that was never assigned",[43,54344,54345],{},"A function name that does not exist",[43,54347,54348],{},"A module that was not imported",[43,54350,54351],{},"Text without quotes, which Python reads as a name",[14,54353,54354],{},"Always read the exact name shown in the error message first. That tells you what Python could not find.",[14,54356,54357],{},"A typical error looks like this:",[91,54359,54361],{"className":93,"code":54360,"language":95,"meta":96,"style":96},"print(total)\n",[17,54362,54363],{"__ignoreMap":96},[100,54364,54365,54367,54369,54371],{"class":102,"line":103},[100,54366,372],{"class":114},[100,54368,170],{"class":118},[100,54370,2520],{"class":178},[100,54372,215],{"class":118},[14,54374,218],{},[91,54376,54377],{"className":93,"code":53385,"language":95,"meta":96,"style":96},[17,54378,54379],{"__ignoreMap":96},[100,54380,54381,54383,54385,54387,54389,54391,54393,54395,54397],{"class":102,"line":103},[100,54382,5616],{"class":191},[100,54384,89],{"class":118},[100,54386,4915],{"class":106},[100,54388,1280],{"class":205},[100,54390,2520],{"class":209},[100,54392,1280],{"class":205},[100,54394,16702],{"class":110},[100,54396,4921],{"class":110},[100,54398,16707],{"class":106},[14,54400,54401,54402,54404],{},"In this example, Python is telling you that ",[17,54403,2520],{}," does not exist at that point in the program.",[77,54406,54408],{"id":54407},"simple-example-that-causes-nameerror","Simple example that causes NameError",[14,54410,54411],{},"Here is a very short example:",[91,54413,54415],{"className":93,"code":54414,"language":95,"meta":96,"style":96},"print(score)\nscore = 100\n",[17,54416,54417,54428],{"__ignoreMap":96},[100,54418,54419,54421,54423,54426],{"class":102,"line":103},[100,54420,372],{"class":114},[100,54422,170],{"class":118},[100,54424,54425],{"class":178},"score",[100,54427,215],{"class":118},[100,54429,54430,54432,54434],{"class":102,"line":135},[100,54431,2249],{"class":106},[100,54433,111],{"class":110},[100,54435,5377],{"class":122},[14,54437,218],{},[91,54439,54441],{"className":93,"code":54440,"language":95,"meta":96,"style":96},"Traceback (most recent call last):\n  File \"example.py\", line 1, in \u003Cmodule>\n    print(score)\nNameError: name 'score' is not defined\n",[17,54442,54443,54453,54479,54489],{"__ignoreMap":96},[100,54444,54445,54447,54449,54451],{"class":102,"line":103},[100,54446,514],{"class":178},[100,54448,170],{"class":118},[100,54450,519],{"class":178},[100,54452,522],{"class":118},[100,54454,54455,54457,54459,54461,54463,54465,54467,54469,54471,54473,54475,54477],{"class":102,"line":135},[100,54456,1952],{"class":106},[100,54458,206],{"class":205},[100,54460,1957],{"class":209},[100,54462,206],{"class":205},[100,54464,126],{"class":118},[100,54466,1964],{"class":106},[100,54468,123],{"class":122},[100,54470,126],{"class":118},[100,54472,1971],{"class":110},[100,54474,1974],{"class":110},[100,54476,1977],{"class":106},[100,54478,1980],{"class":110},[100,54480,54481,54483,54485,54487],{"class":102,"line":142},[100,54482,200],{"class":114},[100,54484,170],{"class":118},[100,54486,54425],{"class":178},[100,54488,215],{"class":118},[100,54490,54491,54493,54495,54497,54499,54501,54503,54505,54507],{"class":102,"line":152},[100,54492,5616],{"class":191},[100,54494,89],{"class":118},[100,54496,4915],{"class":106},[100,54498,1280],{"class":205},[100,54500,54425],{"class":209},[100,54502,1280],{"class":205},[100,54504,16702],{"class":110},[100,54506,4921],{"class":110},[100,54508,16707],{"class":106},[14,54510,54511,54512,54514],{},"The problem is on the first line. ",[17,54513,54425],{}," is used before it is assigned a value.",[14,54516,54517],{},"Python runs code from top to bottom, so it must see the assignment first.",[77,54519,1344],{"id":1343},[14,54521,54522,54523,89],{},"These are the most common reasons for ",[17,54524,5616],{},[40,54526,54527,54529,54532,54535,54538,54541,54544],{},[43,54528,54071],{},[43,54530,54531],{},"Using a variable before it is assigned",[43,54533,54534],{},"Forgetting quotes around a string value",[43,54536,54537],{},"Forgetting to import a module or function",[43,54539,54540],{},"Using the wrong variable name inside a loop or function",[43,54542,54543],{},"Trying to use a name that only exists in another scope",[43,54545,54546],{},"Running only part of a script in a notebook or interactive session",[675,54548,54550],{"id":54549},"example-misspelled-name","Example: misspelled name",[91,54552,54554],{"className":93,"code":54553,"language":95,"meta":96,"style":96},"username = \"Maya\"\nprint(usernme)\n",[17,54555,54556,54568],{"__ignoreMap":96},[100,54557,54558,54560,54562,54564,54566],{"class":102,"line":103},[100,54559,2686],{"class":106},[100,54561,111],{"class":110},[100,54563,1708],{"class":205},[100,54565,2918],{"class":209},[100,54567,1714],{"class":205},[100,54569,54570,54572,54574,54577],{"class":102,"line":135},[100,54571,372],{"class":114},[100,54573,170],{"class":118},[100,54575,54576],{"class":178},"usernme",[100,54578,215],{"class":118},[14,54580,218],{},[91,54582,54584],{"className":93,"code":54583,"language":95,"meta":96,"style":96},"NameError: name 'usernme' is not defined\n",[17,54585,54586],{"__ignoreMap":96},[100,54587,54588,54590,54592,54594,54596,54598,54600,54602,54604],{"class":102,"line":103},[100,54589,5616],{"class":191},[100,54591,89],{"class":118},[100,54593,4915],{"class":106},[100,54595,1280],{"class":205},[100,54597,54576],{"class":209},[100,54599,1280],{"class":205},[100,54601,16702],{"class":110},[100,54603,4921],{"class":110},[100,54605,16707],{"class":106},[14,54607,54608,54610,54611,54613],{},[17,54609,11330],{}," was defined, but ",[17,54612,54576],{}," was used.",[675,54615,54617],{"id":54616},"example-missing-quotes","Example: missing quotes",[91,54619,54621],{"className":93,"code":54620,"language":95,"meta":96,"style":96},"print(hello)\n",[17,54622,54623],{"__ignoreMap":96},[100,54624,54625,54627,54629,54631],{"class":102,"line":103},[100,54626,372],{"class":114},[100,54628,170],{"class":118},[100,54630,3506],{"class":178},[100,54632,215],{"class":118},[14,54634,218],{},[91,54636,54638],{"className":93,"code":54637,"language":95,"meta":96,"style":96},"NameError: name 'hello' is not defined\n",[17,54639,54640],{"__ignoreMap":96},[100,54641,54642,54644,54646,54648,54650,54652,54654,54656,54658],{"class":102,"line":103},[100,54643,5616],{"class":191},[100,54645,89],{"class":118},[100,54647,4915],{"class":106},[100,54649,1280],{"class":205},[100,54651,3506],{"class":209},[100,54653,1280],{"class":205},[100,54655,16702],{"class":110},[100,54657,4921],{"class":110},[100,54659,16707],{"class":106},[14,54661,54662],{},"If you meant text, write:",[91,54664,54666],{"className":93,"code":54665,"language":95,"meta":96,"style":96},"print(\"hello\")\n",[17,54667,54668],{"__ignoreMap":96},[100,54669,54670,54672,54674,54676,54678,54680],{"class":102,"line":103},[100,54671,372],{"class":114},[100,54673,170],{"class":118},[100,54675,206],{"class":205},[100,54677,3506],{"class":209},[100,54679,206],{"class":205},[100,54681,215],{"class":118},[675,54683,54685],{"id":54684},"example-missing-import","Example: missing import",[91,54687,54689],{"className":93,"code":54688,"language":95,"meta":96,"style":96},"print(math.sqrt(16))\n",[17,54690,54691],{"__ignoreMap":96},[100,54692,54693,54695,54697,54699,54701,54703,54705,54707],{"class":102,"line":103},[100,54694,372],{"class":114},[100,54696,170],{"class":118},[100,54698,9271],{"class":178},[100,54700,290],{"class":118},[100,54702,9276],{"class":178},[100,54704,170],{"class":118},[100,54706,10180],{"class":122},[100,54708,182],{"class":118},[14,54710,218],{},[91,54712,54713],{"className":93,"code":53449,"language":95,"meta":96,"style":96},[17,54714,54715],{"__ignoreMap":96},[100,54716,54717,54719,54721,54723,54725,54727,54729,54731,54733],{"class":102,"line":103},[100,54718,5616],{"class":191},[100,54720,89],{"class":118},[100,54722,4915],{"class":106},[100,54724,1280],{"class":205},[100,54726,9271],{"class":209},[100,54728,1280],{"class":205},[100,54730,16702],{"class":110},[100,54732,4921],{"class":110},[100,54734,16707],{"class":106},[14,54736,54737,54738,54740],{},"This happens because ",[17,54739,9271],{}," was never imported.",[14,54742,54743,54744,54747,54748,290],{},"If you are still learning how variables work, see ",[295,54745,53345],{"href":54746},"\u002Flearn\u002Fpython-variables-explained-for-beginners\u002F",". If the problem is related to imports, see ",[295,54749,10137],{"href":25158},[77,54751,54753],{"id":54752},"how-to-fix-a-nameerror","How to fix a NameError",[14,54755,30422],{},[40,54757,54758,54761,54764,54767,54769,54772],{},[43,54759,54760],{},"Check the spelling of the name carefully",[43,54762,54763],{},"Define the variable before using it",[43,54765,54766],{},"Add quotes if you meant a string",[43,54768,53610],{},[43,54770,54771],{},"Make sure the name exists in the current function or file",[43,54773,54774],{},"Run the code from the top so earlier definitions are available",[14,54776,54777],{},"A good way to think about it is:",[3282,54779,54780,54783,54786,54789],{},[43,54781,54782],{},"Find the exact unknown name in the error message",[43,54784,54785],{},"Search earlier in the file for where it should be created",[43,54787,54788],{},"Fix the missing definition, typo, quotes, or import",[43,54790,54791],{},"Run the program again",[77,54793,54795],{"id":54794},"example-fixes","Example fixes",[675,54797,54799],{"id":54798},"fix-a-typo-in-a-variable-name","Fix a typo in a variable name",[14,54801,9709],{},[91,54803,54805],{"className":93,"code":54804,"language":95,"meta":96,"style":96},"message = \"Welcome\"\nprint(mesage)\n",[17,54806,54807,54820],{"__ignoreMap":96},[100,54808,54809,54811,54813,54815,54818],{"class":102,"line":103},[100,54810,6868],{"class":106},[100,54812,111],{"class":110},[100,54814,1708],{"class":205},[100,54816,54817],{"class":209},"Welcome",[100,54819,1714],{"class":205},[100,54821,54822,54824,54826,54829],{"class":102,"line":135},[100,54823,372],{"class":114},[100,54825,170],{"class":118},[100,54827,54828],{"class":178},"mesage",[100,54830,215],{"class":118},[14,54832,9750],{},[91,54834,54836],{"className":93,"code":54835,"language":95,"meta":96,"style":96},"message = \"Welcome\"\nprint(message)\n",[17,54837,54838,54850],{"__ignoreMap":96},[100,54839,54840,54842,54844,54846,54848],{"class":102,"line":103},[100,54841,6868],{"class":106},[100,54843,111],{"class":110},[100,54845,1708],{"class":205},[100,54847,54817],{"class":209},[100,54849,1714],{"class":205},[100,54851,54852,54854,54856,54858],{"class":102,"line":135},[100,54853,372],{"class":114},[100,54855,170],{"class":118},[100,54857,1836],{"class":178},[100,54859,215],{"class":118},[675,54861,54863,54864],{"id":54862},"move-the-assignment-above-the-print","Move the assignment above the ",[17,54865,13235],{},[14,54867,9709],{},[91,54869,54871],{"className":93,"code":54870,"language":95,"meta":96,"style":96},"print(price)\nprice = 9.99\n",[17,54872,54873,54883],{"__ignoreMap":96},[100,54874,54875,54877,54879,54881],{"class":102,"line":103},[100,54876,372],{"class":114},[100,54878,170],{"class":118},[100,54880,3211],{"class":178},[100,54882,215],{"class":118},[100,54884,54885,54888,54890],{"class":102,"line":135},[100,54886,54887],{"class":106},"price ",[100,54889,111],{"class":110},[100,54891,54892],{"class":122}," 9.99\n",[14,54894,9750],{},[91,54896,54898],{"className":93,"code":54897,"language":95,"meta":96,"style":96},"price = 9.99\nprint(price)\n",[17,54899,54900,54908],{"__ignoreMap":96},[100,54901,54902,54904,54906],{"class":102,"line":103},[100,54903,54887],{"class":106},[100,54905,111],{"class":110},[100,54907,54892],{"class":122},[100,54909,54910,54912,54914,54916],{"class":102,"line":135},[100,54911,372],{"class":114},[100,54913,170],{"class":118},[100,54915,3211],{"class":178},[100,54917,215],{"class":118},[675,54919,54921,54922,9932,54924,54926],{"id":54920},"change-hello-to-hello-when-text-is-intended","Change ",[17,54923,3506],{},[17,54925,3519],{}," when text is intended",[14,54928,9709],{},[91,54930,54932],{"className":93,"code":54931,"language":95,"meta":96,"style":96},"word = hello\nprint(word)\n",[17,54933,54934,54943],{"__ignoreMap":96},[100,54935,54936,54938,54940],{"class":102,"line":103},[100,54937,33213],{"class":106},[100,54939,111],{"class":110},[100,54941,54942],{"class":106}," hello\n",[100,54944,54945,54947,54949,54951],{"class":102,"line":135},[100,54946,372],{"class":114},[100,54948,170],{"class":118},[100,54950,8478],{"class":178},[100,54952,215],{"class":118},[14,54954,9750],{},[91,54956,54958],{"className":93,"code":54957,"language":95,"meta":96,"style":96},"word = \"hello\"\nprint(word)\n",[17,54959,54960,54972],{"__ignoreMap":96},[100,54961,54962,54964,54966,54968,54970],{"class":102,"line":103},[100,54963,33213],{"class":106},[100,54965,111],{"class":110},[100,54967,1708],{"class":205},[100,54969,3506],{"class":209},[100,54971,1714],{"class":205},[100,54973,54974,54976,54978,54980],{"class":102,"line":135},[100,54975,372],{"class":114},[100,54977,170],{"class":118},[100,54979,8478],{"class":178},[100,54981,215],{"class":118},[675,54983,54985,54986,54988,54989],{"id":54984},"import-math-before-using-mathsqrt","Import ",[17,54987,9271],{}," before using ",[17,54990,54185],{},[14,54992,9709],{},[91,54994,54996],{"className":93,"code":54995,"language":95,"meta":96,"style":96},"print(math.sqrt(25))\n",[17,54997,54998],{"__ignoreMap":96},[100,54999,55000,55002,55004,55006,55008,55010,55012,55014],{"class":102,"line":103},[100,55001,372],{"class":114},[100,55003,170],{"class":118},[100,55005,9271],{"class":178},[100,55007,290],{"class":118},[100,55009,9276],{"class":178},[100,55011,170],{"class":118},[100,55013,27135],{"class":122},[100,55015,182],{"class":118},[14,55017,9750],{},[91,55019,55021],{"className":93,"code":55020,"language":95,"meta":96,"style":96},"import math\n\nprint(math.sqrt(25))\n",[17,55022,55023,55029,55033],{"__ignoreMap":96},[100,55024,55025,55027],{"class":102,"line":103},[100,55026,9259],{"class":145},[100,55028,9262],{"class":106},[100,55030,55031],{"class":102,"line":135},[100,55032,139],{"emptyLinePlaceholder":138},[100,55034,55035,55037,55039,55041,55043,55045,55047,55049],{"class":102,"line":142},[100,55036,372],{"class":114},[100,55038,170],{"class":118},[100,55040,9271],{"class":178},[100,55042,290],{"class":118},[100,55044,9276],{"class":178},[100,55046,170],{"class":118},[100,55048,27135],{"class":122},[100,55050,182],{"class":118},[14,55052,218],{},[91,55054,55056],{"className":93,"code":55055,"language":95,"meta":96,"style":96},"5.0\n",[17,55057,55058],{"__ignoreMap":96},[100,55059,55060],{"class":102,"line":103},[100,55061,55055],{"class":122},[77,55063,55065],{"id":55064},"nameerror-vs-unboundlocalerror","NameError vs UnboundLocalError",[14,55067,55068],{},"These two errors are related, but they are not the same.",[675,55070,55072],{"id":55071},"nameerror",[17,55073,5616],{},[14,55075,55076,55078],{},[17,55077,5616],{}," means the name does not exist at all in the current lookup path.",[14,55080,1844],{},[91,55082,55084],{"className":93,"code":55083,"language":95,"meta":96,"style":96},"print(count)\n",[17,55085,55086],{"__ignoreMap":96},[100,55087,55088,55090,55092,55094],{"class":102,"line":103},[100,55089,372],{"class":114},[100,55091,170],{"class":118},[100,55093,51844],{"class":178},[100,55095,215],{"class":118},[14,55097,11734,55098,55100,55101,290],{},[17,55099,51844],{}," was never defined, Python raises ",[17,55102,5616],{},[675,55104,55106],{"id":55105},"unboundlocalerror",[17,55107,16651],{},[14,55109,55110,55112],{},[17,55111,16651],{}," means Python thinks a variable is local to a function, but you used it before assigning a value inside that function.",[14,55114,1844],{},[91,55116,55118],{"className":93,"code":55117,"language":95,"meta":96,"style":96},"count = 10\n\ndef show_count():\n    print(count)\n    count = 20\n\nshow_count()\n",[17,55119,55120,55128,55132,55141,55151,55159,55163],{"__ignoreMap":96},[100,55121,55122,55124,55126],{"class":102,"line":103},[100,55123,2176],{"class":106},[100,55125,111],{"class":110},[100,55127,2181],{"class":122},[100,55129,55130],{"class":102,"line":135},[100,55131,139],{"emptyLinePlaceholder":138},[100,55133,55134,55136,55139],{"class":102,"line":142},[100,55135,1078],{"class":1077},[100,55137,55138],{"class":1081}," show_count",[100,55140,1085],{"class":118},[100,55142,55143,55145,55147,55149],{"class":102,"line":152},[100,55144,200],{"class":114},[100,55146,170],{"class":118},[100,55148,51844],{"class":178},[100,55150,215],{"class":118},[100,55152,55153,55155,55157],{"class":102,"line":164},[100,55154,41602],{"class":106},[100,55156,111],{"class":110},[100,55158,1669],{"class":122},[100,55160,55161],{"class":102,"line":185},[100,55162,139],{"emptyLinePlaceholder":138},[100,55164,55165,55168],{"class":102,"line":197},[100,55166,55167],{"class":178},"show_count",[100,55169,4734],{"class":118},[14,55171,3513,55172,9472,55174,290],{},[17,55173,16651],{},[17,55175,5616],{},[14,55177,55178,55179,290],{},"If you are not sure which one you have, read ",[295,55180,55182],{"href":55181},"\u002Ferrors\u002Funboundlocalerror-local-variable-referenced-before-assignment-fix\u002F","UnboundLocalError: local variable referenced before assignment fix",[77,55184,6924],{"id":6923},[14,55186,3277,55187,3280],{},[17,55188,5616],{},[3282,55190,55191,55194,55197,55200,55203],{},[43,55192,55193],{},"Look at the exact line number in the traceback",[43,55195,55196],{},"Find the unknown name on that line",[43,55198,55199],{},"Search earlier in the file for where it should be defined",[43,55201,55202],{},"Check for typos, missing imports, and missing quotes",[43,55204,55205],{},"Test a small fix and run the file again",[14,55207,55208],{},"These commands can also help while debugging:",[91,55210,55211],{"className":10352,"code":31396,"language":10354,"meta":96,"style":96},[17,55212,55213],{"__ignoreMap":96},[100,55214,55215,55217],{"class":102,"line":103},[100,55216,95],{"class":10361},[100,55218,30457],{"class":209},[14,55220,55221],{},"Run the whole script from the top. This helps when the problem is caused by missing earlier definitions.",[91,55223,55225],{"className":93,"code":55224,"language":95,"meta":96,"style":96},"print(variable_name)\nprint(type(variable_name))\ndir()\nhelp(name)\n",[17,55226,55227,55237,55251,55257],{"__ignoreMap":96},[100,55228,55229,55231,55233,55235],{"class":102,"line":103},[100,55230,372],{"class":114},[100,55232,170],{"class":118},[100,55234,53946],{"class":178},[100,55236,215],{"class":118},[100,55238,55239,55241,55243,55245,55247,55249],{"class":102,"line":135},[100,55240,372],{"class":114},[100,55242,170],{"class":118},[100,55244,1250],{"class":191},[100,55246,170],{"class":118},[100,55248,53946],{"class":178},[100,55250,182],{"class":118},[100,55252,55253,55255],{"class":102,"line":142},[100,55254,3822],{"class":114},[100,55256,4734],{"class":118},[100,55258,55259,55261,55263,55265],{"class":102,"line":152},[100,55260,2478],{"class":114},[100,55262,170],{"class":118},[100,55264,2853],{"class":178},[100,55266,215],{"class":118},[14,55268,55269],{},"Use them carefully:",[40,55271,55272,55278,55284,55289],{},[43,55273,55274,55277],{},[17,55275,55276],{},"print(variable_name)"," can help if the variable exists",[43,55279,55280,55283],{},[17,55281,55282],{},"print(type(variable_name))"," shows what kind of value it is",[43,55285,55286,55288],{},[17,55287,4565],{}," shows names available in the current scope",[43,55290,55291,55294],{},[17,55292,55293],{},"help(name)"," gives information about a function, class, or module",[14,55296,55297,55298,3266,55300,55302],{},"Be aware that ",[17,55299,55276],{},[17,55301,55293],{}," will also fail if the name truly does not exist yet.",[77,55304,7117],{"id":7116},[14,55306,55307,55308,55310],{},"Some beginner mistakes cause ",[17,55309,5616],{}," again and again:",[40,55312,55313,55316,55318,55321,55324,55327],{},[43,55314,55315],{},"Typing a variable name incorrectly",[43,55317,53356],{},[43,55319,55320],{},"Forgetting to put quotes around text",[43,55322,55323],{},"Forgetting an import statement",[43,55325,55326],{},"Using a local name outside its scope",[43,55328,55329],{},"Running only part of a script in an interactive environment",[14,55331,55332],{},"For example, this can happen in a notebook:",[91,55334,55336],{"className":93,"code":55335,"language":95,"meta":96,"style":96},"# Cell 2\nprint(total)\n",[17,55337,55338,55343],{"__ignoreMap":96},[100,55339,55340],{"class":102,"line":103},[100,55341,55342],{"class":414},"# Cell 2\n",[100,55344,55345,55347,55349,55351],{"class":102,"line":135},[100,55346,372],{"class":114},[100,55348,170],{"class":118},[100,55350,2520],{"class":178},[100,55352,215],{"class":118},[14,55354,55355,55356,55358,55359,290],{},"If you forgot to run the cell that creates ",[17,55357,2520],{},", Python will raise ",[17,55360,5616],{},[14,55362,55363,55364,55367,55368,290],{},"If your error message is specifically ",[17,55365,55366],{},"name 'x' is not defined",", see the more focused guide: ",[295,55369,55370],{"href":16727},"NameError: name is not defined fix",[77,55372,1514],{"id":1513},[675,55374,55376],{"id":55375},"what-is-the-difference-between-nameerror-and-syntaxerror","What is the difference between NameError and SyntaxError?",[14,55378,55379,55381,55382,55384],{},[17,55380,16503],{}," means the code is written in an invalid way. ",[17,55383,5616],{}," means the code syntax is valid, but Python cannot find a name at runtime.",[675,55386,55388],{"id":55387},"why-do-i-get-nameerror-for-text-like-hello","Why do I get NameError for text like hello?",[14,55390,55391,55392,55394,55395,290],{},"Python treats ",[17,55393,3506],{}," as a variable name unless you write it as a string with quotes, like ",[17,55396,3519],{},[675,55398,55400],{"id":55399},"can-a-missing-import-cause-nameerror","Can a missing import cause NameError?",[14,55402,55403,55404,290],{},"Yes. If you use a module, function, or class without importing it first, Python may raise ",[17,55405,5616],{},[14,55407,55408,55409,290],{},"If the problem is about importing a package that does not exist, you may also need ",[295,55410,55411],{"href":53208},"ModuleNotFoundError: No module named X fix",[675,55413,55415],{"id":55414},"why-does-code-work-in-one-cell-but-fail-in-another","Why does code work in one cell but fail in another?",[14,55417,55418],{},"In notebooks or interactive sessions, a name may only exist if the cell that defines it was run first.",[77,55420,1554],{"id":1553},[40,55422,55423,55427,55431,55435,55439,55443],{},[43,55424,55425],{},[295,55426,55370],{"href":16727},[43,55428,55429],{},[295,55430,55182],{"href":55181},[43,55432,55433],{},[295,55434,53345],{"href":54746},[43,55436,55437],{},[295,55438,10666],{"href":25158},[43,55440,55441],{},[295,55442,55411],{"href":53208},[43,55444,55445],{},[295,55446,3728],{"href":8972},[1589,55448,55449],{},"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 .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 .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 .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 .sbgvK, html code.shiki .sbgvK{--shiki-light:#E2931D;--shiki-default:#6F42C1;--shiki-dark:#B392F0}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}",{"title":96,"searchDepth":135,"depth":135,"links":55451},[55452,55453,55454,55455,55460,55461,55470,55474,55475,55476,55482],{"id":79,"depth":135,"text":80},{"id":5880,"depth":135,"text":5881},{"id":54407,"depth":135,"text":54408},{"id":1343,"depth":135,"text":1344,"children":55456},[55457,55458,55459],{"id":54549,"depth":142,"text":54550},{"id":54616,"depth":142,"text":54617},{"id":54684,"depth":142,"text":54685},{"id":54752,"depth":135,"text":54753},{"id":54794,"depth":135,"text":54795,"children":55462},[55463,55464,55466,55468],{"id":54798,"depth":142,"text":54799},{"id":54862,"depth":142,"text":55465},"Move the assignment above the print()",{"id":54920,"depth":142,"text":55467},"Change hello to \"hello\" when text is intended",{"id":54984,"depth":142,"text":55469},"Import math before using math.sqrt()",{"id":55064,"depth":135,"text":55065,"children":55471},[55472,55473],{"id":55071,"depth":142,"text":5616},{"id":55105,"depth":142,"text":16651},{"id":6923,"depth":135,"text":6924},{"id":7116,"depth":135,"text":7117},{"id":1513,"depth":135,"text":1514,"children":55477},[55478,55479,55480,55481],{"id":55375,"depth":142,"text":55376},{"id":55387,"depth":142,"text":55388},{"id":55399,"depth":142,"text":55400},{"id":55414,"depth":142,"text":55415},{"id":1553,"depth":135,"text":1554},"Master nameerror in python causes and fixes in our comprehensive Python beginner guide.",{},{"title":54251,"description":55483},"errors\u002Fnameerror-in-python-causes-and-fixes","gYaZYQzjyz6N4ceii1rM-7pjpGwWx-ti3U2PsQZDc9Q",{"id":55489,"title":55490,"body":55491,"description":56734,"extension":1623,"meta":56735,"navigation":138,"path":54194,"seo":56736,"stem":56737,"__hash__":56738},"content\u002Ferrors\u002Fnameerror-name-is-not-defined-fix.md","NameError: name is not defined (Fix)",{"type":7,"value":55492,"toc":56710},[55493,55496,55501,55503,55530,55533,55535,55540,55545,55557,55562,55564,55578,55580,55604,55609,55611,55614,55636,55638,55662,55670,55673,55695,55697,55705,55709,55713,55718,55721,55735,55744,55771,55773,55798,55800,55827,55830,55834,55837,55839,55863,55865,55888,55894,55897,55901,55904,55951,55953,55977,55981,55984,56039,56041,56049,56052,56057,56061,56066,56088,56090,56114,56116,56148,56150,56158,56161,56193,56195,56203,56208,56212,56214,56217,56231,56242,56244,56268,56271,56289,56291,56299,56302,56335,56338,56340,56345,56362,56365,56416,56419,56447,56458,56462,56467,56469,56472,56487,56493,56495,56498,56550,56558,56563,56566,56569,56608,56615,56619,56621,56623,56641,56647,56649,56653,56656,56660,56663,56667,56672,56676,56679,56681,56707],[10,55494,55490],{"id":55495},"nameerror-name-is-not-defined-fix",[14,55497,31738,55498,55500],{},[17,55499,16728],{},". This page explains what the error means, why it happens, and the most common ways to correct it.",[77,55502,80],{"id":79},[91,55504,55506],{"className":93,"code":55505,"language":95,"meta":96,"style":96},"user_name = \"Sam\"\nprint(user_name)\n",[17,55507,55508,55520],{"__ignoreMap":96},[100,55509,55510,55512,55514,55516,55518],{"class":102,"line":103},[100,55511,53538],{"class":106},[100,55513,111],{"class":110},[100,55515,1708],{"class":205},[100,55517,29329],{"class":209},[100,55519,1714],{"class":205},[100,55521,55522,55524,55526,55528],{"class":102,"line":135},[100,55523,372],{"class":114},[100,55525,170],{"class":118},[100,55527,53589],{"class":178},[100,55529,215],{"class":118},[14,55531,55532],{},"This error happens when Python sees a name that has not been created yet, is misspelled, or is outside the current scope.",[77,55534,5881],{"id":5880},[14,55536,55537,55539],{},[17,55538,5616],{}," means Python found a variable, function, or module name it does not know.",[14,55541,55542,55543,9348],{},"In Python, a ",[22,55544,2853],{},[40,55546,55547,55549,55551,55554],{},[43,55548,53273],{},[43,55550,53276],{},[43,55552,55553],{},"A module",[43,55555,55556],{},"Another object you assigned to a label",[14,55558,55559,55560,290],{},"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 ",[17,55561,5616],{},[14,55563,580],{},[91,55565,55566],{"className":93,"code":54360,"language":95,"meta":96,"style":96},[17,55567,55568],{"__ignoreMap":96},[100,55569,55570,55572,55574,55576],{"class":102,"line":103},[100,55571,372],{"class":114},[100,55573,170],{"class":118},[100,55575,2520],{"class":178},[100,55577,215],{"class":118},[14,55579,218],{},[91,55581,55582],{"className":93,"code":53385,"language":95,"meta":96,"style":96},[17,55583,55584],{"__ignoreMap":96},[100,55585,55586,55588,55590,55592,55594,55596,55598,55600,55602],{"class":102,"line":103},[100,55587,5616],{"class":191},[100,55589,89],{"class":118},[100,55591,4915],{"class":106},[100,55593,1280],{"class":205},[100,55595,2520],{"class":209},[100,55597,1280],{"class":205},[100,55599,16702],{"class":110},[100,55601,4921],{"class":110},[100,55603,16707],{"class":106},[14,55605,55606,55607,53417],{},"Python stops because ",[17,55608,2520],{},[77,55610,24958],{"id":24957},[14,55612,55613],{},"One of the most common causes is using a variable before assigning a value to it.",[91,55615,55616],{"className":93,"code":53359,"language":95,"meta":96,"style":96},[17,55617,55618,55628],{"__ignoreMap":96},[100,55619,55620,55622,55624,55626],{"class":102,"line":103},[100,55621,372],{"class":114},[100,55623,170],{"class":118},[100,55625,2520],{"class":178},[100,55627,215],{"class":118},[100,55629,55630,55632,55634],{"class":102,"line":135},[100,55631,53376],{"class":106},[100,55633,111],{"class":110},[100,55635,2181],{"class":122},[14,55637,218],{},[91,55639,55640],{"className":93,"code":53385,"language":95,"meta":96,"style":96},[17,55641,55642],{"__ignoreMap":96},[100,55643,55644,55646,55648,55650,55652,55654,55656,55658,55660],{"class":102,"line":103},[100,55645,5616],{"class":191},[100,55647,89],{"class":118},[100,55649,4915],{"class":106},[100,55651,1280],{"class":205},[100,55653,2520],{"class":209},[100,55655,1280],{"class":205},[100,55657,16702],{"class":110},[100,55659,4921],{"class":110},[100,55661,16707],{"class":106},[14,55663,55664,55665,5870,55667,55669],{},"This fails because Python runs code from top to bottom. When it reaches ",[17,55666,53413],{},[17,55668,2520],{}," has not been created yet.",[14,55671,55672],{},"To fix it, define the variable first:",[91,55674,55675],{"className":93,"code":53619,"language":95,"meta":96,"style":96},[17,55676,55677,55685],{"__ignoreMap":96},[100,55678,55679,55681,55683],{"class":102,"line":103},[100,55680,53376],{"class":106},[100,55682,111],{"class":110},[100,55684,2181],{"class":122},[100,55686,55687,55689,55691,55693],{"class":102,"line":135},[100,55688,372],{"class":114},[100,55690,170],{"class":118},[100,55692,2520],{"class":178},[100,55694,215],{"class":118},[14,55696,218],{},[91,55698,55699],{"className":93,"code":3410,"language":95,"meta":96,"style":96},[17,55700,55701],{"__ignoreMap":96},[100,55702,55703],{"class":102,"line":103},[100,55704,3410],{"class":122},[14,55706,53341,55707,290],{},[295,55708,53345],{"href":54746},[77,55710,55712],{"id":55711},"cause-1-misspelled-variable-or-function-name","Cause 1: Misspelled variable or function name",[14,55714,55715,55716,290],{},"A small typo can cause ",[17,55717,5616],{},[14,55719,55720],{},"Check these carefully:",[40,55722,55723,55726,55729,55732],{},[43,55724,55725],{},"Uppercase and lowercase letters",[43,55727,55728],{},"Missing letters",[43,55730,55731],{},"Extra letters",[43,55733,55734],{},"Different naming styles",[14,55736,55737,55738,3266,55741,55743],{},"For example, ",[17,55739,55740],{},"userName",[17,55742,11330],{}," are different names in Python.",[91,55745,55747],{"className":93,"code":55746,"language":95,"meta":96,"style":96},"username = \"Sam\"\nprint(userName)\n",[17,55748,55749,55761],{"__ignoreMap":96},[100,55750,55751,55753,55755,55757,55759],{"class":102,"line":103},[100,55752,2686],{"class":106},[100,55754,111],{"class":110},[100,55756,1708],{"class":205},[100,55758,29329],{"class":209},[100,55760,1714],{"class":205},[100,55762,55763,55765,55767,55769],{"class":102,"line":135},[100,55764,372],{"class":114},[100,55766,170],{"class":118},[100,55768,55740],{"class":178},[100,55770,215],{"class":118},[14,55772,218],{},[91,55774,55776],{"className":93,"code":55775,"language":95,"meta":96,"style":96},"NameError: name 'userName' is not defined\n",[17,55777,55778],{"__ignoreMap":96},[100,55779,55780,55782,55784,55786,55788,55790,55792,55794,55796],{"class":102,"line":103},[100,55781,5616],{"class":191},[100,55783,89],{"class":118},[100,55785,4915],{"class":106},[100,55787,1280],{"class":205},[100,55789,55740],{"class":209},[100,55791,1280],{"class":205},[100,55793,16702],{"class":110},[100,55795,4921],{"class":110},[100,55797,16707],{"class":106},[14,55799,15034],{},[91,55801,55803],{"className":93,"code":55802,"language":95,"meta":96,"style":96},"username = \"Sam\"\nprint(username)\n",[17,55804,55805,55817],{"__ignoreMap":96},[100,55806,55807,55809,55811,55813,55815],{"class":102,"line":103},[100,55808,2686],{"class":106},[100,55810,111],{"class":110},[100,55812,1708],{"class":205},[100,55814,29329],{"class":209},[100,55816,1714],{"class":205},[100,55818,55819,55821,55823,55825],{"class":102,"line":135},[100,55820,372],{"class":114},[100,55822,170],{"class":118},[100,55824,11330],{"class":178},[100,55826,215],{"class":118},[14,55828,55829],{},"Make sure the name is spelled the same everywhere in your code.",[77,55831,55833],{"id":55832},"cause-2-variable-created-after-use","Cause 2: Variable created after use",[14,55835,55836],{},"Assignment must happen before the name is used.",[14,55838,9709],{},[91,55840,55842],{"className":93,"code":55841,"language":95,"meta":96,"style":96},"print(score)\nscore = 95\n",[17,55843,55844,55854],{"__ignoreMap":96},[100,55845,55846,55848,55850,55852],{"class":102,"line":103},[100,55847,372],{"class":114},[100,55849,170],{"class":118},[100,55851,54425],{"class":178},[100,55853,215],{"class":118},[100,55855,55856,55858,55860],{"class":102,"line":135},[100,55857,2249],{"class":106},[100,55859,111],{"class":110},[100,55861,55862],{"class":122}," 95\n",[14,55864,15825],{},[91,55866,55868],{"className":93,"code":55867,"language":95,"meta":96,"style":96},"score = 95\nprint(score)\n",[17,55869,55870,55878],{"__ignoreMap":96},[100,55871,55872,55874,55876],{"class":102,"line":103},[100,55873,2249],{"class":106},[100,55875,111],{"class":110},[100,55877,55862],{"class":122},[100,55879,55880,55882,55884,55886],{"class":102,"line":135},[100,55881,372],{"class":114},[100,55883,170],{"class":118},[100,55885,54425],{"class":178},[100,55887,215],{"class":118},[14,55889,55890,55891,55893],{},"When debugging, read your code from top to bottom. Python does not look ahead to see that ",[17,55892,54425],{}," will be created later.",[14,55895,55896],{},"This is a very common beginner issue when learning how variables work.",[77,55898,55900],{"id":55899},"cause-3-name-is-outside-the-current-scope","Cause 3: Name is outside the current scope",[14,55902,55903],{},"A variable inside a function is usually not available outside it.",[91,55905,55907],{"className":93,"code":55906,"language":95,"meta":96,"style":96},"def create_message():\n    message = \"Hello\"\n\ncreate_message()\nprint(message)\n",[17,55908,55909,55918,55930,55934,55941],{"__ignoreMap":96},[100,55910,55911,55913,55916],{"class":102,"line":103},[100,55912,1078],{"class":1077},[100,55914,55915],{"class":1081}," create_message",[100,55917,1085],{"class":118},[100,55919,55920,55922,55924,55926,55928],{"class":102,"line":135},[100,55921,53759],{"class":106},[100,55923,111],{"class":110},[100,55925,1708],{"class":205},[100,55927,7683],{"class":209},[100,55929,1714],{"class":205},[100,55931,55932],{"class":102,"line":142},[100,55933,139],{"emptyLinePlaceholder":138},[100,55935,55936,55939],{"class":102,"line":152},[100,55937,55938],{"class":178},"create_message",[100,55940,4734],{"class":118},[100,55942,55943,55945,55947,55949],{"class":102,"line":164},[100,55944,372],{"class":114},[100,55946,170],{"class":118},[100,55948,1836],{"class":178},[100,55950,215],{"class":118},[14,55952,218],{},[91,55954,55955],{"className":93,"code":53288,"language":95,"meta":96,"style":96},[17,55956,55957],{"__ignoreMap":96},[100,55958,55959,55961,55963,55965,55967,55969,55971,55973,55975],{"class":102,"line":103},[100,55960,5616],{"class":191},[100,55962,89],{"class":118},[100,55964,4915],{"class":106},[100,55966,1280],{"class":205},[100,55968,1836],{"class":209},[100,55970,1280],{"class":205},[100,55972,16702],{"class":110},[100,55974,4921],{"class":110},[100,55976,16707],{"class":106},[14,55978,17008,55979,25753],{},[17,55980,1836],{},[14,55982,55983],{},"To fix this, return the value and store it in a variable:",[91,55985,55987],{"className":93,"code":55986,"language":95,"meta":96,"style":96},"def create_message():\n    message = \"Hello\"\n    return message\n\nresult = create_message()\nprint(result)\n",[17,55988,55989,55997,56009,56015,56019,56029],{"__ignoreMap":96},[100,55990,55991,55993,55995],{"class":102,"line":103},[100,55992,1078],{"class":1077},[100,55994,55915],{"class":1081},[100,55996,1085],{"class":118},[100,55998,55999,56001,56003,56005,56007],{"class":102,"line":135},[100,56000,53759],{"class":106},[100,56002,111],{"class":110},[100,56004,1708],{"class":205},[100,56006,7683],{"class":209},[100,56008,1714],{"class":205},[100,56010,56011,56013],{"class":102,"line":142},[100,56012,2552],{"class":145},[100,56014,53866],{"class":106},[100,56016,56017],{"class":102,"line":152},[100,56018,139],{"emptyLinePlaceholder":138},[100,56020,56021,56023,56025,56027],{"class":102,"line":164},[100,56022,11556],{"class":106},[100,56024,111],{"class":110},[100,56026,55915],{"class":178},[100,56028,4734],{"class":118},[100,56030,56031,56033,56035,56037],{"class":102,"line":185},[100,56032,372],{"class":114},[100,56034,170],{"class":118},[100,56036,11580],{"class":178},[100,56038,215],{"class":118},[14,56040,218],{},[91,56042,56043],{"className":93,"code":29173,"language":95,"meta":96,"style":96},[17,56044,56045],{"__ignoreMap":96},[100,56046,56047],{"class":102,"line":103},[100,56048,29173],{"class":106},[14,56050,56051],{},"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,56053,56054,56055,290],{},"If function scope is confusing, read ",[295,56056,29749],{"href":29189},[77,56058,56060],{"id":56059},"cause-4-forgot-to-import-a-module-or-function","Cause 4: Forgot to import a module or function",[14,56062,56063,56064,290],{},"If you use a module name before importing it, Python will raise a ",[17,56065,5616],{},[91,56067,56068],{"className":93,"code":54688,"language":95,"meta":96,"style":96},[17,56069,56070],{"__ignoreMap":96},[100,56071,56072,56074,56076,56078,56080,56082,56084,56086],{"class":102,"line":103},[100,56073,372],{"class":114},[100,56075,170],{"class":118},[100,56077,9271],{"class":178},[100,56079,290],{"class":118},[100,56081,9276],{"class":178},[100,56083,170],{"class":118},[100,56085,10180],{"class":122},[100,56087,182],{"class":118},[14,56089,218],{},[91,56091,56092],{"className":93,"code":53449,"language":95,"meta":96,"style":96},[17,56093,56094],{"__ignoreMap":96},[100,56095,56096,56098,56100,56102,56104,56106,56108,56110,56112],{"class":102,"line":103},[100,56097,5616],{"class":191},[100,56099,89],{"class":118},[100,56101,4915],{"class":106},[100,56103,1280],{"class":205},[100,56105,9271],{"class":209},[100,56107,1280],{"class":205},[100,56109,16702],{"class":110},[100,56111,4921],{"class":110},[100,56113,16707],{"class":106},[14,56115,15034],{},[91,56117,56118],{"className":93,"code":10151,"language":95,"meta":96,"style":96},[17,56119,56120,56126,56130],{"__ignoreMap":96},[100,56121,56122,56124],{"class":102,"line":103},[100,56123,9259],{"class":145},[100,56125,9262],{"class":106},[100,56127,56128],{"class":102,"line":135},[100,56129,139],{"emptyLinePlaceholder":138},[100,56131,56132,56134,56136,56138,56140,56142,56144,56146],{"class":102,"line":142},[100,56133,372],{"class":114},[100,56135,170],{"class":118},[100,56137,9271],{"class":178},[100,56139,290],{"class":118},[100,56141,9276],{"class":178},[100,56143,170],{"class":118},[100,56145,10180],{"class":122},[100,56147,182],{"class":118},[14,56149,218],{},[91,56151,56152],{"className":93,"code":25067,"language":95,"meta":96,"style":96},[17,56153,56154],{"__ignoreMap":96},[100,56155,56156],{"class":102,"line":103},[100,56157,25067],{"class":122},[14,56159,56160],{},"You can also import a specific function:",[91,56162,56163],{"className":93,"code":10194,"language":95,"meta":96,"style":96},[17,56164,56165,56175,56179],{"__ignoreMap":96},[100,56166,56167,56169,56171,56173],{"class":102,"line":103},[100,56168,10201],{"class":145},[100,56170,10204],{"class":106},[100,56172,9259],{"class":145},[100,56174,10209],{"class":106},[100,56176,56177],{"class":102,"line":135},[100,56178,139],{"emptyLinePlaceholder":138},[100,56180,56181,56183,56185,56187,56189,56191],{"class":102,"line":142},[100,56182,372],{"class":114},[100,56184,170],{"class":118},[100,56186,9276],{"class":178},[100,56188,170],{"class":118},[100,56190,10180],{"class":122},[100,56192,182],{"class":118},[14,56194,218],{},[91,56196,56197],{"className":93,"code":25067,"language":95,"meta":96,"style":96},[17,56198,56199],{"__ignoreMap":96},[100,56200,56201],{"class":102,"line":103},[100,56202,25067],{"class":122},[14,56204,56205,56206,290],{},"Check whether you imported the full module or a specific function. If you want to learn this properly, see ",[295,56207,10137],{"href":25158},[77,56209,56211],{"id":56210},"cause-5-quotes-missing-around-a-string","Cause 5: Quotes missing around a string",[14,56213,26761],{},[14,56215,56216],{},"If you write this:",[91,56218,56219],{"className":93,"code":54620,"language":95,"meta":96,"style":96},[17,56220,56221],{"__ignoreMap":96},[100,56222,56223,56225,56227,56229],{"class":102,"line":103},[100,56224,372],{"class":114},[100,56226,170],{"class":118},[100,56228,3506],{"class":178},[100,56230,215],{"class":118},[14,56232,56233,56234,56236,56237,56239,56240,290],{},"Python thinks ",[17,56235,3506],{}," is a variable name. Since no variable called ",[17,56238,3506],{}," exists, you get a ",[17,56241,5616],{},[14,56243,218],{},[91,56245,56246],{"className":93,"code":54637,"language":95,"meta":96,"style":96},[17,56247,56248],{"__ignoreMap":96},[100,56249,56250,56252,56254,56256,56258,56260,56262,56264,56266],{"class":102,"line":103},[100,56251,5616],{"class":191},[100,56253,89],{"class":118},[100,56255,4915],{"class":106},[100,56257,1280],{"class":205},[100,56259,3506],{"class":209},[100,56261,1280],{"class":205},[100,56263,16702],{"class":110},[100,56265,4921],{"class":110},[100,56267,16707],{"class":106},[14,56269,56270],{},"To print text, use quotes:",[91,56272,56273],{"className":93,"code":54665,"language":95,"meta":96,"style":96},[17,56274,56275],{"__ignoreMap":96},[100,56276,56277,56279,56281,56283,56285,56287],{"class":102,"line":103},[100,56278,372],{"class":114},[100,56280,170],{"class":118},[100,56282,206],{"class":205},[100,56284,3506],{"class":209},[100,56286,206],{"class":205},[100,56288,215],{"class":118},[14,56290,218],{},[91,56292,56293],{"className":93,"code":6651,"language":95,"meta":96,"style":96},[17,56294,56295],{"__ignoreMap":96},[100,56296,56297],{"class":102,"line":103},[100,56298,6651],{"class":106},[14,56300,56301],{},"Use either single quotes or double quotes:",[91,56303,56305],{"className":93,"code":56304,"language":95,"meta":96,"style":96},"print('hello')\nprint(\"hello\")\n",[17,56306,56307,56321],{"__ignoreMap":96},[100,56308,56309,56311,56313,56315,56317,56319],{"class":102,"line":103},[100,56310,372],{"class":114},[100,56312,170],{"class":118},[100,56314,1280],{"class":205},[100,56316,3506],{"class":209},[100,56318,1280],{"class":205},[100,56320,215],{"class":118},[100,56322,56323,56325,56327,56329,56331,56333],{"class":102,"line":135},[100,56324,372],{"class":114},[100,56326,170],{"class":118},[100,56328,206],{"class":205},[100,56330,3506],{"class":209},[100,56332,206],{"class":205},[100,56334,215],{"class":118},[14,56336,56337],{},"Both are valid.",[77,56339,52309],{"id":52308},[14,56341,3277,56342,22668],{},[17,56343,56344],{},"NameError: name 'something' is not defined",[3282,56346,56347,56350,56353,56356,56359],{},[43,56348,56349],{},"Find the exact name shown in the error message.",[43,56351,56352],{},"Check whether that name was created earlier in the code.",[43,56354,56355],{},"Check spelling and letter case.",[43,56357,56358],{},"Check whether the name is inside the correct function or block.",[43,56360,56361],{},"Check imports if the name should come from a module.",[14,56363,56364],{},"These simple debugging commands can also help:",[91,56366,56368],{"className":93,"code":56367,"language":95,"meta":96,"style":96},"print(variable_name)\nprint(locals())\nprint(globals())\ndir()\nhelp(name)\n",[17,56369,56370,56380,56390,56400,56406],{"__ignoreMap":96},[100,56371,56372,56374,56376,56378],{"class":102,"line":103},[100,56373,372],{"class":114},[100,56375,170],{"class":118},[100,56377,53946],{"class":178},[100,56379,215],{"class":118},[100,56381,56382,56384,56386,56388],{"class":102,"line":135},[100,56383,372],{"class":114},[100,56385,170],{"class":118},[100,56387,53957],{"class":114},[100,56389,3370],{"class":118},[100,56391,56392,56394,56396,56398],{"class":102,"line":142},[100,56393,372],{"class":114},[100,56395,170],{"class":118},[100,56397,53968],{"class":114},[100,56399,3370],{"class":118},[100,56401,56402,56404],{"class":102,"line":152},[100,56403,3822],{"class":114},[100,56405,4734],{"class":118},[100,56407,56408,56410,56412,56414],{"class":102,"line":164},[100,56409,2478],{"class":114},[100,56411,170],{"class":118},[100,56413,2853],{"class":178},[100,56415,215],{"class":118},[14,56417,56418],{},"Useful notes:",[40,56420,56421,56426,56431,56437,56442],{},[43,56422,56423,56425],{},[17,56424,55276],{}," helps confirm a value exists",[43,56427,56428,54035],{},[17,56429,56430],{},"print(locals())",[43,56432,56433,56436],{},[17,56434,56435],{},"print(globals())"," shows global names",[43,56438,56439,56441],{},[17,56440,4565],{}," shows available names in the current context",[43,56443,56444,56446],{},[17,56445,55293],{}," gives information about an object if it exists",[14,56448,56449,56450,56452,56453,56455,56456,290],{},"Be careful with ",[17,56451,55276],{},": if ",[17,56454,53946],{}," does not exist, that line will also raise a ",[17,56457,5616],{},[77,56459,56461],{"id":56460},"how-this-differs-from-related-errors","How this differs from related errors",[14,56463,56464,56466],{},[17,56465,5616],{}," is easy to confuse with other Python errors.",[675,56468,5616],{"id":55071},[14,56470,56471],{},"This means the name does not exist at all in the current scope.",[91,56473,56475],{"className":93,"code":56474,"language":95,"meta":96,"style":96},"print(color)\n",[17,56476,56477],{"__ignoreMap":96},[100,56478,56479,56481,56483,56485],{"class":102,"line":103},[100,56480,372],{"class":114},[100,56482,170],{"class":118},[100,56484,38431],{"class":178},[100,56486,215],{"class":118},[14,56488,11734,56489,55100,56491,290],{},[17,56490,38431],{},[17,56492,5616],{},[675,56494,16651],{"id":55105},[14,56496,56497],{},"This is a related scope problem that often happens inside functions.",[91,56499,56500],{"className":93,"code":55117,"language":95,"meta":96,"style":96},[17,56501,56502,56510,56514,56522,56532,56540,56544],{"__ignoreMap":96},[100,56503,56504,56506,56508],{"class":102,"line":103},[100,56505,2176],{"class":106},[100,56507,111],{"class":110},[100,56509,2181],{"class":122},[100,56511,56512],{"class":102,"line":135},[100,56513,139],{"emptyLinePlaceholder":138},[100,56515,56516,56518,56520],{"class":102,"line":142},[100,56517,1078],{"class":1077},[100,56519,55138],{"class":1081},[100,56521,1085],{"class":118},[100,56523,56524,56526,56528,56530],{"class":102,"line":152},[100,56525,200],{"class":114},[100,56527,170],{"class":118},[100,56529,51844],{"class":178},[100,56531,215],{"class":118},[100,56533,56534,56536,56538],{"class":102,"line":164},[100,56535,41602],{"class":106},[100,56537,111],{"class":110},[100,56539,1669],{"class":122},[100,56541,56542],{"class":102,"line":185},[100,56543,139],{"emptyLinePlaceholder":138},[100,56545,56546,56548],{"class":102,"line":197},[100,56547,55167],{"class":178},[100,56549,4734],{"class":118},[14,56551,3513,56552,56554,56555,56557],{},[17,56553,16651],{}," because Python treats ",[17,56556,51844],{}," as a local variable inside the function, but it is used before assignment there.",[14,56559,56560,56561,290],{},"For a full explanation, see ",[295,56562,53909],{"href":53908},[675,56564,3738],{"id":56565},"attributeerror",[14,56567,56568],{},"This happens when an object exists, but it does not have the attribute or method you asked for.",[91,56570,56572],{"className":93,"code":56571,"language":95,"meta":96,"style":96},"name = \"Sam\"\nprint(name.append(\"x\"))\n",[17,56573,56574,56586],{"__ignoreMap":96},[100,56575,56576,56578,56580,56582,56584],{"class":102,"line":103},[100,56577,1691],{"class":106},[100,56579,111],{"class":110},[100,56581,1708],{"class":205},[100,56583,29329],{"class":209},[100,56585,1714],{"class":205},[100,56587,56588,56590,56592,56594,56596,56598,56600,56602,56604,56606],{"class":102,"line":135},[100,56589,372],{"class":114},[100,56591,170],{"class":118},[100,56593,2853],{"class":178},[100,56595,290],{"class":118},[100,56597,4254],{"class":178},[100,56599,170],{"class":118},[100,56601,206],{"class":205},[100,56603,39024],{"class":209},[100,56605,206],{"class":205},[100,56607,182],{"class":118},[14,56609,3513,56610,56612,56613,13361],{},[17,56611,3738],{}," because strings exist, but they do not have an ",[17,56614,7659],{},[14,56616,23054,56617,290],{},[295,56618,9381],{"href":12826},[77,56620,7117],{"id":7116},[14,56622,9001],{},[40,56624,56625,56628,56630,56633,56636,56638],{},[43,56626,56627],{},"Misspelled variable name",[43,56629,54068],{},[43,56631,56632],{},"Case mismatch in a name",[43,56634,56635],{},"Using a local variable outside its function",[43,56637,53421],{},[43,56639,56640],{},"Missing quotes around a string",[14,56642,56643,56644,290],{},"If your error looks similar but not identical, you may also want to read ",[295,56645,54200],{"href":56646},"\u002Ferrors\u002Fnameerror-in-python-causes-and-fixes\u002F",[77,56648,1514],{"id":1513},[675,56650,56652],{"id":56651},"what-does-nameerror-mean-in-python","What does NameError mean in Python?",[14,56654,56655],{},"It means Python cannot find the variable, function, or module name you used.",[675,56657,56659],{"id":56658},"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,56661,56662],{},"You probably forgot quotes, so Python treated the word as a variable name instead of a string.",[675,56664,56666],{"id":56665},"is-nameerror-the-same-as-unboundlocalerror","Is NameError the same as UnboundLocalError?",[14,56668,14506,56669,56671],{},[17,56670,16651],{}," is a more specific scope-related error that often happens inside functions.",[675,56673,56675],{"id":56674},"can-a-typo-cause-nameerror","Can a typo cause NameError?",[14,56677,56678],{},"Yes. Even a small spelling difference or wrong capitalization can cause it.",[77,56680,1554],{"id":1553},[40,56682,56683,56687,56691,56695,56699,56703],{},[43,56684,56685],{},[295,56686,53345],{"href":54746},[43,56688,56689],{},[295,56690,29749],{"href":29189},[43,56692,56693],{},[295,56694,10666],{"href":25158},[43,56696,56697],{},[295,56698,54200],{"href":56646},[43,56700,56701],{},[295,56702,53909],{"href":53908},[43,56704,56705],{},[295,56706,9381],{"href":12826},[1589,56708,56709],{},"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":96,"searchDepth":135,"depth":135,"links":56711},[56712,56713,56714,56715,56716,56717,56718,56719,56720,56721,56726,56727,56733],{"id":79,"depth":135,"text":80},{"id":5880,"depth":135,"text":5881},{"id":24957,"depth":135,"text":24958},{"id":55711,"depth":135,"text":55712},{"id":55832,"depth":135,"text":55833},{"id":55899,"depth":135,"text":55900},{"id":56059,"depth":135,"text":56060},{"id":56210,"depth":135,"text":56211},{"id":52308,"depth":135,"text":52309},{"id":56460,"depth":135,"text":56461,"children":56722},[56723,56724,56725],{"id":55071,"depth":142,"text":5616},{"id":55105,"depth":142,"text":16651},{"id":56565,"depth":142,"text":3738},{"id":7116,"depth":135,"text":7117},{"id":1513,"depth":135,"text":1514,"children":56728},[56729,56730,56731,56732],{"id":56651,"depth":142,"text":56652},{"id":56658,"depth":142,"text":56659},{"id":56665,"depth":142,"text":56666},{"id":56674,"depth":142,"text":56675},{"id":1553,"depth":135,"text":1554},"Master nameerror name is not defined fix in our comprehensive Python beginner guide.",{},{"title":55490,"description":56734},"errors\u002Fnameerror-name-is-not-defined-fix","wPiPu8-D6ajimIBbQr9322DpmxzKkTyNMGF7C9pAUN8",{"id":56740,"title":56741,"body":56742,"description":58340,"extension":1623,"meta":58341,"navigation":138,"path":23081,"seo":58342,"stem":58343,"__hash__":58344},"content\u002Ferrors\u002Fnotadirectoryerror-errno-20-not-a-directory-fix.md","NotADirectoryError: [Errno 20] Not a directory (Fix)",{"type":7,"value":56743,"toc":58305},[56744,56750,56761,56773,56775,56780,56874,56878,56895,56900,56902,56904,56920,56922,56936,56942,56944,56946,56960,56963,56974,56976,56982,57019,57021,57064,57066,57074,57077,57115,57118,57154,57159,57164,57166,57169,57173,57176,57223,57227,57321,57324,57329,57337,57344,57419,57421,57432,57438,57442,57445,57554,57557,57571,57575,57578,57582,57585,57607,57610,57620,57624,57627,57658,57661,57675,57678,57680,57683,57687,57702,57705,57709,57746,57752,57756,57815,57818,57822,57825,57853,57857,57870,57876,57880,57883,57902,57908,57942,57945,57991,57999,58001,58007,58028,58030,58135,58137,58141,58146,58151,58158,58162,58165,58169,58173,58175,58224,58227,58239,58243,58245,58255,58262,58264,58302],[10,56745,40979,56747,56749],{"id":56746},"notadirectoryerror-errno-20-not-a-directory-fix",[100,56748,40982],{}," Not a directory (Fix)",[14,56751,56752,56755,56756,56758,56759,10873],{},[17,56753,56754],{},"NotADirectoryError: [Errno 20] Not a directory"," happens when Python expects a ",[22,56757,39474],{},", but your code gives it a ",[22,56760,39478],{},[14,56762,56763,56764,5870,56766,5629,56769,56772],{},"This usually appears with functions like ",[17,56765,18259],{},[17,56767,56768],{},"os.chdir()",[17,56770,56771],{},"os.scandir()",". These functions work with directories, not files.",[77,56774,80],{"id":79},[14,56776,241,56777,56779],{},[17,56778,39878],{}," before calling a function that expects a directory:",[91,56781,56783],{"className":93,"code":56782,"language":95,"meta":96,"style":96},"import os\n\npath = \"notes.txt\"\n\nif os.path.isdir(path):\n    print(os.listdir(path))\nelse:\n    print(\"This path is not a directory:\", path)\n",[17,56784,56785,56791,56795,56807,56811,56831,56849,56855],{"__ignoreMap":96},[100,56786,56787,56789],{"class":102,"line":103},[100,56788,9259],{"class":145},[100,56790,18702],{"class":106},[100,56792,56793],{"class":102,"line":135},[100,56794,139],{"emptyLinePlaceholder":138},[100,56796,56797,56799,56801,56803,56805],{"class":102,"line":142},[100,56798,19365],{"class":106},[100,56800,111],{"class":110},[100,56802,1708],{"class":205},[100,56804,22157],{"class":209},[100,56806,1714],{"class":205},[100,56808,56809],{"class":102,"line":152},[100,56810,139],{"emptyLinePlaceholder":138},[100,56812,56813,56815,56817,56819,56821,56823,56825,56827,56829],{"class":102,"line":164},[100,56814,2736],{"class":145},[100,56816,19012],{"class":106},[100,56818,290],{"class":118},[100,56820,19017],{"class":2494},[100,56822,290],{"class":118},[100,56824,24218],{"class":178},[100,56826,170],{"class":118},[100,56828,19017],{"class":178},[100,56830,522],{"class":118},[100,56832,56833,56835,56837,56839,56841,56843,56845,56847],{"class":102,"line":185},[100,56834,200],{"class":114},[100,56836,170],{"class":118},[100,56838,18035],{"class":178},[100,56840,290],{"class":118},[100,56842,22871],{"class":178},[100,56844,170],{"class":118},[100,56846,19017],{"class":178},[100,56848,182],{"class":118},[100,56850,56851,56853],{"class":102,"line":197},[100,56852,4944],{"class":145},[100,56854,149],{"class":118},[100,56856,56857,56859,56861,56863,56866,56868,56870,56872],{"class":102,"line":771},[100,56858,200],{"class":114},[100,56860,170],{"class":118},[100,56862,206],{"class":205},[100,56864,56865],{"class":209},"This path is not a directory:",[100,56867,206],{"class":205},[100,56869,126],{"class":118},[100,56871,23294],{"class":178},[100,56873,215],{"class":118},[14,56875,56876],{},[22,56877,26197],{},[40,56879,56880,56886,56892],{},[43,56881,56882,56883,56885],{},"Checks whether ",[17,56884,19017],{}," is a directory",[43,56887,56888,56889,56891],{},"Only calls ",[17,56890,18259],{}," if the path is a folder",[43,56893,56894],{},"Avoids the error when the path points to a file",[14,56896,56897,56898,290],{},"If you need help understanding path types, see ",[295,56899,18286],{"href":18285},[77,56901,5881],{"id":5880},[14,56903,9328],{},[40,56905,56906,56909,56912],{},[43,56907,56908],{},"Python expected a directory path",[43,56910,56911],{},"You gave a file path or another non-directory path instead",[43,56913,56914,56915,3178,56917,56919],{},"A function like ",[17,56916,18259],{},[17,56918,56768],{}," cannot continue",[14,56921,12895],{},[40,56923,56924,56930],{},[43,56925,56926,56929],{},[17,56927,56928],{},"\"documents\u002F\""," is usually a directory path",[43,56931,56932,56935],{},[17,56933,56934],{},"\"notes.txt\""," is usually a file path",[14,56937,56938,56939,56941],{},"If a function needs a folder, passing ",[17,56940,56934],{}," will cause this error.",[77,56943,25091],{"id":25090},[14,56945,14889],{},[40,56947,56948,56951,56954,56957],{},[43,56949,56950],{},"A filename is passed to a function that needs a folder",[43,56952,56953],{},"Part of the path points to a file, not a directory",[43,56955,56956],{},"A variable name suggests a folder, but actually stores a file path",[43,56958,56959],{},"The path was built incorrectly",[14,56961,56962],{},"This is especially common when:",[40,56964,56965,56968,56971],{},[43,56966,56967],{},"You are joining paths by hand",[43,56969,56970],{},"You are using user input",[43,56972,56973],{},"You assume a path is a folder without checking it first",[77,56975,11031],{"id":11030},[14,56977,56978,56979,56981],{},"Suppose ",[17,56980,22157],{}," is a real file in your current folder.",[91,56983,56985],{"className":93,"code":56984,"language":95,"meta":96,"style":96},"import os\n\nprint(os.listdir(\"notes.txt\"))\n",[17,56986,56987,56993,56997],{"__ignoreMap":96},[100,56988,56989,56991],{"class":102,"line":103},[100,56990,9259],{"class":145},[100,56992,18702],{"class":106},[100,56994,56995],{"class":102,"line":135},[100,56996,139],{"emptyLinePlaceholder":138},[100,56998,56999,57001,57003,57005,57007,57009,57011,57013,57015,57017],{"class":102,"line":142},[100,57000,372],{"class":114},[100,57002,170],{"class":118},[100,57004,18035],{"class":178},[100,57006,290],{"class":118},[100,57008,22871],{"class":178},[100,57010,170],{"class":118},[100,57012,206],{"class":205},[100,57014,22157],{"class":209},[100,57016,206],{"class":205},[100,57018,182],{"class":118},[14,57020,218],{},[91,57022,57024],{"className":93,"code":57023,"language":95,"meta":96,"style":96},"Traceback (most recent call last):\n  ...\nNotADirectoryError: [Errno 20] Not a directory: 'notes.txt'\n",[17,57025,57026,57036,57040],{"__ignoreMap":96},[100,57027,57028,57030,57032,57034],{"class":102,"line":103},[100,57029,514],{"class":178},[100,57031,170],{"class":118},[100,57033,519],{"class":178},[100,57035,522],{"class":118},[100,57037,57038],{"class":102,"line":135},[100,57039,528],{"class":527},[100,57041,57042,57044,57046,57048,57050,57052,57054,57056,57058,57060,57062],{"class":102,"line":142},[100,57043,17504],{"class":191},[100,57045,89],{"class":118},[100,57047,594],{"class":118},[100,57049,17586],{"class":106},[100,57051,6152],{"class":122},[100,57053,17591],{"class":118},[100,57055,40983],{"class":106},[100,57057,89],{"class":118},[100,57059,1274],{"class":205},[100,57061,22157],{"class":209},[100,57063,3925],{"class":205},[675,57065,35438],{"id":35437},[14,57067,57068,57070,57071,290],{},[17,57069,18259],{}," lists the contents of a ",[22,57072,57073],{},"directory",[14,57075,57076],{},"It can do this:",[91,57078,57080],{"className":93,"code":57079,"language":95,"meta":96,"style":96},"import os\n\nprint(os.listdir(\"documents\"))\n",[17,57081,57082,57088,57092],{"__ignoreMap":96},[100,57083,57084,57086],{"class":102,"line":103},[100,57085,9259],{"class":145},[100,57087,18702],{"class":106},[100,57089,57090],{"class":102,"line":135},[100,57091,139],{"emptyLinePlaceholder":138},[100,57093,57094,57096,57098,57100,57102,57104,57106,57108,57111,57113],{"class":102,"line":142},[100,57095,372],{"class":114},[100,57097,170],{"class":118},[100,57099,18035],{"class":178},[100,57101,290],{"class":118},[100,57103,22871],{"class":178},[100,57105,170],{"class":118},[100,57107,206],{"class":205},[100,57109,57110],{"class":209},"documents",[100,57112,206],{"class":205},[100,57114,182],{"class":118},[14,57116,57117],{},"But it cannot do this:",[91,57119,57120],{"className":93,"code":56984,"language":95,"meta":96,"style":96},[17,57121,57122,57128,57132],{"__ignoreMap":96},[100,57123,57124,57126],{"class":102,"line":103},[100,57125,9259],{"class":145},[100,57127,18702],{"class":106},[100,57129,57130],{"class":102,"line":135},[100,57131,139],{"emptyLinePlaceholder":138},[100,57133,57134,57136,57138,57140,57142,57144,57146,57148,57150,57152],{"class":102,"line":142},[100,57135,372],{"class":114},[100,57137,170],{"class":118},[100,57139,18035],{"class":178},[100,57141,290],{"class":118},[100,57143,22871],{"class":178},[100,57145,170],{"class":118},[100,57147,206],{"class":205},[100,57149,22157],{"class":209},[100,57151,206],{"class":205},[100,57153,182],{"class":118},[14,57155,14478,57156,57158],{},[17,57157,22157],{}," is a file, not a folder.",[14,57160,57161,57162,290],{},"If you want to learn more about this function, see ",[295,57163,40580],{"href":40579},[77,57165,6244],{"id":6243},[14,57167,57168],{},"There are several ways to fix this error.",[675,57170,57172],{"id":57171},"pass-the-correct-folder-path","Pass the correct folder path",[14,57174,57175],{},"If you meant to list a folder, pass the folder path:",[91,57177,57179],{"className":93,"code":57178,"language":95,"meta":96,"style":96},"import os\n\nfolder_path = \"documents\"\nprint(os.listdir(folder_path))\n",[17,57180,57181,57187,57191,57204],{"__ignoreMap":96},[100,57182,57183,57185],{"class":102,"line":103},[100,57184,9259],{"class":145},[100,57186,18702],{"class":106},[100,57188,57189],{"class":102,"line":135},[100,57190,139],{"emptyLinePlaceholder":138},[100,57192,57193,57196,57198,57200,57202],{"class":102,"line":142},[100,57194,57195],{"class":106},"folder_path ",[100,57197,111],{"class":110},[100,57199,1708],{"class":205},[100,57201,57110],{"class":209},[100,57203,1714],{"class":205},[100,57205,57206,57208,57210,57212,57214,57216,57218,57221],{"class":102,"line":152},[100,57207,372],{"class":114},[100,57209,170],{"class":118},[100,57211,18035],{"class":178},[100,57213,290],{"class":118},[100,57215,22871],{"class":178},[100,57217,170],{"class":118},[100,57219,57220],{"class":178},"folder_path",[100,57222,182],{"class":118},[675,57224,57226],{"id":57225},"check-the-path-before-using-it","Check the path before using it",[91,57228,57230],{"className":93,"code":57229,"language":95,"meta":96,"style":96},"import os\n\npath = \"notes.txt\"\n\nif os.path.isdir(path):\n    print(os.listdir(path))\nelse:\n    print(\"Expected a directory, but got:\", path)\n",[17,57231,57232,57238,57242,57254,57258,57278,57296,57302],{"__ignoreMap":96},[100,57233,57234,57236],{"class":102,"line":103},[100,57235,9259],{"class":145},[100,57237,18702],{"class":106},[100,57239,57240],{"class":102,"line":135},[100,57241,139],{"emptyLinePlaceholder":138},[100,57243,57244,57246,57248,57250,57252],{"class":102,"line":142},[100,57245,19365],{"class":106},[100,57247,111],{"class":110},[100,57249,1708],{"class":205},[100,57251,22157],{"class":209},[100,57253,1714],{"class":205},[100,57255,57256],{"class":102,"line":152},[100,57257,139],{"emptyLinePlaceholder":138},[100,57259,57260,57262,57264,57266,57268,57270,57272,57274,57276],{"class":102,"line":164},[100,57261,2736],{"class":145},[100,57263,19012],{"class":106},[100,57265,290],{"class":118},[100,57267,19017],{"class":2494},[100,57269,290],{"class":118},[100,57271,24218],{"class":178},[100,57273,170],{"class":118},[100,57275,19017],{"class":178},[100,57277,522],{"class":118},[100,57279,57280,57282,57284,57286,57288,57290,57292,57294],{"class":102,"line":185},[100,57281,200],{"class":114},[100,57283,170],{"class":118},[100,57285,18035],{"class":178},[100,57287,290],{"class":118},[100,57289,22871],{"class":178},[100,57291,170],{"class":118},[100,57293,19017],{"class":178},[100,57295,182],{"class":118},[100,57297,57298,57300],{"class":102,"line":197},[100,57299,4944],{"class":145},[100,57301,149],{"class":118},[100,57303,57304,57306,57308,57310,57313,57315,57317,57319],{"class":102,"line":771},[100,57305,200],{"class":114},[100,57307,170],{"class":118},[100,57309,206],{"class":205},[100,57311,57312],{"class":209},"Expected a directory, but got:",[100,57314,206],{"class":205},[100,57316,126],{"class":118},[100,57318,23294],{"class":178},[100,57320,215],{"class":118},[14,57322,57323],{},"This is one of the safest beginner-friendly fixes.",[14,57325,4424,57326,57328],{},[295,57327,22125],{"href":19525}," to check whether the path exists at all.",[675,57330,241,57332,41027,57334,57336],{"id":57331},"use-open-for-files-and-oslistdir-for-folders",[17,57333,18032],{},[17,57335,18259],{}," for folders",[14,57338,57339,57340,15957,57342,89],{},"If your path is a file, use ",[17,57341,18032],{},[17,57343,18259],{},[91,57345,57347],{"className":93,"code":57346,"language":95,"meta":96,"style":96},"path = \"notes.txt\"\n\nwith open(path, \"r\") as file:\n    content = file.read()\n\nprint(content)\n",[17,57348,57349,57361,57365,57391,57405,57409],{"__ignoreMap":96},[100,57350,57351,57353,57355,57357,57359],{"class":102,"line":103},[100,57352,19365],{"class":106},[100,57354,111],{"class":110},[100,57356,1708],{"class":205},[100,57358,22157],{"class":209},[100,57360,1714],{"class":205},[100,57362,57363],{"class":102,"line":135},[100,57364,139],{"emptyLinePlaceholder":138},[100,57366,57367,57369,57371,57373,57375,57377,57379,57381,57383,57385,57387,57389],{"class":102,"line":142},[100,57368,17521],{"class":145},[100,57370,17524],{"class":114},[100,57372,170],{"class":118},[100,57374,19017],{"class":178},[100,57376,126],{"class":118},[100,57378,1708],{"class":205},[100,57380,17540],{"class":209},[100,57382,206],{"class":205},[100,57384,6155],{"class":118},[100,57386,16224],{"class":145},[100,57388,17550],{"class":17549},[100,57390,149],{"class":118},[100,57392,57393,57395,57397,57399,57401,57403],{"class":102,"line":152},[100,57394,18414],{"class":106},[100,57396,111],{"class":110},[100,57398,17550],{"class":17549},[100,57400,290],{"class":118},[100,57402,17566],{"class":178},[100,57404,4734],{"class":118},[100,57406,57407],{"class":102,"line":164},[100,57408,139],{"emptyLinePlaceholder":138},[100,57410,57411,57413,57415,57417],{"class":102,"line":185},[100,57412,372],{"class":114},[100,57414,170],{"class":118},[100,57416,18182],{"class":178},[100,57418,215],{"class":118},[14,57420,27749],{},[40,57422,57423,57428],{},[43,57424,57425,57427],{},[17,57426,18032],{}," for files",[43,57429,57430,57336],{},[17,57431,18259],{},[14,57433,57434,57435,290],{},"If needed, read more about the ",[295,57436,57437],{"href":18280},"Python open() function",[675,57439,57441],{"id":57440},"print-the-path-value","Print the path value",[14,57443,57444],{},"A very common problem is that the variable contains something different from what you expected.",[91,57446,57448],{"className":93,"code":57447,"language":95,"meta":96,"style":96},"import os\n\npath = \"notes.txt\"\nprint(\"Path value:\", path)\n\nif os.path.isdir(path):\n    print(os.listdir(path))\nelse:\n    print(\"Not a directory\")\n",[17,57449,57450,57456,57460,57472,57491,57495,57515,57533,57539],{"__ignoreMap":96},[100,57451,57452,57454],{"class":102,"line":103},[100,57453,9259],{"class":145},[100,57455,18702],{"class":106},[100,57457,57458],{"class":102,"line":135},[100,57459,139],{"emptyLinePlaceholder":138},[100,57461,57462,57464,57466,57468,57470],{"class":102,"line":142},[100,57463,19365],{"class":106},[100,57465,111],{"class":110},[100,57467,1708],{"class":205},[100,57469,22157],{"class":209},[100,57471,1714],{"class":205},[100,57473,57474,57476,57478,57480,57483,57485,57487,57489],{"class":102,"line":152},[100,57475,372],{"class":114},[100,57477,170],{"class":118},[100,57479,206],{"class":205},[100,57481,57482],{"class":209},"Path value:",[100,57484,206],{"class":205},[100,57486,126],{"class":118},[100,57488,23294],{"class":178},[100,57490,215],{"class":118},[100,57492,57493],{"class":102,"line":164},[100,57494,139],{"emptyLinePlaceholder":138},[100,57496,57497,57499,57501,57503,57505,57507,57509,57511,57513],{"class":102,"line":185},[100,57498,2736],{"class":145},[100,57500,19012],{"class":106},[100,57502,290],{"class":118},[100,57504,19017],{"class":2494},[100,57506,290],{"class":118},[100,57508,24218],{"class":178},[100,57510,170],{"class":118},[100,57512,19017],{"class":178},[100,57514,522],{"class":118},[100,57516,57517,57519,57521,57523,57525,57527,57529,57531],{"class":102,"line":197},[100,57518,200],{"class":114},[100,57520,170],{"class":118},[100,57522,18035],{"class":178},[100,57524,290],{"class":118},[100,57526,22871],{"class":178},[100,57528,170],{"class":118},[100,57530,19017],{"class":178},[100,57532,182],{"class":118},[100,57534,57535,57537],{"class":102,"line":771},[100,57536,4944],{"class":145},[100,57538,149],{"class":118},[100,57540,57541,57543,57545,57547,57550,57552],{"class":102,"line":787},[100,57542,200],{"class":114},[100,57544,170],{"class":118},[100,57546,206],{"class":205},[100,57548,57549],{"class":209},"Not a directory",[100,57551,206],{"class":205},[100,57553,215],{"class":118},[14,57555,57556],{},"Printing the path often helps you spot:",[40,57558,57559,57562,57565,57568],{},[43,57560,57561],{},"wrong filenames",[43,57563,57564],{},"missing folder names",[43,57566,57567],{},"incorrect path building",[43,57569,57570],{},"unexpected user input",[77,57572,57574],{"id":57573},"file-path-vs-directory-path","File path vs directory path",[14,57576,57577],{},"Understanding this difference helps prevent the error.",[675,57579,57581],{"id":57580},"file-path","File path",[14,57583,57584],{},"A file path points to one file:",[91,57586,57588],{"className":93,"code":57587,"language":95,"meta":96,"style":96},"\"data.txt\"\n\"images\u002Fphoto.jpg\"\n",[17,57589,57590,57598],{"__ignoreMap":96},[100,57591,57592,57594,57596],{"class":102,"line":103},[100,57593,206],{"class":3553},[100,57595,18094],{"class":3556},[100,57597,1714],{"class":3553},[100,57599,57600,57602,57605],{"class":102,"line":135},[100,57601,206],{"class":3553},[100,57603,57604],{"class":3556},"images\u002Fphoto.jpg",[100,57606,1714],{"class":3553},[14,57608,57609],{},"You usually use file paths with functions like:",[40,57611,57612,57616],{},[43,57613,57614],{},[17,57615,18032],{},[43,57617,57618],{},[17,57619,39872],{},[675,57621,57623],{"id":57622},"directory-path","Directory path",[14,57625,57626],{},"A directory path points to a folder:",[91,57628,57630],{"className":93,"code":57629,"language":95,"meta":96,"style":96},"\"documents\"\n\"images\"\n\"projects\u002Fpython\"\n",[17,57631,57632,57640,57649],{"__ignoreMap":96},[100,57633,57634,57636,57638],{"class":102,"line":103},[100,57635,206],{"class":3553},[100,57637,57110],{"class":3556},[100,57639,1714],{"class":3553},[100,57641,57642,57644,57647],{"class":102,"line":135},[100,57643,206],{"class":3553},[100,57645,57646],{"class":3556},"images",[100,57648,1714],{"class":3553},[100,57650,57651,57653,57656],{"class":102,"line":142},[100,57652,206],{"class":3553},[100,57654,57655],{"class":3556},"projects\u002Fpython",[100,57657,1714],{"class":3553},[14,57659,57660],{},"You usually use directory paths with functions like:",[40,57662,57663,57667,57671],{},[43,57664,57665],{},[17,57666,18259],{},[43,57668,57669],{},[17,57670,56768],{},[43,57672,57673],{},[17,57674,56771],{},[14,57676,57677],{},"Different Python functions expect different path types. If you mix them up, errors happen.",[77,57679,6924],{"id":6923},[14,57681,57682],{},"If you get this error, check these things in order.",[675,57684,57686],{"id":57685},"_1-print-the-path","1. Print the path",[91,57688,57690],{"className":93,"code":57689,"language":95,"meta":96,"style":96},"print(path)\n",[17,57691,57692],{"__ignoreMap":96},[100,57693,57694,57696,57698,57700],{"class":102,"line":103},[100,57695,372],{"class":114},[100,57697,170],{"class":118},[100,57699,19017],{"class":178},[100,57701,215],{"class":118},[14,57703,57704],{},"Make sure the value is really what you think it is.",[675,57706,57708],{"id":57707},"_2-check-whether-the-path-exists","2. Check whether the path exists",[91,57710,57712],{"className":93,"code":57711,"language":95,"meta":96,"style":96},"import os\n\nprint(os.path.exists(path))\n",[17,57713,57714,57720,57724],{"__ignoreMap":96},[100,57715,57716,57718],{"class":102,"line":103},[100,57717,9259],{"class":145},[100,57719,18702],{"class":106},[100,57721,57722],{"class":102,"line":135},[100,57723,139],{"emptyLinePlaceholder":138},[100,57725,57726,57728,57730,57732,57734,57736,57738,57740,57742,57744],{"class":102,"line":142},[100,57727,372],{"class":114},[100,57729,170],{"class":118},[100,57731,18035],{"class":178},[100,57733,290],{"class":118},[100,57735,19017],{"class":2494},[100,57737,290],{"class":118},[100,57739,18114],{"class":178},[100,57741,170],{"class":118},[100,57743,19017],{"class":178},[100,57745,182],{"class":118},[14,57747,57748,57749,57751],{},"If it returns ",[17,57750,1649],{},", the path is wrong or missing.",[675,57753,57755],{"id":57754},"_3-check-whether-it-is-a-file-or-directory","3. Check whether it is a file or directory",[91,57757,57759],{"className":93,"code":57758,"language":95,"meta":96,"style":96},"import os\n\nprint(os.path.isfile(path))\nprint(os.path.isdir(path))\n",[17,57760,57761,57767,57771,57793],{"__ignoreMap":96},[100,57762,57763,57765],{"class":102,"line":103},[100,57764,9259],{"class":145},[100,57766,18702],{"class":106},[100,57768,57769],{"class":102,"line":135},[100,57770,139],{"emptyLinePlaceholder":138},[100,57772,57773,57775,57777,57779,57781,57783,57785,57787,57789,57791],{"class":102,"line":142},[100,57774,372],{"class":114},[100,57776,170],{"class":118},[100,57778,18035],{"class":178},[100,57780,290],{"class":118},[100,57782,19017],{"class":2494},[100,57784,290],{"class":118},[100,57786,24191],{"class":178},[100,57788,170],{"class":118},[100,57790,19017],{"class":178},[100,57792,182],{"class":118},[100,57794,57795,57797,57799,57801,57803,57805,57807,57809,57811,57813],{"class":102,"line":152},[100,57796,372],{"class":114},[100,57798,170],{"class":118},[100,57800,18035],{"class":178},[100,57802,290],{"class":118},[100,57804,19017],{"class":2494},[100,57806,290],{"class":118},[100,57808,24218],{"class":178},[100,57810,170],{"class":118},[100,57812,19017],{"class":178},[100,57814,182],{"class":118},[14,57816,57817],{},"This tells you what the path actually points to.",[675,57819,57821],{"id":57820},"_4-check-your-current-working-directory","4. Check your current working directory",[14,57823,57824],{},"Sometimes the path is relative, and Python is looking in a different place than you expect.",[91,57826,57827],{"className":93,"code":18693,"language":95,"meta":96,"style":96},[17,57828,57829,57835,57839],{"__ignoreMap":96},[100,57830,57831,57833],{"class":102,"line":103},[100,57832,9259],{"class":145},[100,57834,18702],{"class":106},[100,57836,57837],{"class":102,"line":135},[100,57838,139],{"emptyLinePlaceholder":138},[100,57840,57841,57843,57845,57847,57849,57851],{"class":102,"line":142},[100,57842,372],{"class":114},[100,57844,170],{"class":118},[100,57846,18035],{"class":178},[100,57848,290],{"class":118},[100,57850,18719],{"class":178},[100,57852,3370],{"class":118},[675,57854,57856],{"id":57855},"_5-make-sure-you-are-using-the-right-function","5. Make sure you are using the right function",[40,57858,57859,57864],{},[43,57860,57861,57862],{},"If the path is a file, use ",[17,57863,18032],{},[43,57865,57866,57867,57869],{},"If the path is a folder, use ",[17,57868,18259],{}," or another directory function",[14,57871,57872,57873,290],{},"If you need a full overview of these tools, see the ",[295,57874,57875],{"href":18733},"Python os module overview",[77,57877,57879],{"id":57878},"common-places-this-appears","Common places this appears",[14,57881,57882],{},"You will often see this error in code like:",[40,57884,57885,57889,57894,57899],{},[43,57886,57887],{},[17,57888,40747],{},[43,57890,57891],{},[17,57892,57893],{},"os.chdir(path)",[43,57895,57896],{},[17,57897,57898],{},"os.scandir(path)",[43,57900,57901],{},"Any code that expects a folder but receives a file path",[14,57903,57904,57905,57907],{},"For example, this will also fail if ",[17,57906,18326],{}," is a file:",[91,57909,57911],{"className":93,"code":57910,"language":95,"meta":96,"style":96},"import os\n\nos.chdir(\"report.txt\")\n",[17,57912,57913,57919,57923],{"__ignoreMap":96},[100,57914,57915,57917],{"class":102,"line":103},[100,57916,9259],{"class":145},[100,57918,18702],{"class":106},[100,57920,57921],{"class":102,"line":135},[100,57922,139],{"emptyLinePlaceholder":138},[100,57924,57925,57927,57929,57932,57934,57936,57938,57940],{"class":102,"line":142},[100,57926,18035],{"class":106},[100,57928,290],{"class":118},[100,57930,57931],{"class":178},"chdir",[100,57933,170],{"class":118},[100,57935,206],{"class":205},[100,57937,18326],{"class":209},[100,57939,206],{"class":205},[100,57941,215],{"class":118},[14,57943,57944],{},"And this can fail when part of the path is a file:",[91,57946,57948],{"className":93,"code":57947,"language":95,"meta":96,"style":96},"import os\n\npath = \"notes.txt\u002Farchive\"\nprint(os.listdir(path))\n",[17,57949,57950,57956,57960,57973],{"__ignoreMap":96},[100,57951,57952,57954],{"class":102,"line":103},[100,57953,9259],{"class":145},[100,57955,18702],{"class":106},[100,57957,57958],{"class":102,"line":135},[100,57959,139],{"emptyLinePlaceholder":138},[100,57961,57962,57964,57966,57968,57971],{"class":102,"line":142},[100,57963,19365],{"class":106},[100,57965,111],{"class":110},[100,57967,1708],{"class":205},[100,57969,57970],{"class":209},"notes.txt\u002Farchive",[100,57972,1714],{"class":205},[100,57974,57975,57977,57979,57981,57983,57985,57987,57989],{"class":102,"line":152},[100,57976,372],{"class":114},[100,57978,170],{"class":118},[100,57980,18035],{"class":178},[100,57982,290],{"class":118},[100,57984,22871],{"class":178},[100,57986,170],{"class":118},[100,57988,19017],{"class":178},[100,57990,182],{"class":118},[14,57992,11734,57993,57995,57996,290],{},[17,57994,22157],{}," is a file, Python cannot treat it like a folder containing ",[17,57997,57998],{},"archive",[77,58000,7117],{"id":7116},[14,58002,58003,58004,58006],{},"Common causes of ",[17,58005,17504],{}," include:",[40,58008,58009,58014,58019,58022,58025],{},[43,58010,315,58011,58013],{},[17,58012,18259],{}," on a file",[43,58015,315,58016,58018],{},[17,58017,56768],{}," with a filename",[43,58020,58021],{},"Joining paths incorrectly",[43,58023,58024],{},"Confusing a file path variable with a folder path variable",[43,58026,58027],{},"Using user input without checking whether it is a directory",[14,58029,5295],{},[91,58031,58033],{"className":93,"code":58032,"language":95,"meta":96,"style":96},"print(path)\n\nimport os\nprint(os.path.exists(path))\nprint(os.path.isfile(path))\nprint(os.path.isdir(path))\nprint(os.getcwd())\n",[17,58034,58035,58045,58049,58055,58077,58099,58121],{"__ignoreMap":96},[100,58036,58037,58039,58041,58043],{"class":102,"line":103},[100,58038,372],{"class":114},[100,58040,170],{"class":118},[100,58042,19017],{"class":178},[100,58044,215],{"class":118},[100,58046,58047],{"class":102,"line":135},[100,58048,139],{"emptyLinePlaceholder":138},[100,58050,58051,58053],{"class":102,"line":142},[100,58052,9259],{"class":145},[100,58054,18702],{"class":106},[100,58056,58057,58059,58061,58063,58065,58067,58069,58071,58073,58075],{"class":102,"line":152},[100,58058,372],{"class":114},[100,58060,170],{"class":118},[100,58062,18035],{"class":178},[100,58064,290],{"class":118},[100,58066,19017],{"class":2494},[100,58068,290],{"class":118},[100,58070,18114],{"class":178},[100,58072,170],{"class":118},[100,58074,19017],{"class":178},[100,58076,182],{"class":118},[100,58078,58079,58081,58083,58085,58087,58089,58091,58093,58095,58097],{"class":102,"line":164},[100,58080,372],{"class":114},[100,58082,170],{"class":118},[100,58084,18035],{"class":178},[100,58086,290],{"class":118},[100,58088,19017],{"class":2494},[100,58090,290],{"class":118},[100,58092,24191],{"class":178},[100,58094,170],{"class":118},[100,58096,19017],{"class":178},[100,58098,182],{"class":118},[100,58100,58101,58103,58105,58107,58109,58111,58113,58115,58117,58119],{"class":102,"line":185},[100,58102,372],{"class":114},[100,58104,170],{"class":118},[100,58106,18035],{"class":178},[100,58108,290],{"class":118},[100,58110,19017],{"class":2494},[100,58112,290],{"class":118},[100,58114,24218],{"class":178},[100,58116,170],{"class":118},[100,58118,19017],{"class":178},[100,58120,182],{"class":118},[100,58122,58123,58125,58127,58129,58131,58133],{"class":102,"line":197},[100,58124,372],{"class":114},[100,58126,170],{"class":118},[100,58128,18035],{"class":178},[100,58130,290],{"class":118},[100,58132,18719],{"class":178},[100,58134,3370],{"class":118},[77,58136,1514],{"id":1513},[675,58138,58140],{"id":58139},"what-is-the-difference-between-notadirectoryerror-and-filenotfounderror","What is the difference between NotADirectoryError and FileNotFoundError?",[14,58142,58143,58145],{},[17,58144,17504],{}," means the path exists, but it is not a folder.",[14,58147,58148,58150],{},[17,58149,17489],{}," means the path does not exist.",[14,58152,58153,58154,290],{},"For that related case, see ",[295,58155,18016,58156,40974],{"href":17620},[100,58157,18019],{},[675,58159,58161],{"id":58160},"can-a-file-path-cause-notadirectoryerror","Can a file path cause NotADirectoryError?",[14,58163,58164],{},"Yes. If a function expects a directory and you pass a file path, Python raises this error.",[675,58166,58168],{"id":58167},"how-do-i-check-if-a-path-is-a-directory","How do I check if a path is a directory?",[14,58170,241,58171,290],{},[17,58172,40104],{},[14,58174,1844],{},[91,58176,58178],{"className":93,"code":58177,"language":95,"meta":96,"style":96},"import os\n\npath = \"documents\"\nprint(os.path.isdir(path))\n",[17,58179,58180,58186,58190,58202],{"__ignoreMap":96},[100,58181,58182,58184],{"class":102,"line":103},[100,58183,9259],{"class":145},[100,58185,18702],{"class":106},[100,58187,58188],{"class":102,"line":135},[100,58189,139],{"emptyLinePlaceholder":138},[100,58191,58192,58194,58196,58198,58200],{"class":102,"line":142},[100,58193,19365],{"class":106},[100,58195,111],{"class":110},[100,58197,1708],{"class":205},[100,58199,57110],{"class":209},[100,58201,1714],{"class":205},[100,58203,58204,58206,58208,58210,58212,58214,58216,58218,58220,58222],{"class":102,"line":152},[100,58205,372],{"class":114},[100,58207,170],{"class":118},[100,58209,18035],{"class":178},[100,58211,290],{"class":118},[100,58213,19017],{"class":2494},[100,58215,290],{"class":118},[100,58217,24218],{"class":178},[100,58219,170],{"class":118},[100,58221,19017],{"class":178},[100,58223,182],{"class":118},[14,58225,58226],{},"It returns:",[40,58228,58229,58234],{},[43,58230,58231,58233],{},[17,58232,19246],{}," if the path is a directory",[43,58235,58236,58238],{},[17,58237,1649],{}," otherwise",[675,58240,58242],{"id":58241},"should-i-use-open-or-oslistdir","Should I use open() or os.listdir()?",[14,58244,27749],{},[40,58246,58247,58251],{},[43,58248,58249,57427],{},[17,58250,18032],{},[43,58252,58253,57336],{},[17,58254,18259],{},[14,58256,58257,58258,290],{},"Using the wrong one can cause errors like this one or the related ",[295,58259,20632,58260,39773],{"href":20631},[100,58261,20635],{},[77,58263,1554],{"id":1553},[40,58265,58266,58270,58274,58278,58282,58286,58290,58296],{},[43,58267,58268],{},[295,58269,20596],{"href":18285},[43,58271,58272],{},[295,58273,41058],{"href":40579},[43,58275,58276],{},[295,58277,24741],{"href":19525},[43,58279,58280],{},[295,58281,23157],{"href":22121},[43,58283,58284],{},[295,58285,24736],{"href":18280},[43,58287,58288],{},[295,58289,57875],{"href":18733},[43,58291,58292],{},[295,58293,20632,58294,39773],{"href":20631},[100,58295,20635],{},[43,58297,58298],{},[295,58299,18016,58300,40974],{"href":17620},[100,58301,18019],{},[1589,58303,58304],{},"html pre.shiki code .sVHd0, html code.shiki .sVHd0{--shiki-light:#39ADB5;--shiki-light-font-style:italic;--shiki-default:#D73A49;--shiki-default-font-style:inherit;--shiki-dark:#F97583;--shiki-dark-font-style:inherit}html pre.shiki code .su5hD, html code.shiki .su5hD{--shiki-light:#90A4AE;--shiki-default:#24292E;--shiki-dark:#E1E4E8}html pre.shiki code .smGrS, html code.shiki .smGrS{--shiki-light:#39ADB5;--shiki-default:#D73A49;--shiki-dark:#F97583}html pre.shiki code .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 .sP7_E, html code.shiki .sP7_E{--shiki-light:#39ADB5;--shiki-default:#24292E;--shiki-dark:#E1E4E8}html pre.shiki code .skxfh, html code.shiki .skxfh{--shiki-light:#E53935;--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 pre.shiki code .sptTA, html code.shiki .sptTA{--shiki-light:#6182B8;--shiki-default:#005CC5;--shiki-dark:#79B8FF}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 .s_hVV, html code.shiki .s_hVV{--shiki-light:#90A4AE;--shiki-default:#005CC5;--shiki-dark:#79B8FF}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 .sMMDD, html code.shiki .sMMDD{--shiki-light:#90A4AE;--shiki-default:#E36209;--shiki-dark:#FFAB70}html pre.shiki code .s2W-s, html code.shiki .s2W-s{--shiki-light:#39ADB5;--shiki-light-font-style:italic;--shiki-default:#032F62;--shiki-default-font-style:inherit;--shiki-dark:#9ECBFF;--shiki-dark-font-style:inherit}html pre.shiki code .sithA, html code.shiki .sithA{--shiki-light:#90A4AE;--shiki-light-font-style:italic;--shiki-default:#032F62;--shiki-default-font-style:inherit;--shiki-dark:#9ECBFF;--shiki-dark-font-style:inherit}",{"title":96,"searchDepth":135,"depth":135,"links":58306},[58307,58308,58309,58310,58313,58320,58324,58331,58332,58333,58339],{"id":79,"depth":135,"text":80},{"id":5880,"depth":135,"text":5881},{"id":25090,"depth":135,"text":25091},{"id":11030,"depth":135,"text":11031,"children":58311},[58312],{"id":35437,"depth":142,"text":35438},{"id":6243,"depth":135,"text":6244,"children":58314},[58315,58316,58317,58319],{"id":57171,"depth":142,"text":57172},{"id":57225,"depth":142,"text":57226},{"id":57331,"depth":142,"text":58318},"Use open() for files and os.listdir() for folders",{"id":57440,"depth":142,"text":57441},{"id":57573,"depth":135,"text":57574,"children":58321},[58322,58323],{"id":57580,"depth":142,"text":57581},{"id":57622,"depth":142,"text":57623},{"id":6923,"depth":135,"text":6924,"children":58325},[58326,58327,58328,58329,58330],{"id":57685,"depth":142,"text":57686},{"id":57707,"depth":142,"text":57708},{"id":57754,"depth":142,"text":57755},{"id":57820,"depth":142,"text":57821},{"id":57855,"depth":142,"text":57856},{"id":57878,"depth":135,"text":57879},{"id":7116,"depth":135,"text":7117},{"id":1513,"depth":135,"text":1514,"children":58334},[58335,58336,58337,58338],{"id":58139,"depth":142,"text":58140},{"id":58160,"depth":142,"text":58161},{"id":58167,"depth":142,"text":58168},{"id":58241,"depth":142,"text":58242},{"id":1553,"depth":135,"text":1554},"Master notadirectoryerror errno 20 not a directory fix in our comprehensive Python beginner guide.",{},{"title":56741,"description":58340},"errors\u002Fnotadirectoryerror-errno-20-not-a-directory-fix","XHMv1RogmNU4X2Ue8cZNNXoSpnB_83IjvitYrgBJeQM",{"id":58346,"title":58347,"body":58348,"description":59576,"extension":1623,"meta":59577,"navigation":138,"path":59578,"seo":59579,"stem":59580,"__hash__":59581},"content\u002Ferrors\u002Foserror-in-python-causes-and-fixes.md","OSError in Python: Causes and Fixes",{"type":7,"value":58349,"toc":59559},[58350,58353,58358,58361,58374,58377,58390,58393,58522,58528,58532,58537,58540,58554,58560,58578,58584,58588,58593,58613,58615,58637,58641,58644,58650,58668,58671,58693,58697,58700,58729,58731,58754,58760,58764,58767,58838,58840,58874,58882,58890,58894,58898,58903,59010,59013,59016,59054,59057,59147,59150,59152,59155,59280,59283,59311,59314,59419,59422,59429,59433,59438,59441,59444,59462,59465,59475,59478,59494,59496,59500,59506,59510,59513,59518,59522,59525,59529,59532,59534,59556],[10,58351,58347],{"id":58352},"oserror-in-python-causes-and-fixes",[14,58354,58355,58357],{},[17,58356,17509],{}," is a general Python error for operating system related problems.",[14,58359,58360],{},"You will often see it when your code works with:",[40,58362,58363,58365,58368,58371],{},[43,58364,18915],{},[43,58366,58367],{},"folders",[43,58369,58370],{},"paths",[43,58372,58373],{},"permissions",[14,58375,58376],{},"For beginners, the most important thing to know is this:",[40,58378,58379,58384],{},[43,58380,58381,58383],{},[17,58382,17509],{}," usually means something is wrong with the path, file, folder, or access",[43,58385,58386,58387,58389],{},"the error message after ",[17,58388,17509],{}," usually tells you what the real problem is",[14,58391,58392],{},"A quick way to start debugging is to check whether the path actually exists before using it.",[91,58394,58396],{"className":93,"code":58395,"language":95,"meta":96,"style":96},"import os\n\npath = 'example.txt'\n\nif os.path.exists(path):\n    with open(path, 'r', encoding='utf-8') as file:\n        print(file.read())\nelse:\n    print('File not found:', path)\n",[17,58397,58398,58404,58408,58421,58425,58445,58483,58497,58503],{"__ignoreMap":96},[100,58399,58400,58402],{"class":102,"line":103},[100,58401,9259],{"class":145},[100,58403,18702],{"class":106},[100,58405,58406],{"class":102,"line":135},[100,58407,139],{"emptyLinePlaceholder":138},[100,58409,58410,58412,58414,58416,58419],{"class":102,"line":142},[100,58411,19365],{"class":106},[100,58413,111],{"class":110},[100,58415,1274],{"class":205},[100,58417,58418],{"class":209},"example.txt",[100,58420,3925],{"class":205},[100,58422,58423],{"class":102,"line":152},[100,58424,139],{"emptyLinePlaceholder":138},[100,58426,58427,58429,58431,58433,58435,58437,58439,58441,58443],{"class":102,"line":164},[100,58428,2736],{"class":145},[100,58430,19012],{"class":106},[100,58432,290],{"class":118},[100,58434,19017],{"class":2494},[100,58436,290],{"class":118},[100,58438,18114],{"class":178},[100,58440,170],{"class":118},[100,58442,19017],{"class":178},[100,58444,522],{"class":118},[100,58446,58447,58449,58451,58453,58455,58457,58459,58461,58463,58465,58467,58469,58471,58473,58475,58477,58479,58481],{"class":102,"line":185},[100,58448,18121],{"class":145},[100,58450,17524],{"class":114},[100,58452,170],{"class":118},[100,58454,19017],{"class":178},[100,58456,126],{"class":118},[100,58458,1274],{"class":205},[100,58460,17540],{"class":209},[100,58462,1280],{"class":205},[100,58464,126],{"class":118},[100,58466,18142],{"class":18141},[100,58468,111],{"class":110},[100,58470,1280],{"class":205},[100,58472,15307],{"class":209},[100,58474,1280],{"class":205},[100,58476,6155],{"class":118},[100,58478,16224],{"class":145},[100,58480,17550],{"class":17549},[100,58482,149],{"class":118},[100,58484,58485,58487,58489,58491,58493,58495],{"class":102,"line":197},[100,58486,167],{"class":114},[100,58488,170],{"class":118},[100,58490,17597],{"class":17549},[100,58492,290],{"class":118},[100,58494,17566],{"class":178},[100,58496,3370],{"class":118},[100,58498,58499,58501],{"class":102,"line":771},[100,58500,4944],{"class":145},[100,58502,149],{"class":118},[100,58504,58505,58507,58509,58511,58514,58516,58518,58520],{"class":102,"line":787},[100,58506,200],{"class":114},[100,58508,170],{"class":118},[100,58510,1280],{"class":205},[100,58512,58513],{"class":209},"File not found:",[100,58515,1280],{"class":205},[100,58517,126],{"class":118},[100,58519,23294],{"class":178},[100,58521,215],{"class":118},[14,58523,58524,58525,58527],{},"This helps with many common cases. A lot of ",[17,58526,17509],{}," problems happen because the path is wrong, missing, or points to the wrong thing.",[77,58529,58531],{"id":58530},"what-oserror-means","What OSError means",[14,58533,58534,58536],{},[17,58535,17509],{}," is a broad error category for operating system related problems.",[14,58538,58539],{},"In practice, that usually means Python failed while trying to do something like:",[40,58541,58542,58545,58548,58551],{},[43,58543,58544],{},"open a file",[43,58546,58547],{},"create or remove a folder",[43,58549,58550],{},"rename a path",[43,58552,58553],{},"access a location without permission",[14,58555,58556,58557,58559],{},"Also, many common errors are part of the ",[17,58558,17509],{}," family, including:",[40,58561,58562,58566,58570,58574],{},[43,58563,58564],{},[17,58565,17489],{},[43,58567,58568],{},[17,58569,17494],{},[43,58571,58572],{},[17,58573,17499],{},[43,58575,58576],{},[17,58577,17504],{},[14,58579,58580,58581,58583],{},"So even if you do not see plain ",[17,58582,17509],{},", you may still be dealing with the same family of problem.",[77,58585,58587],{"id":58586},"when-oserror-happens","When OSError happens",[14,58589,58590,58592],{},[17,58591,17509],{}," and its subclasses often happen in situations like these:",[40,58594,58595,58598,58601,58604,58607,58610],{},[43,58596,58597],{},"Opening a file that does not exist",[43,58599,58600],{},"Trying to write to a location without permission",[43,58602,58603],{},"Using a directory path where a file path is expected",[43,58605,58606],{},"Using a file path where a directory path is expected",[43,58608,58609],{},"Renaming, deleting, or listing invalid paths",[43,58611,58612],{},"Working with broken or platform-specific paths",[14,58614,580],{},[40,58616,58617,58625,58632],{},[43,58618,58619,58622,58623],{},[17,58620,58621],{},"open(\"missing.txt\")"," may raise ",[17,58624,17489],{},[43,58626,58627,58628,58622,58630],{},"opening a folder with ",[17,58629,18032],{},[17,58631,17499],{},[43,58633,58634,58635],{},"writing to a protected location may raise ",[17,58636,17494],{},[77,58638,58640],{"id":58639},"read-the-error-message-carefully","Read the error message carefully",[14,58642,58643],{},"The traceback usually gives the best clue.",[14,58645,58646,58647,58649],{},"When you see an ",[17,58648,17509],{},", check these things first:",[40,58651,58652,58659,58662,58665],{},[43,58653,58654,58655,3178,58657],{},"The error number, such as ",[17,58656,18019],{},[17,58658,20474],{},[43,58660,58661],{},"The exact path shown in the error",[43,58663,58664],{},"Whether Python expected a file or a directory",[43,58666,58667],{},"Whether the problem is a missing path, wrong path, or blocked access",[14,58669,58670],{},"A few common examples:",[40,58672,58673,58678,58683,58688],{},[43,58674,58675,58677],{},[17,58676,18019],{}," often means the file or folder was not found",[43,58679,58680,58682],{},[17,58681,20474],{}," often means permission was denied",[43,58684,58685,58687],{},[17,58686,20635],{}," often means you used a directory where a file was expected",[43,58689,58690,58692],{},[17,58691,40982],{}," often means you used a file where a directory was expected",[77,58694,58696],{"id":58695},"common-ways-to-fix-oserror","Common ways to fix OSError",[14,58698,58699],{},"Here are the most common fixes:",[40,58701,58702,58705,58708,58711,58716,58719,58724],{},[43,58703,58704],{},"Make sure the file or folder really exists",[43,58706,58707],{},"Use the correct absolute or relative path",[43,58709,58710],{},"Check your current working directory",[43,58712,241,58713,58715],{},[17,58714,19482],{}," before file operations when helpful",[43,58717,58718],{},"Make sure you have permission to read or write",[43,58720,241,58721,58723],{},[17,58722,18988],{}," to build paths safely",[43,58725,58726,58727],{},"Do not try to open a directory with ",[17,58728,18032],{},[14,58730,3971],{},[40,58732,58733,58736,58739,58742,58745,58748,58751],{},[43,58734,58735],{},"Wrong file path",[43,58737,58738],{},"Relative path used from the wrong working directory",[43,58740,58741],{},"Missing file or folder",[43,58743,58744],{},"No permission to access the path",[43,58746,58747],{},"Using a directory as if it were a file",[43,58749,58750],{},"Using a file as if it were a directory",[43,58752,58753],{},"Invalid characters or unsupported path format on the current system",[14,58755,58756,58757,58759],{},"If you are new to paths, see ",[295,58758,18286],{"href":18285}," for a clearer explanation of relative and absolute paths.",[77,58761,58763],{"id":58762},"example-missing-file-causes-an-oserror-family-error","Example: missing file causes an OSError family error",[14,58765,58766],{},"A missing file is one of the most common causes.",[91,58768,58770],{"className":93,"code":58769,"language":95,"meta":96,"style":96},"with open(\"missing_file.txt\", \"r\", encoding=\"utf-8\") as file:\n    content = file.read()\n    print(content)\n",[17,58771,58772,58814,58828],{"__ignoreMap":96},[100,58773,58774,58776,58778,58780,58782,58784,58786,58788,58790,58792,58794,58796,58798,58800,58802,58804,58806,58808,58810,58812],{"class":102,"line":103},[100,58775,17521],{"class":145},[100,58777,17524],{"class":114},[100,58779,170],{"class":118},[100,58781,206],{"class":205},[100,58783,17531],{"class":209},[100,58785,206],{"class":205},[100,58787,126],{"class":118},[100,58789,1708],{"class":205},[100,58791,17540],{"class":209},[100,58793,206],{"class":205},[100,58795,126],{"class":118},[100,58797,18142],{"class":18141},[100,58799,111],{"class":110},[100,58801,206],{"class":205},[100,58803,15307],{"class":209},[100,58805,206],{"class":205},[100,58807,6155],{"class":118},[100,58809,16224],{"class":145},[100,58811,17550],{"class":17549},[100,58813,149],{"class":118},[100,58815,58816,58818,58820,58822,58824,58826],{"class":102,"line":135},[100,58817,18414],{"class":106},[100,58819,111],{"class":110},[100,58821,17550],{"class":17549},[100,58823,290],{"class":118},[100,58825,17566],{"class":178},[100,58827,4734],{"class":118},[100,58829,58830,58832,58834,58836],{"class":102,"line":142},[100,58831,200],{"class":114},[100,58833,170],{"class":118},[100,58835,18182],{"class":178},[100,58837,215],{"class":118},[14,58839,24978],{},[91,58841,58842],{"className":93,"code":17573,"language":95,"meta":96,"style":96},[17,58843,58844],{"__ignoreMap":96},[100,58845,58846,58848,58850,58852,58854,58856,58858,58860,58862,58864,58866,58868,58870,58872],{"class":102,"line":103},[100,58847,17489],{"class":191},[100,58849,89],{"class":118},[100,58851,594],{"class":118},[100,58853,17586],{"class":106},[100,58855,559],{"class":122},[100,58857,17591],{"class":118},[100,58859,17594],{"class":106},[100,58861,17597],{"class":17549},[100,58863,17600],{"class":110},[100,58865,17603],{"class":106},[100,58867,89],{"class":118},[100,58869,1274],{"class":205},[100,58871,17531],{"class":209},[100,58873,3925],{"class":205},[14,58875,58876,58877,58879,58880,290],{},"In modern Python, this usually raises ",[17,58878,17489],{}," instead of plain ",[17,58881,17509],{},[14,58883,58884,58885,58887,58888,290],{},"That is still part of the same error family. ",[17,58886,17489],{}," is a more specific subclass of ",[17,58889,17509],{},[14,58891,4964,58892,290],{},[295,58893,20642],{"href":23203},[77,58895,58897],{"id":58896},"example-handling-oserror-with-try-except","Example: handling OSError with try-except",[14,58899,58900,58901,290],{},"If you want to handle several operating system related problems together, you can catch ",[17,58902,17509],{},[91,58904,58906],{"className":93,"code":58905,"language":95,"meta":96,"style":96},"try:\n    with open(\"example.txt\", \"r\", encoding=\"utf-8\") as file:\n        print(file.read())\nexcept OSError as error:\n    print(\"Something went wrong:\")\n    print(error)\n",[17,58907,58908,58914,58956,58970,58984,58999],{"__ignoreMap":96},[100,58909,58910,58912],{"class":102,"line":103},[100,58911,146],{"class":145},[100,58913,149],{"class":118},[100,58915,58916,58918,58920,58922,58924,58926,58928,58930,58932,58934,58936,58938,58940,58942,58944,58946,58948,58950,58952,58954],{"class":102,"line":135},[100,58917,18121],{"class":145},[100,58919,17524],{"class":114},[100,58921,170],{"class":118},[100,58923,206],{"class":205},[100,58925,58418],{"class":209},[100,58927,206],{"class":205},[100,58929,126],{"class":118},[100,58931,1708],{"class":205},[100,58933,17540],{"class":209},[100,58935,206],{"class":205},[100,58937,126],{"class":118},[100,58939,18142],{"class":18141},[100,58941,111],{"class":110},[100,58943,206],{"class":205},[100,58945,15307],{"class":209},[100,58947,206],{"class":205},[100,58949,6155],{"class":118},[100,58951,16224],{"class":145},[100,58953,17550],{"class":17549},[100,58955,149],{"class":118},[100,58957,58958,58960,58962,58964,58966,58968],{"class":102,"line":142},[100,58959,167],{"class":114},[100,58961,170],{"class":118},[100,58963,17597],{"class":17549},[100,58965,290],{"class":118},[100,58967,17566],{"class":178},[100,58969,3370],{"class":118},[100,58971,58972,58974,58977,58979,58982],{"class":102,"line":152},[100,58973,188],{"class":145},[100,58975,58976],{"class":191}," OSError",[100,58978,16224],{"class":145},[100,58980,58981],{"class":106}," error",[100,58983,149],{"class":118},[100,58985,58986,58988,58990,58992,58995,58997],{"class":102,"line":164},[100,58987,200],{"class":114},[100,58989,170],{"class":118},[100,58991,206],{"class":205},[100,58993,58994],{"class":209},"Something went wrong:",[100,58996,206],{"class":205},[100,58998,215],{"class":118},[100,59000,59001,59003,59005,59008],{"class":102,"line":185},[100,59002,200],{"class":114},[100,59004,170],{"class":118},[100,59006,59007],{"class":178},"error",[100,59009,215],{"class":118},[14,59011,59012],{},"This is useful when the exact subclass does not matter and you just want to show the error or fail safely.",[14,59014,59015],{},"For example, the output might be:",[91,59017,59019],{"className":93,"code":59018,"language":95,"meta":96,"style":96},"Something went wrong:\n[Errno 2] No such file or directory: 'example.txt'\n",[17,59020,59021,59028],{"__ignoreMap":96},[100,59022,59023,59026],{"class":102,"line":103},[100,59024,59025],{"class":106},"Something went wrong",[100,59027,149],{"class":118},[100,59029,59030,59032,59034,59036,59038,59040,59042,59044,59046,59048,59050,59052],{"class":102,"line":135},[100,59031,3166],{"class":118},[100,59033,17586],{"class":106},[100,59035,559],{"class":122},[100,59037,17591],{"class":118},[100,59039,17594],{"class":106},[100,59041,17597],{"class":17549},[100,59043,17600],{"class":110},[100,59045,17603],{"class":106},[100,59047,89],{"class":118},[100,59049,1274],{"class":205},[100,59051,58418],{"class":209},[100,59053,3925],{"class":205},[14,59055,59056],{},"If you already know the likely problem, catching the specific error is better.",[91,59058,59060],{"className":93,"code":59059,"language":95,"meta":96,"style":96},"try:\n    with open(\"example.txt\", \"r\", encoding=\"utf-8\") as file:\n        print(file.read())\nexcept FileNotFoundError:\n    print(\"The file does not exist.\")\n",[17,59061,59062,59068,59110,59124,59132],{"__ignoreMap":96},[100,59063,59064,59066],{"class":102,"line":103},[100,59065,146],{"class":145},[100,59067,149],{"class":118},[100,59069,59070,59072,59074,59076,59078,59080,59082,59084,59086,59088,59090,59092,59094,59096,59098,59100,59102,59104,59106,59108],{"class":102,"line":135},[100,59071,18121],{"class":145},[100,59073,17524],{"class":114},[100,59075,170],{"class":118},[100,59077,206],{"class":205},[100,59079,58418],{"class":209},[100,59081,206],{"class":205},[100,59083,126],{"class":118},[100,59085,1708],{"class":205},[100,59087,17540],{"class":209},[100,59089,206],{"class":205},[100,59091,126],{"class":118},[100,59093,18142],{"class":18141},[100,59095,111],{"class":110},[100,59097,206],{"class":205},[100,59099,15307],{"class":209},[100,59101,206],{"class":205},[100,59103,6155],{"class":118},[100,59105,16224],{"class":145},[100,59107,17550],{"class":17549},[100,59109,149],{"class":118},[100,59111,59112,59114,59116,59118,59120,59122],{"class":102,"line":142},[100,59113,167],{"class":114},[100,59115,170],{"class":118},[100,59117,17597],{"class":17549},[100,59119,290],{"class":118},[100,59121,17566],{"class":178},[100,59123,3370],{"class":118},[100,59125,59126,59128,59130],{"class":102,"line":152},[100,59127,188],{"class":145},[100,59129,20558],{"class":191},[100,59131,149],{"class":118},[100,59133,59134,59136,59138,59140,59143,59145],{"class":102,"line":164},[100,59135,200],{"class":114},[100,59137,170],{"class":118},[100,59139,206],{"class":205},[100,59141,59142],{"class":209},"The file does not exist.",[100,59144,206],{"class":205},[100,59146,215],{"class":118},[14,59148,59149],{},"Specific exceptions are easier to understand and usually easier to fix.",[77,59151,1168],{"id":1167},[14,59153,59154],{},"When you are not sure what is wrong, test the path directly.",[91,59156,59158],{"className":93,"code":59157,"language":95,"meta":96,"style":96},"import os\n\npath = \"example.txt\"\n\nprint(os.getcwd())\nprint(os.path.exists(path))\nprint(os.path.isfile(path))\nprint(os.path.isdir(path))\nprint(repr(path))\n",[17,59159,59160,59166,59170,59182,59186,59200,59222,59244,59266],{"__ignoreMap":96},[100,59161,59162,59164],{"class":102,"line":103},[100,59163,9259],{"class":145},[100,59165,18702],{"class":106},[100,59167,59168],{"class":102,"line":135},[100,59169,139],{"emptyLinePlaceholder":138},[100,59171,59172,59174,59176,59178,59180],{"class":102,"line":142},[100,59173,19365],{"class":106},[100,59175,111],{"class":110},[100,59177,1708],{"class":205},[100,59179,58418],{"class":209},[100,59181,1714],{"class":205},[100,59183,59184],{"class":102,"line":152},[100,59185,139],{"emptyLinePlaceholder":138},[100,59187,59188,59190,59192,59194,59196,59198],{"class":102,"line":164},[100,59189,372],{"class":114},[100,59191,170],{"class":118},[100,59193,18035],{"class":178},[100,59195,290],{"class":118},[100,59197,18719],{"class":178},[100,59199,3370],{"class":118},[100,59201,59202,59204,59206,59208,59210,59212,59214,59216,59218,59220],{"class":102,"line":185},[100,59203,372],{"class":114},[100,59205,170],{"class":118},[100,59207,18035],{"class":178},[100,59209,290],{"class":118},[100,59211,19017],{"class":2494},[100,59213,290],{"class":118},[100,59215,18114],{"class":178},[100,59217,170],{"class":118},[100,59219,19017],{"class":178},[100,59221,182],{"class":118},[100,59223,59224,59226,59228,59230,59232,59234,59236,59238,59240,59242],{"class":102,"line":197},[100,59225,372],{"class":114},[100,59227,170],{"class":118},[100,59229,18035],{"class":178},[100,59231,290],{"class":118},[100,59233,19017],{"class":2494},[100,59235,290],{"class":118},[100,59237,24191],{"class":178},[100,59239,170],{"class":118},[100,59241,19017],{"class":178},[100,59243,182],{"class":118},[100,59245,59246,59248,59250,59252,59254,59256,59258,59260,59262,59264],{"class":102,"line":771},[100,59247,372],{"class":114},[100,59249,170],{"class":118},[100,59251,18035],{"class":178},[100,59253,290],{"class":118},[100,59255,19017],{"class":2494},[100,59257,290],{"class":118},[100,59259,24218],{"class":178},[100,59261,170],{"class":118},[100,59263,19017],{"class":178},[100,59265,182],{"class":118},[100,59267,59268,59270,59272,59274,59276,59278],{"class":102,"line":787},[100,59269,372],{"class":114},[100,59271,170],{"class":118},[100,59273,12228],{"class":114},[100,59275,170],{"class":118},[100,59277,19017],{"class":178},[100,59279,182],{"class":118},[14,59281,59282],{},"What each line helps you check:",[40,59284,59285,59290,59295,59300,59305],{},[43,59286,59287,59289],{},[17,59288,19603],{}," shows your current working folder",[43,59291,59292,59294],{},[17,59293,40726],{}," checks whether the path exists",[43,59296,59297,59299],{},[17,59298,40098],{}," checks whether it is a file",[43,59301,59302,59304],{},[17,59303,40104],{}," checks whether it is a directory",[43,59306,59307,59310],{},[17,59308,59309],{},"repr(path)"," shows the exact string, including hidden spaces or escape characters",[14,59312,59313],{},"These are the most useful debugging commands:",[91,59315,59317],{"className":93,"code":59316,"language":95,"meta":96,"style":96},"import os\nprint(os.getcwd())\nprint(os.path.exists(path))\nprint(os.path.isfile(path))\nprint(os.path.isdir(path))\nprint(repr(path))\n",[17,59318,59319,59325,59339,59361,59383,59405],{"__ignoreMap":96},[100,59320,59321,59323],{"class":102,"line":103},[100,59322,9259],{"class":145},[100,59324,18702],{"class":106},[100,59326,59327,59329,59331,59333,59335,59337],{"class":102,"line":135},[100,59328,372],{"class":114},[100,59330,170],{"class":118},[100,59332,18035],{"class":178},[100,59334,290],{"class":118},[100,59336,18719],{"class":178},[100,59338,3370],{"class":118},[100,59340,59341,59343,59345,59347,59349,59351,59353,59355,59357,59359],{"class":102,"line":142},[100,59342,372],{"class":114},[100,59344,170],{"class":118},[100,59346,18035],{"class":178},[100,59348,290],{"class":118},[100,59350,19017],{"class":2494},[100,59352,290],{"class":118},[100,59354,18114],{"class":178},[100,59356,170],{"class":118},[100,59358,19017],{"class":178},[100,59360,182],{"class":118},[100,59362,59363,59365,59367,59369,59371,59373,59375,59377,59379,59381],{"class":102,"line":152},[100,59364,372],{"class":114},[100,59366,170],{"class":118},[100,59368,18035],{"class":178},[100,59370,290],{"class":118},[100,59372,19017],{"class":2494},[100,59374,290],{"class":118},[100,59376,24191],{"class":178},[100,59378,170],{"class":118},[100,59380,19017],{"class":178},[100,59382,182],{"class":118},[100,59384,59385,59387,59389,59391,59393,59395,59397,59399,59401,59403],{"class":102,"line":164},[100,59386,372],{"class":114},[100,59388,170],{"class":118},[100,59390,18035],{"class":178},[100,59392,290],{"class":118},[100,59394,19017],{"class":2494},[100,59396,290],{"class":118},[100,59398,24218],{"class":178},[100,59400,170],{"class":118},[100,59402,19017],{"class":178},[100,59404,182],{"class":118},[100,59406,59407,59409,59411,59413,59415,59417],{"class":102,"line":185},[100,59408,372],{"class":114},[100,59410,170],{"class":118},[100,59412,12228],{"class":114},[100,59414,170],{"class":118},[100,59416,19017],{"class":178},[100,59418,182],{"class":118},[14,59420,59421],{},"You can also try a simpler path that you know exists. That helps you tell whether the problem is your code or the specific file location.",[14,59423,59424,59425,3266,59427,290],{},"If you want a broader introduction to these tools, see the ",[295,59426,57875],{"href":18733},[295,59428,22125],{"href":19525},[77,59430,59432],{"id":59431},"oserror-vs-specific-errors","OSError vs specific errors",[14,59434,59435,59437],{},[17,59436,17509],{}," is the broad parent category.",[14,59439,59440],{},"Specific subclasses give you a clearer description of what went wrong.",[14,59442,59443],{},"Common examples include:",[40,59445,59446,59450,59454,59458],{},[43,59447,59448],{},[17,59449,17489],{},[43,59451,59452],{},[17,59453,17494],{},[43,59455,59456],{},[17,59457,17499],{},[43,59459,59460],{},[17,59461,17504],{},[14,59463,59464],{},"In general:",[40,59466,59467,59472],{},[43,59468,42151,59469,59471],{},[17,59470,17509],{}," when you want to handle many OS-related problems together",[43,59473,59474],{},"Catch a specific subclass when you know the likely cause",[14,59476,59477],{},"For exact fixes, see:",[40,59479,59480,59484,59489],{},[43,59481,59482],{},[295,59483,23058],{"href":23057},[43,59485,59486],{},[295,59487,59488],{"href":23069},"IsADirectoryError: Errno 21 is a directory fix",[43,59490,59491],{},[295,59492,59493],{"href":23081},"NotADirectoryError: Errno 20 not a directory fix",[77,59495,1514],{"id":1513},[675,59497,59499],{"id":59498},"is-oserror-the-same-as-filenotfounderror","Is OSError the same as FileNotFoundError?",[14,59501,14506,59502,58887,59504,290],{},[17,59503,17489],{},[17,59505,17509],{},[675,59507,59509],{"id":59508},"should-i-catch-oserror-or-filenotfounderror","Should I catch OSError or FileNotFoundError?",[14,59511,59512],{},"Catch the specific error when you know the problem.",[14,59514,42151,59515,59517],{},[17,59516,17509],{}," when you want to handle several operating system related errors together.",[675,59519,59521],{"id":59520},"why-does-my-code-work-in-one-folder-but-not-another","Why does my code work in one folder but not another?",[14,59523,59524],{},"The current working directory may be different, or the second location may have different files or permissions.",[675,59526,59528],{"id":59527},"can-oserror-happen-without-using-files","Can OSError happen without using files?",[14,59530,59531],{},"Yes. It can also happen with other operating system operations such as directories, process actions, or some path operations.",[77,59533,1554],{"id":1553},[40,59535,59536,59540,59544,59548,59552],{},[43,59537,59538],{},[295,59539,20591],{"href":20590},[43,59541,59542],{},[295,59543,20596],{"href":18285},[43,59545,59546],{},[295,59547,57875],{"href":18733},[43,59549,59550],{},[295,59551,24741],{"href":19525},[43,59553,59554],{},[295,59555,20642],{"href":23203},[1589,59557,59558],{},"html pre.shiki code .sVHd0, html code.shiki .sVHd0{--shiki-light:#39ADB5;--shiki-light-font-style:italic;--shiki-default:#D73A49;--shiki-default-font-style:inherit;--shiki-dark:#F97583;--shiki-dark-font-style:inherit}html pre.shiki code .su5hD, html code.shiki .su5hD{--shiki-light:#90A4AE;--shiki-default:#24292E;--shiki-dark:#E1E4E8}html pre.shiki code .smGrS, html code.shiki .smGrS{--shiki-light:#39ADB5;--shiki-default:#D73A49;--shiki-dark:#F97583}html pre.shiki code .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 .sP7_E, html code.shiki .sP7_E{--shiki-light:#39ADB5;--shiki-default:#24292E;--shiki-dark:#E1E4E8}html pre.shiki code .skxfh, html code.shiki .skxfh{--shiki-light:#E53935;--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 pre.shiki code .sptTA, html code.shiki .sptTA{--shiki-light:#6182B8;--shiki-default:#005CC5;--shiki-dark:#79B8FF}html pre.shiki code .s99_P, html code.shiki .s99_P{--shiki-light:#90A4AE;--shiki-light-font-style:italic;--shiki-default:#E36209;--shiki-default-font-style:inherit;--shiki-dark:#FFAB70;--shiki-dark-font-style:inherit}html pre.shiki code .sMMDD, html code.shiki .sMMDD{--shiki-light:#90A4AE;--shiki-default:#E36209;--shiki-dark:#FFAB70}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}",{"title":96,"searchDepth":135,"depth":135,"links":59560},[59561,59562,59563,59564,59565,59566,59567,59568,59569,59575],{"id":58530,"depth":135,"text":58531},{"id":58586,"depth":135,"text":58587},{"id":58639,"depth":135,"text":58640},{"id":58695,"depth":135,"text":58696},{"id":58762,"depth":135,"text":58763},{"id":58896,"depth":135,"text":58897},{"id":1167,"depth":135,"text":1168},{"id":59431,"depth":135,"text":59432},{"id":1513,"depth":135,"text":1514,"children":59570},[59571,59572,59573,59574],{"id":59498,"depth":142,"text":59499},{"id":59508,"depth":142,"text":59509},{"id":59520,"depth":142,"text":59521},{"id":59527,"depth":142,"text":59528},{"id":1553,"depth":135,"text":1554},"Master oserror in python causes and fixes in our comprehensive Python beginner guide.",{},"\u002Ferrors\u002Foserror-in-python-causes-and-fixes",{"title":58347,"description":59576},"errors\u002Foserror-in-python-causes-and-fixes","7_WrEz6P-8OCYP4_PVTslUwpuHdfOrwykiK4iX-ocCI",{"id":59583,"title":59584,"body":59585,"description":60811,"extension":1623,"meta":60812,"navigation":138,"path":60813,"seo":60814,"stem":60815,"__hash__":60816},"content\u002Ferrors\u002Foverflowerror-numerical-result-out-of-range-fix.md","OverflowError: numerical result out of range (Fix)",{"type":7,"value":59586,"toc":60777},[59587,59590,59595,59601,59604,59606,59718,59725,59727,59732,59734,59751,59754,59758,59760,59762,59782,59785,59787,59792,59835,59838,59861,59863,59882,59885,59918,59920,59929,59936,59944,59946,59950,59953,60031,60041,60045,60048,60090,60093,60097,60100,60103,60106,60114,60120,60123,60208,60213,60220,60225,60287,60290,60293,60306,60308,60311,60315,60394,60397,60401,60403,60423,60427,60430,60486,60493,60497,60500,60556,60559,60563,60565,60625,60628,60632,60634,60654,60657,60676,60678,60681,60705,60707,60711,60714,60720,60725,60733,60736,60742,60748,60750,60774],[10,59588,59584],{"id":59589},"overflowerror-numerical-result-out-of-range-fix",[14,59591,59592,59594],{},[17,59593,52114],{}," means a calculation produced a number that is too large for Python to handle in that specific operation.",[14,59596,59597,59598,59600],{},"This error is common with floating-point math and functions from the ",[17,59599,9271],{}," module. It is less common with normal Python integers.",[14,59602,59603],{},"If you are trying to fix it quickly, start by checking whether your input has become extremely large before the failing calculation.",[77,59605,80],{"id":79},[91,59607,59609],{"className":93,"code":59608,"language":95,"meta":96,"style":96},"import math\n\nx = 1000\n\n# This can raise OverflowError\n# result = math.exp(x)\n\n# Safer approach: check before calling\nif x > 709:\n    print(\"Number is too large for math.exp() with a float\")\nelse:\n    result = math.exp(x)\n    print(result)\n",[17,59610,59611,59617,59621,59630,59634,59639,59644,59648,59653,59667,59682,59688,59708],{"__ignoreMap":96},[100,59612,59613,59615],{"class":102,"line":103},[100,59614,9259],{"class":145},[100,59616,9262],{"class":106},[100,59618,59619],{"class":102,"line":135},[100,59620,139],{"emptyLinePlaceholder":138},[100,59622,59623,59625,59627],{"class":102,"line":142},[100,59624,54291],{"class":106},[100,59626,111],{"class":110},[100,59628,59629],{"class":122}," 1000\n",[100,59631,59632],{"class":102,"line":152},[100,59633,139],{"emptyLinePlaceholder":138},[100,59635,59636],{"class":102,"line":164},[100,59637,59638],{"class":414},"# This can raise OverflowError\n",[100,59640,59641],{"class":102,"line":185},[100,59642,59643],{"class":414},"# result = math.exp(x)\n",[100,59645,59646],{"class":102,"line":197},[100,59647,139],{"emptyLinePlaceholder":138},[100,59649,59650],{"class":102,"line":771},[100,59651,59652],{"class":414},"# Safer approach: check before calling\n",[100,59654,59655,59657,59660,59662,59665],{"class":102,"line":787},[100,59656,2736],{"class":145},[100,59658,59659],{"class":106}," x ",[100,59661,1918],{"class":110},[100,59663,59664],{"class":122}," 709",[100,59666,149],{"class":118},[100,59668,59669,59671,59673,59675,59678,59680],{"class":102,"line":5816},[100,59670,200],{"class":114},[100,59672,170],{"class":118},[100,59674,206],{"class":205},[100,59676,59677],{"class":209},"Number is too large for math.exp() with a float",[100,59679,206],{"class":205},[100,59681,215],{"class":118},[100,59683,59684,59686],{"class":102,"line":5833},[100,59685,4944],{"class":145},[100,59687,149],{"class":118},[100,59689,59690,59693,59695,59697,59699,59702,59704,59706],{"class":102,"line":5848},[100,59691,59692],{"class":106},"    result ",[100,59694,111],{"class":110},[100,59696,53666],{"class":106},[100,59698,290],{"class":118},[100,59700,59701],{"class":178},"exp",[100,59703,170],{"class":118},[100,59705,39024],{"class":178},[100,59707,215],{"class":118},[100,59709,59710,59712,59714,59716],{"class":102,"line":5855},[100,59711,200],{"class":114},[100,59713,170],{"class":118},[100,59715,11580],{"class":178},[100,59717,215],{"class":118},[14,59719,59720,59721,59724],{},"A common fix is to validate very large values before calling functions like ",[17,59722,59723],{},"math.exp()"," or raising very large numbers to a power.",[77,59726,5881],{"id":5880},[14,59728,59729,59731],{},[17,59730,17668],{}," happens when a numeric result is outside the allowed range for that operation.",[14,59733,3866],{},[40,59735,59736,59739,59744],{},[43,59737,59738],{},"It usually appears in float-based calculations.",[43,59740,59741,59742,290],{},"It often happens with functions like ",[17,59743,59723],{},[43,59745,59746,59747,59750],{},"The message ",[17,59748,59749],{},"numerical result out of range"," means the answer is too large to fit in the numeric type being used.",[14,59752,59753],{},"In beginner code, this often happens when numbers keep growing and there is no limit check before the calculation.",[14,59755,13254,59756,290],{},[295,59757,1587],{"href":1749},[77,59759,7741],{"id":7740},[14,59761,3971],{},[40,59763,59764,59770,59773,59779],{},[43,59765,59766,59767,290],{},"You call a math function with a very large value, such as ",[17,59768,59769],{},"math.exp(1000)",[43,59771,59772],{},"You raise a number to a very large power in float arithmetic.",[43,59774,59775,59776,290],{},"You convert a huge integer to a float with ",[17,59777,59778],{},"float()",[43,59780,59781],{},"Your program multiplies values in a loop until they become too large.",[14,59783,59784],{},"This happens because floats have a maximum size. Python integers are different and can grow much larger.",[77,59786,11031],{"id":11030},[14,59788,59789,59790,89],{},"A simple example is ",[17,59791,59769],{},[91,59793,59795],{"className":93,"code":59794,"language":95,"meta":96,"style":96},"import math\n\nresult = math.exp(1000)\nprint(result)\n",[17,59796,59797,59803,59807,59825],{"__ignoreMap":96},[100,59798,59799,59801],{"class":102,"line":103},[100,59800,9259],{"class":145},[100,59802,9262],{"class":106},[100,59804,59805],{"class":102,"line":135},[100,59806,139],{"emptyLinePlaceholder":138},[100,59808,59809,59811,59813,59815,59817,59819,59821,59823],{"class":102,"line":142},[100,59810,11556],{"class":106},[100,59812,111],{"class":110},[100,59814,53666],{"class":106},[100,59816,290],{"class":118},[100,59818,59701],{"class":178},[100,59820,170],{"class":118},[100,59822,51961],{"class":122},[100,59824,215],{"class":118},[100,59826,59827,59829,59831,59833],{"class":102,"line":152},[100,59828,372],{"class":114},[100,59830,170],{"class":118},[100,59832,11580],{"class":178},[100,59834,215],{"class":118},[14,59836,59837],{},"The error happens on this line:",[91,59839,59841],{"className":93,"code":59840,"language":95,"meta":96,"style":96},"result = math.exp(1000)\n",[17,59842,59843],{"__ignoreMap":96},[100,59844,59845,59847,59849,59851,59853,59855,59857,59859],{"class":102,"line":103},[100,59846,11556],{"class":106},[100,59848,111],{"class":110},[100,59850,53666],{"class":106},[100,59852,290],{"class":118},[100,59854,59701],{"class":178},[100,59856,170],{"class":118},[100,59858,51961],{"class":122},[100,59860,215],{"class":118},[14,59862,32043],{},[40,59864,59865,59871,59874,59879],{},[43,59866,59867,59870],{},[17,59868,59869],{},"exp(x)"," means (e^x)",[43,59872,59873],{},"Exponential growth becomes huge very quickly",[43,59875,59876,59878],{},[17,59877,59723],{}," returns a float",[43,59880,59881],{},"That result is too large to fit in a normal float",[14,59883,59884],{},"You can compare this with a smaller value:",[91,59886,59888],{"className":93,"code":59887,"language":95,"meta":96,"style":96},"import math\n\nprint(math.exp(10))\n",[17,59889,59890,59896,59900],{"__ignoreMap":96},[100,59891,59892,59894],{"class":102,"line":103},[100,59893,9259],{"class":145},[100,59895,9262],{"class":106},[100,59897,59898],{"class":102,"line":135},[100,59899,139],{"emptyLinePlaceholder":138},[100,59901,59902,59904,59906,59908,59910,59912,59914,59916],{"class":102,"line":142},[100,59903,372],{"class":114},[100,59905,170],{"class":118},[100,59907,9271],{"class":178},[100,59909,290],{"class":118},[100,59911,59701],{"class":178},[100,59913,170],{"class":118},[100,59915,356],{"class":122},[100,59917,182],{"class":118},[14,59919,14742],{},[91,59921,59923],{"className":93,"code":59922,"language":95,"meta":96,"style":96},"22026.465794806718\n",[17,59924,59925],{"__ignoreMap":96},[100,59926,59927],{"class":102,"line":103},[100,59928,59922],{"class":122},[14,59930,59931,59932,59935],{},"That works because ",[17,59933,59934],{},"exp(10)"," is still within float range.",[14,59937,59938,59939,59943],{},"If you are working with math functions, the ",[295,59940,59942],{"href":59941},"\u002Fstandard-library\u002Fpython-math-module-overview\u002F","Python math module overview"," is a useful reference.",[77,59945,6244],{"id":6243},[675,59947,59949],{"id":59948},"check-inputs-before-doing-the-calculation","Check inputs before doing the calculation",[14,59951,59952],{},"This is often the best fix.",[91,59954,59956],{"className":93,"code":59955,"language":95,"meta":96,"style":96},"import math\n\nx = 1000\n\nif x > 709:\n    print(\"Input is too large for math.exp()\")\nelse:\n    print(math.exp(x))\n",[17,59957,59958,59964,59968,59976,59980,59992,60007,60013],{"__ignoreMap":96},[100,59959,59960,59962],{"class":102,"line":103},[100,59961,9259],{"class":145},[100,59963,9262],{"class":106},[100,59965,59966],{"class":102,"line":135},[100,59967,139],{"emptyLinePlaceholder":138},[100,59969,59970,59972,59974],{"class":102,"line":142},[100,59971,54291],{"class":106},[100,59973,111],{"class":110},[100,59975,59629],{"class":122},[100,59977,59978],{"class":102,"line":152},[100,59979,139],{"emptyLinePlaceholder":138},[100,59981,59982,59984,59986,59988,59990],{"class":102,"line":164},[100,59983,2736],{"class":145},[100,59985,59659],{"class":106},[100,59987,1918],{"class":110},[100,59989,59664],{"class":122},[100,59991,149],{"class":118},[100,59993,59994,59996,59998,60000,60003,60005],{"class":102,"line":185},[100,59995,200],{"class":114},[100,59997,170],{"class":118},[100,59999,206],{"class":205},[100,60001,60002],{"class":209},"Input is too large for math.exp()",[100,60004,206],{"class":205},[100,60006,215],{"class":118},[100,60008,60009,60011],{"class":102,"line":197},[100,60010,4944],{"class":145},[100,60012,149],{"class":118},[100,60014,60015,60017,60019,60021,60023,60025,60027,60029],{"class":102,"line":771},[100,60016,200],{"class":114},[100,60018,170],{"class":118},[100,60020,9271],{"class":178},[100,60022,290],{"class":118},[100,60024,59701],{"class":178},[100,60026,170],{"class":118},[100,60028,39024],{"class":178},[100,60030,182],{"class":118},[14,60032,60033,60034,60036,60037,60040],{},"For many systems, ",[17,60035,59723],{}," overflows somewhere above about ",[17,60038,60039],{},"709"," because of float limits.",[675,60042,60044],{"id":60043},"use-smaller-values-or-scale-numbers-down","Use smaller values or scale numbers down",[14,60046,60047],{},"Sometimes your formula works, but the numbers are too large in their current form.",[91,60049,60051],{"className":93,"code":60050,"language":95,"meta":96,"style":96},"value = 1_000_000\nscaled_value = value \u002F 1000\n\nprint(scaled_value)\n",[17,60052,60053,60062,60075,60079],{"__ignoreMap":96},[100,60054,60055,60057,60059],{"class":102,"line":103},[100,60056,3318],{"class":106},[100,60058,111],{"class":110},[100,60060,60061],{"class":122}," 1_000_000\n",[100,60063,60064,60067,60069,60071,60073],{"class":102,"line":135},[100,60065,60066],{"class":106},"scaled_value ",[100,60068,111],{"class":110},[100,60070,989],{"class":106},[100,60072,2558],{"class":110},[100,60074,59629],{"class":122},[100,60076,60077],{"class":102,"line":142},[100,60078,139],{"emptyLinePlaceholder":138},[100,60080,60081,60083,60085,60088],{"class":102,"line":152},[100,60082,372],{"class":114},[100,60084,170],{"class":118},[100,60086,60087],{"class":178},"scaled_value",[100,60089,215],{"class":118},[14,60091,60092],{},"Scaling only helps if it still makes sense for your problem.",[675,60094,60096],{"id":60095},"rewrite-the-formula-to-avoid-huge-intermediate-results","Rewrite the formula to avoid huge intermediate results",[14,60098,60099],{},"Sometimes the final answer is reasonable, but one step in the middle becomes huge.",[14,60101,60102],{},"For example, if a formula creates a very large exponent and then divides later, try rewriting it so the calculation stays smaller throughout.",[14,60104,60105],{},"This depends on the formula, but the goal is simple:",[40,60107,60108,60111],{},[43,60109,60110],{},"avoid creating giant temporary numbers",[43,60112,60113],{},"combine operations in a safer order when possible",[675,60115,60117,60118],{"id":60116},"catch-the-error-with-try-except","Catch the error with ",[17,60119,244],{},[14,60121,60122],{},"If large input is possible, you can prevent your program from crashing.",[91,60124,60126],{"className":93,"code":60125,"language":95,"meta":96,"style":96},"import math\n\nx = 1000\n\ntry:\n    result = math.exp(x)\n    print(result)\nexcept OverflowError:\n    print(\"The number is too large for this calculation.\")\n",[17,60127,60128,60134,60138,60146,60150,60156,60174,60184,60193],{"__ignoreMap":96},[100,60129,60130,60132],{"class":102,"line":103},[100,60131,9259],{"class":145},[100,60133,9262],{"class":106},[100,60135,60136],{"class":102,"line":135},[100,60137,139],{"emptyLinePlaceholder":138},[100,60139,60140,60142,60144],{"class":102,"line":142},[100,60141,54291],{"class":106},[100,60143,111],{"class":110},[100,60145,59629],{"class":122},[100,60147,60148],{"class":102,"line":152},[100,60149,139],{"emptyLinePlaceholder":138},[100,60151,60152,60154],{"class":102,"line":164},[100,60153,146],{"class":145},[100,60155,149],{"class":118},[100,60157,60158,60160,60162,60164,60166,60168,60170,60172],{"class":102,"line":185},[100,60159,59692],{"class":106},[100,60161,111],{"class":110},[100,60163,53666],{"class":106},[100,60165,290],{"class":118},[100,60167,59701],{"class":178},[100,60169,170],{"class":118},[100,60171,39024],{"class":178},[100,60173,215],{"class":118},[100,60175,60176,60178,60180,60182],{"class":102,"line":197},[100,60177,200],{"class":114},[100,60179,170],{"class":118},[100,60181,11580],{"class":178},[100,60183,215],{"class":118},[100,60185,60186,60188,60191],{"class":102,"line":771},[100,60187,188],{"class":145},[100,60189,60190],{"class":191}," OverflowError",[100,60192,149],{"class":118},[100,60194,60195,60197,60199,60201,60204,60206],{"class":102,"line":787},[100,60196,200],{"class":114},[100,60198,170],{"class":118},[100,60200,206],{"class":205},[100,60202,60203],{"class":209},"The number is too large for this calculation.",[100,60205,206],{"class":205},[100,60207,215],{"class":118},[14,60209,60210,60211,290],{},"This is useful when you expect bad or extreme input. For a beginner guide, see ",[295,60212,1029],{"href":2824},[675,60214,241,60216,60219],{"id":60215},"use-decimal-in-some-cases",[17,60217,60218],{},"decimal"," in some cases",[14,60221,43223,60222,60224],{},[17,60223,60218],{}," module can help when you need more control over precision.",[91,60226,60228],{"className":93,"code":60227,"language":95,"meta":96,"style":96},"from decimal import Decimal\n\nx = Decimal(\"100\")\nresult = x ** 2\nprint(result)\n",[17,60229,60230,60242,60246,60265,60277],{"__ignoreMap":96},[100,60231,60232,60234,60237,60239],{"class":102,"line":103},[100,60233,10201],{"class":145},[100,60235,60236],{"class":106}," decimal ",[100,60238,9259],{"class":145},[100,60240,60241],{"class":106}," Decimal\n",[100,60243,60244],{"class":102,"line":135},[100,60245,139],{"emptyLinePlaceholder":138},[100,60247,60248,60250,60252,60255,60257,60259,60261,60263],{"class":102,"line":142},[100,60249,54291],{"class":106},[100,60251,111],{"class":110},[100,60253,60254],{"class":178}," Decimal",[100,60256,170],{"class":118},[100,60258,206],{"class":205},[100,60260,2579],{"class":209},[100,60262,206],{"class":205},[100,60264,215],{"class":118},[100,60266,60267,60269,60271,60273,60275],{"class":102,"line":152},[100,60268,11556],{"class":106},[100,60270,111],{"class":110},[100,60272,59659],{"class":106},[100,60274,50906],{"class":110},[100,60276,3564],{"class":122},[100,60278,60279,60281,60283,60285],{"class":102,"line":164},[100,60280,372],{"class":114},[100,60282,170],{"class":118},[100,60284,11580],{"class":178},[100,60286,215],{"class":118},[14,60288,60289],{},"But this is not a magic fix.",[14,60291,60292],{},"Important:",[40,60294,60295,60300,60303],{},[43,60296,60297,60299],{},[17,60298,60218],{}," can help in some situations",[43,60301,60302],{},"very large calculations can still become slow or impractical",[43,60304,60305],{},"you still need to think about input size and formula design",[77,60307,34384],{"id":34383},[14,60309,60310],{},"When this error appears, use a simple process.",[675,60312,60314],{"id":60313},"_1-print-the-value-before-the-failing-line","1. Print the value before the failing line",[91,60316,60318],{"className":93,"code":60317,"language":95,"meta":96,"style":96},"import math\n\nx = 1000\nprint(x)\nprint(type(x))\n\nresult = math.exp(x)\nprint(result)\n",[17,60319,60320,60326,60330,60338,60348,60362,60366,60384],{"__ignoreMap":96},[100,60321,60322,60324],{"class":102,"line":103},[100,60323,9259],{"class":145},[100,60325,9262],{"class":106},[100,60327,60328],{"class":102,"line":135},[100,60329,139],{"emptyLinePlaceholder":138},[100,60331,60332,60334,60336],{"class":102,"line":142},[100,60333,54291],{"class":106},[100,60335,111],{"class":110},[100,60337,59629],{"class":122},[100,60339,60340,60342,60344,60346],{"class":102,"line":152},[100,60341,372],{"class":114},[100,60343,170],{"class":118},[100,60345,39024],{"class":178},[100,60347,215],{"class":118},[100,60349,60350,60352,60354,60356,60358,60360],{"class":102,"line":164},[100,60351,372],{"class":114},[100,60353,170],{"class":118},[100,60355,1250],{"class":191},[100,60357,170],{"class":118},[100,60359,39024],{"class":178},[100,60361,182],{"class":118},[100,60363,60364],{"class":102,"line":185},[100,60365,139],{"emptyLinePlaceholder":138},[100,60367,60368,60370,60372,60374,60376,60378,60380,60382],{"class":102,"line":197},[100,60369,11556],{"class":106},[100,60371,111],{"class":110},[100,60373,53666],{"class":106},[100,60375,290],{"class":118},[100,60377,59701],{"class":178},[100,60379,170],{"class":118},[100,60381,39024],{"class":178},[100,60383,215],{"class":118},[100,60385,60386,60388,60390,60392],{"class":102,"line":771},[100,60387,372],{"class":114},[100,60389,170],{"class":118},[100,60391,11580],{"class":178},[100,60393,215],{"class":118},[14,60395,60396],{},"This helps you confirm what value is being passed in.",[675,60398,60400],{"id":60399},"_2-find-which-operation-is-growing-too-fast","2. Find which operation is growing too fast",[14,60402,25531],{},[40,60404,60405,60408,60413,60418],{},[43,60406,60407],{},"repeated multiplication",[43,60409,60410,60411],{},"exponentiation with ",[17,60412,50906],{},[43,60414,60415,60416],{},"calls like ",[17,60417,59723],{},[43,60419,60420,60421],{},"conversion to ",[17,60422,59778],{},[675,60424,60426],{"id":60425},"_3-check-for-accidental-float-conversion","3. Check for accidental float conversion",[14,60428,60429],{},"A very large integer may exist safely, but converting it to float can fail.",[91,60431,60433],{"className":93,"code":60432,"language":95,"meta":96,"style":96},"big_number = 10 ** 1000\n\nprint(type(big_number))\nprint(float(big_number))  # Can raise OverflowError\n",[17,60434,60435,60449,60453,60468],{"__ignoreMap":96},[100,60436,60437,60440,60442,60444,60447],{"class":102,"line":103},[100,60438,60439],{"class":106},"big_number ",[100,60441,111],{"class":110},[100,60443,6550],{"class":122},[100,60445,60446],{"class":110}," **",[100,60448,59629],{"class":122},[100,60450,60451],{"class":102,"line":135},[100,60452,139],{"emptyLinePlaceholder":138},[100,60454,60455,60457,60459,60461,60463,60466],{"class":102,"line":142},[100,60456,372],{"class":114},[100,60458,170],{"class":118},[100,60460,1250],{"class":191},[100,60462,170],{"class":118},[100,60464,60465],{"class":178},"big_number",[100,60467,182],{"class":118},[100,60469,60470,60472,60474,60477,60479,60481,60483],{"class":102,"line":152},[100,60471,372],{"class":114},[100,60473,170],{"class":118},[100,60475,60476],{"class":191},"float",[100,60478,170],{"class":118},[100,60480,60465],{"class":178},[100,60482,411],{"class":118},[100,60484,60485],{"class":414},"  # Can raise OverflowError\n",[14,60487,60488,60489,290],{},"If you need to understand float conversion better, see ",[295,60490,60492],{"href":60491},"\u002Freference\u002Fpython-float-function-explained\u002F","Python float() function explained",[675,60494,60496],{"id":60495},"_4-test-with-smaller-input","4. Test with smaller input",[14,60498,60499],{},"Try a smaller value to confirm the cause.",[91,60501,60503],{"className":93,"code":60502,"language":95,"meta":96,"style":96},"import math\n\nprint(math.exp(10))\nprint(math.exp(100))\n# print(math.exp(1000))  # likely to overflow\n",[17,60504,60505,60511,60515,60533,60551],{"__ignoreMap":96},[100,60506,60507,60509],{"class":102,"line":103},[100,60508,9259],{"class":145},[100,60510,9262],{"class":106},[100,60512,60513],{"class":102,"line":135},[100,60514,139],{"emptyLinePlaceholder":138},[100,60516,60517,60519,60521,60523,60525,60527,60529,60531],{"class":102,"line":142},[100,60518,372],{"class":114},[100,60520,170],{"class":118},[100,60522,9271],{"class":178},[100,60524,290],{"class":118},[100,60526,59701],{"class":178},[100,60528,170],{"class":118},[100,60530,356],{"class":122},[100,60532,182],{"class":118},[100,60534,60535,60537,60539,60541,60543,60545,60547,60549],{"class":102,"line":152},[100,60536,372],{"class":114},[100,60538,170],{"class":118},[100,60540,9271],{"class":178},[100,60542,290],{"class":118},[100,60544,59701],{"class":178},[100,60546,170],{"class":118},[100,60548,2579],{"class":122},[100,60550,182],{"class":118},[100,60552,60553],{"class":102,"line":164},[100,60554,60555],{"class":414},"# print(math.exp(1000))  # likely to overflow\n",[14,60557,60558],{},"If smaller values work and larger ones fail, you have confirmed that range is the problem.",[675,60560,60562],{"id":60561},"_5-check-loops-for-unbounded-growth","5. Check loops for unbounded growth",[14,60564,26761],{},[91,60566,60568],{"className":93,"code":60567,"language":95,"meta":96,"style":96},"value = 2.0\n\nfor i in range(20):\n    value = value ** 2\n    print(i, value)\n",[17,60569,60570,60579,60583,60599,60611],{"__ignoreMap":96},[100,60571,60572,60574,60576],{"class":102,"line":103},[100,60573,3318],{"class":106},[100,60575,111],{"class":110},[100,60577,60578],{"class":122}," 2.0\n",[100,60580,60581],{"class":102,"line":135},[100,60582,139],{"emptyLinePlaceholder":138},[100,60584,60585,60587,60589,60591,60593,60595,60597],{"class":102,"line":142},[100,60586,71],{"class":145},[100,60588,29838],{"class":106},[100,60590,617],{"class":145},[100,60592,29843],{"class":114},[100,60594,170],{"class":118},[100,60596,6152],{"class":122},[100,60598,522],{"class":118},[100,60600,60601,60603,60605,60607,60609],{"class":102,"line":152},[100,60602,53992],{"class":106},[100,60604,111],{"class":110},[100,60606,989],{"class":106},[100,60608,50906],{"class":110},[100,60610,3564],{"class":122},[100,60612,60613,60615,60617,60619,60621,60623],{"class":102,"line":164},[100,60614,200],{"class":114},[100,60616,170],{"class":118},[100,60618,29858],{"class":178},[100,60620,126],{"class":118},[100,60622,6747],{"class":178},[100,60624,215],{"class":118},[14,60626,60627],{},"If a loop keeps squaring or multiplying a number, it can grow out of range very quickly.",[77,60629,60631],{"id":60630},"common-places-beginners-see-this","Common places beginners see this",[14,60633,57882],{},[40,60635,60636,60641,60646,60651],{},[43,60637,17889,60638,60640],{},[17,60639,59723],{}," with large numbers",[43,60642,17889,60643,60645],{},[17,60644,50906],{}," with very large float values",[43,60647,60648,60649],{},"converting a huge integer with ",[17,60650,59778],{},[43,60652,60653],{},"scientific or financial formulas copied from examples without input checks",[14,60655,60656],{},"Common causes at a glance:",[40,60658,60659,60664,60667,60670,60673],{},[43,60660,60661,60663],{},[17,60662,59723],{}," called with a very large number",[43,60665,60666],{},"very large exponent with float arithmetic",[43,60668,60669],{},"converting an extremely large integer to float",[43,60671,60672],{},"unbounded growth inside a loop",[43,60674,60675],{},"a formula that produces huge intermediate values",[77,60677,7081],{"id":7080},[14,60679,60680],{},"Sometimes this error is confused with other exceptions.",[40,60682,60683,60690,60698],{},[43,60684,60685,60689],{},[295,60686,60687],{"href":3264},[17,60688,3483],{}," happens when the input itself is invalid, not just too large.",[43,60691,60692,60697],{},[295,60693,60695],{"href":60694},"\u002Ferrors\u002Fmemoryerror-in-python-causes-and-fixes\u002F",[17,60696,50726],{}," happens when Python runs out of memory.",[43,60699,60700,60704],{},[295,60701,60702],{"href":3269},[17,60703,3270],{}," helps when the real problem is the wrong kind of value, not its size.",[77,60706,1514],{"id":1513},[675,60708,60710],{"id":60709},"does-python-integers-overflow","Does Python integers overflow?",[14,60712,60713],{},"Normal Python integers can grow very large, so this error is more common with float-based operations and some math functions.",[675,60715,9137,60717,60719],{"id":60716},"why-does-mathexp-fail-but-large-integers-work",[17,60718,59723],{}," fail but large integers work?",[14,60721,60722,60724],{},[17,60723,59723],{}," returns a float, and floats have a maximum size. Python integers are handled differently and can grow much larger.",[675,60726,50625,60728,60730,60731,11353],{"id":60727},"can-try-except-fix-overflowerror",[17,60729,244],{}," fix ",[17,60732,17668],{},[14,60734,60735],{},"It can stop your program from crashing, but you still need to decide what your program should do when numbers become too large.",[675,60737,39391,60739,60741],{"id":60738},"should-i-use-decimal-to-avoid-this-error",[17,60740,60218],{}," to avoid this error?",[14,60743,60744,60745,60747],{},"Sometimes, but not always. ",[17,60746,60218],{}," can help in some cases, but very large calculations can still become impractical.",[77,60749,1554],{"id":1553},[40,60751,60752,60756,60760,60764,60770],{},[43,60753,60754],{},[295,60755,1587],{"href":1749},[43,60757,60758],{},[295,60759,1581],{"href":2824},[43,60761,60762],{},[295,60763,59942],{"href":59941},[43,60765,60766],{},[295,60767,60769],{"href":60768},"\u002Fstandard-library\u002Fmath.pow-function-explained","math.pow() function explained",[43,60771,60772],{},[295,60773,60492],{"href":60491},[1589,60775,60776],{},"html pre.shiki code .sVHd0, html code.shiki .sVHd0{--shiki-light:#39ADB5;--shiki-light-font-style:italic;--shiki-default:#D73A49;--shiki-default-font-style:inherit;--shiki-dark:#F97583;--shiki-dark-font-style:inherit}html pre.shiki code .su5hD, html code.shiki .su5hD{--shiki-light:#90A4AE;--shiki-default:#24292E;--shiki-dark:#E1E4E8}html pre.shiki code .smGrS, html code.shiki .smGrS{--shiki-light:#39ADB5;--shiki-default:#D73A49;--shiki-dark:#F97583}html pre.shiki code .srdBf, html code.shiki .srdBf{--shiki-light:#F76D47;--shiki-default:#005CC5;--shiki-dark:#79B8FF}html pre.shiki code .sutJx, html code.shiki .sutJx{--shiki-light:#90A4AE;--shiki-light-font-style:italic;--shiki-default:#6A737D;--shiki-default-font-style:inherit;--shiki-dark:#6A737D;--shiki-dark-font-style:inherit}html pre.shiki code .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 .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 .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}",{"title":96,"searchDepth":135,"depth":135,"links":60778},[60779,60780,60781,60782,60783,60792,60799,60800,60801,60810],{"id":79,"depth":135,"text":80},{"id":5880,"depth":135,"text":5881},{"id":7740,"depth":135,"text":7741},{"id":11030,"depth":135,"text":11031},{"id":6243,"depth":135,"text":6244,"children":60784},[60785,60786,60787,60788,60790],{"id":59948,"depth":142,"text":59949},{"id":60043,"depth":142,"text":60044},{"id":60095,"depth":142,"text":60096},{"id":60116,"depth":142,"text":60789},"Catch the error with try-except",{"id":60215,"depth":142,"text":60791},"Use decimal in some cases",{"id":34383,"depth":135,"text":34384,"children":60793},[60794,60795,60796,60797,60798],{"id":60313,"depth":142,"text":60314},{"id":60399,"depth":142,"text":60400},{"id":60425,"depth":142,"text":60426},{"id":60495,"depth":142,"text":60496},{"id":60561,"depth":142,"text":60562},{"id":60630,"depth":135,"text":60631},{"id":7080,"depth":135,"text":7081},{"id":1513,"depth":135,"text":1514,"children":60802},[60803,60804,60806,60808],{"id":60709,"depth":142,"text":60710},{"id":60716,"depth":142,"text":60805},"Why does math.exp() fail but large integers work?",{"id":60727,"depth":142,"text":60807},"Can try-except fix OverflowError?",{"id":60738,"depth":142,"text":60809},"Should I use decimal to avoid this error?",{"id":1553,"depth":135,"text":1554},"Master overflowerror numerical result out of range fix in our comprehensive Python beginner guide.",{},"\u002Ferrors\u002Foverflowerror-numerical-result-out-of-range-fix",{"title":59584,"description":60811},"errors\u002Foverflowerror-numerical-result-out-of-range-fix","OSJ0BgfdA6YnoU4u6PvR5adJ_cC1ocxFd8dEw1l1tUI",{"id":60818,"title":60819,"body":60820,"description":61884,"extension":1623,"meta":61885,"navigation":138,"path":23960,"seo":61886,"stem":61887,"__hash__":61888},"content\u002Ferrors\u002Fpermissionerror-errno-13-permission-denied-fix.md","PermissionError: [Errno 13] Permission denied (Fix)",{"type":7,"value":60821,"toc":61862},[60822,60827,60833,60835,60849,60852,60854,60857,60940,60943,60954,60963,60965,60968,60987,60990,60993,60995,60997,61014,61016,61038,61040,61043,61121,61124,61146,61152,61225,61232,61235,61237,61240,61263,61266,61344,61347,61358,61360,61363,61365,61392,61395,61397,61446,61451,61455,61576,61582,61586,61614,61617,61621,61741,61744,61748,61751,61765,61772,61783,61785,61789,61792,61796,61805,61809,61812,61816,61819,61821,61859],[10,60823,20471,60825,20475],{"id":60824},"permissionerror-errno-13-permission-denied-fix",[100,60826,20474],{},[14,60828,60829,60832],{},[17,60830,60831],{},"PermissionError: [Errno 13] Permission denied"," means Python asked the operating system to access a file, folder, or other resource, but the operating system refused.",[14,60834,30678],{},[40,60836,60837,60840,60843,60846],{},[43,60838,60839],{},"your program does not have permission to read or write something",[43,60841,60842],{},"the path points to a folder instead of a file",[43,60844,60845],{},"you are trying to save in a protected location",[43,60847,60848],{},"another program is locking the file",[14,60850,60851],{},"The good news is that this error is often easy to fix once you check the path, file type, and location.",[77,60853,80],{"id":79},[14,60855,60856],{},"Start with a simple file path that you know you can access:",[91,60858,60860],{"className":93,"code":60859,"language":95,"meta":96,"style":96},"file_path = \"example.txt\"\n\nwith open(file_path, \"r\", encoding=\"utf-8\") as file:\n    content = file.read()\n    print(content)\n",[17,60861,60862,60874,60878,60916,60930],{"__ignoreMap":96},[100,60863,60864,60866,60868,60870,60872],{"class":102,"line":103},[100,60865,18082],{"class":106},[100,60867,111],{"class":110},[100,60869,1708],{"class":205},[100,60871,58418],{"class":209},[100,60873,1714],{"class":205},[100,60875,60876],{"class":102,"line":135},[100,60877,139],{"emptyLinePlaceholder":138},[100,60879,60880,60882,60884,60886,60888,60890,60892,60894,60896,60898,60900,60902,60904,60906,60908,60910,60912,60914],{"class":102,"line":142},[100,60881,17521],{"class":145},[100,60883,17524],{"class":114},[100,60885,170],{"class":118},[100,60887,18128],{"class":178},[100,60889,126],{"class":118},[100,60891,1708],{"class":205},[100,60893,17540],{"class":209},[100,60895,206],{"class":205},[100,60897,126],{"class":118},[100,60899,18142],{"class":18141},[100,60901,111],{"class":110},[100,60903,206],{"class":205},[100,60905,15307],{"class":209},[100,60907,206],{"class":205},[100,60909,6155],{"class":118},[100,60911,16224],{"class":145},[100,60913,17550],{"class":17549},[100,60915,149],{"class":118},[100,60917,60918,60920,60922,60924,60926,60928],{"class":102,"line":152},[100,60919,18414],{"class":106},[100,60921,111],{"class":110},[100,60923,17550],{"class":17549},[100,60925,290],{"class":118},[100,60927,17566],{"class":178},[100,60929,4734],{"class":118},[100,60931,60932,60934,60936,60938],{"class":102,"line":164},[100,60933,200],{"class":114},[100,60935,170],{"class":118},[100,60937,18182],{"class":178},[100,60939,215],{"class":118},[14,60941,60942],{},"First check that:",[40,60944,60945,60948,60951],{},[43,60946,60947],{},"the path is correct",[43,60949,60950],{},"the path points to a file, not a folder",[43,60952,60953],{},"your user account has permission to read or write that location",[14,60955,48145,60956,28239,60959,290],{},[295,60957,60958],{"href":23471},"how file paths work in Python",[295,60960,60961,1576],{"href":24735},[17,60962,18032],{},[77,60964,5881],{"id":5880},[14,60966,60967],{},"This error means Python tried to do something like:",[40,60969,60970,60972,60975,60978,60981,60984],{},[43,60971,58544],{},[43,60973,60974],{},"create a file",[43,60976,60977],{},"modify a file",[43,60979,60980],{},"delete a file",[43,60982,60983],{},"rename a file",[43,60985,60986],{},"access a folder",[14,60988,60989],{},"But the operating system blocked the action.",[14,60991,60992],{},"In beginner code, this is usually related to file or folder permissions. It can also happen if you try to write to a protected system location, such as a system folder or a file owned by another user.",[77,60994,29975],{"id":29974},[14,60996,25094],{},[40,60998,60999,61002,61005,61008,61011],{},[43,61000,61001],{},"Trying to write to a file or folder you do not own",[43,61003,61004],{},"Trying to save a file in a protected system directory",[43,61006,61007],{},"Using a folder path where Python expects a file path",[43,61009,61010],{},"Trying to open a file that is locked by another program",[43,61012,61013],{},"Trying to access a file with the wrong mode, such as writing where only reading is allowed",[14,61015,60656],{},[40,61017,61018,61021,61024,61027,61030,61033],{},[43,61019,61020],{},"Wrong file path points to a folder instead of a file",[43,61022,61023],{},"No permission to read the file",[43,61025,61026],{},"No permission to write to the target folder",[43,61028,61029],{},"File is open or locked by another program",[43,61031,61032],{},"Trying to modify a protected system file",[43,61034,1357,61035,61037],{},[17,61036,18032],{}," on a directory path",[77,61039,11031],{"id":11030},[14,61041,61042],{},"Opening a protected file with write mode can fail:",[91,61044,61046],{"className":93,"code":61045,"language":95,"meta":96,"style":96},"path = \"\u002Fprotected-folder\u002Fexample.txt\"\n\nwith open(path, \"w\", encoding=\"utf-8\") as file:\n    file.write(\"Hello\")\n",[17,61047,61048,61061,61065,61103],{"__ignoreMap":96},[100,61049,61050,61052,61054,61056,61059],{"class":102,"line":103},[100,61051,19365],{"class":106},[100,61053,111],{"class":110},[100,61055,1708],{"class":205},[100,61057,61058],{"class":209},"\u002Fprotected-folder\u002Fexample.txt",[100,61060,1714],{"class":205},[100,61062,61063],{"class":102,"line":135},[100,61064,139],{"emptyLinePlaceholder":138},[100,61066,61067,61069,61071,61073,61075,61077,61079,61081,61083,61085,61087,61089,61091,61093,61095,61097,61099,61101],{"class":102,"line":142},[100,61068,17521],{"class":145},[100,61070,17524],{"class":114},[100,61072,170],{"class":118},[100,61074,19017],{"class":178},[100,61076,126],{"class":118},[100,61078,1708],{"class":205},[100,61080,19143],{"class":209},[100,61082,206],{"class":205},[100,61084,126],{"class":118},[100,61086,18142],{"class":18141},[100,61088,111],{"class":110},[100,61090,206],{"class":205},[100,61092,15307],{"class":209},[100,61094,206],{"class":205},[100,61096,6155],{"class":118},[100,61098,16224],{"class":145},[100,61100,17550],{"class":17549},[100,61102,149],{"class":118},[100,61104,61105,61107,61109,61111,61113,61115,61117,61119],{"class":102,"line":152},[100,61106,19170],{"class":17549},[100,61108,290],{"class":118},[100,61110,19175],{"class":178},[100,61112,170],{"class":118},[100,61114,206],{"class":205},[100,61116,7683],{"class":209},[100,61118,206],{"class":205},[100,61120,215],{"class":118},[14,61122,61123],{},"If your account cannot write to that folder, Python may raise:",[91,61125,61127],{"className":93,"code":61126,"language":95,"meta":96,"style":96},"PermissionError: [Errno 13] Permission denied\n",[17,61128,61129],{"__ignoreMap":96},[100,61130,61131,61133,61135,61137,61139,61141,61143],{"class":102,"line":103},[100,61132,17494],{"class":191},[100,61134,89],{"class":118},[100,61136,594],{"class":118},[100,61138,17586],{"class":106},[100,61140,23908],{"class":122},[100,61142,17591],{"class":118},[100,61144,61145],{"class":106}," Permission denied\n",[14,61147,61148,61149,61151],{},"Another common mistake is passing a directory to ",[17,61150,18032],{}," instead of a file:",[91,61153,61155],{"className":93,"code":61154,"language":95,"meta":96,"style":96},"path = \"my_folder\"\n\nwith open(path, \"r\", encoding=\"utf-8\") as file:\n    print(file.read())\n",[17,61156,61157,61169,61173,61211],{"__ignoreMap":96},[100,61158,61159,61161,61163,61165,61167],{"class":102,"line":103},[100,61160,19365],{"class":106},[100,61162,111],{"class":110},[100,61164,1708],{"class":205},[100,61166,39610],{"class":209},[100,61168,1714],{"class":205},[100,61170,61171],{"class":102,"line":135},[100,61172,139],{"emptyLinePlaceholder":138},[100,61174,61175,61177,61179,61181,61183,61185,61187,61189,61191,61193,61195,61197,61199,61201,61203,61205,61207,61209],{"class":102,"line":142},[100,61176,17521],{"class":145},[100,61178,17524],{"class":114},[100,61180,170],{"class":118},[100,61182,19017],{"class":178},[100,61184,126],{"class":118},[100,61186,1708],{"class":205},[100,61188,17540],{"class":209},[100,61190,206],{"class":205},[100,61192,126],{"class":118},[100,61194,18142],{"class":18141},[100,61196,111],{"class":110},[100,61198,206],{"class":205},[100,61200,15307],{"class":209},[100,61202,206],{"class":205},[100,61204,6155],{"class":118},[100,61206,16224],{"class":145},[100,61208,17550],{"class":17549},[100,61210,149],{"class":118},[100,61212,61213,61215,61217,61219,61221,61223],{"class":102,"line":152},[100,61214,200],{"class":114},[100,61216,170],{"class":118},[100,61218,17597],{"class":17549},[100,61220,290],{"class":118},[100,61222,17566],{"class":178},[100,61224,3370],{"class":118},[14,61226,61227,61228,290],{},"On some systems, this can raise a permission-related error. On others, you may get a different error such as ",[295,61229,20632,61230,39773],{"href":23069},[100,61231,20635],{},[14,61233,61234],{},"Deleting or renaming files in restricted locations can also cause the same problem.",[77,61236,6244],{"id":6243},[14,61238,61239],{},"Work through these checks one by one:",[40,61241,61242,61245,61248,61251,61254,61257,61260],{},[43,61243,61244],{},"Check that the path points to a real file, not a directory",[43,61246,61247],{},"Make sure the file or folder exists in a location you can access",[43,61249,61250],{},"Use a different folder such as your home directory, Desktop, or project folder",[43,61252,61253],{},"Close other programs that may be using the file",[43,61255,61256],{},"Check whether you need read mode or write mode",[43,61258,61259],{},"Run the script with the correct user account",[43,61261,61262],{},"Only use admin or sudo access when truly necessary",[14,61264,61265],{},"A safer beginner approach is to work inside your project folder:",[91,61267,61269],{"className":93,"code":61268,"language":95,"meta":96,"style":96},"path = \"notes.txt\"\n\nwith open(path, \"w\", encoding=\"utf-8\") as file:\n    file.write(\"This file is in my project folder.\")\n",[17,61270,61271,61283,61287,61325],{"__ignoreMap":96},[100,61272,61273,61275,61277,61279,61281],{"class":102,"line":103},[100,61274,19365],{"class":106},[100,61276,111],{"class":110},[100,61278,1708],{"class":205},[100,61280,22157],{"class":209},[100,61282,1714],{"class":205},[100,61284,61285],{"class":102,"line":135},[100,61286,139],{"emptyLinePlaceholder":138},[100,61288,61289,61291,61293,61295,61297,61299,61301,61303,61305,61307,61309,61311,61313,61315,61317,61319,61321,61323],{"class":102,"line":142},[100,61290,17521],{"class":145},[100,61292,17524],{"class":114},[100,61294,170],{"class":118},[100,61296,19017],{"class":178},[100,61298,126],{"class":118},[100,61300,1708],{"class":205},[100,61302,19143],{"class":209},[100,61304,206],{"class":205},[100,61306,126],{"class":118},[100,61308,18142],{"class":18141},[100,61310,111],{"class":110},[100,61312,206],{"class":205},[100,61314,15307],{"class":209},[100,61316,206],{"class":205},[100,61318,6155],{"class":118},[100,61320,16224],{"class":145},[100,61322,17550],{"class":17549},[100,61324,149],{"class":118},[100,61326,61327,61329,61331,61333,61335,61337,61340,61342],{"class":102,"line":152},[100,61328,19170],{"class":17549},[100,61330,290],{"class":118},[100,61332,19175],{"class":178},[100,61334,170],{"class":118},[100,61336,206],{"class":205},[100,61338,61339],{"class":209},"This file is in my project folder.",[100,61341,206],{"class":205},[100,61343,215],{"class":118},[14,61345,61346],{},"This usually works because you normally have permission to create and edit files in your own working folder.",[14,61348,61349,61350,61353,61354,10873],{},"If the file might not exist, also check ",[295,61351,61352],{"href":24150},"how to test whether a file exists in Python",". If the path is wrong, you may see ",[295,61355,18016,61356,40974],{"href":20691},[100,61357,18019],{},[77,61359,22662],{"id":22661},[14,61361,61362],{},"When you are not sure what is wrong, print and test the path directly.",[675,61364,23970],{"id":23969},[91,61366,61368],{"className":93,"code":61367,"language":95,"meta":96,"style":96},"path = \"example.txt\"\nprint(path)\n",[17,61369,61370,61382],{"__ignoreMap":96},[100,61371,61372,61374,61376,61378,61380],{"class":102,"line":103},[100,61373,19365],{"class":106},[100,61375,111],{"class":110},[100,61377,1708],{"class":205},[100,61379,58418],{"class":209},[100,61381,1714],{"class":205},[100,61383,61384,61386,61388,61390],{"class":102,"line":135},[100,61385,372],{"class":114},[100,61387,170],{"class":118},[100,61389,19017],{"class":178},[100,61391,215],{"class":118},[14,61393,61394],{},"This helps you confirm the program is using the path you expect.",[675,61396,57708],{"id":57707},[91,61398,61400],{"className":93,"code":61399,"language":95,"meta":96,"style":96},"import os\n\npath = \"example.txt\"\nprint(os.path.exists(path))\n",[17,61401,61402,61408,61412,61424],{"__ignoreMap":96},[100,61403,61404,61406],{"class":102,"line":103},[100,61405,9259],{"class":145},[100,61407,18702],{"class":106},[100,61409,61410],{"class":102,"line":135},[100,61411,139],{"emptyLinePlaceholder":138},[100,61413,61414,61416,61418,61420,61422],{"class":102,"line":142},[100,61415,19365],{"class":106},[100,61417,111],{"class":110},[100,61419,1708],{"class":205},[100,61421,58418],{"class":209},[100,61423,1714],{"class":205},[100,61425,61426,61428,61430,61432,61434,61436,61438,61440,61442,61444],{"class":102,"line":152},[100,61427,372],{"class":114},[100,61429,170],{"class":118},[100,61431,18035],{"class":178},[100,61433,290],{"class":118},[100,61435,19017],{"class":2494},[100,61437,290],{"class":118},[100,61439,18114],{"class":178},[100,61441,170],{"class":118},[100,61443,19017],{"class":178},[100,61445,182],{"class":118},[14,61447,19590,61448,61450],{},[17,61449,1649],{},", the path is wrong or the file does not exist.",[675,61452,61454],{"id":61453},"_3-check-whether-it-is-a-file-or-a-directory","3. Check whether it is a file or a directory",[91,61456,61458],{"className":93,"code":61457,"language":95,"meta":96,"style":96},"import os\n\npath = \"example.txt\"\n\nprint(\"Exists:\", os.path.exists(path))\nprint(\"Is file:\", os.path.isfile(path))\nprint(\"Is directory:\", os.path.isdir(path))\n",[17,61459,61460,61466,61470,61482,61486,61516,61546],{"__ignoreMap":96},[100,61461,61462,61464],{"class":102,"line":103},[100,61463,9259],{"class":145},[100,61465,18702],{"class":106},[100,61467,61468],{"class":102,"line":135},[100,61469,139],{"emptyLinePlaceholder":138},[100,61471,61472,61474,61476,61478,61480],{"class":102,"line":142},[100,61473,19365],{"class":106},[100,61475,111],{"class":110},[100,61477,1708],{"class":205},[100,61479,58418],{"class":209},[100,61481,1714],{"class":205},[100,61483,61484],{"class":102,"line":152},[100,61485,139],{"emptyLinePlaceholder":138},[100,61487,61488,61490,61492,61494,61496,61498,61500,61502,61504,61506,61508,61510,61512,61514],{"class":102,"line":164},[100,61489,372],{"class":114},[100,61491,170],{"class":118},[100,61493,206],{"class":205},[100,61495,22993],{"class":209},[100,61497,206],{"class":205},[100,61499,126],{"class":118},[100,61501,19012],{"class":178},[100,61503,290],{"class":118},[100,61505,19017],{"class":2494},[100,61507,290],{"class":118},[100,61509,18114],{"class":178},[100,61511,170],{"class":118},[100,61513,19017],{"class":178},[100,61515,182],{"class":118},[100,61517,61518,61520,61522,61524,61526,61528,61530,61532,61534,61536,61538,61540,61542,61544],{"class":102,"line":185},[100,61519,372],{"class":114},[100,61521,170],{"class":118},[100,61523,206],{"class":205},[100,61525,40842],{"class":209},[100,61527,206],{"class":205},[100,61529,126],{"class":118},[100,61531,19012],{"class":178},[100,61533,290],{"class":118},[100,61535,19017],{"class":2494},[100,61537,290],{"class":118},[100,61539,24191],{"class":178},[100,61541,170],{"class":118},[100,61543,19017],{"class":178},[100,61545,182],{"class":118},[100,61547,61548,61550,61552,61554,61556,61558,61560,61562,61564,61566,61568,61570,61572,61574],{"class":102,"line":197},[100,61549,372],{"class":114},[100,61551,170],{"class":118},[100,61553,206],{"class":205},[100,61555,40873],{"class":209},[100,61557,206],{"class":205},[100,61559,126],{"class":118},[100,61561,19012],{"class":178},[100,61563,290],{"class":118},[100,61565,19017],{"class":2494},[100,61567,290],{"class":118},[100,61569,24218],{"class":178},[100,61571,170],{"class":118},[100,61573,19017],{"class":178},[100,61575,182],{"class":118},[14,61577,61578,61579,61581],{},"This is important because ",[17,61580,18032],{}," expects a file path, not a directory path.",[675,61583,61585],{"id":61584},"_4-check-your-current-working-folder","4. Check your current working folder",[91,61587,61588],{"className":93,"code":18693,"language":95,"meta":96,"style":96},[17,61589,61590,61596,61600],{"__ignoreMap":96},[100,61591,61592,61594],{"class":102,"line":103},[100,61593,9259],{"class":145},[100,61595,18702],{"class":106},[100,61597,61598],{"class":102,"line":135},[100,61599,139],{"emptyLinePlaceholder":138},[100,61601,61602,61604,61606,61608,61610,61612],{"class":102,"line":142},[100,61603,372],{"class":114},[100,61605,170],{"class":118},[100,61607,18035],{"class":178},[100,61609,290],{"class":118},[100,61611,18719],{"class":178},[100,61613,3370],{"class":118},[14,61615,61616],{},"Sometimes the relative path is fine, but your script is running from a different folder than you expected.",[675,61618,61620],{"id":61619},"_5-try-a-simple-read-test","5. Try a simple read test",[91,61622,61624],{"className":93,"code":61623,"language":95,"meta":96,"style":96},"path = \"example.txt\"\n\ntry:\n    with open(path, \"r\", encoding=\"utf-8\") as f:\n        print(f.read())\nexcept Exception as e:\n    print(type(e).__name__)\n    print(e)\n",[17,61625,61626,61638,61642,61648,61687,61701,61713,61731],{"__ignoreMap":96},[100,61627,61628,61630,61632,61634,61636],{"class":102,"line":103},[100,61629,19365],{"class":106},[100,61631,111],{"class":110},[100,61633,1708],{"class":205},[100,61635,58418],{"class":209},[100,61637,1714],{"class":205},[100,61639,61640],{"class":102,"line":135},[100,61641,139],{"emptyLinePlaceholder":138},[100,61643,61644,61646],{"class":102,"line":142},[100,61645,146],{"class":145},[100,61647,149],{"class":118},[100,61649,61650,61652,61654,61656,61658,61660,61662,61664,61666,61668,61670,61672,61674,61676,61678,61680,61682,61685],{"class":102,"line":152},[100,61651,18121],{"class":145},[100,61653,17524],{"class":114},[100,61655,170],{"class":118},[100,61657,19017],{"class":178},[100,61659,126],{"class":118},[100,61661,1708],{"class":205},[100,61663,17540],{"class":209},[100,61665,206],{"class":205},[100,61667,126],{"class":118},[100,61669,18142],{"class":18141},[100,61671,111],{"class":110},[100,61673,206],{"class":205},[100,61675,15307],{"class":209},[100,61677,206],{"class":205},[100,61679,6155],{"class":118},[100,61681,16224],{"class":145},[100,61683,61684],{"class":106}," f",[100,61686,149],{"class":118},[100,61688,61689,61691,61693,61695,61697,61699],{"class":102,"line":164},[100,61690,167],{"class":114},[100,61692,170],{"class":118},[100,61694,18199],{"class":178},[100,61696,290],{"class":118},[100,61698,17566],{"class":178},[100,61700,3370],{"class":118},[100,61702,61703,61705,61707,61709,61711],{"class":102,"line":185},[100,61704,188],{"class":145},[100,61706,16221],{"class":191},[100,61708,16224],{"class":145},[100,61710,16227],{"class":106},[100,61712,149],{"class":118},[100,61714,61715,61717,61719,61721,61723,61725,61727,61729],{"class":102,"line":197},[100,61716,200],{"class":114},[100,61718,170],{"class":118},[100,61720,1250],{"class":191},[100,61722,170],{"class":118},[100,61724,14770],{"class":178},[100,61726,5799],{"class":118},[100,61728,16246],{"class":527},[100,61730,215],{"class":118},[100,61732,61733,61735,61737,61739],{"class":102,"line":771},[100,61734,200],{"class":114},[100,61736,170],{"class":118},[100,61738,14770],{"class":178},[100,61740,215],{"class":118},[14,61742,61743],{},"This shows the full error clearly and helps you confirm whether the problem is the path, the file type, or permissions.",[77,61745,61747],{"id":61746},"safe-beginner-advice","Safe beginner advice",[14,61749,61750],{},"If you are new to Python, these habits will help you avoid permission problems:",[40,61752,61753,61756,61759,61762],{},[43,61754,61755],{},"Prefer working inside your own project folder",[43,61757,61758],{},"Avoid protected locations until you understand permissions better",[43,61760,61761],{},"Do not solve every permission problem by running as administrator",[43,61763,61764],{},"If the path comes from user input, validate it before using it",[14,61766,61767,61768,61771],{},"Running as administrator or with ",[17,61769,61770],{},"sudo"," can sometimes make the error go away, but it is not the best first fix. It is safer to use files and folders that your account already controls.",[14,61773,61774,61775,61779,61780,290],{},"If you are dealing with different path problems, you may also want to read ",[295,61776,40979,61777,40983],{"href":23081},[100,61778,40982],{}," or the broader guide to ",[295,61781,61782],{"href":59578},"OSError in Python: causes and fixes",[77,61784,1514],{"id":1513},[675,61786,61788],{"id":61787},"why-do-i-get-permissionerror-even-when-the-file-exists","Why do I get PermissionError even when the file exists?",[14,61790,61791],{},"Because existence and permission are different. The file may be there, but your program may not be allowed to read or change it.",[675,61793,61795],{"id":61794},"can-a-folder-path-cause-this-error","Can a folder path cause this error?",[14,61797,61798,61799,61801,61802,61804],{},"Yes. If you pass a directory to ",[17,61800,18032],{},", Python may fail because ",[17,61803,18032],{}," expects a file path.",[675,61806,61808],{"id":61807},"should-i-always-run-python-as-administrator-to-fix-this","Should I always run Python as administrator to fix this?",[14,61810,61811],{},"No. It is better to use a file location you already have access to. Admin access should be a last resort.",[675,61813,61815],{"id":61814},"can-another-program-cause-permissionerror","Can another program cause PermissionError?",[14,61817,61818],{},"Yes. A file can be locked by another app, which can stop Python from reading, writing, renaming, or deleting it.",[77,61820,1554],{"id":1553},[40,61822,61823,61827,61831,61837,61843,61849,61855],{},[43,61824,61825],{},[295,61826,23157],{"href":24150},[43,61828,61829],{},[295,61830,20596],{"href":23471},[43,61832,61833],{},[295,61834,1572,61835,1576],{"href":24735},[17,61836,18032],{},[43,61838,61839],{},[295,61840,18016,61841,40974],{"href":20691},[100,61842,18019],{},[43,61844,61845],{},[295,61846,20632,61847,39773],{"href":23069},[100,61848,20635],{},[43,61850,61851],{},[295,61852,40979,61853,40983],{"href":23081},[100,61854,40982],{},[43,61856,61857],{},[295,61858,61782],{"href":59578},[1589,61860,61861],{},"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 .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 .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 pre.shiki code .s99_P, html code.shiki .s99_P{--shiki-light:#90A4AE;--shiki-light-font-style:italic;--shiki-default:#E36209;--shiki-default-font-style:inherit;--shiki-dark:#FFAB70;--shiki-dark-font-style:inherit}html pre.shiki code .sMMDD, html code.shiki .sMMDD{--shiki-light:#90A4AE;--shiki-default:#E36209;--shiki-dark:#FFAB70}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 .skxfh, html code.shiki .skxfh{--shiki-light:#E53935;--shiki-default:#24292E;--shiki-dark:#E1E4E8}html pre.shiki code .s_hVV, html code.shiki .s_hVV{--shiki-light:#90A4AE;--shiki-default:#005CC5;--shiki-dark:#79B8FF}",{"title":96,"searchDepth":135,"depth":135,"links":61863},[61864,61865,61866,61867,61868,61869,61876,61877,61883],{"id":79,"depth":135,"text":80},{"id":5880,"depth":135,"text":5881},{"id":29974,"depth":135,"text":29975},{"id":11030,"depth":135,"text":11031},{"id":6243,"depth":135,"text":6244},{"id":22661,"depth":135,"text":22662,"children":61870},[61871,61872,61873,61874,61875],{"id":23969,"depth":142,"text":23970},{"id":57707,"depth":142,"text":57708},{"id":61453,"depth":142,"text":61454},{"id":61584,"depth":142,"text":61585},{"id":61619,"depth":142,"text":61620},{"id":61746,"depth":135,"text":61747},{"id":1513,"depth":135,"text":1514,"children":61878},[61879,61880,61881,61882],{"id":61787,"depth":142,"text":61788},{"id":61794,"depth":142,"text":61795},{"id":61807,"depth":142,"text":61808},{"id":61814,"depth":142,"text":61815},{"id":1553,"depth":135,"text":1554},"Master permissionerror errno 13 permission denied fix in our comprehensive Python beginner guide.",{},{"title":60819,"description":61884},"errors\u002Fpermissionerror-errno-13-permission-denied-fix","lsX7kIDbXw2Kh8bY1qyKy3EQRaMRGjb3eNRTDYpnZhc",{"id":61890,"title":61891,"body":61892,"description":63672,"extension":1623,"meta":63673,"navigation":138,"path":23057,"seo":63674,"stem":63675,"__hash__":63676},"content\u002Ferrors\u002Fpermissionerror-in-python-causes-and-fixes.md","PermissionError in Python: Causes and Fixes",{"type":7,"value":61893,"toc":63633},[61894,61897,61902,61905,61925,61928,61948,61951,61965,61967,61970,62108,62111,62115,62120,62122,62138,62141,62143,62148,62168,62178,62182,62186,62248,62251,62280,62284,62350,62352,62381,62385,62446,62449,62478,62481,62485,62488,62490,62503,62508,62702,62704,62723,62728,62732,62735,62738,62752,62754,62820,62823,62830,62834,62837,62840,62854,62857,62868,62871,62882,62885,62889,62892,62903,62907,62910,62926,62929,62933,62936,62956,62959,62963,62966,62969,62983,62986,62989,63000,63003,63007,63010,63012,63026,63029,63031,63079,63081,63114,63123,63125,63128,63152,63156,63159,63274,63278,63281,63284,63288,63291,63302,63306,63312,63471,63474,63479,63481,63484,63515,63517,63521,63524,63530,63533,63547,63554,63563,63567,63570,63582,63586,63589,63600,63602,63630],[10,61895,61891],{"id":61896},"permissionerror-in-python-causes-and-fixes",[14,61898,61899,61901],{},[17,61900,17494],{}," means Python tried to access a file or folder, but your user account was not allowed to do it.",[14,61903,61904],{},"This usually happens when you:",[40,61906,61907,61910,61913,61916,61922],{},[43,61908,61909],{},"open a protected file",[43,61911,61912],{},"write to a restricted folder",[43,61914,61915],{},"try to rename or delete something you do not control",[43,61917,61918,61919,61921],{},"pass a folder path to ",[17,61920,18032],{}," by mistake",[43,61923,61924],{},"use a file that another program is already using",[14,61926,61927],{},"A common message looks like this:",[91,61929,61930],{"className":93,"code":61126,"language":95,"meta":96,"style":96},[17,61931,61932],{"__ignoreMap":96},[100,61933,61934,61936,61938,61940,61942,61944,61946],{"class":102,"line":103},[100,61935,17494],{"class":191},[100,61937,89],{"class":118},[100,61939,594],{"class":118},[100,61941,17586],{"class":106},[100,61943,23908],{"class":122},[100,61945,17591],{"class":118},[100,61947,61145],{"class":106},[14,61949,61950],{},"The goal is to quickly find out whether the problem is:",[40,61952,61953,61956,61959,61962],{},[43,61954,61955],{},"the path",[43,61957,61958],{},"the file type",[43,61960,61961],{},"file permissions",[43,61963,61964],{},"or another program locking the file",[77,61966,80],{"id":79},[14,61968,61969],{},"Try this first:",[91,61971,61973],{"className":93,"code":61972,"language":95,"meta":96,"style":96},"from pathlib import Path\n\npath = Path(\"example.txt\")\n\nif path.exists() and path.is_file():\n    with open(path, \"r\", encoding=\"utf-8\") as file:\n        print(file.read())\nelse:\n    print(\"Check the path and permissions.\")\n",[17,61974,61975,61985,61989,62007,62011,62035,62073,62087,62093],{"__ignoreMap":96},[100,61976,61977,61979,61981,61983],{"class":102,"line":103},[100,61978,10201],{"class":145},[100,61980,18068],{"class":106},[100,61982,9259],{"class":145},[100,61984,18073],{"class":106},[100,61986,61987],{"class":102,"line":135},[100,61988,139],{"emptyLinePlaceholder":138},[100,61990,61991,61993,61995,61997,61999,62001,62003,62005],{"class":102,"line":142},[100,61992,19365],{"class":106},[100,61994,111],{"class":110},[100,61996,18087],{"class":178},[100,61998,170],{"class":118},[100,62000,206],{"class":205},[100,62002,58418],{"class":209},[100,62004,206],{"class":205},[100,62006,215],{"class":118},[100,62008,62009],{"class":102,"line":152},[100,62010,139],{"emptyLinePlaceholder":138},[100,62012,62013,62015,62017,62019,62021,62023,62026,62028,62030,62033],{"class":102,"line":164},[100,62014,2736],{"class":145},[100,62016,23294],{"class":106},[100,62018,290],{"class":118},[100,62020,18114],{"class":178},[100,62022,7562],{"class":118},[100,62024,62025],{"class":110}," and",[100,62027,23294],{"class":106},[100,62029,290],{"class":118},[100,62031,62032],{"class":178},"is_file",[100,62034,1085],{"class":118},[100,62036,62037,62039,62041,62043,62045,62047,62049,62051,62053,62055,62057,62059,62061,62063,62065,62067,62069,62071],{"class":102,"line":185},[100,62038,18121],{"class":145},[100,62040,17524],{"class":114},[100,62042,170],{"class":118},[100,62044,19017],{"class":178},[100,62046,126],{"class":118},[100,62048,1708],{"class":205},[100,62050,17540],{"class":209},[100,62052,206],{"class":205},[100,62054,126],{"class":118},[100,62056,18142],{"class":18141},[100,62058,111],{"class":110},[100,62060,206],{"class":205},[100,62062,15307],{"class":209},[100,62064,206],{"class":205},[100,62066,6155],{"class":118},[100,62068,16224],{"class":145},[100,62070,17550],{"class":17549},[100,62072,149],{"class":118},[100,62074,62075,62077,62079,62081,62083,62085],{"class":102,"line":197},[100,62076,167],{"class":114},[100,62078,170],{"class":118},[100,62080,17597],{"class":17549},[100,62082,290],{"class":118},[100,62084,17566],{"class":178},[100,62086,3370],{"class":118},[100,62088,62089,62091],{"class":102,"line":771},[100,62090,4944],{"class":145},[100,62092,149],{"class":118},[100,62094,62095,62097,62099,62101,62104,62106],{"class":102,"line":787},[100,62096,200],{"class":114},[100,62098,170],{"class":118},[100,62100,206],{"class":205},[100,62102,62103],{"class":209},"Check the path and permissions.",[100,62105,206],{"class":205},[100,62107,215],{"class":118},[14,62109,62110],{},"Use a real file path, make sure it is a file and not a folder, and confirm your user account has permission to read it.",[77,62112,62114],{"id":62113},"what-permissionerror-means","What PermissionError means",[14,62116,62117,62119],{},[17,62118,17494],{}," happens when Python tries to access something the current user is not allowed to use.",[14,62121,266],{},[40,62123,62124,62127,62132],{},[43,62125,62126],{},"It often appears while opening, writing, renaming, or deleting files and folders.",[43,62128,62129,62130,290],{},"It is a subclass of ",[17,62131,17509],{},[43,62133,62134,62135,290],{},"The most common message is ",[17,62136,62137],{},"[Errno 13] Permission denied",[14,62139,62140],{},"In simple terms, Python is saying: \"I found this path, but I am not allowed to use it in the way you asked.\"",[77,62142,18290],{"id":18289},[14,62144,62145,62146,26295],{},"You will often see ",[17,62147,17494],{},[40,62149,62150,62153,62156,62159,62162,62165],{},[43,62151,62152],{},"Opening a file without read permission",[43,62154,62155],{},"Writing to a file or folder without write permission",[43,62157,62158],{},"Trying to delete or rename a protected file",[43,62160,62161],{},"Trying to open a folder as if it were a file",[43,62163,62164],{},"Accessing system locations that require admin rights",[43,62166,62167],{},"Trying to use a file that another program has locked",[14,62169,62170,62171,28239,62174,290],{},"If you are new to file handling, it also helps to understand the basics of ",[295,62172,62173],{"href":22656},"Python file handling: read and write",[295,62175,1572,62176,6354],{"href":18280},[17,62177,18032],{},[77,62179,62181],{"id":62180},"example-that-causes-permissionerror","Example that causes PermissionError",[675,62183,62185],{"id":62184},"example-1-opening-a-protected-file","Example 1: Opening a protected file",[91,62187,62189],{"className":93,"code":62188,"language":95,"meta":96,"style":96},"with open(\"\u002Froot\u002Fsecret.txt\", \"r\", encoding=\"utf-8\") as file:\n    print(file.read())\n",[17,62190,62191,62234],{"__ignoreMap":96},[100,62192,62193,62195,62197,62199,62201,62204,62206,62208,62210,62212,62214,62216,62218,62220,62222,62224,62226,62228,62230,62232],{"class":102,"line":103},[100,62194,17521],{"class":145},[100,62196,17524],{"class":114},[100,62198,170],{"class":118},[100,62200,206],{"class":205},[100,62202,62203],{"class":209},"\u002Froot\u002Fsecret.txt",[100,62205,206],{"class":205},[100,62207,126],{"class":118},[100,62209,1708],{"class":205},[100,62211,17540],{"class":209},[100,62213,206],{"class":205},[100,62215,126],{"class":118},[100,62217,18142],{"class":18141},[100,62219,111],{"class":110},[100,62221,206],{"class":205},[100,62223,15307],{"class":209},[100,62225,206],{"class":205},[100,62227,6155],{"class":118},[100,62229,16224],{"class":145},[100,62231,17550],{"class":17549},[100,62233,149],{"class":118},[100,62235,62236,62238,62240,62242,62244,62246],{"class":102,"line":135},[100,62237,200],{"class":114},[100,62239,170],{"class":118},[100,62241,17597],{"class":17549},[100,62243,290],{"class":118},[100,62245,17566],{"class":178},[100,62247,3370],{"class":118},[14,62249,62250],{},"On many systems, this may fail with:",[91,62252,62254],{"className":93,"code":62253,"language":95,"meta":96,"style":96},"PermissionError: [Errno 13] Permission denied: '\u002Froot\u002Fsecret.txt'\n",[17,62255,62256],{"__ignoreMap":96},[100,62257,62258,62260,62262,62264,62266,62268,62270,62272,62274,62276,62278],{"class":102,"line":103},[100,62259,17494],{"class":191},[100,62261,89],{"class":118},[100,62263,594],{"class":118},[100,62265,17586],{"class":106},[100,62267,23908],{"class":122},[100,62269,17591],{"class":118},[100,62271,23913],{"class":106},[100,62273,89],{"class":118},[100,62275,1274],{"class":205},[100,62277,62203],{"class":209},[100,62279,3925],{"class":205},[675,62281,62283],{"id":62282},"example-2-writing-inside-a-restricted-folder","Example 2: Writing inside a restricted folder",[91,62285,62287],{"className":93,"code":62286,"language":95,"meta":96,"style":96},"with open(\"\u002Fsystem_file.txt\", \"w\", encoding=\"utf-8\") as file:\n    file.write(\"Hello\")\n",[17,62288,62289,62332],{"__ignoreMap":96},[100,62290,62291,62293,62295,62297,62299,62302,62304,62306,62308,62310,62312,62314,62316,62318,62320,62322,62324,62326,62328,62330],{"class":102,"line":103},[100,62292,17521],{"class":145},[100,62294,17524],{"class":114},[100,62296,170],{"class":118},[100,62298,206],{"class":205},[100,62300,62301],{"class":209},"\u002Fsystem_file.txt",[100,62303,206],{"class":205},[100,62305,126],{"class":118},[100,62307,1708],{"class":205},[100,62309,19143],{"class":209},[100,62311,206],{"class":205},[100,62313,126],{"class":118},[100,62315,18142],{"class":18141},[100,62317,111],{"class":110},[100,62319,206],{"class":205},[100,62321,15307],{"class":209},[100,62323,206],{"class":205},[100,62325,6155],{"class":118},[100,62327,16224],{"class":145},[100,62329,17550],{"class":17549},[100,62331,149],{"class":118},[100,62333,62334,62336,62338,62340,62342,62344,62346,62348],{"class":102,"line":135},[100,62335,19170],{"class":17549},[100,62337,290],{"class":118},[100,62339,19175],{"class":178},[100,62341,170],{"class":118},[100,62343,206],{"class":205},[100,62345,7683],{"class":209},[100,62347,206],{"class":205},[100,62349,215],{"class":118},[14,62351,32015],{},[91,62353,62355],{"className":93,"code":62354,"language":95,"meta":96,"style":96},"PermissionError: [Errno 13] Permission denied: '\u002Fsystem_file.txt'\n",[17,62356,62357],{"__ignoreMap":96},[100,62358,62359,62361,62363,62365,62367,62369,62371,62373,62375,62377,62379],{"class":102,"line":103},[100,62360,17494],{"class":191},[100,62362,89],{"class":118},[100,62364,594],{"class":118},[100,62366,17586],{"class":106},[100,62368,23908],{"class":122},[100,62370,17591],{"class":118},[100,62372,23913],{"class":106},[100,62374,89],{"class":118},[100,62376,1274],{"class":205},[100,62378,62301],{"class":209},[100,62380,3925],{"class":205},[675,62382,62384],{"id":62383},"example-3-opening-a-directory-by-mistake","Example 3: Opening a directory by mistake",[91,62386,62388],{"className":93,"code":62387,"language":95,"meta":96,"style":96},"with open(\".\", \"r\", encoding=\"utf-8\") as file:\n    print(file.read())\n",[17,62389,62390,62432],{"__ignoreMap":96},[100,62391,62392,62394,62396,62398,62400,62402,62404,62406,62408,62410,62412,62414,62416,62418,62420,62422,62424,62426,62428,62430],{"class":102,"line":103},[100,62393,17521],{"class":145},[100,62395,17524],{"class":114},[100,62397,170],{"class":118},[100,62399,206],{"class":205},[100,62401,290],{"class":209},[100,62403,206],{"class":205},[100,62405,126],{"class":118},[100,62407,1708],{"class":205},[100,62409,17540],{"class":209},[100,62411,206],{"class":205},[100,62413,126],{"class":118},[100,62415,18142],{"class":18141},[100,62417,111],{"class":110},[100,62419,206],{"class":205},[100,62421,15307],{"class":209},[100,62423,206],{"class":205},[100,62425,6155],{"class":118},[100,62427,16224],{"class":145},[100,62429,17550],{"class":17549},[100,62431,149],{"class":118},[100,62433,62434,62436,62438,62440,62442,62444],{"class":102,"line":135},[100,62435,200],{"class":114},[100,62437,170],{"class":118},[100,62439,17597],{"class":17549},[100,62441,290],{"class":118},[100,62443,17566],{"class":178},[100,62445,3370],{"class":118},[14,62447,62448],{},"On some systems, this raises:",[91,62450,62452],{"className":93,"code":62451,"language":95,"meta":96,"style":96},"PermissionError: [Errno 13] Permission denied: '.'\n",[17,62453,62454],{"__ignoreMap":96},[100,62455,62456,62458,62460,62462,62464,62466,62468,62470,62472,62474,62476],{"class":102,"line":103},[100,62457,17494],{"class":191},[100,62459,89],{"class":118},[100,62461,594],{"class":118},[100,62463,17586],{"class":106},[100,62465,23908],{"class":122},[100,62467,17591],{"class":118},[100,62469,23913],{"class":106},[100,62471,89],{"class":118},[100,62473,1274],{"class":205},[100,62475,290],{"class":209},[100,62477,3925],{"class":205},[14,62479,62480],{},"The traceback will point to the exact line that failed. Read that line carefully. It tells you which file operation caused the problem.",[77,62482,62484],{"id":62483},"fix-1-check-the-file-path-carefully","Fix 1: Check the file path carefully",[14,62486,62487],{},"A very common cause is using the wrong path.",[14,62489,21221],{},[40,62491,62492,62495,62500],{},[43,62493,62494],{},"the path exists",[43,62496,62497,62498],{},"the path points to a file when using ",[17,62499,18032],{},[43,62501,62502],{},"you did not accidentally pass a folder path",[14,62504,241,62505,62507],{},[17,62506,18038],{}," to check:",[91,62509,62511],{"className":93,"code":62510,"language":95,"meta":96,"style":96},"from pathlib import Path\n\npath = Path(\"example.txt\")\n\nprint(path)\nprint(path.exists())\nprint(path.is_file())\nprint(path.is_dir())\n\nif path.exists() and path.is_file():\n    with open(path, \"r\", encoding=\"utf-8\") as file:\n        print(file.read())\nelse:\n    print(\"The path is missing or it is not a file.\")\n",[17,62512,62513,62523,62527,62545,62549,62559,62573,62587,62602,62606,62628,62666,62680,62686],{"__ignoreMap":96},[100,62514,62515,62517,62519,62521],{"class":102,"line":103},[100,62516,10201],{"class":145},[100,62518,18068],{"class":106},[100,62520,9259],{"class":145},[100,62522,18073],{"class":106},[100,62524,62525],{"class":102,"line":135},[100,62526,139],{"emptyLinePlaceholder":138},[100,62528,62529,62531,62533,62535,62537,62539,62541,62543],{"class":102,"line":142},[100,62530,19365],{"class":106},[100,62532,111],{"class":110},[100,62534,18087],{"class":178},[100,62536,170],{"class":118},[100,62538,206],{"class":205},[100,62540,58418],{"class":209},[100,62542,206],{"class":205},[100,62544,215],{"class":118},[100,62546,62547],{"class":102,"line":152},[100,62548,139],{"emptyLinePlaceholder":138},[100,62550,62551,62553,62555,62557],{"class":102,"line":164},[100,62552,372],{"class":114},[100,62554,170],{"class":118},[100,62556,19017],{"class":178},[100,62558,215],{"class":118},[100,62560,62561,62563,62565,62567,62569,62571],{"class":102,"line":185},[100,62562,372],{"class":114},[100,62564,170],{"class":118},[100,62566,19017],{"class":178},[100,62568,290],{"class":118},[100,62570,18114],{"class":178},[100,62572,3370],{"class":118},[100,62574,62575,62577,62579,62581,62583,62585],{"class":102,"line":197},[100,62576,372],{"class":114},[100,62578,170],{"class":118},[100,62580,19017],{"class":178},[100,62582,290],{"class":118},[100,62584,62032],{"class":178},[100,62586,3370],{"class":118},[100,62588,62589,62591,62593,62595,62597,62600],{"class":102,"line":771},[100,62590,372],{"class":114},[100,62592,170],{"class":118},[100,62594,19017],{"class":178},[100,62596,290],{"class":118},[100,62598,62599],{"class":178},"is_dir",[100,62601,3370],{"class":118},[100,62603,62604],{"class":102,"line":787},[100,62605,139],{"emptyLinePlaceholder":138},[100,62607,62608,62610,62612,62614,62616,62618,62620,62622,62624,62626],{"class":102,"line":5816},[100,62609,2736],{"class":145},[100,62611,23294],{"class":106},[100,62613,290],{"class":118},[100,62615,18114],{"class":178},[100,62617,7562],{"class":118},[100,62619,62025],{"class":110},[100,62621,23294],{"class":106},[100,62623,290],{"class":118},[100,62625,62032],{"class":178},[100,62627,1085],{"class":118},[100,62629,62630,62632,62634,62636,62638,62640,62642,62644,62646,62648,62650,62652,62654,62656,62658,62660,62662,62664],{"class":102,"line":5833},[100,62631,18121],{"class":145},[100,62633,17524],{"class":114},[100,62635,170],{"class":118},[100,62637,19017],{"class":178},[100,62639,126],{"class":118},[100,62641,1708],{"class":205},[100,62643,17540],{"class":209},[100,62645,206],{"class":205},[100,62647,126],{"class":118},[100,62649,18142],{"class":18141},[100,62651,111],{"class":110},[100,62653,206],{"class":205},[100,62655,15307],{"class":209},[100,62657,206],{"class":205},[100,62659,6155],{"class":118},[100,62661,16224],{"class":145},[100,62663,17550],{"class":17549},[100,62665,149],{"class":118},[100,62667,62668,62670,62672,62674,62676,62678],{"class":102,"line":5848},[100,62669,167],{"class":114},[100,62671,170],{"class":118},[100,62673,17597],{"class":17549},[100,62675,290],{"class":118},[100,62677,17566],{"class":178},[100,62679,3370],{"class":118},[100,62681,62682,62684],{"class":102,"line":5855},[100,62683,4944],{"class":145},[100,62685,149],{"class":118},[100,62687,62689,62691,62693,62695,62698,62700],{"class":102,"line":62688},14,[100,62690,200],{"class":114},[100,62692,170],{"class":118},[100,62694,206],{"class":205},[100,62696,62697],{"class":209},"The path is missing or it is not a file.",[100,62699,206],{"class":205},[100,62701,215],{"class":118},[14,62703,26197],{},[40,62705,62706,62712,62717],{},[43,62707,62708,62711],{},[17,62709,62710],{},"exists()"," checks whether the path is real",[43,62713,62714,59299],{},[17,62715,62716],{},"is_file()",[43,62718,62719,62722],{},[17,62720,62721],{},"is_dir()"," checks whether it is a folder",[14,62724,62725,62726,290],{},"If you want to get better at this, see ",[295,62727,18286],{"href":18285},[77,62729,62731],{"id":62730},"fix-2-use-a-location-your-user-can-access","Fix 2: Use a location your user can access",[14,62733,62734],{},"If your code writes to a protected folder, move the file to a normal location first.",[14,62736,62737],{},"Good beginner-friendly places:",[40,62739,62740,62743,62746,62749],{},[43,62741,62742],{},"your project folder",[43,62744,62745],{},"your home folder",[43,62747,62748],{},"your desktop",[43,62750,62751],{},"a folder you created yourself",[14,62753,1844],{},[91,62755,62757],{"className":93,"code":62756,"language":95,"meta":96,"style":96},"with open(\"notes.txt\", \"w\", encoding=\"utf-8\") as file:\n    file.write(\"This file is in the current project folder.\")\n",[17,62758,62759,62801],{"__ignoreMap":96},[100,62760,62761,62763,62765,62767,62769,62771,62773,62775,62777,62779,62781,62783,62785,62787,62789,62791,62793,62795,62797,62799],{"class":102,"line":103},[100,62762,17521],{"class":145},[100,62764,17524],{"class":114},[100,62766,170],{"class":118},[100,62768,206],{"class":205},[100,62770,22157],{"class":209},[100,62772,206],{"class":205},[100,62774,126],{"class":118},[100,62776,1708],{"class":205},[100,62778,19143],{"class":209},[100,62780,206],{"class":205},[100,62782,126],{"class":118},[100,62784,18142],{"class":18141},[100,62786,111],{"class":110},[100,62788,206],{"class":205},[100,62790,15307],{"class":209},[100,62792,206],{"class":205},[100,62794,6155],{"class":118},[100,62796,16224],{"class":145},[100,62798,17550],{"class":17549},[100,62800,149],{"class":118},[100,62802,62803,62805,62807,62809,62811,62813,62816,62818],{"class":102,"line":135},[100,62804,19170],{"class":17549},[100,62806,290],{"class":118},[100,62808,19175],{"class":178},[100,62810,170],{"class":118},[100,62812,206],{"class":205},[100,62814,62815],{"class":209},"This file is in the current project folder.",[100,62817,206],{"class":205},[100,62819,215],{"class":118},[14,62821,62822],{},"This is much safer than trying to write into system folders.",[14,62824,62825,62826,290],{},"If you are trying to save data, see ",[295,62827,62829],{"href":62828},"\u002Fhow-to\u002Fhow-to-write-to-a-file-in-python\u002F","how to write to a file in Python",[77,62831,62833],{"id":62832},"fix-3-close-programs-that-may-be-using-the-file","Fix 3: Close programs that may be using the file",[14,62835,62836],{},"Sometimes the path and permissions are correct, but another program is using the file.",[14,62838,62839],{},"This is common when the file is open in:",[40,62841,62842,62845,62848,62851],{},[43,62843,62844],{},"a text editor",[43,62846,62847],{},"Excel or another spreadsheet program",[43,62849,62850],{},"another Python script",[43,62852,62853],{},"a backup or sync tool",[14,62855,62856],{},"This often affects:",[40,62858,62859,62862,62865],{},[43,62860,62861],{},"writing to a file",[43,62863,62864],{},"renaming a file",[43,62866,62867],{},"deleting a file",[14,62869,62870],{},"If you think a file is locked:",[3282,62872,62873,62876,62879],{},[43,62874,62875],{},"Close any program that may be using it.",[43,62877,62878],{},"Run your script again.",[43,62880,62881],{},"If needed, restart the program or your computer.",[14,62883,62884],{},"This issue is especially common on Windows.",[77,62886,62888],{"id":62887},"fix-4-check-file-and-folder-permissions","Fix 4: Check file and folder permissions",[14,62890,62891],{},"Your account needs the right permission for the action you want:",[40,62893,62894,62897,62900],{},[43,62895,62896],{},"read permission to open a file for reading",[43,62898,62899],{},"write permission to change or save a file",[43,62901,62902],{},"permission on the folder when creating or deleting files",[675,62904,62906],{"id":62905},"on-linux-and-macos","On Linux and macOS",[14,62908,62909],{},"You can inspect permissions in the terminal:",[91,62911,62913],{"className":10352,"code":62912,"language":10354,"meta":96,"style":96},"ls -l example.txt\n",[17,62914,62915],{"__ignoreMap":96},[100,62916,62917,62920,62923],{"class":102,"line":103},[100,62918,62919],{"class":10361},"ls",[100,62921,62922],{"class":10364}," -l",[100,62924,62925],{"class":209}," example.txt\n",[14,62927,62928],{},"This shows who can read, write, or execute the file.",[675,62930,62932],{"id":62931},"on-windows","On Windows",[14,62934,62935],{},"You can:",[3282,62937,62938,62941,62947,62953],{},[43,62939,62940],{},"Right-click the file or folder",[43,62942,62943,62944],{},"Open ",[22,62945,62946],{},"Properties",[43,62948,62949,62950],{},"Go to ",[22,62951,62952],{},"Security",[43,62954,62955],{},"Check whether your user account has the needed access",[14,62957,62958],{},"Only change permissions if you understand what you are changing. A safer first step is to move the file into a folder you already control.",[77,62960,62962],{"id":62961},"fix-5-avoid-running-code-in-restricted-system-locations","Fix 5: Avoid running code in restricted system locations",[14,62964,62965],{},"Some folders are protected by the operating system.",[14,62967,62968],{},"Examples include:",[40,62970,62971,62974,62977,62980],{},[43,62972,62973],{},"system directories",[43,62975,62976],{},"application install folders",[43,62978,62979],{},"other users' folders",[43,62981,62982],{},"admin-only locations",[14,62984,62985],{},"For beginner code, do not start there.",[14,62987,62988],{},"Instead:",[40,62990,62991,62994,62997],{},[43,62992,62993],{},"create a normal project folder",[43,62995,62996],{},"store your test files there",[43,62998,62999],{},"make sure you can manually open and edit those files",[14,63001,63002],{},"Running Python as administrator is usually not the best first fix. It can hide the real problem and make your code harder to test safely.",[77,63004,63006],{"id":63005},"how-to-debug-permissionerror-step-by-step","How to debug PermissionError step by step",[14,63008,63009],{},"When you see this error, go through these checks in order.",[675,63011,23970],{"id":23969},[91,63013,63014],{"className":93,"code":57689,"language":95,"meta":96,"style":96},[17,63015,63016],{"__ignoreMap":96},[100,63017,63018,63020,63022,63024],{"class":102,"line":103},[100,63019,372],{"class":114},[100,63021,170],{"class":118},[100,63023,19017],{"class":178},[100,63025,215],{"class":118},[14,63027,63028],{},"Make sure Python is using the path you expect.",[675,63030,57708],{"id":57707},[91,63032,63034],{"className":93,"code":63033,"language":95,"meta":96,"style":96},"from pathlib import Path\n\np = Path(path)\nprint(p.exists())\n",[17,63035,63036,63046,63050,63065],{"__ignoreMap":96},[100,63037,63038,63040,63042,63044],{"class":102,"line":103},[100,63039,10201],{"class":145},[100,63041,18068],{"class":106},[100,63043,9259],{"class":145},[100,63045,18073],{"class":106},[100,63047,63048],{"class":102,"line":135},[100,63049,139],{"emptyLinePlaceholder":138},[100,63051,63052,63055,63057,63059,63061,63063],{"class":102,"line":142},[100,63053,63054],{"class":106},"p ",[100,63056,111],{"class":110},[100,63058,18087],{"class":178},[100,63060,170],{"class":118},[100,63062,19017],{"class":178},[100,63064,215],{"class":118},[100,63066,63067,63069,63071,63073,63075,63077],{"class":102,"line":152},[100,63068,372],{"class":114},[100,63070,170],{"class":118},[100,63072,14],{"class":178},[100,63074,290],{"class":118},[100,63076,18114],{"class":178},[100,63078,3370],{"class":118},[675,63080,57755],{"id":57754},[91,63082,63084],{"className":93,"code":63083,"language":95,"meta":96,"style":96},"print(p.is_file())\nprint(p.is_dir())\n",[17,63085,63086,63100],{"__ignoreMap":96},[100,63087,63088,63090,63092,63094,63096,63098],{"class":102,"line":103},[100,63089,372],{"class":114},[100,63091,170],{"class":118},[100,63093,14],{"class":178},[100,63095,290],{"class":118},[100,63097,62032],{"class":178},[100,63099,3370],{"class":118},[100,63101,63102,63104,63106,63108,63110,63112],{"class":102,"line":135},[100,63103,372],{"class":114},[100,63105,170],{"class":118},[100,63107,14],{"class":178},[100,63109,290],{"class":118},[100,63111,62599],{"class":178},[100,63113,3370],{"class":118},[14,63115,11734,63116,2025,63118,63120,63121,290],{},[17,63117,62721],{},[17,63119,19246],{},", do not pass that path to ",[17,63122,18032],{},[675,63124,61585],{"id":61584},[14,63126,63127],{},"Sometimes a relative path points somewhere unexpected.",[91,63129,63130],{"className":93,"code":19413,"language":95,"meta":96,"style":96},[17,63131,63132,63138],{"__ignoreMap":96},[100,63133,63134,63136],{"class":102,"line":103},[100,63135,9259],{"class":145},[100,63137,18702],{"class":106},[100,63139,63140,63142,63144,63146,63148,63150],{"class":102,"line":135},[100,63141,372],{"class":114},[100,63143,170],{"class":118},[100,63145,18035],{"class":178},[100,63147,290],{"class":118},[100,63149,18719],{"class":178},[100,63151,3370],{"class":118},[675,63153,63155],{"id":63154},"_5-try-read-only-access-first","5. Try read-only access first",[14,63157,63158],{},"If writing fails, test with reading first:",[91,63160,63162],{"className":93,"code":63161,"language":95,"meta":96,"style":96},"from pathlib import Path\n\npath = Path(\"example.txt\")\n\nif path.exists() and path.is_file():\n    with open(path, \"r\", encoding=\"utf-8\") as file:\n        print(file.read())\n",[17,63163,63164,63174,63178,63196,63200,63222,63260],{"__ignoreMap":96},[100,63165,63166,63168,63170,63172],{"class":102,"line":103},[100,63167,10201],{"class":145},[100,63169,18068],{"class":106},[100,63171,9259],{"class":145},[100,63173,18073],{"class":106},[100,63175,63176],{"class":102,"line":135},[100,63177,139],{"emptyLinePlaceholder":138},[100,63179,63180,63182,63184,63186,63188,63190,63192,63194],{"class":102,"line":142},[100,63181,19365],{"class":106},[100,63183,111],{"class":110},[100,63185,18087],{"class":178},[100,63187,170],{"class":118},[100,63189,206],{"class":205},[100,63191,58418],{"class":209},[100,63193,206],{"class":205},[100,63195,215],{"class":118},[100,63197,63198],{"class":102,"line":152},[100,63199,139],{"emptyLinePlaceholder":138},[100,63201,63202,63204,63206,63208,63210,63212,63214,63216,63218,63220],{"class":102,"line":164},[100,63203,2736],{"class":145},[100,63205,23294],{"class":106},[100,63207,290],{"class":118},[100,63209,18114],{"class":178},[100,63211,7562],{"class":118},[100,63213,62025],{"class":110},[100,63215,23294],{"class":106},[100,63217,290],{"class":118},[100,63219,62032],{"class":178},[100,63221,1085],{"class":118},[100,63223,63224,63226,63228,63230,63232,63234,63236,63238,63240,63242,63244,63246,63248,63250,63252,63254,63256,63258],{"class":102,"line":185},[100,63225,18121],{"class":145},[100,63227,17524],{"class":114},[100,63229,170],{"class":118},[100,63231,19017],{"class":178},[100,63233,126],{"class":118},[100,63235,1708],{"class":205},[100,63237,17540],{"class":209},[100,63239,206],{"class":205},[100,63241,126],{"class":118},[100,63243,18142],{"class":18141},[100,63245,111],{"class":110},[100,63247,206],{"class":205},[100,63249,15307],{"class":209},[100,63251,206],{"class":205},[100,63253,6155],{"class":118},[100,63255,16224],{"class":145},[100,63257,17550],{"class":17549},[100,63259,149],{"class":118},[100,63261,63262,63264,63266,63268,63270,63272],{"class":102,"line":197},[100,63263,167],{"class":114},[100,63265,170],{"class":118},[100,63267,17597],{"class":17549},[100,63269,290],{"class":118},[100,63271,17566],{"class":178},[100,63273,3370],{"class":118},[675,63275,63277],{"id":63276},"_6-try-the-same-code-with-a-simple-local-file","6. Try the same code with a simple local file",[14,63279,63280],{},"Put a test file in your project folder and try again.",[14,63282,63283],{},"If that works, the problem is likely the original path or permissions.",[675,63285,63287],{"id":63286},"_7-read-the-full-traceback","7. Read the full traceback",[14,63289,63290],{},"The traceback tells you:",[40,63292,63293,63296,63299],{},[43,63294,63295],{},"which line failed",[43,63297,63298],{},"which path caused the issue",[43,63300,63301],{},"what operation Python was trying to do",[77,63303,63305],{"id":63304},"example-of-handling-the-error-with-try-except","Example of handling the error with try-except",[14,63307,63308,63309,63311],{},"You can catch ",[17,63310,17494],{}," and show a clearer message.",[91,63313,63315],{"className":93,"code":63314,"language":95,"meta":96,"style":96},"from pathlib import Path\n\npath = Path(\"example.txt\")\n\ntry:\n    with open(path, \"r\", encoding=\"utf-8\") as file:\n        print(file.read())\nexcept PermissionError:\n    print(\"Permission denied.\")\n    print(\"Check the file path, file permissions, and whether another program is using the file.\")\nexcept FileNotFoundError:\n    print(\"The file was not found.\")\n",[17,63316,63317,63327,63331,63349,63353,63359,63397,63411,63419,63434,63449,63457],{"__ignoreMap":96},[100,63318,63319,63321,63323,63325],{"class":102,"line":103},[100,63320,10201],{"class":145},[100,63322,18068],{"class":106},[100,63324,9259],{"class":145},[100,63326,18073],{"class":106},[100,63328,63329],{"class":102,"line":135},[100,63330,139],{"emptyLinePlaceholder":138},[100,63332,63333,63335,63337,63339,63341,63343,63345,63347],{"class":102,"line":142},[100,63334,19365],{"class":106},[100,63336,111],{"class":110},[100,63338,18087],{"class":178},[100,63340,170],{"class":118},[100,63342,206],{"class":205},[100,63344,58418],{"class":209},[100,63346,206],{"class":205},[100,63348,215],{"class":118},[100,63350,63351],{"class":102,"line":152},[100,63352,139],{"emptyLinePlaceholder":138},[100,63354,63355,63357],{"class":102,"line":164},[100,63356,146],{"class":145},[100,63358,149],{"class":118},[100,63360,63361,63363,63365,63367,63369,63371,63373,63375,63377,63379,63381,63383,63385,63387,63389,63391,63393,63395],{"class":102,"line":185},[100,63362,18121],{"class":145},[100,63364,17524],{"class":114},[100,63366,170],{"class":118},[100,63368,19017],{"class":178},[100,63370,126],{"class":118},[100,63372,1708],{"class":205},[100,63374,17540],{"class":209},[100,63376,206],{"class":205},[100,63378,126],{"class":118},[100,63380,18142],{"class":18141},[100,63382,111],{"class":110},[100,63384,206],{"class":205},[100,63386,15307],{"class":209},[100,63388,206],{"class":205},[100,63390,6155],{"class":118},[100,63392,16224],{"class":145},[100,63394,17550],{"class":17549},[100,63396,149],{"class":118},[100,63398,63399,63401,63403,63405,63407,63409],{"class":102,"line":197},[100,63400,167],{"class":114},[100,63402,170],{"class":118},[100,63404,17597],{"class":17549},[100,63406,290],{"class":118},[100,63408,17566],{"class":178},[100,63410,3370],{"class":118},[100,63412,63413,63415,63417],{"class":102,"line":771},[100,63414,188],{"class":145},[100,63416,23388],{"class":191},[100,63418,149],{"class":118},[100,63420,63421,63423,63425,63427,63430,63432],{"class":102,"line":787},[100,63422,200],{"class":114},[100,63424,170],{"class":118},[100,63426,206],{"class":205},[100,63428,63429],{"class":209},"Permission denied.",[100,63431,206],{"class":205},[100,63433,215],{"class":118},[100,63435,63436,63438,63440,63442,63445,63447],{"class":102,"line":5816},[100,63437,200],{"class":114},[100,63439,170],{"class":118},[100,63441,206],{"class":205},[100,63443,63444],{"class":209},"Check the file path, file permissions, and whether another program is using the file.",[100,63446,206],{"class":205},[100,63448,215],{"class":118},[100,63450,63451,63453,63455],{"class":102,"line":5833},[100,63452,188],{"class":145},[100,63454,20558],{"class":191},[100,63456,149],{"class":118},[100,63458,63459,63461,63463,63465,63467,63469],{"class":102,"line":5848},[100,63460,200],{"class":114},[100,63462,170],{"class":118},[100,63464,206],{"class":205},[100,63466,20571],{"class":209},[100,63468,206],{"class":205},[100,63470,215],{"class":118},[14,63472,63473],{},"This helps users understand what to check next.",[14,63475,63476,63477,290],{},"If you need help with missing files too, see ",[295,63478,20642],{"href":23203},[77,63480,7081],{"id":7080},[14,63482,63483],{},"Some file errors look similar but mean different things.",[40,63485,63486,63493,63500,63507],{},[43,63487,63488,63490,63492],{},[295,63489,17489],{"href":23203},[9927,63491],{},"\nThe path does not exist.",[43,63494,63495,63497,63499],{},[295,63496,17499],{"href":23069},[9927,63498],{},"\nYou used a directory where a file was expected.",[43,63501,63502,63504,63506],{},[295,63503,17504],{"href":23081},[9927,63505],{},"\nPart of the path is not a directory.",[43,63508,63509,63512,63514],{},[295,63510,17509],{"href":63511},"\u002Ferrors\u002Foserror-in-python-causes-and-fixes\u002F",[9927,63513],{},"\nA broader file or operating system error category.",[77,63516,1514],{"id":1513},[675,63518,63520],{"id":63519},"what-is-permissionerror-in-python","What is PermissionError in Python?",[14,63522,63523],{},"It means Python was blocked from accessing a file or folder because of permission rules or file locking.",[675,63525,63527,63528,11353],{"id":63526},"why-do-i-get-errno-13-permission-denied","Why do I get ",[17,63529,62137],{},[14,63531,63532],{},"Usually because:",[40,63534,63535,63538,63541,63544],{},[43,63536,63537],{},"the file is protected",[43,63539,63540],{},"the folder is restricted",[43,63542,63543],{},"the path points to a directory",[43,63545,63546],{},"another program is using the file",[675,63548,63550,63551,63553],{"id":63549},"can-opening-a-folder-with-open-cause-permissionerror","Can opening a folder with ",[17,63552,18032],{}," cause PermissionError?",[14,63555,63556,63557,63559,63560,63562],{},"Yes. If you pass a directory path to ",[17,63558,18032],{},", some systems raise ",[17,63561,17494],{}," instead of reading a file.",[675,63564,63566],{"id":63565},"should-i-run-python-as-administrator-to-fix-this","Should I run Python as administrator to fix this?",[14,63568,63569],{},"Not as a first step. First check:",[40,63571,63572,63574,63577,63579],{},[43,63573,61955],{},[43,63575,63576],{},"whether it is a file or folder",[43,63578,58373],{},[43,63580,63581],{},"whether another program has locked the file",[675,63583,63585],{"id":63584},"how-do-i-avoid-permissionerror-as-a-beginner","How do I avoid PermissionError as a beginner?",[14,63587,63588],{},"The safest approach is to:",[40,63590,63591,63594,63597],{},[43,63592,63593],{},"use files inside your project folder",[43,63595,63596],{},"verify the path before opening it",[43,63598,63599],{},"close apps that may already be using the file",[77,63601,1554],{"id":1553},[40,63603,63604,63608,63613,63619,63623],{},[43,63605,63606],{},[295,63607,20591],{"href":20590},[43,63609,63610],{},[295,63611,63612],{"href":62828},"How to write to a file in Python",[43,63614,63615],{},[295,63616,1572,63617,1576],{"href":18280},[17,63618,18032],{},[43,63620,63621],{},[295,63622,20596],{"href":18285},[43,63624,63625],{},[295,63626,20471,63627,63629],{"href":20470},[17,63628,62137],{}," fix",[1589,63631,63632],{},"html pre.shiki code .sZMiF, html code.shiki .sZMiF{--shiki-light:#E2931D;--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 .su5hD, html code.shiki .su5hD{--shiki-light:#90A4AE;--shiki-default:#24292E;--shiki-dark:#E1E4E8}html pre.shiki code .srdBf, html code.shiki .srdBf{--shiki-light:#F76D47;--shiki-default:#005CC5;--shiki-dark:#79B8FF}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 .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 .smGrS, html code.shiki .smGrS{--shiki-light:#39ADB5;--shiki-default:#D73A49;--shiki-dark:#F97583}html pre.shiki code .slqww, html code.shiki .slqww{--shiki-light:#6182B8;--shiki-default:#24292E;--shiki-dark:#E1E4E8}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 .s99_P, html code.shiki .s99_P{--shiki-light:#90A4AE;--shiki-light-font-style:italic;--shiki-default:#E36209;--shiki-default-font-style:inherit;--shiki-dark:#FFAB70;--shiki-dark-font-style:inherit}html pre.shiki code .sMMDD, html code.shiki .sMMDD{--shiki-light:#90A4AE;--shiki-default:#E36209;--shiki-dark:#FFAB70}html pre.shiki code .sbgvK, html code.shiki .sbgvK{--shiki-light:#E2931D;--shiki-default:#6F42C1;--shiki-dark:#B392F0}html pre.shiki code .stzsN, html code.shiki .stzsN{--shiki-light:#91B859;--shiki-default:#005CC5;--shiki-dark:#79B8FF}",{"title":96,"searchDepth":135,"depth":135,"links":63634},[63635,63636,63637,63638,63643,63644,63645,63646,63650,63651,63660,63661,63662,63671],{"id":79,"depth":135,"text":80},{"id":62113,"depth":135,"text":62114},{"id":18289,"depth":135,"text":18290},{"id":62180,"depth":135,"text":62181,"children":63639},[63640,63641,63642],{"id":62184,"depth":142,"text":62185},{"id":62282,"depth":142,"text":62283},{"id":62383,"depth":142,"text":62384},{"id":62483,"depth":135,"text":62484},{"id":62730,"depth":135,"text":62731},{"id":62832,"depth":135,"text":62833},{"id":62887,"depth":135,"text":62888,"children":63647},[63648,63649],{"id":62905,"depth":142,"text":62906},{"id":62931,"depth":142,"text":62932},{"id":62961,"depth":135,"text":62962},{"id":63005,"depth":135,"text":63006,"children":63652},[63653,63654,63655,63656,63657,63658,63659],{"id":23969,"depth":142,"text":23970},{"id":57707,"depth":142,"text":57708},{"id":57754,"depth":142,"text":57755},{"id":61584,"depth":142,"text":61585},{"id":63154,"depth":142,"text":63155},{"id":63276,"depth":142,"text":63277},{"id":63286,"depth":142,"text":63287},{"id":63304,"depth":135,"text":63305},{"id":7080,"depth":135,"text":7081},{"id":1513,"depth":135,"text":1514,"children":63663},[63664,63665,63667,63669,63670],{"id":63519,"depth":142,"text":63520},{"id":63526,"depth":142,"text":63666},"Why do I get [Errno 13] Permission denied?",{"id":63549,"depth":142,"text":63668},"Can opening a folder with open() cause PermissionError?",{"id":63565,"depth":142,"text":63566},{"id":63584,"depth":142,"text":63585},{"id":1553,"depth":135,"text":1554},"Master permissionerror in python causes and fixes in our comprehensive Python beginner guide.",{},{"title":61891,"description":63672},"errors\u002Fpermissionerror-in-python-causes-and-fixes","j27_f0-NM81ST4srbijS63utyyLD1fzhYIRX3ofy0Xs",{"id":63678,"title":63679,"body":63680,"description":64912,"extension":1623,"meta":64913,"navigation":138,"path":64914,"seo":64915,"stem":64916,"__hash__":64917},"content\u002Ferrors\u002Frecursionerror-in-python-causes-and-fixes.md","RecursionError in Python: Causes and Fixes",{"type":7,"value":63681,"toc":64882},[63682,63685,63690,63695,63697,63776,63780,63806,63811,63813,63826,63829,63843,63849,63851,63853,63867,63874,63876,63881,63925,63927,63930,63941,63944,63949,63953,63959,63962,63966,64024,64027,64031,64104,64108,64133,64136,64140,64143,64147,64218,64227,64229,64300,64304,64321,64324,64335,64339,64347,64350,64354,64425,64429,64478,64482,64506,64512,64514,64517,64531,64534,64623,64627,64672,64675,64705,64710,64714,64717,64724,64769,64775,64778,64789,64792,64794,64798,64815,64820,64822,64826,64829,64833,64836,64840,64843,64847,64850,64852,64879],[10,63683,63679],{"id":63684},"recursionerror-in-python-causes-and-fixes",[14,63686,63687,63689],{},[17,63688,17658],{}," happens when a function keeps calling itself too many times.",[14,63691,63692,63693,290],{},"In Python, recursion means a function calls itself to solve a smaller version of the same problem. This can be useful, but the function must have a clear way to stop. If it never stops, or if it goes too deep, Python raises ",[17,63694,17658],{},[77,63696,80],{"id":79},[91,63698,63700],{"className":93,"code":63699,"language":95,"meta":96,"style":96},"def countdown(n):\n    if n \u003C= 0:   # base case\n        return\n    print(n)\n    countdown(n - 1)\n\ncountdown(5)\n",[17,63701,63702,63715,63731,63736,63746,63761,63765],{"__ignoreMap":96},[100,63703,63704,63706,63709,63711,63713],{"class":102,"line":103},[100,63705,1078],{"class":1077},[100,63707,63708],{"class":1081}," countdown",[100,63710,170],{"class":118},[100,63712,1433],{"class":2519},[100,63714,522],{"class":118},[100,63716,63717,63719,63721,63724,63726,63728],{"class":102,"line":135},[100,63718,2777],{"class":145},[100,63720,1418],{"class":106},[100,63722,63723],{"class":110},"\u003C=",[100,63725,1921],{"class":122},[100,63727,89],{"class":118},[100,63729,63730],{"class":414},"   # base case\n",[100,63732,63733],{"class":102,"line":142},[100,63734,63735],{"class":145},"        return\n",[100,63737,63738,63740,63742,63744],{"class":102,"line":152},[100,63739,200],{"class":114},[100,63741,170],{"class":118},[100,63743,1433],{"class":178},[100,63745,215],{"class":118},[100,63747,63748,63751,63753,63755,63757,63759],{"class":102,"line":164},[100,63749,63750],{"class":178},"    countdown",[100,63752,170],{"class":118},[100,63754,51224],{"class":178},[100,63756,2193],{"class":110},[100,63758,6915],{"class":122},[100,63760,215],{"class":118},[100,63762,63763],{"class":102,"line":185},[100,63764,139],{"emptyLinePlaceholder":138},[100,63766,63767,63770,63772,63774],{"class":102,"line":197},[100,63768,63769],{"class":178},"countdown",[100,63771,170],{"class":118},[100,63773,3557],{"class":122},[100,63775,215],{"class":118},[14,63777,63778],{},[22,63779,218],{},[91,63781,63783],{"className":93,"code":63782,"language":95,"meta":96,"style":96},"5\n4\n3\n2\n1\n",[17,63784,63785,63789,63794,63798,63802],{"__ignoreMap":96},[100,63786,63787],{"class":102,"line":103},[100,63788,1909],{"class":122},[100,63790,63791],{"class":102,"line":135},[100,63792,63793],{"class":122},"4\n",[100,63795,63796],{"class":102,"line":142},[100,63797,1500],{"class":122},[100,63799,63800],{"class":102,"line":152},[100,63801,233],{"class":122},[100,63803,63804],{"class":102,"line":164},[100,63805,228],{"class":122},[14,63807,3840,63808,63810],{},[17,63809,17658],{}," problems happen because the function has no base case or the base case is never reached.",[77,63812,5881],{"id":5880},[40,63814,63815,63818,63823],{},[43,63816,63817],{},"Recursion happens when a function calls itself.",[43,63819,63820,63821,290],{},"Python stops very deep recursion and raises ",[17,63822,17658],{},[43,63824,63825],{},"This usually means the function keeps calling itself too many times.",[14,63827,63828],{},"A common error message looks like this:",[91,63830,63832],{"className":93,"code":63831,"language":95,"meta":96,"style":96},"RecursionError: maximum recursion depth exceeded\n",[17,63833,63834],{"__ignoreMap":96},[100,63835,63836,63838,63840],{"class":102,"line":103},[100,63837,17658],{"class":191},[100,63839,89],{"class":118},[100,63841,63842],{"class":106}," maximum recursion depth exceeded\n",[14,63844,63845,63846,63848],{},"If you are new to functions, see ",[295,63847,29749],{"href":29189}," first.",[77,63850,25091],{"id":25090},[14,63852,38066],{},[40,63854,63855,63858,63861,63864],{},[43,63856,63857],{},"There is no base case to stop the recursion.",[43,63859,63860],{},"The base case exists but the code never reaches it.",[43,63862,63863],{},"The recursive step does not move toward the stopping condition.",[43,63865,63866],{},"The input is too large for a recursive solution in Python.",[14,63868,63869,63870,290],{},"If you want a simple definition of recursion, read ",[295,63871,63873],{"href":63872},"\u002Fglossary\u002Fwhat-is-recursion-in-python\u002F","what recursion is in Python",[77,63875,11031],{"id":11030},[14,63877,63878,63879,89],{},"Here is a minimal example that causes ",[17,63880,17658],{},[91,63882,63884],{"className":93,"code":63883,"language":95,"meta":96,"style":96},"def greet():\n    print(\"Hello\")\n    greet()\n\ngreet()\n",[17,63885,63886,63894,63908,63915,63919],{"__ignoreMap":96},[100,63887,63888,63890,63892],{"class":102,"line":103},[100,63889,1078],{"class":1077},[100,63891,2848],{"class":1081},[100,63893,1085],{"class":118},[100,63895,63896,63898,63900,63902,63904,63906],{"class":102,"line":135},[100,63897,200],{"class":114},[100,63899,170],{"class":118},[100,63901,206],{"class":205},[100,63903,7683],{"class":209},[100,63905,206],{"class":205},[100,63907,215],{"class":118},[100,63909,63910,63913],{"class":102,"line":142},[100,63911,63912],{"class":178},"    greet",[100,63914,4734],{"class":118},[100,63916,63917],{"class":102,"line":152},[100,63918,139],{"emptyLinePlaceholder":138},[100,63920,63921,63923],{"class":102,"line":164},[100,63922,2911],{"class":178},[100,63924,4734],{"class":118},[675,63926,35438],{"id":35437},[14,63928,63929],{},"The line below keeps repeating forever:",[91,63931,63933],{"className":93,"code":63932,"language":95,"meta":96,"style":96},"greet()\n",[17,63934,63935],{"__ignoreMap":96},[100,63936,63937,63939],{"class":102,"line":103},[100,63938,2911],{"class":178},[100,63940,4734],{"class":118},[14,63942,63943],{},"The function calls itself, then calls itself again, then again, with no stopping condition.",[14,63945,63946,63947,290],{},"After enough calls, Python stops the program and raises ",[17,63948,17658],{},[77,63950,63952],{"id":63951},"fix-1-add-a-base-case","Fix 1: Add a base case",[14,63954,571,63955,63958],{},[22,63956,63957],{},"base case"," is the condition that stops the function.",[14,63960,63961],{},"Put the base case before the recursive call, and make sure it returns or exits.",[675,63963,63965],{"id":63964},"broken-version","Broken version",[91,63967,63969],{"className":93,"code":63968,"language":95,"meta":96,"style":96},"def count_up(n):\n    print(n)\n    count_up(n + 1)\n\ncount_up(1)\n",[17,63970,63971,63984,63994,64009,64013],{"__ignoreMap":96},[100,63972,63973,63975,63978,63980,63982],{"class":102,"line":103},[100,63974,1078],{"class":1077},[100,63976,63977],{"class":1081}," count_up",[100,63979,170],{"class":118},[100,63981,1433],{"class":2519},[100,63983,522],{"class":118},[100,63985,63986,63988,63990,63992],{"class":102,"line":135},[100,63987,200],{"class":114},[100,63989,170],{"class":118},[100,63991,1433],{"class":178},[100,63993,215],{"class":118},[100,63995,63996,63999,64001,64003,64005,64007],{"class":102,"line":142},[100,63997,63998],{"class":178},"    count_up",[100,64000,170],{"class":118},[100,64002,51224],{"class":178},[100,64004,6547],{"class":110},[100,64006,6915],{"class":122},[100,64008,215],{"class":118},[100,64010,64011],{"class":102,"line":152},[100,64012,139],{"emptyLinePlaceholder":138},[100,64014,64015,64018,64020,64022],{"class":102,"line":164},[100,64016,64017],{"class":178},"count_up",[100,64019,170],{"class":118},[100,64021,123],{"class":122},[100,64023,215],{"class":118},[14,64025,64026],{},"This never stops.",[675,64028,64030],{"id":64029},"fixed-version","Fixed version",[91,64032,64034],{"className":93,"code":64033,"language":95,"meta":96,"style":96},"def count_up(n):\n    if n > 5:   # base case\n        return\n    print(n)\n    count_up(n + 1)\n\ncount_up(1)\n",[17,64035,64036,64048,64062,64066,64076,64090,64094],{"__ignoreMap":96},[100,64037,64038,64040,64042,64044,64046],{"class":102,"line":103},[100,64039,1078],{"class":1077},[100,64041,63977],{"class":1081},[100,64043,170],{"class":118},[100,64045,1433],{"class":2519},[100,64047,522],{"class":118},[100,64049,64050,64052,64054,64056,64058,64060],{"class":102,"line":135},[100,64051,2777],{"class":145},[100,64053,1418],{"class":106},[100,64055,1918],{"class":110},[100,64057,31164],{"class":122},[100,64059,89],{"class":118},[100,64061,63730],{"class":414},[100,64063,64064],{"class":102,"line":142},[100,64065,63735],{"class":145},[100,64067,64068,64070,64072,64074],{"class":102,"line":152},[100,64069,200],{"class":114},[100,64071,170],{"class":118},[100,64073,1433],{"class":178},[100,64075,215],{"class":118},[100,64077,64078,64080,64082,64084,64086,64088],{"class":102,"line":164},[100,64079,63998],{"class":178},[100,64081,170],{"class":118},[100,64083,51224],{"class":178},[100,64085,6547],{"class":110},[100,64087,6915],{"class":122},[100,64089,215],{"class":118},[100,64091,64092],{"class":102,"line":185},[100,64093,139],{"emptyLinePlaceholder":138},[100,64095,64096,64098,64100,64102],{"class":102,"line":197},[100,64097,64017],{"class":178},[100,64099,170],{"class":118},[100,64101,123],{"class":122},[100,64103,215],{"class":118},[14,64105,64106],{},[22,64107,218],{},[91,64109,64111],{"className":93,"code":64110,"language":95,"meta":96,"style":96},"1\n2\n3\n4\n5\n",[17,64112,64113,64117,64121,64125,64129],{"__ignoreMap":96},[100,64114,64115],{"class":102,"line":103},[100,64116,228],{"class":122},[100,64118,64119],{"class":102,"line":135},[100,64120,233],{"class":122},[100,64122,64123],{"class":102,"line":142},[100,64124,1500],{"class":122},[100,64126,64127],{"class":102,"line":152},[100,64128,63793],{"class":122},[100,64130,64131],{"class":102,"line":164},[100,64132,1909],{"class":122},[14,64134,64135],{},"The important part is that the function checks the stopping condition before making the next recursive call.",[77,64137,64139],{"id":64138},"fix-2-move-toward-the-base-case","Fix 2: Move toward the base case",[14,64141,64142],{},"Even if you have a base case, the function still fails if the values do not move toward it.",[675,64144,64146],{"id":64145},"wrong-direction","Wrong direction",[91,64148,64150],{"className":93,"code":64149,"language":95,"meta":96,"style":96},"def countdown(n):\n    if n == 0:\n        return\n    print(n)\n    countdown(n + 1)\n\ncountdown(3)\n",[17,64151,64152,64164,64176,64180,64190,64204,64208],{"__ignoreMap":96},[100,64153,64154,64156,64158,64160,64162],{"class":102,"line":103},[100,64155,1078],{"class":1077},[100,64157,63708],{"class":1081},[100,64159,170],{"class":118},[100,64161,1433],{"class":2519},[100,64163,522],{"class":118},[100,64165,64166,64168,64170,64172,64174],{"class":102,"line":135},[100,64167,2777],{"class":145},[100,64169,1418],{"class":106},[100,64171,32169],{"class":110},[100,64173,1921],{"class":122},[100,64175,149],{"class":118},[100,64177,64178],{"class":102,"line":142},[100,64179,63735],{"class":145},[100,64181,64182,64184,64186,64188],{"class":102,"line":152},[100,64183,200],{"class":114},[100,64185,170],{"class":118},[100,64187,1433],{"class":178},[100,64189,215],{"class":118},[100,64191,64192,64194,64196,64198,64200,64202],{"class":102,"line":164},[100,64193,63750],{"class":178},[100,64195,170],{"class":118},[100,64197,51224],{"class":178},[100,64199,6547],{"class":110},[100,64201,6915],{"class":122},[100,64203,215],{"class":118},[100,64205,64206],{"class":102,"line":185},[100,64207,139],{"emptyLinePlaceholder":138},[100,64209,64210,64212,64214,64216],{"class":102,"line":197},[100,64211,63769],{"class":178},[100,64213,170],{"class":118},[100,64215,11541],{"class":122},[100,64217,215],{"class":118},[14,64219,64220,64221,64223,64224,64226],{},"This has a base case, but ",[17,64222,1433],{}," moves away from ",[17,64225,2279],{}," instead of toward it.",[675,64228,30958],{"id":30957},[91,64230,64232],{"className":93,"code":64231,"language":95,"meta":96,"style":96},"def countdown(n):\n    if n == 0:\n        return\n    print(n)\n    countdown(n - 1)\n\ncountdown(3)\n",[17,64233,64234,64246,64258,64262,64272,64286,64290],{"__ignoreMap":96},[100,64235,64236,64238,64240,64242,64244],{"class":102,"line":103},[100,64237,1078],{"class":1077},[100,64239,63708],{"class":1081},[100,64241,170],{"class":118},[100,64243,1433],{"class":2519},[100,64245,522],{"class":118},[100,64247,64248,64250,64252,64254,64256],{"class":102,"line":135},[100,64249,2777],{"class":145},[100,64251,1418],{"class":106},[100,64253,32169],{"class":110},[100,64255,1921],{"class":122},[100,64257,149],{"class":118},[100,64259,64260],{"class":102,"line":142},[100,64261,63735],{"class":145},[100,64263,64264,64266,64268,64270],{"class":102,"line":152},[100,64265,200],{"class":114},[100,64267,170],{"class":118},[100,64269,1433],{"class":178},[100,64271,215],{"class":118},[100,64273,64274,64276,64278,64280,64282,64284],{"class":102,"line":164},[100,64275,63750],{"class":178},[100,64277,170],{"class":118},[100,64279,51224],{"class":178},[100,64281,2193],{"class":110},[100,64283,6915],{"class":122},[100,64285,215],{"class":118},[100,64287,64288],{"class":102,"line":185},[100,64289,139],{"emptyLinePlaceholder":138},[100,64291,64292,64294,64296,64298],{"class":102,"line":197},[100,64293,63769],{"class":178},[100,64295,170],{"class":118},[100,64297,11541],{"class":122},[100,64299,215],{"class":118},[14,64301,64302],{},[22,64303,218],{},[91,64305,64307],{"className":93,"code":64306,"language":95,"meta":96,"style":96},"3\n2\n1\n",[17,64308,64309,64313,64317],{"__ignoreMap":96},[100,64310,64311],{"class":102,"line":103},[100,64312,1500],{"class":122},[100,64314,64315],{"class":102,"line":135},[100,64316,233],{"class":122},[100,64318,64319],{"class":102,"line":142},[100,64320,228],{"class":122},[14,64322,64323],{},"When writing recursive code:",[40,64325,64326,64329,64332],{},[43,64327,64328],{},"Change the argument on each call.",[43,64330,64331],{},"Make the problem smaller each time.",[43,64333,64334],{},"Check that the value really gets closer to the stopping condition.",[77,64336,64338],{"id":64337},"fix-3-use-a-loop-instead","Fix 3: Use a loop instead",[14,64340,64341,64342,64344,64345,32217],{},"Some tasks are easier with a ",[17,64343,71],{}," loop or ",[17,64346,721],{},[14,64348,64349],{},"Loops do not build up recursive calls in the same way, so they avoid deep recursion problems.",[675,64351,64353],{"id":64352},"recursive-version","Recursive version",[91,64355,64357],{"className":93,"code":64356,"language":95,"meta":96,"style":96},"def countdown(n):\n    if n \u003C= 0:\n        return\n    print(n)\n    countdown(n - 1)\n\ncountdown(5)\n",[17,64358,64359,64371,64383,64387,64397,64411,64415],{"__ignoreMap":96},[100,64360,64361,64363,64365,64367,64369],{"class":102,"line":103},[100,64362,1078],{"class":1077},[100,64364,63708],{"class":1081},[100,64366,170],{"class":118},[100,64368,1433],{"class":2519},[100,64370,522],{"class":118},[100,64372,64373,64375,64377,64379,64381],{"class":102,"line":135},[100,64374,2777],{"class":145},[100,64376,1418],{"class":106},[100,64378,63723],{"class":110},[100,64380,1921],{"class":122},[100,64382,149],{"class":118},[100,64384,64385],{"class":102,"line":142},[100,64386,63735],{"class":145},[100,64388,64389,64391,64393,64395],{"class":102,"line":152},[100,64390,200],{"class":114},[100,64392,170],{"class":118},[100,64394,1433],{"class":178},[100,64396,215],{"class":118},[100,64398,64399,64401,64403,64405,64407,64409],{"class":102,"line":164},[100,64400,63750],{"class":178},[100,64402,170],{"class":118},[100,64404,51224],{"class":178},[100,64406,2193],{"class":110},[100,64408,6915],{"class":122},[100,64410,215],{"class":118},[100,64412,64413],{"class":102,"line":185},[100,64414,139],{"emptyLinePlaceholder":138},[100,64416,64417,64419,64421,64423],{"class":102,"line":197},[100,64418,63769],{"class":178},[100,64420,170],{"class":118},[100,64422,3557],{"class":122},[100,64424,215],{"class":118},[675,64426,64428],{"id":64427},"loop-version","Loop version",[91,64430,64432],{"className":93,"code":64431,"language":95,"meta":96,"style":96},"n = 5\n\nwhile n > 0:\n    print(n)\n    n -= 1\n",[17,64433,64434,64442,64446,64458,64468],{"__ignoreMap":96},[100,64435,64436,64438,64440],{"class":102,"line":103},[100,64437,51224],{"class":106},[100,64439,111],{"class":110},[100,64441,37580],{"class":122},[100,64443,64444],{"class":102,"line":135},[100,64445,139],{"emptyLinePlaceholder":138},[100,64447,64448,64450,64452,64454,64456],{"class":102,"line":142},[100,64449,721],{"class":145},[100,64451,1418],{"class":106},[100,64453,1918],{"class":110},[100,64455,1921],{"class":122},[100,64457,149],{"class":118},[100,64459,64460,64462,64464,64466],{"class":102,"line":152},[100,64461,200],{"class":114},[100,64463,170],{"class":118},[100,64465,1433],{"class":178},[100,64467,215],{"class":118},[100,64469,64470,64473,64476],{"class":102,"line":164},[100,64471,64472],{"class":106},"    n ",[100,64474,64475],{"class":110},"-=",[100,64477,1093],{"class":122},[14,64479,64480],{},[22,64481,218],{},[91,64483,64484],{"className":93,"code":63782,"language":95,"meta":96,"style":96},[17,64485,64486,64490,64494,64498,64502],{"__ignoreMap":96},[100,64487,64488],{"class":102,"line":103},[100,64489,1909],{"class":122},[100,64491,64492],{"class":102,"line":135},[100,64493,63793],{"class":122},[100,64495,64496],{"class":102,"line":142},[100,64497,1500],{"class":122},[100,64499,64500],{"class":102,"line":152},[100,64501,233],{"class":122},[100,64503,64504],{"class":102,"line":164},[100,64505,228],{"class":122},[14,64507,64508,64509,290],{},"If recursion is not necessary, iteration is often simpler. You can learn more in ",[295,64510,42258],{"href":64511},"\u002Flearn\u002Fpython-while-loops-explained\u002F",[77,64513,1168],{"id":1167},[14,64515,64516],{},"If your recursive function is failing, try this checklist:",[40,64518,64519,64522,64525,64528],{},[43,64520,64521],{},"Print the function argument before the recursive call.",[43,64523,64524],{},"Check whether the value changes on each call.",[43,64526,64527],{},"Confirm the base case can actually become true.",[43,64529,64530],{},"Test with a very small input first.",[14,64532,64533],{},"Useful debugging examples:",[91,64535,64537],{"className":93,"code":64536,"language":95,"meta":96,"style":96},"def countdown(n):\n    print(\"calling with:\", n)\n\n    if n \u003C= 0:\n        return\n\n    countdown(n - 1)\n\ncountdown(3)\n",[17,64538,64539,64551,64571,64575,64587,64591,64595,64609,64613],{"__ignoreMap":96},[100,64540,64541,64543,64545,64547,64549],{"class":102,"line":103},[100,64542,1078],{"class":1077},[100,64544,63708],{"class":1081},[100,64546,170],{"class":118},[100,64548,1433],{"class":2519},[100,64550,522],{"class":118},[100,64552,64553,64555,64557,64559,64562,64564,64566,64569],{"class":102,"line":135},[100,64554,200],{"class":114},[100,64556,170],{"class":118},[100,64558,206],{"class":205},[100,64560,64561],{"class":209},"calling with:",[100,64563,206],{"class":205},[100,64565,126],{"class":118},[100,64567,64568],{"class":178}," n",[100,64570,215],{"class":118},[100,64572,64573],{"class":102,"line":142},[100,64574,139],{"emptyLinePlaceholder":138},[100,64576,64577,64579,64581,64583,64585],{"class":102,"line":152},[100,64578,2777],{"class":145},[100,64580,1418],{"class":106},[100,64582,63723],{"class":110},[100,64584,1921],{"class":122},[100,64586,149],{"class":118},[100,64588,64589],{"class":102,"line":164},[100,64590,63735],{"class":145},[100,64592,64593],{"class":102,"line":185},[100,64594,139],{"emptyLinePlaceholder":138},[100,64596,64597,64599,64601,64603,64605,64607],{"class":102,"line":197},[100,64598,63750],{"class":178},[100,64600,170],{"class":118},[100,64602,51224],{"class":178},[100,64604,2193],{"class":110},[100,64606,6915],{"class":122},[100,64608,215],{"class":118},[100,64610,64611],{"class":102,"line":771},[100,64612,139],{"emptyLinePlaceholder":138},[100,64614,64615,64617,64619,64621],{"class":102,"line":787},[100,64616,63769],{"class":178},[100,64618,170],{"class":118},[100,64620,11541],{"class":122},[100,64622,215],{"class":118},[14,64624,64625],{},[22,64626,218],{},[91,64628,64630],{"className":93,"code":64629,"language":95,"meta":96,"style":96},"calling with: 3\ncalling with: 2\ncalling with: 1\ncalling with: 0\n",[17,64631,64632,64642,64652,64662],{"__ignoreMap":96},[100,64633,64634,64636,64638,64640],{"class":102,"line":103},[100,64635,45],{"class":106},[100,64637,17521],{"class":145},[100,64639,89],{"class":118},[100,64641,31170],{"class":122},[100,64643,64644,64646,64648,64650],{"class":102,"line":135},[100,64645,45],{"class":106},[100,64647,17521],{"class":145},[100,64649,89],{"class":118},[100,64651,3564],{"class":122},[100,64653,64654,64656,64658,64660],{"class":102,"line":142},[100,64655,45],{"class":106},[100,64657,17521],{"class":145},[100,64659,89],{"class":118},[100,64661,1093],{"class":122},[100,64663,64664,64666,64668,64670],{"class":102,"line":152},[100,64665,45],{"class":106},[100,64667,17521],{"class":145},[100,64669,89],{"class":118},[100,64671,2069],{"class":122},[14,64673,64674],{},"You can also inspect Python's current recursion limit:",[91,64676,64678],{"className":93,"code":64677,"language":95,"meta":96,"style":96},"import sys\n\nprint(sys.getrecursionlimit())\n",[17,64679,64680,64686,64690],{"__ignoreMap":96},[100,64681,64682,64684],{"class":102,"line":103},[100,64683,9259],{"class":145},[100,64685,26131],{"class":106},[100,64687,64688],{"class":102,"line":135},[100,64689,139],{"emptyLinePlaceholder":138},[100,64691,64692,64694,64696,64698,64700,64703],{"class":102,"line":142},[100,64693,372],{"class":114},[100,64695,170],{"class":118},[100,64697,26140],{"class":178},[100,64699,290],{"class":118},[100,64701,64702],{"class":178},"getrecursionlimit",[100,64704,3370],{"class":118},[14,64706,64707,64708,290],{},"If you are still stuck, see this ",[295,64709,7077],{"href":3852},[77,64711,64713],{"id":64712},"about-increasing-the-recursion-limit","About increasing the recursion limit",[14,64715,64716],{},"Python has a recursion limit to protect your program from crashing due to infinite or very deep recursion.",[14,64718,64719,64720,64723],{},"You can change the limit with ",[17,64721,64722],{},"sys.setrecursionlimit()",", like this:",[91,64725,64727],{"className":93,"code":64726,"language":95,"meta":96,"style":96},"import sys\n\nsys.setrecursionlimit(2000)\nprint(sys.getrecursionlimit())\n",[17,64728,64729,64735,64739,64755],{"__ignoreMap":96},[100,64730,64731,64733],{"class":102,"line":103},[100,64732,9259],{"class":145},[100,64734,26131],{"class":106},[100,64736,64737],{"class":102,"line":135},[100,64738,139],{"emptyLinePlaceholder":138},[100,64740,64741,64743,64745,64748,64750,64753],{"class":102,"line":142},[100,64742,26140],{"class":106},[100,64744,290],{"class":118},[100,64746,64747],{"class":178},"setrecursionlimit",[100,64749,170],{"class":118},[100,64751,64752],{"class":122},"2000",[100,64754,215],{"class":118},[100,64756,64757,64759,64761,64763,64765,64767],{"class":102,"line":152},[100,64758,372],{"class":114},[100,64760,170],{"class":118},[100,64762,26140],{"class":178},[100,64764,290],{"class":118},[100,64766,64702],{"class":178},[100,64768,3370],{"class":118},[14,64770,64771,64772,290],{},"But this is ",[22,64773,64774],{},"usually not the first fix",[14,64776,64777],{},"For beginners, the better solution is normally to fix the recursive logic:",[40,64779,64780,64783,64786],{},[43,64781,64782],{},"Add a base case",[43,64784,64785],{},"Make sure the function moves toward that base case",[43,64787,64788],{},"Use a loop if recursion is not needed",[14,64790,64791],{},"Raising the limit can hide a bug instead of solving it.",[77,64793,7117],{"id":7116},[14,64795,29978,64796,89],{},[17,64797,17658],{},[40,64799,64800,64803,64806,64809,64812],{},[43,64801,64802],{},"Missing base case",[43,64804,64805],{},"Wrong comparison in the base case",[43,64807,64808],{},"Recursive call uses the same value every time",[43,64810,64811],{},"Value changes in the wrong direction",[43,64813,64814],{},"Using recursion for very large input",[14,64816,64817,64818,290],{},"For a broader overview of Python exceptions, read ",[295,64819,1587],{"href":1749},[77,64821,1514],{"id":1513},[675,64823,64825],{"id":64824},"what-is-a-base-case-in-recursion","What is a base case in recursion?",[14,64827,64828],{},"It is the condition that stops the function from calling itself again.",[675,64830,64832],{"id":64831},"can-i-fix-recursionerror-by-increasing-the-recursion-limit","Can I fix RecursionError by increasing the recursion limit?",[14,64834,64835],{},"Sometimes, but usually the better fix is to correct the recursive logic or use a loop.",[675,64837,64839],{"id":64838},"why-does-my-recursive-function-never-stop","Why does my recursive function never stop?",[14,64841,64842],{},"Usually because the stopping condition is missing, incorrect, or never reached.",[675,64844,64846],{"id":64845},"is-recursion-bad-in-python","Is recursion bad in Python?",[14,64848,64849],{},"No, but Python is not ideal for very deep recursion. Simple recursion is fine when the stopping condition is clear.",[77,64851,1554],{"id":1553},[40,64853,64854,64858,64863,64867,64871,64875],{},[43,64855,64856],{},[295,64857,29749],{"href":29189},[43,64859,64860],{},[295,64861,64862],{"href":63872},"What is recursion in Python?",[43,64864,64865],{},[295,64866,1587],{"href":1749},[43,64868,64869],{},[295,64870,5690],{"href":3852},[43,64872,64873],{},[295,64874,52120],{"href":52119},[43,64876,64877],{},[295,64878,42258],{"href":64511},[1589,64880,64881],{},"html pre.shiki code .sbsja, html code.shiki .sbsja{--shiki-light:#9C3EDA;--shiki-default:#D73A49;--shiki-dark:#F97583}html pre.shiki code .sGLFI, html code.shiki .sGLFI{--shiki-light:#6182B8;--shiki-default:#6F42C1;--shiki-dark:#B392F0}html pre.shiki code .sP7_E, html code.shiki .sP7_E{--shiki-light:#39ADB5;--shiki-default:#24292E;--shiki-dark:#E1E4E8}html pre.shiki code .sFwrP, html code.shiki .sFwrP{--shiki-light:#90A4AE;--shiki-light-font-style:italic;--shiki-default:#24292E;--shiki-default-font-style:inherit;--shiki-dark:#E1E4E8;--shiki-dark-font-style:inherit}html pre.shiki code .sVHd0, html code.shiki .sVHd0{--shiki-light:#39ADB5;--shiki-light-font-style:italic;--shiki-default:#D73A49;--shiki-default-font-style:inherit;--shiki-dark:#F97583;--shiki-dark-font-style:inherit}html pre.shiki code .su5hD, html code.shiki .su5hD{--shiki-light:#90A4AE;--shiki-default:#24292E;--shiki-dark:#E1E4E8}html pre.shiki code .smGrS, html code.shiki .smGrS{--shiki-light:#39ADB5;--shiki-default:#D73A49;--shiki-dark:#F97583}html pre.shiki code .srdBf, html code.shiki .srdBf{--shiki-light:#F76D47;--shiki-default:#005CC5;--shiki-dark:#79B8FF}html pre.shiki code .sutJx, html code.shiki .sutJx{--shiki-light:#90A4AE;--shiki-light-font-style:italic;--shiki-default:#6A737D;--shiki-default-font-style:inherit;--shiki-dark:#6A737D;--shiki-dark-font-style:inherit}html pre.shiki code .sptTA, html code.shiki .sptTA{--shiki-light:#6182B8;--shiki-default:#005CC5;--shiki-dark:#79B8FF}html pre.shiki code .slqww, html code.shiki .slqww{--shiki-light:#6182B8;--shiki-default:#24292E;--shiki-dark:#E1E4E8}html .light .shiki span {color: var(--shiki-light);background: var(--shiki-light-bg);font-style: var(--shiki-light-font-style);font-weight: var(--shiki-light-font-weight);text-decoration: var(--shiki-light-text-decoration);}html.light .shiki span {color: var(--shiki-light);background: var(--shiki-light-bg);font-style: var(--shiki-light-font-style);font-weight: var(--shiki-light-font-weight);text-decoration: var(--shiki-light-text-decoration);}html .default .shiki span {color: var(--shiki-default);background: var(--shiki-default-bg);font-style: var(--shiki-default-font-style);font-weight: var(--shiki-default-font-weight);text-decoration: var(--shiki-default-text-decoration);}html .shiki span {color: var(--shiki-default);background: var(--shiki-default-bg);font-style: var(--shiki-default-font-style);font-weight: var(--shiki-default-font-weight);text-decoration: var(--shiki-default-text-decoration);}html .dark .shiki span {color: var(--shiki-dark);background: var(--shiki-dark-bg);font-style: var(--shiki-dark-font-style);font-weight: var(--shiki-dark-font-weight);text-decoration: var(--shiki-dark-text-decoration);}html.dark .shiki span {color: var(--shiki-dark);background: var(--shiki-dark-bg);font-style: var(--shiki-dark-font-style);font-weight: var(--shiki-dark-font-weight);text-decoration: var(--shiki-dark-text-decoration);}html pre.shiki code .sZMiF, html code.shiki .sZMiF{--shiki-light:#E2931D;--shiki-default:#005CC5;--shiki-dark:#79B8FF}html pre.shiki code .sjJ54, html code.shiki .sjJ54{--shiki-light:#39ADB5;--shiki-default:#032F62;--shiki-dark:#9ECBFF}html pre.shiki code .s_sjI, html code.shiki .s_sjI{--shiki-light:#91B859;--shiki-default:#032F62;--shiki-dark:#9ECBFF}",{"title":96,"searchDepth":135,"depth":135,"links":64883},[64884,64885,64886,64887,64890,64894,64898,64902,64903,64904,64905,64911],{"id":79,"depth":135,"text":80},{"id":5880,"depth":135,"text":5881},{"id":25090,"depth":135,"text":25091},{"id":11030,"depth":135,"text":11031,"children":64888},[64889],{"id":35437,"depth":142,"text":35438},{"id":63951,"depth":135,"text":63952,"children":64891},[64892,64893],{"id":63964,"depth":142,"text":63965},{"id":64029,"depth":142,"text":64030},{"id":64138,"depth":135,"text":64139,"children":64895},[64896,64897],{"id":64145,"depth":142,"text":64146},{"id":30957,"depth":142,"text":30958},{"id":64337,"depth":135,"text":64338,"children":64899},[64900,64901],{"id":64352,"depth":142,"text":64353},{"id":64427,"depth":142,"text":64428},{"id":1167,"depth":135,"text":1168},{"id":64712,"depth":135,"text":64713},{"id":7116,"depth":135,"text":7117},{"id":1513,"depth":135,"text":1514,"children":64906},[64907,64908,64909,64910],{"id":64824,"depth":142,"text":64825},{"id":64831,"depth":142,"text":64832},{"id":64838,"depth":142,"text":64839},{"id":64845,"depth":142,"text":64846},{"id":1553,"depth":135,"text":1554},"Master recursionerror in python causes and fixes in our comprehensive Python beginner guide.",{},"\u002Ferrors\u002Frecursionerror-in-python-causes-and-fixes",{"title":63679,"description":64912},"errors\u002Frecursionerror-in-python-causes-and-fixes","GS-NdbD-wzgujRj9ahzo_j3RrCxFrYzzEhPqArgK-V8",{"id":64919,"title":64920,"body":64921,"description":66259,"extension":1623,"meta":66260,"navigation":138,"path":66261,"seo":66262,"stem":66263,"__hash__":66264},"content\u002Ferrors\u002Frecursionerror-maximum-recursion-depth-exceeded-fix.md","RecursionError: maximum recursion depth exceeded (Fix)",{"type":7,"value":64922,"toc":66234},[64923,64926,64932,64935,64938,64940,65017,65021,65045,65048,65072,65077,65088,65090,65093,65095,65106,65113,65115,65118,65135,65138,65141,65149,65152,65154,65157,65215,65224,65227,65247,65250,65252,65256,65259,65330,65334,65337,65340,65414,65416,65432,65436,65439,65509,65516,65520,65526,65614,65618,65662,65667,65671,65674,65754,65759,65762,65766,65769,65772,65842,65845,65891,65894,65898,65901,65903,65964,65967,65970,65978,65984,65987,65998,66001,66026,66029,66031,66034,66051,66054,66087,66090,66102,66107,66111,66114,66125,66128,66148,66153,66155,66158,66179,66181,66184,66187,66191,66194,66198,66201,66203,66206,66208,66231],[10,64924,64920],{"id":64925},"recursionerror-maximum-recursion-depth-exceeded-fix",[14,64927,64928,64931],{},[17,64929,64930],{},"RecursionError: maximum recursion depth exceeded"," means Python stopped your program because a function called itself too many times.",[14,64933,64934],{},"This usually happens with recursive functions. A recursive function is a function that calls itself. Python allows recursion, but it also has a safety limit so your program does not keep running forever and using too much memory.",[14,64936,64937],{},"If you want the quick fix first: check that your function has a base case, and make sure every recursive call moves toward that stopping point.",[77,64939,80],{"id":79},[91,64941,64943],{"className":93,"code":64942,"language":95,"meta":96,"style":96},"def count_down(n):\n    if n \u003C= 0:  # base case\n        return\n    print(n)\n    count_down(n - 1)\n\ncount_down(5)\n",[17,64944,64945,64958,64973,64977,64987,65002,65006],{"__ignoreMap":96},[100,64946,64947,64949,64952,64954,64956],{"class":102,"line":103},[100,64948,1078],{"class":1077},[100,64950,64951],{"class":1081}," count_down",[100,64953,170],{"class":118},[100,64955,1433],{"class":2519},[100,64957,522],{"class":118},[100,64959,64960,64962,64964,64966,64968,64970],{"class":102,"line":135},[100,64961,2777],{"class":145},[100,64963,1418],{"class":106},[100,64965,63723],{"class":110},[100,64967,1921],{"class":122},[100,64969,89],{"class":118},[100,64971,64972],{"class":414},"  # base case\n",[100,64974,64975],{"class":102,"line":142},[100,64976,63735],{"class":145},[100,64978,64979,64981,64983,64985],{"class":102,"line":152},[100,64980,200],{"class":114},[100,64982,170],{"class":118},[100,64984,1433],{"class":178},[100,64986,215],{"class":118},[100,64988,64989,64992,64994,64996,64998,65000],{"class":102,"line":164},[100,64990,64991],{"class":178},"    count_down",[100,64993,170],{"class":118},[100,64995,51224],{"class":178},[100,64997,2193],{"class":110},[100,64999,6915],{"class":122},[100,65001,215],{"class":118},[100,65003,65004],{"class":102,"line":185},[100,65005,139],{"emptyLinePlaceholder":138},[100,65007,65008,65011,65013,65015],{"class":102,"line":197},[100,65009,65010],{"class":178},"count_down",[100,65012,170],{"class":118},[100,65014,3557],{"class":122},[100,65016,215],{"class":118},[14,65018,65019],{},[22,65020,218],{},[91,65022,65023],{"className":93,"code":63782,"language":95,"meta":96,"style":96},[17,65024,65025,65029,65033,65037,65041],{"__ignoreMap":96},[100,65026,65027],{"class":102,"line":103},[100,65028,1909],{"class":122},[100,65030,65031],{"class":102,"line":135},[100,65032,63793],{"class":122},[100,65034,65035],{"class":102,"line":142},[100,65036,1500],{"class":122},[100,65038,65039],{"class":102,"line":152},[100,65040,233],{"class":122},[100,65042,65043],{"class":102,"line":164},[100,65044,228],{"class":122},[14,65046,65047],{},"Why this works:",[40,65049,65050,65058,65061],{},[43,65051,65052,65055,65056],{},[17,65053,65054],{},"if n \u003C= 0:"," is the ",[22,65057,63957],{},[43,65059,65060],{},"It stops the recursion",[43,65062,65063,65066,65067,65069,65070],{},[17,65064,65065],{},"count_down(n - 1)"," moves ",[17,65068,1433],{}," closer to ",[17,65071,2279],{},[14,65073,3840,65074,65076],{},[17,65075,17658],{}," problems happen because:",[40,65078,65079,65082,65085],{},[43,65080,65081],{},"the function has no base case",[43,65083,65084],{},"the base case is never reached",[43,65086,65087],{},"each call creates more calls than expected",[77,65089,5881],{"id":5880},[14,65091,65092],{},"Python raises this error when a function keeps calling itself until it hits Python's recursion limit.",[14,65094,12895],{},[40,65096,65097,65100,65103],{},[43,65098,65099],{},"Your function did not stop in time",[43,65101,65102],{},"Python stopped it for safety",[43,65104,65105],{},"The problem is usually in the recursion logic, not in Python itself",[14,65107,65108,65109,63848],{},"If you are new to the idea of recursive functions, see ",[295,65110,65112],{"href":65111},"\u002Fglossary\u002Fwhat-is-recursion-in-python","what recursion means in Python",[77,65114,26289],{"id":26288},[14,65116,65117],{},"This error commonly appears when:",[40,65119,65120,65123,65126,65129,65132],{},[43,65121,65122],{},"A recursive function has no base case",[43,65124,65125],{},"The base case exists but is never reached",[43,65127,65128],{},"The value changes in the wrong direction",[43,65130,65131],{},"Two functions call each other forever",[43,65133,65134],{},"The recursion is valid, but the depth is still too large",[14,65136,65137],{},"Here is the main idea:",[14,65139,65140],{},"A recursive function needs two things:",[3282,65142,65143,65146],{},[43,65144,65145],{},"A stopping condition",[43,65147,65148],{},"A change that moves each call toward that stopping condition",[14,65150,65151],{},"Without both, recursion does not end.",[77,65153,11031],{"id":11030},[14,65155,65156],{},"Here is a small example that fails:",[91,65158,65160],{"className":93,"code":65159,"language":95,"meta":96,"style":96},"def count_forever(n):\n    print(n)\n    count_forever(n - 1)\n\ncount_forever(5)\n",[17,65161,65162,65175,65185,65200,65204],{"__ignoreMap":96},[100,65163,65164,65166,65169,65171,65173],{"class":102,"line":103},[100,65165,1078],{"class":1077},[100,65167,65168],{"class":1081}," count_forever",[100,65170,170],{"class":118},[100,65172,1433],{"class":2519},[100,65174,522],{"class":118},[100,65176,65177,65179,65181,65183],{"class":102,"line":135},[100,65178,200],{"class":114},[100,65180,170],{"class":118},[100,65182,1433],{"class":178},[100,65184,215],{"class":118},[100,65186,65187,65190,65192,65194,65196,65198],{"class":102,"line":142},[100,65188,65189],{"class":178},"    count_forever",[100,65191,170],{"class":118},[100,65193,51224],{"class":178},[100,65195,2193],{"class":110},[100,65197,6915],{"class":122},[100,65199,215],{"class":118},[100,65201,65202],{"class":102,"line":152},[100,65203,139],{"emptyLinePlaceholder":138},[100,65205,65206,65209,65211,65213],{"class":102,"line":164},[100,65207,65208],{"class":178},"count_forever",[100,65210,170],{"class":118},[100,65212,3557],{"class":122},[100,65214,215],{"class":118},[14,65216,65217,65218,65220,65221,290],{},"This causes a ",[17,65219,17658],{}," because the function has ",[22,65222,65223],{},"no base case",[14,65225,65226],{},"It keeps calling itself again and again:",[40,65228,65229,65234,65239,65244],{},[43,65230,65231],{},[17,65232,65233],{},"count_forever(5)",[43,65235,65236],{},[17,65237,65238],{},"count_forever(4)",[43,65240,65241],{},[17,65242,65243],{},"count_forever(3)",[43,65245,65246],{},"and so on...",[14,65248,65249],{},"It never stops, so Python eventually raises the error.",[77,65251,6244],{"id":6243},[675,65253,65255],{"id":65254},"_1-add-a-base-case","1. Add a base case",[14,65257,65258],{},"A base case is the condition that stops the function.",[91,65260,65262],{"className":93,"code":65261,"language":95,"meta":96,"style":96},"def count_down(n):\n    if n \u003C= 0:\n        return\n    print(n)\n    count_down(n - 1)\n\ncount_down(5)\n",[17,65263,65264,65276,65288,65292,65302,65316,65320],{"__ignoreMap":96},[100,65265,65266,65268,65270,65272,65274],{"class":102,"line":103},[100,65267,1078],{"class":1077},[100,65269,64951],{"class":1081},[100,65271,170],{"class":118},[100,65273,1433],{"class":2519},[100,65275,522],{"class":118},[100,65277,65278,65280,65282,65284,65286],{"class":102,"line":135},[100,65279,2777],{"class":145},[100,65281,1418],{"class":106},[100,65283,63723],{"class":110},[100,65285,1921],{"class":122},[100,65287,149],{"class":118},[100,65289,65290],{"class":102,"line":142},[100,65291,63735],{"class":145},[100,65293,65294,65296,65298,65300],{"class":102,"line":152},[100,65295,200],{"class":114},[100,65297,170],{"class":118},[100,65299,1433],{"class":178},[100,65301,215],{"class":118},[100,65303,65304,65306,65308,65310,65312,65314],{"class":102,"line":164},[100,65305,64991],{"class":178},[100,65307,170],{"class":118},[100,65309,51224],{"class":178},[100,65311,2193],{"class":110},[100,65313,6915],{"class":122},[100,65315,215],{"class":118},[100,65317,65318],{"class":102,"line":185},[100,65319,139],{"emptyLinePlaceholder":138},[100,65321,65322,65324,65326,65328],{"class":102,"line":197},[100,65323,65010],{"class":178},[100,65325,170],{"class":118},[100,65327,3557],{"class":122},[100,65329,215],{"class":118},[675,65331,65333],{"id":65332},"_2-make-sure-each-call-gets-closer-to-stopping","2. Make sure each call gets closer to stopping",[14,65335,65336],{},"A base case is not enough if your value never reaches it.",[14,65338,65339],{},"This version looks close, but it is wrong:",[91,65341,65343],{"className":93,"code":65342,"language":95,"meta":96,"style":96},"def count_down(n):\n    if n \u003C= 0:\n        return\n    print(n)\n    count_down(n + 1)  # wrong direction\n\ncount_down(5)\n",[17,65344,65345,65357,65369,65373,65383,65400,65404],{"__ignoreMap":96},[100,65346,65347,65349,65351,65353,65355],{"class":102,"line":103},[100,65348,1078],{"class":1077},[100,65350,64951],{"class":1081},[100,65352,170],{"class":118},[100,65354,1433],{"class":2519},[100,65356,522],{"class":118},[100,65358,65359,65361,65363,65365,65367],{"class":102,"line":135},[100,65360,2777],{"class":145},[100,65362,1418],{"class":106},[100,65364,63723],{"class":110},[100,65366,1921],{"class":122},[100,65368,149],{"class":118},[100,65370,65371],{"class":102,"line":142},[100,65372,63735],{"class":145},[100,65374,65375,65377,65379,65381],{"class":102,"line":152},[100,65376,200],{"class":114},[100,65378,170],{"class":118},[100,65380,1433],{"class":178},[100,65382,215],{"class":118},[100,65384,65385,65387,65389,65391,65393,65395,65397],{"class":102,"line":164},[100,65386,64991],{"class":178},[100,65388,170],{"class":118},[100,65390,51224],{"class":178},[100,65392,6547],{"class":110},[100,65394,6915],{"class":122},[100,65396,6155],{"class":118},[100,65398,65399],{"class":414},"  # wrong direction\n",[100,65401,65402],{"class":102,"line":185},[100,65403,139],{"emptyLinePlaceholder":138},[100,65405,65406,65408,65410,65412],{"class":102,"line":197},[100,65407,65010],{"class":178},[100,65409,170],{"class":118},[100,65411,3557],{"class":122},[100,65413,215],{"class":118},[14,65415,39219],{},[40,65417,65418,65424,65429],{},[43,65419,65420,65421],{},"The base case is ",[17,65422,65423],{},"n \u003C= 0",[43,65425,38150,65426,65428],{},[17,65427,1433],{}," keeps increasing",[43,65430,65431],{},"So the stopping condition never becomes true",[675,65433,65435],{"id":65434},"_3-check-for-recursive-calls-using-the-same-value","3. Check for recursive calls using the same value",[14,65437,65438],{},"If the function keeps using the same value, it will not make progress.",[91,65440,65442],{"className":93,"code":65441,"language":95,"meta":96,"style":96},"def count_down(n):\n    if n \u003C= 0:\n        return\n    print(n)\n    count_down(n)  # same value again\n\ncount_down(5)\n",[17,65443,65444,65456,65468,65472,65482,65495,65499],{"__ignoreMap":96},[100,65445,65446,65448,65450,65452,65454],{"class":102,"line":103},[100,65447,1078],{"class":1077},[100,65449,64951],{"class":1081},[100,65451,170],{"class":118},[100,65453,1433],{"class":2519},[100,65455,522],{"class":118},[100,65457,65458,65460,65462,65464,65466],{"class":102,"line":135},[100,65459,2777],{"class":145},[100,65461,1418],{"class":106},[100,65463,63723],{"class":110},[100,65465,1921],{"class":122},[100,65467,149],{"class":118},[100,65469,65470],{"class":102,"line":142},[100,65471,63735],{"class":145},[100,65473,65474,65476,65478,65480],{"class":102,"line":152},[100,65475,200],{"class":114},[100,65477,170],{"class":118},[100,65479,1433],{"class":178},[100,65481,215],{"class":118},[100,65483,65484,65486,65488,65490,65492],{"class":102,"line":164},[100,65485,64991],{"class":178},[100,65487,170],{"class":118},[100,65489,1433],{"class":178},[100,65491,6155],{"class":118},[100,65493,65494],{"class":414},"  # same value again\n",[100,65496,65497],{"class":102,"line":185},[100,65498,139],{"emptyLinePlaceholder":138},[100,65500,65501,65503,65505,65507],{"class":102,"line":197},[100,65502,65010],{"class":178},[100,65504,170],{"class":118},[100,65506,3557],{"class":122},[100,65508,215],{"class":118},[14,65510,65511,65512,65515],{},"This keeps calling ",[17,65513,65514],{},"count_down(5)"," forever.",[675,65517,65519],{"id":65518},"_4-print-the-value-to-trace-what-is-happening","4. Print the value to trace what is happening",[14,65521,65522,65523,65525],{},"A simple ",[17,65524,13235],{}," can help you see whether the function is moving toward the base case.",[91,65527,65529],{"className":93,"code":65528,"language":95,"meta":96,"style":96},"def count_down(n):\n    print(\"calling with:\", n)\n    \n    if n \u003C= 0:\n        return\n    \n    count_down(n - 1)\n\ncount_down(3)\n",[17,65530,65531,65543,65561,65566,65578,65582,65586,65600,65604],{"__ignoreMap":96},[100,65532,65533,65535,65537,65539,65541],{"class":102,"line":103},[100,65534,1078],{"class":1077},[100,65536,64951],{"class":1081},[100,65538,170],{"class":118},[100,65540,1433],{"class":2519},[100,65542,522],{"class":118},[100,65544,65545,65547,65549,65551,65553,65555,65557,65559],{"class":102,"line":135},[100,65546,200],{"class":114},[100,65548,170],{"class":118},[100,65550,206],{"class":205},[100,65552,64561],{"class":209},[100,65554,206],{"class":205},[100,65556,126],{"class":118},[100,65558,64568],{"class":178},[100,65560,215],{"class":118},[100,65562,65563],{"class":102,"line":142},[100,65564,65565],{"class":106},"    \n",[100,65567,65568,65570,65572,65574,65576],{"class":102,"line":152},[100,65569,2777],{"class":145},[100,65571,1418],{"class":106},[100,65573,63723],{"class":110},[100,65575,1921],{"class":122},[100,65577,149],{"class":118},[100,65579,65580],{"class":102,"line":164},[100,65581,63735],{"class":145},[100,65583,65584],{"class":102,"line":185},[100,65585,65565],{"class":106},[100,65587,65588,65590,65592,65594,65596,65598],{"class":102,"line":197},[100,65589,64991],{"class":178},[100,65591,170],{"class":118},[100,65593,51224],{"class":178},[100,65595,2193],{"class":110},[100,65597,6915],{"class":122},[100,65599,215],{"class":118},[100,65601,65602],{"class":102,"line":771},[100,65603,139],{"emptyLinePlaceholder":138},[100,65605,65606,65608,65610,65612],{"class":102,"line":787},[100,65607,65010],{"class":178},[100,65609,170],{"class":118},[100,65611,11541],{"class":122},[100,65613,215],{"class":118},[14,65615,65616],{},[22,65617,218],{},[91,65619,65620],{"className":93,"code":64629,"language":95,"meta":96,"style":96},[17,65621,65622,65632,65642,65652],{"__ignoreMap":96},[100,65623,65624,65626,65628,65630],{"class":102,"line":103},[100,65625,45],{"class":106},[100,65627,17521],{"class":145},[100,65629,89],{"class":118},[100,65631,31170],{"class":122},[100,65633,65634,65636,65638,65640],{"class":102,"line":135},[100,65635,45],{"class":106},[100,65637,17521],{"class":145},[100,65639,89],{"class":118},[100,65641,3564],{"class":122},[100,65643,65644,65646,65648,65650],{"class":102,"line":142},[100,65645,45],{"class":106},[100,65647,17521],{"class":145},[100,65649,89],{"class":118},[100,65651,1093],{"class":122},[100,65653,65654,65656,65658,65660],{"class":102,"line":152},[100,65655,45],{"class":106},[100,65657,17521],{"class":145},[100,65659,89],{"class":118},[100,65661,2069],{"class":122},[14,65663,65664,65665,290],{},"This is a fast way to debug recursive code. If you want more general debugging help, see this ",[295,65666,7077],{"href":3852},[675,65668,65670],{"id":65669},"_5-check-for-accidental-circular-calls","5. Check for accidental circular calls",[14,65672,65673],{},"Sometimes one function does not call itself directly. Instead, two functions call each other forever.",[91,65675,65677],{"className":93,"code":65676,"language":95,"meta":96,"style":96},"def func_a():\n    print(\"A\")\n    func_b()\n\ndef func_b():\n    print(\"B\")\n    func_a()\n\nfunc_a()\n",[17,65678,65679,65688,65702,65709,65713,65722,65736,65743,65747],{"__ignoreMap":96},[100,65680,65681,65683,65686],{"class":102,"line":103},[100,65682,1078],{"class":1077},[100,65684,65685],{"class":1081}," func_a",[100,65687,1085],{"class":118},[100,65689,65690,65692,65694,65696,65698,65700],{"class":102,"line":135},[100,65691,200],{"class":114},[100,65693,170],{"class":118},[100,65695,206],{"class":205},[100,65697,7414],{"class":209},[100,65699,206],{"class":205},[100,65701,215],{"class":118},[100,65703,65704,65707],{"class":102,"line":142},[100,65705,65706],{"class":178},"    func_b",[100,65708,4734],{"class":118},[100,65710,65711],{"class":102,"line":152},[100,65712,139],{"emptyLinePlaceholder":138},[100,65714,65715,65717,65720],{"class":102,"line":164},[100,65716,1078],{"class":1077},[100,65718,65719],{"class":1081}," func_b",[100,65721,1085],{"class":118},[100,65723,65724,65726,65728,65730,65732,65734],{"class":102,"line":185},[100,65725,200],{"class":114},[100,65727,170],{"class":118},[100,65729,206],{"class":205},[100,65731,7423],{"class":209},[100,65733,206],{"class":205},[100,65735,215],{"class":118},[100,65737,65738,65741],{"class":102,"line":197},[100,65739,65740],{"class":178},"    func_a",[100,65742,4734],{"class":118},[100,65744,65745],{"class":102,"line":771},[100,65746,139],{"emptyLinePlaceholder":138},[100,65748,65749,65752],{"class":102,"line":787},[100,65750,65751],{"class":178},"func_a",[100,65753,4734],{"class":118},[14,65755,65756,65757,290],{},"This also leads to a ",[17,65758,17658],{},[14,65760,65761],{},"To fix it, make sure one of the functions has a condition that stops the chain of calls.",[675,65763,65765],{"id":65764},"_6-use-a-loop-when-recursion-is-not-necessary","6. Use a loop when recursion is not necessary",[14,65767,65768],{},"For many beginner tasks, a loop is simpler and safer.",[14,65770,65771],{},"Recursive version:",[91,65773,65774],{"className":93,"code":65261,"language":95,"meta":96,"style":96},[17,65775,65776,65788,65800,65804,65814,65828,65832],{"__ignoreMap":96},[100,65777,65778,65780,65782,65784,65786],{"class":102,"line":103},[100,65779,1078],{"class":1077},[100,65781,64951],{"class":1081},[100,65783,170],{"class":118},[100,65785,1433],{"class":2519},[100,65787,522],{"class":118},[100,65789,65790,65792,65794,65796,65798],{"class":102,"line":135},[100,65791,2777],{"class":145},[100,65793,1418],{"class":106},[100,65795,63723],{"class":110},[100,65797,1921],{"class":122},[100,65799,149],{"class":118},[100,65801,65802],{"class":102,"line":142},[100,65803,63735],{"class":145},[100,65805,65806,65808,65810,65812],{"class":102,"line":152},[100,65807,200],{"class":114},[100,65809,170],{"class":118},[100,65811,1433],{"class":178},[100,65813,215],{"class":118},[100,65815,65816,65818,65820,65822,65824,65826],{"class":102,"line":164},[100,65817,64991],{"class":178},[100,65819,170],{"class":118},[100,65821,51224],{"class":178},[100,65823,2193],{"class":110},[100,65825,6915],{"class":122},[100,65827,215],{"class":118},[100,65829,65830],{"class":102,"line":185},[100,65831,139],{"emptyLinePlaceholder":138},[100,65833,65834,65836,65838,65840],{"class":102,"line":197},[100,65835,65010],{"class":178},[100,65837,170],{"class":118},[100,65839,3557],{"class":122},[100,65841,215],{"class":118},[14,65843,65844],{},"Loop version:",[91,65846,65847],{"className":93,"code":64431,"language":95,"meta":96,"style":96},[17,65848,65849,65857,65861,65873,65883],{"__ignoreMap":96},[100,65850,65851,65853,65855],{"class":102,"line":103},[100,65852,51224],{"class":106},[100,65854,111],{"class":110},[100,65856,37580],{"class":122},[100,65858,65859],{"class":102,"line":135},[100,65860,139],{"emptyLinePlaceholder":138},[100,65862,65863,65865,65867,65869,65871],{"class":102,"line":142},[100,65864,721],{"class":145},[100,65866,1418],{"class":106},[100,65868,1918],{"class":110},[100,65870,1921],{"class":122},[100,65872,149],{"class":118},[100,65874,65875,65877,65879,65881],{"class":102,"line":152},[100,65876,200],{"class":114},[100,65878,170],{"class":118},[100,65880,1433],{"class":178},[100,65882,215],{"class":118},[100,65884,65885,65887,65889],{"class":102,"line":164},[100,65886,64472],{"class":106},[100,65888,64475],{"class":110},[100,65890,1093],{"class":122},[14,65892,65893],{},"For simple repetition, a loop is often easier to read and avoids recursion depth problems.",[77,65895,65897],{"id":65896},"deep-recursion-that-is-technically-correct","Deep recursion that is technically correct",[14,65899,65900],{},"Sometimes your recursion logic is correct, but the input is so large that Python still reaches the recursion limit.",[14,65902,1844],{},[91,65904,65906],{"className":93,"code":65905,"language":95,"meta":96,"style":96},"def count_down(n):\n    if n \u003C= 0:\n        return\n    count_down(n - 1)\n\ncount_down(2000)\n",[17,65907,65908,65920,65932,65936,65950,65954],{"__ignoreMap":96},[100,65909,65910,65912,65914,65916,65918],{"class":102,"line":103},[100,65911,1078],{"class":1077},[100,65913,64951],{"class":1081},[100,65915,170],{"class":118},[100,65917,1433],{"class":2519},[100,65919,522],{"class":118},[100,65921,65922,65924,65926,65928,65930],{"class":102,"line":135},[100,65923,2777],{"class":145},[100,65925,1418],{"class":106},[100,65927,63723],{"class":110},[100,65929,1921],{"class":122},[100,65931,149],{"class":118},[100,65933,65934],{"class":102,"line":142},[100,65935,63735],{"class":145},[100,65937,65938,65940,65942,65944,65946,65948],{"class":102,"line":152},[100,65939,64991],{"class":178},[100,65941,170],{"class":118},[100,65943,51224],{"class":178},[100,65945,2193],{"class":110},[100,65947,6915],{"class":122},[100,65949,215],{"class":118},[100,65951,65952],{"class":102,"line":164},[100,65953,139],{"emptyLinePlaceholder":138},[100,65955,65956,65958,65960,65962],{"class":102,"line":185},[100,65957,65010],{"class":178},[100,65959,170],{"class":118},[100,65961,64752],{"class":122},[100,65963,215],{"class":118},[14,65965,65966],{},"This may fail even though the function does stop eventually.",[14,65968,65969],{},"For beginners, the best fix is usually:",[40,65971,65972,65975],{},[43,65973,65974],{},"rewrite the code using a loop",[43,65976,65977],{},"avoid very deep recursion unless you truly need it",[14,65979,65980,65981,65983],{},"It is possible to change the recursion limit with ",[17,65982,64722],{},", but this is usually not the best first fix.",[14,65985,65986],{},"Why not?",[40,65988,65989,65992,65995],{},[43,65990,65991],{},"It can still fail",[43,65993,65994],{},"It can use more memory",[43,65996,65997],{},"It may hide a logic error instead of fixing it",[14,65999,66000],{},"If you are curious, Python can show help for that function:",[91,66002,66004],{"className":93,"code":66003,"language":95,"meta":96,"style":96},"import sys\nhelp(sys.setrecursionlimit)\n",[17,66005,66006,66012],{"__ignoreMap":96},[100,66007,66008,66010],{"class":102,"line":103},[100,66009,9259],{"class":145},[100,66011,26131],{"class":106},[100,66013,66014,66016,66018,66020,66022,66024],{"class":102,"line":135},[100,66015,2478],{"class":114},[100,66017,170],{"class":118},[100,66019,26140],{"class":178},[100,66021,290],{"class":118},[100,66023,64747],{"class":2494},[100,66025,215],{"class":118},[14,66027,66028],{},"Use this carefully. In most beginner programs, changing the limit is not necessary.",[77,66030,6924],{"id":6923},[14,66032,66033],{},"If you see this error, use this checklist:",[3282,66035,66036,66039,66042,66045,66048],{},[43,66037,66038],{},"Find the recursive function in the traceback",[43,66040,66041],{},"Identify the base case",[43,66043,66044],{},"Check whether the base case can actually become true",[43,66046,66047],{},"Check whether the argument changes on every call",[43,66049,66050],{},"Test with a very small input first",[14,66052,66053],{},"Useful debug lines:",[91,66055,66057],{"className":93,"code":66056,"language":95,"meta":96,"style":96},"print(n)\nprint(\"calling with:\", n)\n",[17,66058,66059,66069],{"__ignoreMap":96},[100,66060,66061,66063,66065,66067],{"class":102,"line":103},[100,66062,372],{"class":114},[100,66064,170],{"class":118},[100,66066,1433],{"class":178},[100,66068,215],{"class":118},[100,66070,66071,66073,66075,66077,66079,66081,66083,66085],{"class":102,"line":135},[100,66072,372],{"class":114},[100,66074,170],{"class":118},[100,66076,206],{"class":205},[100,66078,64561],{"class":209},[100,66080,206],{"class":205},[100,66082,126],{"class":118},[100,66084,64568],{"class":178},[100,66086,215],{"class":118},[14,66088,66089],{},"You can also inspect the traceback:",[91,66091,66093],{"className":93,"code":66092,"language":95,"meta":96,"style":96},"import traceback\n",[17,66094,66095],{"__ignoreMap":96},[100,66096,66097,66099],{"class":102,"line":103},[100,66098,9259],{"class":145},[100,66100,66101],{"class":106}," traceback\n",[14,66103,66104,66105,290],{},"If Python errors and exceptions are still confusing, read ",[295,66106,1587],{"href":1749},[77,66108,66110],{"id":66109},"related-errors-and-confusion","Related errors and confusion",[14,66112,66113],{},"A recursion problem can make your program:",[40,66115,66116,66119,66122],{},[43,66117,66118],{},"slow before it fails",[43,66120,66121],{},"print many repeated lines",[43,66123,66124],{},"appear stuck",[14,66126,66127],{},"This error is different from other common errors:",[40,66129,66130,66137,66145],{},[43,66131,41219,66132,25407,66134,66136],{},[22,66133,1059],{},[17,66135,16503],{},", because the code can start running before failing",[43,66138,41219,66139,66141,66142,66144],{},[22,66140,1059],{}," usually a ",[17,66143,5616],{},", because the function name normally exists",[43,66146,66147],{},"It is a runtime problem caused by how the function behaves while the program runs",[14,66149,66150,66151,290],{},"If you want to review how functions work in general, see ",[295,66152,29749],{"href":29189},[77,66154,1344],{"id":1343},[14,66156,66157],{},"These are the most common reasons beginners get this error:",[40,66159,66160,66162,66164,66167,66170,66173,66176],{},[43,66161,64802],{},[43,66163,64805],{},[43,66165,66166],{},"Recursive call uses the same value again",[43,66168,66169],{},"Recursive call moves away from the stopping condition",[43,66171,66172],{},"Mutual recursion between two functions",[43,66174,66175],{},"Using recursion for a task better solved with a loop",[43,66177,66178],{},"Trying to process very large input with recursion",[77,66180,1514],{"id":1513},[675,66182,64862],{"id":66183},"what-is-recursion-in-python",[14,66185,66186],{},"Recursion is when a function calls itself. It must have a stopping condition called a base case.",[675,66188,66190],{"id":66189},"can-i-fix-this-by-increasing-the-recursion-limit","Can I fix this by increasing the recursion limit?",[14,66192,66193],{},"Sometimes, but it is usually better to fix the logic or rewrite the code with a loop first.",[675,66195,66197],{"id":66196},"why-does-my-base-case-not-work","Why does my base case not work?",[14,66199,66200],{},"The value may never reach the condition, or the comparison may be wrong.",[675,66202,64846],{"id":64845},[14,66204,66205],{},"No, but for many beginner tasks a loop is simpler and avoids recursion depth problems.",[77,66207,1554],{"id":1553},[40,66209,66210,66215,66219,66223,66227],{},[43,66211,66212],{},[295,66213,66214],{"href":65111},"What recursion means in Python",[43,66216,66217],{},[295,66218,29749],{"href":29189},[43,66220,66221],{},[295,66222,42267],{"href":3852},[43,66224,66225],{},[295,66226,1587],{"href":1749},[43,66228,66229],{},[295,66230,52120],{"href":52119},[1589,66232,66233],{},"html pre.shiki code .sbsja, html code.shiki .sbsja{--shiki-light:#9C3EDA;--shiki-default:#D73A49;--shiki-dark:#F97583}html pre.shiki code .sGLFI, html code.shiki .sGLFI{--shiki-light:#6182B8;--shiki-default:#6F42C1;--shiki-dark:#B392F0}html pre.shiki code .sP7_E, html code.shiki .sP7_E{--shiki-light:#39ADB5;--shiki-default:#24292E;--shiki-dark:#E1E4E8}html pre.shiki code .sFwrP, html code.shiki .sFwrP{--shiki-light:#90A4AE;--shiki-light-font-style:italic;--shiki-default:#24292E;--shiki-default-font-style:inherit;--shiki-dark:#E1E4E8;--shiki-dark-font-style:inherit}html pre.shiki code .sVHd0, html code.shiki .sVHd0{--shiki-light:#39ADB5;--shiki-light-font-style:italic;--shiki-default:#D73A49;--shiki-default-font-style:inherit;--shiki-dark:#F97583;--shiki-dark-font-style:inherit}html pre.shiki code .su5hD, html code.shiki .su5hD{--shiki-light:#90A4AE;--shiki-default:#24292E;--shiki-dark:#E1E4E8}html pre.shiki code .smGrS, html code.shiki .smGrS{--shiki-light:#39ADB5;--shiki-default:#D73A49;--shiki-dark:#F97583}html pre.shiki code .srdBf, html code.shiki .srdBf{--shiki-light:#F76D47;--shiki-default:#005CC5;--shiki-dark:#79B8FF}html pre.shiki code .sutJx, html code.shiki .sutJx{--shiki-light:#90A4AE;--shiki-light-font-style:italic;--shiki-default:#6A737D;--shiki-default-font-style:inherit;--shiki-dark:#6A737D;--shiki-dark-font-style:inherit}html pre.shiki code .sptTA, html code.shiki .sptTA{--shiki-light:#6182B8;--shiki-default:#005CC5;--shiki-dark:#79B8FF}html pre.shiki code .slqww, html code.shiki .slqww{--shiki-light:#6182B8;--shiki-default:#24292E;--shiki-dark:#E1E4E8}html .light .shiki span {color: var(--shiki-light);background: var(--shiki-light-bg);font-style: var(--shiki-light-font-style);font-weight: var(--shiki-light-font-weight);text-decoration: var(--shiki-light-text-decoration);}html.light .shiki span {color: var(--shiki-light);background: var(--shiki-light-bg);font-style: var(--shiki-light-font-style);font-weight: var(--shiki-light-font-weight);text-decoration: var(--shiki-light-text-decoration);}html .default .shiki span {color: var(--shiki-default);background: var(--shiki-default-bg);font-style: var(--shiki-default-font-style);font-weight: var(--shiki-default-font-weight);text-decoration: var(--shiki-default-text-decoration);}html .shiki span {color: var(--shiki-default);background: var(--shiki-default-bg);font-style: var(--shiki-default-font-style);font-weight: var(--shiki-default-font-weight);text-decoration: var(--shiki-default-text-decoration);}html .dark .shiki span {color: var(--shiki-dark);background: var(--shiki-dark-bg);font-style: var(--shiki-dark-font-style);font-weight: var(--shiki-dark-font-weight);text-decoration: var(--shiki-dark-text-decoration);}html.dark .shiki span {color: var(--shiki-dark);background: var(--shiki-dark-bg);font-style: var(--shiki-dark-font-style);font-weight: var(--shiki-dark-font-weight);text-decoration: var(--shiki-dark-text-decoration);}html pre.shiki code .sjJ54, html code.shiki .sjJ54{--shiki-light:#39ADB5;--shiki-default:#032F62;--shiki-dark:#9ECBFF}html pre.shiki code .s_sjI, html code.shiki .s_sjI{--shiki-light:#91B859;--shiki-default:#032F62;--shiki-dark:#9ECBFF}html pre.shiki code .skxfh, html code.shiki .skxfh{--shiki-light:#E53935;--shiki-default:#24292E;--shiki-dark:#E1E4E8}",{"title":96,"searchDepth":135,"depth":135,"links":66235},[66236,66237,66238,66239,66240,66248,66249,66250,66251,66252,66258],{"id":79,"depth":135,"text":80},{"id":5880,"depth":135,"text":5881},{"id":26288,"depth":135,"text":26289},{"id":11030,"depth":135,"text":11031},{"id":6243,"depth":135,"text":6244,"children":66241},[66242,66243,66244,66245,66246,66247],{"id":65254,"depth":142,"text":65255},{"id":65332,"depth":142,"text":65333},{"id":65434,"depth":142,"text":65435},{"id":65518,"depth":142,"text":65519},{"id":65669,"depth":142,"text":65670},{"id":65764,"depth":142,"text":65765},{"id":65896,"depth":135,"text":65897},{"id":6923,"depth":135,"text":6924},{"id":66109,"depth":135,"text":66110},{"id":1343,"depth":135,"text":1344},{"id":1513,"depth":135,"text":1514,"children":66253},[66254,66255,66256,66257],{"id":66183,"depth":142,"text":64862},{"id":66189,"depth":142,"text":66190},{"id":66196,"depth":142,"text":66197},{"id":64845,"depth":142,"text":64846},{"id":1553,"depth":135,"text":1554},"Master recursionerror maximum recursion depth exceeded fix in our comprehensive Python beginner guide.",{},"\u002Ferrors\u002Frecursionerror-maximum-recursion-depth-exceeded-fix",{"title":64920,"description":66259},"errors\u002Frecursionerror-maximum-recursion-depth-exceeded-fix","6Z89f6JYTfEEyKQGFWrXzgMNKiZL6jIAD_Kk131-Nmk",{"id":66266,"title":66267,"body":66268,"description":67770,"extension":1623,"meta":67771,"navigation":138,"path":42113,"seo":67772,"stem":67773,"__hash__":67774},"content\u002Ferrors\u002Fruntimeerror-in-python-causes-and-fixes.md","RuntimeError in Python: Causes and Fixes",{"type":7,"value":66269,"toc":67748},[66270,66273,66281,66292,66298,66300,66355,66358,66366,66370,66379,66382,66405,66407,66419,66421,66426,66428,66448,66454,66460,66464,66467,66489,66491,66557,66560,66569,66572,66574,66620,66623,66627,66631,66634,66729,66731,66745,66747,66758,66761,66877,66879,66912,66916,66919,66982,66988,66994,67044,67047,67051,67054,67135,67141,67144,67219,67221,67230,67234,67240,67243,67260,67263,67278,67282,67292,67295,67317,67320,67342,67347,67349,67352,67370,67373,67394,67397,67436,67438,67543,67546,67553,67557,67563,67565,67635,67638,67649,67652,67654,67671,67676,67678,67682,67690,67694,67697,67701,67704,67708,67715,67717,67745],[10,66271,66267],{"id":66272},"runtimeerror-in-python-causes-and-fixes",[14,66274,66275,66277,66278,290],{},[17,66276,17663],{}," is a general Python exception that happens ",[22,66279,66280],{},"while your program is running",[14,66282,66283,66284,28239,66289,290],{},"For beginners, this error can feel vague because the name does not tell you exactly what went wrong. The important clue is usually the ",[22,66285,66286,66287],{},"message after ",[17,66288,17663],{},[22,66290,66291],{},"traceback line that points to the problem",[14,66293,66294,66295,66297],{},"This guide explains what ",[17,66296,17663],{}," means, why it happens, and how to debug it using simple real examples.",[77,66299,80],{"id":79},[91,66301,66303],{"className":93,"code":66302,"language":95,"meta":96,"style":96},"try:\n    # code that may fail at runtime\n    do_something()\nexcept RuntimeError as e:\n    print('RuntimeError:', e)\n",[17,66304,66305,66311,66316,66323,66336],{"__ignoreMap":96},[100,66306,66307,66309],{"class":102,"line":103},[100,66308,146],{"class":145},[100,66310,149],{"class":118},[100,66312,66313],{"class":102,"line":135},[100,66314,66315],{"class":414},"    # code that may fail at runtime\n",[100,66317,66318,66321],{"class":102,"line":142},[100,66319,66320],{"class":178},"    do_something",[100,66322,4734],{"class":118},[100,66324,66325,66327,66330,66332,66334],{"class":102,"line":152},[100,66326,188],{"class":145},[100,66328,66329],{"class":191}," RuntimeError",[100,66331,16224],{"class":145},[100,66333,16227],{"class":106},[100,66335,149],{"class":118},[100,66337,66338,66340,66342,66344,66347,66349,66351,66353],{"class":102,"line":164},[100,66339,200],{"class":114},[100,66341,170],{"class":118},[100,66343,1280],{"class":205},[100,66345,66346],{"class":209},"RuntimeError:",[100,66348,1280],{"class":205},[100,66350,126],{"class":118},[100,66352,16227],{"class":178},[100,66354,215],{"class":118},[14,66356,66357],{},"Catching the error can help you inspect the message, but the real fix is to find the operation that fails during program execution.",[14,66359,41228,66360,3266,66362,290],{},[295,66361,1587],{"href":1749},[295,66363,66365],{"href":66364},"\u002Fhow-to\u002Fhow-to-use-try-except-blocks-in-python\u002F","how to use try-except blocks in Python",[77,66367,66369],{"id":66368},"what-runtimeerror-means","What RuntimeError means",[14,66371,571,66372,66374,66375,66378],{},[17,66373,17663],{}," means a problem happened ",[22,66376,66377],{},"during execution",", not before the program started.",[14,66380,66381],{},"Key points:",[40,66383,66384,66387,66390,66399],{},[43,66385,66386],{},"It happens while the program is running",[43,66388,66389],{},"Python or a library detected a runtime problem",[43,66391,66392,66393,3178,66397],{},"It is more general than errors like ",[295,66394,66395],{"href":3264},[17,66396,3483],{},[17,66398,3538],{},[43,66400,66401,66402,66404],{},"The message after ",[17,66403,17663],{}," is often the most useful clue",[14,66406,580],{},[40,66408,66409,66414],{},[43,66410,66411,66413],{},[17,66412,16503],{}," means the code is written incorrectly",[43,66415,66416,66418],{},[17,66417,17663],{}," means the code started running, but failed during execution",[77,66420,3944],{"id":3943},[14,66422,66423,66425],{},[17,66424,17663],{}," often appears when the problem does not fit a more specific built-in exception.",[14,66427,14889],{},[40,66429,66430,66436,66439,66442,66445],{},[43,66431,66432,66433,66435],{},"A library raises ",[17,66434,17663],{}," for a general runtime problem",[43,66437,66438],{},"Your code changes something while Python is still using it",[43,66440,66441],{},"A generator or iterator stops in an unexpected way",[43,66443,66444],{},"Recursion or environment state causes execution to fail",[43,66446,66447],{},"A framework reports that something happened in the wrong state",[14,66449,66450,66451,66453],{},"In short, ",[17,66452,17663],{}," usually means:",[66455,66456,66457],"blockquote",{},[14,66458,66459],{},"\"The program reached a bad state while running.\"",[77,66461,66463],{"id":66462},"simple-example-that-raises-runtimeerror","Simple example that raises RuntimeError",[14,66465,66466],{},"Programmers and libraries can raise this error directly.",[91,66468,66470],{"className":93,"code":66469,"language":95,"meta":96,"style":96},"raise RuntimeError(\"something went wrong\")\n",[17,66471,66472],{"__ignoreMap":96},[100,66473,66474,66476,66478,66480,66482,66485,66487],{"class":102,"line":103},[100,66475,3066],{"class":145},[100,66477,66329],{"class":191},[100,66479,170],{"class":118},[100,66481,206],{"class":205},[100,66483,66484],{"class":209},"something went wrong",[100,66486,206],{"class":205},[100,66488,215],{"class":118},[14,66490,218],{},[91,66492,66494],{"className":93,"code":66493,"language":95,"meta":96,"style":96},"Traceback (most recent call last):\n  File \"example.py\", line 1, in \u003Cmodule>\n    raise RuntimeError(\"something went wrong\")\nRuntimeError: something went wrong\n",[17,66495,66496,66506,66532,66548],{"__ignoreMap":96},[100,66497,66498,66500,66502,66504],{"class":102,"line":103},[100,66499,514],{"class":178},[100,66501,170],{"class":118},[100,66503,519],{"class":178},[100,66505,522],{"class":118},[100,66507,66508,66510,66512,66514,66516,66518,66520,66522,66524,66526,66528,66530],{"class":102,"line":135},[100,66509,1952],{"class":106},[100,66511,206],{"class":205},[100,66513,1957],{"class":209},[100,66515,206],{"class":205},[100,66517,126],{"class":118},[100,66519,1964],{"class":106},[100,66521,123],{"class":122},[100,66523,126],{"class":118},[100,66525,1971],{"class":110},[100,66527,1974],{"class":110},[100,66529,1977],{"class":106},[100,66531,1980],{"class":110},[100,66533,66534,66536,66538,66540,66542,66544,66546],{"class":102,"line":142},[100,66535,1098],{"class":145},[100,66537,66329],{"class":191},[100,66539,170],{"class":118},[100,66541,206],{"class":205},[100,66543,66484],{"class":209},[100,66545,206],{"class":205},[100,66547,215],{"class":118},[100,66549,66550,66552,66554],{"class":102,"line":152},[100,66551,17663],{"class":191},[100,66553,89],{"class":118},[100,66555,66556],{"class":106}," something went wrong\n",[14,66558,66559],{},"The important part is the message:",[91,66561,66563],{"className":93,"code":66562,"language":95,"meta":96,"style":96},"something went wrong\n",[17,66564,66565],{"__ignoreMap":96},[100,66566,66567],{"class":102,"line":103},[100,66568,66562],{"class":106},[14,66570,66571],{},"In real programs, the message should be more specific.",[14,66573,580],{},[91,66575,66577],{"className":93,"code":66576,"language":95,"meta":96,"style":96},"user_logged_in = False\n\nif not user_logged_in:\n    raise RuntimeError(\"Cannot load dashboard before login\")\n",[17,66578,66579,66588,66592,66603],{"__ignoreMap":96},[100,66580,66581,66584,66586],{"class":102,"line":103},[100,66582,66583],{"class":106},"user_logged_in ",[100,66585,111],{"class":110},[100,66587,44952],{"class":158},[100,66589,66590],{"class":102,"line":135},[100,66591,139],{"emptyLinePlaceholder":138},[100,66593,66594,66596,66598,66601],{"class":102,"line":142},[100,66595,2736],{"class":145},[100,66597,4921],{"class":110},[100,66599,66600],{"class":106}," user_logged_in",[100,66602,149],{"class":118},[100,66604,66605,66607,66609,66611,66613,66616,66618],{"class":102,"line":152},[100,66606,1098],{"class":145},[100,66608,66329],{"class":191},[100,66610,170],{"class":118},[100,66612,206],{"class":205},[100,66614,66615],{"class":209},"Cannot load dashboard before login",[100,66617,206],{"class":205},[100,66619,215],{"class":118},[14,66621,66622],{},"This is much more helpful because it tells you what state was invalid.",[77,66624,66626],{"id":66625},"common-real-world-cases","Common real-world cases",[675,66628,66630],{"id":66629},"dictionary-changed-size-during-iteration","Dictionary changed size during iteration",[14,66632,66633],{},"A common runtime problem is changing a dictionary while looping through it.",[91,66635,66637],{"className":93,"code":66636,"language":95,"meta":96,"style":96},"data = {\"a\": 1, \"b\": 2, \"c\": 3}\n\nfor key in data:\n    if key == \"b\":\n        del data[key]\n",[17,66638,66639,66683,66687,66700,66716],{"__ignoreMap":96},[100,66640,66641,66643,66645,66647,66649,66651,66653,66655,66657,66659,66661,66663,66665,66667,66669,66671,66673,66675,66677,66679,66681],{"class":102,"line":103},[100,66642,7909],{"class":106},[100,66644,111],{"class":110},[100,66646,12080],{"class":118},[100,66648,206],{"class":205},[100,66650,295],{"class":209},[100,66652,206],{"class":205},[100,66654,89],{"class":118},[100,66656,6915],{"class":122},[100,66658,126],{"class":118},[100,66660,1708],{"class":205},[100,66662,4027],{"class":209},[100,66664,206],{"class":205},[100,66666,89],{"class":118},[100,66668,129],{"class":122},[100,66670,126],{"class":118},[100,66672,1708],{"class":205},[100,66674,4036],{"class":209},[100,66676,206],{"class":205},[100,66678,89],{"class":118},[100,66680,1405],{"class":122},[100,66682,12093],{"class":118},[100,66684,66685],{"class":102,"line":135},[100,66686,139],{"emptyLinePlaceholder":138},[100,66688,66689,66691,66694,66696,66698],{"class":102,"line":142},[100,66690,71],{"class":145},[100,66692,66693],{"class":106}," key ",[100,66695,617],{"class":145},[100,66697,12102],{"class":106},[100,66699,149],{"class":118},[100,66701,66702,66704,66706,66708,66710,66712,66714],{"class":102,"line":152},[100,66703,2777],{"class":145},[100,66705,66693],{"class":106},[100,66707,32169],{"class":110},[100,66709,1708],{"class":205},[100,66711,4027],{"class":209},[100,66713,206],{"class":205},[100,66715,149],{"class":118},[100,66717,66718,66721,66723,66725,66727],{"class":102,"line":164},[100,66719,66720],{"class":145},"        del",[100,66722,12102],{"class":106},[100,66724,3166],{"class":118},[100,66726,43824],{"class":106},[100,66728,603],{"class":118},[14,66730,7710],{},[91,66732,66734],{"className":93,"code":66733,"language":95,"meta":96,"style":96},"RuntimeError: dictionary changed size during iteration\n",[17,66735,66736],{"__ignoreMap":96},[100,66737,66738,66740,66742],{"class":102,"line":103},[100,66739,17663],{"class":191},[100,66741,89],{"class":118},[100,66743,66744],{"class":106}," dictionary changed size during iteration\n",[14,66746,47370],{},[40,66748,66749,66752,66755],{},[43,66750,66751],{},"Python is iterating over the dictionary",[43,66753,66754],{},"At the same time, the dictionary is being changed",[43,66756,66757],{},"That makes the iteration unsafe",[14,66759,66760],{},"One fix is to loop over a copy of the keys:",[91,66762,66764],{"className":93,"code":66763,"language":95,"meta":96,"style":96},"data = {\"a\": 1, \"b\": 2, \"c\": 3}\n\nfor key in list(data.keys()):\n    if key == \"b\":\n        del data[key]\n\nprint(data)\n",[17,66765,66766,66810,66814,66835,66851,66863,66867],{"__ignoreMap":96},[100,66767,66768,66770,66772,66774,66776,66778,66780,66782,66784,66786,66788,66790,66792,66794,66796,66798,66800,66802,66804,66806,66808],{"class":102,"line":103},[100,66769,7909],{"class":106},[100,66771,111],{"class":110},[100,66773,12080],{"class":118},[100,66775,206],{"class":205},[100,66777,295],{"class":209},[100,66779,206],{"class":205},[100,66781,89],{"class":118},[100,66783,6915],{"class":122},[100,66785,126],{"class":118},[100,66787,1708],{"class":205},[100,66789,4027],{"class":209},[100,66791,206],{"class":205},[100,66793,89],{"class":118},[100,66795,129],{"class":122},[100,66797,126],{"class":118},[100,66799,1708],{"class":205},[100,66801,4036],{"class":209},[100,66803,206],{"class":205},[100,66805,89],{"class":118},[100,66807,1405],{"class":122},[100,66809,12093],{"class":118},[100,66811,66812],{"class":102,"line":135},[100,66813,139],{"emptyLinePlaceholder":138},[100,66815,66816,66818,66820,66822,66824,66826,66828,66830,66832],{"class":102,"line":142},[100,66817,71],{"class":145},[100,66819,66693],{"class":106},[100,66821,617],{"class":145},[100,66823,14704],{"class":191},[100,66825,170],{"class":118},[100,66827,7934],{"class":178},[100,66829,290],{"class":118},[100,66831,6185],{"class":178},[100,66833,66834],{"class":118},"()):\n",[100,66836,66837,66839,66841,66843,66845,66847,66849],{"class":102,"line":152},[100,66838,2777],{"class":145},[100,66840,66693],{"class":106},[100,66842,32169],{"class":110},[100,66844,1708],{"class":205},[100,66846,4027],{"class":209},[100,66848,206],{"class":205},[100,66850,149],{"class":118},[100,66852,66853,66855,66857,66859,66861],{"class":102,"line":164},[100,66854,66720],{"class":145},[100,66856,12102],{"class":106},[100,66858,3166],{"class":118},[100,66860,43824],{"class":106},[100,66862,603],{"class":118},[100,66864,66865],{"class":102,"line":185},[100,66866,139],{"emptyLinePlaceholder":138},[100,66868,66869,66871,66873,66875],{"class":102,"line":197},[100,66870,372],{"class":114},[100,66872,170],{"class":118},[100,66874,7934],{"class":178},[100,66876,215],{"class":118},[14,66878,218],{},[91,66880,66882],{"className":93,"code":66881,"language":95,"meta":96,"style":96},"{'a': 1, 'c': 3}\n",[17,66883,66884],{"__ignoreMap":96},[100,66885,66886,66888,66890,66892,66894,66896,66898,66900,66902,66904,66906,66908,66910],{"class":102,"line":103},[100,66887,16490],{"class":118},[100,66889,1280],{"class":205},[100,66891,295],{"class":209},[100,66893,1280],{"class":205},[100,66895,89],{"class":118},[100,66897,6915],{"class":122},[100,66899,126],{"class":118},[100,66901,1274],{"class":205},[100,66903,4036],{"class":209},[100,66905,1280],{"class":205},[100,66907,89],{"class":118},[100,66909,1405],{"class":122},[100,66911,12093],{"class":118},[675,66913,66915],{"id":66914},"generator-raised-stopiteration-in-the-wrong-place","Generator raised StopIteration in the wrong place",[14,66917,66918],{},"Generators can also lead to runtime problems.",[91,66920,66922],{"className":93,"code":66921,"language":95,"meta":96,"style":96},"def broken_generator():\n    raise StopIteration(\"stop here\")\n    yield 1\n\nfor item in broken_generator():\n    print(item)\n",[17,66923,66924,66933,66950,66956,66960,66972],{"__ignoreMap":96},[100,66925,66926,66928,66931],{"class":102,"line":103},[100,66927,1078],{"class":1077},[100,66929,66930],{"class":1081}," broken_generator",[100,66932,1085],{"class":118},[100,66934,66935,66937,66939,66941,66943,66946,66948],{"class":102,"line":135},[100,66936,1098],{"class":145},[100,66938,192],{"class":191},[100,66940,170],{"class":118},[100,66942,206],{"class":205},[100,66944,66945],{"class":209},"stop here",[100,66947,206],{"class":205},[100,66949,215],{"class":118},[100,66951,66952,66954],{"class":102,"line":142},[100,66953,1090],{"class":145},[100,66955,1093],{"class":122},[100,66957,66958],{"class":102,"line":152},[100,66959,139],{"emptyLinePlaceholder":138},[100,66961,66962,66964,66966,66968,66970],{"class":102,"line":164},[100,66963,71],{"class":145},[100,66965,7568],{"class":106},[100,66967,617],{"class":145},[100,66969,66930],{"class":178},[100,66971,1085],{"class":118},[100,66973,66974,66976,66978,66980],{"class":102,"line":185},[100,66975,200],{"class":114},[100,66977,170],{"class":118},[100,66979,7555],{"class":178},[100,66981,215],{"class":118},[14,66983,66984,66985,66987],{},"In modern Python, this becomes a runtime error because raising ",[17,66986,19],{}," directly inside a generator is not the correct pattern.",[14,66989,66990,66991,66993],{},"A better approach is to use ",[17,66992,1053],{}," to stop the generator:",[91,66995,66997],{"className":93,"code":66996,"language":95,"meta":96,"style":96},"def good_generator():\n    return\n    yield 1\n\nfor item in good_generator():\n    print(item)\n",[17,66998,66999,67008,67012,67018,67022,67034],{"__ignoreMap":96},[100,67000,67001,67003,67006],{"class":102,"line":103},[100,67002,1078],{"class":1077},[100,67004,67005],{"class":1081}," good_generator",[100,67007,1085],{"class":118},[100,67009,67010],{"class":102,"line":135},[100,67011,1132],{"class":145},[100,67013,67014,67016],{"class":102,"line":142},[100,67015,1090],{"class":145},[100,67017,1093],{"class":122},[100,67019,67020],{"class":102,"line":152},[100,67021,139],{"emptyLinePlaceholder":138},[100,67023,67024,67026,67028,67030,67032],{"class":102,"line":164},[100,67025,71],{"class":145},[100,67027,7568],{"class":106},[100,67029,617],{"class":145},[100,67031,67005],{"class":178},[100,67033,1085],{"class":118},[100,67035,67036,67038,67040,67042],{"class":102,"line":185},[100,67037,200],{"class":114},[100,67039,170],{"class":118},[100,67041,7555],{"class":178},[100,67043,215],{"class":118},[14,67045,67046],{},"This ends the generator cleanly.",[675,67048,67050],{"id":67049},"operation-called-in-the-wrong-program-state","Operation called in the wrong program state",[14,67052,67053],{},"Sometimes your own code should only run when something has already happened.",[91,67055,67057],{"className":93,"code":67056,"language":95,"meta":96,"style":96},"connected = False\n\ndef send_data():\n    if not connected:\n        raise RuntimeError(\"Cannot send data before connecting\")\n    print(\"Data sent\")\n\nsend_data()\n",[17,67058,67059,67068,67072,67081,67092,67109,67124,67128],{"__ignoreMap":96},[100,67060,67061,67064,67066],{"class":102,"line":103},[100,67062,67063],{"class":106},"connected ",[100,67065,111],{"class":110},[100,67067,44952],{"class":158},[100,67069,67070],{"class":102,"line":135},[100,67071,139],{"emptyLinePlaceholder":138},[100,67073,67074,67076,67079],{"class":102,"line":142},[100,67075,1078],{"class":1077},[100,67077,67078],{"class":1081}," send_data",[100,67080,1085],{"class":118},[100,67082,67083,67085,67087,67090],{"class":102,"line":152},[100,67084,2777],{"class":145},[100,67086,4921],{"class":110},[100,67088,67089],{"class":106}," connected",[100,67091,149],{"class":118},[100,67093,67094,67096,67098,67100,67102,67105,67107],{"class":102,"line":164},[100,67095,2791],{"class":145},[100,67097,66329],{"class":191},[100,67099,170],{"class":118},[100,67101,206],{"class":205},[100,67103,67104],{"class":209},"Cannot send data before connecting",[100,67106,206],{"class":205},[100,67108,215],{"class":118},[100,67110,67111,67113,67115,67117,67120,67122],{"class":102,"line":185},[100,67112,200],{"class":114},[100,67114,170],{"class":118},[100,67116,206],{"class":205},[100,67118,67119],{"class":209},"Data sent",[100,67121,206],{"class":205},[100,67123,215],{"class":118},[100,67125,67126],{"class":102,"line":197},[100,67127,139],{"emptyLinePlaceholder":138},[100,67129,67130,67133],{"class":102,"line":771},[100,67131,67132],{"class":178},"send_data",[100,67134,4734],{"class":118},[14,67136,67137,67138,67140],{},"This raises a ",[17,67139,17663],{}," because the program state is invalid.",[14,67142,67143],{},"Fix it by changing the state first:",[91,67145,67147],{"className":93,"code":67146,"language":95,"meta":96,"style":96},"connected = True\n\ndef send_data():\n    if not connected:\n        raise RuntimeError(\"Cannot send data before connecting\")\n    print(\"Data sent\")\n\nsend_data()\n",[17,67148,67149,67157,67161,67169,67179,67195,67209,67213],{"__ignoreMap":96},[100,67150,67151,67153,67155],{"class":102,"line":103},[100,67152,67063],{"class":106},[100,67154,111],{"class":110},[100,67156,3588],{"class":158},[100,67158,67159],{"class":102,"line":135},[100,67160,139],{"emptyLinePlaceholder":138},[100,67162,67163,67165,67167],{"class":102,"line":142},[100,67164,1078],{"class":1077},[100,67166,67078],{"class":1081},[100,67168,1085],{"class":118},[100,67170,67171,67173,67175,67177],{"class":102,"line":152},[100,67172,2777],{"class":145},[100,67174,4921],{"class":110},[100,67176,67089],{"class":106},[100,67178,149],{"class":118},[100,67180,67181,67183,67185,67187,67189,67191,67193],{"class":102,"line":164},[100,67182,2791],{"class":145},[100,67184,66329],{"class":191},[100,67186,170],{"class":118},[100,67188,206],{"class":205},[100,67190,67104],{"class":209},[100,67192,206],{"class":205},[100,67194,215],{"class":118},[100,67196,67197,67199,67201,67203,67205,67207],{"class":102,"line":185},[100,67198,200],{"class":114},[100,67200,170],{"class":118},[100,67202,206],{"class":205},[100,67204,67119],{"class":209},[100,67206,206],{"class":205},[100,67208,215],{"class":118},[100,67210,67211],{"class":102,"line":197},[100,67212,139],{"emptyLinePlaceholder":138},[100,67214,67215,67217],{"class":102,"line":771},[100,67216,67132],{"class":178},[100,67218,4734],{"class":118},[14,67220,218],{},[91,67222,67224],{"className":93,"code":67223,"language":95,"meta":96,"style":96},"Data sent\n",[17,67225,67226],{"__ignoreMap":96},[100,67227,67228],{"class":102,"line":103},[100,67229,67223],{"class":106},[675,67231,67233],{"id":67232},"framework-or-package-reports-a-runtime-problem","Framework or package reports a runtime problem",[14,67235,67236,67237,67239],{},"Many third-party libraries use ",[17,67238,17663],{}," when something is technically wrong at runtime, but not a perfect match for another error type.",[14,67241,67242],{},"For example, a package may raise errors like:",[40,67244,67245,67250,67255],{},[43,67246,67247],{},[17,67248,67249],{},"RuntimeError: event loop is already running",[43,67251,67252],{},[17,67253,67254],{},"RuntimeError: cannot schedule new futures after shutdown",[43,67256,67257],{},[17,67258,67259],{},"RuntimeError: model is not initialized",[14,67261,67262],{},"In these cases:",[40,67264,67265,67268,67275],{},[43,67266,67267],{},"Read the full message carefully",[43,67269,67270,67271,67274],{},"Check the line in ",[22,67272,67273],{},"your code"," that called the library",[43,67276,67277],{},"Look for setup steps that were missed or called in the wrong order",[77,67279,67281],{"id":67280},"how-to-fix-runtimeerror","How to fix RuntimeError",[14,67283,3277,67284,67286,67287,28239,67290,290],{},[17,67285,17663],{},", do not focus only on the error name. Focus on the ",[22,67288,67289],{},"traceback",[22,67291,1836],{},[14,67293,67294],{},"Use this process:",[3282,67296,67297,67299,67302,67305,67308,67311,67314],{},[43,67298,12164],{},[43,67300,67301],{},"Look at the last line first",[43,67303,67304],{},"Find the exact file and line number in your code",[43,67306,67307],{},"Check what the program was doing at that line",[43,67309,67310],{},"Inspect the values of nearby variables",[43,67312,67313],{},"See whether you changed a collection during iteration",[43,67315,67316],{},"Check whether the code ran in the wrong state",[14,67318,67319],{},"A few common fixes:",[40,67321,67322,67325,67328,67331,67339],{},[43,67323,67324],{},"Avoid changing a dictionary or set while looping over it",[43,67326,67327],{},"Move state-changing code before or after the loop",[43,67329,67330],{},"Make sure setup happens before dependent operations",[43,67332,67333,67334,67336,67337],{},"Replace direct ",[17,67335,19],{}," in generators with ",[17,67338,1053],{},[43,67340,67341],{},"Check whether a library expects a different calling order",[14,67343,67344,67345,290],{},"If you need to recover from errors, see ",[295,67346,1029],{"href":2824},[77,67348,1168],{"id":1167},[14,67350,67351],{},"Use this checklist when you are stuck:",[40,67353,67354,67358,67361,67364,67367],{},[43,67355,16306,67356,48328],{},[22,67357,16302],{},[43,67359,67360],{},"Then read upward to find your file and line number",[43,67362,67363],{},"Check variable values near the failing line",[43,67365,67366],{},"Reduce the code to a small reproducible example",[43,67368,67369],{},"Test one fix at a time",[14,67371,67372],{},"These commands and tools can help:",[91,67374,67376],{"className":10352,"code":67375,"language":10354,"meta":96,"style":96},"python your_script.py\npython -m pdb your_script.py\n",[17,67377,67378,67384],{"__ignoreMap":96},[100,67379,67380,67382],{"class":102,"line":103},[100,67381,95],{"class":10361},[100,67383,30457],{"class":209},[100,67385,67386,67388,67390,67392],{"class":102,"line":135},[100,67387,95],{"class":10361},[100,67389,26622],{"class":10364},[100,67391,42038],{"class":209},[100,67393,30457],{"class":209},[14,67395,67396],{},"Useful checks inside your code:",[91,67398,67400],{"className":93,"code":67399,"language":95,"meta":96,"style":96},"print(variable_name)\nprint(type(variable_name))\nhelp(RuntimeError)\n",[17,67401,67402,67412,67426],{"__ignoreMap":96},[100,67403,67404,67406,67408,67410],{"class":102,"line":103},[100,67405,372],{"class":114},[100,67407,170],{"class":118},[100,67409,53946],{"class":178},[100,67411,215],{"class":118},[100,67413,67414,67416,67418,67420,67422,67424],{"class":102,"line":135},[100,67415,372],{"class":114},[100,67417,170],{"class":118},[100,67419,1250],{"class":191},[100,67421,170],{"class":118},[100,67423,53946],{"class":178},[100,67425,182],{"class":118},[100,67427,67428,67430,67432,67434],{"class":102,"line":142},[100,67429,2478],{"class":114},[100,67431,170],{"class":118},[100,67433,17663],{"class":191},[100,67435,215],{"class":118},[14,67437,1844],{},[91,67439,67441],{"className":93,"code":67440,"language":95,"meta":96,"style":96},"data = {\"a\": 1, \"b\": 2}\n\nfor key in data:\n    print(\"Current key:\", key)\n    print(\"Dictionary before change:\", data)\n    del data[key]\n",[17,67442,67443,67475,67479,67491,67511,67530],{"__ignoreMap":96},[100,67444,67445,67447,67449,67451,67453,67455,67457,67459,67461,67463,67465,67467,67469,67471,67473],{"class":102,"line":103},[100,67446,7909],{"class":106},[100,67448,111],{"class":110},[100,67450,12080],{"class":118},[100,67452,206],{"class":205},[100,67454,295],{"class":209},[100,67456,206],{"class":205},[100,67458,89],{"class":118},[100,67460,6915],{"class":122},[100,67462,126],{"class":118},[100,67464,1708],{"class":205},[100,67466,4027],{"class":209},[100,67468,206],{"class":205},[100,67470,89],{"class":118},[100,67472,129],{"class":122},[100,67474,12093],{"class":118},[100,67476,67477],{"class":102,"line":135},[100,67478,139],{"emptyLinePlaceholder":138},[100,67480,67481,67483,67485,67487,67489],{"class":102,"line":142},[100,67482,71],{"class":145},[100,67484,66693],{"class":106},[100,67486,617],{"class":145},[100,67488,12102],{"class":106},[100,67490,149],{"class":118},[100,67492,67493,67495,67497,67499,67502,67504,67506,67509],{"class":102,"line":152},[100,67494,200],{"class":114},[100,67496,170],{"class":118},[100,67498,206],{"class":205},[100,67500,67501],{"class":209},"Current key:",[100,67503,206],{"class":205},[100,67505,126],{"class":118},[100,67507,67508],{"class":178}," key",[100,67510,215],{"class":118},[100,67512,67513,67515,67517,67519,67522,67524,67526,67528],{"class":102,"line":164},[100,67514,200],{"class":114},[100,67516,170],{"class":118},[100,67518,206],{"class":205},[100,67520,67521],{"class":209},"Dictionary before change:",[100,67523,206],{"class":205},[100,67525,126],{"class":118},[100,67527,12102],{"class":178},[100,67529,215],{"class":118},[100,67531,67532,67535,67537,67539,67541],{"class":102,"line":185},[100,67533,67534],{"class":145},"    del",[100,67536,12102],{"class":106},[100,67538,3166],{"class":118},[100,67540,43824],{"class":106},[100,67542,603],{"class":118},[14,67544,67545],{},"This kind of debug output helps you see exactly what happens before the crash.",[14,67547,67548,67549,290],{},"If your problem involves deep repeated function calls, it may also be related to ",[295,67550,67552],{"href":67551},"\u002Ferrors\u002Frecursionerror-in-python-causes-and-fixes\u002F","RecursionError in Python",[77,67554,67556],{"id":67555},"when-to-raise-runtimeerror-yourself","When to raise RuntimeError yourself",[14,67558,67559,67560,67562],{},"You can raise ",[17,67561,17663],{}," in your own code when the program reaches an invalid runtime state.",[14,67564,1844],{},[91,67566,67568],{"className":93,"code":67567,"language":95,"meta":96,"style":96},"file_open = False\n\ndef write_report():\n    if not file_open:\n        raise RuntimeError(\"Cannot write report because the file is not open\")\n    print(\"Report written\")\n",[17,67569,67570,67579,67583,67592,67603,67620],{"__ignoreMap":96},[100,67571,67572,67575,67577],{"class":102,"line":103},[100,67573,67574],{"class":106},"file_open ",[100,67576,111],{"class":110},[100,67578,44952],{"class":158},[100,67580,67581],{"class":102,"line":135},[100,67582,139],{"emptyLinePlaceholder":138},[100,67584,67585,67587,67590],{"class":102,"line":142},[100,67586,1078],{"class":1077},[100,67588,67589],{"class":1081}," write_report",[100,67591,1085],{"class":118},[100,67593,67594,67596,67598,67601],{"class":102,"line":152},[100,67595,2777],{"class":145},[100,67597,4921],{"class":110},[100,67599,67600],{"class":106}," file_open",[100,67602,149],{"class":118},[100,67604,67605,67607,67609,67611,67613,67616,67618],{"class":102,"line":164},[100,67606,2791],{"class":145},[100,67608,66329],{"class":191},[100,67610,170],{"class":118},[100,67612,206],{"class":205},[100,67614,67615],{"class":209},"Cannot write report because the file is not open",[100,67617,206],{"class":205},[100,67619,215],{"class":118},[100,67621,67622,67624,67626,67628,67631,67633],{"class":102,"line":185},[100,67623,200],{"class":114},[100,67625,170],{"class":118},[100,67627,206],{"class":205},[100,67629,67630],{"class":209},"Report written",[100,67632,206],{"class":205},[100,67634,215],{"class":118},[14,67636,67637],{},"This is reasonable when:",[40,67639,67640,67643,67646],{},[43,67641,67642],{},"The problem happens at runtime",[43,67644,67645],{},"The state is invalid",[43,67647,67648],{},"A more specific built-in exception does not fit better",[14,67650,67651],{},"Still, prefer a more specific exception when possible.",[14,67653,580],{},[40,67655,67656,67661,67666],{},[43,67657,241,67658,67660],{},[17,67659,3483],{}," for a bad value",[43,67662,241,67663,67665],{},[17,67664,3538],{}," for the wrong type",[43,67667,241,67668,67670],{},[17,67669,17663],{}," for a bad runtime state",[14,67672,67673,67674,290],{},"If you are unsure about the difference between exception types, see ",[295,67675,3270],{"href":3269},[77,67677,1514],{"id":1513},[675,67679,67681],{"id":67680},"is-runtimeerror-the-same-as-syntaxerror","Is RuntimeError the same as SyntaxError?",[14,67683,14506,67684,67686,67687,67689],{},[17,67685,16503],{}," happens before the program runs. ",[17,67688,17663],{}," happens while the program is running.",[675,67691,67693],{"id":67692},"how-do-i-fix-runtimeerror-quickly","How do I fix RuntimeError quickly?",[14,67695,67696],{},"Read the traceback, find the exact failing line, and use the error message to identify the invalid runtime condition.",[675,67698,67700],{"id":67699},"should-i-catch-runtimeerror-with-try-except","Should I catch RuntimeError with try-except?",[14,67702,67703],{},"You can catch it for debugging or recovery, but you should still fix the real cause.",[675,67705,67707],{"id":67706},"can-i-raise-runtimeerror-myself","Can I raise RuntimeError myself?",[14,67709,67710,67711,67714],{},"Yes. You can use ",[17,67712,67713],{},"raise RuntimeError(\"message\")"," when your program reaches an invalid runtime state.",[77,67716,1554],{"id":1553},[40,67718,67719,67723,67727,67732,67737,67741],{},[43,67720,67721],{},[295,67722,1587],{"href":1749},[43,67724,67725],{},[295,67726,1581],{"href":2824},[43,67728,67729],{},[295,67730,67731],{"href":66364},"How to use try-except blocks in Python",[43,67733,67734],{},[295,67735,67736],{"href":67551},"RecursionError in Python: causes and fixes",[43,67738,67739],{},[295,67740,3265],{"href":3264},[43,67742,67743],{},[295,67744,3270],{"href":3269},[1589,67746,67747],{},"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 .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 .slqww, html code.shiki .slqww{--shiki-light:#6182B8;--shiki-default:#24292E;--shiki-dark:#E1E4E8}html pre.shiki code .sZMiF, html code.shiki .sZMiF{--shiki-light:#E2931D;--shiki-default:#005CC5;--shiki-dark:#79B8FF}html pre.shiki code .su5hD, html code.shiki .su5hD{--shiki-light:#90A4AE;--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 .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 .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 .srdBf, html code.shiki .srdBf{--shiki-light:#F76D47;--shiki-default:#005CC5;--shiki-dark:#79B8FF}html pre.shiki code .smGrS, html code.shiki .smGrS{--shiki-light:#39ADB5;--shiki-default:#D73A49;--shiki-dark:#F97583}html pre.shiki code .s39Yj, html code.shiki .s39Yj{--shiki-light:#39ADB5;--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 .sbgvK, html code.shiki .sbgvK{--shiki-light:#E2931D;--shiki-default:#6F42C1;--shiki-dark:#B392F0}html pre.shiki code .stzsN, html code.shiki .stzsN{--shiki-light:#91B859;--shiki-default:#005CC5;--shiki-dark:#79B8FF}",{"title":96,"searchDepth":135,"depth":135,"links":67749},[67750,67751,67752,67753,67754,67760,67761,67762,67763,67769],{"id":79,"depth":135,"text":80},{"id":66368,"depth":135,"text":66369},{"id":3943,"depth":135,"text":3944},{"id":66462,"depth":135,"text":66463},{"id":66625,"depth":135,"text":66626,"children":67755},[67756,67757,67758,67759],{"id":66629,"depth":142,"text":66630},{"id":66914,"depth":142,"text":66915},{"id":67049,"depth":142,"text":67050},{"id":67232,"depth":142,"text":67233},{"id":67280,"depth":135,"text":67281},{"id":1167,"depth":135,"text":1168},{"id":67555,"depth":135,"text":67556},{"id":1513,"depth":135,"text":1514,"children":67764},[67765,67766,67767,67768],{"id":67680,"depth":142,"text":67681},{"id":67692,"depth":142,"text":67693},{"id":67699,"depth":142,"text":67700},{"id":67706,"depth":142,"text":67707},{"id":1553,"depth":135,"text":1554},"Master runtimeerror in python causes and fixes in our comprehensive Python beginner guide.",{},{"title":66267,"description":67770},"errors\u002Fruntimeerror-in-python-causes-and-fixes","A7VT_XdPmUIBfLSM0dlqfchOTlGyKppQtLGVCXCuWaI",{"id":4,"title":5,"body":67776,"description":1622,"extension":1623,"meta":69049,"navigation":138,"path":1625,"seo":69050,"stem":1627,"__hash__":1628},{"type":7,"value":67777,"toc":69022},[67778,67780,67786,67792,67794,67810,67816,67818,67824,67900,67902,67918,67928,67930,67934,67936,67944,67952,67956,67958,67962,67984,67986,68056,68058,68060,68062,68130,68132,68162,68164,68184,68186,68192,68194,68240,68242,68254,68258,68264,68268,68270,68276,68280,68370,68372,68388,68392,68396,68476,68478,68494,68500,68504,68508,68514,68562,68564,68576,68578,68582,68584,68588,68590,68598,68604,68606,68630,68632,68634,68656,68658,68676,68678,68682,68684,68688,68708,68710,68778,68780,68800,68802,68820,68822,68824,68826,68844,68846,68848,68944,68946,68968,68970,68972,68974,68976,68978,68980,68982,68988,68990,68994,68996,69020],[10,67779,5],{"id":12},[14,67781,67782,20,67784,25],{},[17,67783,19],{},[22,67785,24],{},[14,67787,28,67788,32,67790,35],{},[22,67789,31],{},[17,67791,19],{},[14,67793,38],{},[40,67795,67796,67800,67804,67806],{},[43,67797,45,67798,49],{},[17,67799,48],{},[43,67801,52,67802,56],{},[17,67803,55],{},[43,67805,59],{},[43,67807,62,67808,65],{},[17,67809,19],{},[14,67811,68,67812,72,67814,75],{},[17,67813,71],{},[17,67815,48],{},[77,67817,80],{"id":79},[14,67819,83,67820,86,67822,89],{},[17,67821,48],{},[17,67823,19],{},[91,67825,67826],{"className":93,"code":94,"language":95,"meta":96,"style":96},[17,67827,67828,67846,67850,67856,67864,67878,67886],{"__ignoreMap":96},[100,67829,67830,67832,67834,67836,67838,67840,67842,67844],{"class":102,"line":103},[100,67831,107],{"class":106},[100,67833,111],{"class":110},[100,67835,115],{"class":114},[100,67837,119],{"class":118},[100,67839,123],{"class":122},[100,67841,126],{"class":118},[100,67843,129],{"class":122},[100,67845,132],{"class":118},[100,67847,67848],{"class":102,"line":135},[100,67849,139],{"emptyLinePlaceholder":138},[100,67851,67852,67854],{"class":102,"line":142},[100,67853,146],{"class":145},[100,67855,149],{"class":118},[100,67857,67858,67860,67862],{"class":102,"line":152},[100,67859,155],{"class":145},[100,67861,159],{"class":158},[100,67863,149],{"class":118},[100,67865,67866,67868,67870,67872,67874,67876],{"class":102,"line":164},[100,67867,167],{"class":114},[100,67869,170],{"class":118},[100,67871,173],{"class":114},[100,67873,170],{"class":118},[100,67875,179],{"class":178},[100,67877,182],{"class":118},[100,67879,67880,67882,67884],{"class":102,"line":185},[100,67881,188],{"class":145},[100,67883,192],{"class":191},[100,67885,149],{"class":118},[100,67887,67888,67890,67892,67894,67896,67898],{"class":102,"line":197},[100,67889,200],{"class":114},[100,67891,170],{"class":118},[100,67893,206],{"class":205},[100,67895,210],{"class":209},[100,67897,206],{"class":205},[100,67899,215],{"class":118},[14,67901,218],{},[91,67903,67904],{"className":93,"code":221,"language":95,"meta":96,"style":96},[17,67905,67906,67910,67914],{"__ignoreMap":96},[100,67907,67908],{"class":102,"line":103},[100,67909,228],{"class":122},[100,67911,67912],{"class":102,"line":135},[100,67913,233],{"class":122},[100,67915,67916],{"class":102,"line":142},[100,67917,238],{"class":106},[14,67919,241,67920,245,67922,248,67924,251,67926,254],{},[17,67921,244],{},[17,67923,48],{},[17,67925,71],{},[17,67927,19],{},[77,67929,258],{"id":257},[14,67931,67932,263],{},[17,67933,19],{},[14,67935,266],{},[40,67937,67938,67940,67942],{},[43,67939,271],{},[43,67941,274],{},[43,67943,277],{},[14,67945,280,67946,284,67948,287,67950,290],{},[17,67947,283],{},[17,67949,48],{},[17,67951,19],{},[14,67953,293,67954,290],{},[295,67955,298],{"href":297},[77,67957,302],{"id":301},[14,67959,305,67960,308],{},[17,67961,19],{},[40,67963,67964,67970,67974,67980],{},[43,67965,67966],{},[22,67967,315,67968,49],{},[17,67969,48],{},[43,67971,67972],{},[22,67973,322],{},[43,67975,67976],{},[22,67977,327,67978,330],{},[17,67979,19],{},[43,67981,67982],{},[22,67983,335],{},[14,67985,338],{},[91,67987,67988],{"className":93,"code":341,"language":95,"meta":96,"style":96},[17,67989,67990,68008,68012,68026,68040],{"__ignoreMap":96},[100,67991,67992,67994,67996,67998,68000,68002,68004,68006],{"class":102,"line":103},[100,67993,107],{"class":106},[100,67995,111],{"class":110},[100,67997,115],{"class":114},[100,67999,119],{"class":118},[100,68001,356],{"class":122},[100,68003,126],{"class":118},[100,68005,361],{"class":122},[100,68007,132],{"class":118},[100,68009,68010],{"class":102,"line":135},[100,68011,139],{"emptyLinePlaceholder":138},[100,68013,68014,68016,68018,68020,68022,68024],{"class":102,"line":142},[100,68015,372],{"class":114},[100,68017,170],{"class":118},[100,68019,173],{"class":114},[100,68021,170],{"class":118},[100,68023,179],{"class":178},[100,68025,182],{"class":118},[100,68027,68028,68030,68032,68034,68036,68038],{"class":102,"line":152},[100,68029,372],{"class":114},[100,68031,170],{"class":118},[100,68033,173],{"class":114},[100,68035,170],{"class":118},[100,68037,179],{"class":178},[100,68039,182],{"class":118},[100,68041,68042,68044,68046,68048,68050,68052,68054],{"class":102,"line":164},[100,68043,372],{"class":114},[100,68045,170],{"class":118},[100,68047,173],{"class":114},[100,68049,170],{"class":118},[100,68051,179],{"class":178},[100,68053,411],{"class":118},[100,68055,415],{"class":414},[14,68057,418],{},[77,68059,422],{"id":421},[14,68061,425],{},[91,68063,68064],{"className":93,"code":428,"language":95,"meta":96,"style":96},[17,68065,68066,68084,68088,68102,68116],{"__ignoreMap":96},[100,68067,68068,68070,68072,68074,68076,68078,68080,68082],{"class":102,"line":103},[100,68069,107],{"class":106},[100,68071,111],{"class":110},[100,68073,115],{"class":114},[100,68075,119],{"class":118},[100,68077,123],{"class":122},[100,68079,126],{"class":118},[100,68081,129],{"class":122},[100,68083,132],{"class":118},[100,68085,68086],{"class":102,"line":135},[100,68087,139],{"emptyLinePlaceholder":138},[100,68089,68090,68092,68094,68096,68098,68100],{"class":102,"line":142},[100,68091,372],{"class":114},[100,68093,170],{"class":118},[100,68095,173],{"class":114},[100,68097,170],{"class":118},[100,68099,179],{"class":178},[100,68101,182],{"class":118},[100,68103,68104,68106,68108,68110,68112,68114],{"class":102,"line":152},[100,68105,372],{"class":114},[100,68107,170],{"class":118},[100,68109,173],{"class":114},[100,68111,170],{"class":118},[100,68113,179],{"class":178},[100,68115,182],{"class":118},[100,68117,68118,68120,68122,68124,68126,68128],{"class":102,"line":164},[100,68119,372],{"class":114},[100,68121,170],{"class":118},[100,68123,173],{"class":114},[100,68125,170],{"class":118},[100,68127,179],{"class":178},[100,68129,182],{"class":118},[14,68131,218],{},[91,68133,68134],{"className":93,"code":499,"language":95,"meta":96,"style":96},[17,68135,68136,68140,68144,68154,68158],{"__ignoreMap":96},[100,68137,68138],{"class":102,"line":103},[100,68139,228],{"class":122},[100,68141,68142],{"class":102,"line":135},[100,68143,233],{"class":122},[100,68145,68146,68148,68150,68152],{"class":102,"line":142},[100,68147,514],{"class":178},[100,68149,170],{"class":118},[100,68151,519],{"class":178},[100,68153,522],{"class":118},[100,68155,68156],{"class":102,"line":152},[100,68157,528],{"class":527},[100,68159,68160],{"class":102,"line":164},[100,68161,533],{"class":191},[14,68163,536],{},[40,68165,68166,68170,68176,68180],{},[43,68167,68168,544],{},[17,68169,543],{},[43,68171,547,68172,551,68174],{},[17,68173,550],{},[17,68175,123],{},[43,68177,556,68178],{},[17,68179,559],{},[43,68181,562,68182],{},[17,68183,19],{},[77,68185,568],{"id":567},[14,68187,571,68188,574,68190,577],{},[17,68189,71],{},[17,68191,48],{},[14,68193,580],{},[91,68195,68196],{"className":93,"code":583,"language":95,"meta":96,"style":96},[17,68197,68198,68214,68218,68230],{"__ignoreMap":96},[100,68199,68200,68202,68204,68206,68208,68210,68212],{"class":102,"line":103},[100,68201,107],{"class":106},[100,68203,111],{"class":110},[100,68205,594],{"class":118},[100,68207,123],{"class":122},[100,68209,126],{"class":118},[100,68211,129],{"class":122},[100,68213,603],{"class":118},[100,68215,68216],{"class":102,"line":135},[100,68217,139],{"emptyLinePlaceholder":138},[100,68219,68220,68222,68224,68226,68228],{"class":102,"line":142},[100,68221,71],{"class":145},[100,68223,614],{"class":106},[100,68225,617],{"class":145},[100,68227,620],{"class":106},[100,68229,149],{"class":118},[100,68231,68232,68234,68236,68238],{"class":102,"line":152},[100,68233,200],{"class":114},[100,68235,170],{"class":118},[100,68237,631],{"class":178},[100,68239,215],{"class":118},[14,68241,218],{},[91,68243,68244],{"className":93,"code":638,"language":95,"meta":96,"style":96},[17,68245,68246,68250],{"__ignoreMap":96},[100,68247,68248],{"class":102,"line":103},[100,68249,228],{"class":122},[100,68251,68252],{"class":102,"line":135},[100,68253,233],{"class":122},[14,68255,651,68256,654],{},[17,68257,19],{},[14,68259,657,68260,660,68262,663],{},[17,68261,71],{},[17,68263,48],{},[14,68265,666,68266,669],{},[17,68267,71],{},[77,68269,673],{"id":672},[675,68271,678,68272,681,68274],{"id":677},[17,68273,48],{},[17,68275,244],{},[14,68277,686,68278,689],{},[17,68279,48],{},[91,68281,68282],{"className":93,"code":692,"language":95,"meta":96,"style":96},[17,68283,68284,68302,68306,68314,68320,68334,68344,68352,68366],{"__ignoreMap":96},[100,68285,68286,68288,68290,68292,68294,68296,68298,68300],{"class":102,"line":103},[100,68287,107],{"class":106},[100,68289,111],{"class":110},[100,68291,115],{"class":114},[100,68293,119],{"class":118},[100,68295,123],{"class":122},[100,68297,126],{"class":118},[100,68299,129],{"class":122},[100,68301,132],{"class":118},[100,68303,68304],{"class":102,"line":135},[100,68305,139],{"emptyLinePlaceholder":138},[100,68307,68308,68310,68312],{"class":102,"line":142},[100,68309,721],{"class":145},[100,68311,159],{"class":158},[100,68313,149],{"class":118},[100,68315,68316,68318],{"class":102,"line":152},[100,68317,730],{"class":145},[100,68319,149],{"class":118},[100,68321,68322,68324,68326,68328,68330,68332],{"class":102,"line":164},[100,68323,737],{"class":106},[100,68325,111],{"class":110},[100,68327,742],{"class":114},[100,68329,170],{"class":118},[100,68331,179],{"class":178},[100,68333,215],{"class":118},[100,68335,68336,68338,68340,68342],{"class":102,"line":185},[100,68337,167],{"class":114},[100,68339,170],{"class":118},[100,68341,757],{"class":178},[100,68343,215],{"class":118},[100,68345,68346,68348,68350],{"class":102,"line":197},[100,68347,764],{"class":145},[100,68349,192],{"class":191},[100,68351,149],{"class":118},[100,68353,68354,68356,68358,68360,68362,68364],{"class":102,"line":771},[100,68355,167],{"class":114},[100,68357,170],{"class":118},[100,68359,206],{"class":205},[100,68361,780],{"class":209},[100,68363,206],{"class":205},[100,68365,215],{"class":118},[100,68367,68368],{"class":102,"line":787},[100,68369,790],{"class":145},[14,68371,218],{},[91,68373,68374],{"className":93,"code":795,"language":95,"meta":96,"style":96},[17,68375,68376,68380,68384],{"__ignoreMap":96},[100,68377,68378],{"class":102,"line":103},[100,68379,228],{"class":122},[100,68381,68382],{"class":102,"line":135},[100,68383,233],{"class":122},[100,68385,68386],{"class":102,"line":142},[100,68387,810],{"class":106},[675,68389,814,68390],{"id":813},[17,68391,48],{},[14,68393,819,68394,822],{},[17,68395,48],{},[91,68397,68398],{"className":93,"code":825,"language":95,"meta":96,"style":96},[17,68399,68400,68418,68422,68440,68458],{"__ignoreMap":96},[100,68401,68402,68404,68406,68408,68410,68412,68414,68416],{"class":102,"line":103},[100,68403,107],{"class":106},[100,68405,111],{"class":110},[100,68407,115],{"class":114},[100,68409,119],{"class":118},[100,68411,123],{"class":122},[100,68413,126],{"class":118},[100,68415,129],{"class":122},[100,68417,132],{"class":118},[100,68419,68420],{"class":102,"line":135},[100,68421,139],{"emptyLinePlaceholder":138},[100,68423,68424,68426,68428,68430,68432,68434,68436,68438],{"class":102,"line":142},[100,68425,372],{"class":114},[100,68427,170],{"class":118},[100,68429,173],{"class":114},[100,68431,170],{"class":118},[100,68433,179],{"class":178},[100,68435,126],{"class":118},[100,68437,866],{"class":158},[100,68439,182],{"class":118},[100,68441,68442,68444,68446,68448,68450,68452,68454,68456],{"class":102,"line":152},[100,68443,372],{"class":114},[100,68445,170],{"class":118},[100,68447,173],{"class":114},[100,68449,170],{"class":118},[100,68451,179],{"class":178},[100,68453,126],{"class":118},[100,68455,866],{"class":158},[100,68457,182],{"class":118},[100,68459,68460,68462,68464,68466,68468,68470,68472,68474],{"class":102,"line":164},[100,68461,372],{"class":114},[100,68463,170],{"class":118},[100,68465,173],{"class":114},[100,68467,170],{"class":118},[100,68469,179],{"class":178},[100,68471,126],{"class":118},[100,68473,866],{"class":158},[100,68475,182],{"class":118},[14,68477,218],{},[91,68479,68480],{"className":93,"code":909,"language":95,"meta":96,"style":96},[17,68481,68482,68486,68490],{"__ignoreMap":96},[100,68483,68484],{"class":102,"line":103},[100,68485,228],{"class":122},[100,68487,68488],{"class":102,"line":135},[100,68489,233],{"class":122},[100,68491,68492],{"class":102,"line":142},[100,68493,924],{"class":158},[14,68495,927,68496,931,68498,290],{},[17,68497,930],{},[17,68499,19],{},[14,68501,936,68502,939],{},[17,68503,930],{},[675,68505,943,68506,946],{"id":942},[17,68507,48],{},[14,68509,949,68510,952,68512,955],{},[17,68511,71],{},[17,68513,48],{},[91,68515,68516],{"className":93,"code":958,"language":95,"meta":96,"style":96},[17,68517,68518,68536,68540,68552],{"__ignoreMap":96},[100,68519,68520,68522,68524,68526,68528,68530,68532,68534],{"class":102,"line":103},[100,68521,107],{"class":106},[100,68523,111],{"class":110},[100,68525,115],{"class":114},[100,68527,119],{"class":118},[100,68529,123],{"class":122},[100,68531,126],{"class":118},[100,68533,129],{"class":122},[100,68535,132],{"class":118},[100,68537,68538],{"class":102,"line":135},[100,68539,139],{"emptyLinePlaceholder":138},[100,68541,68542,68544,68546,68548,68550],{"class":102,"line":142},[100,68543,71],{"class":145},[100,68545,989],{"class":106},[100,68547,617],{"class":145},[100,68549,620],{"class":106},[100,68551,149],{"class":118},[100,68553,68554,68556,68558,68560],{"class":102,"line":152},[100,68555,200],{"class":114},[100,68557,170],{"class":118},[100,68559,757],{"class":178},[100,68561,215],{"class":118},[14,68563,218],{},[91,68565,68566],{"className":93,"code":638,"language":95,"meta":96,"style":96},[17,68567,68568,68572],{"__ignoreMap":96},[100,68569,68570],{"class":102,"line":103},[100,68571,228],{"class":122},[100,68573,68574],{"class":102,"line":135},[100,68575,233],{"class":122},[14,68577,1022],{},[14,68579,1025,68580,290],{},[295,68581,1029],{"href":1028},[77,68583,1033],{"id":1032},[14,68585,571,68586,1039],{},[22,68587,1038],{},[14,68589,1042],{},[40,68591,68592,68594],{},[43,68593,1047],{},[43,68595,1050,68596],{},[17,68597,1053],{},[14,68599,1056,68600,1060,68602,1063],{},[22,68601,1059],{},[17,68603,19],{},[675,68605,1067],{"id":1066},[91,68607,68608],{"className":93,"code":1070,"language":95,"meta":96,"style":96},[17,68609,68610,68618,68624],{"__ignoreMap":96},[100,68611,68612,68614,68616],{"class":102,"line":103},[100,68613,1078],{"class":1077},[100,68615,1082],{"class":1081},[100,68617,1085],{"class":118},[100,68619,68620,68622],{"class":102,"line":135},[100,68621,1090],{"class":145},[100,68623,1093],{"class":122},[100,68625,68626,68628],{"class":102,"line":142},[100,68627,1098],{"class":145},[100,68629,1101],{"class":191},[14,68631,1104],{},[675,68633,1108],{"id":1107},[91,68635,68636],{"className":93,"code":1111,"language":95,"meta":96,"style":96},[17,68637,68638,68646,68652],{"__ignoreMap":96},[100,68639,68640,68642,68644],{"class":102,"line":103},[100,68641,1078],{"class":1077},[100,68643,1082],{"class":1081},[100,68645,1085],{"class":118},[100,68647,68648,68650],{"class":102,"line":135},[100,68649,1090],{"class":145},[100,68651,1093],{"class":122},[100,68653,68654],{"class":102,"line":142},[100,68655,1132],{"class":145},[14,68657,1135],{},[91,68659,68660],{"className":93,"code":1138,"language":95,"meta":96,"style":96},[17,68661,68662,68670],{"__ignoreMap":96},[100,68663,68664,68666,68668],{"class":102,"line":103},[100,68665,1078],{"class":1077},[100,68667,1082],{"class":1081},[100,68669,1085],{"class":118},[100,68671,68672,68674],{"class":102,"line":135},[100,68673,1090],{"class":145},[100,68675,1093],{"class":122},[14,68677,1157],{},[14,68679,1160,68680,290],{},[295,68681,1164],{"href":1163},[77,68683,1168],{"id":1167},[14,68685,1171,68686,1174],{},[17,68687,19],{},[40,68689,68690,68694,68696,68698,68704],{},[43,68691,1179,68692,1182],{},[17,68693,48],{},[43,68695,1185],{},[43,68697,1188],{},[43,68699,1191,68700,1194,68702,1197],{},[17,68701,71],{},[17,68703,48],{},[43,68705,1200,68706],{},[17,68707,1203],{},[14,68709,1206],{},[91,68711,68712],{"className":93,"code":1209,"language":95,"meta":96,"style":96},[17,68713,68714,68728,68742,68756],{"__ignoreMap":96},[100,68715,68716,68718,68720,68722,68724,68726],{"class":102,"line":103},[100,68717,372],{"class":114},[100,68719,170],{"class":118},[100,68721,173],{"class":114},[100,68723,170],{"class":118},[100,68725,1224],{"class":178},[100,68727,182],{"class":118},[100,68729,68730,68732,68734,68736,68738,68740],{"class":102,"line":135},[100,68731,372],{"class":114},[100,68733,170],{"class":118},[100,68735,1235],{"class":191},[100,68737,170],{"class":118},[100,68739,1224],{"class":178},[100,68741,182],{"class":118},[100,68743,68744,68746,68748,68750,68752,68754],{"class":102,"line":142},[100,68745,372],{"class":114},[100,68747,170],{"class":118},[100,68749,1250],{"class":191},[100,68751,170],{"class":118},[100,68753,1224],{"class":178},[100,68755,182],{"class":118},[100,68757,68758,68760,68762,68764,68766,68768,68770,68772,68774,68776],{"class":102,"line":152},[100,68759,372],{"class":114},[100,68761,170],{"class":118},[100,68763,1265],{"class":114},[100,68765,170],{"class":118},[100,68767,1224],{"class":178},[100,68769,126],{"class":118},[100,68771,1274],{"class":205},[100,68773,1277],{"class":209},[100,68775,1280],{"class":205},[100,68777,182],{"class":118},[14,68779,1285],{},[40,68781,68782,68786,68790,68796],{},[43,68783,68784,1293],{},[17,68785,1292],{},[43,68787,68788,1299],{},[17,68789,1298],{},[43,68791,68792,1305,68794,1309],{},[17,68793,1304],{},[22,68795,1308],{},[43,68797,68798,1315],{},[17,68799,1314],{},[14,68801,1318],{},[91,68803,68804],{"className":93,"code":1321,"language":95,"meta":96,"style":96},[17,68805,68806],{"__ignoreMap":96},[100,68807,68808,68810,68812,68814,68816,68818],{"class":102,"line":103},[100,68809,372],{"class":114},[100,68811,170],{"class":118},[100,68813,1235],{"class":191},[100,68815,170],{"class":118},[100,68817,1224],{"class":178},[100,68819,182],{"class":118},[14,68821,1340],{},[77,68823,1344],{"id":1343},[14,68825,1347],{},[40,68827,68828,68832,68838,68842],{},[43,68829,315,68830,1354],{},[17,68831,48],{},[43,68833,1357,68834,1360,68836,1363],{},[17,68835,48],{},[17,68837,55],{},[43,68839,1366,68840,1369],{},[17,68841,19],{},[43,68843,1372],{},[675,68845,1376],{"id":1375},[14,68847,1379],{},[91,68849,68850],{"className":93,"code":1382,"language":95,"meta":96,"style":96},[17,68851,68852,68874,68878,68890,68900,68904,68918,68922,68934],{"__ignoreMap":96},[100,68853,68854,68856,68858,68860,68862,68864,68866,68868,68870,68872],{"class":102,"line":103},[100,68855,107],{"class":106},[100,68857,111],{"class":110},[100,68859,115],{"class":114},[100,68861,119],{"class":118},[100,68863,123],{"class":122},[100,68865,126],{"class":118},[100,68867,129],{"class":122},[100,68869,126],{"class":118},[100,68871,1405],{"class":122},[100,68873,132],{"class":118},[100,68875,68876],{"class":102,"line":135},[100,68877,139],{"emptyLinePlaceholder":138},[100,68879,68880,68882,68884,68886,68888],{"class":102,"line":142},[100,68881,71],{"class":145},[100,68883,1418],{"class":106},[100,68885,617],{"class":145},[100,68887,620],{"class":106},[100,68889,149],{"class":118},[100,68891,68892,68894,68896,68898],{"class":102,"line":152},[100,68893,200],{"class":114},[100,68895,170],{"class":118},[100,68897,1433],{"class":178},[100,68899,215],{"class":118},[100,68901,68902],{"class":102,"line":164},[100,68903,139],{"emptyLinePlaceholder":138},[100,68905,68906,68908,68910,68912,68914,68916],{"class":102,"line":185},[100,68907,372],{"class":114},[100,68909,170],{"class":118},[100,68911,206],{"class":205},[100,68913,1450],{"class":209},[100,68915,206],{"class":205},[100,68917,215],{"class":118},[100,68919,68920],{"class":102,"line":197},[100,68921,139],{"emptyLinePlaceholder":138},[100,68923,68924,68926,68928,68930,68932],{"class":102,"line":771},[100,68925,71],{"class":145},[100,68927,1418],{"class":106},[100,68929,617],{"class":145},[100,68931,620],{"class":106},[100,68933,149],{"class":118},[100,68935,68936,68938,68940,68942],{"class":102,"line":787},[100,68937,200],{"class":114},[100,68939,170],{"class":118},[100,68941,1433],{"class":178},[100,68943,215],{"class":118},[14,68945,218],{},[91,68947,68948],{"className":93,"code":1485,"language":95,"meta":96,"style":96},[17,68949,68950,68954,68958,68962],{"__ignoreMap":96},[100,68951,68952],{"class":102,"line":103},[100,68953,228],{"class":122},[100,68955,68956],{"class":102,"line":135},[100,68957,233],{"class":122},[100,68959,68960],{"class":102,"line":142},[100,68961,1500],{"class":122},[100,68963,68964,68966],{"class":102,"line":152},[100,68965,1505],{"class":106},[100,68967,149],{"class":118},[14,68969,1510],{},[77,68971,1514],{"id":1513},[675,68973,1518],{"id":1517},[14,68975,1521],{},[675,68977,1525],{"id":1524},[14,68979,1528],{},[675,68981,1532],{"id":1531},[14,68983,241,68984,1537,68986,290],{},[17,68985,244],{},[17,68987,1540],{},[675,68989,1544],{"id":1543},[14,68991,1547,68992,1550],{},[17,68993,1053],{},[77,68995,1554],{"id":1553},[40,68997,68998,69002,69006,69012,69016],{},[43,68999,69000],{},[295,69001,1561],{"href":297},[43,69003,69004],{},[295,69005,1566],{"href":1163},[43,69007,69008],{},[295,69009,1572,69010,1576],{"href":1571},[17,69011,1575],{},[43,69013,69014],{},[295,69015,1581],{"href":1028},[43,69017,69018],{},[295,69019,1587],{"href":1586},[1589,69021,1591],{},{"title":96,"searchDepth":135,"depth":135,"links":69023},[69024,69025,69026,69027,69028,69029,69034,69038,69039,69042,69048],{"id":79,"depth":135,"text":80},{"id":257,"depth":135,"text":258},{"id":301,"depth":135,"text":302},{"id":421,"depth":135,"text":422},{"id":567,"depth":135,"text":568},{"id":672,"depth":135,"text":673,"children":69030},[69031,69032,69033],{"id":677,"depth":142,"text":1602},{"id":813,"depth":142,"text":1604},{"id":942,"depth":142,"text":1606},{"id":1032,"depth":135,"text":1033,"children":69035},[69036,69037],{"id":1066,"depth":142,"text":1067},{"id":1107,"depth":142,"text":1108},{"id":1167,"depth":135,"text":1168},{"id":1343,"depth":135,"text":1344,"children":69040},[69041],{"id":1375,"depth":142,"text":1376},{"id":1513,"depth":135,"text":1514,"children":69043},[69044,69045,69046,69047],{"id":1517,"depth":142,"text":1518},{"id":1524,"depth":142,"text":1525},{"id":1531,"depth":142,"text":1532},{"id":1543,"depth":142,"text":1544},{"id":1553,"depth":135,"text":1554},{},{"title":5,"description":1622},{"id":69052,"title":69053,"body":69054,"description":70055,"extension":1623,"meta":70056,"navigation":138,"path":70057,"seo":70058,"stem":70059,"__hash__":70060},"content\u002Ferrors\u002Fsyntaxerror-eol-while-scanning-string-literal-fix.md","SyntaxError: EOL while scanning string literal (Fix)",{"type":7,"value":69055,"toc":70031},[69056,69059,69062,69065,69067,69121,69124,69126,69137,69143,69145,69148,69173,69176,69228,69234,69237,69239,69243,69258,69260,69277,69281,69296,69298,69315,69319,69322,69338,69341,69357,69360,69376,69380,69382,69405,69410,69413,69431,69434,69456,69460,69483,69489,69492,69516,69519,69537,69541,69544,69565,69568,69591,69594,69620,69627,69629,69632,69649,69652,69777,69779,69781,69798,69801,69811,69814,69828,69833,69835,69838,69867,69869,69872,69892,69902,69904,69908,69911,69915,69918,69922,69925,69927,69945,69947,69969,69973,69976,69978,70001,70003,70028],[10,69057,69053],{"id":69058},"syntaxerror-eol-while-scanning-string-literal-fix",[14,69060,69061],{},"This error means Python found the start of a string, but never found the end of it.",[14,69063,69064],{},"In simple terms, you opened a string with a quote, but the closing quote is missing or incorrect. Because this is a syntax error, your code will not run until you fix it.",[77,69066,80],{"id":79},[91,69068,69070],{"className":93,"code":69069,"language":95,"meta":96,"style":96},"name = \"Alice\"\nprint(name)\n\nmessage = 'Hello world'\nprint(message)\n",[17,69071,69072,69084,69094,69098,69111],{"__ignoreMap":96},[100,69073,69074,69076,69078,69080,69082],{"class":102,"line":103},[100,69075,1691],{"class":106},[100,69077,111],{"class":110},[100,69079,1708],{"class":205},[100,69081,15343],{"class":209},[100,69083,1714],{"class":205},[100,69085,69086,69088,69090,69092],{"class":102,"line":135},[100,69087,372],{"class":114},[100,69089,170],{"class":118},[100,69091,2853],{"class":178},[100,69093,215],{"class":118},[100,69095,69096],{"class":102,"line":142},[100,69097,139],{"emptyLinePlaceholder":138},[100,69099,69100,69102,69104,69106,69109],{"class":102,"line":152},[100,69101,6868],{"class":106},[100,69103,111],{"class":110},[100,69105,1274],{"class":205},[100,69107,69108],{"class":209},"Hello world",[100,69110,3925],{"class":205},[100,69112,69113,69115,69117,69119],{"class":102,"line":164},[100,69114,372],{"class":114},[100,69116,170],{"class":118},[100,69118,1836],{"class":178},[100,69120,215],{"class":118},[14,69122,69123],{},"This error usually happens when a string starts with a quote but does not end with the matching quote on the same line.",[77,69125,5881],{"id":5880},[40,69127,69128,69131,69134],{},[43,69129,69130],{},"Python reached the end of the line before the string was finished.",[43,69132,69133],{},"A string started with a quote, but Python could not find the closing quote.",[43,69135,69136],{},"This is a syntax error, so the code will not run at all until it is fixed.",[14,69138,69139,69140,290],{},"If you are new to strings, see ",[295,69141,69142],{"href":14879},"Python strings explained with basics and examples",[77,69144,11031],{"id":11030},[14,69146,69147],{},"Here is a simple example with a missing closing quote:",[91,69149,69151],{"className":93,"code":69150,"language":95,"meta":96,"style":96},"name = \"Alice\nprint(name)\n",[17,69152,69153,69163],{"__ignoreMap":96},[100,69154,69155,69157,69159,69161],{"class":102,"line":103},[100,69156,1691],{"class":106},[100,69158,111],{"class":110},[100,69160,1708],{"class":205},[100,69162,11279],{"class":209},[100,69164,69165,69167,69169,69171],{"class":102,"line":135},[100,69166,372],{"class":114},[100,69168,170],{"class":118},[100,69170,2853],{"class":178},[100,69172,215],{"class":118},[14,69174,69175],{},"You will usually see an error like this:",[91,69177,69179],{"className":93,"code":69178,"language":95,"meta":96,"style":96},"  File \"test.py\", line 1\n    name = \"Alice\n                 ^\nSyntaxError: EOL while scanning string literal\n",[17,69180,69181,69198,69208,69213],{"__ignoreMap":96},[100,69182,69183,69185,69187,69190,69192,69194,69196],{"class":102,"line":103},[100,69184,1952],{"class":106},[100,69186,206],{"class":205},[100,69188,69189],{"class":209},"test.py",[100,69191,206],{"class":205},[100,69193,126],{"class":118},[100,69195,1964],{"class":106},[100,69197,228],{"class":122},[100,69199,69200,69202,69204,69206],{"class":102,"line":135},[100,69201,29361],{"class":106},[100,69203,111],{"class":110},[100,69205,1708],{"class":205},[100,69207,11279],{"class":209},[100,69209,69210],{"class":102,"line":142},[100,69211,69212],{"class":110},"                 ^\n",[100,69214,69215,69217,69219,69222,69225],{"class":102,"line":152},[100,69216,16503],{"class":191},[100,69218,89],{"class":118},[100,69220,69221],{"class":527}," EOL",[100,69223,69224],{"class":145}," while",[100,69226,69227],{"class":106}," scanning string literal\n",[14,69229,69230,69231,69233],{},"Python expected the string to end with another ",[17,69232,206],{}," on the same line.",[14,69235,69236],{},"Sometimes the error points to the next line instead. That happens because Python still thinks the string is open.",[77,69238,1344],{"id":1343},[675,69240,69242],{"id":69241},"missing-closing-single-quote","Missing closing single quote",[91,69244,69246],{"className":93,"code":69245,"language":95,"meta":96,"style":96},"message = 'Hello\n",[17,69247,69248],{"__ignoreMap":96},[100,69249,69250,69252,69254,69256],{"class":102,"line":103},[100,69251,6868],{"class":106},[100,69253,111],{"class":110},[100,69255,1274],{"class":205},[100,69257,29173],{"class":209},[14,69259,15034],{},[91,69261,69263],{"className":93,"code":69262,"language":95,"meta":96,"style":96},"message = 'Hello'\n",[17,69264,69265],{"__ignoreMap":96},[100,69266,69267,69269,69271,69273,69275],{"class":102,"line":103},[100,69268,6868],{"class":106},[100,69270,111],{"class":110},[100,69272,1274],{"class":205},[100,69274,7683],{"class":209},[100,69276,3925],{"class":205},[675,69278,69280],{"id":69279},"missing-closing-double-quote","Missing closing double quote",[91,69282,69284],{"className":93,"code":69283,"language":95,"meta":96,"style":96},"message = \"Hello\n",[17,69285,69286],{"__ignoreMap":96},[100,69287,69288,69290,69292,69294],{"class":102,"line":103},[100,69289,6868],{"class":106},[100,69291,111],{"class":110},[100,69293,1708],{"class":205},[100,69295,29173],{"class":209},[14,69297,15034],{},[91,69299,69301],{"className":93,"code":69300,"language":95,"meta":96,"style":96},"message = \"Hello\"\n",[17,69302,69303],{"__ignoreMap":96},[100,69304,69305,69307,69309,69311,69313],{"class":102,"line":103},[100,69306,6868],{"class":106},[100,69308,111],{"class":110},[100,69310,1708],{"class":205},[100,69312,7683],{"class":209},[100,69314,1714],{"class":205},[675,69316,69318],{"id":69317},"opening-and-closing-quotes-do-not-match","Opening and closing quotes do not match",[14,69320,69321],{},"This starts with a single quote and ends with a double quote:",[91,69323,69325],{"className":93,"code":69324,"language":95,"meta":96,"style":96},"message = 'Hello\"\n",[17,69326,69327],{"__ignoreMap":96},[100,69328,69329,69331,69333,69335],{"class":102,"line":103},[100,69330,6868],{"class":106},[100,69332,111],{"class":110},[100,69334,1274],{"class":205},[100,69336,69337],{"class":209},"Hello\"\n",[14,69339,69340],{},"Fix it by using matching quotes:",[91,69342,69343],{"className":93,"code":69262,"language":95,"meta":96,"style":96},[17,69344,69345],{"__ignoreMap":96},[100,69346,69347,69349,69351,69353,69355],{"class":102,"line":103},[100,69348,6868],{"class":106},[100,69350,111],{"class":110},[100,69352,1274],{"class":205},[100,69354,7683],{"class":209},[100,69356,3925],{"class":205},[14,69358,69359],{},"or:",[91,69361,69362],{"className":93,"code":69300,"language":95,"meta":96,"style":96},[17,69363,69364],{"__ignoreMap":96},[100,69365,69366,69368,69370,69372,69374],{"class":102,"line":103},[100,69367,6868],{"class":106},[100,69369,111],{"class":110},[100,69371,1708],{"class":205},[100,69373,7683],{"class":209},[100,69375,1714],{"class":205},[675,69377,69379],{"id":69378},"apostrophe-inside-a-single-quoted-string","Apostrophe inside a single-quoted string",[14,69381,33829],{},[91,69383,69385],{"className":93,"code":69384,"language":95,"meta":96,"style":96},"text = 'don't do that'\n",[17,69386,69387],{"__ignoreMap":96},[100,69388,69389,69391,69393,69395,69398,69400,69403],{"class":102,"line":103},[100,69390,4331],{"class":106},[100,69392,111],{"class":110},[100,69394,1274],{"class":205},[100,69396,69397],{"class":209},"don",[100,69399,1280],{"class":205},[100,69401,69402],{"class":106},"t do that",[100,69404,3925],{"class":205},[14,69406,69407,69408,290],{},"Python thinks the string ends after ",[17,69409,69397],{},[14,69411,69412],{},"Fix it by using double quotes:",[91,69414,69416],{"className":93,"code":69415,"language":95,"meta":96,"style":96},"text = \"don't do that\"\n",[17,69417,69418],{"__ignoreMap":96},[100,69419,69420,69422,69424,69426,69429],{"class":102,"line":103},[100,69421,4331],{"class":106},[100,69423,111],{"class":110},[100,69425,1708],{"class":205},[100,69427,69428],{"class":209},"don't do that",[100,69430,1714],{"class":205},[14,69432,69433],{},"You can also escape the apostrophe:",[91,69435,69437],{"className":93,"code":69436,"language":95,"meta":96,"style":96},"text = 'don\\'t do that'\n",[17,69438,69439],{"__ignoreMap":96},[100,69440,69441,69443,69445,69447,69449,69452,69454],{"class":102,"line":103},[100,69442,4331],{"class":106},[100,69444,111],{"class":110},[100,69446,1274],{"class":205},[100,69448,69397],{"class":209},[100,69450,69451],{"class":527},"\\'",[100,69453,69402],{"class":209},[100,69455,3925],{"class":205},[675,69457,69459],{"id":69458},"quote-inside-the-string-is-not-escaped","Quote inside the string is not escaped",[91,69461,69463],{"className":93,"code":69462,"language":95,"meta":96,"style":96},"text = \"She said \"hello\"\"\n",[17,69464,69465],{"__ignoreMap":96},[100,69466,69467,69469,69471,69473,69476,69478,69480],{"class":102,"line":103},[100,69468,4331],{"class":106},[100,69470,111],{"class":110},[100,69472,1708],{"class":205},[100,69474,69475],{"class":209},"She said ",[100,69477,206],{"class":205},[100,69479,3506],{"class":106},[100,69481,69482],{"class":205},"\"\"\n",[14,69484,69485,69486,69488],{},"Python sees the second ",[17,69487,206],{}," as the end of the string.",[14,69490,69491],{},"Fix it by escaping the inner quotes:",[91,69493,69495],{"className":93,"code":69494,"language":95,"meta":96,"style":96},"text = \"She said \\\"hello\\\"\"\n",[17,69496,69497],{"__ignoreMap":96},[100,69498,69499,69501,69503,69505,69507,69510,69512,69514],{"class":102,"line":103},[100,69500,4331],{"class":106},[100,69502,111],{"class":110},[100,69504,1708],{"class":205},[100,69506,69475],{"class":209},[100,69508,69509],{"class":527},"\\\"",[100,69511,3506],{"class":209},[100,69513,69509],{"class":527},[100,69515,1714],{"class":205},[14,69517,69518],{},"Or use single quotes around the whole string:",[91,69520,69522],{"className":93,"code":69521,"language":95,"meta":96,"style":96},"text = 'She said \"hello\"'\n",[17,69523,69524],{"__ignoreMap":96},[100,69525,69526,69528,69530,69532,69535],{"class":102,"line":103},[100,69527,4331],{"class":106},[100,69529,111],{"class":110},[100,69531,1274],{"class":205},[100,69533,69534],{"class":209},"She said \"hello\"",[100,69536,3925],{"class":205},[675,69538,69540],{"id":69539},"breaking-a-normal-string-across-lines","Breaking a normal string across lines",[14,69542,69543],{},"This does not work:",[91,69545,69547],{"className":93,"code":69546,"language":95,"meta":96,"style":96},"text = \"Hello\nworld\"\n",[17,69548,69549,69559],{"__ignoreMap":96},[100,69550,69551,69553,69555,69557],{"class":102,"line":103},[100,69552,4331],{"class":106},[100,69554,111],{"class":110},[100,69556,1708],{"class":205},[100,69558,29173],{"class":209},[100,69560,69561,69563],{"class":102,"line":135},[100,69562,8535],{"class":106},[100,69564,1714],{"class":205},[14,69566,69567],{},"Use triple quotes for a multi-line string:",[91,69569,69571],{"className":93,"code":69570,"language":95,"meta":96,"style":96},"text = \"\"\"Hello\nworld\"\"\"\n",[17,69572,69573,69584],{"__ignoreMap":96},[100,69574,69575,69577,69579,69582],{"class":102,"line":103},[100,69576,4331],{"class":106},[100,69578,111],{"class":110},[100,69580,69581],{"class":205}," \"\"\"",[100,69583,29173],{"class":209},[100,69585,69586,69588],{"class":102,"line":135},[100,69587,8535],{"class":209},[100,69589,69590],{"class":205},"\"\"\"\n",[14,69592,69593],{},"Or join separate strings correctly:",[91,69595,69597],{"className":93,"code":69596,"language":95,"meta":96,"style":96},"text = \"Hello \" + \"world\"\n",[17,69598,69599],{"__ignoreMap":96},[100,69600,69601,69603,69605,69607,69610,69612,69614,69616,69618],{"class":102,"line":103},[100,69602,4331],{"class":106},[100,69604,111],{"class":110},[100,69606,1708],{"class":205},[100,69608,69609],{"class":209},"Hello ",[100,69611,206],{"class":205},[100,69613,2895],{"class":110},[100,69615,1708],{"class":205},[100,69617,8535],{"class":209},[100,69619,1714],{"class":205},[14,69621,69622,69623,290],{},"If you want to build strings in different ways, see ",[295,69624,69626],{"href":69625},"\u002Fhow-to\u002Fhow-to-format-strings-in-python\u002F","how to format strings in Python",[77,69628,6244],{"id":6243},[14,69630,69631],{},"Use these checks:",[40,69633,69634,69637,69640,69643,69646],{},[43,69635,69636],{},"Add the missing closing quote.",[43,69638,69639],{},"Make sure the opening and closing quotes match.",[43,69641,69642],{},"Use double quotes if the text contains an apostrophe.",[43,69644,69645],{},"Escape inner quotes with a backslash when needed.",[43,69647,69648],{},"Use triple quotes for multi-line strings.",[14,69650,69651],{},"Here are a few corrected examples:",[91,69653,69655],{"className":93,"code":69654,"language":95,"meta":96,"style":96},"# Missing closing quote fixed\nname = \"Alice\"\n\n# Matching quotes\ncolor = 'blue'\n\n# Apostrophe handled correctly\nanswer = \"I don't know\"\n\n# Inner quotes escaped\nquote = \"He said \\\"Python is fun\\\"\"\n\n# Multi-line string\nmessage = \"\"\"Line one\nLine two\"\"\"\n",[17,69656,69657,69662,69674,69678,69683,69696,69700,69705,69719,69723,69728,69749,69753,69758,69769],{"__ignoreMap":96},[100,69658,69659],{"class":102,"line":103},[100,69660,69661],{"class":414},"# Missing closing quote fixed\n",[100,69663,69664,69666,69668,69670,69672],{"class":102,"line":135},[100,69665,1691],{"class":106},[100,69667,111],{"class":110},[100,69669,1708],{"class":205},[100,69671,15343],{"class":209},[100,69673,1714],{"class":205},[100,69675,69676],{"class":102,"line":142},[100,69677,139],{"emptyLinePlaceholder":138},[100,69679,69680],{"class":102,"line":152},[100,69681,69682],{"class":414},"# Matching quotes\n",[100,69684,69685,69688,69690,69692,69694],{"class":102,"line":164},[100,69686,69687],{"class":106},"color ",[100,69689,111],{"class":110},[100,69691,1274],{"class":205},[100,69693,35284],{"class":209},[100,69695,3925],{"class":205},[100,69697,69698],{"class":102,"line":185},[100,69699,139],{"emptyLinePlaceholder":138},[100,69701,69702],{"class":102,"line":197},[100,69703,69704],{"class":414},"# Apostrophe handled correctly\n",[100,69706,69707,69710,69712,69714,69717],{"class":102,"line":771},[100,69708,69709],{"class":106},"answer ",[100,69711,111],{"class":110},[100,69713,1708],{"class":205},[100,69715,69716],{"class":209},"I don't know",[100,69718,1714],{"class":205},[100,69720,69721],{"class":102,"line":787},[100,69722,139],{"emptyLinePlaceholder":138},[100,69724,69725],{"class":102,"line":5816},[100,69726,69727],{"class":414},"# Inner quotes escaped\n",[100,69729,69730,69733,69735,69737,69740,69742,69745,69747],{"class":102,"line":5833},[100,69731,69732],{"class":106},"quote ",[100,69734,111],{"class":110},[100,69736,1708],{"class":205},[100,69738,69739],{"class":209},"He said ",[100,69741,69509],{"class":527},[100,69743,69744],{"class":209},"Python is fun",[100,69746,69509],{"class":527},[100,69748,1714],{"class":205},[100,69750,69751],{"class":102,"line":5848},[100,69752,139],{"emptyLinePlaceholder":138},[100,69754,69755],{"class":102,"line":5855},[100,69756,69757],{"class":414},"# Multi-line string\n",[100,69759,69760,69762,69764,69766],{"class":102,"line":62688},[100,69761,6868],{"class":106},[100,69763,111],{"class":110},[100,69765,69581],{"class":205},[100,69767,69768],{"class":209},"Line one\n",[100,69770,69772,69775],{"class":102,"line":69771},15,[100,69773,69774],{"class":209},"Line two",[100,69776,69590],{"class":205},[77,69778,3274],{"id":3273},[14,69780,10386],{},[3282,69782,69783,69786,69789,69792,69795],{},[43,69784,69785],{},"Look at the line named in the error message first.",[43,69787,69788],{},"Check the line before it too, because the real problem may start earlier.",[43,69790,69791],{},"Count opening and closing quotes.",[43,69793,69794],{},"Check whether a quote inside the text ends the string by accident.",[43,69796,69797],{},"If the string spans lines, replace it with triple quotes or join lines correctly.",[14,69799,69800],{},"You can also run your file directly:",[91,69802,69803],{"className":10352,"code":31396,"language":10354,"meta":96,"style":96},[17,69804,69805],{"__ignoreMap":96},[100,69806,69807,69809],{"class":102,"line":103},[100,69808,95],{"class":10361},[100,69810,30457],{"class":209},[14,69812,69813],{},"Or check it without running the program:",[91,69815,69816],{"className":10352,"code":31421,"language":10354,"meta":96,"style":96},[17,69817,69818],{"__ignoreMap":96},[100,69819,69820,69822,69824,69826],{"class":102,"line":103},[100,69821,95],{"class":10361},[100,69823,26622],{"class":10364},[100,69825,29530],{"class":209},[100,69827,30457],{"class":209},[14,69829,69830,69831,290],{},"If Python reports a broader syntax problem, compare it with ",[295,69832,16612],{"href":16611},[77,69834,7081],{"id":7080},[14,69836,69837],{},"Some syntax errors look similar but mean different things:",[40,69839,69840,69845,69860],{},[43,69841,69842,69844],{},[295,69843,16612],{"href":16611}," is a general syntax error.",[43,69846,69847,69849,69850,5870,69852,5870,69854,5870,69856,5894,69858,290],{},[17,69848,29615],{}," happens when punctuation is missing after ",[17,69851,2736],{},[17,69853,71],{},[17,69855,721],{},[17,69857,1078],{},[17,69859,3417],{},[43,69861,69862,69866],{},[295,69863,69865],{"href":69864},"\u002Ferrors\u002Fsyntaxerror-unexpected-eof-while-parsing-fix\u002F","SyntaxError: unexpected EOF while parsing"," happens when Python reaches the end of the file with unfinished code.",[77,69868,7117],{"id":7116},[14,69870,69871],{},"These are the most common reasons this error appears:",[40,69873,69874,69877,69880,69886,69889],{},[43,69875,69876],{},"A missing closing quote at the end of a string",[43,69878,69879],{},"Mixed quote types, such as opening with a single quote and closing with a double quote",[43,69881,69882,69883],{},"An apostrophe inside a single-quoted string like ",[17,69884,69885],{},"'don't'",[43,69887,69888],{},"A double quote inside a double-quoted string without escaping it",[43,69890,69891],{},"Trying to write a multi-line string with normal quotes",[14,69893,69894,69895,69901],{},"Understanding how string quotes work will help you avoid this error again. You can also review the ",[295,69896,1572,69898,1576],{"href":69897},"\u002Freference\u002Fpython-str-function-explained\u002F",[17,69899,69900],{},"str()"," if you want to understand strings better.",[77,69903,1514],{"id":1513},[675,69905,69907],{"id":69906},"what-does-eol-mean-in-this-error","What does EOL mean in this error?",[14,69909,69910],{},"EOL means end of line. Python reached the end of the line before the string was closed.",[675,69912,69914],{"id":69913},"why-does-the-error-sometimes-point-to-the-next-line","Why does the error sometimes point to the next line?",[14,69916,69917],{},"If Python thinks a string is still open, it may continue reading until the next line before reporting the error.",[675,69919,69921],{"id":69920},"how-do-i-include-an-apostrophe-inside-a-string","How do I include an apostrophe inside a string?",[14,69923,69924],{},"Use double quotes around the string, or escape the apostrophe with a backslash.",[14,69926,1844],{},[91,69928,69930],{"className":93,"code":69929,"language":95,"meta":96,"style":96},"text = \"don't\"\n",[17,69931,69932],{"__ignoreMap":96},[100,69933,69934,69936,69938,69940,69943],{"class":102,"line":103},[100,69935,4331],{"class":106},[100,69937,111],{"class":110},[100,69939,1708],{"class":205},[100,69941,69942],{"class":209},"don't",[100,69944,1714],{"class":205},[14,69946,69359],{},[91,69948,69950],{"className":93,"code":69949,"language":95,"meta":96,"style":96},"text = 'don\\'t'\n",[17,69951,69952],{"__ignoreMap":96},[100,69953,69954,69956,69958,69960,69962,69964,69967],{"class":102,"line":103},[100,69955,4331],{"class":106},[100,69957,111],{"class":110},[100,69959,1274],{"class":205},[100,69961,69397],{"class":209},[100,69963,69451],{"class":527},[100,69965,69966],{"class":209},"t",[100,69968,3925],{"class":205},[675,69970,69972],{"id":69971},"can-i-split-a-string-across-multiple-lines","Can I split a string across multiple lines?",[14,69974,69975],{},"Yes. Use triple quotes for a multi-line string, or combine separate strings correctly.",[14,69977,1844],{},[91,69979,69981],{"className":93,"code":69980,"language":95,"meta":96,"style":96},"text = \"\"\"first line\nsecond line\"\"\"\n",[17,69982,69983,69994],{"__ignoreMap":96},[100,69984,69985,69987,69989,69991],{"class":102,"line":103},[100,69986,4331],{"class":106},[100,69988,111],{"class":110},[100,69990,69581],{"class":205},[100,69992,69993],{"class":209},"first line\n",[100,69995,69996,69999],{"class":102,"line":135},[100,69997,69998],{"class":209},"second line",[100,70000,69590],{"class":205},[77,70002,1554],{"id":1553},[40,70004,70005,70009,70013,70017,70022],{},[43,70006,70007],{},[295,70008,69142],{"href":14879},[43,70010,70011],{},[295,70012,16612],{"href":16611},[43,70014,70015],{},[295,70016,69865],{"href":69864},[43,70018,70019],{},[295,70020,70021],{"href":69625},"How to format strings in Python",[43,70023,70024],{},[295,70025,1572,70026,1576],{"href":69897},[17,70027,69900],{},[1589,70029,70030],{},"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 .srdBf, html code.shiki .srdBf{--shiki-light:#F76D47;--shiki-default:#005CC5;--shiki-dark:#79B8FF}html pre.shiki code .sZMiF, html code.shiki .sZMiF{--shiki-light:#E2931D;--shiki-default:#005CC5;--shiki-dark:#79B8FF}html pre.shiki code .s_hVV, html code.shiki .s_hVV{--shiki-light:#90A4AE;--shiki-default:#005CC5;--shiki-dark:#79B8FF}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 .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 .sbgvK, html code.shiki .sbgvK{--shiki-light:#E2931D;--shiki-default:#6F42C1;--shiki-dark:#B392F0}html pre.shiki code .stzsN, html code.shiki .stzsN{--shiki-light:#91B859;--shiki-default:#005CC5;--shiki-dark:#79B8FF}",{"title":96,"searchDepth":135,"depth":135,"links":70032},[70033,70034,70035,70036,70044,70045,70046,70047,70048,70054],{"id":79,"depth":135,"text":80},{"id":5880,"depth":135,"text":5881},{"id":11030,"depth":135,"text":11031},{"id":1343,"depth":135,"text":1344,"children":70037},[70038,70039,70040,70041,70042,70043],{"id":69241,"depth":142,"text":69242},{"id":69279,"depth":142,"text":69280},{"id":69317,"depth":142,"text":69318},{"id":69378,"depth":142,"text":69379},{"id":69458,"depth":142,"text":69459},{"id":69539,"depth":142,"text":69540},{"id":6243,"depth":135,"text":6244},{"id":3273,"depth":135,"text":3274},{"id":7080,"depth":135,"text":7081},{"id":7116,"depth":135,"text":7117},{"id":1513,"depth":135,"text":1514,"children":70049},[70050,70051,70052,70053],{"id":69906,"depth":142,"text":69907},{"id":69913,"depth":142,"text":69914},{"id":69920,"depth":142,"text":69921},{"id":69971,"depth":142,"text":69972},{"id":1553,"depth":135,"text":1554},"Master syntaxerror eol while scanning string literal fix in our comprehensive Python beginner guide.",{},"\u002Ferrors\u002Fsyntaxerror-eol-while-scanning-string-literal-fix",{"title":69053,"description":70055},"errors\u002Fsyntaxerror-eol-while-scanning-string-literal-fix","px5dUnsnpigWpcaD8cPzjCoi1pv5QRqkcULfOze-7tk",{"id":70062,"title":70063,"body":70064,"description":70921,"extension":1623,"meta":70922,"navigation":138,"path":70923,"seo":70924,"stem":70925,"__hash__":70926},"content\u002Ferrors\u002Fsyntaxerror-indentation-error-fix.md","SyntaxError: indentation error (Fix)",{"type":7,"value":70065,"toc":70896},[70066,70069,70072,70074,70102,70105,70107,70110,70113,70116,70158,70161,70165,70167,70171,70174,70200,70207,70209,70235,70239,70242,70276,70278,70311,70316,70320,70323,70326,70369,70372,70374,70416,70419,70421,70424,70440,70444,70447,70478,70489,70491,70522,70524,70529,70533,70535,70577,70582,70584,70625,70629,70632,70642,70645,70659,70664,70668,70671,70689,70692,70720,70722,70749,70752,70756,70759,70772,70777,70781,70784,70787,70809,70811,70813,70829,70832,70834,70838,70846,70848,70851,70855,70858,70862,70865,70867,70893],[10,70067,70063],{"id":70068},"syntaxerror-indentation-error-fix",[14,70070,70071],{},"This page helps beginners fix Python indentation-related syntax errors. It explains what the error means, why it happens, how to find the problem line, and how to correct it safely.",[77,70073,80],{"id":79},[91,70075,70077],{"className":93,"code":70076,"language":95,"meta":96,"style":96},"if True:\n    print(\"Correctly indented\")\n",[17,70078,70079,70087],{"__ignoreMap":96},[100,70080,70081,70083,70085],{"class":102,"line":103},[100,70082,2736],{"class":145},[100,70084,159],{"class":158},[100,70086,149],{"class":118},[100,70088,70089,70091,70093,70095,70098,70100],{"class":102,"line":135},[100,70090,200],{"class":114},[100,70092,170],{"class":118},[100,70094,206],{"class":205},[100,70096,70097],{"class":209},"Correctly indented",[100,70099,206],{"class":205},[100,70101,215],{"class":118},[14,70103,70104],{},"Python uses indentation to define code blocks. After a line ending with a colon, indent the next line consistently.",[77,70106,5881],{"id":5880},[14,70108,70109],{},"Python uses indentation to show which lines belong inside a block of code.",[14,70111,70112],{},"This error usually means Python expected indentation, or found indentation that does not match the surrounding code.",[14,70114,70115],{},"This often happens after statements such as:",[40,70117,70118,70122,70126,70130,70134,70138,70142,70146,70150,70154],{},[43,70119,70120],{},[17,70121,2736],{},[43,70123,70124],{},[17,70125,71],{},[43,70127,70128],{},[17,70129,721],{},[43,70131,70132],{},[17,70133,1078],{},[43,70135,70136],{},[17,70137,3417],{},[43,70139,70140],{},[17,70141,146],{},[43,70143,70144],{},[17,70145,188],{},[43,70147,70148],{},[17,70149,4944],{},[43,70151,70152],{},[17,70153,30862],{},[43,70155,70156],{},[17,70157,17521],{},[14,70159,70160],{},"In short, the code structure is unclear because spaces or tabs are missing, extra, or inconsistent.",[14,70162,28943,70163,290],{},[295,70164,28947],{"href":28946},[77,70166,24958],{"id":24957},[675,70168,70170],{"id":70169},"_1-a-line-after-a-colon-is-not-indented","1. A line after a colon is not indented",[14,70172,70173],{},"This is one of the most common causes.",[91,70175,70176],{"className":93,"code":28993,"language":95,"meta":96,"style":96},[17,70177,70178,70186],{"__ignoreMap":96},[100,70179,70180,70182,70184],{"class":102,"line":103},[100,70181,2736],{"class":145},[100,70183,159],{"class":158},[100,70185,149],{"class":118},[100,70187,70188,70190,70192,70194,70196,70198],{"class":102,"line":135},[100,70189,372],{"class":114},[100,70191,170],{"class":118},[100,70193,206],{"class":205},[100,70195,7683],{"class":209},[100,70197,206],{"class":205},[100,70199,215],{"class":118},[14,70201,30078,70202,70204,70205,25128],{},[17,70203,13235],{}," line to be inside the ",[17,70206,2736],{},[14,70208,29343],{},[91,70210,70211],{"className":93,"code":29144,"language":95,"meta":96,"style":96},[17,70212,70213,70221],{"__ignoreMap":96},[100,70214,70215,70217,70219],{"class":102,"line":103},[100,70216,2736],{"class":145},[100,70218,159],{"class":158},[100,70220,149],{"class":118},[100,70222,70223,70225,70227,70229,70231,70233],{"class":102,"line":135},[100,70224,200],{"class":114},[100,70226,170],{"class":118},[100,70228,206],{"class":205},[100,70230,7683],{"class":209},[100,70232,206],{"class":205},[100,70234,215],{"class":118},[675,70236,70238],{"id":70237},"_2-a-line-is-indented-when-python-is-not-expecting-it","2. A line is indented when Python is not expecting it",[14,70240,70241],{},"Sometimes a line starts with spaces even though it should begin at the left side.",[91,70243,70245],{"className":93,"code":70244,"language":95,"meta":96,"style":96},"print(\"Start\")\n    print(\"This line has extra indentation\")\n",[17,70246,70247,70261],{"__ignoreMap":96},[100,70248,70249,70251,70253,70255,70257,70259],{"class":102,"line":103},[100,70250,372],{"class":114},[100,70252,170],{"class":118},[100,70254,206],{"class":205},[100,70256,29572],{"class":209},[100,70258,206],{"class":205},[100,70260,215],{"class":118},[100,70262,70263,70265,70267,70269,70272,70274],{"class":102,"line":135},[100,70264,200],{"class":114},[100,70266,170],{"class":118},[100,70268,206],{"class":205},[100,70270,70271],{"class":209},"This line has extra indentation",[100,70273,206],{"class":205},[100,70275,215],{"class":118},[14,70277,29343],{},[91,70279,70281],{"className":93,"code":70280,"language":95,"meta":96,"style":96},"print(\"Start\")\nprint(\"This line has extra indentation\")\n",[17,70282,70283,70297],{"__ignoreMap":96},[100,70284,70285,70287,70289,70291,70293,70295],{"class":102,"line":103},[100,70286,372],{"class":114},[100,70288,170],{"class":118},[100,70290,206],{"class":205},[100,70292,29572],{"class":209},[100,70294,206],{"class":205},[100,70296,215],{"class":118},[100,70298,70299,70301,70303,70305,70307,70309],{"class":102,"line":135},[100,70300,372],{"class":114},[100,70302,170],{"class":118},[100,70304,206],{"class":205},[100,70306,70271],{"class":209},[100,70308,206],{"class":205},[100,70310,215],{"class":118},[14,70312,70313,70314,290],{},"If Python shows a more specific message, see ",[295,70315,16618],{"href":16617},[675,70317,70319],{"id":70318},"_3-tabs-and-spaces-are-mixed-in-the-same-block","3. Tabs and spaces are mixed in the same block",[14,70321,70322],{},"This can be hard to see because tabs and spaces may look similar in your editor.",[14,70324,70325],{},"Problem example:",[91,70327,70329],{"className":93,"code":70328,"language":95,"meta":96,"style":96},"if True:\n    print(\"Line with a tab\")\n    print(\"Line with spaces\")\n",[17,70330,70331,70339,70354],{"__ignoreMap":96},[100,70332,70333,70335,70337],{"class":102,"line":103},[100,70334,2736],{"class":145},[100,70336,159],{"class":158},[100,70338,149],{"class":118},[100,70340,70341,70343,70345,70347,70350,70352],{"class":102,"line":135},[100,70342,200],{"class":114},[100,70344,170],{"class":118},[100,70346,206],{"class":205},[100,70348,70349],{"class":209},"Line with a tab",[100,70351,206],{"class":205},[100,70353,215],{"class":118},[100,70355,70356,70358,70360,70362,70365,70367],{"class":102,"line":142},[100,70357,200],{"class":114},[100,70359,170],{"class":118},[100,70361,206],{"class":205},[100,70363,70364],{"class":209},"Line with spaces",[100,70366,206],{"class":205},[100,70368,215],{"class":118},[14,70370,70371],{},"The indentation looks similar, but the first indented line uses a tab and the second uses spaces.",[14,70373,29343],{},[91,70375,70377],{"className":93,"code":70376,"language":95,"meta":96,"style":96},"if True:\n    print(\"Line one\")\n    print(\"Line two\")\n",[17,70378,70379,70387,70402],{"__ignoreMap":96},[100,70380,70381,70383,70385],{"class":102,"line":103},[100,70382,2736],{"class":145},[100,70384,159],{"class":158},[100,70386,149],{"class":118},[100,70388,70389,70391,70393,70395,70398,70400],{"class":102,"line":135},[100,70390,200],{"class":114},[100,70392,170],{"class":118},[100,70394,206],{"class":205},[100,70396,70397],{"class":209},"Line one",[100,70399,206],{"class":205},[100,70401,215],{"class":118},[100,70403,70404,70406,70408,70410,70412,70414],{"class":102,"line":142},[100,70405,200],{"class":114},[100,70407,170],{"class":118},[100,70409,206],{"class":205},[100,70411,69774],{"class":209},[100,70413,206],{"class":205},[100,70415,215],{"class":118},[14,70417,70418],{},"Use spaces only if you are a beginner. Four spaces per indentation level is the standard.",[77,70420,6244],{"id":6243},[14,70422,70423],{},"Use these steps to find and fix the problem quickly:",[40,70425,70426,70429,70432,70435,70438],{},[43,70427,70428],{},"Check the line shown in the traceback",[43,70430,70431],{},"Also check the line above it because the real mistake is often there",[43,70433,70434],{},"Make sure lines inside the same block use the same indentation level",[43,70436,70437],{},"Use 4 spaces for each indentation level",[43,70439,30509],{},[675,70441,70443],{"id":70442},"example-checking-the-line-above","Example: checking the line above",[14,70445,70446],{},"This code will fail:",[91,70448,70450],{"className":93,"code":70449,"language":95,"meta":96,"style":96},"for i in range(3):\nprint(i)\n",[17,70451,70452,70468],{"__ignoreMap":96},[100,70453,70454,70456,70458,70460,70462,70464,70466],{"class":102,"line":103},[100,70455,71],{"class":145},[100,70457,29838],{"class":106},[100,70459,617],{"class":145},[100,70461,29843],{"class":114},[100,70463,170],{"class":118},[100,70465,11541],{"class":122},[100,70467,522],{"class":118},[100,70469,70470,70472,70474,70476],{"class":102,"line":135},[100,70471,372],{"class":114},[100,70473,170],{"class":118},[100,70475,29858],{"class":178},[100,70477,215],{"class":118},[14,70479,70480,70481,70484,70485,70488],{},"The error may point to the ",[17,70482,70483],{},"print(i)"," line, but the real issue is that the line after ",[17,70486,70487],{},"for ...:"," was not indented.",[14,70490,6031],{},[91,70492,70494],{"className":93,"code":70493,"language":95,"meta":96,"style":96},"for i in range(3):\n    print(i)\n",[17,70495,70496,70512],{"__ignoreMap":96},[100,70497,70498,70500,70502,70504,70506,70508,70510],{"class":102,"line":103},[100,70499,71],{"class":145},[100,70501,29838],{"class":106},[100,70503,617],{"class":145},[100,70505,29843],{"class":114},[100,70507,170],{"class":118},[100,70509,11541],{"class":122},[100,70511,522],{"class":118},[100,70513,70514,70516,70518,70520],{"class":102,"line":135},[100,70515,200],{"class":114},[100,70517,170],{"class":118},[100,70519,29858],{"class":178},[100,70521,215],{"class":118},[14,70523,14742],{},[91,70525,70527],{"className":70526,"code":51139,"language":4468,"meta":96},[26775],[17,70528,51139],{"__ignoreMap":96},[675,70530,70532],{"id":70531},"example-keeping-the-same-indentation-inside-one-block","Example: keeping the same indentation inside one block",[14,70534,9709],{},[91,70536,70538],{"className":93,"code":70537,"language":95,"meta":96,"style":96},"def greet():\n    print(\"Hello\")\n      print(\"Welcome\")\n",[17,70539,70540,70548,70562],{"__ignoreMap":96},[100,70541,70542,70544,70546],{"class":102,"line":103},[100,70543,1078],{"class":1077},[100,70545,2848],{"class":1081},[100,70547,1085],{"class":118},[100,70549,70550,70552,70554,70556,70558,70560],{"class":102,"line":135},[100,70551,200],{"class":114},[100,70553,170],{"class":118},[100,70555,206],{"class":205},[100,70557,7683],{"class":209},[100,70559,206],{"class":205},[100,70561,215],{"class":118},[100,70563,70564,70567,70569,70571,70573,70575],{"class":102,"line":142},[100,70565,70566],{"class":114},"      print",[100,70568,170],{"class":118},[100,70570,206],{"class":205},[100,70572,54817],{"class":209},[100,70574,206],{"class":205},[100,70576,215],{"class":118},[14,70578,32048,70579,70581],{},[17,70580,13235],{}," line has different indentation.",[14,70583,9750],{},[91,70585,70587],{"className":93,"code":70586,"language":95,"meta":96,"style":96},"def greet():\n    print(\"Hello\")\n    print(\"Welcome\")\n",[17,70588,70589,70597,70611],{"__ignoreMap":96},[100,70590,70591,70593,70595],{"class":102,"line":103},[100,70592,1078],{"class":1077},[100,70594,2848],{"class":1081},[100,70596,1085],{"class":118},[100,70598,70599,70601,70603,70605,70607,70609],{"class":102,"line":135},[100,70600,200],{"class":114},[100,70602,170],{"class":118},[100,70604,206],{"class":205},[100,70606,7683],{"class":209},[100,70608,206],{"class":205},[100,70610,215],{"class":118},[100,70612,70613,70615,70617,70619,70621,70623],{"class":102,"line":142},[100,70614,200],{"class":114},[100,70616,170],{"class":118},[100,70618,206],{"class":205},[100,70620,54817],{"class":209},[100,70622,206],{"class":205},[100,70624,215],{"class":118},[675,70626,70628],{"id":70627},"useful-commands-for-checking-your-file","Useful commands for checking your file",[14,70630,70631],{},"Run your script normally:",[91,70633,70634],{"className":10352,"code":31396,"language":10354,"meta":96,"style":96},[17,70635,70636],{"__ignoreMap":96},[100,70637,70638,70640],{"class":102,"line":103},[100,70639,95],{"class":10361},[100,70641,30457],{"class":209},[14,70643,70644],{},"Or ask Python to compile it and report syntax problems:",[91,70646,70647],{"className":10352,"code":31421,"language":10354,"meta":96,"style":96},[17,70648,70649],{"__ignoreMap":96},[100,70650,70651,70653,70655,70657],{"class":102,"line":103},[100,70652,95],{"class":10361},[100,70654,26622],{"class":10364},[100,70656,29530],{"class":209},[100,70658,30457],{"class":209},[14,70660,70661,70662,16458],{},"If you are still stuck, a step-by-step ",[295,70663,7077],{"href":3852},[77,70665,70667],{"id":70666},"places-where-beginners-often-see-it","Places where beginners often see it",[14,70669,70670],{},"Beginners often run into this error in these places:",[40,70672,70673,70677,70680,70683,70686],{},[43,70674,28974,70675,32335],{},[17,70676,2736],{},[43,70678,70679],{},"Inside loops",[43,70681,70682],{},"Inside functions",[43,70684,70685],{},"When copying code from websites or documents",[43,70687,70688],{},"After editing code in different editors",[14,70690,70691],{},"A very common example is forgetting to indent after a function definition:",[91,70693,70695],{"className":93,"code":70694,"language":95,"meta":96,"style":96},"def say_hi():\nprint(\"Hi\")\n",[17,70696,70697,70706],{"__ignoreMap":96},[100,70698,70699,70701,70704],{"class":102,"line":103},[100,70700,1078],{"class":1077},[100,70702,70703],{"class":1081}," say_hi",[100,70705,1085],{"class":118},[100,70707,70708,70710,70712,70714,70716,70718],{"class":102,"line":135},[100,70709,372],{"class":114},[100,70711,170],{"class":118},[100,70713,206],{"class":205},[100,70715,29041],{"class":209},[100,70717,206],{"class":205},[100,70719,215],{"class":118},[14,70721,6031],{},[91,70723,70725],{"className":93,"code":70724,"language":95,"meta":96,"style":96},"def say_hi():\n    print(\"Hi\")\n",[17,70726,70727,70735],{"__ignoreMap":96},[100,70728,70729,70731,70733],{"class":102,"line":103},[100,70730,1078],{"class":1077},[100,70732,70703],{"class":1081},[100,70734,1085],{"class":118},[100,70736,70737,70739,70741,70743,70745,70747],{"class":102,"line":135},[100,70738,200],{"class":114},[100,70740,170],{"class":118},[100,70742,206],{"class":205},[100,70744,29041],{"class":209},[100,70746,206],{"class":205},[100,70748,215],{"class":118},[14,70750,70751],{},"If the problem started after copying code, retype the indentation manually instead of trusting the pasted whitespace.",[77,70753,70755],{"id":70754},"how-to-prevent-it","How to prevent it",[14,70757,70758],{},"A few simple habits can prevent most indentation errors:",[40,70760,70761,70764,70766,70769],{},[43,70762,70763],{},"Configure your editor to insert spaces instead of tabs",[43,70765,30407],{},[43,70767,70768],{},"Indent code immediately after typing a colon",[43,70770,70771],{},"Format code consistently before running it",[14,70773,70774,70775,290],{},"It also helps to understand basic Python structure. If needed, review ",[295,70776,30533],{"href":31678},[77,70778,70780],{"id":70779},"when-to-read-a-more-specific-error-page","When to read a more specific error page",[14,70782,70783],{},"This page covers indentation-related syntax problems in general. Sometimes Python gives a more exact error name.",[14,70785,70786],{},"Use a more specific page when needed:",[40,70788,70789,70796,70804],{},[43,70790,70791,70792,70795],{},"Read ",[295,70793,28816],{"href":70794},"\u002Ferrors\u002Findentationerror-expected-an-indented-block-fix\u002F"," if Python says that exact message",[43,70797,70791,70798,70801,70802],{},[295,70799,29615],{"href":70800},"\u002Ferrors\u002Fsyntaxerror-missing-colon-fix\u002F"," if the block-starting line forgot ",[17,70803,89],{},[43,70805,70791,70806,70808],{},[295,70807,16618],{"href":16617}," if a line is indented when it should not be",[77,70810,7117],{"id":7116},[14,70812,9001],{},[40,70814,70815,70818,70821,70823,70826],{},[43,70816,70817],{},"Missing indentation after a colon",[43,70819,70820],{},"Extra indentation on a line that should start at the left margin",[43,70822,29638],{},[43,70824,70825],{},"Copying code with broken whitespace",[43,70827,70828],{},"Indenting one line in a block more or less than the others",[14,70830,70831],{},"When checking your code, compare each line in the same block carefully. Even one extra space can be enough to break the structure.",[77,70833,1514],{"id":1513},[675,70835,70837],{"id":70836},"is-this-the-same-as-indentationerror","Is this the same as IndentationError?",[14,70839,70840,70841,70843,70844,290],{},"They are closely related. Some indentation problems appear as ",[17,70842,16503],{},", while others appear as ",[17,70845,16508],{},[675,70847,32549],{"id":32548},[14,70849,70850],{},"Use 4 spaces for each indentation level. This is the standard style.",[675,70852,70854],{"id":70853},"why-does-the-error-point-to-the-wrong-line-sometimes","Why does the error point to the wrong line sometimes?",[14,70856,70857],{},"The real problem is often on the previous line, especially after a line that should start a block.",[675,70859,70861],{"id":70860},"can-tabs-work-in-python","Can tabs work in Python?",[14,70863,70864],{},"Tabs can appear in code, but mixing tabs and spaces causes problems. Beginners should use spaces only.",[77,70866,1554],{"id":1553},[40,70868,70869,70873,70877,70881,70885,70889],{},[43,70870,70871],{},[295,70872,28947],{"href":28946},[43,70874,70875],{},[295,70876,16618],{"href":16617},[43,70878,70879],{},[295,70880,28816],{"href":70794},[43,70882,70883],{},[295,70884,29615],{"href":70800},[43,70886,70887],{},[295,70888,30533],{"href":31678},[43,70890,70891],{},[295,70892,42267],{"href":3852},[1589,70894,70895],{},"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 .s39Yj, html code.shiki .s39Yj{--shiki-light:#39ADB5;--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 .sptTA, html code.shiki .sptTA{--shiki-light:#6182B8;--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 .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 .su5hD, html code.shiki .su5hD{--shiki-light:#90A4AE;--shiki-default:#24292E;--shiki-dark:#E1E4E8}html pre.shiki code .srdBf, html code.shiki .srdBf{--shiki-light:#F76D47;--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 .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 .sbgvK, html code.shiki .sbgvK{--shiki-light:#E2931D;--shiki-default:#6F42C1;--shiki-dark:#B392F0}html pre.shiki code .stzsN, html code.shiki .stzsN{--shiki-light:#91B859;--shiki-default:#005CC5;--shiki-dark:#79B8FF}",{"title":96,"searchDepth":135,"depth":135,"links":70897},[70898,70899,70900,70905,70910,70911,70912,70913,70914,70920],{"id":79,"depth":135,"text":80},{"id":5880,"depth":135,"text":5881},{"id":24957,"depth":135,"text":24958,"children":70901},[70902,70903,70904],{"id":70169,"depth":142,"text":70170},{"id":70237,"depth":142,"text":70238},{"id":70318,"depth":142,"text":70319},{"id":6243,"depth":135,"text":6244,"children":70906},[70907,70908,70909],{"id":70442,"depth":142,"text":70443},{"id":70531,"depth":142,"text":70532},{"id":70627,"depth":142,"text":70628},{"id":70666,"depth":135,"text":70667},{"id":70754,"depth":135,"text":70755},{"id":70779,"depth":135,"text":70780},{"id":7116,"depth":135,"text":7117},{"id":1513,"depth":135,"text":1514,"children":70915},[70916,70917,70918,70919],{"id":70836,"depth":142,"text":70837},{"id":32548,"depth":142,"text":32549},{"id":70853,"depth":142,"text":70854},{"id":70860,"depth":142,"text":70861},{"id":1553,"depth":135,"text":1554},"Master syntaxerror indentation error fix in our comprehensive Python beginner guide.",{},"\u002Ferrors\u002Fsyntaxerror-indentation-error-fix",{"title":70063,"description":70921},"errors\u002Fsyntaxerror-indentation-error-fix","AOHKhXd-FzeAGMVPxE3MRFnoRlooxbhUXTuZxdKjh7E",{"id":70928,"title":70929,"body":70930,"description":71987,"extension":1623,"meta":71988,"navigation":138,"path":71989,"seo":71990,"stem":71991,"__hash__":71992},"content\u002Ferrors\u002Fsyntaxerror-invalid-character-in-identifier-fix.md","SyntaxError: invalid character in identifier (Fix)",{"type":7,"value":70931,"toc":71948},[70932,70935,70941,70944,70947,70949,71013,71016,71027,71029,71032,71039,71050,71053,71103,71106,71109,71113,71115,71148,71152,71169,71175,71177,71181,71184,71200,71210,71212,71231,71233,71238,71242,71245,71273,71276,71278,71305,71307,71312,71316,71319,71348,71351,71353,71379,71381,71386,71388,71391,71417,71423,71427,71430,71434,71437,71447,71450,71457,71460,71494,71497,71501,71504,71521,71525,71528,71531,71535,71538,71552,71555,71559,71562,71566,71583,71586,71590,71593,71597,71600,71640,71643,71654,71658,71661,71705,71708,71726,71730,71733,71762,71766,71769,71792,71796,71799,71817,71820,71825,71829,71832,71849,71852,71866,71870,71873,71875,71891,71893,71897,71900,71904,71907,71911,71914,71918,71921,71923,71945],[10,70933,70929],{"id":70934},"syntaxerror-invalid-character-in-identifier-fix",[14,70936,70937,70940],{},[17,70938,70939],{},"SyntaxError: invalid character in identifier"," means Python found a character in your code that does not belong there.",[14,70942,70943],{},"This usually happens when code contains a character that looks normal but is not the normal Python version. Common examples include curly quotes, an em dash, or a hidden Unicode symbol copied from a website, PDF, or document.",[14,70945,70946],{},"This page shows what the error means, what causes it, how to find the bad character, and how to fix it.",[77,70948,80],{"id":79},[91,70950,70952],{"className":93,"code":70951,"language":95,"meta":96,"style":96},"name = \"Alice\"\nprint(name)\n\n# Common problem:\n# print(“Hello”)   # curly quotes cause the error\n\n# Fix:\nprint(\"Hello\")\n",[17,70953,70954,70966,70976,70980,70985,70990,70994,70999],{"__ignoreMap":96},[100,70955,70956,70958,70960,70962,70964],{"class":102,"line":103},[100,70957,1691],{"class":106},[100,70959,111],{"class":110},[100,70961,1708],{"class":205},[100,70963,15343],{"class":209},[100,70965,1714],{"class":205},[100,70967,70968,70970,70972,70974],{"class":102,"line":135},[100,70969,372],{"class":114},[100,70971,170],{"class":118},[100,70973,2853],{"class":178},[100,70975,215],{"class":118},[100,70977,70978],{"class":102,"line":142},[100,70979,139],{"emptyLinePlaceholder":138},[100,70981,70982],{"class":102,"line":152},[100,70983,70984],{"class":414},"# Common problem:\n",[100,70986,70987],{"class":102,"line":164},[100,70988,70989],{"class":414},"# print(“Hello”)   # curly quotes cause the error\n",[100,70991,70992],{"class":102,"line":185},[100,70993,139],{"emptyLinePlaceholder":138},[100,70995,70996],{"class":102,"line":197},[100,70997,70998],{"class":414},"# Fix:\n",[100,71000,71001,71003,71005,71007,71009,71011],{"class":102,"line":771},[100,71002,372],{"class":114},[100,71004,170],{"class":118},[100,71006,206],{"class":205},[100,71008,7683],{"class":209},[100,71010,206],{"class":205},[100,71012,215],{"class":118},[14,71014,71015],{},"In many cases, the fastest fix is:",[40,71017,71018,71021,71024],{},[43,71019,71020],{},"Delete the suspicious character",[43,71022,71023],{},"Type it again manually",[43,71025,71026],{},"Use normal keyboard characters only",[77,71028,5881],{"id":5880},[14,71030,71031],{},"Python found a character inside a name or line of code that it cannot use there.",[14,71033,71034,71035,71038],{},"A Python ",[22,71036,71037],{},"identifier"," is a name such as:",[40,71040,71041,71044,71047],{},[43,71042,71043],{},"A variable name",[43,71045,71046],{},"A function name",[43,71048,71049],{},"A class name",[14,71051,71052],{},"For example, these are identifiers:",[91,71054,71056],{"className":93,"code":71055,"language":95,"meta":96,"style":96},"user_name = \"Alice\"\n\ndef greet():\n    pass\n\nclass Person:\n    pass\n",[17,71057,71058,71070,71074,71082,71086,71090,71099],{"__ignoreMap":96},[100,71059,71060,71062,71064,71066,71068],{"class":102,"line":103},[100,71061,53538],{"class":106},[100,71063,111],{"class":110},[100,71065,1708],{"class":205},[100,71067,15343],{"class":209},[100,71069,1714],{"class":205},[100,71071,71072],{"class":102,"line":135},[100,71073,139],{"emptyLinePlaceholder":138},[100,71075,71076,71078,71080],{"class":102,"line":142},[100,71077,1078],{"class":1077},[100,71079,2848],{"class":1081},[100,71081,1085],{"class":118},[100,71083,71084],{"class":102,"line":152},[100,71085,16214],{"class":145},[100,71087,71088],{"class":102,"line":164},[100,71089,139],{"emptyLinePlaceholder":138},[100,71091,71092,71094,71097],{"class":102,"line":185},[100,71093,3417],{"class":1077},[100,71095,71096],{"class":10361}," Person",[100,71098,149],{"class":118},[100,71100,71101],{"class":102,"line":197},[100,71102,16214],{"class":145},[14,71104,71105],{},"The problem is often not the whole line. It is usually just one bad character on that line.",[14,71107,71108],{},"Also, the character may look correct on screen but still be different from the normal keyboard version.",[77,71110,71112],{"id":71111},"what-usually-causes-it","What usually causes it",[14,71114,3971],{},[40,71116,71117,71120,71130,71142,71145],{},[43,71118,71119],{},"Copying code from a web page, PDF, Word document, or chat app",[43,71121,71122,71123,3178,71126,71129],{},"Using curly quotes like ",[17,71124,71125],{},"“ ”",[17,71127,71128],{},"‘ ’"," instead of straight quotes",[43,71131,71132,71133,3178,71136,71139,71140],{},"Using a dash like ",[17,71134,71135],{},"–",[17,71137,71138],{},"—"," instead of the normal minus sign ",[17,71141,2193],{},[43,71143,71144],{},"Pasting invisible characters or non-breaking spaces into code",[43,71146,71147],{},"Using punctuation from another keyboard layout inside names",[675,71149,71151],{"id":71150},"common-causes-at-a-glance","Common causes at a glance",[40,71153,71154,71157,71160,71163,71166],{},[43,71155,71156],{},"Curly quotes copied from a website or document",[43,71158,71159],{},"Em dash or en dash used instead of a minus sign",[43,71161,71162],{},"Invisible Unicode character pasted into code",[43,71164,71165],{},"Non-breaking space inside a line",[43,71167,71168],{},"Invalid symbol inside a variable or function name",[14,71170,71171,71172,290],{},"If you are new to Python syntax, it also helps to review ",[295,71173,71174],{"href":31678},"Python syntax basics",[77,71176,11031],{"id":11030},[675,71178,71180],{"id":71179},"example-1-curly-quotes","Example 1: Curly quotes",[14,71182,71183],{},"This line uses curly quotes, not normal double quotes:",[91,71185,71187],{"className":93,"code":71186,"language":95,"meta":96,"style":96},"print(“Hello”)\n",[17,71188,71189],{"__ignoreMap":96},[100,71190,71191,71193,71195,71198],{"class":102,"line":103},[100,71192,372],{"class":114},[100,71194,170],{"class":118},[100,71196,71197],{"class":178},"“Hello”",[100,71199,215],{"class":118},[14,71201,71202,71203,3266,71206,71209],{},"Python will raise a syntax error because ",[17,71204,71205],{},"“",[17,71207,71208],{},"”"," are not valid string delimiters in Python.",[14,71211,6031],{},[91,71213,71215],{"className":93,"code":71214,"language":95,"meta":96,"style":96},"print(\"Hello\")\n",[17,71216,71217],{"__ignoreMap":96},[100,71218,71219,71221,71223,71225,71227,71229],{"class":102,"line":103},[100,71220,372],{"class":114},[100,71222,170],{"class":118},[100,71224,206],{"class":205},[100,71226,7683],{"class":209},[100,71228,206],{"class":205},[100,71230,215],{"class":118},[14,71232,14742],{},[91,71234,71236],{"className":71235,"code":29173,"language":4468,"meta":96},[26775],[17,71237,29173],{"__ignoreMap":96},[675,71239,71241],{"id":71240},"example-2-em-dash-instead-of-minus-sign","Example 2: Em dash instead of minus sign",[14,71243,71244],{},"This line uses an em dash:",[91,71246,71248],{"className":93,"code":71247,"language":95,"meta":96,"style":96},"result = 10 — 5\nprint(result)\n",[17,71249,71250,71263],{"__ignoreMap":96},[100,71251,71252,71254,71256,71258,71261],{"class":102,"line":103},[100,71253,11556],{"class":106},[100,71255,111],{"class":110},[100,71257,6550],{"class":122},[100,71259,71260],{"class":106}," — ",[100,71262,1909],{"class":122},[100,71264,71265,71267,71269,71271],{"class":102,"line":135},[100,71266,372],{"class":114},[100,71268,170],{"class":118},[100,71270,11580],{"class":178},[100,71272,215],{"class":118},[14,71274,71275],{},"That dash is not the normal minus sign, so Python cannot parse it correctly.",[14,71277,6031],{},[91,71279,71281],{"className":93,"code":71280,"language":95,"meta":96,"style":96},"result = 10 - 5\nprint(result)\n",[17,71282,71283,71295],{"__ignoreMap":96},[100,71284,71285,71287,71289,71291,71293],{"class":102,"line":103},[100,71286,11556],{"class":106},[100,71288,111],{"class":110},[100,71290,6550],{"class":122},[100,71292,1906],{"class":110},[100,71294,37580],{"class":122},[100,71296,71297,71299,71301,71303],{"class":102,"line":135},[100,71298,372],{"class":114},[100,71300,170],{"class":118},[100,71302,11580],{"class":178},[100,71304,215],{"class":118},[14,71306,14742],{},[91,71308,71310],{"className":71309,"code":1909,"language":4468,"meta":96},[26775],[17,71311,1909],{"__ignoreMap":96},[675,71313,71315],{"id":71314},"example-3-bad-character-in-a-name","Example 3: Bad character in a name",[14,71317,71318],{},"Here, the variable name contains an invalid symbol:",[91,71320,71322],{"className":93,"code":71321,"language":95,"meta":96,"style":96},"user—name = \"Sam\"\nprint(user—name)\n",[17,71323,71324,71337],{"__ignoreMap":96},[100,71325,71326,71329,71331,71333,71335],{"class":102,"line":103},[100,71327,71328],{"class":106},"user—name ",[100,71330,111],{"class":110},[100,71332,1708],{"class":205},[100,71334,29329],{"class":209},[100,71336,1714],{"class":205},[100,71338,71339,71341,71343,71346],{"class":102,"line":135},[100,71340,372],{"class":114},[100,71342,170],{"class":118},[100,71344,71345],{"class":178},"user—name",[100,71347,215],{"class":118},[14,71349,71350],{},"The em dash is not valid in a Python identifier.",[14,71352,6031],{},[91,71354,71355],{"className":93,"code":55505,"language":95,"meta":96,"style":96},[17,71356,71357,71369],{"__ignoreMap":96},[100,71358,71359,71361,71363,71365,71367],{"class":102,"line":103},[100,71360,53538],{"class":106},[100,71362,111],{"class":110},[100,71364,1708],{"class":205},[100,71366,29329],{"class":209},[100,71368,1714],{"class":205},[100,71370,71371,71373,71375,71377],{"class":102,"line":135},[100,71372,372],{"class":114},[100,71374,170],{"class":118},[100,71376,53589],{"class":178},[100,71378,215],{"class":118},[14,71380,14742],{},[91,71382,71384],{"className":71383,"code":16526,"language":4468,"meta":96},[26775],[17,71385,16526],{"__ignoreMap":96},[77,71387,6244],{"id":6243},[14,71389,71390],{},"Try these fixes:",[40,71392,71393,71396,71403,71408,71411,71414],{},[43,71394,71395],{},"Delete the suspicious character and type it again manually",[43,71397,71398,71399,3178,71401],{},"Replace curly quotes with normal quotes: ",[17,71400,206],{},[17,71402,1280],{},[43,71404,71405,71406],{},"Replace long dashes with the normal minus sign: ",[17,71407,2193],{},[43,71409,71410],{},"Retype the whole line if you cannot see which character is wrong",[43,71412,71413],{},"Paste the code into a plain text editor to remove rich-text formatting",[43,71415,71416],{},"Check variable and function names for unusual symbols",[14,71418,71419,71420,71422],{},"If the error is inside a string example, reviewing ",[295,71421,6080],{"href":14879}," can help you spot quote problems faster.",[77,71424,71426],{"id":71425},"how-to-find-the-bad-character","How to find the bad character",[14,71428,71429],{},"Use the traceback to narrow it down.",[675,71431,71433],{"id":71432},"_1-look-at-the-line-number","1. Look at the line number",[14,71435,71436],{},"Run your script:",[91,71438,71439],{"className":10352,"code":31396,"language":10354,"meta":96,"style":96},[17,71440,71441],{"__ignoreMap":96},[100,71442,71443,71445],{"class":102,"line":103},[100,71444,95],{"class":10361},[100,71446,30457],{"class":209},[14,71448,71449],{},"Python usually shows the exact line where the error happened.",[675,71451,71453,71454],{"id":71452},"_2-check-the-caret","2. Check the caret ",[17,71455,71456],{},"^",[14,71458,71459],{},"Sometimes Python marks the position with a caret:",[91,71461,71463],{"className":93,"code":71462,"language":95,"meta":96,"style":96},"print(“Hello”)\n      ^\nSyntaxError: invalid character in identifier\n",[17,71464,71465,71475,71480],{"__ignoreMap":96},[100,71466,71467,71469,71471,71473],{"class":102,"line":103},[100,71468,372],{"class":114},[100,71470,170],{"class":118},[100,71472,71197],{"class":178},[100,71474,215],{"class":118},[100,71476,71477],{"class":102,"line":135},[100,71478,71479],{"class":110},"      ^\n",[100,71481,71482,71484,71486,71489,71491],{"class":102,"line":142},[100,71483,16503],{"class":191},[100,71485,89],{"class":118},[100,71487,71488],{"class":106}," invalid character ",[100,71490,617],{"class":110},[100,71492,71493],{"class":106}," identifier\n",[14,71495,71496],{},"The marker may point at or near the problem character.",[675,71498,71500],{"id":71499},"_3-check-suspicious-characters-closely","3. Check suspicious characters closely",[14,71502,71503],{},"Look carefully for:",[40,71505,71506,71509,71512,71515,71518],{},[43,71507,71508],{},"Quotes",[43,71510,71511],{},"Dashes",[43,71513,71514],{},"Spaces",[43,71516,71517],{},"Punctuation",[43,71519,71520],{},"Characters copied from another source",[675,71522,71524],{"id":71523},"_4-retype-the-suspicious-part","4. Retype the suspicious part",[14,71526,71527],{},"Move through the line character by character and retype the part that looks wrong.",[14,71529,71530],{},"If needed, retype the whole line manually.",[675,71532,71534],{"id":71533},"_5-compile-the-file-to-check-syntax","5. Compile the file to check syntax",[14,71536,71537],{},"You can also ask Python to check the file without running it:",[91,71539,71540],{"className":10352,"code":31421,"language":10354,"meta":96,"style":96},[17,71541,71542],{"__ignoreMap":96},[100,71543,71544,71546,71548,71550],{"class":102,"line":103},[100,71545,95],{"class":10361},[100,71547,26622],{"class":10364},[100,71549,29530],{"class":209},[100,71551,30457],{"class":209},[14,71553,71554],{},"This is useful when you want to find syntax problems quickly.",[675,71556,71558],{"id":71557},"_6-try-a-plain-text-editor","6. Try a plain text editor",[14,71560,71561],{},"If the line still looks fine, paste it into a plain text editor and compare it with your code editor version.",[675,71563,71565],{"id":71564},"_7-utf-8-mode-may-help-in-some-environments","7. UTF-8 mode may help in some environments",[91,71567,71569],{"className":10352,"code":71568,"language":10354,"meta":96,"style":96},"python -X utf8 your_script.py\n",[17,71570,71571],{"__ignoreMap":96},[100,71572,71573,71575,71578,71581],{"class":102,"line":103},[100,71574,95],{"class":10361},[100,71576,71577],{"class":10364}," -X",[100,71579,71580],{"class":209}," utf8",[100,71582,30457],{"class":209},[14,71584,71585],{},"This does not fix invalid characters by itself, but it can help avoid encoding-related confusion in some setups.",[77,71587,71589],{"id":71588},"valid-vs-invalid-examples","Valid vs invalid examples",[14,71591,71592],{},"Python identifiers should be simple and predictable.",[675,71594,71596],{"id":71595},"valid-names","Valid names",[14,71598,71599],{},"These are valid identifiers:",[91,71601,71603],{"className":93,"code":71602,"language":95,"meta":96,"style":96},"name = \"Ana\"\nuser_1 = \"Tom\"\ntotal_count = 3\n",[17,71604,71605,71617,71631],{"__ignoreMap":96},[100,71606,71607,71609,71611,71613,71615],{"class":102,"line":103},[100,71608,1691],{"class":106},[100,71610,111],{"class":110},[100,71612,1708],{"class":205},[100,71614,34819],{"class":209},[100,71616,1714],{"class":205},[100,71618,71619,71622,71624,71626,71629],{"class":102,"line":135},[100,71620,71621],{"class":106},"user_1 ",[100,71623,111],{"class":110},[100,71625,1708],{"class":205},[100,71627,71628],{"class":209},"Tom",[100,71630,1714],{"class":205},[100,71632,71633,71636,71638],{"class":102,"line":142},[100,71634,71635],{"class":106},"total_count ",[100,71637,111],{"class":110},[100,71639,31170],{"class":122},[14,71641,71642],{},"Why they work:",[40,71644,71645,71648,71651],{},[43,71646,71647],{},"They use letters",[43,71649,71650],{},"They use numbers only after the first character",[43,71652,71653],{},"They use underscores",[675,71655,71657],{"id":71656},"invalid-names","Invalid names",[14,71659,71660],{},"These are not valid:",[91,71662,71664],{"className":93,"code":71663,"language":95,"meta":96,"style":96},"user name = \"Tom\"\nuser—name = \"Tom\"\n2name = \"Tom\"\n",[17,71665,71666,71679,71691],{"__ignoreMap":96},[100,71667,71668,71671,71673,71675,71677],{"class":102,"line":103},[100,71669,71670],{"class":106},"user name ",[100,71672,111],{"class":110},[100,71674,1708],{"class":205},[100,71676,71628],{"class":209},[100,71678,1714],{"class":205},[100,71680,71681,71683,71685,71687,71689],{"class":102,"line":135},[100,71682,71328],{"class":106},[100,71684,111],{"class":110},[100,71686,1708],{"class":205},[100,71688,71628],{"class":209},[100,71690,1714],{"class":205},[100,71692,71693,71697,71699,71701,71703],{"class":102,"line":142},[100,71694,71696],{"class":71695},"srjyR","2name",[100,71698,47743],{"class":110},[100,71700,1708],{"class":205},[100,71702,71628],{"class":209},[100,71704,1714],{"class":205},[14,71706,71707],{},"Problems:",[40,71709,71710,71716,71721],{},[43,71711,71712,71715],{},[17,71713,71714],{},"user name"," contains a space",[43,71717,71718,71720],{},[17,71719,71345],{}," contains an em dash",[43,71722,71723,71725],{},[17,71724,71696],{}," starts with a number",[675,71727,71729],{"id":71728},"valid-string-quotes","Valid string quotes",[14,71731,71732],{},"Use normal quotes only:",[91,71734,71736],{"className":93,"code":71735,"language":95,"meta":96,"style":96},"message = \"Hello\"\ntext = 'Python'\n",[17,71737,71738,71750],{"__ignoreMap":96},[100,71739,71740,71742,71744,71746,71748],{"class":102,"line":103},[100,71741,6868],{"class":106},[100,71743,111],{"class":110},[100,71745,1708],{"class":205},[100,71747,7683],{"class":209},[100,71749,1714],{"class":205},[100,71751,71752,71754,71756,71758,71760],{"class":102,"line":135},[100,71753,4331],{"class":106},[100,71755,111],{"class":110},[100,71757,1274],{"class":205},[100,71759,12937],{"class":209},[100,71761,3925],{"class":205},[675,71763,71765],{"id":71764},"invalid-string-quotes","Invalid string quotes",[14,71767,71768],{},"These will cause problems:",[91,71770,71772],{"className":93,"code":71771,"language":95,"meta":96,"style":96},"message = “Hello”\ntext = ‘Python’\n",[17,71773,71774,71783],{"__ignoreMap":96},[100,71775,71776,71778,71780],{"class":102,"line":103},[100,71777,6868],{"class":106},[100,71779,111],{"class":110},[100,71781,71782],{"class":106}," “Hello”\n",[100,71784,71785,71787,71789],{"class":102,"line":135},[100,71786,4331],{"class":106},[100,71788,111],{"class":110},[100,71790,71791],{"class":106}," ‘Python’\n",[675,71793,71795],{"id":71794},"valid-operators","Valid operators",[14,71797,71798],{},"Use standard Python operators from your keyboard:",[91,71800,71802],{"className":93,"code":71801,"language":95,"meta":96,"style":96},"answer = 8 - 3\n",[17,71803,71804],{"__ignoreMap":96},[100,71805,71806,71808,71810,71813,71815],{"class":102,"line":103},[100,71807,69709],{"class":106},[100,71809,111],{"class":110},[100,71811,71812],{"class":122}," 8",[100,71814,1906],{"class":110},[100,71816,31170],{"class":122},[14,71818,71819],{},"Not fancy punctuation copied from formatted text.",[14,71821,71822,71823,290],{},"If you see a more general syntax problem, you may also need to fix ",[295,71824,16612],{"href":16611},[77,71826,71828],{"id":71827},"prevention-tips","Prevention tips",[14,71830,71831],{},"To avoid this error in the future:",[40,71833,71834,71837,71840,71843,71846],{},[43,71835,71836],{},"Write code in a code editor instead of a word processor",[43,71838,71839],{},"Avoid copying code from formatted documents when possible",[43,71841,71842],{},"If you paste code, test it right away",[43,71844,71845],{},"Use a Python-friendly editor that highlights syntax errors clearly",[43,71847,71848],{},"Keep your keyboard layout consistent when writing code",[14,71850,71851],{},"A good habit is to keep identifiers simple:",[40,71853,71854,71857,71860,71863],{},[43,71855,71856],{},"Use letters",[43,71858,71859],{},"Use numbers after the first character",[43,71861,71862],{},"Use underscores",[43,71864,71865],{},"Avoid fancy punctuation",[77,71867,71869],{"id":71868},"related-errors-to-check","Related errors to check",[14,71871,71872],{},"Sometimes this error appears alongside other beginner syntax issues.",[14,71874,40964],{},[40,71876,71877,71881,71887],{},[43,71878,71879],{},[295,71880,16612],{"href":16611},[43,71882,71883],{},[295,71884,71886],{"href":71885},"\u002Ferrors\u002Fsyntaxerror-eol-while-scanning-string-literal-fix\u002F","SyntaxError: EOL while scanning string literal",[43,71888,71889],{},[295,71890,16728],{"href":16727},[77,71892,1514],{"id":1513},[675,71894,71896],{"id":71895},"what-is-an-identifier-in-python","What is an identifier in Python?",[14,71898,71899],{},"An identifier is a name you create in Python, such as a variable name, function name, or class name.",[675,71901,71903],{"id":71902},"can-unicode-characters-be-used-in-python-identifiers","Can Unicode characters be used in Python identifiers?",[14,71905,71906],{},"Some Unicode letters can be valid in identifiers, but many copied symbols and punctuation marks are not. If you are a beginner, it is safest to use letters, numbers, and underscores only.",[675,71908,71910],{"id":71909},"why-does-the-line-look-correct-but-still-fail","Why does the line look correct but still fail?",[14,71912,71913],{},"The bad character may look almost the same as a normal one. This often happens with curly quotes, long dashes, or invisible spaces.",[675,71915,71917],{"id":71916},"what-is-the-fastest-fix","What is the fastest fix?",[14,71919,71920],{},"Delete the part of the line that looks suspicious and type it again manually in your code editor.",[77,71922,1554],{"id":1553},[40,71924,71925,71929,71933,71937,71941],{},[43,71926,71927],{},[295,71928,16612],{"href":16611},[43,71930,71931],{},[295,71932,71886],{"href":71885},[43,71934,71935],{},[295,71936,16728],{"href":16727},[43,71938,71939],{},[295,71940,71174],{"href":31678},[43,71942,71943],{},[295,71944,6080],{"href":14879},[1589,71946,71947],{},"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 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 .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}html pre.shiki code .sbgvK, html code.shiki .sbgvK{--shiki-light:#E2931D;--shiki-default:#6F42C1;--shiki-dark:#B392F0}html pre.shiki code .srdBf, html code.shiki .srdBf{--shiki-light:#F76D47;--shiki-default:#005CC5;--shiki-dark:#79B8FF}html pre.shiki code .sZMiF, html code.shiki .sZMiF{--shiki-light:#E2931D;--shiki-default:#005CC5;--shiki-dark:#79B8FF}html pre.shiki code .stzsN, html code.shiki .stzsN{--shiki-light:#91B859;--shiki-default:#005CC5;--shiki-dark:#79B8FF}html pre.shiki code .srjyR, html code.shiki .srjyR{--shiki-light:#90A4AE;--shiki-light-font-style:inherit;--shiki-default:#B31D28;--shiki-default-font-style:italic;--shiki-dark:#FDAEB7;--shiki-dark-font-style:italic}",{"title":96,"searchDepth":135,"depth":135,"links":71949},[71950,71951,71952,71955,71960,71961,71971,71978,71979,71980,71986],{"id":79,"depth":135,"text":80},{"id":5880,"depth":135,"text":5881},{"id":71111,"depth":135,"text":71112,"children":71953},[71954],{"id":71150,"depth":142,"text":71151},{"id":11030,"depth":135,"text":11031,"children":71956},[71957,71958,71959],{"id":71179,"depth":142,"text":71180},{"id":71240,"depth":142,"text":71241},{"id":71314,"depth":142,"text":71315},{"id":6243,"depth":135,"text":6244},{"id":71425,"depth":135,"text":71426,"children":71962},[71963,71964,71966,71967,71968,71969,71970],{"id":71432,"depth":142,"text":71433},{"id":71452,"depth":142,"text":71965},"2. Check the caret ^",{"id":71499,"depth":142,"text":71500},{"id":71523,"depth":142,"text":71524},{"id":71533,"depth":142,"text":71534},{"id":71557,"depth":142,"text":71558},{"id":71564,"depth":142,"text":71565},{"id":71588,"depth":135,"text":71589,"children":71972},[71973,71974,71975,71976,71977],{"id":71595,"depth":142,"text":71596},{"id":71656,"depth":142,"text":71657},{"id":71728,"depth":142,"text":71729},{"id":71764,"depth":142,"text":71765},{"id":71794,"depth":142,"text":71795},{"id":71827,"depth":135,"text":71828},{"id":71868,"depth":135,"text":71869},{"id":1513,"depth":135,"text":1514,"children":71981},[71982,71983,71984,71985],{"id":71895,"depth":142,"text":71896},{"id":71902,"depth":142,"text":71903},{"id":71909,"depth":142,"text":71910},{"id":71916,"depth":142,"text":71917},{"id":1553,"depth":135,"text":1554},"Master syntaxerror invalid character in identifier fix in our comprehensive Python beginner guide.",{},"\u002Ferrors\u002Fsyntaxerror-invalid-character-in-identifier-fix",{"title":70929,"description":71987},"errors\u002Fsyntaxerror-invalid-character-in-identifier-fix","MsHaSgTClzVhA30MNRrtHOs-xgqpHFdiyqKgbgtH_Yc",{"id":71994,"title":71995,"body":71996,"description":73000,"extension":1623,"meta":73001,"navigation":138,"path":73002,"seo":73003,"stem":73004,"__hash__":73005},"content\u002Ferrors\u002Fsyntaxerror-invalid-syntax-fix.md","SyntaxError: invalid syntax (Fix)",{"type":7,"value":71997,"toc":72974},[71998,72001,72006,72009,72020,72022,72144,72149,72151,72156,72170,72175,72177,72179,72211,72214,72243,72249,72251,72257,72261,72303,72309,72311,72354,72358,72361,72375,72380,72384,72387,72429,72432,72436,72466,72470,72497,72501,72536,72540,72542,72615,72617,72686,72688,72691,72711,72714,72724,72726,72740,72745,72749,72754,72788,72791,72838,72845,72847,72891,72893,72896,72919,72921,72925,72928,72932,72937,72941,72944,72948,72951,72953,72971],[10,71999,71995],{"id":72000},"syntaxerror-invalid-syntax-fix",[14,72002,72003,72005],{},[17,72004,16612],{}," means Python could not understand part of your code.",[14,72007,72008],{},"This is one of the most common beginner errors. It usually happens because something is missing, extra, or written in the wrong form.",[14,72010,72011,72012,72016,72017,290],{},"The tricky part is this: Python often points to the place where it ",[72013,72014,72015],"em",{},"noticed"," the problem, not the place where the mistake actually started. That is why you should always check the highlighted line ",[22,72018,72019],{},"and the line just before it",[77,72021,80],{"id":79},[91,72023,72025],{"className":93,"code":72024,"language":95,"meta":96,"style":96},"# Check the line Python points to and the line just before it\n# Common fixes:\n\n# 1. Add a missing colon\nif x > 5:\n    print(x)\n\n# 2. Close missing quotes or brackets\nname = \"Alice\"\nitems = [1, 2, 3]\n\n# 3. Fix misspelled keywords\nfor i in range(3):\n    print(i)\n",[17,72026,72027,72032,72037,72041,72046,72058,72068,72072,72077,72089,72109,72113,72118,72134],{"__ignoreMap":96},[100,72028,72029],{"class":102,"line":103},[100,72030,72031],{"class":414},"# Check the line Python points to and the line just before it\n",[100,72033,72034],{"class":102,"line":135},[100,72035,72036],{"class":414},"# Common fixes:\n",[100,72038,72039],{"class":102,"line":142},[100,72040,139],{"emptyLinePlaceholder":138},[100,72042,72043],{"class":102,"line":152},[100,72044,72045],{"class":414},"# 1. Add a missing colon\n",[100,72047,72048,72050,72052,72054,72056],{"class":102,"line":164},[100,72049,2736],{"class":145},[100,72051,59659],{"class":106},[100,72053,1918],{"class":110},[100,72055,31164],{"class":122},[100,72057,149],{"class":118},[100,72059,72060,72062,72064,72066],{"class":102,"line":185},[100,72061,200],{"class":114},[100,72063,170],{"class":118},[100,72065,39024],{"class":178},[100,72067,215],{"class":118},[100,72069,72070],{"class":102,"line":197},[100,72071,139],{"emptyLinePlaceholder":138},[100,72073,72074],{"class":102,"line":771},[100,72075,72076],{"class":414},"# 2. Close missing quotes or brackets\n",[100,72078,72079,72081,72083,72085,72087],{"class":102,"line":787},[100,72080,1691],{"class":106},[100,72082,111],{"class":110},[100,72084,1708],{"class":205},[100,72086,15343],{"class":209},[100,72088,1714],{"class":205},[100,72090,72091,72093,72095,72097,72099,72101,72103,72105,72107],{"class":102,"line":5816},[100,72092,3095],{"class":106},[100,72094,111],{"class":110},[100,72096,594],{"class":118},[100,72098,123],{"class":122},[100,72100,126],{"class":118},[100,72102,129],{"class":122},[100,72104,126],{"class":118},[100,72106,1405],{"class":122},[100,72108,603],{"class":118},[100,72110,72111],{"class":102,"line":5833},[100,72112,139],{"emptyLinePlaceholder":138},[100,72114,72115],{"class":102,"line":5848},[100,72116,72117],{"class":414},"# 3. Fix misspelled keywords\n",[100,72119,72120,72122,72124,72126,72128,72130,72132],{"class":102,"line":5855},[100,72121,71],{"class":145},[100,72123,29838],{"class":106},[100,72125,617],{"class":145},[100,72127,29843],{"class":114},[100,72129,170],{"class":118},[100,72131,11541],{"class":122},[100,72133,522],{"class":118},[100,72135,72136,72138,72140,72142],{"class":102,"line":62688},[100,72137,200],{"class":114},[100,72139,170],{"class":118},[100,72141,29858],{"class":178},[100,72143,215],{"class":118},[66455,72145,72146],{},[14,72147,72148],{},"Python often shows the error where it noticed the problem, not where the mistake started. Always inspect the previous line too.",[77,72150,5881],{"id":5880},[14,72152,3277,72153,72155],{},[17,72154,16612],{},", Python is telling you:",[40,72157,72158,72161,72164,72167],{},[43,72159,72160],{},"It could not understand part of your code.",[43,72162,72163],{},"The code breaks Python's grammar rules.",[43,72165,72166],{},"The program stops before it fully runs.",[43,72168,72169],{},"The message is broad, so you need to inspect nearby lines carefully.",[14,72171,72172,72173,290],{},"If you are new to Python, it helps to review ",[295,72174,71174],{"href":30532},[77,72176,25091],{"id":25090},[14,72178,3971],{},[40,72180,72181,72196,72199,72202,72205,72208],{},[43,72182,72183,72184,5870,72186,5870,72188,5870,72190,5870,72192,5894,72194],{},"A colon is missing after ",[17,72185,2736],{},[17,72187,71],{},[17,72189,721],{},[17,72191,1078],{},[17,72193,3417],{},[17,72195,146],{},[43,72197,72198],{},"A quote, parenthesis, bracket, or brace is not closed",[43,72200,72201],{},"A keyword is misspelled",[43,72203,72204],{},"Operators are used incorrectly",[43,72206,72207],{},"Two statements are combined in an invalid way",[43,72209,72210],{},"Indentation changes lead to related syntax problems",[14,72212,72213],{},"Very common examples are:",[40,72215,72216,72221,72227,72233,72240],{},[43,72217,72218,72219,9320],{},"Missing colon after an ",[17,72220,2736],{},[43,72222,72223,72224],{},"Unclosed string like ",[17,72225,72226],{},"\"hello",[43,72228,72229,72230],{},"Unclosed list like ",[17,72231,72232],{},"[1, 2, 3",[43,72234,18323,72235,15957,72238],{},[17,72236,72237],{},"else if",[17,72239,30862],{},[43,72241,72242],{},"Adding an extra symbol that does not belong",[14,72244,72245,72246,290],{},"If the problem turns out to be spacing and code blocks, see ",[295,72247,72248],{"href":29936},"Python indentation rules",[77,72250,11031],{"id":11030},[14,72252,72253,72254,72256],{},"A missing colon after an ",[17,72255,2736],{}," statement is a classic cause.",[675,72258,72260],{"id":72259},"code-with-the-error","Code with the error",[91,72262,72264],{"className":93,"code":72263,"language":95,"meta":96,"style":96},"x = 10\n\nif x > 5\n    print(\"x is greater than 5\")\n",[17,72265,72266,72274,72278,72288],{"__ignoreMap":96},[100,72267,72268,72270,72272],{"class":102,"line":103},[100,72269,54291],{"class":106},[100,72271,111],{"class":110},[100,72273,2181],{"class":122},[100,72275,72276],{"class":102,"line":135},[100,72277,139],{"emptyLinePlaceholder":138},[100,72279,72280,72282,72284,72286],{"class":102,"line":142},[100,72281,2736],{"class":145},[100,72283,59659],{"class":106},[100,72285,1918],{"class":110},[100,72287,37580],{"class":122},[100,72289,72290,72292,72294,72296,72299,72301],{"class":102,"line":152},[100,72291,200],{"class":114},[100,72293,170],{"class":118},[100,72295,206],{"class":205},[100,72297,72298],{"class":209},"x is greater than 5",[100,72300,206],{"class":205},[100,72302,215],{"class":118},[14,72304,72305,72306,72308],{},"Python will raise a syntax error because the ",[17,72307,2736],{}," line should end with a colon.",[675,72310,30958],{"id":30957},[91,72312,72314],{"className":93,"code":72313,"language":95,"meta":96,"style":96},"x = 10\n\nif x > 5:\n    print(\"x is greater than 5\")\n",[17,72315,72316,72324,72328,72340],{"__ignoreMap":96},[100,72317,72318,72320,72322],{"class":102,"line":103},[100,72319,54291],{"class":106},[100,72321,111],{"class":110},[100,72323,2181],{"class":122},[100,72325,72326],{"class":102,"line":135},[100,72327,139],{"emptyLinePlaceholder":138},[100,72329,72330,72332,72334,72336,72338],{"class":102,"line":142},[100,72331,2736],{"class":145},[100,72333,59659],{"class":106},[100,72335,1918],{"class":110},[100,72337,31164],{"class":122},[100,72339,149],{"class":118},[100,72341,72342,72344,72346,72348,72350,72352],{"class":102,"line":152},[100,72343,200],{"class":114},[100,72345,170],{"class":118},[100,72347,206],{"class":205},[100,72349,72298],{"class":209},[100,72351,206],{"class":205},[100,72353,215],{"class":118},[675,72355,72357],{"id":72356},"why-this-works","Why this works",[14,72359,72360],{},"The colon tells Python that a block of code will follow.",[14,72362,72363,72364,5870,72366,5870,72368,5870,72370,5629,72372,72374],{},"Statements like ",[17,72365,2736],{},[17,72367,71],{},[17,72369,721],{},[17,72371,1078],{},[17,72373,3417],{}," need a colon at the end.",[14,72376,72377,72378,290],{},"If this is your exact problem, read ",[295,72379,29615],{"href":29614},[77,72381,72383],{"id":72382},"common-fixes","Common fixes",[14,72385,72386],{},"Try these fixes near the highlighted line:",[40,72388,72389,72408,72411,72420,72423,72426],{},[43,72390,72391,72392,5870,72394,5870,72396,5870,72398,5870,72400,5870,72402,5870,72404,5629,72406],{},"Add missing colons after block starters like ",[17,72393,2736],{},[17,72395,71],{},[17,72397,721],{},[17,72399,1078],{},[17,72401,3417],{},[17,72403,146],{},[17,72405,188],{},[17,72407,4944],{},[43,72409,72410],{},"Close all quotes and brackets",[43,72412,72413,72414,5870,72416,5629,72418],{},"Check for misspelled Python keywords like ",[17,72415,30862],{},[17,72417,721],{},[17,72419,1053],{},[43,72421,72422],{},"Separate statements properly",[43,72424,72425],{},"Remove extra symbols that do not belong",[43,72427,72428],{},"Look at the line above the one Python highlights",[14,72430,72431],{},"Here are a few short examples.",[675,72433,72435],{"id":72434},"_1-add-a-missing-colon","1. Add a missing colon",[91,72437,72438],{"className":93,"code":70493,"language":95,"meta":96,"style":96},[17,72439,72440,72456],{"__ignoreMap":96},[100,72441,72442,72444,72446,72448,72450,72452,72454],{"class":102,"line":103},[100,72443,71],{"class":145},[100,72445,29838],{"class":106},[100,72447,617],{"class":145},[100,72449,29843],{"class":114},[100,72451,170],{"class":118},[100,72453,11541],{"class":122},[100,72455,522],{"class":118},[100,72457,72458,72460,72462,72464],{"class":102,"line":135},[100,72459,200],{"class":114},[100,72461,170],{"class":118},[100,72463,29858],{"class":178},[100,72465,215],{"class":118},[675,72467,72469],{"id":72468},"_2-close-a-string-properly","2. Close a string properly",[91,72471,72473],{"className":93,"code":72472,"language":95,"meta":96,"style":96},"name = \"Alice\"\nprint(name)\n",[17,72474,72475,72487],{"__ignoreMap":96},[100,72476,72477,72479,72481,72483,72485],{"class":102,"line":103},[100,72478,1691],{"class":106},[100,72480,111],{"class":110},[100,72482,1708],{"class":205},[100,72484,15343],{"class":209},[100,72486,1714],{"class":205},[100,72488,72489,72491,72493,72495],{"class":102,"line":135},[100,72490,372],{"class":114},[100,72492,170],{"class":118},[100,72494,2853],{"class":178},[100,72496,215],{"class":118},[675,72498,72500],{"id":72499},"_3-close-brackets-properly","3. Close brackets properly",[91,72502,72504],{"className":93,"code":72503,"language":95,"meta":96,"style":96},"numbers = [1, 2, 3]\nprint(numbers)\n",[17,72505,72506,72526],{"__ignoreMap":96},[100,72507,72508,72510,72512,72514,72516,72518,72520,72522,72524],{"class":102,"line":103},[100,72509,107],{"class":106},[100,72511,111],{"class":110},[100,72513,594],{"class":118},[100,72515,123],{"class":122},[100,72517,126],{"class":118},[100,72519,129],{"class":122},[100,72521,126],{"class":118},[100,72523,1405],{"class":122},[100,72525,603],{"class":118},[100,72527,72528,72530,72532,72534],{"class":102,"line":135},[100,72529,372],{"class":114},[100,72531,170],{"class":118},[100,72533,179],{"class":178},[100,72535,215],{"class":118},[675,72537,72539],{"id":72538},"_4-fix-a-misspelled-keyword","4. Fix a misspelled keyword",[14,72541,9709],{},[91,72543,72545],{"className":93,"code":72544,"language":95,"meta":96,"style":96},"x = 5\n\nif x > 10:\n    print(\"big\")\nelse if x > 3:\n    print(\"medium\")\n",[17,72546,72547,72555,72559,72571,72586,72600],{"__ignoreMap":96},[100,72548,72549,72551,72553],{"class":102,"line":103},[100,72550,54291],{"class":106},[100,72552,111],{"class":110},[100,72554,37580],{"class":122},[100,72556,72557],{"class":102,"line":135},[100,72558,139],{"emptyLinePlaceholder":138},[100,72560,72561,72563,72565,72567,72569],{"class":102,"line":142},[100,72562,2736],{"class":145},[100,72564,59659],{"class":106},[100,72566,1918],{"class":110},[100,72568,6550],{"class":122},[100,72570,149],{"class":118},[100,72572,72573,72575,72577,72579,72582,72584],{"class":102,"line":152},[100,72574,200],{"class":114},[100,72576,170],{"class":118},[100,72578,206],{"class":205},[100,72580,72581],{"class":209},"big",[100,72583,206],{"class":205},[100,72585,215],{"class":118},[100,72587,72588,72590,72592,72594,72596,72598],{"class":102,"line":164},[100,72589,4944],{"class":145},[100,72591,11904],{"class":145},[100,72593,59659],{"class":106},[100,72595,1918],{"class":110},[100,72597,1405],{"class":122},[100,72599,149],{"class":118},[100,72601,72602,72604,72606,72608,72611,72613],{"class":102,"line":185},[100,72603,200],{"class":114},[100,72605,170],{"class":118},[100,72607,206],{"class":205},[100,72609,72610],{"class":209},"medium",[100,72612,206],{"class":205},[100,72614,215],{"class":118},[14,72616,9750],{},[91,72618,72620],{"className":93,"code":72619,"language":95,"meta":96,"style":96},"x = 5\n\nif x > 10:\n    print(\"big\")\nelif x > 3:\n    print(\"medium\")\n",[17,72621,72622,72630,72634,72646,72660,72672],{"__ignoreMap":96},[100,72623,72624,72626,72628],{"class":102,"line":103},[100,72625,54291],{"class":106},[100,72627,111],{"class":110},[100,72629,37580],{"class":122},[100,72631,72632],{"class":102,"line":135},[100,72633,139],{"emptyLinePlaceholder":138},[100,72635,72636,72638,72640,72642,72644],{"class":102,"line":142},[100,72637,2736],{"class":145},[100,72639,59659],{"class":106},[100,72641,1918],{"class":110},[100,72643,6550],{"class":122},[100,72645,149],{"class":118},[100,72647,72648,72650,72652,72654,72656,72658],{"class":102,"line":152},[100,72649,200],{"class":114},[100,72651,170],{"class":118},[100,72653,206],{"class":205},[100,72655,72581],{"class":209},[100,72657,206],{"class":205},[100,72659,215],{"class":118},[100,72661,72662,72664,72666,72668,72670],{"class":102,"line":164},[100,72663,30862],{"class":145},[100,72665,59659],{"class":106},[100,72667,1918],{"class":110},[100,72669,1405],{"class":122},[100,72671,149],{"class":118},[100,72673,72674,72676,72678,72680,72682,72684],{"class":102,"line":185},[100,72675,200],{"class":114},[100,72677,170],{"class":118},[100,72679,206],{"class":205},[100,72681,72610],{"class":209},[100,72683,206],{"class":205},[100,72685,215],{"class":118},[77,72687,22662],{"id":22661},[14,72689,72690],{},"Use this process to find the real problem faster:",[3282,72692,72693,72696,72699,72702,72705,72708],{},[43,72694,72695],{},"Read the full error message carefully.",[43,72697,72698],{},"Check the exact line with the caret marker.",[43,72700,72701],{},"Check the line before it for missing punctuation.",[43,72703,72704],{},"Match every opening bracket with a closing bracket.",[43,72706,72707],{},"Match every opening quote with a closing quote.",[43,72709,72710],{},"Run the code again after each small fix.",[14,72712,72713],{},"You can also run your file from the terminal:",[91,72715,72716],{"className":10352,"code":31396,"language":10354,"meta":96,"style":96},[17,72717,72718],{"__ignoreMap":96},[100,72719,72720,72722],{"class":102,"line":103},[100,72721,95],{"class":10361},[100,72723,30457],{"class":209},[14,72725,70644],{},[91,72727,72728],{"className":10352,"code":31421,"language":10354,"meta":96,"style":96},[17,72729,72730],{"__ignoreMap":96},[100,72731,72732,72734,72736,72738],{"class":102,"line":103},[100,72733,95],{"class":10361},[100,72735,26622],{"class":10364},[100,72737,29530],{"class":209},[100,72739,30457],{"class":209},[14,72741,72742,72743,290],{},"If you want a broader process for finding problems, see this ",[295,72744,7077],{"href":3852},[77,72746,72748],{"id":72747},"mistakes-beginners-often-make","Mistakes beginners often make",[14,72750,72751,72752,89],{},"These mistakes often lead to ",[17,72753,16612],{},[40,72755,72756,72769,72776,72779,72785],{},[43,72757,72758,72759,5870,72761,5870,72763,5870,72765,5629,72767],{},"Forgetting the colon in ",[17,72760,2736],{},[17,72762,71],{},[17,72764,721],{},[17,72766,1078],{},[17,72768,3417],{},[43,72770,1357,72771,15957,72773,72775],{},[17,72772,111],{},[17,72774,32169],{}," in a condition",[43,72777,72778],{},"Leaving a string unfinished",[43,72780,18323,72781,15957,72783],{},[17,72782,72237],{},[17,72784,30862],{},[43,72786,72787],{},"Using natural language instead of Python syntax",[14,72789,72790],{},"For example, this is invalid Python:",[91,72792,72794],{"className":93,"code":72793,"language":95,"meta":96,"style":96},"age = 18\n\nif age is greater than 16:\n    print(\"You can apply\")\n",[17,72795,72796,72804,72808,72823],{"__ignoreMap":96},[100,72797,72798,72800,72802],{"class":102,"line":103},[100,72799,1664],{"class":106},[100,72801,111],{"class":110},[100,72803,1682],{"class":122},[100,72805,72806],{"class":102,"line":135},[100,72807,139],{"emptyLinePlaceholder":138},[100,72809,72810,72812,72814,72816,72819,72821],{"class":102,"line":142},[100,72811,2736],{"class":145},[100,72813,1676],{"class":106},[100,72815,4918],{"class":110},[100,72817,72818],{"class":106}," greater than ",[100,72820,10180],{"class":122},[100,72822,149],{"class":118},[100,72824,72825,72827,72829,72831,72834,72836],{"class":102,"line":152},[100,72826,200],{"class":114},[100,72828,170],{"class":118},[100,72830,206],{"class":205},[100,72832,72833],{"class":209},"You can apply",[100,72835,206],{"class":205},[100,72837,215],{"class":118},[14,72839,72840,72841,72844],{},"Python does not understand ",[17,72842,72843],{},"is greater than"," as a condition.",[14,72846,6031],{},[91,72848,72850],{"className":93,"code":72849,"language":95,"meta":96,"style":96},"age = 18\n\nif age > 16:\n    print(\"You can apply\")\n",[17,72851,72852,72860,72864,72877],{"__ignoreMap":96},[100,72853,72854,72856,72858],{"class":102,"line":103},[100,72855,1664],{"class":106},[100,72857,111],{"class":110},[100,72859,1682],{"class":122},[100,72861,72862],{"class":102,"line":135},[100,72863,139],{"emptyLinePlaceholder":138},[100,72865,72866,72868,72870,72872,72875],{"class":102,"line":142},[100,72867,2736],{"class":145},[100,72869,1676],{"class":106},[100,72871,1918],{"class":110},[100,72873,72874],{"class":122}," 16",[100,72876,149],{"class":118},[100,72878,72879,72881,72883,72885,72887,72889],{"class":102,"line":152},[100,72880,200],{"class":114},[100,72882,170],{"class":118},[100,72884,206],{"class":205},[100,72886,72833],{"class":209},[100,72888,206],{"class":205},[100,72890,215],{"class":118},[77,72892,30537],{"id":30536},[14,72894,72895],{},"Sometimes the message changes once you get closer to the real problem. Related errors include:",[40,72897,72898,72902,72907,72911,72915],{},[43,72899,72900],{},[295,72901,29615],{"href":29614},[43,72903,72904],{},[295,72905,69865],{"href":72906},"\u002Ferrors\u002Fsyntaxerror-unexpected-eof-while-parsing-fix",[43,72908,72909],{},[295,72910,71886],{"href":70057},[43,72912,72913],{},[295,72914,28816],{"href":29781},[43,72916,72917],{},[295,72918,16618],{"href":29596},[77,72920,1514],{"id":1513},[675,72922,72924],{"id":72923},"why-does-python-point-to-the-wrong-line","Why does Python point to the wrong line?",[14,72926,72927],{},"Python points to where it noticed the syntax problem. The real cause is often on the same line or the line before it.",[675,72929,72931],{"id":72930},"is-syntaxerror-the-same-as-indentationerror","Is SyntaxError the same as IndentationError?",[14,72933,14506,72934,72936],{},[17,72935,16508],{}," is a more specific syntax-related error about spacing and code blocks.",[675,72938,72940],{"id":72939},"can-a-missing-bracket-cause-invalid-syntax","Can a missing bracket cause invalid syntax?",[14,72942,72943],{},"Yes. Missing brackets, quotes, or braces are very common causes of this error.",[675,72945,72947],{"id":72946},"how-do-i-fix-invalid-syntax-quickly","How do I fix invalid syntax quickly?",[14,72949,72950],{},"Check for missing colons, unclosed quotes, unclosed brackets, and misspelled keywords near the highlighted line.",[77,72952,1554],{"id":1553},[40,72954,72955,72959,72963,72967],{},[43,72956,72957],{},[295,72958,71174],{"href":30532},[43,72960,72961],{},[295,72962,28947],{"href":29936},[43,72964,72965],{},[295,72966,5690],{"href":3852},[43,72968,72969],{},[295,72970,69865],{"href":72906},[1589,72972,72973],{},"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 .sVHd0, html code.shiki .sVHd0{--shiki-light:#39ADB5;--shiki-light-font-style:italic;--shiki-default:#D73A49;--shiki-default-font-style:inherit;--shiki-dark:#F97583;--shiki-dark-font-style:inherit}html pre.shiki code .su5hD, html code.shiki .su5hD{--shiki-light:#90A4AE;--shiki-default:#24292E;--shiki-dark:#E1E4E8}html pre.shiki code .smGrS, html code.shiki .smGrS{--shiki-light:#39ADB5;--shiki-default:#D73A49;--shiki-dark:#F97583}html pre.shiki code .srdBf, html code.shiki .srdBf{--shiki-light:#F76D47;--shiki-default:#005CC5;--shiki-dark:#79B8FF}html pre.shiki code .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 .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 .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 .sbgvK, html code.shiki .sbgvK{--shiki-light:#E2931D;--shiki-default:#6F42C1;--shiki-dark:#B392F0}html pre.shiki code .stzsN, html code.shiki .stzsN{--shiki-light:#91B859;--shiki-default:#005CC5;--shiki-dark:#79B8FF}",{"title":96,"searchDepth":135,"depth":135,"links":72975},[72976,72977,72978,72979,72984,72990,72991,72992,72993,72999],{"id":79,"depth":135,"text":80},{"id":5880,"depth":135,"text":5881},{"id":25090,"depth":135,"text":25091},{"id":11030,"depth":135,"text":11031,"children":72980},[72981,72982,72983],{"id":72259,"depth":142,"text":72260},{"id":30957,"depth":142,"text":30958},{"id":72356,"depth":142,"text":72357},{"id":72382,"depth":135,"text":72383,"children":72985},[72986,72987,72988,72989],{"id":72434,"depth":142,"text":72435},{"id":72468,"depth":142,"text":72469},{"id":72499,"depth":142,"text":72500},{"id":72538,"depth":142,"text":72539},{"id":22661,"depth":135,"text":22662},{"id":72747,"depth":135,"text":72748},{"id":30536,"depth":135,"text":30537},{"id":1513,"depth":135,"text":1514,"children":72994},[72995,72996,72997,72998],{"id":72923,"depth":142,"text":72924},{"id":72930,"depth":142,"text":72931},{"id":72939,"depth":142,"text":72940},{"id":72946,"depth":142,"text":72947},{"id":1553,"depth":135,"text":1554},"Master syntaxerror invalid syntax fix in our comprehensive Python beginner guide.",{},"\u002Ferrors\u002Fsyntaxerror-invalid-syntax-fix",{"title":71995,"description":73000},"errors\u002Fsyntaxerror-invalid-syntax-fix","RIOB42O_Na6AGGhOGbTkynOH_dUq_Js_91g0LLTq2Gs",{"id":73007,"title":73008,"body":73009,"description":74008,"extension":1623,"meta":74009,"navigation":138,"path":29614,"seo":74010,"stem":74011,"__hash__":74012},"content\u002Ferrors\u002Fsyntaxerror-missing-colon-fix.md","SyntaxError: missing colon (Fix)",{"type":7,"value":73010,"toc":73988},[73011,73014,73019,73021,73052,73170,73173,73175,73181,73184,73206,73208,73236,73239,73241,73247,73250,73300,73303,73307,73310,73367,73378,73380,73383,73424,73427,73478,73481,73484,73527,73529,73535,73537,73539,73558,73560,73595,73598,73600,73635,73637,73661,73664,73701,73703,73740,73742,73748,73752,73755,73789,73791,73802,73805,73820,73827,73831,73834,73836,73849,73854,73856,73858,73898,73900,73904,73913,73917,73920,73924,73927,73933,73940,73944,73947,73949,73985],[10,73012,73008],{"id":73013},"syntaxerror-missing-colon-fix",[14,73015,31738,73016,73018],{},[17,73017,29615],{},". This page helps beginners find where the colon is missing, understand why Python needs it, and correct the code quickly.",[77,73020,80],{"id":79},[14,73022,73023,73024,5870,73026,5870,73028,5870,73030,5870,73032,5870,73034,5870,73036,5870,73038,5870,73040,5870,73042,5870,73044,5629,73046,2558,73049,290],{},"Add a colon at the end of lines that start a block, such as ",[17,73025,2736],{},[17,73027,30862],{},[17,73029,4944],{},[17,73031,71],{},[17,73033,721],{},[17,73035,1078],{},[17,73037,3417],{},[17,73039,146],{},[17,73041,188],{},[17,73043,28983],{},[17,73045,17521],{},[17,73047,73048],{},"match",[17,73050,73051],{},"case",[91,73053,73055],{"className":93,"code":73054,"language":95,"meta":96,"style":96},"if age > 18:\n    print(\"Adult\")\n\nfor item in [1, 2, 3]:\n    print(item)\n\ndef greet(name):\n    print(\"Hello\", name)\n\nclass Person:\n    pass\n",[17,73056,73057,73069,73084,73088,73110,73120,73124,73136,73154,73158,73166],{"__ignoreMap":96},[100,73058,73059,73061,73063,73065,73067],{"class":102,"line":103},[100,73060,2736],{"class":145},[100,73062,1676],{"class":106},[100,73064,1918],{"class":110},[100,73066,1869],{"class":122},[100,73068,149],{"class":118},[100,73070,73071,73073,73075,73077,73080,73082],{"class":102,"line":135},[100,73072,200],{"class":114},[100,73074,170],{"class":118},[100,73076,206],{"class":205},[100,73078,73079],{"class":209},"Adult",[100,73081,206],{"class":205},[100,73083,215],{"class":118},[100,73085,73086],{"class":102,"line":142},[100,73087,139],{"emptyLinePlaceholder":138},[100,73089,73090,73092,73094,73096,73098,73100,73102,73104,73106,73108],{"class":102,"line":152},[100,73091,71],{"class":145},[100,73093,7568],{"class":106},[100,73095,617],{"class":145},[100,73097,594],{"class":118},[100,73099,123],{"class":122},[100,73101,126],{"class":118},[100,73103,129],{"class":122},[100,73105,126],{"class":118},[100,73107,1405],{"class":122},[100,73109,29073],{"class":118},[100,73111,73112,73114,73116,73118],{"class":102,"line":164},[100,73113,200],{"class":114},[100,73115,170],{"class":118},[100,73117,7555],{"class":178},[100,73119,215],{"class":118},[100,73121,73122],{"class":102,"line":185},[100,73123,139],{"emptyLinePlaceholder":138},[100,73125,73126,73128,73130,73132,73134],{"class":102,"line":197},[100,73127,1078],{"class":1077},[100,73129,2848],{"class":1081},[100,73131,170],{"class":118},[100,73133,2853],{"class":2519},[100,73135,522],{"class":118},[100,73137,73138,73140,73142,73144,73146,73148,73150,73152],{"class":102,"line":771},[100,73139,200],{"class":114},[100,73141,170],{"class":118},[100,73143,206],{"class":205},[100,73145,7683],{"class":209},[100,73147,206],{"class":205},[100,73149,126],{"class":118},[100,73151,1703],{"class":178},[100,73153,215],{"class":118},[100,73155,73156],{"class":102,"line":787},[100,73157,139],{"emptyLinePlaceholder":138},[100,73159,73160,73162,73164],{"class":102,"line":5816},[100,73161,3417],{"class":1077},[100,73163,71096],{"class":10361},[100,73165,149],{"class":118},[100,73167,73168],{"class":102,"line":5833},[100,73169,16214],{"class":145},[14,73171,73172],{},"If you add the colon, also make sure the next line is indented.",[77,73174,5881],{"id":5880},[14,73176,73177,73178,73180],{},"Python expected a colon ",[17,73179,89],{}," at the end of a statement that starts an indented block.",[14,73182,73183],{},"A block is a group of indented lines under statements like:",[40,73185,73186,73190,73194,73198,73202],{},[43,73187,73188],{},[17,73189,2736],{},[43,73191,73192],{},[17,73193,71],{},[43,73195,73196],{},[17,73197,721],{},[43,73199,73200],{},[17,73201,1078],{},[43,73203,73204],{},[17,73205,3417],{},[14,73207,580],{},[91,73209,73211],{"className":93,"code":73210,"language":95,"meta":96,"style":96},"if True:\n    print(\"This line is inside the if block\")\n",[17,73212,73213,73221],{"__ignoreMap":96},[100,73214,73215,73217,73219],{"class":102,"line":103},[100,73216,2736],{"class":145},[100,73218,159],{"class":158},[100,73220,149],{"class":118},[100,73222,73223,73225,73227,73229,73232,73234],{"class":102,"line":135},[100,73224,200],{"class":114},[100,73226,170],{"class":118},[100,73228,206],{"class":205},[100,73230,73231],{"class":209},"This line is inside the if block",[100,73233,206],{"class":205},[100,73235,215],{"class":118},[14,73237,73238],{},"This is a syntax error, which means Python cannot run the code until you fix it.",[77,73240,7741],{"id":7740},[14,73242,73243,73244,73246],{},"This error usually happens when you forget ",[17,73245,89],{}," after a line that should start a block.",[14,73248,73249],{},"Common cases include:",[40,73251,73252,73264,73273,73280,73287],{},[43,73253,73254,73255,73257,73258,5870,73260,5894,73262,11037],{},"You forgot ",[17,73256,89],{}," after an ",[17,73259,2736],{},[17,73261,30862],{},[17,73263,4944],{},[43,73265,73254,73266,73268,73269,3178,73271,290],{},[17,73267,89],{}," after a loop like ",[17,73270,71],{},[17,73272,721],{},[43,73274,73254,73275,73277,73278,290],{},[17,73276,89],{}," after defining a function with ",[17,73279,1078],{},[43,73281,73254,73282,73284,73285,290],{},[17,73283,89],{}," after defining a class with ",[17,73286,3417],{},[43,73288,73254,73289,73291,73292,5870,73294,5870,73296,5894,73298,290],{},[17,73290,89],{}," after ",[17,73293,146],{},[17,73295,188],{},[17,73297,28983],{},[17,73299,17521],{},[14,73301,73302],{},"Sometimes Python highlights the line where it noticed the problem, but the real mistake is on the line above it.",[77,73304,73306],{"id":73305},"common-places-where-a-colon-is-required","Common places where a colon is required",[14,73308,73309],{},"Here are some common Python statements that must end with a colon:",[40,73311,73312,73317,73322,73327,73332,73337,73342,73347,73352,73357,73362],{},[43,73313,73314],{},[17,73315,73316],{},"if condition:",[43,73318,73319],{},[17,73320,73321],{},"elif condition:",[43,73323,73324],{},[17,73325,73326],{},"else:",[43,73328,73329],{},[17,73330,73331],{},"for item in items:",[43,73333,73334],{},[17,73335,73336],{},"while condition:",[43,73338,73339],{},[17,73340,73341],{},"def my_function():",[43,73343,73344],{},[17,73345,73346],{},"class MyClass:",[43,73348,73349],{},[17,73350,73351],{},"try:",[43,73353,73354],{},[17,73355,73356],{},"except ValueError:",[43,73358,73359],{},[17,73360,73361],{},"finally:",[43,73363,73364],{},[17,73365,73366],{},"with open(\"file.txt\") as f:",[14,73368,73369,73370,5870,73372,5629,73376,290],{},"If you are learning these statements for the first time, see ",[295,73371,29744],{"href":29185},[295,73373,73375],{"href":73374},"\u002Flearn\u002Fpython-for-loops-explained\u002F","Python for loops explained",[295,73377,29749],{"href":29189},[77,73379,11031],{"id":11030},[14,73381,73382],{},"Here is a simple example with a missing colon:",[91,73384,73386],{"className":93,"code":73385,"language":95,"meta":96,"style":96},"age = 20\n\nif age > 18\n    print(\"Adult\")\n",[17,73387,73388,73396,73400,73410],{"__ignoreMap":96},[100,73389,73390,73392,73394],{"class":102,"line":103},[100,73391,1664],{"class":106},[100,73393,111],{"class":110},[100,73395,1669],{"class":122},[100,73397,73398],{"class":102,"line":135},[100,73399,139],{"emptyLinePlaceholder":138},[100,73401,73402,73404,73406,73408],{"class":102,"line":142},[100,73403,2736],{"class":145},[100,73405,1676],{"class":106},[100,73407,1918],{"class":110},[100,73409,1682],{"class":122},[100,73411,73412,73414,73416,73418,73420,73422],{"class":102,"line":152},[100,73413,200],{"class":114},[100,73415,170],{"class":118},[100,73417,206],{"class":205},[100,73419,73079],{"class":209},[100,73421,206],{"class":205},[100,73423,215],{"class":118},[14,73425,73426],{},"A beginner will usually see an error like this:",[91,73428,73430],{"className":93,"code":73429,"language":95,"meta":96,"style":96},"  File \"script.py\", line 3\n    if age > 18\n               ^\nSyntaxError: expected ':'\n",[17,73431,73432,73449,73459,73464],{"__ignoreMap":96},[100,73433,73434,73436,73438,73441,73443,73445,73447],{"class":102,"line":103},[100,73435,1952],{"class":106},[100,73437,206],{"class":205},[100,73439,73440],{"class":209},"script.py",[100,73442,206],{"class":205},[100,73444,126],{"class":118},[100,73446,1964],{"class":106},[100,73448,1500],{"class":122},[100,73450,73451,73453,73455,73457],{"class":102,"line":135},[100,73452,2777],{"class":145},[100,73454,1676],{"class":106},[100,73456,1918],{"class":110},[100,73458,1682],{"class":122},[100,73460,73461],{"class":102,"line":142},[100,73462,73463],{"class":110},"               ^\n",[100,73465,73466,73468,73470,73472,73474,73476],{"class":102,"line":152},[100,73467,16503],{"class":191},[100,73469,89],{"class":118},[100,73471,16595],{"class":106},[100,73473,1280],{"class":205},[100,73475,89],{"class":209},[100,73477,3925],{"class":205},[14,73479,73480],{},"The problem is easy to miss because only one character is missing.",[14,73482,73483],{},"To fix it, add the colon:",[91,73485,73487],{"className":93,"code":73486,"language":95,"meta":96,"style":96},"age = 20\n\nif age > 18:\n    print(\"Adult\")\n",[17,73488,73489,73497,73501,73513],{"__ignoreMap":96},[100,73490,73491,73493,73495],{"class":102,"line":103},[100,73492,1664],{"class":106},[100,73494,111],{"class":110},[100,73496,1669],{"class":122},[100,73498,73499],{"class":102,"line":135},[100,73500,139],{"emptyLinePlaceholder":138},[100,73502,73503,73505,73507,73509,73511],{"class":102,"line":142},[100,73504,2736],{"class":145},[100,73506,1676],{"class":106},[100,73508,1918],{"class":110},[100,73510,1869],{"class":122},[100,73512,149],{"class":118},[100,73514,73515,73517,73519,73521,73523,73525],{"class":102,"line":152},[100,73516,200],{"class":114},[100,73518,170],{"class":118},[100,73520,206],{"class":205},[100,73522,73079],{"class":209},[100,73524,206],{"class":205},[100,73526,215],{"class":118},[14,73528,218],{},[91,73530,73533],{"className":73531,"code":73532,"language":4468,"meta":96},[26775],"Adult\n",[17,73534,73532],{"__ignoreMap":96},[77,73536,6244],{"id":6243},[14,73538,29195],{},[3282,73540,73541,73544,73547,73552,73555],{},[43,73542,73543],{},"Look at the line named in the error message.",[43,73545,73546],{},"Check whether that line starts a block.",[43,73548,6342,73549,73551],{},[17,73550,89],{}," at the end of that line.",[43,73553,73554],{},"Make sure the next line is indented correctly.",[43,73556,73557],{},"Run the code again.",[14,73559,1844],{},[91,73561,73563],{"className":93,"code":73562,"language":95,"meta":96,"style":96},"def greet(name)\n    print(\"Hello\", name)\n",[17,73564,73565,73577],{"__ignoreMap":96},[100,73566,73567,73569,73571,73573,73575],{"class":102,"line":103},[100,73568,1078],{"class":1077},[100,73570,2848],{"class":1081},[100,73572,170],{"class":118},[100,73574,2853],{"class":2519},[100,73576,215],{"class":118},[100,73578,73579,73581,73583,73585,73587,73589,73591,73593],{"class":102,"line":135},[100,73580,200],{"class":114},[100,73582,170],{"class":118},[100,73584,206],{"class":205},[100,73586,7683],{"class":209},[100,73588,206],{"class":205},[100,73590,126],{"class":118},[100,73592,1703],{"class":178},[100,73594,215],{"class":118},[14,73596,73597],{},"This is wrong because the function definition is missing a colon.",[14,73599,6031],{},[91,73601,73603],{"className":93,"code":73602,"language":95,"meta":96,"style":96},"def greet(name):\n    print(\"Hello\", name)\n",[17,73604,73605,73617],{"__ignoreMap":96},[100,73606,73607,73609,73611,73613,73615],{"class":102,"line":103},[100,73608,1078],{"class":1077},[100,73610,2848],{"class":1081},[100,73612,170],{"class":118},[100,73614,2853],{"class":2519},[100,73616,522],{"class":118},[100,73618,73619,73621,73623,73625,73627,73629,73631,73633],{"class":102,"line":135},[100,73620,200],{"class":114},[100,73622,170],{"class":118},[100,73624,206],{"class":205},[100,73626,7683],{"class":209},[100,73628,206],{"class":205},[100,73630,126],{"class":118},[100,73632,1703],{"class":178},[100,73634,215],{"class":118},[14,73636,218],{},[91,73638,73640],{"className":93,"code":73639,"language":95,"meta":96,"style":96},"greet(\"Sam\")\n# Hello Sam\n",[17,73641,73642,73656],{"__ignoreMap":96},[100,73643,73644,73646,73648,73650,73652,73654],{"class":102,"line":103},[100,73645,2911],{"class":178},[100,73647,170],{"class":118},[100,73649,206],{"class":205},[100,73651,29329],{"class":209},[100,73653,206],{"class":205},[100,73655,215],{"class":118},[100,73657,73658],{"class":102,"line":135},[100,73659,73660],{"class":414},"# Hello Sam\n",[14,73662,73663],{},"Another example with a loop:",[91,73665,73667],{"className":93,"code":73666,"language":95,"meta":96,"style":96},"for item in [1, 2, 3]\n    print(item)\n",[17,73668,73669,73691],{"__ignoreMap":96},[100,73670,73671,73673,73675,73677,73679,73681,73683,73685,73687,73689],{"class":102,"line":103},[100,73672,71],{"class":145},[100,73674,7568],{"class":106},[100,73676,617],{"class":145},[100,73678,594],{"class":118},[100,73680,123],{"class":122},[100,73682,126],{"class":118},[100,73684,129],{"class":122},[100,73686,126],{"class":118},[100,73688,1405],{"class":122},[100,73690,603],{"class":118},[100,73692,73693,73695,73697,73699],{"class":102,"line":135},[100,73694,200],{"class":114},[100,73696,170],{"class":118},[100,73698,7555],{"class":178},[100,73700,215],{"class":118},[14,73702,15034],{},[91,73704,73706],{"className":93,"code":73705,"language":95,"meta":96,"style":96},"for item in [1, 2, 3]:\n    print(item)\n",[17,73707,73708,73730],{"__ignoreMap":96},[100,73709,73710,73712,73714,73716,73718,73720,73722,73724,73726,73728],{"class":102,"line":103},[100,73711,71],{"class":145},[100,73713,7568],{"class":106},[100,73715,617],{"class":145},[100,73717,594],{"class":118},[100,73719,123],{"class":122},[100,73721,126],{"class":118},[100,73723,129],{"class":122},[100,73725,126],{"class":118},[100,73727,1405],{"class":122},[100,73729,29073],{"class":118},[100,73731,73732,73734,73736,73738],{"class":102,"line":135},[100,73733,200],{"class":114},[100,73735,170],{"class":118},[100,73737,7555],{"class":178},[100,73739,215],{"class":118},[14,73741,218],{},[91,73743,73746],{"className":73744,"code":73745,"language":4468,"meta":96},[26775],"1\n2\n3\n",[17,73747,73745],{"__ignoreMap":96},[77,73749,73751],{"id":73750},"debugging-tips-for-beginners","Debugging tips for beginners",[14,73753,73754],{},"When you get this error, try these simple checks:",[40,73756,73757,73759,73762,73765,73778,73783,73786],{},[43,73758,72695],{},[43,73760,73761],{},"Check the line Python points to.",[43,73763,73764],{},"Also check the line just above it.",[43,73766,73767,73768,5870,73770,5870,73772,5870,73774,5894,73776,290],{},"Look for lines starting with ",[17,73769,2736],{},[17,73771,71],{},[17,73773,721],{},[17,73775,1078],{},[17,73777,3417],{},[43,73779,73780,73781,290],{},"Make sure those lines end with ",[17,73782,89],{},[43,73784,73785],{},"Use a code editor that highlights syntax errors.",[43,73787,73788],{},"Fix one syntax error at a time, then run the code again.",[14,73790,72713],{},[91,73792,73794],{"className":10352,"code":73793,"language":10354,"meta":96,"style":96},"python script.py\n",[17,73795,73796],{"__ignoreMap":96},[100,73797,73798,73800],{"class":102,"line":103},[100,73799,95],{"class":10361},[100,73801,42020],{"class":209},[14,73803,73804],{},"Or ask Python to check the file for syntax problems:",[91,73806,73808],{"className":10352,"code":73807,"language":10354,"meta":96,"style":96},"python -m py_compile script.py\n",[17,73809,73810],{"__ignoreMap":96},[100,73811,73812,73814,73816,73818],{"class":102,"line":103},[100,73813,95],{"class":10361},[100,73815,26622],{"class":10364},[100,73817,29530],{"class":209},[100,73819,42020],{"class":209},[14,73821,73822,73823,3178,73825,290],{},"If the colon is fixed but Python then complains about indentation, see ",[295,73824,28816],{"href":70794},[295,73826,16618],{"href":16617},[77,73828,73830],{"id":73829},"related-problems-that-can-look-similar","Related problems that can look similar",[14,73832,73833],{},"A missing colon often leads to other confusing errors.",[14,73835,580],{},[40,73837,73838,73841,73844],{},[43,73839,73840],{},"After adding the colon, you may get an indentation error if the next line is not indented correctly.",[43,73842,73843],{},"A missing parenthesis or quote can sometimes make Python point to the wrong place.",[43,73845,73846,73847,290],{},"A more general syntax mistake may be better explained by ",[295,73848,16612],{"href":16611},[14,73850,73851,73852,290],{},"If you are unsure how indentation works in Python, read ",[295,73853,28947],{"href":28946},[77,73855,7117],{"id":7116},[14,73857,9001],{},[40,73859,73860,73865,73871,73877,73881,73885,73895],{},[43,73861,73862,73863],{},"Missing colon after ",[17,73864,2736],{},[43,73866,73862,73867,3178,73869],{},[17,73868,30862],{},[17,73870,4944],{},[43,73872,73862,73873,3178,73875],{},[17,73874,71],{},[17,73876,721],{},[43,73878,73862,73879],{},[17,73880,1078],{},[43,73882,73862,73883],{},[17,73884,3417],{},[43,73886,73862,73887,5870,73889,5870,73891,5894,73893],{},[17,73888,146],{},[17,73890,188],{},[17,73892,28983],{},[17,73894,17521],{},[43,73896,73897],{},"Looking only at the highlighted line when the actual mistake is just above it",[77,73899,1514],{"id":1513},[675,73901,73903],{"id":73902},"where-should-the-colon-go-in-python","Where should the colon go in Python?",[14,73905,73906,73907,3178,73910,290],{},"Put the colon at the end of a line that starts a block, like ",[17,73908,73909],{},"if x > 0:",[17,73911,73912],{},"def greet():",[675,73914,73916],{"id":73915},"why-does-python-need-a-colon","Why does Python need a colon?",[14,73918,73919],{},"The colon tells Python that an indented block of code comes next.",[675,73921,73923],{"id":73922},"why-is-python-pointing-to-the-wrong-line","Why is Python pointing to the wrong line?",[14,73925,73926],{},"Syntax errors are sometimes reported on the next line, so check the line above too.",[675,73928,73930,73931,11353],{"id":73929},"can-this-happen-with-else","Can this happen with ",[17,73932,4944],{},[14,73934,10626,73935,73937,73938,290],{},[17,73936,4944],{}," must be written as ",[17,73939,73326],{},[675,73941,73943],{"id":73942},"is-this-the-same-as-an-indentation-error","Is this the same as an indentation error?",[14,73945,73946],{},"No. A missing colon is a syntax error. But after adding the colon, you may also need to fix indentation.",[77,73948,1554],{"id":1553},[40,73950,73951,73955,73959,73963,73969,73973,73977,73981],{},[43,73952,73953],{},[295,73954,29744],{"href":29185},[43,73956,73957],{},[295,73958,73375],{"href":73374},[43,73960,73961],{},[295,73962,29749],{"href":29189},[43,73964,73965],{},[295,73966,73968],{"href":73967},"\u002Flearn\u002Fpython-classes-and-objects-explained\u002F","Python classes and objects explained",[43,73970,73971],{},[295,73972,16612],{"href":16611},[43,73974,73975],{},[295,73976,28816],{"href":70794},[43,73978,73979],{},[295,73980,16618],{"href":16617},[43,73982,73983],{},[295,73984,28947],{"href":28946},[1589,73986,73987],{},"html pre.shiki code .sVHd0, html code.shiki .sVHd0{--shiki-light:#39ADB5;--shiki-light-font-style:italic;--shiki-default:#D73A49;--shiki-default-font-style:inherit;--shiki-dark:#F97583;--shiki-dark-font-style:inherit}html pre.shiki code .su5hD, html code.shiki .su5hD{--shiki-light:#90A4AE;--shiki-default:#24292E;--shiki-dark:#E1E4E8}html pre.shiki code .smGrS, html code.shiki .smGrS{--shiki-light:#39ADB5;--shiki-default:#D73A49;--shiki-dark:#F97583}html pre.shiki code .srdBf, html code.shiki .srdBf{--shiki-light:#F76D47;--shiki-default:#005CC5;--shiki-dark:#79B8FF}html pre.shiki code .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 .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 .slqww, html code.shiki .slqww{--shiki-light:#6182B8;--shiki-default:#24292E;--shiki-dark:#E1E4E8}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 .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 .sbgvK, html code.shiki .sbgvK{--shiki-light:#E2931D;--shiki-default:#6F42C1;--shiki-dark:#B392F0}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 .s39Yj, html code.shiki .s39Yj{--shiki-light:#39ADB5;--shiki-default:#005CC5;--shiki-dark:#79B8FF}html pre.shiki code .sZMiF, html code.shiki .sZMiF{--shiki-light:#E2931D;--shiki-default:#005CC5;--shiki-dark:#79B8FF}html pre.shiki code .sutJx, html code.shiki .sutJx{--shiki-light:#90A4AE;--shiki-light-font-style:italic;--shiki-default:#6A737D;--shiki-default-font-style:inherit;--shiki-dark:#6A737D;--shiki-dark-font-style:inherit}html pre.shiki code .stzsN, html code.shiki .stzsN{--shiki-light:#91B859;--shiki-default:#005CC5;--shiki-dark:#79B8FF}",{"title":96,"searchDepth":135,"depth":135,"links":73989},[73990,73991,73992,73993,73994,73995,73996,73997,73998,73999,74007],{"id":79,"depth":135,"text":80},{"id":5880,"depth":135,"text":5881},{"id":7740,"depth":135,"text":7741},{"id":73305,"depth":135,"text":73306},{"id":11030,"depth":135,"text":11031},{"id":6243,"depth":135,"text":6244},{"id":73750,"depth":135,"text":73751},{"id":73829,"depth":135,"text":73830},{"id":7116,"depth":135,"text":7117},{"id":1513,"depth":135,"text":1514,"children":74000},[74001,74002,74003,74004,74006],{"id":73902,"depth":142,"text":73903},{"id":73915,"depth":142,"text":73916},{"id":73922,"depth":142,"text":73923},{"id":73929,"depth":142,"text":74005},"Can this happen with else?",{"id":73942,"depth":142,"text":73943},{"id":1553,"depth":135,"text":1554},"Master syntaxerror missing colon fix in our comprehensive Python beginner guide.",{},{"title":73008,"description":74008},"errors\u002Fsyntaxerror-missing-colon-fix","FktQYjN4s---3FEgbkV9J4bPkrTmNuBWH2NGZdG7Apw",{"id":74014,"title":74015,"body":74016,"description":74859,"extension":1623,"meta":74860,"navigation":138,"path":72906,"seo":74861,"stem":74862,"__hash__":74863},"content\u002Ferrors\u002Fsyntaxerror-unexpected-eof-while-parsing-fix.md","SyntaxError: unexpected EOF while parsing (Fix)",{"type":7,"value":74017,"toc":74827},[74018,74021,74026,74029,74046,74049,74051,74054,74088,74091,74094,74115,74117,74120,74125,74136,74145,74148,74155,74157,74160,74177,74181,74184,74201,74207,74211,74229,74231,74241,74245,74248,74252,74277,74280,74306,74309,74312,74317,74321,74324,74327,74360,74365,74367,74420,74424,74427,74518,74521,74524,74527,74529,74532,74572,74575,74591,74596,74599,74626,74630,74633,74657,74660,74670,74673,74687,74689,74692,74718,74723,74725,74727,74756,74758,74762,74768,74772,74775,74779,74782,74786,74796,74798,74824],[10,74019,74015],{"id":74020},"syntaxerror-unexpected-eof-while-parsing-fix",[14,74022,74023,74025],{},[17,74024,69865],{}," means Python reached the end of your file before the code was complete.",[14,74027,74028],{},"This usually happens when something is missing, such as:",[40,74030,74031,74034,74037,74040,74043],{},[43,74032,74033],{},"a closing quote",[43,74035,74036],{},"a closing parenthesis",[43,74038,74039],{},"a closing bracket",[43,74041,74042],{},"a closing brace",[43,74044,74045],{},"the rest of an unfinished statement",[14,74047,74048],{},"This page will help you find the missing part, fix the syntax, and understand why Python shows this error.",[77,74050,80],{"id":79},[14,74052,74053],{},"A complete statement looks like this:",[91,74055,74057],{"className":93,"code":74056,"language":95,"meta":96,"style":96},"name = input(\"Enter your name: \")\nprint(name)\n",[17,74058,74059,74078],{"__ignoreMap":96},[100,74060,74061,74063,74065,74067,74069,74071,74074,74076],{"class":102,"line":103},[100,74062,1691],{"class":106},[100,74064,111],{"class":110},[100,74066,7221],{"class":114},[100,74068,170],{"class":118},[100,74070,206],{"class":205},[100,74072,74073],{"class":209},"Enter your name: ",[100,74075,206],{"class":205},[100,74077,215],{"class":118},[100,74079,74080,74082,74084,74086],{"class":102,"line":135},[100,74081,372],{"class":114},[100,74083,170],{"class":118},[100,74085,2853],{"class":178},[100,74087,215],{"class":118},[14,74089,74090],{},"This error usually means Python expected something more before the file ended.",[14,74092,74093],{},"Check for:",[40,74095,74096,74099,74104,74108,74112],{},[43,74097,74098],{},"a missing closing quote",[43,74100,74101,74102],{},"a missing ",[17,74103,6155],{},[43,74105,74101,74106],{},[17,74107,17591],{},[43,74109,74101,74110],{},[17,74111,18216],{},[43,74113,74114],{},"an unfinished function call or expression",[77,74116,5881],{"id":5880},[14,74118,74119],{},"Python reads your code from top to bottom.",[14,74121,22665,74122,74124],{},[17,74123,69865],{},", Python is saying:",[40,74126,74127,74130,74133],{},[43,74128,74129],{},"it got to the end of the file",[43,74131,74132],{},"it was still expecting more code",[43,74134,74135],{},"something earlier was left incomplete",[14,74137,74138,74141,74142,290],{},[17,74139,74140],{},"EOF"," means ",[22,74143,74144],{},"end of file",[14,74146,74147],{},"In many cases, the line shown in the error is not the real problem. The mistake is often on the same line or a line above it.",[14,74149,74150,74151,3266,74153,290],{},"If you want a broader overview of how syntax problems work, see ",[295,74152,30533],{"href":31678},[295,74154,1587],{"href":1749},[77,74156,7741],{"id":7740},[14,74158,74159],{},"This error is commonly caused by one of these problems:",[40,74161,74162,74165,74168,74171,74174],{},[43,74163,74164],{},"A parenthesis was opened but not closed.",[43,74166,74167],{},"A string started with a quote but never ended.",[43,74169,74170],{},"A list, tuple, dictionary, or set was not closed.",[43,74172,74173],{},"A function call or expression was left unfinished.",[43,74175,74176],{},"A multi-line statement was started but not completed.",[77,74178,74180],{"id":74179},"example-missing-closing-parenthesis","Example: missing closing parenthesis",[14,74182,74183],{},"Here is a common example:",[91,74185,74187],{"className":93,"code":74186,"language":95,"meta":96,"style":96},"print(\"Hello\"\n",[17,74188,74189],{"__ignoreMap":96},[100,74190,74191,74193,74195,74197,74199],{"class":102,"line":103},[100,74192,372],{"class":114},[100,74194,170],{"class":118},[100,74196,206],{"class":205},[100,74198,7683],{"class":209},[100,74200,1714],{"class":205},[14,74202,74203,74204,74206],{},"Python expects a closing ",[17,74205,6155],{}," but reaches the end of the file first.",[675,74208,74210],{"id":74209},"fix","Fix",[91,74212,74213],{"className":93,"code":71214,"language":95,"meta":96,"style":96},[17,74214,74215],{"__ignoreMap":96},[100,74216,74217,74219,74221,74223,74225,74227],{"class":102,"line":103},[100,74218,372],{"class":114},[100,74220,170],{"class":118},[100,74222,206],{"class":205},[100,74224,7683],{"class":209},[100,74226,206],{"class":205},[100,74228,215],{"class":118},[675,74230,72357],{"id":72356},[14,74232,43223,74233,74235,74236,74238,74239,290],{},[17,74234,13235],{}," function call starts with ",[17,74237,170],{}," and must end with ",[17,74240,6155],{},[77,74242,74244],{"id":74243},"example-missing-quote","Example: missing quote",[14,74246,74247],{},"If a string starts but never ends, Python keeps reading until the file ends.",[675,74249,74251],{"id":74250},"bad-code","Bad code",[91,74253,74255],{"className":93,"code":74254,"language":95,"meta":96,"style":96},"message = \"Hello\nprint(message)\n",[17,74256,74257,74267],{"__ignoreMap":96},[100,74258,74259,74261,74263,74265],{"class":102,"line":103},[100,74260,6868],{"class":106},[100,74262,111],{"class":110},[100,74264,1708],{"class":205},[100,74266,29173],{"class":209},[100,74268,74269,74271,74273,74275],{"class":102,"line":135},[100,74270,372],{"class":114},[100,74272,170],{"class":118},[100,74274,1836],{"class":178},[100,74276,215],{"class":118},[675,74278,74210],{"id":74279},"fix-1",[91,74281,74282],{"className":93,"code":53230,"language":95,"meta":96,"style":96},[17,74283,74284,74296],{"__ignoreMap":96},[100,74285,74286,74288,74290,74292,74294],{"class":102,"line":103},[100,74287,6868],{"class":106},[100,74289,111],{"class":110},[100,74291,1708],{"class":205},[100,74293,7683],{"class":209},[100,74295,1714],{"class":205},[100,74297,74298,74300,74302,74304],{"class":102,"line":135},[100,74299,372],{"class":114},[100,74301,170],{"class":118},[100,74303,1836],{"class":178},[100,74305,215],{"class":118},[675,74307,72357],{"id":74308},"why-this-works-1",[14,74310,74311],{},"Strings must have both an opening and a closing quote.",[14,74313,74314,74315,290],{},"If your problem is specifically caused by an unclosed string, see ",[295,74316,71886],{"href":71885},[77,74318,74320],{"id":74319},"example-unfinished-list-or-dictionary","Example: unfinished list or dictionary",[14,74322,74323],{},"This can also happen with collections.",[675,74325,74251],{"id":74326},"bad-code-1",[91,74328,74330],{"className":93,"code":74329,"language":95,"meta":96,"style":96},"numbers = [1, 2, 3\nprint(numbers)\n",[17,74331,74332,74350],{"__ignoreMap":96},[100,74333,74334,74336,74338,74340,74342,74344,74346,74348],{"class":102,"line":103},[100,74335,107],{"class":106},[100,74337,111],{"class":110},[100,74339,594],{"class":118},[100,74341,123],{"class":122},[100,74343,126],{"class":118},[100,74345,129],{"class":122},[100,74347,126],{"class":118},[100,74349,31170],{"class":122},[100,74351,74352,74354,74356,74358],{"class":102,"line":135},[100,74353,372],{"class":114},[100,74355,170],{"class":118},[100,74357,179],{"class":178},[100,74359,215],{"class":118},[14,74361,74362,74363,290],{},"Python expects the closing ",[17,74364,17591],{},[14,74366,12066],{},[91,74368,74370],{"className":93,"code":74369,"language":95,"meta":96,"style":96},"person = {\n    \"name\": \"Ana\",\n    \"age\": 25\nprint(person)\n",[17,74371,74372,74380,74398,74410],{"__ignoreMap":96},[100,74373,74374,74376,74378],{"class":102,"line":103},[100,74375,43766],{"class":106},[100,74377,111],{"class":110},[100,74379,42523],{"class":118},[100,74381,74382,74384,74386,74388,74390,74392,74394,74396],{"class":102,"line":135},[100,74383,42528],{"class":205},[100,74385,2853],{"class":209},[100,74387,206],{"class":205},[100,74389,89],{"class":118},[100,74391,1708],{"class":205},[100,74393,34819],{"class":209},[100,74395,206],{"class":205},[100,74397,42543],{"class":118},[100,74399,74400,74402,74404,74406,74408],{"class":102,"line":142},[100,74401,42528],{"class":205},[100,74403,2770],{"class":209},[100,74405,206],{"class":205},[100,74407,89],{"class":118},[100,74409,16667],{"class":122},[100,74411,74412,74414,74416,74418],{"class":102,"line":152},[100,74413,372],{"class":114},[100,74415,170],{"class":118},[100,74417,43801],{"class":178},[100,74419,215],{"class":118},[14,74421,74362,74422,290],{},[17,74423,18216],{},[675,74425,74210],{"id":74426},"fix-2",[91,74428,74430],{"className":93,"code":74429,"language":95,"meta":96,"style":96},"numbers = [1, 2, 3]\nprint(numbers)\n\nperson = {\n    \"name\": \"Ana\",\n    \"age\": 25\n}\nprint(person)\n",[17,74431,74432,74452,74462,74466,74474,74492,74504,74508],{"__ignoreMap":96},[100,74433,74434,74436,74438,74440,74442,74444,74446,74448,74450],{"class":102,"line":103},[100,74435,107],{"class":106},[100,74437,111],{"class":110},[100,74439,594],{"class":118},[100,74441,123],{"class":122},[100,74443,126],{"class":118},[100,74445,129],{"class":122},[100,74447,126],{"class":118},[100,74449,1405],{"class":122},[100,74451,603],{"class":118},[100,74453,74454,74456,74458,74460],{"class":102,"line":135},[100,74455,372],{"class":114},[100,74457,170],{"class":118},[100,74459,179],{"class":178},[100,74461,215],{"class":118},[100,74463,74464],{"class":102,"line":142},[100,74465,139],{"emptyLinePlaceholder":138},[100,74467,74468,74470,74472],{"class":102,"line":152},[100,74469,43766],{"class":106},[100,74471,111],{"class":110},[100,74473,42523],{"class":118},[100,74475,74476,74478,74480,74482,74484,74486,74488,74490],{"class":102,"line":164},[100,74477,42528],{"class":205},[100,74479,2853],{"class":209},[100,74481,206],{"class":205},[100,74483,89],{"class":118},[100,74485,1708],{"class":205},[100,74487,34819],{"class":209},[100,74489,206],{"class":205},[100,74491,42543],{"class":118},[100,74493,74494,74496,74498,74500,74502],{"class":102,"line":185},[100,74495,42528],{"class":205},[100,74497,2770],{"class":209},[100,74499,206],{"class":205},[100,74501,89],{"class":118},[100,74503,16667],{"class":122},[100,74505,74506],{"class":102,"line":197},[100,74507,12093],{"class":118},[100,74509,74510,74512,74514,74516],{"class":102,"line":771},[100,74511,372],{"class":114},[100,74513,170],{"class":118},[100,74515,43801],{"class":178},[100,74517,215],{"class":118},[675,74519,72357],{"id":74520},"why-this-works-2",[14,74522,74523],{},"Lists, dictionaries, tuples, and sets must be fully closed before Python can continue.",[14,74525,74526],{},"Check collection literals carefully, especially when they span multiple lines.",[77,74528,52309],{"id":52308},[14,74530,74531],{},"Use this process when you see the error:",[3282,74533,74534,74537,74540,74563,74566,74569],{},[43,74535,74536],{},"Read the line shown in the error message.",[43,74538,74539],{},"Check the line above it too.",[43,74541,74542,74543],{},"Match every opening symbol with a closing symbol:\n",[40,74544,74545,74551,74557],{},[43,74546,74547,42154,74549],{},[17,74548,170],{},[17,74550,6155],{},[43,74552,74553,42154,74555],{},[17,74554,3166],{},[17,74556,17591],{},[43,74558,74559,42154,74561],{},[17,74560,16490],{},[17,74562,18216],{},[43,74564,74565],{},"Check that all strings have both opening and closing quotes.",[43,74567,74568],{},"Look for unfinished function calls or expressions.",[43,74570,74571],{},"If needed, delete the broken line and type it again carefully.",[14,74573,74574],{},"For example, this unfinished expression causes the same problem:",[91,74576,74578],{"className":93,"code":74577,"language":95,"meta":96,"style":96},"total = 10 +\n",[17,74579,74580],{"__ignoreMap":96},[100,74581,74582,74584,74586,74588],{"class":102,"line":103},[100,74583,53376],{"class":106},[100,74585,111],{"class":110},[100,74587,6550],{"class":122},[100,74589,74590],{"class":110}," +\n",[14,74592,74593,74594,290],{},"Python expects something after the ",[17,74595,6547],{},[675,74597,74210],{"id":74598},"fix-3",[91,74600,74602],{"className":93,"code":74601,"language":95,"meta":96,"style":96},"total = 10 + 5\nprint(total)\n",[17,74603,74604,74616],{"__ignoreMap":96},[100,74605,74606,74608,74610,74612,74614],{"class":102,"line":103},[100,74607,53376],{"class":106},[100,74609,111],{"class":110},[100,74611,6550],{"class":122},[100,74613,2895],{"class":110},[100,74615,37580],{"class":122},[100,74617,74618,74620,74622,74624],{"class":102,"line":135},[100,74619,372],{"class":114},[100,74621,170],{"class":118},[100,74623,2520],{"class":178},[100,74625,215],{"class":118},[77,74627,74629],{"id":74628},"fast-debugging-checklist","Fast debugging checklist",[14,74631,74632],{},"When you need to fix the error quickly, check these first:",[40,74634,74635,74645,74648,74651,74654],{},[43,74636,74637,74638,5870,74640,5629,74642,74644],{},"Count ",[17,74639,170],{},[17,74641,3166],{},[17,74643,16490],{}," characters and make sure they are closed.",[43,74646,74647],{},"Check for single or double quotes that were not closed.",[43,74649,74650],{},"Look for a comma or operator at the end of a line with no next value.",[43,74652,74653],{},"Check code copied from the web for missing last characters.",[43,74655,74656],{},"Use an editor that highlights matching brackets.",[14,74658,74659],{},"You can also run your file from the command line:",[91,74661,74662],{"className":10352,"code":31396,"language":10354,"meta":96,"style":96},[17,74663,74664],{"__ignoreMap":96},[100,74665,74666,74668],{"class":102,"line":103},[100,74667,95],{"class":10361},[100,74669,30457],{"class":209},[14,74671,74672],{},"Or check the file for syntax problems with:",[91,74674,74675],{"className":10352,"code":31421,"language":10354,"meta":96,"style":96},[17,74676,74677],{"__ignoreMap":96},[100,74678,74679,74681,74683,74685],{"class":102,"line":103},[100,74680,95],{"class":10361},[100,74682,26622],{"class":10364},[100,74684,29530],{"class":209},[100,74686,30457],{"class":209},[77,74688,1344],{"id":1343},[14,74690,74691],{},"These are the most common reasons for this error:",[40,74693,74694,74699,74704,74709,74712,74715],{},[43,74695,74696,74697],{},"Missing closing parenthesis ",[17,74698,6155],{},[43,74700,74701,74702],{},"Missing closing bracket ",[17,74703,17591],{},[43,74705,74706,74707],{},"Missing closing brace ",[17,74708,18216],{},[43,74710,74711],{},"Unclosed string quote",[43,74713,74714],{},"Unfinished function call",[43,74716,74717],{},"Incomplete expression at the end of the file",[14,74719,74720,74721,290],{},"A more general syntax page may also help if the message is less clear: ",[295,74722,16612],{"href":16611},[77,74724,7081],{"id":7080},[14,74726,69837],{},[40,74728,74729,74734,74751],{},[43,74730,74731,74733],{},[295,74732,71886],{"href":71885}," is more specific for unclosed strings.",[43,74735,74736,74738,74739,73291,74741,5870,74743,5870,74745,5870,74747,5894,74749,290],{},[295,74737,29615],{"href":70800}," happens when Python expects ",[17,74740,89],{},[17,74742,2736],{},[17,74744,71],{},[17,74746,721],{},[17,74748,1078],{},[17,74750,3417],{},[43,74752,74753,74755],{},[295,74754,28816],{"href":70794}," happens when the syntax is otherwise valid, but the block indentation is wrong.",[77,74757,1514],{"id":1513},[675,74759,74761],{"id":74760},"what-does-eof-mean-in-python","What does EOF mean in Python?",[14,74763,74764,74765,74767],{},"EOF means ",[22,74766,74144],{},". Python got to the end of your code while still expecting more syntax.",[675,74769,74771],{"id":74770},"is-the-error-always-on-the-last-line","Is the error always on the last line?",[14,74773,74774],{},"No. The real problem is often earlier, such as an unclosed quote or bracket on a previous line.",[675,74776,74778],{"id":74777},"can-this-happen-with-copy-pasted-code","Can this happen with copy-pasted code?",[14,74780,74781],{},"Yes. Code can be missing a closing character or may have been cut off when copied.",[675,74783,74785],{"id":74784},"how-do-i-find-the-missing-symbol-quickly","How do I find the missing symbol quickly?",[14,74787,74788,74789,5870,74791,5870,74793,74795],{},"Check matching pairs: ",[17,74790,7562],{},[17,74792,45218],{},[17,74794,45815],{},", and quotes. Most editors also highlight matching brackets.",[77,74797,1554],{"id":1553},[40,74799,74800,74804,74808,74812,74816,74820],{},[43,74801,74802],{},[295,74803,16612],{"href":16611},[43,74805,74806],{},[295,74807,71886],{"href":71885},[43,74809,74810],{},[295,74811,29615],{"href":70800},[43,74813,74814],{},[295,74815,28816],{"href":70794},[43,74817,74818],{},[295,74819,1587],{"href":1749},[43,74821,74822],{},[295,74823,30533],{"href":31678},[1589,74825,74826],{},"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 .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 .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 .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 .srdBf, html code.shiki .srdBf{--shiki-light:#F76D47;--shiki-default:#005CC5;--shiki-dark:#79B8FF}html pre.shiki code .sbgvK, html code.shiki .sbgvK{--shiki-light:#E2931D;--shiki-default:#6F42C1;--shiki-dark:#B392F0}html pre.shiki code .stzsN, html code.shiki .stzsN{--shiki-light:#91B859;--shiki-default:#005CC5;--shiki-dark:#79B8FF}",{"title":96,"searchDepth":135,"depth":135,"links":74828},[74829,74830,74831,74832,74836,74841,74846,74849,74850,74851,74852,74858],{"id":79,"depth":135,"text":80},{"id":5880,"depth":135,"text":5881},{"id":7740,"depth":135,"text":7741},{"id":74179,"depth":135,"text":74180,"children":74833},[74834,74835],{"id":74209,"depth":142,"text":74210},{"id":72356,"depth":142,"text":72357},{"id":74243,"depth":135,"text":74244,"children":74837},[74838,74839,74840],{"id":74250,"depth":142,"text":74251},{"id":74279,"depth":142,"text":74210},{"id":74308,"depth":142,"text":72357},{"id":74319,"depth":135,"text":74320,"children":74842},[74843,74844,74845],{"id":74326,"depth":142,"text":74251},{"id":74426,"depth":142,"text":74210},{"id":74520,"depth":142,"text":72357},{"id":52308,"depth":135,"text":52309,"children":74847},[74848],{"id":74598,"depth":142,"text":74210},{"id":74628,"depth":135,"text":74629},{"id":1343,"depth":135,"text":1344},{"id":7080,"depth":135,"text":7081},{"id":1513,"depth":135,"text":1514,"children":74853},[74854,74855,74856,74857],{"id":74760,"depth":142,"text":74761},{"id":74770,"depth":142,"text":74771},{"id":74777,"depth":142,"text":74778},{"id":74784,"depth":142,"text":74785},{"id":1553,"depth":135,"text":1554},"Master syntaxerror unexpected eof while parsing fix in our comprehensive Python beginner guide.",{},{"title":74015,"description":74859},"errors\u002Fsyntaxerror-unexpected-eof-while-parsing-fix","leI94pTqZ7aMQm3-O4mw0OZrzn_mnI9eCgoInKasNWw",{"id":74865,"title":74866,"body":74867,"description":76532,"extension":1623,"meta":76533,"navigation":138,"path":76534,"seo":76535,"stem":76536,"__hash__":76537},"content\u002Ferrors\u002Ftypeerror-bool-object-is-not-iterable-fix.md","TypeError: 'bool' object is not iterable (Fix)",{"type":7,"value":74868,"toc":76488},[74869,74876,74883,74886,74896,74901,74904,74906,74981,74983,74991,74999,75001,75004,75007,75025,75035,75037,75039,75078,75081,75107,75114,75119,75121,75124,75126,75152,75156,75210,75215,75222,75226,75229,75232,75247,75249,75289,75293,75358,75360,75379,75383,75386,75462,75464,75477,75484,75488,75491,75498,75501,75542,75545,75585,75587,75607,75617,75620,75624,75627,75630,75692,75695,75772,75774,75805,75811,75816,75820,75823,75832,75871,75877,75920,75929,75936,75997,76000,76004,76088,76091,76098,76143,76146,76174,76180,76184,76215,76222,76229,76231,76234,76267,76271,76354,76357,76359,76362,76390,76393,76395,76399,76407,76411,76414,76418,76421,76434,76441,76448,76451,76453,76485],[10,74870,74872,74873,7375],{"id":74871},"typeerror-bool-object-is-not-iterable-fix","TypeError: ",[17,74874,74875],{},"'bool' object is not iterable",[14,74877,74878,74879,3178,74881,10873],{},"This error happens when Python expects something it can loop over, unpack, or check membership in, but gets ",[17,74880,19246],{},[17,74882,1649],{},[14,74884,74885],{},"A boolean is only a single value:",[40,74887,74888,74892],{},[43,74889,74890],{},[17,74891,19246],{},[43,74893,74894],{},[17,74895,1649],{},[14,74897,41219,74898,74900],{},[22,74899,1059],{}," a collection like a list, string, tuple, set, or dictionary.",[14,74902,74903],{},"This guide shows what the error means, why it happens, and how to fix it based on what your variable should actually contain.",[77,74905,80],{"id":79},[91,74907,74909],{"className":93,"code":74908,"language":95,"meta":96,"style":96},"value = True\n\n# Wrong:\n# for item in value:\n#     print(item)\n\n# Fix: use a real iterable\nitems = [value]\nfor item in items:\n    print(item)\n",[17,74910,74911,74919,74923,74928,74933,74938,74942,74947,74959,74971],{"__ignoreMap":96},[100,74912,74913,74915,74917],{"class":102,"line":103},[100,74914,3318],{"class":106},[100,74916,111],{"class":110},[100,74918,3588],{"class":158},[100,74920,74921],{"class":102,"line":135},[100,74922,139],{"emptyLinePlaceholder":138},[100,74924,74925],{"class":102,"line":142},[100,74926,74927],{"class":414},"# Wrong:\n",[100,74929,74930],{"class":102,"line":152},[100,74931,74932],{"class":414},"# for item in value:\n",[100,74934,74935],{"class":102,"line":164},[100,74936,74937],{"class":414},"#     print(item)\n",[100,74939,74940],{"class":102,"line":185},[100,74941,139],{"emptyLinePlaceholder":138},[100,74943,74944],{"class":102,"line":197},[100,74945,74946],{"class":414},"# Fix: use a real iterable\n",[100,74948,74949,74951,74953,74955,74957],{"class":102,"line":771},[100,74950,3095],{"class":106},[100,74952,111],{"class":110},[100,74954,594],{"class":118},[100,74956,757],{"class":106},[100,74958,603],{"class":118},[100,74960,74961,74963,74965,74967,74969],{"class":102,"line":787},[100,74962,71],{"class":145},[100,74964,7568],{"class":106},[100,74966,617],{"class":145},[100,74968,12000],{"class":106},[100,74970,149],{"class":118},[100,74972,74973,74975,74977,74979],{"class":102,"line":5816},[100,74974,200],{"class":114},[100,74976,170],{"class":118},[100,74978,7555],{"class":178},[100,74980,215],{"class":118},[14,74982,218],{},[91,74984,74985],{"className":93,"code":3431,"language":95,"meta":96,"style":96},[17,74986,74987],{"__ignoreMap":96},[100,74988,74989],{"class":102,"line":103},[100,74990,3431],{"class":158},[14,74992,74993,74994,2558,74996,74998],{},"A boolean is a single ",[17,74995,19246],{},[17,74997,1649],{}," value, not a collection. If you need iteration, use a list, tuple, string, set, or dictionary instead.",[77,75000,5881],{"id":5880},[14,75002,75003],{},"Python raises this error when code expects something it can loop over.",[14,75005,75006],{},"This usually happens in places like:",[40,75008,75009,75014,75019,75022],{},[43,75010,75011,75013],{},[17,75012,71],{}," loops",[43,75015,75016,75017],{},"membership tests using ",[17,75018,617],{},[43,75020,75021],{},"unpacking",[43,75023,75024],{},"functions that expect iterables",[14,75026,571,75027,75029,75030,3178,75032,75034],{},[17,75028,46487],{}," only stores ",[17,75031,19246],{},[17,75033,1649],{},", so Python cannot treat it like a group of items.",[77,75036,11031],{"id":11030},[14,75038,18361],{},[91,75040,75042],{"className":93,"code":75041,"language":95,"meta":96,"style":96},"value = True\n\nfor item in value:\n    print(item)\n",[17,75043,75044,75052,75056,75068],{"__ignoreMap":96},[100,75045,75046,75048,75050],{"class":102,"line":103},[100,75047,3318],{"class":106},[100,75049,111],{"class":110},[100,75051,3588],{"class":158},[100,75053,75054],{"class":102,"line":135},[100,75055,139],{"emptyLinePlaceholder":138},[100,75057,75058,75060,75062,75064,75066],{"class":102,"line":142},[100,75059,71],{"class":145},[100,75061,7568],{"class":106},[100,75063,617],{"class":145},[100,75065,6747],{"class":106},[100,75067,149],{"class":118},[100,75069,75070,75072,75074,75076],{"class":102,"line":152},[100,75071,200],{"class":114},[100,75073,170],{"class":118},[100,75075,7555],{"class":178},[100,75077,215],{"class":118},[14,75079,75080],{},"Python raises:",[91,75082,75084],{"className":93,"code":75083,"language":95,"meta":96,"style":96},"TypeError: 'bool' object is not iterable\n",[17,75085,75086],{"__ignoreMap":96},[100,75087,75088,75090,75092,75094,75096,75098,75100,75102,75104],{"class":102,"line":103},[100,75089,3538],{"class":191},[100,75091,89],{"class":118},[100,75093,1274],{"class":205},[100,75095,46487],{"class":209},[100,75097,1280],{"class":205},[100,75099,3914],{"class":191},[100,75101,16702],{"class":110},[100,75103,4921],{"class":110},[100,75105,75106],{"class":106}," iterable\n",[14,75108,75109,75110,75113],{},"Why? Because ",[17,75111,75112],{},"for item in ..."," only works with iterable values such as lists, strings, tuples, and dictionaries.",[14,75115,75116,75117,290],{},"If you are new to loops, see ",[295,75118,73375],{"href":73374},[77,75120,25091],{"id":25090},[14,75122,75123],{},"This error usually means your variable is not the type you thought it was.",[14,75125,23481],{},[40,75127,75128,75131,75142,75145],{},[43,75129,75130],{},"A variable contains a boolean when you expected a list or string",[43,75132,75133,75134,551,75137,3178,75139,75141],{},"A comparison like ",[17,75135,75136],{},"x > 5",[17,75138,19246],{},[17,75140,1649],{},", not multiple values",[43,75143,75144],{},"A function returns a boolean, but later code tries to loop over the result",[43,75146,75147,75148,3178,75150,61921],{},"A variable gets overwritten with ",[17,75149,19246],{},[17,75151,1649],{},[675,75153,75155],{"id":75154},"example-comparison-result-used-like-a-list","Example: comparison result used like a list",[91,75157,75159],{"className":93,"code":75158,"language":95,"meta":96,"style":96},"x = 10\nvalue = x > 5   # value becomes True\n\nfor item in value:\n    print(item)\n",[17,75160,75161,75169,75184,75188,75200],{"__ignoreMap":96},[100,75162,75163,75165,75167],{"class":102,"line":103},[100,75164,54291],{"class":106},[100,75166,111],{"class":110},[100,75168,2181],{"class":122},[100,75170,75171,75173,75175,75177,75179,75181],{"class":102,"line":135},[100,75172,3318],{"class":106},[100,75174,111],{"class":110},[100,75176,59659],{"class":106},[100,75178,1918],{"class":110},[100,75180,31164],{"class":122},[100,75182,75183],{"class":414},"   # value becomes True\n",[100,75185,75186],{"class":102,"line":142},[100,75187,139],{"emptyLinePlaceholder":138},[100,75189,75190,75192,75194,75196,75198],{"class":102,"line":152},[100,75191,71],{"class":145},[100,75193,7568],{"class":106},[100,75195,617],{"class":145},[100,75197,6747],{"class":106},[100,75199,149],{"class":118},[100,75201,75202,75204,75206,75208],{"class":102,"line":164},[100,75203,200],{"class":114},[100,75205,170],{"class":118},[100,75207,7555],{"class":178},[100,75209,215],{"class":118},[14,75211,75212,75214],{},[17,75213,75136],{}," is a condition. It does not create a collection.",[14,75216,75217,75218,290],{},"To understand this better, see ",[295,75219,75221],{"href":75220},"\u002Flearn\u002Fpython-booleans-explained-true-and-false\u002F","Python booleans explained: True and False",[77,75223,75225],{"id":75224},"fix-1-use-the-correct-iterable-value","Fix 1: Use the correct iterable value",[14,75227,75228],{},"First, check what the variable is supposed to contain.",[14,75230,75231],{},"If you meant to store multiple items, use a real iterable such as:",[40,75233,75234,75236,75239,75242,75245],{},[43,75235,1235],{},[43,75237,75238],{},"tuple",[43,75240,75241],{},"string",[43,75243,75244],{},"set",[43,75246,48164],{},[675,75248,6371],{"id":6370},[91,75250,75252],{"className":93,"code":75251,"language":95,"meta":96,"style":96},"names = True\n\nfor name in names:\n    print(name)\n",[17,75253,75254,75262,75266,75279],{"__ignoreMap":96},[100,75255,75256,75258,75260],{"class":102,"line":103},[100,75257,7789],{"class":106},[100,75259,111],{"class":110},[100,75261,3588],{"class":158},[100,75263,75264],{"class":102,"line":135},[100,75265,139],{"emptyLinePlaceholder":138},[100,75267,75268,75270,75272,75274,75277],{"class":102,"line":142},[100,75269,71],{"class":145},[100,75271,4915],{"class":106},[100,75273,617],{"class":145},[100,75275,75276],{"class":106}," names",[100,75278,149],{"class":118},[100,75280,75281,75283,75285,75287],{"class":102,"line":152},[100,75282,200],{"class":114},[100,75284,170],{"class":118},[100,75286,2853],{"class":178},[100,75288,215],{"class":118},[675,75290,75292],{"id":75291},"correct","Correct",[91,75294,75296],{"className":93,"code":75295,"language":95,"meta":96,"style":96},"names = [\"Alice\", \"Bob\", \"Charlie\"]\n\nfor name in names:\n    print(name)\n",[17,75297,75298,75332,75336,75348],{"__ignoreMap":96},[100,75299,75300,75302,75304,75306,75308,75310,75312,75314,75316,75319,75321,75323,75325,75328,75330],{"class":102,"line":103},[100,75301,7789],{"class":106},[100,75303,111],{"class":110},[100,75305,594],{"class":118},[100,75307,206],{"class":205},[100,75309,15343],{"class":209},[100,75311,206],{"class":205},[100,75313,126],{"class":118},[100,75315,1708],{"class":205},[100,75317,75318],{"class":209},"Bob",[100,75320,206],{"class":205},[100,75322,126],{"class":118},[100,75324,1708],{"class":205},[100,75326,75327],{"class":209},"Charlie",[100,75329,206],{"class":205},[100,75331,603],{"class":118},[100,75333,75334],{"class":102,"line":135},[100,75335,139],{"emptyLinePlaceholder":138},[100,75337,75338,75340,75342,75344,75346],{"class":102,"line":142},[100,75339,71],{"class":145},[100,75341,4915],{"class":106},[100,75343,617],{"class":145},[100,75345,75276],{"class":106},[100,75347,149],{"class":118},[100,75349,75350,75352,75354,75356],{"class":102,"line":152},[100,75351,200],{"class":114},[100,75353,170],{"class":118},[100,75355,2853],{"class":178},[100,75357,215],{"class":118},[14,75359,218],{},[91,75361,75363],{"className":93,"code":75362,"language":95,"meta":96,"style":96},"Alice\nBob\nCharlie\n",[17,75364,75365,75369,75374],{"__ignoreMap":96},[100,75366,75367],{"class":102,"line":103},[100,75368,11279],{"class":106},[100,75370,75371],{"class":102,"line":135},[100,75372,75373],{"class":106},"Bob\n",[100,75375,75376],{"class":102,"line":142},[100,75377,75378],{"class":106},"Charlie\n",[675,75380,75382],{"id":75381},"if-the-value-came-from-a-function","If the value came from a function",[14,75384,75385],{},"Make sure the function returns the data you need.",[91,75387,75389],{"className":93,"code":75388,"language":95,"meta":96,"style":96},"def get_names():\n    return [\"Alice\", \"Bob\"]\n\nnames = get_names()\n\nfor name in names:\n    print(name)\n",[17,75390,75391,75400,75422,75426,75436,75440,75452],{"__ignoreMap":96},[100,75392,75393,75395,75398],{"class":102,"line":103},[100,75394,1078],{"class":1077},[100,75396,75397],{"class":1081}," get_names",[100,75399,1085],{"class":118},[100,75401,75402,75404,75406,75408,75410,75412,75414,75416,75418,75420],{"class":102,"line":135},[100,75403,2552],{"class":145},[100,75405,594],{"class":118},[100,75407,206],{"class":205},[100,75409,15343],{"class":209},[100,75411,206],{"class":205},[100,75413,126],{"class":118},[100,75415,1708],{"class":205},[100,75417,75318],{"class":209},[100,75419,206],{"class":205},[100,75421,603],{"class":118},[100,75423,75424],{"class":102,"line":142},[100,75425,139],{"emptyLinePlaceholder":138},[100,75427,75428,75430,75432,75434],{"class":102,"line":152},[100,75429,7789],{"class":106},[100,75431,111],{"class":110},[100,75433,75397],{"class":178},[100,75435,4734],{"class":118},[100,75437,75438],{"class":102,"line":164},[100,75439,139],{"emptyLinePlaceholder":138},[100,75441,75442,75444,75446,75448,75450],{"class":102,"line":185},[100,75443,71],{"class":145},[100,75445,4915],{"class":106},[100,75447,617],{"class":145},[100,75449,75276],{"class":106},[100,75451,149],{"class":118},[100,75453,75454,75456,75458,75460],{"class":102,"line":197},[100,75455,200],{"class":114},[100,75457,170],{"class":118},[100,75459,2853],{"class":178},[100,75461,215],{"class":118},[14,75463,218],{},[91,75465,75467],{"className":93,"code":75466,"language":95,"meta":96,"style":96},"Alice\nBob\n",[17,75468,75469,75473],{"__ignoreMap":96},[100,75470,75471],{"class":102,"line":103},[100,75472,11279],{"class":106},[100,75474,75475],{"class":102,"line":135},[100,75476,75373],{"class":106},[14,75478,75479,75480,290],{},"If you are not sure what a function returned, check it with Python’s ",[295,75481,75482,6354],{"href":4427},[17,75483,4430],{},[77,75485,75487],{"id":75486},"fix-2-do-not-loop-over-a-boolean","Fix 2: Do not loop over a boolean",[14,75489,75490],{},"Sometimes the value is meant to be a condition, not a collection.",[14,75492,75493,75494,9472,75496,290],{},"In that case, use ",[17,75495,2736],{},[17,75497,71],{},[675,75499,6371],{"id":75500},"wrong-1",[91,75502,75504],{"className":93,"code":75503,"language":95,"meta":96,"style":96},"is_logged_in = True\n\nfor item in is_logged_in:\n    print(item)\n",[17,75505,75506,75515,75519,75532],{"__ignoreMap":96},[100,75507,75508,75511,75513],{"class":102,"line":103},[100,75509,75510],{"class":106},"is_logged_in ",[100,75512,111],{"class":110},[100,75514,3588],{"class":158},[100,75516,75517],{"class":102,"line":135},[100,75518,139],{"emptyLinePlaceholder":138},[100,75520,75521,75523,75525,75527,75530],{"class":102,"line":142},[100,75522,71],{"class":145},[100,75524,7568],{"class":106},[100,75526,617],{"class":145},[100,75528,75529],{"class":106}," is_logged_in",[100,75531,149],{"class":118},[100,75533,75534,75536,75538,75540],{"class":102,"line":152},[100,75535,200],{"class":114},[100,75537,170],{"class":118},[100,75539,7555],{"class":178},[100,75541,215],{"class":118},[675,75543,75292],{"id":75544},"correct-1",[91,75546,75548],{"className":93,"code":75547,"language":95,"meta":96,"style":96},"is_logged_in = True\n\nif is_logged_in:\n    print(\"The user is logged in.\")\n",[17,75549,75550,75558,75562,75570],{"__ignoreMap":96},[100,75551,75552,75554,75556],{"class":102,"line":103},[100,75553,75510],{"class":106},[100,75555,111],{"class":110},[100,75557,3588],{"class":158},[100,75559,75560],{"class":102,"line":135},[100,75561,139],{"emptyLinePlaceholder":138},[100,75563,75564,75566,75568],{"class":102,"line":142},[100,75565,2736],{"class":145},[100,75567,75529],{"class":106},[100,75569,149],{"class":118},[100,75571,75572,75574,75576,75578,75581,75583],{"class":102,"line":152},[100,75573,200],{"class":114},[100,75575,170],{"class":118},[100,75577,206],{"class":205},[100,75579,75580],{"class":209},"The user is logged in.",[100,75582,206],{"class":205},[100,75584,215],{"class":118},[14,75586,218],{},[91,75588,75590],{"className":93,"code":75589,"language":95,"meta":96,"style":96},"The user is logged in.\n",[17,75591,75592],{"__ignoreMap":96},[100,75593,75594,75597,75599,75602,75604],{"class":102,"line":103},[100,75595,75596],{"class":106},"The user ",[100,75598,4918],{"class":110},[100,75600,75601],{"class":106}," logged ",[100,75603,617],{"class":110},[100,75605,75606],{"class":118},".\n",[14,75608,241,75609,75612,75613,3178,75615,290],{},[17,75610,75611],{},"if value:"," when you want to test whether something is ",[17,75614,19246],{},[17,75616,1649],{},[14,75618,75619],{},"Do not treat condition results like lists or strings.",[77,75621,75623],{"id":75622},"fix-3-debug-the-variable-before-using-it","Fix 3: Debug the variable before using it",[14,75625,75626],{},"If the error is confusing, inspect the variable before the line that fails.",[14,75628,75629],{},"Useful debug checks:",[91,75631,75633],{"className":93,"code":75632,"language":95,"meta":96,"style":96},"print(value)\nprint(type(value))\nprint(repr(value))\nprint(isinstance(value, bool))\n",[17,75634,75635,75645,75659,75673],{"__ignoreMap":96},[100,75636,75637,75639,75641,75643],{"class":102,"line":103},[100,75638,372],{"class":114},[100,75640,170],{"class":118},[100,75642,757],{"class":178},[100,75644,215],{"class":118},[100,75646,75647,75649,75651,75653,75655,75657],{"class":102,"line":135},[100,75648,372],{"class":114},[100,75650,170],{"class":118},[100,75652,1250],{"class":191},[100,75654,170],{"class":118},[100,75656,757],{"class":178},[100,75658,182],{"class":118},[100,75660,75661,75663,75665,75667,75669,75671],{"class":102,"line":142},[100,75662,372],{"class":114},[100,75664,170],{"class":118},[100,75666,12228],{"class":114},[100,75668,170],{"class":118},[100,75670,757],{"class":178},[100,75672,182],{"class":118},[100,75674,75675,75677,75679,75681,75683,75685,75687,75690],{"class":102,"line":152},[100,75676,372],{"class":114},[100,75678,170],{"class":118},[100,75680,6994],{"class":114},[100,75682,170],{"class":118},[100,75684,757],{"class":178},[100,75686,126],{"class":118},[100,75688,75689],{"class":191}," bool",[100,75691,182],{"class":118},[675,75693,48552],{"id":75694},"example",[91,75696,75698],{"className":93,"code":75697,"language":95,"meta":96,"style":96},"value = 5 > 2\n\nprint(value)\nprint(type(value))\nprint(repr(value))\nprint(isinstance(value, bool))\n",[17,75699,75700,75712,75716,75726,75740,75754],{"__ignoreMap":96},[100,75701,75702,75704,75706,75708,75710],{"class":102,"line":103},[100,75703,3318],{"class":106},[100,75705,111],{"class":110},[100,75707,31164],{"class":122},[100,75709,31167],{"class":110},[100,75711,3564],{"class":122},[100,75713,75714],{"class":102,"line":135},[100,75715,139],{"emptyLinePlaceholder":138},[100,75717,75718,75720,75722,75724],{"class":102,"line":142},[100,75719,372],{"class":114},[100,75721,170],{"class":118},[100,75723,757],{"class":178},[100,75725,215],{"class":118},[100,75727,75728,75730,75732,75734,75736,75738],{"class":102,"line":152},[100,75729,372],{"class":114},[100,75731,170],{"class":118},[100,75733,1250],{"class":191},[100,75735,170],{"class":118},[100,75737,757],{"class":178},[100,75739,182],{"class":118},[100,75741,75742,75744,75746,75748,75750,75752],{"class":102,"line":164},[100,75743,372],{"class":114},[100,75745,170],{"class":118},[100,75747,12228],{"class":114},[100,75749,170],{"class":118},[100,75751,757],{"class":178},[100,75753,182],{"class":118},[100,75755,75756,75758,75760,75762,75764,75766,75768,75770],{"class":102,"line":185},[100,75757,372],{"class":114},[100,75759,170],{"class":118},[100,75761,6994],{"class":114},[100,75763,170],{"class":118},[100,75765,757],{"class":178},[100,75767,126],{"class":118},[100,75769,75689],{"class":191},[100,75771,182],{"class":118},[14,75773,218],{},[91,75775,75777],{"className":93,"code":75776,"language":95,"meta":96,"style":96},"True\n\u003Cclass 'bool'>\nTrue\nTrue\n",[17,75778,75779,75783,75797,75801],{"__ignoreMap":96},[100,75780,75781],{"class":102,"line":103},[100,75782,3431],{"class":158},[100,75784,75785,75787,75789,75791,75793,75795],{"class":102,"line":135},[100,75786,2782],{"class":110},[100,75788,3417],{"class":1077},[100,75790,1274],{"class":205},[100,75792,46487],{"class":209},[100,75794,1280],{"class":205},[100,75796,1980],{"class":110},[100,75798,75799],{"class":102,"line":142},[100,75800,3431],{"class":158},[100,75802,75803],{"class":102,"line":152},[100,75804,3431],{"class":158},[14,75806,75807,75808,75810],{},"This quickly shows that ",[17,75809,757],{}," is a boolean, not an iterable.",[14,75812,75813,75814,290],{},"If you need a step-by-step process, see the ",[295,75815,7077],{"href":3852},[77,75817,75819],{"id":75818},"common-real-world-causes","Common real-world causes",[14,75821,75822],{},"Here are common patterns that cause this error.",[675,75824,75826,75827,3178,75829,75831],{"id":75825},"_1-looping-over-true-or-false-directly","1. Looping over ",[17,75828,19246],{},[17,75830,1649],{}," directly",[91,75833,75835],{"className":93,"code":75834,"language":95,"meta":96,"style":96},"value = False\n\nfor item in value:\n    print(item)\n",[17,75836,75837,75845,75849,75861],{"__ignoreMap":96},[100,75838,75839,75841,75843],{"class":102,"line":103},[100,75840,3318],{"class":106},[100,75842,111],{"class":110},[100,75844,44952],{"class":158},[100,75846,75847],{"class":102,"line":135},[100,75848,139],{"emptyLinePlaceholder":138},[100,75850,75851,75853,75855,75857,75859],{"class":102,"line":142},[100,75852,71],{"class":145},[100,75854,7568],{"class":106},[100,75856,617],{"class":145},[100,75858,6747],{"class":106},[100,75860,149],{"class":118},[100,75862,75863,75865,75867,75869],{"class":102,"line":152},[100,75864,200],{"class":114},[100,75866,170],{"class":118},[100,75868,7555],{"class":178},[100,75870,215],{"class":118},[675,75872,75874,75875,32343],{"id":75873},"_2-using-a-comparison-result-in-a-for-loop","2. Using a comparison result in a ",[17,75876,71],{},[91,75878,75880],{"className":93,"code":75879,"language":95,"meta":96,"style":96},"x = 20\n\nfor item in x > 10:\n    print(item)\n",[17,75881,75882,75890,75894,75910],{"__ignoreMap":96},[100,75883,75884,75886,75888],{"class":102,"line":103},[100,75885,54291],{"class":106},[100,75887,111],{"class":110},[100,75889,1669],{"class":122},[100,75891,75892],{"class":102,"line":135},[100,75893,139],{"emptyLinePlaceholder":138},[100,75895,75896,75898,75900,75902,75904,75906,75908],{"class":102,"line":142},[100,75897,71],{"class":145},[100,75899,7568],{"class":106},[100,75901,617],{"class":145},[100,75903,59659],{"class":106},[100,75905,1918],{"class":110},[100,75907,6550],{"class":122},[100,75909,149],{"class":118},[100,75911,75912,75914,75916,75918],{"class":102,"line":152},[100,75913,200],{"class":114},[100,75915,170],{"class":118},[100,75917,7555],{"class":178},[100,75919,215],{"class":118},[14,75921,75922,75925,75926,75928],{},[17,75923,75924],{},"x > 10"," evaluates to ",[17,75927,19246],{},", so the loop fails.",[675,75930,75932,75933,75935],{"id":75931},"_3-a-function-returns-bool-but-the-code-expects-a-list","3. A function returns ",[17,75934,46487],{},", but the code expects a list",[91,75937,75939],{"className":93,"code":75938,"language":95,"meta":96,"style":96},"def is_valid():\n    return True\n\nresult = is_valid()\n\nfor item in result:\n    print(item)\n",[17,75940,75941,75950,75956,75960,75970,75974,75987],{"__ignoreMap":96},[100,75942,75943,75945,75948],{"class":102,"line":103},[100,75944,1078],{"class":1077},[100,75946,75947],{"class":1081}," is_valid",[100,75949,1085],{"class":118},[100,75951,75952,75954],{"class":102,"line":135},[100,75953,2552],{"class":145},[100,75955,3588],{"class":158},[100,75957,75958],{"class":102,"line":142},[100,75959,139],{"emptyLinePlaceholder":138},[100,75961,75962,75964,75966,75968],{"class":102,"line":152},[100,75963,11556],{"class":106},[100,75965,111],{"class":110},[100,75967,75947],{"class":178},[100,75969,4734],{"class":118},[100,75971,75972],{"class":102,"line":164},[100,75973,139],{"emptyLinePlaceholder":138},[100,75975,75976,75978,75980,75982,75985],{"class":102,"line":185},[100,75977,71],{"class":145},[100,75979,7568],{"class":106},[100,75981,617],{"class":145},[100,75983,75984],{"class":106}," result",[100,75986,149],{"class":118},[100,75988,75989,75991,75993,75995],{"class":102,"line":197},[100,75990,200],{"class":114},[100,75992,170],{"class":118},[100,75994,7555],{"class":178},[100,75996,215],{"class":118},[14,75998,75999],{},"The function returns one boolean value, not a collection.",[675,76001,76003],{"id":76002},"_4-a-variable-was-overwritten-with-a-boolean","4. A variable was overwritten with a boolean",[91,76005,76007],{"className":93,"code":76006,"language":95,"meta":96,"style":96},"items = [\"a\", \"b\", \"c\"]\nitems = len(items) > 0   # items is now True\n\nfor item in items:\n    print(item)\n",[17,76008,76009,76041,76062,76066,76078],{"__ignoreMap":96},[100,76010,76011,76013,76015,76017,76019,76021,76023,76025,76027,76029,76031,76033,76035,76037,76039],{"class":102,"line":103},[100,76012,3095],{"class":106},[100,76014,111],{"class":110},[100,76016,594],{"class":118},[100,76018,206],{"class":205},[100,76020,295],{"class":209},[100,76022,206],{"class":205},[100,76024,126],{"class":118},[100,76026,1708],{"class":205},[100,76028,4027],{"class":209},[100,76030,206],{"class":205},[100,76032,126],{"class":118},[100,76034,1708],{"class":205},[100,76036,4036],{"class":209},[100,76038,206],{"class":205},[100,76040,603],{"class":118},[100,76042,76043,76045,76047,76049,76051,76053,76055,76057,76059],{"class":102,"line":135},[100,76044,3095],{"class":106},[100,76046,111],{"class":110},[100,76048,3141],{"class":114},[100,76050,170],{"class":118},[100,76052,3146],{"class":178},[100,76054,6155],{"class":118},[100,76056,31167],{"class":110},[100,76058,1921],{"class":122},[100,76060,76061],{"class":414},"   # items is now True\n",[100,76063,76064],{"class":102,"line":142},[100,76065,139],{"emptyLinePlaceholder":138},[100,76067,76068,76070,76072,76074,76076],{"class":102,"line":152},[100,76069,71],{"class":145},[100,76071,7568],{"class":106},[100,76073,617],{"class":145},[100,76075,12000],{"class":106},[100,76077,149],{"class":118},[100,76079,76080,76082,76084,76086],{"class":102,"line":164},[100,76081,200],{"class":114},[100,76083,170],{"class":118},[100,76085,7555],{"class":178},[100,76087,215],{"class":118},[14,76089,76090],{},"This is a very common mistake. Use clear variable names so one variable does not change meaning.",[675,76092,76094,76095,76097],{"id":76093},"_5-using-in-with-a-boolean-on-the-right-side","5. Using ",[17,76096,617],{}," with a boolean on the right side",[91,76099,76101],{"className":93,"code":76100,"language":95,"meta":96,"style":96},"value = \"a\"\ncontainer = True\n\nprint(value in container)\n",[17,76102,76103,76115,76124,76128],{"__ignoreMap":96},[100,76104,76105,76107,76109,76111,76113],{"class":102,"line":103},[100,76106,3318],{"class":106},[100,76108,111],{"class":110},[100,76110,1708],{"class":205},[100,76112,295],{"class":209},[100,76114,1714],{"class":205},[100,76116,76117,76120,76122],{"class":102,"line":135},[100,76118,76119],{"class":106},"container ",[100,76121,111],{"class":110},[100,76123,3588],{"class":158},[100,76125,76126],{"class":102,"line":142},[100,76127,139],{"emptyLinePlaceholder":138},[100,76129,76130,76132,76134,76136,76138,76141],{"class":102,"line":152},[100,76131,372],{"class":114},[100,76133,170],{"class":118},[100,76135,3318],{"class":178},[100,76137,617],{"class":145},[100,76139,76140],{"class":178}," container",[100,76142,215],{"class":118},[14,76144,76145],{},"This also raises:",[91,76147,76149],{"className":93,"code":76148,"language":95,"meta":96,"style":96},"TypeError: argument of type 'bool' is not iterable\n",[17,76150,76151],{"__ignoreMap":96},[100,76152,76153,76155,76157,76160,76162,76164,76166,76168,76170,76172],{"class":102,"line":103},[100,76154,3538],{"class":191},[100,76156,89],{"class":118},[100,76158,76159],{"class":106}," argument of ",[100,76161,1250],{"class":191},[100,76163,1274],{"class":205},[100,76165,46487],{"class":209},[100,76167,1280],{"class":205},[100,76169,16702],{"class":110},[100,76171,4921],{"class":110},[100,76173,75106],{"class":106},[14,76175,76176,76177,76179],{},"The right side of ",[17,76178,617],{}," must be something iterable, like a string, list, tuple, set, or dictionary.",[675,76181,76183],{"id":76182},"_6-passing-a-bool-into-a-function-that-expects-an-iterable","6. Passing a bool into a function that expects an iterable",[91,76185,76187],{"className":93,"code":76186,"language":95,"meta":96,"style":96},"value = True\n\nprint(list(value))\n",[17,76188,76189,76197,76201],{"__ignoreMap":96},[100,76190,76191,76193,76195],{"class":102,"line":103},[100,76192,3318],{"class":106},[100,76194,111],{"class":110},[100,76196,3588],{"class":158},[100,76198,76199],{"class":102,"line":135},[100,76200,139],{"emptyLinePlaceholder":138},[100,76202,76203,76205,76207,76209,76211,76213],{"class":102,"line":142},[100,76204,372],{"class":114},[100,76206,170],{"class":118},[100,76208,1235],{"class":191},[100,76210,170],{"class":118},[100,76212,757],{"class":178},[100,76214,182],{"class":118},[14,76216,76217,76218,76221],{},"This raises the same error because ",[17,76219,76220],{},"list()"," expects something iterable.",[14,76223,76224,76225,76228],{},"Similar problems can happen with ",[17,76226,76227],{},"tuple()"," and some other functions.",[77,76230,19830],{"id":19829},[14,76232,76233],{},"A few habits make this error much less likely:",[40,76235,76236,76248,76258,76261,76264],{},[43,76237,76238,76239,5870,76242,5894,76245],{},"Use clear boolean names like ",[17,76240,76241],{},"is_valid",[17,76243,76244],{},"has_items",[17,76246,76247],{},"found",[43,76249,76250,76251,5870,76253,5894,76255,76257],{},"Use plural names like ",[17,76252,3146],{},[17,76254,7820],{},[17,76256,179],{}," for collections",[43,76259,76260],{},"Check function return values carefully",[43,76262,76263],{},"Add small debug prints when a variable’s value is unclear",[43,76265,76266],{},"Avoid reusing the same variable name for different kinds of data",[675,76268,76270],{"id":76269},"better-naming-example","Better naming example",[91,76272,76274],{"className":93,"code":76273,"language":95,"meta":96,"style":96},"items = [\"apple\", \"banana\"]\nhas_items = len(items) > 0\n\nif has_items:\n    for item in items:\n        print(item)\n",[17,76275,76276,76300,76319,76323,76332,76344],{"__ignoreMap":96},[100,76277,76278,76280,76282,76284,76286,76288,76290,76292,76294,76296,76298],{"class":102,"line":103},[100,76279,3095],{"class":106},[100,76281,111],{"class":110},[100,76283,594],{"class":118},[100,76285,206],{"class":205},[100,76287,4148],{"class":209},[100,76289,206],{"class":205},[100,76291,126],{"class":118},[100,76293,1708],{"class":205},[100,76295,4157],{"class":209},[100,76297,206],{"class":205},[100,76299,603],{"class":118},[100,76301,76302,76305,76307,76309,76311,76313,76315,76317],{"class":102,"line":135},[100,76303,76304],{"class":106},"has_items ",[100,76306,111],{"class":110},[100,76308,3141],{"class":114},[100,76310,170],{"class":118},[100,76312,3146],{"class":178},[100,76314,6155],{"class":118},[100,76316,31167],{"class":110},[100,76318,2069],{"class":122},[100,76320,76321],{"class":102,"line":142},[100,76322,139],{"emptyLinePlaceholder":138},[100,76324,76325,76327,76330],{"class":102,"line":152},[100,76326,2736],{"class":145},[100,76328,76329],{"class":106}," has_items",[100,76331,149],{"class":118},[100,76333,76334,76336,76338,76340,76342],{"class":102,"line":164},[100,76335,51393],{"class":145},[100,76337,7568],{"class":106},[100,76339,617],{"class":145},[100,76341,12000],{"class":106},[100,76343,149],{"class":118},[100,76345,76346,76348,76350,76352],{"class":102,"line":185},[100,76347,167],{"class":114},[100,76349,170],{"class":118},[100,76351,7555],{"class":178},[100,76353,215],{"class":118},[14,76355,76356],{},"This is easier to read and avoids mixing up a boolean with a collection.",[77,76358,30537],{"id":30536},[14,76360,76361],{},"You may also run into similar errors:",[40,76363,76364,76372,76380,76385],{},[43,76365,76366],{},[295,76367,74872,76369],{"href":76368},"\u002Ferrors\u002Ftypeerror-int-object-is-not-iterable-fix\u002F",[17,76370,76371],{},"'int' object is not iterable",[43,76373,76374],{},[295,76375,74872,76377],{"href":76376},"\u002Ferrors\u002Ftypeerror-nonetype-object-is-not-iterable-fix\u002F",[17,76378,76379],{},"'NoneType' object is not iterable",[43,76381,76382],{},[17,76383,76384],{},"TypeError: 'function' object is not iterable",[43,76386,76387],{},[17,76388,76389],{},"TypeError: 'str' object cannot be interpreted as an integer",[14,76391,76392],{},"These errors have the same general cause: Python expected one kind of value, but got a different one.",[77,76394,1514],{"id":1513},[675,76396,76398],{"id":76397},"why-is-bool-not-iterable-in-python","Why is bool not iterable in Python?",[14,76400,76401,76402,3178,76404,76406],{},"Because a boolean is just one value, ",[17,76403,19246],{},[17,76405,1649],{},". It is not a collection of items.",[675,76408,76410],{"id":76409},"can-i-convert-a-bool-to-a-list","Can I convert a bool to a list?",[14,76412,76413],{},"Not directly in a meaningful way. Usually the real fix is to use the correct iterable value instead of the boolean.",[675,76415,76417],{"id":76416},"why-did-my-variable-become-a-bool","Why did my variable become a bool?",[14,76419,76420],{},"It may have been set by:",[40,76422,76423,76428,76431],{},[43,76424,76425,76426],{},"a comparison such as ",[17,76427,75924],{},[43,76429,76430],{},"a validation function",[43,76432,76433],{},"an accidental reassignment",[14,76435,241,76436,3266,76438,76440],{},[17,76437,7046],{},[17,76439,7052],{}," to check.",[675,76442,39391,76444,15957,76446,11353],{"id":76443},"should-i-use-if-instead-of-for",[17,76445,2736],{},[17,76447,71],{},[14,76449,76450],{},"Yes, if the value is meant to be a condition rather than a group of items.",[77,76452,1554],{"id":1553},[40,76454,76455,76459,76463,76469,76475,76481],{},[43,76456,76457],{},[295,76458,75221],{"href":75220},[43,76460,76461],{},[295,76462,73375],{"href":73374},[43,76464,76465],{},[295,76466,1572,76467,1576],{"href":4427},[17,76468,4430],{},[43,76470,76471],{},[295,76472,74872,76473],{"href":76376},[17,76474,76379],{},[43,76476,76477],{},[295,76478,74872,76479],{"href":76368},[17,76480,76371],{},[43,76482,76483],{},[295,76484,42267],{"href":3852},[1589,76486,76487],{},"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 .s39Yj, html code.shiki .s39Yj{--shiki-light:#39ADB5;--shiki-default:#005CC5;--shiki-dark:#79B8FF}html pre.shiki code .sutJx, html code.shiki .sutJx{--shiki-light:#90A4AE;--shiki-light-font-style:italic;--shiki-default:#6A737D;--shiki-default-font-style:inherit;--shiki-dark:#6A737D;--shiki-dark-font-style:inherit}html pre.shiki code .sP7_E, html code.shiki .sP7_E{--shiki-light:#39ADB5;--shiki-default:#24292E;--shiki-dark:#E1E4E8}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 .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 .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}",{"title":96,"searchDepth":135,"depth":135,"links":76489},[76490,76491,76492,76493,76496,76501,76505,76508,76520,76523,76524,76531],{"id":79,"depth":135,"text":80},{"id":5880,"depth":135,"text":5881},{"id":11030,"depth":135,"text":11031},{"id":25090,"depth":135,"text":25091,"children":76494},[76495],{"id":75154,"depth":142,"text":75155},{"id":75224,"depth":135,"text":75225,"children":76497},[76498,76499,76500],{"id":6370,"depth":142,"text":6371},{"id":75291,"depth":142,"text":75292},{"id":75381,"depth":142,"text":75382},{"id":75486,"depth":135,"text":75487,"children":76502},[76503,76504],{"id":75500,"depth":142,"text":6371},{"id":75544,"depth":142,"text":75292},{"id":75622,"depth":135,"text":75623,"children":76506},[76507],{"id":75694,"depth":142,"text":48552},{"id":75818,"depth":135,"text":75819,"children":76509},[76510,76512,76514,76516,76517,76519],{"id":75825,"depth":142,"text":76511},"1. Looping over True or False directly",{"id":75873,"depth":142,"text":76513},"2. Using a comparison result in a for loop",{"id":75931,"depth":142,"text":76515},"3. A function returns bool, but the code expects a list",{"id":76002,"depth":142,"text":76003},{"id":76093,"depth":142,"text":76518},"5. Using in with a boolean on the right side",{"id":76182,"depth":142,"text":76183},{"id":19829,"depth":135,"text":19830,"children":76521},[76522],{"id":76269,"depth":142,"text":76270},{"id":30536,"depth":135,"text":30537},{"id":1513,"depth":135,"text":1514,"children":76525},[76526,76527,76528,76529],{"id":76397,"depth":142,"text":76398},{"id":76409,"depth":142,"text":76410},{"id":76416,"depth":142,"text":76417},{"id":76443,"depth":142,"text":76530},"Should I use if instead of for?",{"id":1553,"depth":135,"text":1554},"Master typeerror bool object is not iterable fix in our comprehensive Python beginner guide.",{},"\u002Ferrors\u002Ftypeerror-bool-object-is-not-iterable-fix",{"title":74866,"description":76532},"errors\u002Ftypeerror-bool-object-is-not-iterable-fix","V_foH2vEJr5sqr7DcOgq1t8IAe62on51C4pXr5eUjh4",{"id":76539,"title":76540,"body":76541,"description":77671,"extension":1623,"meta":77672,"navigation":138,"path":77673,"seo":77674,"stem":77675,"__hash__":77676},"content\u002Ferrors\u002Ftypeerror-can-only-concatenate-str-not-int-to-str-fix.md","TypeError: can only concatenate str (not \"int\") to str (Fix)",{"type":7,"value":76542,"toc":77652},[76543,76546,76551,76562,76564,76637,76640,76642,76661,76667,76673,76690,76692,76695,76720,76723,76769,76779,76781,76784,76806,76809,76839,76841,76857,76862,76866,76873,76908,76910,76924,76927,76929,76976,76978,76992,76996,77000,77003,77036,77038,77050,77053,77056,77110,77112,77131,77135,77139,77142,77174,77176,77188,77196,77199,77203,77206,77225,77227,77288,77290,77331,77333,77357,77366,77370,77373,77376,77418,77426,77485,77487,77500,77503,77511,77513,77516,77546,77552,77554,77558,77561,77565,77570,77574,77581,77583,77608,77613,77617,77625,77627,77649],[10,76544,76540],{"id":76545},"typeerror-can-only-concatenate-str-not-int-to-str-fix",[14,76547,76548,76549,290],{},"This error happens when Python tries to join a string and an integer with ",[17,76550,6547],{},[14,76552,10747,76553,76555,76556,76559,76560,290],{},[17,76554,6547],{}," can join strings only when ",[22,76557,76558],{},"both values are strings",". If one value is text and the other is a number, Python raises a ",[17,76561,3538],{},[77,76563,80],{"id":79},[91,76565,76567],{"className":93,"code":76566,"language":95,"meta":96,"style":96},"name = \"Age: \"\nage = 25\nprint(name + str(age))\n\n# or\nprint(f\"Age: {age}\")\n",[17,76568,76569,76581,76589,76607,76611,76616],{"__ignoreMap":96},[100,76570,76571,76573,76575,76577,76579],{"class":102,"line":103},[100,76572,1691],{"class":106},[100,76574,111],{"class":110},[100,76576,1708],{"class":205},[100,76578,16775],{"class":209},[100,76580,1714],{"class":205},[100,76582,76583,76585,76587],{"class":102,"line":135},[100,76584,1664],{"class":106},[100,76586,111],{"class":110},[100,76588,16667],{"class":122},[100,76590,76591,76593,76595,76597,76599,76601,76603,76605],{"class":102,"line":142},[100,76592,372],{"class":114},[100,76594,170],{"class":118},[100,76596,1691],{"class":178},[100,76598,6547],{"class":110},[100,76600,2871],{"class":191},[100,76602,170],{"class":118},[100,76604,2770],{"class":178},[100,76606,182],{"class":118},[100,76608,76609],{"class":102,"line":152},[100,76610,139],{"emptyLinePlaceholder":138},[100,76612,76613],{"class":102,"line":164},[100,76614,76615],{"class":414},"# or\n",[100,76617,76618,76620,76622,76624,76627,76629,76631,76633,76635],{"class":102,"line":185},[100,76619,372],{"class":114},[100,76621,170],{"class":118},[100,76623,18199],{"class":1077},[100,76625,76626],{"class":209},"\"Age: ",[100,76628,16490],{"class":122},[100,76630,2770],{"class":178},[100,76632,18216],{"class":122},[100,76634,206],{"class":209},[100,76636,215],{"class":118},[14,76638,76639],{},"Convert the integer to a string before concatenation, or use an f-string for cleaner output.",[77,76641,5881],{"id":5880},[40,76643,76644,76650,76655],{},[43,76645,76646,76647,76649],{},"Python uses ",[17,76648,6547],{}," to join strings only when both values are strings.",[43,76651,76652,76653,290],{},"If one value is a string and the other is an integer, Python raises a ",[17,76654,3538],{},[43,76656,76657,76658,76660],{},"The message means Python cannot automatically turn the ",[17,76659,3499],{}," into text in this case.",[14,76662,55737,76663,76666],{},[17,76664,76665],{},"\"Age: \" + \"25\""," works because both sides are strings.",[14,76668,38150,76669,76672],{},[17,76670,76671],{},"\"Age: \" + 25"," fails because:",[40,76674,76675,76683],{},[43,76676,76677,76680,76681],{},[17,76678,76679],{},"\"Age: \""," is a ",[17,76682,3422],{},[43,76684,76685,76687,76688],{},[17,76686,27135],{}," is an ",[17,76689,3499],{},[77,76691,25091],{"id":25090},[14,76693,76694],{},"This error usually appears when:",[40,76696,76697,76703,76708,76714],{},[43,76698,76699,76700,76702],{},"You used ",[17,76701,6547],{}," between text and a number.",[43,76704,76705,76706,290],{},"A variable you expected to be text is actually an ",[17,76707,3499],{},[43,76709,76710,76711,76713],{},"Input was converted with ",[17,76712,6119],{}," earlier, then joined to a string later.",[43,76715,76716,76717,76719],{},"The error appears in ",[17,76718,13235],{}," statements, labels, and messages.",[14,76721,76722],{},"A common beginner mistake looks like this:",[91,76724,76726],{"className":93,"code":76725,"language":95,"meta":96,"style":96},"age = int(input(\"Enter your age: \"))\nprint(\"Your age is: \" + age)\n",[17,76727,76728,76750],{"__ignoreMap":96},[100,76729,76730,76732,76734,76736,76738,76740,76742,76744,76746,76748],{"class":102,"line":103},[100,76731,1664],{"class":106},[100,76733,111],{"class":110},[100,76735,3389],{"class":191},[100,76737,170],{"class":118},[100,76739,6210],{"class":114},[100,76741,170],{"class":118},[100,76743,206],{"class":205},[100,76745,7173],{"class":209},[100,76747,206],{"class":205},[100,76749,182],{"class":118},[100,76751,76752,76754,76756,76758,76761,76763,76765,76767],{"class":102,"line":135},[100,76753,372],{"class":114},[100,76755,170],{"class":118},[100,76757,206],{"class":205},[100,76759,76760],{"class":209},"Your age is: ",[100,76762,206],{"class":205},[100,76764,2895],{"class":110},[100,76766,31058],{"class":178},[100,76768,215],{"class":118},[14,76770,76771,76773,76774,76776,76777,290],{},[17,76772,2770],{}," is no longer text after ",[17,76775,7200],{},". It is now an integer, so Python cannot concatenate it to a string with ",[17,76778,6547],{},[77,76780,11031],{"id":11030},[14,76782,76783],{},"Here is a short example:",[91,76785,76786],{"className":93,"code":16762,"language":95,"meta":96,"style":96},[17,76787,76788],{"__ignoreMap":96},[100,76789,76790,76792,76794,76796,76798,76800,76802,76804],{"class":102,"line":103},[100,76791,372],{"class":114},[100,76793,170],{"class":118},[100,76795,206],{"class":205},[100,76797,16775],{"class":209},[100,76799,206],{"class":205},[100,76801,2895],{"class":110},[100,76803,16782],{"class":122},[100,76805,215],{"class":118},[14,76807,76808],{},"You will see an error like this:",[91,76810,76811],{"className":93,"code":16789,"language":95,"meta":96,"style":96},[17,76812,76813],{"__ignoreMap":96},[100,76814,76815,76817,76819,76821,76823,76825,76827,76829,76831,76833,76835,76837],{"class":102,"line":103},[100,76816,3538],{"class":191},[100,76818,89],{"class":118},[100,76820,16800],{"class":106},[100,76822,3422],{"class":191},[100,76824,16805],{"class":118},[100,76826,1059],{"class":145},[100,76828,1708],{"class":205},[100,76830,3499],{"class":209},[100,76832,206],{"class":205},[100,76834,6155],{"class":118},[100,76836,9932],{"class":106},[100,76838,16820],{"class":191},[14,76840,32043],{},[40,76842,76843,76850],{},[43,76844,76845,76846,23456,76848],{},"Left side: ",[17,76847,76679],{},[17,76849,3422],{},[43,76851,76852,76853,23456,76855],{},"Right side: ",[17,76854,27135],{},[17,76856,3499],{},[14,76858,76859,76860,290],{},"Python does not mix these types with ",[17,76861,6547],{},[77,76863,76865],{"id":76864},"fix-1-convert-the-integer-with-str","Fix 1: Convert the integer with str()",[14,76867,241,76868,76872],{},[295,76869,76870],{"href":69897},[17,76871,69900],{}," when you need text output.",[91,76874,76876],{"className":93,"code":76875,"language":95,"meta":96,"style":96},"age = 25\nprint(\"Age: \" + str(age))\n",[17,76877,76878,76886],{"__ignoreMap":96},[100,76879,76880,76882,76884],{"class":102,"line":103},[100,76881,1664],{"class":106},[100,76883,111],{"class":110},[100,76885,16667],{"class":122},[100,76887,76888,76890,76892,76894,76896,76898,76900,76902,76904,76906],{"class":102,"line":135},[100,76889,372],{"class":114},[100,76891,170],{"class":118},[100,76893,206],{"class":205},[100,76895,16775],{"class":209},[100,76897,206],{"class":205},[100,76899,2895],{"class":110},[100,76901,2871],{"class":191},[100,76903,170],{"class":118},[100,76905,2770],{"class":178},[100,76907,182],{"class":118},[14,76909,218],{},[91,76911,76913],{"className":93,"code":76912,"language":95,"meta":96,"style":96},"Age: 25\n",[17,76914,76915],{"__ignoreMap":96},[100,76916,76917,76920,76922],{"class":102,"line":103},[100,76918,76919],{"class":106},"Age",[100,76921,89],{"class":118},[100,76923,16667],{"class":122},[14,76925,76926],{},"This is the most direct fix when you are building a string step by step.",[14,76928,12066],{},[91,76930,76932],{"className":93,"code":76931,"language":95,"meta":96,"style":96},"total = 50\nmessage = \"Total: \" + str(total)\nprint(message)\n",[17,76933,76934,76943,76966],{"__ignoreMap":96},[100,76935,76936,76938,76940],{"class":102,"line":103},[100,76937,53376],{"class":106},[100,76939,111],{"class":110},[100,76941,76942],{"class":122}," 50\n",[100,76944,76945,76947,76949,76951,76954,76956,76958,76960,76962,76964],{"class":102,"line":135},[100,76946,6868],{"class":106},[100,76948,111],{"class":110},[100,76950,1708],{"class":205},[100,76952,76953],{"class":209},"Total: ",[100,76955,206],{"class":205},[100,76957,2895],{"class":110},[100,76959,2871],{"class":191},[100,76961,170],{"class":118},[100,76963,2520],{"class":178},[100,76965,215],{"class":118},[100,76967,76968,76970,76972,76974],{"class":102,"line":142},[100,76969,372],{"class":114},[100,76971,170],{"class":118},[100,76973,1836],{"class":178},[100,76975,215],{"class":118},[14,76977,218],{},[91,76979,76981],{"className":93,"code":76980,"language":95,"meta":96,"style":96},"Total: 50\n",[17,76982,76983],{"__ignoreMap":96},[100,76984,76985,76988,76990],{"class":102,"line":103},[100,76986,76987],{"class":106},"Total",[100,76989,89],{"class":118},[100,76991,76942],{"class":122},[14,76993,19522,76994,290],{},[295,76995,6570],{"href":6569},[77,76997,76999],{"id":76998},"fix-2-use-f-strings","Fix 2: Use f-strings",[14,77001,77002],{},"An f-string lets you place variables directly inside a string.",[91,77004,77006],{"className":93,"code":77005,"language":95,"meta":96,"style":96},"age = 25\nprint(f\"Age: {age}\")\n",[17,77007,77008,77016],{"__ignoreMap":96},[100,77009,77010,77012,77014],{"class":102,"line":103},[100,77011,1664],{"class":106},[100,77013,111],{"class":110},[100,77015,16667],{"class":122},[100,77017,77018,77020,77022,77024,77026,77028,77030,77032,77034],{"class":102,"line":135},[100,77019,372],{"class":114},[100,77021,170],{"class":118},[100,77023,18199],{"class":1077},[100,77025,76626],{"class":209},[100,77027,16490],{"class":122},[100,77029,2770],{"class":178},[100,77031,18216],{"class":122},[100,77033,206],{"class":209},[100,77035,215],{"class":118},[14,77037,218],{},[91,77039,77040],{"className":93,"code":76912,"language":95,"meta":96,"style":96},[17,77041,77042],{"__ignoreMap":96},[100,77043,77044,77046,77048],{"class":102,"line":103},[100,77045,76919],{"class":106},[100,77047,89],{"class":118},[100,77049,16667],{"class":122},[14,77051,77052],{},"This is often the cleanest option because Python handles the text conversion for display.",[14,77054,77055],{},"You can include multiple values too:",[91,77057,77059],{"className":93,"code":77058,"language":95,"meta":96,"style":96},"name = \"Sam\"\nage = 25\nprint(f\"{name} is {age} years old.\")\n",[17,77060,77061,77073,77081],{"__ignoreMap":96},[100,77062,77063,77065,77067,77069,77071],{"class":102,"line":103},[100,77064,1691],{"class":106},[100,77066,111],{"class":110},[100,77068,1708],{"class":205},[100,77070,29329],{"class":209},[100,77072,1714],{"class":205},[100,77074,77075,77077,77079],{"class":102,"line":135},[100,77076,1664],{"class":106},[100,77078,111],{"class":110},[100,77080,16667],{"class":122},[100,77082,77083,77085,77087,77089,77091,77093,77095,77097,77099,77101,77103,77105,77108],{"class":102,"line":142},[100,77084,372],{"class":114},[100,77086,170],{"class":118},[100,77088,18199],{"class":1077},[100,77090,206],{"class":209},[100,77092,16490],{"class":122},[100,77094,2853],{"class":178},[100,77096,18216],{"class":122},[100,77098,2025],{"class":209},[100,77100,16490],{"class":122},[100,77102,2770],{"class":178},[100,77104,18216],{"class":122},[100,77106,77107],{"class":209}," years old.\"",[100,77109,215],{"class":118},[14,77111,218],{},[91,77113,77115],{"className":93,"code":77114,"language":95,"meta":96,"style":96},"Sam is 25 years old.\n",[17,77116,77117],{"__ignoreMap":96},[100,77118,77119,77122,77124,77126,77129],{"class":102,"line":103},[100,77120,77121],{"class":106},"Sam ",[100,77123,4918],{"class":110},[100,77125,16782],{"class":122},[100,77127,77128],{"class":106}," years old",[100,77130,75606],{"class":118},[14,77132,1160,77133,290],{},[295,77134,69626],{"href":69625},[77,77136,77138],{"id":77137},"fix-3-use-print-with-commas","Fix 3: Use print() with commas",[14,77140,77141],{},"For simple output, you do not need string concatenation at all.",[91,77143,77145],{"className":93,"code":77144,"language":95,"meta":96,"style":96},"age = 25\nprint(\"Age:\", age)\n",[17,77146,77147,77155],{"__ignoreMap":96},[100,77148,77149,77151,77153],{"class":102,"line":103},[100,77150,1664],{"class":106},[100,77152,111],{"class":110},[100,77154,16667],{"class":122},[100,77156,77157,77159,77161,77163,77166,77168,77170,77172],{"class":102,"line":135},[100,77158,372],{"class":114},[100,77160,170],{"class":118},[100,77162,206],{"class":205},[100,77164,77165],{"class":209},"Age:",[100,77167,206],{"class":205},[100,77169,126],{"class":118},[100,77171,31058],{"class":178},[100,77173,215],{"class":118},[14,77175,218],{},[91,77177,77178],{"className":93,"code":76912,"language":95,"meta":96,"style":96},[17,77179,77180],{"__ignoreMap":96},[100,77181,77182,77184,77186],{"class":102,"line":103},[100,77183,76919],{"class":106},[100,77185,89],{"class":118},[100,77187,16667],{"class":122},[14,77189,2925,77190,77195],{},[295,77191,77193],{"href":77192},"\u002Freference\u002Fpython-print-function-explained\u002F",[17,77194,13235],{}," can display different data types together.",[14,77197,77198],{},"This is useful when you just want to show values on the screen and do not need to build one combined string first.",[77,77200,77202],{"id":77201},"how-to-debug-this-error","How to debug this error",[14,77204,77205],{},"When you see this error:",[3282,77207,77208,77211,77216,77219],{},[43,77209,77210],{},"Check the line mentioned in the traceback.",[43,77212,19653,77213,77215],{},[17,77214,6547],{}," used between values.",[43,77217,77218],{},"Confirm the type of each variable.",[43,77220,77221,77222,77224],{},"Check whether you used ",[17,77223,6119],{}," earlier.",[14,77226,5295],{},[91,77228,77230],{"className":93,"code":77229,"language":95,"meta":96,"style":96},"type(age)\nprint(type(age))\nprint(age)\nprint(\"Age: \" + str(age))\n",[17,77231,77232,77242,77256,77266],{"__ignoreMap":96},[100,77233,77234,77236,77238,77240],{"class":102,"line":103},[100,77235,1250],{"class":191},[100,77237,170],{"class":118},[100,77239,2770],{"class":178},[100,77241,215],{"class":118},[100,77243,77244,77246,77248,77250,77252,77254],{"class":102,"line":135},[100,77245,372],{"class":114},[100,77247,170],{"class":118},[100,77249,1250],{"class":191},[100,77251,170],{"class":118},[100,77253,2770],{"class":178},[100,77255,182],{"class":118},[100,77257,77258,77260,77262,77264],{"class":102,"line":142},[100,77259,372],{"class":114},[100,77261,170],{"class":118},[100,77263,2770],{"class":178},[100,77265,215],{"class":118},[100,77267,77268,77270,77272,77274,77276,77278,77280,77282,77284,77286],{"class":102,"line":152},[100,77269,372],{"class":114},[100,77271,170],{"class":118},[100,77273,206],{"class":205},[100,77275,16775],{"class":209},[100,77277,206],{"class":205},[100,77279,2895],{"class":110},[100,77281,2871],{"class":191},[100,77283,170],{"class":118},[100,77285,2770],{"class":178},[100,77287,182],{"class":118},[14,77289,1844],{},[91,77291,77293],{"className":93,"code":77292,"language":95,"meta":96,"style":96},"age = 25\n\nprint(type(age))\nprint(age)\n",[17,77294,77295,77303,77307,77321],{"__ignoreMap":96},[100,77296,77297,77299,77301],{"class":102,"line":103},[100,77298,1664],{"class":106},[100,77300,111],{"class":110},[100,77302,16667],{"class":122},[100,77304,77305],{"class":102,"line":135},[100,77306,139],{"emptyLinePlaceholder":138},[100,77308,77309,77311,77313,77315,77317,77319],{"class":102,"line":142},[100,77310,372],{"class":114},[100,77312,170],{"class":118},[100,77314,1250],{"class":191},[100,77316,170],{"class":118},[100,77318,2770],{"class":178},[100,77320,182],{"class":118},[100,77322,77323,77325,77327,77329],{"class":102,"line":152},[100,77324,372],{"class":114},[100,77326,170],{"class":118},[100,77328,2770],{"class":178},[100,77330,215],{"class":118},[14,77332,218],{},[91,77334,77336],{"className":93,"code":77335,"language":95,"meta":96,"style":96},"\u003Cclass 'int'>\n25\n",[17,77337,77338,77352],{"__ignoreMap":96},[100,77339,77340,77342,77344,77346,77348,77350],{"class":102,"line":103},[100,77341,2782],{"class":110},[100,77343,3417],{"class":1077},[100,77345,1274],{"class":205},[100,77347,3499],{"class":209},[100,77349,1280],{"class":205},[100,77351,1980],{"class":110},[100,77353,77354],{"class":102,"line":135},[100,77355,77356],{"class":122},"25\n",[14,77358,77359,77360,77362,77363,77365],{},"If a value is ",[17,77361,3499],{}," and you are trying to join it to a string with ",[17,77364,6547],{},", convert it only when creating output text.",[77,77367,77369],{"id":77368},"when-not-to-use-str","When not to use str()",[14,77371,77372],{},"Do not convert numbers to strings if you still need to do math with them.",[14,77374,77375],{},"This is not a good idea:",[91,77377,77379],{"className":93,"code":77378,"language":95,"meta":96,"style":96},"price = str(10)\nquantity = 2\n\n# This will not do math correctly\n# print(price * quantity)\n",[17,77380,77381,77395,77404,77408,77413],{"__ignoreMap":96},[100,77382,77383,77385,77387,77389,77391,77393],{"class":102,"line":103},[100,77384,54887],{"class":106},[100,77386,111],{"class":110},[100,77388,2871],{"class":191},[100,77390,170],{"class":118},[100,77392,356],{"class":122},[100,77394,215],{"class":118},[100,77396,77397,77400,77402],{"class":102,"line":135},[100,77398,77399],{"class":106},"quantity ",[100,77401,111],{"class":110},[100,77403,3564],{"class":122},[100,77405,77406],{"class":102,"line":142},[100,77407,139],{"emptyLinePlaceholder":138},[100,77409,77410],{"class":102,"line":152},[100,77411,77412],{"class":414},"# This will not do math correctly\n",[100,77414,77415],{"class":102,"line":164},[100,77416,77417],{"class":414},"# print(price * quantity)\n",[14,77419,77420,77421,3178,77423,77425],{},"Keep numbers as ",[17,77422,3499],{},[17,77424,60476],{}," for calculations:",[91,77427,77429],{"className":93,"code":77428,"language":95,"meta":96,"style":96},"price = 10\nquantity = 2\ntotal = price * quantity\n\nprint(f\"Total: {total}\")\n",[17,77430,77431,77439,77447,77460,77464],{"__ignoreMap":96},[100,77432,77433,77435,77437],{"class":102,"line":103},[100,77434,54887],{"class":106},[100,77436,111],{"class":110},[100,77438,2181],{"class":122},[100,77440,77441,77443,77445],{"class":102,"line":135},[100,77442,77399],{"class":106},[100,77444,111],{"class":110},[100,77446,3564],{"class":122},[100,77448,77449,77451,77453,77455,77457],{"class":102,"line":142},[100,77450,53376],{"class":106},[100,77452,111],{"class":110},[100,77454,3220],{"class":106},[100,77456,3252],{"class":110},[100,77458,77459],{"class":106}," quantity\n",[100,77461,77462],{"class":102,"line":152},[100,77463,139],{"emptyLinePlaceholder":138},[100,77465,77466,77468,77470,77472,77475,77477,77479,77481,77483],{"class":102,"line":164},[100,77467,372],{"class":114},[100,77469,170],{"class":118},[100,77471,18199],{"class":1077},[100,77473,77474],{"class":209},"\"Total: ",[100,77476,16490],{"class":122},[100,77478,2520],{"class":178},[100,77480,18216],{"class":122},[100,77482,206],{"class":209},[100,77484,215],{"class":118},[14,77486,218],{},[91,77488,77490],{"className":93,"code":77489,"language":95,"meta":96,"style":96},"Total: 20\n",[17,77491,77492],{"__ignoreMap":96},[100,77493,77494,77496,77498],{"class":102,"line":103},[100,77495,76987],{"class":106},[100,77497,89],{"class":118},[100,77499,1669],{"class":122},[14,77501,77502],{},"A good rule is:",[40,77504,77505,77508],{},[43,77506,77507],{},"Use numbers for calculations",[43,77509,77510],{},"Convert to string only when creating text output",[77,77512,1344],{"id":1343},[14,77514,77515],{},"Here are some common situations that trigger this error:",[40,77517,77518,77528,77533,77539],{},[43,77519,1357,77520,77523,77524,76687,77526],{},[17,77521,77522],{},"print(\"Total: \" + total)"," when ",[17,77525,2520],{},[17,77527,3499],{},[43,77529,77530,77531],{},"Joining user-facing text with a number using ",[17,77532,6547],{},[43,77534,77535,77536,77538],{},"Converting input to ",[17,77537,6119],{}," and later trying to concatenate it to text",[43,77540,77541,77542,9932,77544],{},"Assuming Python will automatically convert ",[17,77543,3499],{},[17,77545,3422],{},[14,77547,77548,77549,77551],{},"This error is related to other ",[17,77550,6547],{}," type problems too. If your code is adding two incompatible values, see TypeError: unsupported operand type(s) for +.",[77,77553,1514],{"id":1513},[675,77555,77557],{"id":77556},"why-does-python-not-automatically-convert-int-to-str-here","Why does Python not automatically convert int to str here?",[14,77559,77560],{},"Python keeps numbers and text as different types to avoid unclear behavior and hidden bugs.",[675,77562,77564],{"id":77563},"should-i-use-str-or-an-f-string","Should I use str() or an f-string?",[14,77566,241,77567,77569],{},[17,77568,69900],{}," for a direct fix. Use f-strings when you want cleaner output formatting.",[675,77571,77573],{"id":77572},"does-this-happen-with-float-values-too","Does this happen with float values too?",[14,77575,77576,77577,42154,77579,290],{},"Yes. The same problem happens if you try to concatenate a string and a ",[17,77578,60476],{},[17,77580,6547],{},[14,77582,1844],{},[91,77584,77586],{"className":93,"code":77585,"language":95,"meta":96,"style":96},"print(\"Price: \" + 9.99)\n",[17,77587,77588],{"__ignoreMap":96},[100,77589,77590,77592,77594,77596,77599,77601,77603,77606],{"class":102,"line":103},[100,77591,372],{"class":114},[100,77593,170],{"class":118},[100,77595,206],{"class":205},[100,77597,77598],{"class":209},"Price: ",[100,77600,206],{"class":205},[100,77602,2895],{"class":110},[100,77604,77605],{"class":122}," 9.99",[100,77607,215],{"class":118},[14,77609,77610,77611,290],{},"This also raises a ",[17,77612,3538],{},[675,77614,77616],{"id":77615},"can-print-avoid-this-error","Can print() avoid this error?",[14,77618,10626,77619,9100,77622,77624],{},[17,77620,77621],{},"print(\"Value:\", number)",[17,77623,13235],{}," can display multiple value types together.",[77,77626,1554],{"id":1553},[40,77628,77629,77635,77639,77643],{},[43,77630,77631],{},[295,77632,77633,1576],{"href":69897},[17,77634,69900],{},[43,77636,77637],{},[295,77638,7326],{"href":6569},[43,77640,77641],{},[295,77642,70021],{"href":69625},[43,77644,77645],{},[295,77646,77647,1576],{"href":77192},[17,77648,13235],{},[1589,77650,77651],{},"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 .srdBf, html code.shiki .srdBf{--shiki-light:#F76D47;--shiki-default:#005CC5;--shiki-dark:#79B8FF}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 pre.shiki code .sZMiF, html code.shiki .sZMiF{--shiki-light:#E2931D;--shiki-default:#005CC5;--shiki-dark:#79B8FF}html pre.shiki code .sutJx, html code.shiki .sutJx{--shiki-light:#90A4AE;--shiki-light-font-style:italic;--shiki-default:#6A737D;--shiki-default-font-style:inherit;--shiki-dark:#6A737D;--shiki-dark-font-style:inherit}html pre.shiki code .sbsja, html code.shiki .sbsja{--shiki-light:#9C3EDA;--shiki-default:#D73A49;--shiki-dark:#F97583}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 .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":96,"searchDepth":135,"depth":135,"links":77653},[77654,77655,77656,77657,77658,77659,77660,77661,77662,77663,77664,77670],{"id":79,"depth":135,"text":80},{"id":5880,"depth":135,"text":5881},{"id":25090,"depth":135,"text":25091},{"id":11030,"depth":135,"text":11031},{"id":76864,"depth":135,"text":76865},{"id":76998,"depth":135,"text":76999},{"id":77137,"depth":135,"text":77138},{"id":77201,"depth":135,"text":77202},{"id":77368,"depth":135,"text":77369},{"id":1343,"depth":135,"text":1344},{"id":1513,"depth":135,"text":1514,"children":77665},[77666,77667,77668,77669],{"id":77556,"depth":142,"text":77557},{"id":77563,"depth":142,"text":77564},{"id":77572,"depth":142,"text":77573},{"id":77615,"depth":142,"text":77616},{"id":1553,"depth":135,"text":1554},"Master typeerror can only concatenate str not int to str fix in our comprehensive Python beginner guide.",{},"\u002Ferrors\u002Ftypeerror-can-only-concatenate-str-not-int-to-str-fix",{"title":76540,"description":77671},"errors\u002Ftypeerror-can-only-concatenate-str-not-int-to-str-fix","FprxNcRgyh6-nBSIXWkOb7Y0ETQbKImBXnlm2JsuRtI",{"id":77678,"title":77679,"body":77680,"description":79499,"extension":1623,"meta":79500,"navigation":138,"path":79501,"seo":79502,"stem":79503,"__hash__":79504},"content\u002Ferrors\u002Ftypeerror-cannot-unpack-non-iterable-object-fix.md","TypeError: cannot unpack non-iterable object (Fix)",{"type":7,"value":77681,"toc":79467},[77682,77685,77688,77702,77704,77797,77799,77810,77813,77815,77817,77846,77849,77866,77872,77891,77901,77907,77909,77912,77929,77931,77955,77957,77959,77976,77981,77984,78008,78011,78019,78023,78026,78049,78051,78059,78065,78067,78083,78085,78098,78101,78105,78108,78112,78157,78159,78171,78175,78214,78216,78227,78231,78234,78296,78298,78310,78315,78319,78322,78324,78362,78364,78384,78387,78391,78394,78451,78453,78477,78480,78486,78552,78554,78581,78584,78603,78606,78627,78633,78637,78639,78643,78703,78709,78713,78764,78766,78782,78786,78864,78866,78891,78895,78898,78973,78976,78979,79058,79060,79077,79084,79088,79090,79123,79125,79188,79190,79247,79249,79272,79278,79282,79285,79302,79305,79313,79316,79330,79336,79338,79342,79353,79357,79360,79362,79398,79400,79409,79413,79419,79423,79432,79434,79464],[10,77683,77679],{"id":77684},"typeerror-cannot-unpack-non-iterable-object-fix",[14,77686,77687],{},"This error happens when Python tries to split one value into multiple variables, but that value is not something Python can unpack.",[14,77689,77690,77691,77694,77695,3266,77697,77699,77700,290],{},"For example, Python can unpack a tuple like ",[17,77692,77693],{},"(1, 2)"," into ",[17,77696,295],{},[17,77698,4027],{},". But it cannot unpack a single integer like ",[17,77701,3557],{},[77,77703,80],{"id":79},[91,77705,77707],{"className":93,"code":77706,"language":95,"meta":96,"style":96},"value = 5\n# Wrong:\n# a, b = value\n\n# Fix 1: assign to one variable\nnumber = value\n\n# Fix 2: unpack an actual iterable\npair = (1, 2)\na, b = pair\nprint(a, b)\n",[17,77708,77709,77717,77721,77726,77730,77735,77744,77748,77753,77770,77783],{"__ignoreMap":96},[100,77710,77711,77713,77715],{"class":102,"line":103},[100,77712,3318],{"class":106},[100,77714,111],{"class":110},[100,77716,37580],{"class":122},[100,77718,77719],{"class":102,"line":135},[100,77720,74927],{"class":414},[100,77722,77723],{"class":102,"line":142},[100,77724,77725],{"class":414},"# a, b = value\n",[100,77727,77728],{"class":102,"line":152},[100,77729,139],{"emptyLinePlaceholder":138},[100,77731,77732],{"class":102,"line":164},[100,77733,77734],{"class":414},"# Fix 1: assign to one variable\n",[100,77736,77737,77739,77741],{"class":102,"line":185},[100,77738,1901],{"class":106},[100,77740,111],{"class":110},[100,77742,77743],{"class":106}," value\n",[100,77745,77746],{"class":102,"line":197},[100,77747,139],{"emptyLinePlaceholder":138},[100,77749,77750],{"class":102,"line":771},[100,77751,77752],{"class":414},"# Fix 2: unpack an actual iterable\n",[100,77754,77755,77758,77760,77762,77764,77766,77768],{"class":102,"line":787},[100,77756,77757],{"class":106},"pair ",[100,77759,111],{"class":110},[100,77761,16805],{"class":118},[100,77763,123],{"class":122},[100,77765,126],{"class":118},[100,77767,129],{"class":122},[100,77769,215],{"class":118},[100,77771,77772,77774,77776,77778,77780],{"class":102,"line":5816},[100,77773,295],{"class":106},[100,77775,126],{"class":118},[100,77777,25360],{"class":106},[100,77779,111],{"class":110},[100,77781,77782],{"class":106}," pair\n",[100,77784,77785,77787,77789,77791,77793,77795],{"class":102,"line":5833},[100,77786,372],{"class":114},[100,77788,170],{"class":118},[100,77790,295],{"class":178},[100,77792,126],{"class":118},[100,77794,15869],{"class":178},[100,77796,215],{"class":118},[14,77798,218],{},[91,77800,77802],{"className":93,"code":77801,"language":95,"meta":96,"style":96},"1 2\n",[17,77803,77804],{"__ignoreMap":96},[100,77805,77806,77808],{"class":102,"line":103},[100,77807,123],{"class":122},[100,77809,3564],{"class":122},[14,77811,77812],{},"Unpacking only works when the value on the right side is an iterable, such as a tuple, list, or string, and it contains the expected number of items.",[77,77814,5881],{"id":5880},[14,77816,9328],{},[40,77818,77819,77822,77825],{},[43,77820,77821],{},"Python tried to split one value into multiple variables.",[43,77823,77824],{},"The value was not iterable, so Python could not unpack it.",[43,77826,77827,77828],{},"Common non-iterable values include:\n",[40,77829,77830,77834,77838,77842],{},[43,77831,77832],{},[17,77833,3499],{},[43,77835,77836],{},[17,77837,60476],{},[43,77839,77840],{},[17,77841,46487],{},[43,77843,77844],{},[17,77845,930],{},[14,77847,77848],{},"A very common pattern that causes this is:",[91,77850,77852],{"className":93,"code":77851,"language":95,"meta":96,"style":96},"a, b = value\n",[17,77853,77854],{"__ignoreMap":96},[100,77855,77856,77858,77860,77862,77864],{"class":102,"line":103},[100,77857,295],{"class":106},[100,77859,126],{"class":118},[100,77861,25360],{"class":106},[100,77863,111],{"class":110},[100,77865,77743],{"class":106},[14,77867,77868,77869,77871],{},"This only works if ",[17,77870,757],{}," is something like:",[40,77873,77874,77879,77885],{},[43,77875,77876,77877],{},"a tuple: ",[17,77878,77693],{},[43,77880,77881,77882],{},"a list: ",[17,77883,77884],{},"[1, 2]",[43,77886,77887,77888],{},"a string: ",[17,77889,77890],{},"\"hi\"",[14,77892,11734,77893,77895,77896,77898,77899,290],{},[17,77894,757],{}," is just one number like ",[17,77897,3557],{},", Python raises a ",[17,77900,3538],{},[14,77902,77903,77904,290],{},"If you are not familiar with iterables, see ",[295,77905,298],{"href":77906},"\u002Flearn\u002Fiterators-and-iterable-objects-explained\u002F",[77,77908,26289],{"id":26288},[14,77910,77911],{},"You will usually see this error in one of these situations:",[40,77913,77914,77920,77923,77926],{},[43,77915,77916,77917],{},"Using code like ",[17,77918,77919],{},"a, b = 10",[43,77921,77922],{},"Expecting a function to return multiple values, but it returns one value",[43,77924,77925],{},"Looping with unpacking over items that are not pairs or groups",[43,77927,77928],{},"Reading data and assuming each item has multiple parts",[14,77930,3971],{},[40,77932,77933,77936,77942,77945,77948],{},[43,77934,77935],{},"Trying to unpack an integer into multiple variables",[43,77937,77938,77939,77941],{},"Trying to unpack ",[17,77940,930],{}," returned from a function",[43,77943,77944],{},"Using a loop that expects pairs but the data contains single values",[43,77946,77947],{},"Forgetting that a function returns one value, not multiple values",[43,77949,77950,77951,77954],{},"Mixing up dictionary iteration with dictionary ",[17,77952,77953],{},".items()"," iteration",[77,77956,11031],{"id":11030},[14,77958,1891],{},[91,77960,77962],{"className":93,"code":77961,"language":95,"meta":96,"style":96},"a, b = 5\n",[17,77963,77964],{"__ignoreMap":96},[100,77965,77966,77968,77970,77972,77974],{"class":102,"line":103},[100,77967,295],{"class":106},[100,77969,126],{"class":118},[100,77971,25360],{"class":106},[100,77973,111],{"class":110},[100,77975,37580],{"class":122},[14,77977,7194,77978,77980],{},[17,77979,3557],{}," is a single integer, not a list or tuple.",[14,77982,77983],{},"You will get an error like this:",[91,77985,77987],{"className":93,"code":77986,"language":95,"meta":96,"style":96},"TypeError: cannot unpack non-iterable int object\n",[17,77988,77989],{"__ignoreMap":96},[100,77990,77991,77993,77995,77998,78000,78003,78005],{"class":102,"line":103},[100,77992,3538],{"class":191},[100,77994,89],{"class":118},[100,77996,77997],{"class":106}," cannot unpack non",[100,77999,2193],{"class":110},[100,78001,78002],{"class":106},"iterable ",[100,78004,3499],{"class":191},[100,78006,78007],{"class":191}," object\n",[14,78009,78010],{},"The important part is the same in all versions of this error:",[40,78012,78013,78016],{},[43,78014,78015],{},"Python expected something unpackable",[43,78017,78018],{},"It got a non-iterable value instead",[77,78020,78022],{"id":78021},"fix-1-assign-to-one-variable","Fix 1: assign to one variable",[14,78024,78025],{},"If the value is just one item, use one variable.",[91,78027,78029],{"className":93,"code":78028,"language":95,"meta":96,"style":96},"number = 5\nprint(number)\n",[17,78030,78031,78039],{"__ignoreMap":96},[100,78032,78033,78035,78037],{"class":102,"line":103},[100,78034,1901],{"class":106},[100,78036,111],{"class":110},[100,78038,37580],{"class":122},[100,78040,78041,78043,78045,78047],{"class":102,"line":135},[100,78042,372],{"class":114},[100,78044,170],{"class":118},[100,78046,631],{"class":178},[100,78048,215],{"class":118},[14,78050,218],{},[91,78052,78053],{"className":93,"code":1909,"language":95,"meta":96,"style":96},[17,78054,78055],{"__ignoreMap":96},[100,78056,78057],{"class":102,"line":103},[100,78058,1909],{"class":122},[14,78060,78061,78062,78064],{},"Use this fix when you do ",[22,78063,1059],{}," actually need unpacking.",[14,78066,9709],{},[91,78068,78069],{"className":93,"code":77961,"language":95,"meta":96,"style":96},[17,78070,78071],{"__ignoreMap":96},[100,78072,78073,78075,78077,78079,78081],{"class":102,"line":103},[100,78074,295],{"class":106},[100,78076,126],{"class":118},[100,78078,25360],{"class":106},[100,78080,111],{"class":110},[100,78082,37580],{"class":122},[14,78084,15825],{},[91,78086,78088],{"className":93,"code":78087,"language":95,"meta":96,"style":96},"number = 5\n",[17,78089,78090],{"__ignoreMap":96},[100,78091,78092,78094,78096],{"class":102,"line":103},[100,78093,1901],{"class":106},[100,78095,111],{"class":110},[100,78097,37580],{"class":122},[14,78099,78100],{},"This is the simplest solution when your code only has one value.",[77,78102,78104],{"id":78103},"fix-2-return-or-use-an-iterable","Fix 2: return or use an iterable",[14,78106,78107],{},"If you want multiple variables, the value on the right side must be an iterable with the right number of items.",[675,78109,78111],{"id":78110},"unpacking-a-tuple","Unpacking a tuple",[91,78113,78115],{"className":93,"code":78114,"language":95,"meta":96,"style":96},"a, b = (1, 2)\nprint(a)\nprint(b)\n",[17,78116,78117,78137,78147],{"__ignoreMap":96},[100,78118,78119,78121,78123,78125,78127,78129,78131,78133,78135],{"class":102,"line":103},[100,78120,295],{"class":106},[100,78122,126],{"class":118},[100,78124,25360],{"class":106},[100,78126,111],{"class":110},[100,78128,16805],{"class":118},[100,78130,123],{"class":122},[100,78132,126],{"class":118},[100,78134,129],{"class":122},[100,78136,215],{"class":118},[100,78138,78139,78141,78143,78145],{"class":102,"line":135},[100,78140,372],{"class":114},[100,78142,170],{"class":118},[100,78144,295],{"class":178},[100,78146,215],{"class":118},[100,78148,78149,78151,78153,78155],{"class":102,"line":142},[100,78150,372],{"class":114},[100,78152,170],{"class":118},[100,78154,4027],{"class":178},[100,78156,215],{"class":118},[14,78158,218],{},[91,78160,78161],{"className":93,"code":638,"language":95,"meta":96,"style":96},[17,78162,78163,78167],{"__ignoreMap":96},[100,78164,78165],{"class":102,"line":103},[100,78166,228],{"class":122},[100,78168,78169],{"class":102,"line":135},[100,78170,233],{"class":122},[675,78172,78174],{"id":78173},"unpacking-a-list","Unpacking a list",[91,78176,78178],{"className":93,"code":78177,"language":95,"meta":96,"style":96},"a, b = [10, 20]\nprint(a, b)\n",[17,78179,78180,78200],{"__ignoreMap":96},[100,78181,78182,78184,78186,78188,78190,78192,78194,78196,78198],{"class":102,"line":103},[100,78183,295],{"class":106},[100,78185,126],{"class":118},[100,78187,25360],{"class":106},[100,78189,111],{"class":110},[100,78191,594],{"class":118},[100,78193,356],{"class":122},[100,78195,126],{"class":118},[100,78197,361],{"class":122},[100,78199,603],{"class":118},[100,78201,78202,78204,78206,78208,78210,78212],{"class":102,"line":135},[100,78203,372],{"class":114},[100,78205,170],{"class":118},[100,78207,295],{"class":178},[100,78209,126],{"class":118},[100,78211,15869],{"class":178},[100,78213,215],{"class":118},[14,78215,218],{},[91,78217,78219],{"className":93,"code":78218,"language":95,"meta":96,"style":96},"10 20\n",[17,78220,78221],{"__ignoreMap":96},[100,78222,78223,78225],{"class":102,"line":103},[100,78224,356],{"class":122},[100,78226,1669],{"class":122},[675,78228,78230],{"id":78229},"returning-multiple-values-from-a-function","Returning multiple values from a function",[14,78232,78233],{},"A function that should provide multiple values can return a tuple.",[91,78235,78237],{"className":93,"code":78236,"language":95,"meta":96,"style":96},"def get_coordinates():\n    return (3, 7)\n\nx, y = get_coordinates()\nprint(x, y)\n",[17,78238,78239,78248,78263,78267,78281],{"__ignoreMap":96},[100,78240,78241,78243,78246],{"class":102,"line":103},[100,78242,1078],{"class":1077},[100,78244,78245],{"class":1081}," get_coordinates",[100,78247,1085],{"class":118},[100,78249,78250,78252,78254,78256,78258,78261],{"class":102,"line":135},[100,78251,2552],{"class":145},[100,78253,16805],{"class":118},[100,78255,11541],{"class":122},[100,78257,126],{"class":118},[100,78259,78260],{"class":122}," 7",[100,78262,215],{"class":118},[100,78264,78265],{"class":102,"line":142},[100,78266,139],{"emptyLinePlaceholder":138},[100,78268,78269,78271,78273,78275,78277,78279],{"class":102,"line":152},[100,78270,39024],{"class":106},[100,78272,126],{"class":118},[100,78274,39029],{"class":106},[100,78276,111],{"class":110},[100,78278,78245],{"class":178},[100,78280,4734],{"class":118},[100,78282,78283,78285,78287,78289,78291,78294],{"class":102,"line":164},[100,78284,372],{"class":114},[100,78286,170],{"class":118},[100,78288,39024],{"class":178},[100,78290,126],{"class":118},[100,78292,78293],{"class":178}," y",[100,78295,215],{"class":118},[14,78297,218],{},[91,78299,78301],{"className":93,"code":78300,"language":95,"meta":96,"style":96},"3 7\n",[17,78302,78303],{"__ignoreMap":96},[100,78304,78305,78307],{"class":102,"line":103},[100,78306,11541],{"class":122},[100,78308,78309],{"class":122}," 7\n",[14,78311,78312,78313,290],{},"If this part is unclear, you may want to read ",[295,78314,11292],{"href":11291},[77,78316,78318],{"id":78317},"fix-3-check-what-a-function-really-returns","Fix 3: check what a function really returns",[14,78320,78321],{},"Sometimes the unpacking code looks correct, but the function returns something different from what you expected.",[14,78323,580],{},[91,78325,78327],{"className":93,"code":78326,"language":95,"meta":96,"style":96},"def get_total():\n    return 100\n\na, b = get_total()\n",[17,78328,78329,78338,78344,78348],{"__ignoreMap":96},[100,78330,78331,78333,78336],{"class":102,"line":103},[100,78332,1078],{"class":1077},[100,78334,78335],{"class":1081}," get_total",[100,78337,1085],{"class":118},[100,78339,78340,78342],{"class":102,"line":135},[100,78341,2552],{"class":145},[100,78343,5377],{"class":122},[100,78345,78346],{"class":102,"line":142},[100,78347,139],{"emptyLinePlaceholder":138},[100,78349,78350,78352,78354,78356,78358,78360],{"class":102,"line":152},[100,78351,295],{"class":106},[100,78353,126],{"class":118},[100,78355,25360],{"class":106},[100,78357,111],{"class":110},[100,78359,78335],{"class":178},[100,78361,4734],{"class":118},[14,78363,7710],{},[91,78365,78366],{"className":93,"code":77986,"language":95,"meta":96,"style":96},[17,78367,78368],{"__ignoreMap":96},[100,78369,78370,78372,78374,78376,78378,78380,78382],{"class":102,"line":103},[100,78371,3538],{"class":191},[100,78373,89],{"class":118},[100,78375,77997],{"class":106},[100,78377,2193],{"class":110},[100,78379,78002],{"class":106},[100,78381,3499],{"class":191},[100,78383,78007],{"class":191},[14,78385,78386],{},"The function returns one integer, not two values.",[675,78388,78390],{"id":78389},"debug-it-first","Debug it first",[14,78392,78393],{},"Print the result before unpacking it:",[91,78395,78397],{"className":93,"code":78396,"language":95,"meta":96,"style":96},"def get_total():\n    return 100\n\nresult = get_total()\nprint(result)\nprint(type(result))\n",[17,78398,78399,78407,78413,78417,78427,78437],{"__ignoreMap":96},[100,78400,78401,78403,78405],{"class":102,"line":103},[100,78402,1078],{"class":1077},[100,78404,78335],{"class":1081},[100,78406,1085],{"class":118},[100,78408,78409,78411],{"class":102,"line":135},[100,78410,2552],{"class":145},[100,78412,5377],{"class":122},[100,78414,78415],{"class":102,"line":142},[100,78416,139],{"emptyLinePlaceholder":138},[100,78418,78419,78421,78423,78425],{"class":102,"line":152},[100,78420,11556],{"class":106},[100,78422,111],{"class":110},[100,78424,78335],{"class":178},[100,78426,4734],{"class":118},[100,78428,78429,78431,78433,78435],{"class":102,"line":164},[100,78430,372],{"class":114},[100,78432,170],{"class":118},[100,78434,11580],{"class":178},[100,78436,215],{"class":118},[100,78438,78439,78441,78443,78445,78447,78449],{"class":102,"line":185},[100,78440,372],{"class":114},[100,78442,170],{"class":118},[100,78444,1250],{"class":191},[100,78446,170],{"class":118},[100,78448,11580],{"class":178},[100,78450,182],{"class":118},[14,78452,218],{},[91,78454,78456],{"className":93,"code":78455,"language":95,"meta":96,"style":96},"100\n\u003Cclass 'int'>\n",[17,78457,78458,78463],{"__ignoreMap":96},[100,78459,78460],{"class":102,"line":103},[100,78461,78462],{"class":122},"100\n",[100,78464,78465,78467,78469,78471,78473,78475],{"class":102,"line":135},[100,78466,2782],{"class":110},[100,78468,3417],{"class":1077},[100,78470,1274],{"class":205},[100,78472,3499],{"class":209},[100,78474,1280],{"class":205},[100,78476,1980],{"class":110},[14,78478,78479],{},"Now the problem is easy to see.",[675,78481,78483,78484],{"id":78482},"another-common-case-returning-none","Another common case: returning ",[17,78485,930],{},[91,78487,78489],{"className":93,"code":78488,"language":95,"meta":96,"style":96},"def do_something():\n    print(\"Done\")\n\nresult = do_something()\nprint(result)\nprint(type(result))\n",[17,78490,78491,78500,78514,78518,78528,78538],{"__ignoreMap":96},[100,78492,78493,78495,78498],{"class":102,"line":103},[100,78494,1078],{"class":1077},[100,78496,78497],{"class":1081}," do_something",[100,78499,1085],{"class":118},[100,78501,78502,78504,78506,78508,78510,78512],{"class":102,"line":135},[100,78503,200],{"class":114},[100,78505,170],{"class":118},[100,78507,206],{"class":205},[100,78509,31802],{"class":209},[100,78511,206],{"class":205},[100,78513,215],{"class":118},[100,78515,78516],{"class":102,"line":142},[100,78517,139],{"emptyLinePlaceholder":138},[100,78519,78520,78522,78524,78526],{"class":102,"line":152},[100,78521,11556],{"class":106},[100,78523,111],{"class":110},[100,78525,78497],{"class":178},[100,78527,4734],{"class":118},[100,78529,78530,78532,78534,78536],{"class":102,"line":164},[100,78531,372],{"class":114},[100,78533,170],{"class":118},[100,78535,11580],{"class":178},[100,78537,215],{"class":118},[100,78539,78540,78542,78544,78546,78548,78550],{"class":102,"line":185},[100,78541,372],{"class":114},[100,78543,170],{"class":118},[100,78545,1250],{"class":191},[100,78547,170],{"class":118},[100,78549,11580],{"class":178},[100,78551,182],{"class":118},[14,78553,218],{},[91,78555,78557],{"className":93,"code":78556,"language":95,"meta":96,"style":96},"Done\nNone\n\u003Cclass 'NoneType'>\n",[17,78558,78559,78563,78567],{"__ignoreMap":96},[100,78560,78561],{"class":102,"line":103},[100,78562,46693],{"class":106},[100,78564,78565],{"class":102,"line":135},[100,78566,924],{"class":158},[100,78568,78569,78571,78573,78575,78577,78579],{"class":102,"line":142},[100,78570,2782],{"class":110},[100,78572,3417],{"class":1077},[100,78574,1274],{"class":205},[100,78576,4665],{"class":209},[100,78578,1280],{"class":205},[100,78580,1980],{"class":110},[14,78582,78583],{},"If you then try:",[91,78585,78587],{"className":93,"code":78586,"language":95,"meta":96,"style":96},"a, b = do_something()\n",[17,78588,78589],{"__ignoreMap":96},[100,78590,78591,78593,78595,78597,78599,78601],{"class":102,"line":103},[100,78592,295],{"class":106},[100,78594,126],{"class":118},[100,78596,25360],{"class":106},[100,78598,111],{"class":110},[100,78600,78497],{"class":178},[100,78602,4734],{"class":118},[14,78604,78605],{},"You will get a similar error:",[91,78607,78609],{"className":93,"code":78608,"language":95,"meta":96,"style":96},"TypeError: cannot unpack non-iterable NoneType object\n",[17,78610,78611],{"__ignoreMap":96},[100,78612,78613,78615,78617,78619,78621,78624],{"class":102,"line":103},[100,78614,3538],{"class":191},[100,78616,89],{"class":118},[100,78618,77997],{"class":106},[100,78620,2193],{"class":110},[100,78622,78623],{"class":106},"iterable NoneType ",[100,78625,78626],{"class":191},"object\n",[14,78628,78629,78630,290],{},"That usually means the function did not return the values you expected. This is related to ",[295,78631,78632],{"href":76376},"TypeError: 'NoneType' object is not iterable",[77,78634,78636],{"id":78635},"fix-4-unpack-loop-items-correctly","Fix 4: unpack loop items correctly",[14,78638,40272],{},[675,78640,78642],{"id":78641},"wrong-unpacking-single-numbers","Wrong: unpacking single numbers",[91,78644,78646],{"className":93,"code":78645,"language":95,"meta":96,"style":96},"data = [1, 2, 3]\n\nfor a, b in data:\n    print(a, b)\n",[17,78647,78648,78668,78672,78689],{"__ignoreMap":96},[100,78649,78650,78652,78654,78656,78658,78660,78662,78664,78666],{"class":102,"line":103},[100,78651,7909],{"class":106},[100,78653,111],{"class":110},[100,78655,594],{"class":118},[100,78657,123],{"class":122},[100,78659,126],{"class":118},[100,78661,129],{"class":122},[100,78663,126],{"class":118},[100,78665,1405],{"class":122},[100,78667,603],{"class":118},[100,78669,78670],{"class":102,"line":135},[100,78671,139],{"emptyLinePlaceholder":138},[100,78673,78674,78676,78679,78681,78683,78685,78687],{"class":102,"line":142},[100,78675,71],{"class":145},[100,78677,78678],{"class":106}," a",[100,78680,126],{"class":118},[100,78682,25360],{"class":106},[100,78684,617],{"class":145},[100,78686,12102],{"class":106},[100,78688,149],{"class":118},[100,78690,78691,78693,78695,78697,78699,78701],{"class":102,"line":152},[100,78692,200],{"class":114},[100,78694,170],{"class":118},[100,78696,295],{"class":178},[100,78698,126],{"class":118},[100,78700,15869],{"class":178},[100,78702,215],{"class":118},[14,78704,78705,78706,78708],{},"Each item in ",[17,78707,7934],{}," is a single integer, not a pair. So this fails.",[675,78710,78712],{"id":78711},"right-use-one-loop-variable","Right: use one loop variable",[91,78714,78716],{"className":93,"code":78715,"language":95,"meta":96,"style":96},"data = [1, 2, 3]\n\nfor item in data:\n    print(item)\n",[17,78717,78718,78738,78742,78754],{"__ignoreMap":96},[100,78719,78720,78722,78724,78726,78728,78730,78732,78734,78736],{"class":102,"line":103},[100,78721,7909],{"class":106},[100,78723,111],{"class":110},[100,78725,594],{"class":118},[100,78727,123],{"class":122},[100,78729,126],{"class":118},[100,78731,129],{"class":122},[100,78733,126],{"class":118},[100,78735,1405],{"class":122},[100,78737,603],{"class":118},[100,78739,78740],{"class":102,"line":135},[100,78741,139],{"emptyLinePlaceholder":138},[100,78743,78744,78746,78748,78750,78752],{"class":102,"line":142},[100,78745,71],{"class":145},[100,78747,7568],{"class":106},[100,78749,617],{"class":145},[100,78751,12102],{"class":106},[100,78753,149],{"class":118},[100,78755,78756,78758,78760,78762],{"class":102,"line":152},[100,78757,200],{"class":114},[100,78759,170],{"class":118},[100,78761,7555],{"class":178},[100,78763,215],{"class":118},[14,78765,218],{},[91,78767,78768],{"className":93,"code":73745,"language":95,"meta":96,"style":96},[17,78769,78770,78774,78778],{"__ignoreMap":96},[100,78771,78772],{"class":102,"line":103},[100,78773,228],{"class":122},[100,78775,78776],{"class":102,"line":135},[100,78777,233],{"class":122},[100,78779,78780],{"class":102,"line":142},[100,78781,1500],{"class":122},[675,78783,78785],{"id":78784},"right-use-pairs-if-you-want-unpacking","Right: use pairs if you want unpacking",[91,78787,78789],{"className":93,"code":78788,"language":95,"meta":96,"style":96},"data = [(1, 2), (3, 4), (5, 6)]\n\nfor a, b in data:\n    print(a, b)\n",[17,78790,78791,78830,78834,78850],{"__ignoreMap":96},[100,78792,78793,78795,78797,78800,78802,78804,78806,78808,78810,78812,78814,78816,78818,78820,78822,78824,78827],{"class":102,"line":103},[100,78794,7909],{"class":106},[100,78796,111],{"class":110},[100,78798,78799],{"class":118}," [(",[100,78801,123],{"class":122},[100,78803,126],{"class":118},[100,78805,129],{"class":122},[100,78807,2874],{"class":118},[100,78809,16805],{"class":118},[100,78811,11541],{"class":122},[100,78813,126],{"class":118},[100,78815,2584],{"class":122},[100,78817,2874],{"class":118},[100,78819,16805],{"class":118},[100,78821,3557],{"class":122},[100,78823,126],{"class":118},[100,78825,78826],{"class":122}," 6",[100,78828,78829],{"class":118},")]\n",[100,78831,78832],{"class":102,"line":135},[100,78833,139],{"emptyLinePlaceholder":138},[100,78835,78836,78838,78840,78842,78844,78846,78848],{"class":102,"line":142},[100,78837,71],{"class":145},[100,78839,78678],{"class":106},[100,78841,126],{"class":118},[100,78843,25360],{"class":106},[100,78845,617],{"class":145},[100,78847,12102],{"class":106},[100,78849,149],{"class":118},[100,78851,78852,78854,78856,78858,78860,78862],{"class":102,"line":152},[100,78853,200],{"class":114},[100,78855,170],{"class":118},[100,78857,295],{"class":178},[100,78859,126],{"class":118},[100,78861,15869],{"class":178},[100,78863,215],{"class":118},[14,78865,218],{},[91,78867,78869],{"className":93,"code":78868,"language":95,"meta":96,"style":96},"1 2\n3 4\n5 6\n",[17,78870,78871,78877,78884],{"__ignoreMap":96},[100,78872,78873,78875],{"class":102,"line":103},[100,78874,123],{"class":122},[100,78876,3564],{"class":122},[100,78878,78879,78881],{"class":102,"line":135},[100,78880,11541],{"class":122},[100,78882,78883],{"class":122}," 4\n",[100,78885,78886,78888],{"class":102,"line":142},[100,78887,3557],{"class":122},[100,78889,78890],{"class":122}," 6\n",[675,78892,78894],{"id":78893},"dictionary-example","Dictionary example",[14,78896,78897],{},"A common mistake is iterating over a dictionary like this:",[91,78899,78901],{"className":93,"code":78900,"language":95,"meta":96,"style":96},"person = {\"name\": \"Ana\", \"age\": 20}\n\nfor key, value in person:\n    print(key, value)\n",[17,78902,78903,78939,78943,78959],{"__ignoreMap":96},[100,78904,78905,78907,78909,78911,78913,78915,78917,78919,78921,78923,78925,78927,78929,78931,78933,78935,78937],{"class":102,"line":103},[100,78906,43766],{"class":106},[100,78908,111],{"class":110},[100,78910,12080],{"class":118},[100,78912,206],{"class":205},[100,78914,2853],{"class":209},[100,78916,206],{"class":205},[100,78918,89],{"class":118},[100,78920,1708],{"class":205},[100,78922,34819],{"class":209},[100,78924,206],{"class":205},[100,78926,126],{"class":118},[100,78928,1708],{"class":205},[100,78930,2770],{"class":209},[100,78932,206],{"class":205},[100,78934,89],{"class":118},[100,78936,361],{"class":122},[100,78938,12093],{"class":118},[100,78940,78941],{"class":102,"line":135},[100,78942,139],{"emptyLinePlaceholder":138},[100,78944,78945,78947,78949,78951,78953,78955,78957],{"class":102,"line":142},[100,78946,71],{"class":145},[100,78948,67508],{"class":106},[100,78950,126],{"class":118},[100,78952,989],{"class":106},[100,78954,617],{"class":145},[100,78956,49267],{"class":106},[100,78958,149],{"class":118},[100,78960,78961,78963,78965,78967,78969,78971],{"class":102,"line":152},[100,78962,200],{"class":114},[100,78964,170],{"class":118},[100,78966,43824],{"class":178},[100,78968,126],{"class":118},[100,78970,6747],{"class":178},[100,78972,215],{"class":118},[14,78974,78975],{},"This is wrong because looping over a dictionary normally gives you one key at a time.",[14,78977,78978],{},"The correct version is:",[91,78980,78982],{"className":93,"code":78981,"language":95,"meta":96,"style":96},"person = {\"name\": \"Ana\", \"age\": 20}\n\nfor key, value in person.items():\n    print(key, value)\n",[17,78983,78984,79020,79024,79044],{"__ignoreMap":96},[100,78985,78986,78988,78990,78992,78994,78996,78998,79000,79002,79004,79006,79008,79010,79012,79014,79016,79018],{"class":102,"line":103},[100,78987,43766],{"class":106},[100,78989,111],{"class":110},[100,78991,12080],{"class":118},[100,78993,206],{"class":205},[100,78995,2853],{"class":209},[100,78997,206],{"class":205},[100,78999,89],{"class":118},[100,79001,1708],{"class":205},[100,79003,34819],{"class":209},[100,79005,206],{"class":205},[100,79007,126],{"class":118},[100,79009,1708],{"class":205},[100,79011,2770],{"class":209},[100,79013,206],{"class":205},[100,79015,89],{"class":118},[100,79017,361],{"class":122},[100,79019,12093],{"class":118},[100,79021,79022],{"class":102,"line":135},[100,79023,139],{"emptyLinePlaceholder":138},[100,79025,79026,79028,79030,79032,79034,79036,79038,79040,79042],{"class":102,"line":142},[100,79027,71],{"class":145},[100,79029,67508],{"class":106},[100,79031,126],{"class":118},[100,79033,989],{"class":106},[100,79035,617],{"class":145},[100,79037,49267],{"class":106},[100,79039,290],{"class":118},[100,79041,3146],{"class":178},[100,79043,1085],{"class":118},[100,79045,79046,79048,79050,79052,79054,79056],{"class":102,"line":152},[100,79047,200],{"class":114},[100,79049,170],{"class":118},[100,79051,43824],{"class":178},[100,79053,126],{"class":118},[100,79055,6747],{"class":178},[100,79057,215],{"class":118},[14,79059,218],{},[91,79061,79063],{"className":93,"code":79062,"language":95,"meta":96,"style":96},"name Ana\nage 20\n",[17,79064,79065,79070],{"__ignoreMap":96},[100,79066,79067],{"class":102,"line":103},[100,79068,79069],{"class":106},"name Ana\n",[100,79071,79072,79074],{"class":102,"line":135},[100,79073,1664],{"class":106},[100,79075,79076],{"class":122},"20\n",[14,79078,79079,79080,290],{},"For more help with this pattern, see ",[295,79081,79083],{"href":79082},"\u002Fhow-to\u002Fhow-to-loop-through-a-dictionary-in-python\u002F","how to loop through a dictionary in Python",[77,79085,79087],{"id":79086},"how-to-debug-this-step-by-step","How to debug this step by step",[14,79089,32241],{},[3282,79091,79092,79111,79114,79117,79120],{},[43,79093,79094,79095],{},"Find the line with unpacking, usually something like:\n",[91,79096,79097],{"className":93,"code":77851,"language":95,"meta":96,"style":96},[17,79098,79099],{"__ignoreMap":96},[100,79100,79101,79103,79105,79107,79109],{"class":102,"line":103},[100,79102,295],{"class":106},[100,79104,126],{"class":118},[100,79106,25360],{"class":106},[100,79108,111],{"class":110},[100,79110,77743],{"class":106},[43,79112,79113],{},"Print the value before that line.",[43,79115,79116],{},"Print its type.",[43,79118,79119],{},"Check whether it is iterable.",[43,79121,79122],{},"Check whether it contains the number of items you expect.",[14,79124,5295],{},[91,79126,79128],{"className":93,"code":79127,"language":95,"meta":96,"style":96},"print(value)\nprint(type(value))\nprint(result)\nprint(type(result))\nprint(data)\n",[17,79129,79130,79140,79154,79164,79178],{"__ignoreMap":96},[100,79131,79132,79134,79136,79138],{"class":102,"line":103},[100,79133,372],{"class":114},[100,79135,170],{"class":118},[100,79137,757],{"class":178},[100,79139,215],{"class":118},[100,79141,79142,79144,79146,79148,79150,79152],{"class":102,"line":135},[100,79143,372],{"class":114},[100,79145,170],{"class":118},[100,79147,1250],{"class":191},[100,79149,170],{"class":118},[100,79151,757],{"class":178},[100,79153,182],{"class":118},[100,79155,79156,79158,79160,79162],{"class":102,"line":142},[100,79157,372],{"class":114},[100,79159,170],{"class":118},[100,79161,11580],{"class":178},[100,79163,215],{"class":118},[100,79165,79166,79168,79170,79172,79174,79176],{"class":102,"line":152},[100,79167,372],{"class":114},[100,79169,170],{"class":118},[100,79171,1250],{"class":191},[100,79173,170],{"class":118},[100,79175,11580],{"class":178},[100,79177,182],{"class":118},[100,79179,79180,79182,79184,79186],{"class":102,"line":164},[100,79181,372],{"class":114},[100,79183,170],{"class":118},[100,79185,7934],{"class":178},[100,79187,215],{"class":118},[14,79189,1844],{},[91,79191,79193],{"className":93,"code":79192,"language":95,"meta":96,"style":96},"value = 5\n\nprint(value)\nprint(type(value))\n\na, b = value\n",[17,79194,79195,79203,79207,79217,79231,79235],{"__ignoreMap":96},[100,79196,79197,79199,79201],{"class":102,"line":103},[100,79198,3318],{"class":106},[100,79200,111],{"class":110},[100,79202,37580],{"class":122},[100,79204,79205],{"class":102,"line":135},[100,79206,139],{"emptyLinePlaceholder":138},[100,79208,79209,79211,79213,79215],{"class":102,"line":142},[100,79210,372],{"class":114},[100,79212,170],{"class":118},[100,79214,757],{"class":178},[100,79216,215],{"class":118},[100,79218,79219,79221,79223,79225,79227,79229],{"class":102,"line":152},[100,79220,372],{"class":114},[100,79222,170],{"class":118},[100,79224,1250],{"class":191},[100,79226,170],{"class":118},[100,79228,757],{"class":178},[100,79230,182],{"class":118},[100,79232,79233],{"class":102,"line":164},[100,79234,139],{"emptyLinePlaceholder":138},[100,79236,79237,79239,79241,79243,79245],{"class":102,"line":185},[100,79238,295],{"class":106},[100,79240,126],{"class":118},[100,79242,25360],{"class":106},[100,79244,111],{"class":110},[100,79246,77743],{"class":106},[14,79248,38743],{},[91,79250,79252],{"className":93,"code":79251,"language":95,"meta":96,"style":96},"5\n\u003Cclass 'int'>\n",[17,79253,79254,79258],{"__ignoreMap":96},[100,79255,79256],{"class":102,"line":103},[100,79257,1909],{"class":122},[100,79259,79260,79262,79264,79266,79268,79270],{"class":102,"line":135},[100,79261,2782],{"class":110},[100,79263,3417],{"class":1077},[100,79265,1274],{"class":205},[100,79267,3499],{"class":209},[100,79269,1280],{"class":205},[100,79271,1980],{"class":110},[14,79273,79274,79275,79277],{},"That tells you the problem immediately: ",[17,79276,757],{}," is an integer.",[77,79279,79281],{"id":79280},"common-related-cases","Common related cases",[14,79283,79284],{},"You may see slightly different versions of this error:",[40,79286,79287,79292,79297],{},[43,79288,79289],{},[17,79290,79291],{},"TypeError: cannot unpack non-iterable int object",[43,79293,79294],{},[17,79295,79296],{},"TypeError: cannot unpack non-iterable NoneType object",[43,79298,79299],{},[17,79300,79301],{},"TypeError: cannot unpack non-iterable float object",[14,79303,79304],{},"These all mean the same core thing:",[40,79306,79307,79310],{},[43,79308,79309],{},"Python tried to unpack a value",[43,79311,79312],{},"The value was not iterable",[14,79314,79315],{},"This is different from these errors:",[40,79317,79318,79324],{},[43,79319,79320],{},[295,79321,79323],{"href":79322},"\u002Ferrors\u002Fvalueerror-not-enough-values-to-unpack-fix\u002F","ValueError: not enough values to unpack",[43,79325,79326],{},[295,79327,79329],{"href":79328},"\u002Ferrors\u002Fvalueerror-too-many-values-to-unpack-fix\u002F","ValueError: too many values to unpack",[14,79331,79332,79333,79335],{},"Those errors happen when the value ",[22,79334,4918],{}," iterable, but it has the wrong number of items.",[77,79337,1514],{"id":1513},[675,79339,79341],{"id":79340},"what-does-non-iterable-mean-in-python","What does non-iterable mean in Python?",[14,79343,79344,79345,5870,79347,5870,79349,5629,79351,290],{},"It means the value cannot be looped over or unpacked item by item. Examples include ",[17,79346,3499],{},[17,79348,60476],{},[17,79350,46487],{},[17,79352,930],{},[675,79354,79356],{"id":79355},"can-i-unpack-a-string-in-python","Can I unpack a string in Python?",[14,79358,79359],{},"Yes. A string is iterable, so you can unpack it if it has the expected number of characters.",[14,79361,1844],{},[91,79363,79365],{"className":93,"code":79364,"language":95,"meta":96,"style":96},"a, b = \"hi\"\nprint(a, b)\n",[17,79366,79367,79384],{"__ignoreMap":96},[100,79368,79369,79371,79373,79375,79377,79379,79382],{"class":102,"line":103},[100,79370,295],{"class":106},[100,79372,126],{"class":118},[100,79374,25360],{"class":106},[100,79376,111],{"class":110},[100,79378,1708],{"class":205},[100,79380,79381],{"class":209},"hi",[100,79383,1714],{"class":205},[100,79385,79386,79388,79390,79392,79394,79396],{"class":102,"line":135},[100,79387,372],{"class":114},[100,79389,170],{"class":118},[100,79391,295],{"class":178},[100,79393,126],{"class":118},[100,79395,15869],{"class":178},[100,79397,215],{"class":118},[14,79399,218],{},[91,79401,79403],{"className":93,"code":79402,"language":95,"meta":96,"style":96},"h i\n",[17,79404,79405],{"__ignoreMap":96},[100,79406,79407],{"class":102,"line":103},[100,79408,79402],{"class":106},[675,79410,79412],{"id":79411},"why-do-i-get-this-error-from-a-function-call","Why do I get this error from a function call?",[14,79414,79415,79416,79418],{},"Usually because the function returned one value, or returned ",[17,79417,930],{},", but your code expected multiple values.",[675,79420,79422],{"id":79421},"how-is-this-different-from-not-enough-values-to-unpack","How is this different from not enough values to unpack?",[14,79424,79425,79426,79428,79431],{},"This error means the value is not iterable at all.",[9927,79427],{},[17,79429,79430],{},"not enough values to unpack"," means the value is iterable, but it does not contain enough items.",[77,79433,1554],{"id":1553},[40,79435,79436,79440,79444,79449,79454,79459],{},[43,79437,79438],{},[295,79439,12835],{"href":11291},[43,79441,79442],{},[295,79443,1561],{"href":77906},[43,79445,79446],{},[295,79447,79448],{"href":79082},"How to loop through a dictionary in Python",[43,79450,79451],{},[295,79452,79453],{"href":79322},"ValueError: not enough values to unpack (Fix)",[43,79455,79456],{},[295,79457,79458],{"href":79328},"ValueError: too many values to unpack (Fix)",[43,79460,79461],{},[295,79462,79463],{"href":76376},"TypeError: 'NoneType' object is not iterable (Fix)",[1589,79465,79466],{},"html pre.shiki code .su5hD, html code.shiki .su5hD{--shiki-light:#90A4AE;--shiki-default:#24292E;--shiki-dark:#E1E4E8}html pre.shiki code .smGrS, html code.shiki .smGrS{--shiki-light:#39ADB5;--shiki-default:#D73A49;--shiki-dark:#F97583}html pre.shiki code .srdBf, html code.shiki .srdBf{--shiki-light:#F76D47;--shiki-default:#005CC5;--shiki-dark:#79B8FF}html pre.shiki code .sutJx, html code.shiki .sutJx{--shiki-light:#90A4AE;--shiki-light-font-style:italic;--shiki-default:#6A737D;--shiki-default-font-style:inherit;--shiki-dark:#6A737D;--shiki-dark-font-style:inherit}html pre.shiki code .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 .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 .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}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 .s39Yj, html code.shiki .s39Yj{--shiki-light:#39ADB5;--shiki-default:#005CC5;--shiki-dark:#79B8FF}",{"title":96,"searchDepth":135,"depth":135,"links":79468},[79469,79470,79471,79472,79473,79474,79479,79484,79490,79491,79492,79498],{"id":79,"depth":135,"text":80},{"id":5880,"depth":135,"text":5881},{"id":26288,"depth":135,"text":26289},{"id":11030,"depth":135,"text":11031},{"id":78021,"depth":135,"text":78022},{"id":78103,"depth":135,"text":78104,"children":79475},[79476,79477,79478],{"id":78110,"depth":142,"text":78111},{"id":78173,"depth":142,"text":78174},{"id":78229,"depth":142,"text":78230},{"id":78317,"depth":135,"text":78318,"children":79480},[79481,79482],{"id":78389,"depth":142,"text":78390},{"id":78482,"depth":142,"text":79483},"Another common case: returning None",{"id":78635,"depth":135,"text":78636,"children":79485},[79486,79487,79488,79489],{"id":78641,"depth":142,"text":78642},{"id":78711,"depth":142,"text":78712},{"id":78784,"depth":142,"text":78785},{"id":78893,"depth":142,"text":78894},{"id":79086,"depth":135,"text":79087},{"id":79280,"depth":135,"text":79281},{"id":1513,"depth":135,"text":1514,"children":79493},[79494,79495,79496,79497],{"id":79340,"depth":142,"text":79341},{"id":79355,"depth":142,"text":79356},{"id":79411,"depth":142,"text":79412},{"id":79421,"depth":142,"text":79422},{"id":1553,"depth":135,"text":1554},"Master typeerror cannot unpack non iterable object fix in our comprehensive Python beginner guide.",{},"\u002Ferrors\u002Ftypeerror-cannot-unpack-non-iterable-object-fix",{"title":77679,"description":79499},"errors\u002Ftypeerror-cannot-unpack-non-iterable-object-fix","kYhyri19krqpY1dE7ENEhzrFOqKPlMdmlpgKqaNsy3I",{"id":79506,"title":79507,"body":79508,"description":80755,"extension":1623,"meta":80756,"navigation":138,"path":80757,"seo":80758,"stem":80759,"__hash__":80760},"content\u002Ferrors\u002Ftypeerror-dict-object-is-not-callable-fix.md","TypeError: 'dict' object is not callable (Fix)",{"type":7,"value":79509,"toc":80731},[79510,79516,79524,79526,79535,79611,79615,79623,79625,79638,79641,79692,79697,79701,79709,79712,79716,79763,79767,79814,79818,79826,79832,79909,79913,79926,79932,79936,79939,79944,79983,79992,79994,79997,80059,80063,80089,80101,80109,80113,80119,80121,80176,80182,80184,80187,80238,80242,80251,80255,80258,80329,80333,80341,80344,80364,80366,80393,80395,80461,80465,80489,80491,80510,80512,80601,80604,80618,80620,80622,80646,80648,80652,80655,80659,80669,80673,80676,80682,80688,80690,80728],[10,79511,74872,79513,7375],{"id":79512},"typeerror-dict-object-is-not-callable-fix",[17,79514,79515],{},"'dict' object is not callable",[14,79517,31738,79518,79523],{},[22,79519,79520],{},[17,79521,79522],{},"TypeError: 'dict' object is not callable",". This page explains what the error means, the most common causes, and how to correct your code with simple examples.",[77,79525,80],{"id":79},[14,79527,79528,79529,79531,79532,79534],{},"This error often happens when you use parentheses ",[17,79530,7562],{}," with a dictionary. Use square brackets ",[17,79533,45218],{}," to access a value by key.",[91,79536,79538],{"className":93,"code":79537,"language":95,"meta":96,"style":96},"my_dict = {'a': 1}\n\n# Wrong\n# value = my_dict('a')\n\n# Correct\nvalue = my_dict['a']\nprint(value)\n",[17,79539,79540,79560,79564,79569,79574,79578,79583,79601],{"__ignoreMap":96},[100,79541,79542,79544,79546,79548,79550,79552,79554,79556,79558],{"class":102,"line":103},[100,79543,44748],{"class":106},[100,79545,111],{"class":110},[100,79547,12080],{"class":118},[100,79549,1280],{"class":205},[100,79551,295],{"class":209},[100,79553,1280],{"class":205},[100,79555,89],{"class":118},[100,79557,6915],{"class":122},[100,79559,12093],{"class":118},[100,79561,79562],{"class":102,"line":135},[100,79563,139],{"emptyLinePlaceholder":138},[100,79565,79566],{"class":102,"line":142},[100,79567,79568],{"class":414},"# Wrong\n",[100,79570,79571],{"class":102,"line":152},[100,79572,79573],{"class":414},"# value = my_dict('a')\n",[100,79575,79576],{"class":102,"line":164},[100,79577,139],{"emptyLinePlaceholder":138},[100,79579,79580],{"class":102,"line":185},[100,79581,79582],{"class":414},"# Correct\n",[100,79584,79585,79587,79589,79591,79593,79595,79597,79599],{"class":102,"line":197},[100,79586,3318],{"class":106},[100,79588,111],{"class":110},[100,79590,43305],{"class":106},[100,79592,3166],{"class":118},[100,79594,1280],{"class":205},[100,79596,295],{"class":209},[100,79598,1280],{"class":205},[100,79600,603],{"class":118},[100,79602,79603,79605,79607,79609],{"class":102,"line":771},[100,79604,372],{"class":114},[100,79606,170],{"class":118},[100,79608,757],{"class":178},[100,79610,215],{"class":118},[14,79612,79613],{},[22,79614,218],{},[91,79616,79617],{"className":93,"code":228,"language":95,"meta":96,"style":96},[17,79618,79619],{"__ignoreMap":96},[100,79620,79621],{"class":102,"line":103},[100,79622,228],{"class":122},[77,79624,5881],{"id":5880},[40,79626,79627,79630,79635],{},[43,79628,79629],{},"Python is treating a dictionary like a function.",[43,79631,79632,79633,290],{},"A dictionary stores key-value pairs, but you cannot call it with ",[17,79634,7562],{},[43,79636,79637],{},"If you see this error, check where you used parentheses after a variable name.",[14,79639,79640],{},"For example, this causes the error:",[91,79642,79644],{"className":93,"code":79643,"language":95,"meta":96,"style":96},"user = {'name': 'Sam'}\n\nprint(user('name'))\n",[17,79645,79646,79670,79674],{"__ignoreMap":96},[100,79647,79648,79650,79652,79654,79656,79658,79660,79662,79664,79666,79668],{"class":102,"line":103},[100,79649,15336],{"class":106},[100,79651,111],{"class":110},[100,79653,12080],{"class":118},[100,79655,1280],{"class":205},[100,79657,2853],{"class":209},[100,79659,1280],{"class":205},[100,79661,89],{"class":118},[100,79663,1274],{"class":205},[100,79665,29329],{"class":209},[100,79667,1280],{"class":205},[100,79669,12093],{"class":118},[100,79671,79672],{"class":102,"line":135},[100,79673,139],{"emptyLinePlaceholder":138},[100,79675,79676,79678,79680,79682,79684,79686,79688,79690],{"class":102,"line":142},[100,79677,372],{"class":114},[100,79679,170],{"class":118},[100,79681,15354],{"class":178},[100,79683,170],{"class":118},[100,79685,1280],{"class":205},[100,79687,2853],{"class":209},[100,79689,1280],{"class":205},[100,79691,182],{"class":118},[14,79693,14478,79694,79696],{},[17,79695,15354],{}," is a dictionary, not a function.",[14,79698,8107,79699,290],{},[295,79700,42671],{"href":47107},[77,79702,79704,79705,15957,79707],{"id":79703},"common-cause-using-instead-of","Common cause: using ",[17,79706,7562],{},[17,79708,45218],{},[14,79710,79711],{},"Use square brackets to get a value from a dictionary by key.",[14,79713,79714],{},[22,79715,9709],{},[91,79717,79719],{"className":93,"code":79718,"language":95,"meta":96,"style":96},"user = {'name': 'Sam'}\nprint(user('name'))\n",[17,79720,79721,79745],{"__ignoreMap":96},[100,79722,79723,79725,79727,79729,79731,79733,79735,79737,79739,79741,79743],{"class":102,"line":103},[100,79724,15336],{"class":106},[100,79726,111],{"class":110},[100,79728,12080],{"class":118},[100,79730,1280],{"class":205},[100,79732,2853],{"class":209},[100,79734,1280],{"class":205},[100,79736,89],{"class":118},[100,79738,1274],{"class":205},[100,79740,29329],{"class":209},[100,79742,1280],{"class":205},[100,79744,12093],{"class":118},[100,79746,79747,79749,79751,79753,79755,79757,79759,79761],{"class":102,"line":135},[100,79748,372],{"class":114},[100,79750,170],{"class":118},[100,79752,15354],{"class":178},[100,79754,170],{"class":118},[100,79756,1280],{"class":205},[100,79758,2853],{"class":209},[100,79760,1280],{"class":205},[100,79762,182],{"class":118},[14,79764,79765],{},[22,79766,9750],{},[91,79768,79770],{"className":93,"code":79769,"language":95,"meta":96,"style":96},"user = {'name': 'Sam'}\nprint(user['name'])\n",[17,79771,79772,79796],{"__ignoreMap":96},[100,79773,79774,79776,79778,79780,79782,79784,79786,79788,79790,79792,79794],{"class":102,"line":103},[100,79775,15336],{"class":106},[100,79777,111],{"class":110},[100,79779,12080],{"class":118},[100,79781,1280],{"class":205},[100,79783,2853],{"class":209},[100,79785,1280],{"class":205},[100,79787,89],{"class":118},[100,79789,1274],{"class":205},[100,79791,29329],{"class":209},[100,79793,1280],{"class":205},[100,79795,12093],{"class":118},[100,79797,79798,79800,79802,79804,79806,79808,79810,79812],{"class":102,"line":135},[100,79799,372],{"class":114},[100,79801,170],{"class":118},[100,79803,15354],{"class":178},[100,79805,3166],{"class":118},[100,79807,1280],{"class":205},[100,79809,2853],{"class":209},[100,79811,1280],{"class":205},[100,79813,132],{"class":118},[14,79815,79816],{},[22,79817,218],{},[91,79819,79820],{"className":93,"code":16526,"language":95,"meta":96,"style":96},[17,79821,79822],{"__ignoreMap":96},[100,79823,79824],{"class":102,"line":103},[100,79825,16526],{"class":106},[14,79827,79828,79829,79831],{},"If the key may not exist, use ",[17,79830,6110],{}," instead:",[91,79833,79835],{"className":93,"code":79834,"language":95,"meta":96,"style":96},"user = {'name': 'Sam'}\n\nprint(user.get('name'))\nprint(user.get('age'))\n",[17,79836,79837,79861,79865,79887],{"__ignoreMap":96},[100,79838,79839,79841,79843,79845,79847,79849,79851,79853,79855,79857,79859],{"class":102,"line":103},[100,79840,15336],{"class":106},[100,79842,111],{"class":110},[100,79844,12080],{"class":118},[100,79846,1280],{"class":205},[100,79848,2853],{"class":209},[100,79850,1280],{"class":205},[100,79852,89],{"class":118},[100,79854,1274],{"class":205},[100,79856,29329],{"class":209},[100,79858,1280],{"class":205},[100,79860,12093],{"class":118},[100,79862,79863],{"class":102,"line":135},[100,79864,139],{"emptyLinePlaceholder":138},[100,79866,79867,79869,79871,79873,79875,79877,79879,79881,79883,79885],{"class":102,"line":142},[100,79868,372],{"class":114},[100,79870,170],{"class":118},[100,79872,15354],{"class":178},[100,79874,290],{"class":118},[100,79876,12412],{"class":178},[100,79878,170],{"class":118},[100,79880,1280],{"class":205},[100,79882,2853],{"class":209},[100,79884,1280],{"class":205},[100,79886,182],{"class":118},[100,79888,79889,79891,79893,79895,79897,79899,79901,79903,79905,79907],{"class":102,"line":152},[100,79890,372],{"class":114},[100,79892,170],{"class":118},[100,79894,15354],{"class":178},[100,79896,290],{"class":118},[100,79898,12412],{"class":178},[100,79900,170],{"class":118},[100,79902,1280],{"class":205},[100,79904,2770],{"class":209},[100,79906,1280],{"class":205},[100,79908,182],{"class":118},[14,79910,79911],{},[22,79912,218],{},[91,79914,79916],{"className":93,"code":79915,"language":95,"meta":96,"style":96},"Sam\nNone\n",[17,79917,79918,79922],{"__ignoreMap":96},[100,79919,79920],{"class":102,"line":103},[100,79921,16526],{"class":106},[100,79923,79924],{"class":102,"line":135},[100,79925,924],{"class":158},[14,79927,42973,79928,290],{},[295,79929,45833,79930,4112],{"href":11015},[17,79931,13068],{},[77,79933,79935],{"id":79934},"common-cause-variable-name-hides-a-function","Common cause: variable name hides a function",[14,79937,79938],{},"You may have used the same name for a dictionary and a function.",[14,79940,79941,79942,89],{},"A very common mistake is naming a variable ",[17,79943,16046],{},[91,79945,79947],{"className":93,"code":79946,"language":95,"meta":96,"style":96},"dict = {'a': 1}\n\nprint(dict())\n",[17,79948,79949,79969,79973],{"__ignoreMap":96},[100,79950,79951,79953,79955,79957,79959,79961,79963,79965,79967],{"class":102,"line":103},[100,79952,16046],{"class":191},[100,79954,47743],{"class":110},[100,79956,12080],{"class":118},[100,79958,1280],{"class":205},[100,79960,295],{"class":209},[100,79962,1280],{"class":205},[100,79964,89],{"class":118},[100,79966,6915],{"class":122},[100,79968,12093],{"class":118},[100,79970,79971],{"class":102,"line":135},[100,79972,139],{"emptyLinePlaceholder":138},[100,79974,79975,79977,79979,79981],{"class":102,"line":142},[100,79976,372],{"class":114},[100,79978,170],{"class":118},[100,79980,16046],{"class":191},[100,79982,3370],{"class":118},[14,79984,7194,79985,79987,79988,79991],{},[17,79986,16046],{}," now refers to your dictionary, not Python’s built-in ",[17,79989,79990],{},"dict()"," function.",[675,79993,74210],{"id":74209},[14,79995,79996],{},"Rename the variable to something clearer:",[91,79998,80000],{"className":93,"code":79999,"language":95,"meta":96,"style":96},"data = {'a': 1}\n\nnew_dict = dict()\nprint(data)\nprint(new_dict)\n",[17,80001,80002,80022,80026,80038,80048],{"__ignoreMap":96},[100,80003,80004,80006,80008,80010,80012,80014,80016,80018,80020],{"class":102,"line":103},[100,80005,7909],{"class":106},[100,80007,111],{"class":110},[100,80009,12080],{"class":118},[100,80011,1280],{"class":205},[100,80013,295],{"class":209},[100,80015,1280],{"class":205},[100,80017,89],{"class":118},[100,80019,6915],{"class":122},[100,80021,12093],{"class":118},[100,80023,80024],{"class":102,"line":135},[100,80025,139],{"emptyLinePlaceholder":138},[100,80027,80028,80031,80033,80036],{"class":102,"line":142},[100,80029,80030],{"class":106},"new_dict ",[100,80032,111],{"class":110},[100,80034,80035],{"class":191}," dict",[100,80037,4734],{"class":118},[100,80039,80040,80042,80044,80046],{"class":102,"line":152},[100,80041,372],{"class":114},[100,80043,170],{"class":118},[100,80045,7934],{"class":178},[100,80047,215],{"class":118},[100,80049,80050,80052,80054,80057],{"class":102,"line":164},[100,80051,372],{"class":114},[100,80053,170],{"class":118},[100,80055,80056],{"class":178},"new_dict",[100,80058,215],{"class":118},[14,80060,80061],{},[22,80062,218],{},[91,80064,80066],{"className":93,"code":80065,"language":95,"meta":96,"style":96},"{'a': 1}\n{}\n",[17,80067,80068,80084],{"__ignoreMap":96},[100,80069,80070,80072,80074,80076,80078,80080,80082],{"class":102,"line":103},[100,80071,16490],{"class":118},[100,80073,1280],{"class":205},[100,80075,295],{"class":209},[100,80077,1280],{"class":205},[100,80079,89],{"class":118},[100,80081,6915],{"class":122},[100,80083,12093],{"class":118},[100,80085,80086],{"class":102,"line":135},[100,80087,80088],{"class":118},"{}\n",[14,80090,80091,80092,5870,80094,5894,80097,80100],{},"Using names like ",[17,80093,7934],{},[17,80095,80096],{},"user_info",[17,80098,80099],{},"settings"," helps avoid this problem.",[14,80102,80103,80104,290],{},"If needed, see ",[295,80105,1572,80107,1576],{"href":80106},"\u002Freference\u002Fpython-dict-function-explained\u002F",[17,80108,79990],{},[77,80110,80112],{"id":80111},"common-cause-accidental-extra-parentheses","Common cause: accidental extra parentheses",[14,80114,80115,80116,80118],{},"Sometimes the dictionary access is correct, but extra ",[17,80117,7562],{}," are added after it.",[14,80120,1844],{},[91,80122,80124],{"className":93,"code":80123,"language":95,"meta":96,"style":96},"settings = {'theme': 'dark'}\n\nprint(settings['theme']())\n",[17,80125,80126,80153,80157],{"__ignoreMap":96},[100,80127,80128,80131,80133,80135,80137,80140,80142,80144,80146,80149,80151],{"class":102,"line":103},[100,80129,80130],{"class":106},"settings ",[100,80132,111],{"class":110},[100,80134,12080],{"class":118},[100,80136,1280],{"class":205},[100,80138,80139],{"class":209},"theme",[100,80141,1280],{"class":205},[100,80143,89],{"class":118},[100,80145,1274],{"class":205},[100,80147,80148],{"class":209},"dark",[100,80150,1280],{"class":205},[100,80152,12093],{"class":118},[100,80154,80155],{"class":102,"line":135},[100,80156,139],{"emptyLinePlaceholder":138},[100,80158,80159,80161,80163,80165,80167,80169,80171,80173],{"class":102,"line":142},[100,80160,372],{"class":114},[100,80162,170],{"class":118},[100,80164,80099],{"class":178},[100,80166,3166],{"class":118},[100,80168,1280],{"class":205},[100,80170,80139],{"class":209},[100,80172,1280],{"class":205},[100,80174,80175],{"class":118},"]())\n",[14,80177,77868,80178,80181],{},[17,80179,80180],{},"settings['theme']"," is a function. Here, it is a string, so Python raises an error.",[675,80183,74210],{"id":74279},[14,80185,80186],{},"Remove the extra parentheses:",[91,80188,80190],{"className":93,"code":80189,"language":95,"meta":96,"style":96},"settings = {'theme': 'dark'}\n\nprint(settings['theme'])\n",[17,80191,80192,80216,80220],{"__ignoreMap":96},[100,80193,80194,80196,80198,80200,80202,80204,80206,80208,80210,80212,80214],{"class":102,"line":103},[100,80195,80130],{"class":106},[100,80197,111],{"class":110},[100,80199,12080],{"class":118},[100,80201,1280],{"class":205},[100,80203,80139],{"class":209},[100,80205,1280],{"class":205},[100,80207,89],{"class":118},[100,80209,1274],{"class":205},[100,80211,80148],{"class":209},[100,80213,1280],{"class":205},[100,80215,12093],{"class":118},[100,80217,80218],{"class":102,"line":135},[100,80219,139],{"emptyLinePlaceholder":138},[100,80221,80222,80224,80226,80228,80230,80232,80234,80236],{"class":102,"line":142},[100,80223,372],{"class":114},[100,80225,170],{"class":118},[100,80227,80099],{"class":178},[100,80229,3166],{"class":118},[100,80231,1280],{"class":205},[100,80233,80139],{"class":209},[100,80235,1280],{"class":205},[100,80237,132],{"class":118},[14,80239,80240],{},[22,80241,218],{},[91,80243,80245],{"className":93,"code":80244,"language":95,"meta":96,"style":96},"dark\n",[17,80246,80247],{"__ignoreMap":96},[100,80248,80249],{"class":102,"line":103},[100,80250,80244],{"class":106},[675,80252,80254],{"id":80253},"when-calling-a-dictionary-value-is-valid","When calling a dictionary value is valid",[14,80256,80257],{},"A dictionary can store functions:",[91,80259,80261],{"className":93,"code":80260,"language":95,"meta":96,"style":96},"def greet():\n    return \"Hello\"\n\nactions = {'say_hello': greet}\n\nprint(actions['say_hello']())\n",[17,80262,80263,80271,80281,80285,80306,80310],{"__ignoreMap":96},[100,80264,80265,80267,80269],{"class":102,"line":103},[100,80266,1078],{"class":1077},[100,80268,2848],{"class":1081},[100,80270,1085],{"class":118},[100,80272,80273,80275,80277,80279],{"class":102,"line":135},[100,80274,2552],{"class":145},[100,80276,1708],{"class":205},[100,80278,7683],{"class":209},[100,80280,1714],{"class":205},[100,80282,80283],{"class":102,"line":142},[100,80284,139],{"emptyLinePlaceholder":138},[100,80286,80287,80290,80292,80294,80296,80298,80300,80302,80304],{"class":102,"line":152},[100,80288,80289],{"class":106},"actions ",[100,80291,111],{"class":110},[100,80293,12080],{"class":118},[100,80295,1280],{"class":205},[100,80297,52978],{"class":209},[100,80299,1280],{"class":205},[100,80301,89],{"class":118},[100,80303,2848],{"class":106},[100,80305,12093],{"class":118},[100,80307,80308],{"class":102,"line":164},[100,80309,139],{"emptyLinePlaceholder":138},[100,80311,80312,80314,80316,80319,80321,80323,80325,80327],{"class":102,"line":185},[100,80313,372],{"class":114},[100,80315,170],{"class":118},[100,80317,80318],{"class":178},"actions",[100,80320,3166],{"class":118},[100,80322,1280],{"class":205},[100,80324,52978],{"class":209},[100,80326,1280],{"class":205},[100,80328,80175],{"class":118},[14,80330,80331],{},[22,80332,218],{},[91,80334,80335],{"className":93,"code":29173,"language":95,"meta":96,"style":96},[17,80336,80337],{"__ignoreMap":96},[100,80338,80339],{"class":102,"line":103},[100,80340,29173],{"class":106},[14,80342,80343],{},"The key point is:",[40,80345,80346,80354],{},[43,80347,80348,80350,80351,37367],{},[17,80349,80318],{}," is a dictionary, so ",[17,80352,80353],{},"actions()",[43,80355,80356,80359,80360,80363],{},[17,80357,80358],{},"actions['say_hello']"," is a function, so ",[17,80361,80362],{},"actions['say_hello']()"," is valid",[77,80365,52309],{"id":52308},[3282,80367,80368,80371,80376,80383,80388],{},[43,80369,80370],{},"Find the line shown in the traceback.",[43,80372,80373,80374,290],{},"Look for a dictionary variable followed by ",[17,80375,7562],{},[43,80377,52216,80378,42154,80380,80382],{},[17,80379,7562],{},[17,80381,45218],{}," if you are accessing a key.",[43,80384,80385,80386,290],{},"Check whether you overwrote a built-in name like ",[17,80387,16046],{},[43,80389,5163,80390,80392],{},[17,80391,5283],{}," if you are not sure what the variable contains.",[14,80394,1844],{},[91,80396,80398],{"className":93,"code":80397,"language":95,"meta":96,"style":96},"data = {'name': 'Ava'}\n\nprint(type(data))\nprint(data['name'])\n",[17,80399,80400,80425,80429,80443],{"__ignoreMap":96},[100,80401,80402,80404,80406,80408,80410,80412,80414,80416,80418,80421,80423],{"class":102,"line":103},[100,80403,7909],{"class":106},[100,80405,111],{"class":110},[100,80407,12080],{"class":118},[100,80409,1280],{"class":205},[100,80411,2853],{"class":209},[100,80413,1280],{"class":205},[100,80415,89],{"class":118},[100,80417,1274],{"class":205},[100,80419,80420],{"class":209},"Ava",[100,80422,1280],{"class":205},[100,80424,12093],{"class":118},[100,80426,80427],{"class":102,"line":135},[100,80428,139],{"emptyLinePlaceholder":138},[100,80430,80431,80433,80435,80437,80439,80441],{"class":102,"line":142},[100,80432,372],{"class":114},[100,80434,170],{"class":118},[100,80436,1250],{"class":191},[100,80438,170],{"class":118},[100,80440,7934],{"class":178},[100,80442,182],{"class":118},[100,80444,80445,80447,80449,80451,80453,80455,80457,80459],{"class":102,"line":152},[100,80446,372],{"class":114},[100,80448,170],{"class":118},[100,80450,7934],{"class":178},[100,80452,3166],{"class":118},[100,80454,1280],{"class":205},[100,80456,2853],{"class":209},[100,80458,1280],{"class":205},[100,80460,132],{"class":118},[14,80462,80463],{},[22,80464,218],{},[91,80466,80468],{"className":93,"code":80467,"language":95,"meta":96,"style":96},"\u003Cclass 'dict'>\nAva\n",[17,80469,80470,80484],{"__ignoreMap":96},[100,80471,80472,80474,80476,80478,80480,80482],{"class":102,"line":103},[100,80473,2782],{"class":110},[100,80475,3417],{"class":1077},[100,80477,1274],{"class":205},[100,80479,16046],{"class":209},[100,80481,1280],{"class":205},[100,80483,1980],{"class":110},[100,80485,80486],{"class":102,"line":135},[100,80487,80488],{"class":106},"Ava\n",[77,80490,77202],{"id":77201},[40,80492,80493,80496,80499,80504],{},[43,80494,80495],{},"Read the full traceback to find the exact line.",[43,80497,80498],{},"Print the variable before the failing line.",[43,80500,241,80501,80503],{},[17,80502,5283],{}," to confirm it is a dictionary.",[43,80505,80506,80507,80509],{},"Check earlier code for variables named ",[17,80508,16046],{}," or other function names.",[14,80511,5295],{},[91,80513,80515],{"className":93,"code":80514,"language":95,"meta":96,"style":96},"my_dict = {'a': 1, 'b': 2}\n\nprint(my_dict)\nprint(type(my_dict))\nprint(my_dict.keys())\nprint(locals())\n",[17,80516,80517,80549,80553,80563,80577,80591],{"__ignoreMap":96},[100,80518,80519,80521,80523,80525,80527,80529,80531,80533,80535,80537,80539,80541,80543,80545,80547],{"class":102,"line":103},[100,80520,44748],{"class":106},[100,80522,111],{"class":110},[100,80524,12080],{"class":118},[100,80526,1280],{"class":205},[100,80528,295],{"class":209},[100,80530,1280],{"class":205},[100,80532,89],{"class":118},[100,80534,6915],{"class":122},[100,80536,126],{"class":118},[100,80538,1274],{"class":205},[100,80540,4027],{"class":209},[100,80542,1280],{"class":205},[100,80544,89],{"class":118},[100,80546,129],{"class":122},[100,80548,12093],{"class":118},[100,80550,80551],{"class":102,"line":135},[100,80552,139],{"emptyLinePlaceholder":138},[100,80554,80555,80557,80559,80561],{"class":102,"line":142},[100,80556,372],{"class":114},[100,80558,170],{"class":118},[100,80560,17794],{"class":178},[100,80562,215],{"class":118},[100,80564,80565,80567,80569,80571,80573,80575],{"class":102,"line":152},[100,80566,372],{"class":114},[100,80568,170],{"class":118},[100,80570,1250],{"class":191},[100,80572,170],{"class":118},[100,80574,17794],{"class":178},[100,80576,182],{"class":118},[100,80578,80579,80581,80583,80585,80587,80589],{"class":102,"line":164},[100,80580,372],{"class":114},[100,80582,170],{"class":118},[100,80584,17794],{"class":178},[100,80586,290],{"class":118},[100,80588,6185],{"class":178},[100,80590,3370],{"class":118},[100,80592,80593,80595,80597,80599],{"class":102,"line":185},[100,80594,372],{"class":114},[100,80596,170],{"class":118},[100,80598,53957],{"class":114},[100,80600,3370],{"class":118},[14,80602,80603],{},"These help you confirm:",[40,80605,80606,80609,80612,80615],{},[43,80607,80608],{},"what the variable contains",[43,80610,80611],{},"whether it is really a dictionary",[43,80613,80614],{},"which keys are available",[43,80616,80617],{},"what names exist in the current scope",[77,80619,7117],{"id":7116},[14,80621,74691],{},[40,80623,80624,80632,80640,80643],{},[43,80625,1357,80626,15957,80629],{},[17,80627,80628],{},"my_dict('key')",[17,80630,80631],{},"my_dict['key']",[43,80633,80634,80635,80637,80638],{},"Naming a variable ",[17,80636,16046],{}," and then trying to call ",[17,80639,79990],{},[43,80641,80642],{},"Adding extra parentheses after a dictionary value",[43,80644,80645],{},"Expecting a dictionary value to be a function when it is not",[77,80647,1514],{"id":1513},[675,80649,80651],{"id":80650},"why-does-python-say-a-dict-object-is-not-callable","Why does Python say a dict object is not callable?",[14,80653,80654],{},"Because your code used parentheses after a dictionary, and parentheses are used to call functions.",[675,80656,80658],{"id":80657},"how-do-i-access-a-dictionary-value-correctly","How do I access a dictionary value correctly?",[14,80660,80661,80662,80665,80666,290],{},"Use square brackets with a key, like ",[17,80663,80664],{},"data['name']",", or use ",[17,80667,80668],{},"data.get('name')",[675,80670,80672],{"id":80671},"can-a-dictionary-store-functions","Can a dictionary store functions?",[14,80674,80675],{},"Yes. If a dictionary value is a function, then calling that value can work. But the dictionary itself is still not callable.",[675,80677,80679,80680,11353],{"id":80678},"what-if-i-named-my-variable-dict","What if I named my variable ",[17,80681,16046],{},[14,80683,80684,80685,80687],{},"Rename it to another name, because ",[17,80686,16046],{}," is also the name of a built-in Python function.",[77,80689,1554],{"id":1553},[40,80691,80692,80696,80702,80708,80716,80724],{},[43,80693,80694],{},[295,80695,42671],{"href":47107},[43,80697,80698],{},[295,80699,45833,80700,4112],{"href":11015},[17,80701,13068],{},[43,80703,80704],{},[295,80705,1572,80706,1576],{"href":80106},[17,80707,79990],{},[43,80709,80710],{},[295,80711,74872,80713],{"href":80712},"\u002Ferrors\u002Ftypeerror-list-object-is-not-callable-fix\u002F",[17,80714,80715],{},"'list' object is not callable",[43,80717,80718],{},[295,80719,74872,80721],{"href":80720},"\u002Ferrors\u002Ftypeerror-module-object-is-not-callable-fix\u002F",[17,80722,80723],{},"'module' object is not callable",[43,80725,80726],{},[295,80727,7095],{"href":12826},[1589,80729,80730],{},"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 .sP7_E, html code.shiki .sP7_E{--shiki-light:#39ADB5;--shiki-default:#24292E;--shiki-dark:#E1E4E8}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 .srdBf, html code.shiki .srdBf{--shiki-light:#F76D47;--shiki-default:#005CC5;--shiki-dark:#79B8FF}html pre.shiki code .sutJx, html code.shiki .sutJx{--shiki-light:#90A4AE;--shiki-light-font-style:italic;--shiki-default:#6A737D;--shiki-default-font-style:inherit;--shiki-dark:#6A737D;--shiki-dark-font-style:inherit}html pre.shiki code .sptTA, html code.shiki .sptTA{--shiki-light:#6182B8;--shiki-default:#005CC5;--shiki-dark:#79B8FF}html pre.shiki code .slqww, html code.shiki .slqww{--shiki-light:#6182B8;--shiki-default:#24292E;--shiki-dark:#E1E4E8}html .light .shiki span {color: var(--shiki-light);background: var(--shiki-light-bg);font-style: var(--shiki-light-font-style);font-weight: var(--shiki-light-font-weight);text-decoration: var(--shiki-light-text-decoration);}html.light .shiki span {color: var(--shiki-light);background: var(--shiki-light-bg);font-style: var(--shiki-light-font-style);font-weight: var(--shiki-light-font-weight);text-decoration: var(--shiki-light-text-decoration);}html .default .shiki span {color: var(--shiki-default);background: var(--shiki-default-bg);font-style: var(--shiki-default-font-style);font-weight: var(--shiki-default-font-weight);text-decoration: var(--shiki-default-text-decoration);}html .shiki span {color: var(--shiki-default);background: var(--shiki-default-bg);font-style: var(--shiki-default-font-style);font-weight: var(--shiki-default-font-weight);text-decoration: var(--shiki-default-text-decoration);}html .dark .shiki span {color: var(--shiki-dark);background: var(--shiki-dark-bg);font-style: var(--shiki-dark-font-style);font-weight: var(--shiki-dark-font-weight);text-decoration: var(--shiki-dark-text-decoration);}html.dark .shiki span {color: var(--shiki-dark);background: var(--shiki-dark-bg);font-style: var(--shiki-dark-font-style);font-weight: var(--shiki-dark-font-weight);text-decoration: var(--shiki-dark-text-decoration);}html pre.shiki code .s39Yj, html code.shiki .s39Yj{--shiki-light:#39ADB5;--shiki-default:#005CC5;--shiki-dark:#79B8FF}html pre.shiki code .sZMiF, html code.shiki .sZMiF{--shiki-light:#E2931D;--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":96,"searchDepth":135,"depth":135,"links":80732},[80733,80734,80735,80737,80740,80744,80745,80746,80747,80754],{"id":79,"depth":135,"text":80},{"id":5880,"depth":135,"text":5881},{"id":79703,"depth":135,"text":80736},"Common cause: using () instead of []",{"id":79934,"depth":135,"text":79935,"children":80738},[80739],{"id":74209,"depth":142,"text":74210},{"id":80111,"depth":135,"text":80112,"children":80741},[80742,80743],{"id":74279,"depth":142,"text":74210},{"id":80253,"depth":142,"text":80254},{"id":52308,"depth":135,"text":52309},{"id":77201,"depth":135,"text":77202},{"id":7116,"depth":135,"text":7117},{"id":1513,"depth":135,"text":1514,"children":80748},[80749,80750,80751,80752],{"id":80650,"depth":142,"text":80651},{"id":80657,"depth":142,"text":80658},{"id":80671,"depth":142,"text":80672},{"id":80678,"depth":142,"text":80753},"What if I named my variable dict?",{"id":1553,"depth":135,"text":1554},"Master typeerror dict object is not callable fix in our comprehensive Python beginner guide.",{},"\u002Ferrors\u002Ftypeerror-dict-object-is-not-callable-fix",{"title":79507,"description":80755},"errors\u002Ftypeerror-dict-object-is-not-callable-fix","YLk1VSBMQZ9-xoMM0rblM1h1F1Ve62biky_ibRaUDRI",{"id":80762,"title":80763,"body":80764,"description":82192,"extension":1623,"meta":82193,"navigation":138,"path":82194,"seo":82195,"stem":82196,"__hash__":82197},"content\u002Ferrors\u002Ftypeerror-float-object-cannot-be-interpreted-as-an-integer-fix.md","TypeError: 'float' object cannot be interpreted as an integer (Fix)",{"type":7,"value":80765,"toc":82169},[80766,80772,80778,80781,80783,80831,80835,80860,80868,80870,80872,80896,80906,80913,80917,80920,80939,80950,80952,80954,80985,80990,81018,81020,81037,81041,81047,81093,81097,81121,81128,81131,81148,81150,81178,81182,81190,81196,81200,81203,81209,81263,81267,81290,81293,81340,81346,81353,81400,81404,81428,81430,81446,81450,81453,81455,81541,81545,81568,81571,81588,81591,81668,81672,81703,81705,81766,81770,81773,81779,81784,81821,81827,81876,81880,81909,81912,81914,81917,81937,81939,82015,82018,82033,82038,82040,82043,82074,82081,82083,82092,82100,82106,82112,82120,82128,82134,82137,82139,82166],[10,80767,74872,80769,7375],{"id":80768},"typeerror-float-object-cannot-be-interpreted-as-an-integer-fix",[17,80770,80771],{},"'float' object cannot be interpreted as an integer",[14,80773,31738,80774,80777],{},[17,80775,80776],{},"TypeError: 'float' object cannot be interpreted as an integer",". This error happens when Python needs a whole number, but your code gives it a float instead.",[14,80779,80780],{},"This page shows what the error means, where beginners usually see it, and the safest ways to fix it.",[77,80782,80],{"id":79},[91,80784,80786],{"className":93,"code":80785,"language":95,"meta":96,"style":96},"count = 5.0\n\nfor i in range(int(count)):\n    print(i)\n",[17,80787,80788,80797,80801,80821],{"__ignoreMap":96},[100,80789,80790,80792,80794],{"class":102,"line":103},[100,80791,2176],{"class":106},[100,80793,111],{"class":110},[100,80795,80796],{"class":122}," 5.0\n",[100,80798,80799],{"class":102,"line":135},[100,80800,139],{"emptyLinePlaceholder":138},[100,80802,80803,80805,80807,80809,80811,80813,80815,80817,80819],{"class":102,"line":142},[100,80804,71],{"class":145},[100,80806,29838],{"class":106},[100,80808,617],{"class":145},[100,80810,29843],{"class":114},[100,80812,170],{"class":118},[100,80814,3499],{"class":191},[100,80816,170],{"class":118},[100,80818,51844],{"class":178},[100,80820,33665],{"class":118},[100,80822,80823,80825,80827,80829],{"class":102,"line":152},[100,80824,200],{"class":114},[100,80826,170],{"class":118},[100,80828,29858],{"class":178},[100,80830,215],{"class":118},[14,80832,80833],{},[22,80834,218],{},[91,80836,80838],{"className":93,"code":80837,"language":95,"meta":96,"style":96},"0\n1\n2\n3\n4\n",[17,80839,80840,80844,80848,80852,80856],{"__ignoreMap":96},[100,80841,80842],{"class":102,"line":103},[100,80843,44572],{"class":122},[100,80845,80846],{"class":102,"line":135},[100,80847,228],{"class":122},[100,80849,80850],{"class":102,"line":142},[100,80851,233],{"class":122},[100,80853,80854],{"class":102,"line":152},[100,80855,1500],{"class":122},[100,80857,80858],{"class":102,"line":164},[100,80859,63793],{"class":122},[14,80861,241,80862,80864,80865,80867],{},[17,80863,6119],{}," only when converting to a whole number makes sense. If you need decimal values, change the logic instead of forcing a float into ",[17,80866,32990],{}," or another integer-only operation.",[77,80869,5881],{"id":5880},[14,80871,9328],{},[40,80873,80874,80877,80880,80887],{},[43,80875,80876],{},"Python expected an integer value",[43,80878,80879],{},"Your code gave a float instead",[43,80881,80882,80883,3178,80885],{},"An integer is a whole number like ",[17,80884,11541],{},[17,80886,356],{},[43,80888,80889,80890,3178,80893],{},"A float is a decimal number like ",[17,80891,80892],{},"3.5",[17,80894,80895],{},"10.0",[14,80897,80898,80899,80902,80903,80905],{},"Even if a float looks like a whole number, such as ",[17,80900,80901],{},"5.0",", it is still a ",[17,80904,60476],{}," type in Python.",[14,80907,80908,80909,290],{},"If you are not sure about the difference, see ",[295,80910,80912],{"href":80911},"\u002Flearn\u002Fpython-numbers-explained-int-float-complex\u002F","Python numbers explained: int, float, complex",[77,80914,80916],{"id":80915},"where-beginners-usually-see-this-error","Where beginners usually see this error",[14,80918,80919],{},"Beginners often see this error in places where Python needs a count or a position, such as:",[40,80921,80922,80927,80930,80936],{},[43,80923,1357,80924,80926],{},[17,80925,32990],{}," with a float",[43,80928,80929],{},"Passing a float as a list index",[43,80931,80932,80933],{},"Using a float in string repetition like ",[17,80934,80935],{},"\"a\" * 2.5",[43,80937,80938],{},"Using a float where Python needs a count or position",[14,80940,80941,80942,80946,80947,80949],{},"A very common example is the ",[295,80943,80944,6354],{"href":32987},[17,80945,32990],{},", because ",[17,80948,32990],{}," only accepts integers.",[77,80951,11031],{"id":11030},[14,80953,1891],{},[91,80955,80957],{"className":93,"code":80956,"language":95,"meta":96,"style":96},"for i in range(5.0):\n    print(i)\n",[17,80958,80959,80975],{"__ignoreMap":96},[100,80960,80961,80963,80965,80967,80969,80971,80973],{"class":102,"line":103},[100,80962,71],{"class":145},[100,80964,29838],{"class":106},[100,80966,617],{"class":145},[100,80968,29843],{"class":114},[100,80970,170],{"class":118},[100,80972,80901],{"class":122},[100,80974,522],{"class":118},[100,80976,80977,80979,80981,80983],{"class":102,"line":135},[100,80978,200],{"class":114},[100,80980,170],{"class":118},[100,80982,29858],{"class":178},[100,80984,215],{"class":118},[14,80986,80987],{},[22,80988,80989],{},"Result:",[91,80991,80993],{"className":93,"code":80992,"language":95,"meta":96,"style":96},"TypeError: 'float' object cannot be interpreted as an integer\n",[17,80994,80995],{"__ignoreMap":96},[100,80996,80997,80999,81001,81003,81005,81007,81009,81012,81015],{"class":102,"line":103},[100,80998,3538],{"class":191},[100,81000,89],{"class":118},[100,81002,1274],{"class":205},[100,81004,60476],{"class":209},[100,81006,1280],{"class":205},[100,81008,3914],{"class":191},[100,81010,81011],{"class":106}," cannot be interpreted ",[100,81013,81014],{"class":145},"as",[100,81016,81017],{"class":106}," an integer\n",[14,81019,25013],{},[40,81021,81022,81028,81034],{},[43,81023,81024,81027],{},[17,81025,81026],{},"range(5.0)"," raises this error",[43,81029,81030,81031,81033],{},"Even ",[17,81032,80901],{}," is still a float, not an int",[43,81035,81036],{},"Python does not automatically treat every float as an integer",[77,81038,81040],{"id":81039},"fix-1-convert-the-float-to-int","Fix 1: Convert the float to int",[14,81042,241,81043,81046],{},[17,81044,81045],{},"int(value)"," if a whole number is acceptable.",[91,81048,81049],{"className":93,"code":80785,"language":95,"meta":96,"style":96},[17,81050,81051,81059,81063,81083],{"__ignoreMap":96},[100,81052,81053,81055,81057],{"class":102,"line":103},[100,81054,2176],{"class":106},[100,81056,111],{"class":110},[100,81058,80796],{"class":122},[100,81060,81061],{"class":102,"line":135},[100,81062,139],{"emptyLinePlaceholder":138},[100,81064,81065,81067,81069,81071,81073,81075,81077,81079,81081],{"class":102,"line":142},[100,81066,71],{"class":145},[100,81068,29838],{"class":106},[100,81070,617],{"class":145},[100,81072,29843],{"class":114},[100,81074,170],{"class":118},[100,81076,3499],{"class":191},[100,81078,170],{"class":118},[100,81080,51844],{"class":178},[100,81082,33665],{"class":118},[100,81084,81085,81087,81089,81091],{"class":102,"line":152},[100,81086,200],{"class":114},[100,81088,170],{"class":118},[100,81090,29858],{"class":178},[100,81092,215],{"class":118},[14,81094,81095],{},[22,81096,218],{},[91,81098,81099],{"className":93,"code":80837,"language":95,"meta":96,"style":96},[17,81100,81101,81105,81109,81113,81117],{"__ignoreMap":96},[100,81102,81103],{"class":102,"line":103},[100,81104,44572],{"class":122},[100,81106,81107],{"class":102,"line":135},[100,81108,228],{"class":122},[100,81110,81111],{"class":102,"line":142},[100,81112,233],{"class":122},[100,81114,81115],{"class":102,"line":152},[100,81116,1500],{"class":122},[100,81118,81119],{"class":102,"line":164},[100,81120,63793],{"class":122},[14,81122,2925,81123,34119,81126,290],{},[17,81124,81125],{},"int(5.0)",[17,81127,3557],{},[14,81129,81130],{},"Be careful:",[40,81132,81133,81138],{},[43,81134,81135,81137],{},[17,81136,81045],{}," removes the decimal part",[43,81139,81140,34119,81143,9472,81145],{},[17,81141,81142],{},"int(5.9)",[17,81144,3557],{},[17,81146,81147],{},"6",[14,81149,1844],{},[91,81151,81153],{"className":93,"code":81152,"language":95,"meta":96,"style":96},"value = 5.9\nprint(int(value))\n",[17,81154,81155,81164],{"__ignoreMap":96},[100,81156,81157,81159,81161],{"class":102,"line":103},[100,81158,3318],{"class":106},[100,81160,111],{"class":110},[100,81162,81163],{"class":122}," 5.9\n",[100,81165,81166,81168,81170,81172,81174,81176],{"class":102,"line":135},[100,81167,372],{"class":114},[100,81169,170],{"class":118},[100,81171,3499],{"class":191},[100,81173,170],{"class":118},[100,81175,757],{"class":178},[100,81177,182],{"class":118},[14,81179,81180],{},[22,81181,218],{},[91,81183,81184],{"className":93,"code":1909,"language":95,"meta":96,"style":96},[17,81185,81186],{"__ignoreMap":96},[100,81187,81188],{"class":102,"line":103},[100,81189,1909],{"class":122},[14,81191,1160,81192,290],{},[295,81193,81195],{"href":81194},"\u002Freference\u002Fpython-int-function-explained\u002F","Python int() function explained",[77,81197,81199],{"id":81198},"fix-2-use-floor-division-when-creating-counts","Fix 2: Use floor division when creating counts",[14,81201,81202],{},"A common cause of this error is division.",[14,81204,81205,81206,81208],{},"Normal division with ",[17,81207,2558],{}," returns a float:",[91,81210,81212],{"className":93,"code":81211,"language":95,"meta":96,"style":96},"items = 10\ncount = items \u002F 2\n\nprint(count)\nprint(type(count))\n",[17,81213,81214,81222,81235,81239,81249],{"__ignoreMap":96},[100,81215,81216,81218,81220],{"class":102,"line":103},[100,81217,3095],{"class":106},[100,81219,111],{"class":110},[100,81221,2181],{"class":122},[100,81223,81224,81226,81228,81231,81233],{"class":102,"line":135},[100,81225,2176],{"class":106},[100,81227,111],{"class":110},[100,81229,81230],{"class":106}," items ",[100,81232,2558],{"class":110},[100,81234,3564],{"class":122},[100,81236,81237],{"class":102,"line":142},[100,81238,139],{"emptyLinePlaceholder":138},[100,81240,81241,81243,81245,81247],{"class":102,"line":152},[100,81242,372],{"class":114},[100,81244,170],{"class":118},[100,81246,51844],{"class":178},[100,81248,215],{"class":118},[100,81250,81251,81253,81255,81257,81259,81261],{"class":102,"line":164},[100,81252,372],{"class":114},[100,81254,170],{"class":118},[100,81256,1250],{"class":191},[100,81258,170],{"class":118},[100,81260,51844],{"class":178},[100,81262,182],{"class":118},[14,81264,81265],{},[22,81266,218],{},[91,81268,81270],{"className":93,"code":81269,"language":95,"meta":96,"style":96},"5.0\n\u003Cclass 'float'>\n",[17,81271,81272,81276],{"__ignoreMap":96},[100,81273,81274],{"class":102,"line":103},[100,81275,55055],{"class":122},[100,81277,81278,81280,81282,81284,81286,81288],{"class":102,"line":135},[100,81279,2782],{"class":110},[100,81281,3417],{"class":1077},[100,81283,1274],{"class":205},[100,81285,60476],{"class":209},[100,81287,1280],{"class":205},[100,81289,1980],{"class":110},[14,81291,81292],{},"That can cause a problem here:",[91,81294,81296],{"className":93,"code":81295,"language":95,"meta":96,"style":96},"items = 10\n\nfor i in range(items \u002F 2):\n    print(i)\n",[17,81297,81298,81306,81310,81330],{"__ignoreMap":96},[100,81299,81300,81302,81304],{"class":102,"line":103},[100,81301,3095],{"class":106},[100,81303,111],{"class":110},[100,81305,2181],{"class":122},[100,81307,81308],{"class":102,"line":135},[100,81309,139],{"emptyLinePlaceholder":138},[100,81311,81312,81314,81316,81318,81320,81322,81324,81326,81328],{"class":102,"line":142},[100,81313,71],{"class":145},[100,81315,29838],{"class":106},[100,81317,617],{"class":145},[100,81319,29843],{"class":114},[100,81321,170],{"class":118},[100,81323,3095],{"class":178},[100,81325,2558],{"class":110},[100,81327,129],{"class":122},[100,81329,522],{"class":118},[100,81331,81332,81334,81336,81338],{"class":102,"line":152},[100,81333,200],{"class":114},[100,81335,170],{"class":118},[100,81337,29858],{"class":178},[100,81339,215],{"class":118},[14,81341,76217,81342,81345],{},[17,81343,81344],{},"items \u002F 2"," produces a float.",[14,81347,81348,81349,81352],{},"In many counting cases, floor division with ",[17,81350,81351],{},"\u002F\u002F"," is better:",[91,81354,81356],{"className":93,"code":81355,"language":95,"meta":96,"style":96},"items = 10\n\nfor i in range(items \u002F\u002F 2):\n    print(i)\n",[17,81357,81358,81366,81370,81390],{"__ignoreMap":96},[100,81359,81360,81362,81364],{"class":102,"line":103},[100,81361,3095],{"class":106},[100,81363,111],{"class":110},[100,81365,2181],{"class":122},[100,81367,81368],{"class":102,"line":135},[100,81369,139],{"emptyLinePlaceholder":138},[100,81371,81372,81374,81376,81378,81380,81382,81384,81386,81388],{"class":102,"line":142},[100,81373,71],{"class":145},[100,81375,29838],{"class":106},[100,81377,617],{"class":145},[100,81379,29843],{"class":114},[100,81381,170],{"class":118},[100,81383,3095],{"class":178},[100,81385,81351],{"class":110},[100,81387,129],{"class":122},[100,81389,522],{"class":118},[100,81391,81392,81394,81396,81398],{"class":102,"line":152},[100,81393,200],{"class":114},[100,81395,170],{"class":118},[100,81397,29858],{"class":178},[100,81399,215],{"class":118},[14,81401,81402],{},[22,81403,218],{},[91,81405,81406],{"className":93,"code":80837,"language":95,"meta":96,"style":96},[17,81407,81408,81412,81416,81420,81424],{"__ignoreMap":96},[100,81409,81410],{"class":102,"line":103},[100,81411,44572],{"class":122},[100,81413,81414],{"class":102,"line":135},[100,81415,228],{"class":122},[100,81417,81418],{"class":102,"line":142},[100,81419,233],{"class":122},[100,81421,81422],{"class":102,"line":152},[100,81423,1500],{"class":122},[100,81425,81426],{"class":102,"line":164},[100,81427,63793],{"class":122},[14,81429,26823],{},[40,81431,81432,81436,81441],{},[43,81433,81434,59878],{},[17,81435,2558],{},[43,81437,81438,81440],{},[17,81439,81351],{}," returns a whole-number result in many counting situations",[43,81442,81443,81445],{},[17,81444,32990],{}," needs an integer count",[77,81447,81449],{"id":81448},"fix-3-check-earlier-math-in-your-code","Fix 3: Check earlier math in your code",[14,81451,81452],{},"Sometimes the failing line looks correct, but the variable already became a float earlier.",[14,81454,1844],{},[91,81456,81458],{"className":93,"code":81457,"language":95,"meta":96,"style":96},"total = 12\ngroups = total \u002F 3\n\nprint(groups)\nprint(type(groups))\n\nfor i in range(groups):\n    print(i)\n",[17,81459,81460,81469,81482,81486,81497,81511,81515,81531],{"__ignoreMap":96},[100,81461,81462,81464,81466],{"class":102,"line":103},[100,81463,53376],{"class":106},[100,81465,111],{"class":110},[100,81467,81468],{"class":122}," 12\n",[100,81470,81471,81474,81476,81478,81480],{"class":102,"line":135},[100,81472,81473],{"class":106},"groups ",[100,81475,111],{"class":110},[100,81477,2555],{"class":106},[100,81479,2558],{"class":110},[100,81481,31170],{"class":122},[100,81483,81484],{"class":102,"line":142},[100,81485,139],{"emptyLinePlaceholder":138},[100,81487,81488,81490,81492,81495],{"class":102,"line":152},[100,81489,372],{"class":114},[100,81491,170],{"class":118},[100,81493,81494],{"class":178},"groups",[100,81496,215],{"class":118},[100,81498,81499,81501,81503,81505,81507,81509],{"class":102,"line":164},[100,81500,372],{"class":114},[100,81502,170],{"class":118},[100,81504,1250],{"class":191},[100,81506,170],{"class":118},[100,81508,81494],{"class":178},[100,81510,182],{"class":118},[100,81512,81513],{"class":102,"line":185},[100,81514,139],{"emptyLinePlaceholder":138},[100,81516,81517,81519,81521,81523,81525,81527,81529],{"class":102,"line":197},[100,81518,71],{"class":145},[100,81520,29838],{"class":106},[100,81522,617],{"class":145},[100,81524,29843],{"class":114},[100,81526,170],{"class":118},[100,81528,81494],{"class":178},[100,81530,522],{"class":118},[100,81532,81533,81535,81537,81539],{"class":102,"line":771},[100,81534,200],{"class":114},[100,81536,170],{"class":118},[100,81538,29858],{"class":178},[100,81540,215],{"class":118},[14,81542,81543],{},[22,81544,38743],{},[91,81546,81548],{"className":93,"code":81547,"language":95,"meta":96,"style":96},"4.0\n\u003Cclass 'float'>\n",[17,81549,81550,81554],{"__ignoreMap":96},[100,81551,81552],{"class":102,"line":103},[100,81553,25067],{"class":122},[100,81555,81556,81558,81560,81562,81564,81566],{"class":102,"line":135},[100,81557,2782],{"class":110},[100,81559,3417],{"class":1077},[100,81561,1274],{"class":205},[100,81563,60476],{"class":209},[100,81565,1280],{"class":205},[100,81567,1980],{"class":110},[14,81569,81570],{},"The problem started at this line:",[91,81572,81574],{"className":93,"code":81573,"language":95,"meta":96,"style":96},"groups = total \u002F 3\n",[17,81575,81576],{"__ignoreMap":96},[100,81577,81578,81580,81582,81584,81586],{"class":102,"line":103},[100,81579,81473],{"class":106},[100,81581,111],{"class":110},[100,81583,2555],{"class":106},[100,81585,2558],{"class":110},[100,81587,31170],{"class":122},[14,81589,81590],{},"A good debugging habit is to print the value and its type before the line that fails.",[91,81592,81594],{"className":93,"code":81593,"language":95,"meta":96,"style":96},"value = 8 \u002F 2\n\nprint(value)\nprint(type(value))\nprint(isinstance(value, int))\nprint(int(value))\n",[17,81595,81596,81608,81612,81622,81636,81654],{"__ignoreMap":96},[100,81597,81598,81600,81602,81604,81606],{"class":102,"line":103},[100,81599,3318],{"class":106},[100,81601,111],{"class":110},[100,81603,71812],{"class":122},[100,81605,17686],{"class":110},[100,81607,3564],{"class":122},[100,81609,81610],{"class":102,"line":135},[100,81611,139],{"emptyLinePlaceholder":138},[100,81613,81614,81616,81618,81620],{"class":102,"line":142},[100,81615,372],{"class":114},[100,81617,170],{"class":118},[100,81619,757],{"class":178},[100,81621,215],{"class":118},[100,81623,81624,81626,81628,81630,81632,81634],{"class":102,"line":152},[100,81625,372],{"class":114},[100,81627,170],{"class":118},[100,81629,1250],{"class":191},[100,81631,170],{"class":118},[100,81633,757],{"class":178},[100,81635,182],{"class":118},[100,81637,81638,81640,81642,81644,81646,81648,81650,81652],{"class":102,"line":164},[100,81639,372],{"class":114},[100,81641,170],{"class":118},[100,81643,6994],{"class":114},[100,81645,170],{"class":118},[100,81647,757],{"class":178},[100,81649,126],{"class":118},[100,81651,3389],{"class":191},[100,81653,182],{"class":118},[100,81655,81656,81658,81660,81662,81664,81666],{"class":102,"line":185},[100,81657,372],{"class":114},[100,81659,170],{"class":118},[100,81661,3499],{"class":191},[100,81663,170],{"class":118},[100,81665,757],{"class":178},[100,81667,182],{"class":118},[14,81669,81670],{},[22,81671,218],{},[91,81673,81675],{"className":93,"code":81674,"language":95,"meta":96,"style":96},"4.0\n\u003Cclass 'float'>\nFalse\n4\n",[17,81676,81677,81681,81695,81699],{"__ignoreMap":96},[100,81678,81679],{"class":102,"line":103},[100,81680,25067],{"class":122},[100,81682,81683,81685,81687,81689,81691,81693],{"class":102,"line":135},[100,81684,2782],{"class":110},[100,81686,3417],{"class":1077},[100,81688,1274],{"class":205},[100,81690,60476],{"class":209},[100,81692,1280],{"class":205},[100,81694,1980],{"class":110},[100,81696,81697],{"class":102,"line":142},[100,81698,2407],{"class":158},[100,81700,81701],{"class":102,"line":152},[100,81702,63793],{"class":122},[14,81704,5295],{},[91,81706,81708],{"className":93,"code":81707,"language":95,"meta":96,"style":96},"print(value)\nprint(type(value))\nprint(isinstance(value, int))\nprint(int(value))\n",[17,81709,81710,81720,81734,81752],{"__ignoreMap":96},[100,81711,81712,81714,81716,81718],{"class":102,"line":103},[100,81713,372],{"class":114},[100,81715,170],{"class":118},[100,81717,757],{"class":178},[100,81719,215],{"class":118},[100,81721,81722,81724,81726,81728,81730,81732],{"class":102,"line":135},[100,81723,372],{"class":114},[100,81725,170],{"class":118},[100,81727,1250],{"class":191},[100,81729,170],{"class":118},[100,81731,757],{"class":178},[100,81733,182],{"class":118},[100,81735,81736,81738,81740,81742,81744,81746,81748,81750],{"class":102,"line":142},[100,81737,372],{"class":114},[100,81739,170],{"class":118},[100,81741,6994],{"class":114},[100,81743,170],{"class":118},[100,81745,757],{"class":178},[100,81747,126],{"class":118},[100,81749,3389],{"class":191},[100,81751,182],{"class":118},[100,81753,81754,81756,81758,81760,81762,81764],{"class":102,"line":152},[100,81755,372],{"class":114},[100,81757,170],{"class":118},[100,81759,3499],{"class":191},[100,81761,170],{"class":118},[100,81763,757],{"class":178},[100,81765,182],{"class":118},[77,81767,81769],{"id":81768},"fix-4-change-the-logic-if-decimals-are-required","Fix 4: Change the logic if decimals are required",[14,81771,81772],{},"Do not force a float into an integer-only operation if decimals matter.",[14,81774,81775,81776,81778],{},"For example, if you want to step through decimal values, ",[17,81777,32990],{}," is the wrong tool.",[14,81780,28,81781,81783],{},[22,81782,1059],{}," work:",[91,81785,81787],{"className":93,"code":81786,"language":95,"meta":96,"style":96},"for x in range(0.5, 3.0):\n    print(x)\n",[17,81788,81789,81811],{"__ignoreMap":96},[100,81790,81791,81793,81795,81797,81799,81801,81804,81806,81809],{"class":102,"line":103},[100,81792,71],{"class":145},[100,81794,59659],{"class":106},[100,81796,617],{"class":145},[100,81798,29843],{"class":114},[100,81800,170],{"class":118},[100,81802,81803],{"class":122},"0.5",[100,81805,126],{"class":118},[100,81807,81808],{"class":122}," 3.0",[100,81810,522],{"class":118},[100,81812,81813,81815,81817,81819],{"class":102,"line":135},[100,81814,200],{"class":114},[100,81816,170],{"class":118},[100,81818,39024],{"class":178},[100,81820,215],{"class":118},[14,81822,81823,81824,81826],{},"Instead, use a ",[17,81825,721],{}," loop and update a float value:",[91,81828,81830],{"className":93,"code":81829,"language":95,"meta":96,"style":96},"x = 0.5\n\nwhile x \u003C 3.0:\n    print(x)\n    x += 0.5\n",[17,81831,81832,81841,81845,81857,81867],{"__ignoreMap":96},[100,81833,81834,81836,81838],{"class":102,"line":103},[100,81835,54291],{"class":106},[100,81837,111],{"class":110},[100,81839,81840],{"class":122}," 0.5\n",[100,81842,81843],{"class":102,"line":135},[100,81844,139],{"emptyLinePlaceholder":138},[100,81846,81847,81849,81851,81853,81855],{"class":102,"line":142},[100,81848,721],{"class":145},[100,81850,59659],{"class":106},[100,81852,2782],{"class":110},[100,81854,81808],{"class":122},[100,81856,149],{"class":118},[100,81858,81859,81861,81863,81865],{"class":102,"line":152},[100,81860,200],{"class":114},[100,81862,170],{"class":118},[100,81864,39024],{"class":178},[100,81866,215],{"class":118},[100,81868,81869,81872,81874],{"class":102,"line":164},[100,81870,81871],{"class":106},"    x ",[100,81873,41605],{"class":110},[100,81875,81840],{"class":122},[14,81877,81878],{},[22,81879,218],{},[91,81881,81883],{"className":93,"code":81882,"language":95,"meta":96,"style":96},"0.5\n1.0\n1.5\n2.0\n2.5\n",[17,81884,81885,81890,81895,81900,81904],{"__ignoreMap":96},[100,81886,81887],{"class":102,"line":103},[100,81888,81889],{"class":122},"0.5\n",[100,81891,81892],{"class":102,"line":135},[100,81893,81894],{"class":122},"1.0\n",[100,81896,81897],{"class":102,"line":142},[100,81898,81899],{"class":122},"1.5\n",[100,81901,81902],{"class":102,"line":152},[100,81903,53511],{"class":122},[100,81905,81906],{"class":102,"line":164},[100,81907,81908],{"class":122},"2.5\n",[14,81910,81911],{},"Use this approach when decimal steps are part of the logic.",[77,81913,6924],{"id":6923},[14,81915,81916],{},"If you see this error, follow these steps:",[3282,81918,81919,81922,81927,81930,81934],{},[43,81920,81921],{},"Read the full traceback and find the exact line.",[43,81923,19653,81924,81926],{},[17,81925,32990],{},", indexing, slicing, or repetition on that line.",[43,81928,81929],{},"Print the variable value.",[43,81931,5163,81932,290],{},[17,81933,5283],{},[43,81935,81936],{},"Decide whether to convert the value or rewrite the logic.",[14,81938,1844],{},[91,81940,81942],{"className":93,"code":81941,"language":95,"meta":96,"style":96},"count = 15 \u002F 3\n\nprint(count)\nprint(type(count))\n\nfor i in range(count):\n    print(i)\n",[17,81943,81944,81957,81961,81971,81985,81989,82005],{"__ignoreMap":96},[100,81945,81946,81948,81950,81953,81955],{"class":102,"line":103},[100,81947,2176],{"class":106},[100,81949,111],{"class":110},[100,81951,81952],{"class":122}," 15",[100,81954,17686],{"class":110},[100,81956,31170],{"class":122},[100,81958,81959],{"class":102,"line":135},[100,81960,139],{"emptyLinePlaceholder":138},[100,81962,81963,81965,81967,81969],{"class":102,"line":142},[100,81964,372],{"class":114},[100,81966,170],{"class":118},[100,81968,51844],{"class":178},[100,81970,215],{"class":118},[100,81972,81973,81975,81977,81979,81981,81983],{"class":102,"line":152},[100,81974,372],{"class":114},[100,81976,170],{"class":118},[100,81978,1250],{"class":191},[100,81980,170],{"class":118},[100,81982,51844],{"class":178},[100,81984,182],{"class":118},[100,81986,81987],{"class":102,"line":164},[100,81988,139],{"emptyLinePlaceholder":138},[100,81990,81991,81993,81995,81997,81999,82001,82003],{"class":102,"line":185},[100,81992,71],{"class":145},[100,81994,29838],{"class":106},[100,81996,617],{"class":145},[100,81998,29843],{"class":114},[100,82000,170],{"class":118},[100,82002,51844],{"class":178},[100,82004,522],{"class":118},[100,82006,82007,82009,82011,82013],{"class":102,"line":197},[100,82008,200],{"class":114},[100,82010,170],{"class":118},[100,82012,29858],{"class":178},[100,82014,215],{"class":118},[14,82016,82017],{},"After printing the type, you can decide whether:",[40,82019,82020,82026],{},[43,82021,82022,82025],{},[17,82023,82024],{},"int(count)"," is correct, or",[43,82027,82028,82030,82031],{},[17,82029,51844],{}," should have been created differently, such as with ",[17,82032,81351],{},[14,82034,82035,82036,290],{},"For a broader step-by-step process, see ",[295,82037,3853],{"href":3852},[77,82039,7117],{"id":7116},[14,82041,82042],{},"These are common reasons this error appears:",[40,82044,82045,82054,82060,82063,82066],{},[43,82046,1357,82047,15957,82049,82051,82052],{},[17,82048,2558],{},[17,82050,81351],{}," before ",[17,82053,32990],{},[43,82055,82056,82057,82059],{},"Reading a number from ",[17,82058,14906],{}," and converting it in a way that produces a float",[43,82061,82062],{},"Calculating loop counts with decimal math",[43,82064,82065],{},"Using a float as a list position",[43,82067,15941,82068,82070,82071,82073],{},[17,82069,80901],{}," is the same as ",[17,82072,3557],{}," in all Python operations",[14,82075,82076,82077,290],{},"If you are working with user input, you may also need ",[295,82078,82080],{"href":82079},"\u002Fhow-to\u002Fhow-to-convert-string-to-float-in-python\u002F","how to convert string to float in Python",[77,82082,1514],{"id":1513},[675,82084,9137,82086,82088,82089,82091],{"id":82085},"why-does-range50-fail-if-50-looks-like-a-whole-number",[17,82087,81026],{}," fail if ",[17,82090,80901],{}," looks like a whole number?",[14,82093,14478,82094,82096,82097,82099],{},[17,82095,80901],{}," is still a float type. ",[17,82098,32990],{}," requires an integer, not a float.",[675,82101,82103,82104,11353],{"id":82102},"should-i-always-fix-this-with-int","Should I always fix this with ",[17,82105,6119],{},[14,82107,82108,82109,82111],{},"No. Use ",[17,82110,6119],{}," only if dropping the decimal part is correct for your program.",[675,82113,50591,82115,3266,82117,82119],{"id":82114},"what-is-the-difference-between-and-in-python",[17,82116,2558],{},[17,82118,81351],{}," in Python?",[14,82121,82122,82124,82125,82127],{},[17,82123,2558],{}," returns a float. ",[17,82126,81351],{}," does floor division and is often better when you need a whole-number count.",[675,82129,82131,82132,11353],{"id":82130},"can-this-error-happen-outside-range","Can this error happen outside ",[17,82133,32990],{},[14,82135,82136],{},"Yes. It can also happen with indexing, slicing, repetition, and other places that require integer values.",[77,82138,1554],{"id":1553},[40,82140,82141,82145,82149,82153,82161],{},[43,82142,82143],{},[295,82144,34995],{"href":32987},[43,82146,82147],{},[295,82148,81195],{"href":81194},[43,82150,82151],{},[295,82152,80912],{"href":80911},[43,82154,82155],{},[295,82156,74872,82158],{"href":82157},"\u002Ferrors\u002Ftypeerror-str-object-cannot-be-interpreted-as-an-integer-fix",[17,82159,82160],{},"'str' object cannot be interpreted as an integer",[43,82162,82163],{},[295,82164,8995],{"href":82165},"\u002Ferrors\u002Ftypeerror-list-indices-must-be-integers-or-slices-fix",[1589,82167,82168],{},"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 .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 .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 .sZMiF, html code.shiki .sZMiF{--shiki-light:#E2931D;--shiki-default:#005CC5;--shiki-dark:#79B8FF}html pre.shiki code .slqww, html code.shiki .slqww{--shiki-light:#6182B8;--shiki-default:#24292E;--shiki-dark:#E1E4E8}html .light .shiki span {color: var(--shiki-light);background: var(--shiki-light-bg);font-style: var(--shiki-light-font-style);font-weight: var(--shiki-light-font-weight);text-decoration: var(--shiki-light-text-decoration);}html.light .shiki span {color: var(--shiki-light);background: var(--shiki-light-bg);font-style: var(--shiki-light-font-style);font-weight: var(--shiki-light-font-weight);text-decoration: var(--shiki-light-text-decoration);}html .default .shiki span {color: var(--shiki-default);background: var(--shiki-default-bg);font-style: var(--shiki-default-font-style);font-weight: var(--shiki-default-font-weight);text-decoration: var(--shiki-default-text-decoration);}html .shiki span {color: var(--shiki-default);background: var(--shiki-default-bg);font-style: var(--shiki-default-font-style);font-weight: var(--shiki-default-font-weight);text-decoration: var(--shiki-default-text-decoration);}html .dark .shiki span {color: var(--shiki-dark);background: var(--shiki-dark-bg);font-style: var(--shiki-dark-font-style);font-weight: var(--shiki-dark-font-weight);text-decoration: var(--shiki-dark-text-decoration);}html.dark .shiki span {color: var(--shiki-dark);background: var(--shiki-dark-bg);font-style: var(--shiki-dark-font-style);font-weight: var(--shiki-dark-font-weight);text-decoration: var(--shiki-dark-text-decoration);}html pre.shiki code .sjJ54, html code.shiki .sjJ54{--shiki-light:#39ADB5;--shiki-default:#032F62;--shiki-dark:#9ECBFF}html pre.shiki code .s_sjI, html code.shiki .s_sjI{--shiki-light:#91B859;--shiki-default:#032F62;--shiki-dark:#9ECBFF}html pre.shiki code .sbsja, html code.shiki .sbsja{--shiki-light:#9C3EDA;--shiki-default:#D73A49;--shiki-dark:#F97583}html pre.shiki code .s39Yj, html code.shiki .s39Yj{--shiki-light:#39ADB5;--shiki-default:#005CC5;--shiki-dark:#79B8FF}",{"title":96,"searchDepth":135,"depth":135,"links":82170},[82171,82172,82173,82174,82175,82176,82177,82178,82179,82180,82181,82191],{"id":79,"depth":135,"text":80},{"id":5880,"depth":135,"text":5881},{"id":80915,"depth":135,"text":80916},{"id":11030,"depth":135,"text":11031},{"id":81039,"depth":135,"text":81040},{"id":81198,"depth":135,"text":81199},{"id":81448,"depth":135,"text":81449},{"id":81768,"depth":135,"text":81769},{"id":6923,"depth":135,"text":6924},{"id":7116,"depth":135,"text":7117},{"id":1513,"depth":135,"text":1514,"children":82182},[82183,82185,82187,82189],{"id":82085,"depth":142,"text":82184},"Why does range(5.0) fail if 5.0 looks like a whole number?",{"id":82102,"depth":142,"text":82186},"Should I always fix this with int()?",{"id":82114,"depth":142,"text":82188},"What is the difference between \u002F and \u002F\u002F in Python?",{"id":82130,"depth":142,"text":82190},"Can this error happen outside range()?",{"id":1553,"depth":135,"text":1554},"Master typeerror float object cannot be interpreted as an integer fix in our comprehensive Python beginner guide.",{},"\u002Ferrors\u002Ftypeerror-float-object-cannot-be-interpreted-as-an-integer-fix",{"title":80763,"description":82192},"errors\u002Ftypeerror-float-object-cannot-be-interpreted-as-an-integer-fix","fBGu4W83GhmBLHXYp_XC4sI9VRg0OziNoals1edSTsU",{"id":82199,"title":82200,"body":82201,"description":83966,"extension":1623,"meta":83967,"navigation":138,"path":83968,"seo":83969,"stem":83970,"__hash__":83971},"content\u002Ferrors\u002Ftypeerror-function-object-is-not-iterable-fix.md","TypeError: 'function' object is not iterable (Fix)",{"type":7,"value":82202,"toc":83928},[82203,82209,82216,82222,82224,82282,82285,82287,82294,82316,82322,82329,82336,82338,82340,82376,82378,82380,82437,82439,82465,82467,82481,82488,82492,82495,82551,82553,82569,82572,82574,82587,82591,82594,82596,82641,82647,82651,82654,82711,82713,82736,82746,82749,82803,82810,82814,82817,82819,82900,82905,82907,82910,82992,82995,83052,83055,83059,83062,83068,83130,83137,83194,83204,83247,83252,83302,83305,83357,83359,83362,83376,83379,83447,83449,83546,83548,83612,83615,83626,83631,83633,83635,83663,83666,83743,83752,83754,83831,83833,83837,83840,83844,83851,83855,83858,83862,83865,83885,83887,83925],[10,82204,74872,82206,7375],{"id":82205},"typeerror-function-object-is-not-iterable-fix",[17,82207,82208],{},"'function' object is not iterable",[14,82210,31738,82211,82215],{},[22,82212,82213],{},[17,82214,76384],{},". This error usually means Python expected something it could loop over, but you gave it a function itself instead of the value returned by that function.",[14,82217,82218,82219,290],{},"In most cases, the fix is simple: ",[22,82220,82221],{},"call the function with parentheses",[77,82223,80],{"id":79},[91,82225,82227],{"className":93,"code":82226,"language":95,"meta":96,"style":96},"def get_numbers():\n    return [1, 2, 3]\n\nfor n in get_numbers():\n    print(n)\n",[17,82228,82229,82238,82256,82260,82272],{"__ignoreMap":96},[100,82230,82231,82233,82236],{"class":102,"line":103},[100,82232,1078],{"class":1077},[100,82234,82235],{"class":1081}," get_numbers",[100,82237,1085],{"class":118},[100,82239,82240,82242,82244,82246,82248,82250,82252,82254],{"class":102,"line":135},[100,82241,2552],{"class":145},[100,82243,594],{"class":118},[100,82245,123],{"class":122},[100,82247,126],{"class":118},[100,82249,129],{"class":122},[100,82251,126],{"class":118},[100,82253,1405],{"class":122},[100,82255,603],{"class":118},[100,82257,82258],{"class":102,"line":142},[100,82259,139],{"emptyLinePlaceholder":138},[100,82261,82262,82264,82266,82268,82270],{"class":102,"line":152},[100,82263,71],{"class":145},[100,82265,1418],{"class":106},[100,82267,617],{"class":145},[100,82269,82235],{"class":178},[100,82271,1085],{"class":118},[100,82273,82274,82276,82278,82280],{"class":102,"line":164},[100,82275,200],{"class":114},[100,82277,170],{"class":118},[100,82279,1433],{"class":178},[100,82281,215],{"class":118},[14,82283,82284],{},"This error often happens when you loop over a function name instead of the value returned by the function. Add parentheses if you meant to call the function.",[77,82286,5881],{"id":5880},[14,82288,82289,82290,82293],{},"Python expected an ",[22,82291,82292],{},"iterable"," object, such as:",[40,82295,82296,82299,82302,82305,82308,82311],{},[43,82297,82298],{},"a list",[43,82300,82301],{},"a tuple",[43,82303,82304],{},"a string",[43,82306,82307],{},"a set",[43,82309,82310],{},"a dictionary",[43,82312,82313,82314],{},"a ",[17,82315,50899],{},[14,82317,82318,82319,10873],{},"But it got a ",[22,82320,82321],{},"function object",[14,82323,82324,82325,82328],{},"A function is something you ",[22,82326,82327],{},"call"," to get a result. It is not something you can loop over directly.",[14,82330,82331,82332,3266,82334,290],{},"If you are not fully comfortable with functions yet, see ",[295,82333,29749],{"href":29189},[295,82335,298],{"href":77906},[77,82337,7741],{"id":7740},[14,82339,76694],{},[40,82341,82342,82348,82353,82373],{},[43,82343,82344,82345],{},"You wrote a function name ",[22,82346,82347],{},"without parentheses",[43,82349,82350,82351,32343],{},"You used a function in a ",[17,82352,71],{},[43,82354,82355,82356,5870,82358,5870,82360,5870,82363,5870,82366,5870,82369,82372],{},"You used a function with ",[17,82357,76220],{},[17,82359,76227],{},[17,82361,82362],{},"set()",[17,82364,82365],{},"sum()",[17,82367,82368],{},"any()",[17,82370,82371],{},"all()",", or similar code that expects an iterable",[43,82374,82375],{},"You returned a function instead of returning data by mistake",[77,82377,24958],{"id":24957},[14,82379,1891],{},[91,82381,82383],{"className":93,"code":82382,"language":95,"meta":96,"style":96},"def get_numbers():\n    return [1, 2, 3]\n\nfor n in get_numbers:\n    print(n)\n",[17,82384,82385,82393,82411,82415,82427],{"__ignoreMap":96},[100,82386,82387,82389,82391],{"class":102,"line":103},[100,82388,1078],{"class":1077},[100,82390,82235],{"class":1081},[100,82392,1085],{"class":118},[100,82394,82395,82397,82399,82401,82403,82405,82407,82409],{"class":102,"line":135},[100,82396,2552],{"class":145},[100,82398,594],{"class":118},[100,82400,123],{"class":122},[100,82402,126],{"class":118},[100,82404,129],{"class":122},[100,82406,126],{"class":118},[100,82408,1405],{"class":122},[100,82410,603],{"class":118},[100,82412,82413],{"class":102,"line":142},[100,82414,139],{"emptyLinePlaceholder":138},[100,82416,82417,82419,82421,82423,82425],{"class":102,"line":152},[100,82418,71],{"class":145},[100,82420,1418],{"class":106},[100,82422,617],{"class":145},[100,82424,82235],{"class":106},[100,82426,149],{"class":118},[100,82428,82429,82431,82433,82435],{"class":102,"line":164},[100,82430,200],{"class":114},[100,82432,170],{"class":118},[100,82434,1433],{"class":178},[100,82436,215],{"class":118},[14,82438,218],{},[91,82440,82442],{"className":93,"code":82441,"language":95,"meta":96,"style":96},"TypeError: 'function' object is not iterable\n",[17,82443,82444],{"__ignoreMap":96},[100,82445,82446,82448,82450,82452,82455,82457,82459,82461,82463],{"class":102,"line":103},[100,82447,3538],{"class":191},[100,82449,89],{"class":118},[100,82451,1274],{"class":205},[100,82453,82454],{"class":209},"function",[100,82456,1280],{"class":205},[100,82458,3914],{"class":191},[100,82460,16702],{"class":110},[100,82462,4921],{"class":110},[100,82464,75106],{"class":106},[675,82466,35438],{"id":35437},[40,82468,82469,82475],{},[43,82470,82471,82474],{},[17,82472,82473],{},"get_numbers"," is the function itself",[43,82476,82477,82480],{},[17,82478,82479],{},"get_numbers()"," is the list returned by the function",[14,82482,82483,82484,82487],{},"Python cannot loop over the function object, but it ",[22,82485,82486],{},"can"," loop over the list returned by the function.",[77,82489,82491],{"id":82490},"fix-1-call-the-function","Fix 1: Call the function",[14,82493,82494],{},"If you want the function's result, add parentheses.",[91,82496,82497],{"className":93,"code":82226,"language":95,"meta":96,"style":96},[17,82498,82499,82507,82525,82529,82541],{"__ignoreMap":96},[100,82500,82501,82503,82505],{"class":102,"line":103},[100,82502,1078],{"class":1077},[100,82504,82235],{"class":1081},[100,82506,1085],{"class":118},[100,82508,82509,82511,82513,82515,82517,82519,82521,82523],{"class":102,"line":135},[100,82510,2552],{"class":145},[100,82512,594],{"class":118},[100,82514,123],{"class":122},[100,82516,126],{"class":118},[100,82518,129],{"class":122},[100,82520,126],{"class":118},[100,82522,1405],{"class":122},[100,82524,603],{"class":118},[100,82526,82527],{"class":102,"line":142},[100,82528,139],{"emptyLinePlaceholder":138},[100,82530,82531,82533,82535,82537,82539],{"class":102,"line":152},[100,82532,71],{"class":145},[100,82534,1418],{"class":106},[100,82536,617],{"class":145},[100,82538,82235],{"class":178},[100,82540,1085],{"class":118},[100,82542,82543,82545,82547,82549],{"class":102,"line":164},[100,82544,200],{"class":114},[100,82546,170],{"class":118},[100,82548,1433],{"class":178},[100,82550,215],{"class":118},[14,82552,218],{},[91,82554,82555],{"className":93,"code":73745,"language":95,"meta":96,"style":96},[17,82556,82557,82561,82565],{"__ignoreMap":96},[100,82558,82559],{"class":102,"line":103},[100,82560,228],{"class":122},[100,82562,82563],{"class":102,"line":135},[100,82564,233],{"class":122},[100,82566,82567],{"class":102,"line":142},[100,82568,1500],{"class":122},[14,82570,82571],{},"This is the most common fix.",[14,82573,27749],{},[40,82575,82576,82582],{},[43,82577,82578,82581],{},[17,82579,82580],{},"my_function()"," if you want the returned value",[43,82583,82584,82586],{},[17,82585,24899],{}," only when you want to refer to the function itself",[77,82588,82590],{"id":82589},"fix-2-check-what-the-function-returns","Fix 2: Check what the function returns",[14,82592,82593],{},"Sometimes you already called the function, but the returned value still is not iterable.",[14,82595,580],{},[91,82597,82599],{"className":93,"code":82598,"language":95,"meta":96,"style":96},"def get_total():\n    return 10\n\nfor n in get_total():\n    print(n)\n",[17,82600,82601,82609,82615,82619,82631],{"__ignoreMap":96},[100,82602,82603,82605,82607],{"class":102,"line":103},[100,82604,1078],{"class":1077},[100,82606,78335],{"class":1081},[100,82608,1085],{"class":118},[100,82610,82611,82613],{"class":102,"line":135},[100,82612,2552],{"class":145},[100,82614,2181],{"class":122},[100,82616,82617],{"class":102,"line":142},[100,82618,139],{"emptyLinePlaceholder":138},[100,82620,82621,82623,82625,82627,82629],{"class":102,"line":152},[100,82622,71],{"class":145},[100,82624,1418],{"class":106},[100,82626,617],{"class":145},[100,82628,78335],{"class":178},[100,82630,1085],{"class":118},[100,82632,82633,82635,82637,82639],{"class":102,"line":164},[100,82634,200],{"class":114},[100,82636,170],{"class":118},[100,82638,1433],{"class":178},[100,82640,215],{"class":118},[14,82642,82643,82644,82646],{},"This also fails, because ",[17,82645,356],{}," is an integer, and integers are not iterable.",[675,82648,82650],{"id":82649},"how-to-debug-it","How to debug it",[14,82652,82653],{},"Check the returned value and its type:",[91,82655,82657],{"className":93,"code":82656,"language":95,"meta":96,"style":96},"def get_total():\n    return 10\n\nvalue = get_total()\nprint(value)\nprint(type(value))\n",[17,82658,82659,82667,82673,82677,82687,82697],{"__ignoreMap":96},[100,82660,82661,82663,82665],{"class":102,"line":103},[100,82662,1078],{"class":1077},[100,82664,78335],{"class":1081},[100,82666,1085],{"class":118},[100,82668,82669,82671],{"class":102,"line":135},[100,82670,2552],{"class":145},[100,82672,2181],{"class":122},[100,82674,82675],{"class":102,"line":142},[100,82676,139],{"emptyLinePlaceholder":138},[100,82678,82679,82681,82683,82685],{"class":102,"line":152},[100,82680,3318],{"class":106},[100,82682,111],{"class":110},[100,82684,78335],{"class":178},[100,82686,4734],{"class":118},[100,82688,82689,82691,82693,82695],{"class":102,"line":164},[100,82690,372],{"class":114},[100,82692,170],{"class":118},[100,82694,757],{"class":178},[100,82696,215],{"class":118},[100,82698,82699,82701,82703,82705,82707,82709],{"class":102,"line":185},[100,82700,372],{"class":114},[100,82702,170],{"class":118},[100,82704,1250],{"class":191},[100,82706,170],{"class":118},[100,82708,757],{"class":178},[100,82710,182],{"class":118},[14,82712,218],{},[91,82714,82716],{"className":93,"code":82715,"language":95,"meta":96,"style":96},"10\n\u003Cclass 'int'>\n",[17,82717,82718,82722],{"__ignoreMap":96},[100,82719,82720],{"class":102,"line":103},[100,82721,3410],{"class":122},[100,82723,82724,82726,82728,82730,82732,82734],{"class":102,"line":135},[100,82725,2782],{"class":110},[100,82727,3417],{"class":1077},[100,82729,1274],{"class":205},[100,82731,3499],{"class":209},[100,82733,1280],{"class":205},[100,82735,1980],{"class":110},[14,82737,82738,82739,5870,82741,5894,82743,82745],{},"If the function returns ",[17,82740,930],{},[17,82742,3499],{},[17,82744,46487],{},", you still cannot iterate over it.",[14,82747,82748],{},"A function must return something iterable, such as a list or tuple:",[91,82750,82752],{"className":93,"code":82751,"language":95,"meta":96,"style":96},"def get_total_parts():\n    return [3, 7]\n\nfor n in get_total_parts():\n    print(n)\n",[17,82753,82754,82763,82777,82781,82793],{"__ignoreMap":96},[100,82755,82756,82758,82761],{"class":102,"line":103},[100,82757,1078],{"class":1077},[100,82759,82760],{"class":1081}," get_total_parts",[100,82762,1085],{"class":118},[100,82764,82765,82767,82769,82771,82773,82775],{"class":102,"line":135},[100,82766,2552],{"class":145},[100,82768,594],{"class":118},[100,82770,11541],{"class":122},[100,82772,126],{"class":118},[100,82774,78260],{"class":122},[100,82776,603],{"class":118},[100,82778,82779],{"class":102,"line":142},[100,82780,139],{"emptyLinePlaceholder":138},[100,82782,82783,82785,82787,82789,82791],{"class":102,"line":152},[100,82784,71],{"class":145},[100,82786,1418],{"class":106},[100,82788,617],{"class":145},[100,82790,82760],{"class":178},[100,82792,1085],{"class":118},[100,82794,82795,82797,82799,82801],{"class":102,"line":164},[100,82796,200],{"class":114},[100,82798,170],{"class":118},[100,82800,1433],{"class":178},[100,82802,215],{"class":118},[14,82804,82805,82806,290],{},"If you need help checking types, see ",[295,82807,1572,82808,9570],{"href":4427},[17,82809,4430],{},[77,82811,82813],{"id":82812},"fix-3-do-not-overwrite-iterable-variables-with-functions","Fix 3: Do not overwrite iterable variables with functions",[14,82815,82816],{},"Sometimes the problem is caused by reusing the same name for different things.",[14,82818,1844],{},[91,82820,82822],{"className":93,"code":82821,"language":95,"meta":96,"style":96},"items = [1, 2, 3]\n\ndef items():\n    return [4, 5, 6]\n\nfor x in items:\n    print(x)\n",[17,82823,82824,82844,82848,82856,82874,82878,82890],{"__ignoreMap":96},[100,82825,82826,82828,82830,82832,82834,82836,82838,82840,82842],{"class":102,"line":103},[100,82827,3095],{"class":106},[100,82829,111],{"class":110},[100,82831,594],{"class":118},[100,82833,123],{"class":122},[100,82835,126],{"class":118},[100,82837,129],{"class":122},[100,82839,126],{"class":118},[100,82841,1405],{"class":122},[100,82843,603],{"class":118},[100,82845,82846],{"class":102,"line":135},[100,82847,139],{"emptyLinePlaceholder":138},[100,82849,82850,82852,82854],{"class":102,"line":142},[100,82851,1078],{"class":1077},[100,82853,12000],{"class":1081},[100,82855,1085],{"class":118},[100,82857,82858,82860,82862,82864,82866,82868,82870,82872],{"class":102,"line":152},[100,82859,2552],{"class":145},[100,82861,594],{"class":118},[100,82863,8322],{"class":122},[100,82865,126],{"class":118},[100,82867,31164],{"class":122},[100,82869,126],{"class":118},[100,82871,78826],{"class":122},[100,82873,603],{"class":118},[100,82875,82876],{"class":102,"line":164},[100,82877,139],{"emptyLinePlaceholder":138},[100,82879,82880,82882,82884,82886,82888],{"class":102,"line":185},[100,82881,71],{"class":145},[100,82883,59659],{"class":106},[100,82885,617],{"class":145},[100,82887,12000],{"class":106},[100,82889,149],{"class":118},[100,82891,82892,82894,82896,82898],{"class":102,"line":197},[100,82893,200],{"class":114},[100,82895,170],{"class":118},[100,82897,39024],{"class":178},[100,82899,215],{"class":118},[14,82901,7194,82902,82904],{},[17,82903,3146],{}," now refers to the function, not the original list.",[675,82906,30353],{"id":30352},[14,82908,82909],{},"Use different names:",[91,82911,82913],{"className":93,"code":82912,"language":95,"meta":96,"style":96},"items = [1, 2, 3]\n\ndef get_items():\n    return [4, 5, 6]\n\nfor x in items:\n    print(x)\n",[17,82914,82915,82935,82939,82948,82966,82970,82982],{"__ignoreMap":96},[100,82916,82917,82919,82921,82923,82925,82927,82929,82931,82933],{"class":102,"line":103},[100,82918,3095],{"class":106},[100,82920,111],{"class":110},[100,82922,594],{"class":118},[100,82924,123],{"class":122},[100,82926,126],{"class":118},[100,82928,129],{"class":122},[100,82930,126],{"class":118},[100,82932,1405],{"class":122},[100,82934,603],{"class":118},[100,82936,82937],{"class":102,"line":135},[100,82938,139],{"emptyLinePlaceholder":138},[100,82940,82941,82943,82946],{"class":102,"line":142},[100,82942,1078],{"class":1077},[100,82944,82945],{"class":1081}," get_items",[100,82947,1085],{"class":118},[100,82949,82950,82952,82954,82956,82958,82960,82962,82964],{"class":102,"line":152},[100,82951,2552],{"class":145},[100,82953,594],{"class":118},[100,82955,8322],{"class":122},[100,82957,126],{"class":118},[100,82959,31164],{"class":122},[100,82961,126],{"class":118},[100,82963,78826],{"class":122},[100,82965,603],{"class":118},[100,82967,82968],{"class":102,"line":164},[100,82969,139],{"emptyLinePlaceholder":138},[100,82971,82972,82974,82976,82978,82980],{"class":102,"line":185},[100,82973,71],{"class":145},[100,82975,59659],{"class":106},[100,82977,617],{"class":145},[100,82979,12000],{"class":106},[100,82981,149],{"class":118},[100,82983,82984,82986,82988,82990],{"class":102,"line":197},[100,82985,200],{"class":114},[100,82987,170],{"class":118},[100,82989,39024],{"class":178},[100,82991,215],{"class":118},[14,82993,82994],{},"Or, if you want the function result:",[91,82996,82998],{"className":93,"code":82997,"language":95,"meta":96,"style":96},"def get_items():\n    return [4, 5, 6]\n\nfor x in get_items():\n    print(x)\n",[17,82999,83000,83008,83026,83030,83042],{"__ignoreMap":96},[100,83001,83002,83004,83006],{"class":102,"line":103},[100,83003,1078],{"class":1077},[100,83005,82945],{"class":1081},[100,83007,1085],{"class":118},[100,83009,83010,83012,83014,83016,83018,83020,83022,83024],{"class":102,"line":135},[100,83011,2552],{"class":145},[100,83013,594],{"class":118},[100,83015,8322],{"class":122},[100,83017,126],{"class":118},[100,83019,31164],{"class":122},[100,83021,126],{"class":118},[100,83023,78826],{"class":122},[100,83025,603],{"class":118},[100,83027,83028],{"class":102,"line":142},[100,83029,139],{"emptyLinePlaceholder":138},[100,83031,83032,83034,83036,83038,83040],{"class":102,"line":152},[100,83033,71],{"class":145},[100,83035,59659],{"class":106},[100,83037,617],{"class":145},[100,83039,82945],{"class":178},[100,83041,1085],{"class":118},[100,83043,83044,83046,83048,83050],{"class":102,"line":164},[100,83045,200],{"class":114},[100,83047,170],{"class":118},[100,83049,39024],{"class":178},[100,83051,215],{"class":118},[14,83053,83054],{},"Clear names help prevent this kind of mistake.",[77,83056,83058],{"id":83057},"where-this-error-appears","Where this error appears",[14,83060,83061],{},"You may see this error in places like these:",[675,83063,83065,83066,32343],{"id":83064},"in-a-for-loop","In a ",[17,83067,71],{},[91,83069,83071],{"className":93,"code":83070,"language":95,"meta":96,"style":96},"def names():\n    return [\"Ana\", \"Ben\"]\n\nfor name in names:\n    print(name)\n",[17,83072,83073,83081,83104,83108,83120],{"__ignoreMap":96},[100,83074,83075,83077,83079],{"class":102,"line":103},[100,83076,1078],{"class":1077},[100,83078,75276],{"class":1081},[100,83080,1085],{"class":118},[100,83082,83083,83085,83087,83089,83091,83093,83095,83097,83100,83102],{"class":102,"line":135},[100,83084,2552],{"class":145},[100,83086,594],{"class":118},[100,83088,206],{"class":205},[100,83090,34819],{"class":209},[100,83092,206],{"class":205},[100,83094,126],{"class":118},[100,83096,1708],{"class":205},[100,83098,83099],{"class":209},"Ben",[100,83101,206],{"class":205},[100,83103,603],{"class":118},[100,83105,83106],{"class":102,"line":142},[100,83107,139],{"emptyLinePlaceholder":138},[100,83109,83110,83112,83114,83116,83118],{"class":102,"line":152},[100,83111,71],{"class":145},[100,83113,4915],{"class":106},[100,83115,617],{"class":145},[100,83117,75276],{"class":106},[100,83119,149],{"class":118},[100,83121,83122,83124,83126,83128],{"class":102,"line":164},[100,83123,200],{"class":114},[100,83125,170],{"class":118},[100,83127,2853],{"class":178},[100,83129,215],{"class":118},[675,83131,83133,83134,83136],{"id":83132},"with-the-in-operator","With the ",[17,83135,617],{}," operator",[91,83138,83140],{"className":93,"code":83139,"language":95,"meta":96,"style":96},"def colors():\n    return [\"red\", \"blue\"]\n\nprint(\"red\" in colors)\n",[17,83141,83142,83150,83172,83176],{"__ignoreMap":96},[100,83143,83144,83146,83148],{"class":102,"line":103},[100,83145,1078],{"class":1077},[100,83147,38420],{"class":1081},[100,83149,1085],{"class":118},[100,83151,83152,83154,83156,83158,83160,83162,83164,83166,83168,83170],{"class":102,"line":135},[100,83153,2552],{"class":145},[100,83155,594],{"class":118},[100,83157,206],{"class":205},[100,83159,35266],{"class":209},[100,83161,206],{"class":205},[100,83163,126],{"class":118},[100,83165,1708],{"class":205},[100,83167,35284],{"class":209},[100,83169,206],{"class":205},[100,83171,603],{"class":118},[100,83173,83174],{"class":102,"line":142},[100,83175,139],{"emptyLinePlaceholder":138},[100,83177,83178,83180,83182,83184,83186,83188,83190,83192],{"class":102,"line":152},[100,83179,372],{"class":114},[100,83181,170],{"class":118},[100,83183,206],{"class":205},[100,83185,35266],{"class":209},[100,83187,206],{"class":205},[100,83189,1971],{"class":145},[100,83191,38420],{"class":178},[100,83193,215],{"class":118},[675,83195,83197,83198,5870,83200,5894,83202],{"id":83196},"with-list-tuple-or-set","With ",[17,83199,76220],{},[17,83201,76227],{},[17,83203,82362],{},[91,83205,83207],{"className":93,"code":83206,"language":95,"meta":96,"style":96},"def letters():\n    return \"abc\"\n\nprint(list(letters))\n",[17,83208,83209,83218,83229,83233],{"__ignoreMap":96},[100,83210,83211,83213,83216],{"class":102,"line":103},[100,83212,1078],{"class":1077},[100,83214,83215],{"class":1081}," letters",[100,83217,1085],{"class":118},[100,83219,83220,83222,83224,83227],{"class":102,"line":135},[100,83221,2552],{"class":145},[100,83223,1708],{"class":205},[100,83225,83226],{"class":209},"abc",[100,83228,1714],{"class":205},[100,83230,83231],{"class":102,"line":142},[100,83232,139],{"emptyLinePlaceholder":138},[100,83234,83235,83237,83239,83241,83243,83245],{"class":102,"line":152},[100,83236,372],{"class":114},[100,83238,170],{"class":118},[100,83240,1235],{"class":191},[100,83242,170],{"class":118},[100,83244,7997],{"class":178},[100,83246,182],{"class":118},[675,83248,83197,83250],{"id":83249},"with-sum",[17,83251,82365],{},[91,83253,83255],{"className":93,"code":83254,"language":95,"meta":96,"style":96},"def numbers():\n    return [1, 2, 3]\n\nprint(sum(numbers))\n",[17,83256,83257,83265,83283,83287],{"__ignoreMap":96},[100,83258,83259,83261,83263],{"class":102,"line":103},[100,83260,1078],{"class":1077},[100,83262,620],{"class":1081},[100,83264,1085],{"class":118},[100,83266,83267,83269,83271,83273,83275,83277,83279,83281],{"class":102,"line":135},[100,83268,2552],{"class":145},[100,83270,594],{"class":118},[100,83272,123],{"class":122},[100,83274,126],{"class":118},[100,83276,129],{"class":122},[100,83278,126],{"class":118},[100,83280,1405],{"class":122},[100,83282,603],{"class":118},[100,83284,83285],{"class":102,"line":142},[100,83286,139],{"emptyLinePlaceholder":138},[100,83288,83289,83291,83293,83296,83298,83300],{"class":102,"line":152},[100,83290,372],{"class":114},[100,83292,170],{"class":118},[100,83294,83295],{"class":114},"sum",[100,83297,170],{"class":118},[100,83299,179],{"class":178},[100,83301,182],{"class":118},[14,83303,83304],{},"In all of these cases, the fix is usually to call the function:",[91,83306,83308],{"className":93,"code":83307,"language":95,"meta":96,"style":96},"print(\"red\" in colors())\nprint(list(letters()))\nprint(sum(numbers()))\n",[17,83309,83310,83328,83343],{"__ignoreMap":96},[100,83311,83312,83314,83316,83318,83320,83322,83324,83326],{"class":102,"line":103},[100,83313,372],{"class":114},[100,83315,170],{"class":118},[100,83317,206],{"class":205},[100,83319,35266],{"class":209},[100,83321,206],{"class":205},[100,83323,1971],{"class":145},[100,83325,38420],{"class":178},[100,83327,3370],{"class":118},[100,83329,83330,83332,83334,83336,83338,83340],{"class":102,"line":135},[100,83331,372],{"class":114},[100,83333,170],{"class":118},[100,83335,1235],{"class":191},[100,83337,170],{"class":118},[100,83339,7997],{"class":178},[100,83341,83342],{"class":118},"()))\n",[100,83344,83345,83347,83349,83351,83353,83355],{"class":102,"line":142},[100,83346,372],{"class":114},[100,83348,170],{"class":118},[100,83350,83295],{"class":114},[100,83352,170],{"class":118},[100,83354,179],{"class":178},[100,83356,83342],{"class":118},[77,83358,6924],{"id":6923},[14,83360,83361],{},"When you see this error, use these steps:",[3282,83363,83364,83367,83370,83373],{},[43,83365,83366],{},"Find the exact line shown in the traceback",[43,83368,83369],{},"Check whether you used a function name without parentheses",[43,83371,83372],{},"Print the object and its type before the failing line",[43,83374,83375],{},"Confirm that the value is actually iterable",[14,83377,83378],{},"Useful debugging code:",[91,83380,83382],{"className":93,"code":83381,"language":95,"meta":96,"style":96},"print(my_function)\nprint(type(my_function))\n\nvalue = my_function()\nprint(value)\nprint(type(value))\n",[17,83383,83384,83394,83408,83412,83423,83433],{"__ignoreMap":96},[100,83385,83386,83388,83390,83392],{"class":102,"line":103},[100,83387,372],{"class":114},[100,83389,170],{"class":118},[100,83391,24899],{"class":178},[100,83393,215],{"class":118},[100,83395,83396,83398,83400,83402,83404,83406],{"class":102,"line":135},[100,83397,372],{"class":114},[100,83399,170],{"class":118},[100,83401,1250],{"class":191},[100,83403,170],{"class":118},[100,83405,24899],{"class":178},[100,83407,182],{"class":118},[100,83409,83410],{"class":102,"line":142},[100,83411,139],{"emptyLinePlaceholder":138},[100,83413,83414,83416,83418,83421],{"class":102,"line":152},[100,83415,3318],{"class":106},[100,83417,111],{"class":110},[100,83419,83420],{"class":178}," my_function",[100,83422,4734],{"class":118},[100,83424,83425,83427,83429,83431],{"class":102,"line":164},[100,83426,372],{"class":114},[100,83428,170],{"class":118},[100,83430,757],{"class":178},[100,83432,215],{"class":118},[100,83434,83435,83437,83439,83441,83443,83445],{"class":102,"line":185},[100,83436,372],{"class":114},[100,83438,170],{"class":118},[100,83440,1250],{"class":191},[100,83442,170],{"class":118},[100,83444,757],{"class":178},[100,83446,182],{"class":118},[675,83448,48552],{"id":75694},[91,83450,83452],{"className":93,"code":83451,"language":95,"meta":96,"style":96},"def get_numbers():\n    return [1, 2, 3]\n\nprint(get_numbers)\nprint(type(get_numbers))\n\nvalue = get_numbers()\nprint(value)\nprint(type(value))\n",[17,83453,83454,83462,83480,83484,83494,83508,83512,83522,83532],{"__ignoreMap":96},[100,83455,83456,83458,83460],{"class":102,"line":103},[100,83457,1078],{"class":1077},[100,83459,82235],{"class":1081},[100,83461,1085],{"class":118},[100,83463,83464,83466,83468,83470,83472,83474,83476,83478],{"class":102,"line":135},[100,83465,2552],{"class":145},[100,83467,594],{"class":118},[100,83469,123],{"class":122},[100,83471,126],{"class":118},[100,83473,129],{"class":122},[100,83475,126],{"class":118},[100,83477,1405],{"class":122},[100,83479,603],{"class":118},[100,83481,83482],{"class":102,"line":142},[100,83483,139],{"emptyLinePlaceholder":138},[100,83485,83486,83488,83490,83492],{"class":102,"line":152},[100,83487,372],{"class":114},[100,83489,170],{"class":118},[100,83491,82473],{"class":178},[100,83493,215],{"class":118},[100,83495,83496,83498,83500,83502,83504,83506],{"class":102,"line":164},[100,83497,372],{"class":114},[100,83499,170],{"class":118},[100,83501,1250],{"class":191},[100,83503,170],{"class":118},[100,83505,82473],{"class":178},[100,83507,182],{"class":118},[100,83509,83510],{"class":102,"line":185},[100,83511,139],{"emptyLinePlaceholder":138},[100,83513,83514,83516,83518,83520],{"class":102,"line":197},[100,83515,3318],{"class":106},[100,83517,111],{"class":110},[100,83519,82235],{"class":178},[100,83521,4734],{"class":118},[100,83523,83524,83526,83528,83530],{"class":102,"line":771},[100,83525,372],{"class":114},[100,83527,170],{"class":118},[100,83529,757],{"class":178},[100,83531,215],{"class":118},[100,83533,83534,83536,83538,83540,83542,83544],{"class":102,"line":787},[100,83535,372],{"class":114},[100,83537,170],{"class":118},[100,83539,1250],{"class":191},[100,83541,170],{"class":118},[100,83543,757],{"class":178},[100,83545,182],{"class":118},[14,83547,218],{},[91,83549,83551],{"className":93,"code":83550,"language":95,"meta":96,"style":96},"\u003Cfunction get_numbers at 0x...>\n\u003Cclass 'function'>\n[1, 2, 3]\n\u003Cclass 'list'>\n",[17,83552,83553,83568,83582,83598],{"__ignoreMap":96},[100,83554,83555,83557,83560,83563,83566],{"class":102,"line":103},[100,83556,2782],{"class":110},[100,83558,83559],{"class":106},"function get_numbers at ",[100,83561,83562],{"class":71695},"0x",[100,83564,83565],{"class":527},"...",[100,83567,1980],{"class":110},[100,83569,83570,83572,83574,83576,83578,83580],{"class":102,"line":135},[100,83571,2782],{"class":110},[100,83573,3417],{"class":1077},[100,83575,1274],{"class":205},[100,83577,82454],{"class":209},[100,83579,1280],{"class":205},[100,83581,1980],{"class":110},[100,83583,83584,83586,83588,83590,83592,83594,83596],{"class":102,"line":142},[100,83585,3166],{"class":118},[100,83587,123],{"class":122},[100,83589,126],{"class":118},[100,83591,129],{"class":122},[100,83593,126],{"class":118},[100,83595,1405],{"class":122},[100,83597,603],{"class":118},[100,83599,83600,83602,83604,83606,83608,83610],{"class":102,"line":152},[100,83601,2782],{"class":110},[100,83603,3417],{"class":1077},[100,83605,1274],{"class":205},[100,83607,1235],{"class":209},[100,83609,1280],{"class":205},[100,83611,1980],{"class":110},[14,83613,83614],{},"That makes the problem easier to spot:",[40,83616,83617,83622],{},[43,83618,83619,83621],{},[17,83620,82473],{}," is a function",[43,83623,83624,4101],{},[17,83625,82479],{},[14,83627,83628,83629,290],{},"If you want a step-by-step process, see the ",[295,83630,7077],{"href":3852},[77,83632,7117],{"id":7116},[14,83634,9001],{},[40,83636,83637,83645,83652,83657,83660],{},[43,83638,1357,83639,15957,83642],{},[17,83640,83641],{},"for x in my_function",[17,83643,83644],{},"for x in my_function()",[43,83646,83647,83648,3178,83650,61921],{},"Passing a function to ",[17,83649,76220],{},[17,83651,82362],{},[43,83653,83654,83655,83136],{},"Forgetting to call a function before using the ",[17,83656,617],{},[43,83658,83659],{},"Returning a function object instead of returned data",[43,83661,83662],{},"Reusing the same name for both data and a function",[14,83664,83665],{},"Here is one more example of returning a function by mistake:",[91,83667,83669],{"className":93,"code":83668,"language":95,"meta":96,"style":96},"def get_numbers():\n    return [1, 2, 3]\n\ndef wrapper():\n    return get_numbers\n\nfor n in wrapper():\n    print(n)\n",[17,83670,83671,83679,83697,83701,83710,83717,83721,83733],{"__ignoreMap":96},[100,83672,83673,83675,83677],{"class":102,"line":103},[100,83674,1078],{"class":1077},[100,83676,82235],{"class":1081},[100,83678,1085],{"class":118},[100,83680,83681,83683,83685,83687,83689,83691,83693,83695],{"class":102,"line":135},[100,83682,2552],{"class":145},[100,83684,594],{"class":118},[100,83686,123],{"class":122},[100,83688,126],{"class":118},[100,83690,129],{"class":122},[100,83692,126],{"class":118},[100,83694,1405],{"class":122},[100,83696,603],{"class":118},[100,83698,83699],{"class":102,"line":142},[100,83700,139],{"emptyLinePlaceholder":138},[100,83702,83703,83705,83708],{"class":102,"line":152},[100,83704,1078],{"class":1077},[100,83706,83707],{"class":1081}," wrapper",[100,83709,1085],{"class":118},[100,83711,83712,83714],{"class":102,"line":164},[100,83713,2552],{"class":145},[100,83715,83716],{"class":106}," get_numbers\n",[100,83718,83719],{"class":102,"line":185},[100,83720,139],{"emptyLinePlaceholder":138},[100,83722,83723,83725,83727,83729,83731],{"class":102,"line":197},[100,83724,71],{"class":145},[100,83726,1418],{"class":106},[100,83728,617],{"class":145},[100,83730,83707],{"class":178},[100,83732,1085],{"class":118},[100,83734,83735,83737,83739,83741],{"class":102,"line":771},[100,83736,200],{"class":114},[100,83738,170],{"class":118},[100,83740,1433],{"class":178},[100,83742,215],{"class":118},[14,83744,7194,83745,83748,83749,83751],{},[17,83746,83747],{},"wrapper()"," returns the function ",[17,83750,82473],{},", not the list.",[675,83753,30958],{"id":30957},[91,83755,83757],{"className":93,"code":83756,"language":95,"meta":96,"style":96},"def get_numbers():\n    return [1, 2, 3]\n\ndef wrapper():\n    return get_numbers()\n\nfor n in wrapper():\n    print(n)\n",[17,83758,83759,83767,83785,83789,83797,83805,83809,83821],{"__ignoreMap":96},[100,83760,83761,83763,83765],{"class":102,"line":103},[100,83762,1078],{"class":1077},[100,83764,82235],{"class":1081},[100,83766,1085],{"class":118},[100,83768,83769,83771,83773,83775,83777,83779,83781,83783],{"class":102,"line":135},[100,83770,2552],{"class":145},[100,83772,594],{"class":118},[100,83774,123],{"class":122},[100,83776,126],{"class":118},[100,83778,129],{"class":122},[100,83780,126],{"class":118},[100,83782,1405],{"class":122},[100,83784,603],{"class":118},[100,83786,83787],{"class":102,"line":142},[100,83788,139],{"emptyLinePlaceholder":138},[100,83790,83791,83793,83795],{"class":102,"line":152},[100,83792,1078],{"class":1077},[100,83794,83707],{"class":1081},[100,83796,1085],{"class":118},[100,83798,83799,83801,83803],{"class":102,"line":164},[100,83800,2552],{"class":145},[100,83802,82235],{"class":178},[100,83804,4734],{"class":118},[100,83806,83807],{"class":102,"line":185},[100,83808,139],{"emptyLinePlaceholder":138},[100,83810,83811,83813,83815,83817,83819],{"class":102,"line":197},[100,83812,71],{"class":145},[100,83814,1418],{"class":106},[100,83816,617],{"class":145},[100,83818,83707],{"class":178},[100,83820,1085],{"class":118},[100,83822,83823,83825,83827,83829],{"class":102,"line":771},[100,83824,200],{"class":114},[100,83826,170],{"class":118},[100,83828,1433],{"class":178},[100,83830,215],{"class":118},[77,83832,1514],{"id":1513},[675,83834,83836],{"id":83835},"why-does-python-say-a-function-is-not-iterable","Why does Python say a function is not iterable?",[14,83838,83839],{},"Because a function is something you call with parentheses. It is not a collection of values you can loop over.",[675,83841,83843],{"id":83842},"how-do-i-fix-this-error-quickly","How do I fix this error quickly?",[14,83845,83846,83847,9932,83849,290],{},"Check whether you forgot parentheses. If you meant to use the function result, change ",[17,83848,24899],{},[17,83850,82580],{},[675,83852,83854],{"id":83853},"can-a-function-ever-be-iterable","Can a function ever be iterable?",[14,83856,83857],{},"Not by default. Normally you call a function to get a value, and then you iterate over that returned value if it is iterable.",[675,83859,83861],{"id":83860},"what-if-i-already-called-the-function-and-still-get-an-error","What if I already called the function and still get an error?",[14,83863,83864],{},"Then the returned value may not be iterable. Check the return value and its type.",[14,83866,83867,83868,83870,83871,83875,83876,3266,83880,290],{},"If the returned value is ",[17,83869,930],{},", you may also want to read ",[295,83872,74872,83873],{"href":76376},[17,83874,76379],{},". Similar problems can happen with ",[295,83877,74872,83878],{"href":76368},[17,83879,76371],{},[295,83881,74872,83883],{"href":83882},"\u002Ferrors\u002Ftypeerror-bool-object-is-not-iterable-fix\u002F",[17,83884,74875],{},[77,83886,1554],{"id":1553},[40,83888,83889,83893,83897,83903,83907,83913,83919],{},[43,83890,83891],{},[295,83892,29749],{"href":29189},[43,83894,83895],{},[295,83896,1561],{"href":77906},[43,83898,83899],{},[295,83900,1572,83901,1576],{"href":4427},[17,83902,4430],{},[43,83904,83905],{},[295,83906,42267],{"href":3852},[43,83908,83909],{},[295,83910,74872,83911],{"href":76376},[17,83912,76379],{},[43,83914,83915],{},[295,83916,74872,83917],{"href":76368},[17,83918,76371],{},[43,83920,83921],{},[295,83922,74872,83923],{"href":83882},[17,83924,74875],{},[1589,83926,83927],{},"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 .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 .srdBf, html code.shiki .srdBf{--shiki-light:#F76D47;--shiki-default:#005CC5;--shiki-dark:#79B8FF}html pre.shiki code .su5hD, html code.shiki .su5hD{--shiki-light:#90A4AE;--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 pre.shiki code .sptTA, html code.shiki .sptTA{--shiki-light:#6182B8;--shiki-default:#005CC5;--shiki-dark:#79B8FF}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 .smGrS, html code.shiki .smGrS{--shiki-light:#39ADB5;--shiki-default:#D73A49;--shiki-dark:#F97583}html pre.shiki code .srjyR, html code.shiki .srjyR{--shiki-light:#90A4AE;--shiki-light-font-style:inherit;--shiki-default:#B31D28;--shiki-default-font-style:italic;--shiki-dark:#FDAEB7;--shiki-dark-font-style:italic}html pre.shiki code .s_hVV, html code.shiki .s_hVV{--shiki-light:#90A4AE;--shiki-default:#005CC5;--shiki-dark:#79B8FF}",{"title":96,"searchDepth":135,"depth":135,"links":83929},[83930,83931,83932,83933,83936,83937,83940,83943,83953,83956,83959,83965],{"id":79,"depth":135,"text":80},{"id":5880,"depth":135,"text":5881},{"id":7740,"depth":135,"text":7741},{"id":24957,"depth":135,"text":24958,"children":83934},[83935],{"id":35437,"depth":142,"text":35438},{"id":82490,"depth":135,"text":82491},{"id":82589,"depth":135,"text":82590,"children":83938},[83939],{"id":82649,"depth":142,"text":82650},{"id":82812,"depth":135,"text":82813,"children":83941},[83942],{"id":30352,"depth":142,"text":30353},{"id":83057,"depth":135,"text":83058,"children":83944},[83945,83947,83949,83951],{"id":83064,"depth":142,"text":83946},"In a for loop",{"id":83132,"depth":142,"text":83948},"With the in operator",{"id":83196,"depth":142,"text":83950},"With list(), tuple(), or set()",{"id":83249,"depth":142,"text":83952},"With sum()",{"id":6923,"depth":135,"text":6924,"children":83954},[83955],{"id":75694,"depth":142,"text":48552},{"id":7116,"depth":135,"text":7117,"children":83957},[83958],{"id":30957,"depth":142,"text":30958},{"id":1513,"depth":135,"text":1514,"children":83960},[83961,83962,83963,83964],{"id":83835,"depth":142,"text":83836},{"id":83842,"depth":142,"text":83843},{"id":83853,"depth":142,"text":83854},{"id":83860,"depth":142,"text":83861},{"id":1553,"depth":135,"text":1554},"Master typeerror function object is not iterable fix in our comprehensive Python beginner guide.",{},"\u002Ferrors\u002Ftypeerror-function-object-is-not-iterable-fix",{"title":82200,"description":83966},"errors\u002Ftypeerror-function-object-is-not-iterable-fix","R3VHHeVM486hjj-mJ4qCvL8wW4X7C7vDCooXdNIeEwg",{"id":83973,"title":83974,"body":83975,"description":85157,"extension":1623,"meta":85158,"navigation":138,"path":85159,"seo":85160,"stem":85161,"__hash__":85162},"content\u002Ferrors\u002Ftypeerror-int-object-is-not-iterable-fix.md","TypeError: 'int' object is not iterable (Fix)",{"type":7,"value":83976,"toc":85136},[83977,83982,83988,83990,84069,84077,84079,84082,84087,84090,84116,84121,84123,84126,84147,84149,84151,84178,84181,84206,84208,84216,84222,84225,84234,84265,84267,84291,84299,84308,84312,84315,84317,84356,84358,84409,84411,84427,84430,84471,84473,84495,84502,84506,84509,84512,84557,84559,84575,84583,84588,84590,84593,84596,84684,84686,84753,84755,84779,84782,84796,84798,84801,84822,84824,84857,84860,84862,84865,84898,84901,84972,84978,84980,84984,84989,84998,85006,85010,85017,85021,85023,85054,85057,85084,85086,85127,85133],[10,83978,74872,83980,7375],{"id":83979},"typeerror-int-object-is-not-iterable-fix",[17,83981,76371],{},[14,83983,31738,83984,83987],{},[17,83985,83986],{},"TypeError: 'int' object is not iterable",". This page explains what the error means, why it happens, and the most common ways to correct your code.",[77,83989,80],{"id":79},[91,83991,83993],{"className":93,"code":83992,"language":95,"meta":96,"style":96},"number = 5\n\n# Wrong\nfor item in number:\n    print(item)\n\n# Right: loop over a range\nfor item in range(number):\n    print(item)\n",[17,83994,83995,84003,84007,84011,84024,84034,84038,84043,84059],{"__ignoreMap":96},[100,83996,83997,83999,84001],{"class":102,"line":103},[100,83998,1901],{"class":106},[100,84000,111],{"class":110},[100,84002,37580],{"class":122},[100,84004,84005],{"class":102,"line":135},[100,84006,139],{"emptyLinePlaceholder":138},[100,84008,84009],{"class":102,"line":142},[100,84010,79568],{"class":414},[100,84012,84013,84015,84017,84019,84022],{"class":102,"line":152},[100,84014,71],{"class":145},[100,84016,7568],{"class":106},[100,84018,617],{"class":145},[100,84020,84021],{"class":106}," number",[100,84023,149],{"class":118},[100,84025,84026,84028,84030,84032],{"class":102,"line":164},[100,84027,200],{"class":114},[100,84029,170],{"class":118},[100,84031,7555],{"class":178},[100,84033,215],{"class":118},[100,84035,84036],{"class":102,"line":185},[100,84037,139],{"emptyLinePlaceholder":138},[100,84039,84040],{"class":102,"line":197},[100,84041,84042],{"class":414},"# Right: loop over a range\n",[100,84044,84045,84047,84049,84051,84053,84055,84057],{"class":102,"line":771},[100,84046,71],{"class":145},[100,84048,7568],{"class":106},[100,84050,617],{"class":145},[100,84052,29843],{"class":114},[100,84054,170],{"class":118},[100,84056,631],{"class":178},[100,84058,522],{"class":118},[100,84060,84061,84063,84065,84067],{"class":102,"line":787},[100,84062,200],{"class":114},[100,84064,170],{"class":118},[100,84066,7555],{"class":178},[100,84068,215],{"class":118},[14,84070,84071,84072,84076],{},"An integer is a single number, not a collection. If you want to loop a certain number of times, use ",[295,84073,84074],{"href":32987},[17,84075,32990],{},". If you meant to loop over values, make sure the variable is a list, tuple, string, or another iterable.",[77,84078,5881],{"id":5880},[14,84080,84081],{},"Python raises this error when your code tries to use an integer in a place where an iterable is required.",[14,84083,1764,84084,84086],{},[22,84085,82292],{}," is an object you can loop over one item at a time.",[14,84088,84089],{},"Common iterables include:",[40,84091,84092,84096,84100,84104,84108,84112],{},[43,84093,84094],{},[17,84095,1235],{},[43,84097,84098],{},[17,84099,75238],{},[43,84101,84102],{},[17,84103,75241],{},[43,84105,84106],{},[17,84107,48164],{},[43,84109,84110],{},[17,84111,75244],{},[43,84113,84114],{},[17,84115,50899],{},[14,84117,1764,84118,84120],{},[17,84119,3499],{}," is different. It is just one value, so Python cannot iterate over it.",[77,84122,18290],{"id":18289},[14,84124,84125],{},"This error often appears in situations like these:",[40,84127,84128,84133,84141,84144],{},[43,84129,1357,84130],{},[17,84131,84132],{},"for item in my_number",[43,84134,1357,84135,84137,84138],{},[17,84136,617],{}," with a number, such as ",[17,84139,84140],{},"'a' in 5",[43,84142,84143],{},"Passing an integer to a function that expects an iterable",[43,84145,84146],{},"Accidentally replacing a list variable with an integer",[77,84148,11031],{"id":11030},[14,84150,1891],{},[91,84152,84154],{"className":93,"code":84153,"language":95,"meta":96,"style":96},"for item in 10:\n    print(item)\n",[17,84155,84156,84168],{"__ignoreMap":96},[100,84157,84158,84160,84162,84164,84166],{"class":102,"line":103},[100,84159,71],{"class":145},[100,84161,7568],{"class":106},[100,84163,617],{"class":145},[100,84165,6550],{"class":122},[100,84167,149],{"class":118},[100,84169,84170,84172,84174,84176],{"class":102,"line":135},[100,84171,200],{"class":114},[100,84173,170],{"class":118},[100,84175,7555],{"class":178},[100,84177,215],{"class":118},[14,84179,84180],{},"This produces:",[91,84182,84184],{"className":93,"code":84183,"language":95,"meta":96,"style":96},"TypeError: 'int' object is not iterable\n",[17,84185,84186],{"__ignoreMap":96},[100,84187,84188,84190,84192,84194,84196,84198,84200,84202,84204],{"class":102,"line":103},[100,84189,3538],{"class":191},[100,84191,89],{"class":118},[100,84193,1274],{"class":205},[100,84195,3499],{"class":209},[100,84197,1280],{"class":205},[100,84199,3914],{"class":191},[100,84201,16702],{"class":110},[100,84203,4921],{"class":110},[100,84205,75106],{"class":106},[14,84207,32043],{},[14,84209,14478,84210,84212,84213,84215],{},[17,84211,356],{}," is one value, not a sequence of values. A ",[17,84214,71],{}," loop needs something it can move through one item at a time.",[77,84217,47389,84219,84221],{"id":84218},"fix-1-use-range-when-you-want-to-loop-a-number-of-times",[17,84220,32990],{}," when you want to loop a number of times",[14,84223,84224],{},"This is the most common fix for beginners.",[14,84226,84227,84228,84230,84231,10873],{},"If you want to repeat something 5 times, do not loop over ",[17,84229,3557],{}," directly. Loop over ",[17,84232,84233],{},"range(5)",[91,84235,84237],{"className":93,"code":84236,"language":95,"meta":96,"style":96},"for i in range(5):\n    print(i)\n",[17,84238,84239,84255],{"__ignoreMap":96},[100,84240,84241,84243,84245,84247,84249,84251,84253],{"class":102,"line":103},[100,84242,71],{"class":145},[100,84244,29838],{"class":106},[100,84246,617],{"class":145},[100,84248,29843],{"class":114},[100,84250,170],{"class":118},[100,84252,3557],{"class":122},[100,84254,522],{"class":118},[100,84256,84257,84259,84261,84263],{"class":102,"line":135},[100,84258,200],{"class":114},[100,84260,170],{"class":118},[100,84262,29858],{"class":178},[100,84264,215],{"class":118},[14,84266,218],{},[91,84268,84269],{"className":93,"code":80837,"language":95,"meta":96,"style":96},[17,84270,84271,84275,84279,84283,84287],{"__ignoreMap":96},[100,84272,84273],{"class":102,"line":103},[100,84274,44572],{"class":122},[100,84276,84277],{"class":102,"line":135},[100,84278,228],{"class":122},[100,84280,84281],{"class":102,"line":142},[100,84282,233],{"class":122},[100,84284,84285],{"class":102,"line":152},[100,84286,1500],{"class":122},[100,84288,84289],{"class":102,"line":164},[100,84290,63793],{"class":122},[14,84292,84293,84295,84296,290],{},[17,84294,84233],{}," creates an iterable that produces the numbers ",[17,84297,84298],{},"0, 1, 2, 3, 4",[14,84300,84301,84302,28239,84304,290],{},"If you are still learning loops, see ",[295,84303,73375],{"href":73374},[295,84305,84306,1576],{"href":32987},[17,84307,32990],{},[77,84309,84311],{"id":84310},"fix-2-use-a-real-iterable-when-you-want-to-loop-over-values","Fix 2: Use a real iterable when you want to loop over values",[14,84313,84314],{},"If you want to loop over several values, store those values in a real iterable such as a list, tuple, or string.",[14,84316,9709],{},[91,84318,84320],{"className":93,"code":84319,"language":95,"meta":96,"style":96},"numbers = 123\n\nfor n in numbers:\n    print(n)\n",[17,84321,84322,84330,84334,84346],{"__ignoreMap":96},[100,84323,84324,84326,84328],{"class":102,"line":103},[100,84325,107],{"class":106},[100,84327,111],{"class":110},[100,84329,5761],{"class":122},[100,84331,84332],{"class":102,"line":135},[100,84333,139],{"emptyLinePlaceholder":138},[100,84335,84336,84338,84340,84342,84344],{"class":102,"line":142},[100,84337,71],{"class":145},[100,84339,1418],{"class":106},[100,84341,617],{"class":145},[100,84343,620],{"class":106},[100,84345,149],{"class":118},[100,84347,84348,84350,84352,84354],{"class":102,"line":152},[100,84349,200],{"class":114},[100,84351,170],{"class":118},[100,84353,1433],{"class":178},[100,84355,215],{"class":118},[14,84357,15825],{},[91,84359,84361],{"className":93,"code":84360,"language":95,"meta":96,"style":96},"numbers = [1, 2, 3]\n\nfor n in numbers:\n    print(n)\n",[17,84362,84363,84383,84387,84399],{"__ignoreMap":96},[100,84364,84365,84367,84369,84371,84373,84375,84377,84379,84381],{"class":102,"line":103},[100,84366,107],{"class":106},[100,84368,111],{"class":110},[100,84370,594],{"class":118},[100,84372,123],{"class":122},[100,84374,126],{"class":118},[100,84376,129],{"class":122},[100,84378,126],{"class":118},[100,84380,1405],{"class":122},[100,84382,603],{"class":118},[100,84384,84385],{"class":102,"line":135},[100,84386,139],{"emptyLinePlaceholder":138},[100,84388,84389,84391,84393,84395,84397],{"class":102,"line":142},[100,84390,71],{"class":145},[100,84392,1418],{"class":106},[100,84394,617],{"class":145},[100,84396,620],{"class":106},[100,84398,149],{"class":118},[100,84400,84401,84403,84405,84407],{"class":102,"line":152},[100,84402,200],{"class":114},[100,84404,170],{"class":118},[100,84406,1433],{"class":178},[100,84408,215],{"class":118},[14,84410,218],{},[91,84412,84413],{"className":93,"code":73745,"language":95,"meta":96,"style":96},[17,84414,84415,84419,84423],{"__ignoreMap":96},[100,84416,84417],{"class":102,"line":103},[100,84418,228],{"class":122},[100,84420,84421],{"class":102,"line":135},[100,84422,233],{"class":122},[100,84424,84425],{"class":102,"line":142},[100,84426,1500],{"class":122},[14,84428,84429],{},"If you are unsure what your variable contains, print its type:",[91,84431,84433],{"className":93,"code":84432,"language":95,"meta":96,"style":96},"numbers = 123\n\nprint(numbers)\nprint(type(numbers))\n",[17,84434,84435,84443,84447,84457],{"__ignoreMap":96},[100,84436,84437,84439,84441],{"class":102,"line":103},[100,84438,107],{"class":106},[100,84440,111],{"class":110},[100,84442,5761],{"class":122},[100,84444,84445],{"class":102,"line":135},[100,84446,139],{"emptyLinePlaceholder":138},[100,84448,84449,84451,84453,84455],{"class":102,"line":142},[100,84450,372],{"class":114},[100,84452,170],{"class":118},[100,84454,179],{"class":178},[100,84456,215],{"class":118},[100,84458,84459,84461,84463,84465,84467,84469],{"class":102,"line":152},[100,84460,372],{"class":114},[100,84462,170],{"class":118},[100,84464,1250],{"class":191},[100,84466,170],{"class":118},[100,84468,179],{"class":178},[100,84470,182],{"class":118},[14,84472,218],{},[91,84474,84475],{"className":93,"code":6293,"language":95,"meta":96,"style":96},[17,84476,84477,84481],{"__ignoreMap":96},[100,84478,84479],{"class":102,"line":103},[100,84480,6067],{"class":122},[100,84482,84483,84485,84487,84489,84491,84493],{"class":102,"line":135},[100,84484,2782],{"class":110},[100,84486,3417],{"class":1077},[100,84488,1274],{"class":205},[100,84490,3499],{"class":209},[100,84492,1280],{"class":205},[100,84494,1980],{"class":110},[14,84496,84497,84498,290],{},"You can learn more about checking types with the ",[295,84499,84500,1576],{"href":4427},[17,84501,4430],{},[77,84503,84505],{"id":84504},"fix-3-convert-data-before-iterating-if-needed","Fix 3: Convert data before iterating if needed",[14,84507,84508],{},"Sometimes you really do want to loop over the digits of a number.",[14,84510,84511],{},"In that case, convert the number to a string first:",[91,84513,84515],{"className":93,"code":84514,"language":95,"meta":96,"style":96},"number = 123\n\nfor digit in str(number):\n    print(digit)\n",[17,84516,84517,84525,84529,84546],{"__ignoreMap":96},[100,84518,84519,84521,84523],{"class":102,"line":103},[100,84520,1901],{"class":106},[100,84522,111],{"class":110},[100,84524,5761],{"class":122},[100,84526,84527],{"class":102,"line":135},[100,84528,139],{"emptyLinePlaceholder":138},[100,84530,84531,84533,84536,84538,84540,84542,84544],{"class":102,"line":142},[100,84532,71],{"class":145},[100,84534,84535],{"class":106}," digit ",[100,84537,617],{"class":145},[100,84539,2871],{"class":191},[100,84541,170],{"class":118},[100,84543,631],{"class":178},[100,84545,522],{"class":118},[100,84547,84548,84550,84552,84555],{"class":102,"line":152},[100,84549,200],{"class":114},[100,84551,170],{"class":118},[100,84553,84554],{"class":178},"digit",[100,84556,215],{"class":118},[14,84558,218],{},[91,84560,84561],{"className":93,"code":73745,"language":95,"meta":96,"style":96},[17,84562,84563,84567,84571],{"__ignoreMap":96},[100,84564,84565],{"class":102,"line":103},[100,84566,228],{"class":122},[100,84568,84569],{"class":102,"line":135},[100,84570,233],{"class":122},[100,84572,84573],{"class":102,"line":142},[100,84574,1500],{"class":122},[14,84576,2925,84577,34119,84579,84582],{},[17,84578,6336],{},[17,84580,84581],{},"\"123\"",", and strings are iterable.",[14,84584,84585,84586,290],{},"Use this only when looping over characters or digits makes sense. If needed, see ",[295,84587,6570],{"href":6569},[77,84589,77202],{"id":77201},[14,84591,84592],{},"When this error happens, check the value and type of the variable right before the line that fails.",[14,84594,84595],{},"Useful debug statements:",[91,84597,84599],{"className":93,"code":84598,"language":95,"meta":96,"style":96},"print(value)\nprint(type(value))\nprint(isinstance(value, int))\nprint(isinstance(value, (list, tuple, str, dict, set, range)))\n",[17,84600,84601,84611,84625,84643],{"__ignoreMap":96},[100,84602,84603,84605,84607,84609],{"class":102,"line":103},[100,84604,372],{"class":114},[100,84606,170],{"class":118},[100,84608,757],{"class":178},[100,84610,215],{"class":118},[100,84612,84613,84615,84617,84619,84621,84623],{"class":102,"line":135},[100,84614,372],{"class":114},[100,84616,170],{"class":118},[100,84618,1250],{"class":191},[100,84620,170],{"class":118},[100,84622,757],{"class":178},[100,84624,182],{"class":118},[100,84626,84627,84629,84631,84633,84635,84637,84639,84641],{"class":102,"line":142},[100,84628,372],{"class":114},[100,84630,170],{"class":118},[100,84632,6994],{"class":114},[100,84634,170],{"class":118},[100,84636,757],{"class":178},[100,84638,126],{"class":118},[100,84640,3389],{"class":191},[100,84642,182],{"class":118},[100,84644,84645,84647,84649,84651,84653,84655,84657,84659,84661,84663,84665,84667,84669,84671,84673,84675,84678,84680,84682],{"class":102,"line":152},[100,84646,372],{"class":114},[100,84648,170],{"class":118},[100,84650,6994],{"class":114},[100,84652,170],{"class":118},[100,84654,757],{"class":178},[100,84656,126],{"class":118},[100,84658,16805],{"class":118},[100,84660,1235],{"class":191},[100,84662,126],{"class":118},[100,84664,38023],{"class":191},[100,84666,126],{"class":118},[100,84668,2871],{"class":191},[100,84670,126],{"class":118},[100,84672,80035],{"class":191},[100,84674,126],{"class":118},[100,84676,84677],{"class":191}," set",[100,84679,126],{"class":118},[100,84681,29843],{"class":114},[100,84683,48433],{"class":118},[14,84685,40751],{},[91,84687,84689],{"className":93,"code":84688,"language":95,"meta":96,"style":96},"value = 7\n\nprint(value)\nprint(type(value))\n\nfor item in value:\n    print(item)\n",[17,84690,84691,84699,84703,84713,84727,84731,84743],{"__ignoreMap":96},[100,84692,84693,84695,84697],{"class":102,"line":103},[100,84694,3318],{"class":106},[100,84696,111],{"class":110},[100,84698,78309],{"class":122},[100,84700,84701],{"class":102,"line":135},[100,84702,139],{"emptyLinePlaceholder":138},[100,84704,84705,84707,84709,84711],{"class":102,"line":142},[100,84706,372],{"class":114},[100,84708,170],{"class":118},[100,84710,757],{"class":178},[100,84712,215],{"class":118},[100,84714,84715,84717,84719,84721,84723,84725],{"class":102,"line":152},[100,84716,372],{"class":114},[100,84718,170],{"class":118},[100,84720,1250],{"class":191},[100,84722,170],{"class":118},[100,84724,757],{"class":178},[100,84726,182],{"class":118},[100,84728,84729],{"class":102,"line":164},[100,84730,139],{"emptyLinePlaceholder":138},[100,84732,84733,84735,84737,84739,84741],{"class":102,"line":185},[100,84734,71],{"class":145},[100,84736,7568],{"class":106},[100,84738,617],{"class":145},[100,84740,6747],{"class":106},[100,84742,149],{"class":118},[100,84744,84745,84747,84749,84751],{"class":102,"line":197},[100,84746,200],{"class":114},[100,84748,170],{"class":118},[100,84750,7555],{"class":178},[100,84752,215],{"class":118},[14,84754,38743],{},[91,84756,84758],{"className":93,"code":84757,"language":95,"meta":96,"style":96},"7\n\u003Cclass 'int'>\n",[17,84759,84760,84765],{"__ignoreMap":96},[100,84761,84762],{"class":102,"line":103},[100,84763,84764],{"class":122},"7\n",[100,84766,84767,84769,84771,84773,84775,84777],{"class":102,"line":135},[100,84768,2782],{"class":110},[100,84770,3417],{"class":1077},[100,84772,1274],{"class":205},[100,84774,3499],{"class":209},[100,84776,1280],{"class":205},[100,84778,1980],{"class":110},[14,84780,84781],{},"Debugging steps:",[40,84783,84784,84786,84791,84793],{},[43,84785,8126],{},[43,84787,241,84788,84790],{},[17,84789,5283],{}," to confirm its type",[43,84792,6327],{},[43,84794,84795],{},"Look for places where a list or string was overwritten with an integer",[77,84797,24472],{"id":24471},[14,84799,84800],{},"A few similar-looking values behave very differently:",[40,84802,84803,84811,84819],{},[43,84804,84805,84807,84808,84810],{},[17,84806,84233],{}," is iterable, but ",[17,84809,3557],{}," is not",[43,84812,84813,84814,84816,84817,84810],{},"A string like ",[17,84815,84581],{}," is iterable, but the integer ",[17,84818,3002],{},[43,84820,84821],{},"A dictionary is iterable, but by default it loops over keys",[14,84823,1844],{},[91,84825,84827],{"className":93,"code":84826,"language":95,"meta":96,"style":96},"for ch in \"123\":\n    print(ch)\n",[17,84828,84829,84846],{"__ignoreMap":96},[100,84830,84831,84833,84836,84838,84840,84842,84844],{"class":102,"line":103},[100,84832,71],{"class":145},[100,84834,84835],{"class":106}," ch ",[100,84837,617],{"class":145},[100,84839,1708],{"class":205},[100,84841,3002],{"class":209},[100,84843,206],{"class":205},[100,84845,149],{"class":118},[100,84847,84848,84850,84852,84855],{"class":102,"line":135},[100,84849,200],{"class":114},[100,84851,170],{"class":118},[100,84853,84854],{"class":178},"ch",[100,84856,215],{"class":118},[14,84858,84859],{},"This works because a string can be iterated over one character at a time.",[77,84861,1344],{"id":1343},[14,84863,84864],{},"Here are the most common reasons for this error:",[40,84866,84867,84872,84875,84889,84892],{},[43,84868,84869,84870],{},"Looping over an integer with ",[17,84871,71],{},[43,84873,84874],{},"Using membership testing with an integer",[43,84876,84877,84878,77694,84880,5870,84882,5870,84884,5870,84886,84888],{},"Passing an ",[17,84879,3499],{},[17,84881,76220],{},[17,84883,82365],{},[17,84885,82368],{},[17,84887,82371],{},", or similar functions incorrectly",[43,84890,84891],{},"Reusing a variable name so a list becomes an integer",[43,84893,84894,84895,84897],{},"Converting user input to ",[17,84896,3499],{}," before code that expected a string or list",[14,84899,84900],{},"Example of variable reuse causing the problem:",[91,84902,84904],{"className":93,"code":84903,"language":95,"meta":96,"style":96},"items = [\"a\", \"b\", \"c\"]\nitems = 3\n\nfor item in items:\n    print(item)\n",[17,84905,84906,84938,84946,84950,84962],{"__ignoreMap":96},[100,84907,84908,84910,84912,84914,84916,84918,84920,84922,84924,84926,84928,84930,84932,84934,84936],{"class":102,"line":103},[100,84909,3095],{"class":106},[100,84911,111],{"class":110},[100,84913,594],{"class":118},[100,84915,206],{"class":205},[100,84917,295],{"class":209},[100,84919,206],{"class":205},[100,84921,126],{"class":118},[100,84923,1708],{"class":205},[100,84925,4027],{"class":209},[100,84927,206],{"class":205},[100,84929,126],{"class":118},[100,84931,1708],{"class":205},[100,84933,4036],{"class":209},[100,84935,206],{"class":205},[100,84937,603],{"class":118},[100,84939,84940,84942,84944],{"class":102,"line":135},[100,84941,3095],{"class":106},[100,84943,111],{"class":110},[100,84945,31170],{"class":122},[100,84947,84948],{"class":102,"line":142},[100,84949,139],{"emptyLinePlaceholder":138},[100,84951,84952,84954,84956,84958,84960],{"class":102,"line":152},[100,84953,71],{"class":145},[100,84955,7568],{"class":106},[100,84957,617],{"class":145},[100,84959,12000],{"class":106},[100,84961,149],{"class":118},[100,84963,84964,84966,84968,84970],{"class":102,"line":164},[100,84965,200],{"class":114},[100,84967,170],{"class":118},[100,84969,7555],{"class":178},[100,84971,215],{"class":118},[14,84973,84974,84975,84977],{},"The variable ",[17,84976,3146],{}," started as a list, but later it was changed to an integer.",[77,84979,1514],{"id":1513},[675,84981,84983],{"id":84982},"what-does-iterable-mean-in-python","What does iterable mean in Python?",[14,84985,84986,84987,290],{},"An iterable is an object you can loop over, such as a list, tuple, string, dictionary, set, or ",[17,84988,50899],{},[675,84990,9137,84992,84994,84995,84997],{"id":84991},"why-does-range5-work-but-5-does-not",[17,84993,84233],{}," work but ",[17,84996,3557],{}," does not?",[14,84999,85000,85002,85003,85005],{},[17,85001,84233],{}," creates an iterable sequence of numbers. The integer ",[17,85004,3557],{}," is only one value.",[675,85007,85009],{"id":85008},"can-i-iterate-over-the-digits-of-an-integer","Can I iterate over the digits of an integer?",[14,85011,85012,85013,85016],{},"Yes. Convert it first, such as ",[17,85014,85015],{},"str(123)",", if you want to loop over each digit as a character.",[675,85018,85020],{"id":85019},"how-do-i-loop-10-times-in-python","How do I loop 10 times in Python?",[14,85022,27749],{},[91,85024,85026],{"className":93,"code":85025,"language":95,"meta":96,"style":96},"for i in range(10):\n    print(i)\n",[17,85027,85028,85044],{"__ignoreMap":96},[100,85029,85030,85032,85034,85036,85038,85040,85042],{"class":102,"line":103},[100,85031,71],{"class":145},[100,85033,29838],{"class":106},[100,85035,617],{"class":145},[100,85037,29843],{"class":114},[100,85039,170],{"class":118},[100,85041,356],{"class":122},[100,85043,522],{"class":118},[100,85045,85046,85048,85050,85052],{"class":102,"line":135},[100,85047,200],{"class":114},[100,85049,170],{"class":118},[100,85051,29858],{"class":178},[100,85053,215],{"class":118},[14,85055,85056],{},"not:",[91,85058,85060],{"className":93,"code":85059,"language":95,"meta":96,"style":96},"for i in 10:\n    print(i)\n",[17,85061,85062,85074],{"__ignoreMap":96},[100,85063,85064,85066,85068,85070,85072],{"class":102,"line":103},[100,85065,71],{"class":145},[100,85067,29838],{"class":106},[100,85069,617],{"class":145},[100,85071,6550],{"class":122},[100,85073,149],{"class":118},[100,85075,85076,85078,85080,85082],{"class":102,"line":135},[100,85077,200],{"class":114},[100,85079,170],{"class":118},[100,85081,29858],{"class":178},[100,85083,215],{"class":118},[77,85085,1554],{"id":1553},[40,85087,85088,85092,85098,85104,85108,85115,85121],{},[43,85089,85090],{},[295,85091,73375],{"href":73374},[43,85093,85094],{},[295,85095,1572,85096,1576],{"href":32987},[17,85097,32990],{},[43,85099,85100],{},[295,85101,1572,85102,1576],{"href":4427},[17,85103,4430],{},[43,85105,85106],{},[295,85107,7326],{"href":6569},[43,85109,85110],{},[295,85111,74872,85113,7375],{"href":85112},"\u002Ferrors\u002Ftypeerror-function-object-is-not-iterable-fix\u002F",[17,85114,82208],{},[43,85116,85117],{},[295,85118,74872,85119,7375],{"href":83882},[17,85120,74875],{},[43,85122,85123],{},[295,85124,74872,85125,7375],{"href":76376},[17,85126,76379],{},[14,85128,85129,85130,85132],{},"Check the variable type first, then decide whether you should use ",[17,85131,32990],{}," or a real iterable. That usually leads you to the correct fix quickly.",[1589,85134,85135],{},"html pre.shiki code .su5hD, html code.shiki .su5hD{--shiki-light:#90A4AE;--shiki-default:#24292E;--shiki-dark:#E1E4E8}html pre.shiki code .smGrS, html code.shiki .smGrS{--shiki-light:#39ADB5;--shiki-default:#D73A49;--shiki-dark:#F97583}html pre.shiki code .srdBf, html code.shiki .srdBf{--shiki-light:#F76D47;--shiki-default:#005CC5;--shiki-dark:#79B8FF}html pre.shiki code .sutJx, html code.shiki .sutJx{--shiki-light:#90A4AE;--shiki-light-font-style:italic;--shiki-default:#6A737D;--shiki-default-font-style:inherit;--shiki-dark:#6A737D;--shiki-dark-font-style:inherit}html pre.shiki code .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 .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 .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 .sbsja, html code.shiki .sbsja{--shiki-light:#9C3EDA;--shiki-default:#D73A49;--shiki-dark:#F97583}",{"title":96,"searchDepth":135,"depth":135,"links":85137},[85138,85139,85140,85141,85142,85144,85145,85146,85147,85148,85149,85156],{"id":79,"depth":135,"text":80},{"id":5880,"depth":135,"text":5881},{"id":18289,"depth":135,"text":18290},{"id":11030,"depth":135,"text":11031},{"id":84218,"depth":135,"text":85143},"Fix 1: Use range() when you want to loop a number of times",{"id":84310,"depth":135,"text":84311},{"id":84504,"depth":135,"text":84505},{"id":77201,"depth":135,"text":77202},{"id":24471,"depth":135,"text":24472},{"id":1343,"depth":135,"text":1344},{"id":1513,"depth":135,"text":1514,"children":85150},[85151,85152,85154,85155],{"id":84982,"depth":142,"text":84983},{"id":84991,"depth":142,"text":85153},"Why does range(5) work but 5 does not?",{"id":85008,"depth":142,"text":85009},{"id":85019,"depth":142,"text":85020},{"id":1553,"depth":135,"text":1554},"Master typeerror int object is not iterable fix in our comprehensive Python beginner guide.",{},"\u002Ferrors\u002Ftypeerror-int-object-is-not-iterable-fix",{"title":83974,"description":85157},"errors\u002Ftypeerror-int-object-is-not-iterable-fix","V4wq_bZVfl6xnqtf8l1bswFGxrL7tzN4daGu3k6lMMY",{"id":85164,"title":85165,"body":85166,"description":86893,"extension":1623,"meta":86894,"navigation":138,"path":82165,"seo":86895,"stem":86896,"__hash__":86897},"content\u002Ferrors\u002Ftypeerror-list-indices-must-be-integers-or-slices-fix.md","TypeError: list indices must be integers or slices (Fix)",{"type":7,"value":85167,"toc":86873},[85168,85171,85179,85182,85196,85198,85293,85305,85307,85313,85316,85332,85335,85347,85353,85356,85407,85409,85417,85420,85475,85477,85505,85510,85512,85518,85573,85581,85593,85667,85675,85679,85686,85763,85768,85776,85779,85786,85790,85793,85795,85850,85852,85869,85872,85923,85929,85996,86003,86066,86068,86092,86096,86102,86108,86111,86170,86176,86219,86230,86235,86239,86242,86245,86318,86321,86337,86339,86404,86411,86414,86416,86419,86427,86429,86496,86498,86631,86633,86702,86705,86719,86729,86733,86754,86756,86759,86792,86794,86800,86805,86809,86815,86819,86825,86829,86837,86839,86870],[10,85169,85165],{"id":85170},"typeerror-list-indices-must-be-integers-or-slices-fix",[14,85172,31738,85173,85178],{},[22,85174,85175],{},[17,85176,85177],{},"TypeError: list indices must be integers or slices, not ...",". This error happens when you try to access a list with the wrong kind of value inside square brackets.",[14,85180,85181],{},"This page explains:",[40,85183,85184,85187,85190,85193],{},[43,85185,85186],{},"what the error means",[43,85188,85189],{},"why it happens",[43,85191,85192],{},"the most common ways to fix it",[43,85194,85195],{},"how to debug it quickly",[77,85197,80],{"id":79},[91,85199,85201],{"className":93,"code":85200,"language":95,"meta":96,"style":96},"numbers = [10, 20, 30]\nindex = 1\nprint(numbers[index])\n\n# If the index comes from input, convert it first:\nuser_index = int(input(\"Enter index: \"))\nprint(numbers[user_index])\n",[17,85202,85203,85223,85231,85245,85249,85254,85278],{"__ignoreMap":96},[100,85204,85205,85207,85209,85211,85213,85215,85217,85219,85221],{"class":102,"line":103},[100,85206,107],{"class":106},[100,85208,111],{"class":110},[100,85210,594],{"class":118},[100,85212,356],{"class":122},[100,85214,126],{"class":118},[100,85216,361],{"class":122},[100,85218,126],{"class":118},[100,85220,16342],{"class":122},[100,85222,603],{"class":118},[100,85224,85225,85227,85229],{"class":102,"line":135},[100,85226,3116],{"class":106},[100,85228,111],{"class":110},[100,85230,1093],{"class":122},[100,85232,85233,85235,85237,85239,85241,85243],{"class":102,"line":142},[100,85234,372],{"class":114},[100,85236,170],{"class":118},[100,85238,179],{"class":178},[100,85240,3166],{"class":118},[100,85242,3169],{"class":178},[100,85244,132],{"class":118},[100,85246,85247],{"class":102,"line":152},[100,85248,139],{"emptyLinePlaceholder":138},[100,85250,85251],{"class":102,"line":164},[100,85252,85253],{"class":414},"# If the index comes from input, convert it first:\n",[100,85255,85256,85259,85261,85263,85265,85267,85269,85271,85274,85276],{"class":102,"line":185},[100,85257,85258],{"class":106},"user_index ",[100,85260,111],{"class":110},[100,85262,3389],{"class":191},[100,85264,170],{"class":118},[100,85266,6210],{"class":114},[100,85268,170],{"class":118},[100,85270,206],{"class":205},[100,85272,85273],{"class":209},"Enter index: ",[100,85275,206],{"class":205},[100,85277,182],{"class":118},[100,85279,85280,85282,85284,85286,85288,85291],{"class":102,"line":197},[100,85281,372],{"class":114},[100,85283,170],{"class":118},[100,85285,179],{"class":178},[100,85287,3166],{"class":118},[100,85289,85290],{"class":178},"user_index",[100,85292,132],{"class":118},[14,85294,85295,85296,3178,85298,85300,85301,85304],{},"List positions must be integers like ",[17,85297,2279],{},[17,85299,123],{},", or slices like ",[17,85302,85303],{},"[1:3]",". Strings, floats, and other lists cannot be used as list indices.",[77,85306,5881],{"id":5880},[14,85308,571,85309,85312],{},[22,85310,85311],{},"list index"," tells Python which position to access.",[14,85314,85315],{},"Valid list indexing looks like this:",[40,85317,85318,85322,85327],{},[43,85319,85320],{},[17,85321,36138],{},[43,85323,85324],{},[17,85325,85326],{},"my_list[1]",[43,85328,85329],{},[17,85330,85331],{},"my_list[-1]",[14,85333,85334],{},"Valid list slicing looks like this:",[40,85336,85337,85342],{},[43,85338,85339],{},[17,85340,85341],{},"my_list[1:3]",[43,85343,85344],{},[17,85345,85346],{},"my_list[:2]",[14,85348,85349,85350,85352],{},"The error appears when the value inside ",[17,85351,45218],{}," is the wrong type.",[14,85354,85355],{},"For example, this works:",[91,85357,85359],{"className":93,"code":85358,"language":95,"meta":96,"style":96},"items = [\"a\", \"b\", \"c\"]\nprint(items[0])\n",[17,85360,85361,85393],{"__ignoreMap":96},[100,85362,85363,85365,85367,85369,85371,85373,85375,85377,85379,85381,85383,85385,85387,85389,85391],{"class":102,"line":103},[100,85364,3095],{"class":106},[100,85366,111],{"class":110},[100,85368,594],{"class":118},[100,85370,206],{"class":205},[100,85372,295],{"class":209},[100,85374,206],{"class":205},[100,85376,126],{"class":118},[100,85378,1708],{"class":205},[100,85380,4027],{"class":209},[100,85382,206],{"class":205},[100,85384,126],{"class":118},[100,85386,1708],{"class":205},[100,85388,4036],{"class":209},[100,85390,206],{"class":205},[100,85392,603],{"class":118},[100,85394,85395,85397,85399,85401,85403,85405],{"class":102,"line":135},[100,85396,372],{"class":114},[100,85398,170],{"class":118},[100,85400,3146],{"class":178},[100,85402,3166],{"class":118},[100,85404,2279],{"class":122},[100,85406,132],{"class":118},[14,85408,218],{},[91,85410,85411],{"className":93,"code":32945,"language":95,"meta":96,"style":96},[17,85412,85413],{"__ignoreMap":96},[100,85414,85415],{"class":102,"line":103},[100,85416,32945],{"class":106},[14,85418,85419],{},"But this does not:",[91,85421,85423],{"className":93,"code":85422,"language":95,"meta":96,"style":96},"items = [\"a\", \"b\", \"c\"]\nprint(items[\"0\"])\n",[17,85424,85425,85457],{"__ignoreMap":96},[100,85426,85427,85429,85431,85433,85435,85437,85439,85441,85443,85445,85447,85449,85451,85453,85455],{"class":102,"line":103},[100,85428,3095],{"class":106},[100,85430,111],{"class":110},[100,85432,594],{"class":118},[100,85434,206],{"class":205},[100,85436,295],{"class":209},[100,85438,206],{"class":205},[100,85440,126],{"class":118},[100,85442,1708],{"class":205},[100,85444,4027],{"class":209},[100,85446,206],{"class":205},[100,85448,126],{"class":118},[100,85450,1708],{"class":205},[100,85452,4036],{"class":209},[100,85454,206],{"class":205},[100,85456,603],{"class":118},[100,85458,85459,85461,85463,85465,85467,85469,85471,85473],{"class":102,"line":135},[100,85460,372],{"class":114},[100,85462,170],{"class":118},[100,85464,3146],{"class":178},[100,85466,3166],{"class":118},[100,85468,206],{"class":205},[100,85470,2279],{"class":209},[100,85472,206],{"class":205},[100,85474,132],{"class":118},[14,85476,75080],{},[91,85478,85480],{"className":93,"code":85479,"language":95,"meta":96,"style":96},"TypeError: list indices must be integers or slices, not str\n",[17,85481,85482],{"__ignoreMap":96},[100,85483,85484,85486,85488,85490,85493,85495,85498,85500,85502],{"class":102,"line":103},[100,85485,3538],{"class":191},[100,85487,89],{"class":118},[100,85489,14704],{"class":191},[100,85491,85492],{"class":106}," indices must be integers ",[100,85494,11841],{"class":110},[100,85496,85497],{"class":106}," slices",[100,85499,126],{"class":118},[100,85501,4921],{"class":110},[100,85503,85504],{"class":191}," str\n",[14,85506,85507,85508,290],{},"If you need a refresher on how lists work, see ",[295,85509,35359],{"href":8110},[77,85511,24958],{"id":24957},[14,85513,85514,85515,85517],{},"One of the most common causes is using a ",[22,85516,75241],{}," instead of an integer index.",[91,85519,85521],{"className":93,"code":85520,"language":95,"meta":96,"style":96},"items = [\"apple\", \"banana\", \"orange\"]\nprint(items[\"0\"])\n",[17,85522,85523,85555],{"__ignoreMap":96},[100,85524,85525,85527,85529,85531,85533,85535,85537,85539,85541,85543,85545,85547,85549,85551,85553],{"class":102,"line":103},[100,85526,3095],{"class":106},[100,85528,111],{"class":110},[100,85530,594],{"class":118},[100,85532,206],{"class":205},[100,85534,4148],{"class":209},[100,85536,206],{"class":205},[100,85538,126],{"class":118},[100,85540,1708],{"class":205},[100,85542,4157],{"class":209},[100,85544,206],{"class":205},[100,85546,126],{"class":118},[100,85548,1708],{"class":205},[100,85550,4166],{"class":209},[100,85552,206],{"class":205},[100,85554,603],{"class":118},[100,85556,85557,85559,85561,85563,85565,85567,85569,85571],{"class":102,"line":135},[100,85558,372],{"class":114},[100,85560,170],{"class":118},[100,85562,3146],{"class":178},[100,85564,3166],{"class":118},[100,85566,206],{"class":205},[100,85568,2279],{"class":209},[100,85570,206],{"class":205},[100,85572,132],{"class":118},[14,85574,7194,85575,85578,85579,290],{},[17,85576,85577],{},"\"0\""," is a string, not the integer ",[17,85580,2279],{},[14,85582,85583,85584,85589,85590,85592],{},"A very common reason for this is ",[295,85585,85587],{"href":85586},"\u002Freference\u002Fpython-input-function-explained\u002F",[17,85588,14906],{},". Even when the user types a number, ",[17,85591,14906],{}," returns a string.",[91,85594,85596],{"className":93,"code":85595,"language":95,"meta":96,"style":96},"items = [\"apple\", \"banana\", \"orange\"]\n\nindex = input(\"Enter an index: \")\nprint(items[index])\n",[17,85597,85598,85630,85634,85653],{"__ignoreMap":96},[100,85599,85600,85602,85604,85606,85608,85610,85612,85614,85616,85618,85620,85622,85624,85626,85628],{"class":102,"line":103},[100,85601,3095],{"class":106},[100,85603,111],{"class":110},[100,85605,594],{"class":118},[100,85607,206],{"class":205},[100,85609,4148],{"class":209},[100,85611,206],{"class":205},[100,85613,126],{"class":118},[100,85615,1708],{"class":205},[100,85617,4157],{"class":209},[100,85619,206],{"class":205},[100,85621,126],{"class":118},[100,85623,1708],{"class":205},[100,85625,4166],{"class":209},[100,85627,206],{"class":205},[100,85629,603],{"class":118},[100,85631,85632],{"class":102,"line":135},[100,85633,139],{"emptyLinePlaceholder":138},[100,85635,85636,85638,85640,85642,85644,85646,85649,85651],{"class":102,"line":142},[100,85637,3116],{"class":106},[100,85639,111],{"class":110},[100,85641,7221],{"class":114},[100,85643,170],{"class":118},[100,85645,206],{"class":205},[100,85647,85648],{"class":209},"Enter an index: ",[100,85650,206],{"class":205},[100,85652,215],{"class":118},[100,85654,85655,85657,85659,85661,85663,85665],{"class":102,"line":152},[100,85656,372],{"class":114},[100,85658,170],{"class":118},[100,85660,3146],{"class":178},[100,85662,3166],{"class":118},[100,85664,3169],{"class":178},[100,85666,132],{"class":118},[14,85668,85669,85670,85672,85673,290],{},"If the user enters ",[17,85671,123],{},", Python still sees it as ",[17,85674,34917],{},[77,85676,85678],{"id":85677},"fix-1-convert-string-input-to-an-integer","Fix 1: convert string input to an integer",[14,85680,85681,85682,290],{},"If the value should be a list position, convert it with ",[295,85683,85684],{"href":81194},[17,85685,6119],{},[91,85687,85689],{"className":93,"code":85688,"language":95,"meta":96,"style":96},"items = [\"apple\", \"banana\", \"orange\"]\n\nindex = int(input(\"Enter an index: \"))\nprint(items[index])\n",[17,85690,85691,85723,85727,85749],{"__ignoreMap":96},[100,85692,85693,85695,85697,85699,85701,85703,85705,85707,85709,85711,85713,85715,85717,85719,85721],{"class":102,"line":103},[100,85694,3095],{"class":106},[100,85696,111],{"class":110},[100,85698,594],{"class":118},[100,85700,206],{"class":205},[100,85702,4148],{"class":209},[100,85704,206],{"class":205},[100,85706,126],{"class":118},[100,85708,1708],{"class":205},[100,85710,4157],{"class":209},[100,85712,206],{"class":205},[100,85714,126],{"class":118},[100,85716,1708],{"class":205},[100,85718,4166],{"class":209},[100,85720,206],{"class":205},[100,85722,603],{"class":118},[100,85724,85725],{"class":102,"line":135},[100,85726,139],{"emptyLinePlaceholder":138},[100,85728,85729,85731,85733,85735,85737,85739,85741,85743,85745,85747],{"class":102,"line":142},[100,85730,3116],{"class":106},[100,85732,111],{"class":110},[100,85734,3389],{"class":191},[100,85736,170],{"class":118},[100,85738,6210],{"class":114},[100,85740,170],{"class":118},[100,85742,206],{"class":205},[100,85744,85648],{"class":209},[100,85746,206],{"class":205},[100,85748,182],{"class":118},[100,85750,85751,85753,85755,85757,85759,85761],{"class":102,"line":152},[100,85752,372],{"class":114},[100,85754,170],{"class":118},[100,85756,3146],{"class":178},[100,85758,3166],{"class":118},[100,85760,3169],{"class":178},[100,85762,132],{"class":118},[14,85764,85669,85765,85767],{},[17,85766,123],{},", the output is:",[91,85769,85770],{"className":93,"code":8817,"language":95,"meta":96,"style":96},[17,85771,85772],{"__ignoreMap":96},[100,85773,85774],{"class":102,"line":103},[100,85775,8817],{"class":106},[14,85777,85778],{},"Use this fix only when the value really should be a numeric position.",[14,85780,85781,85782,290],{},"If you want more help with this, read ",[295,85783,85785],{"href":85784},"\u002Fhow-to\u002Fhow-to-convert-user-input-to-numbers-in-python\u002F","how to convert user input to numbers in Python",[77,85787,85789],{"id":85788},"fix-2-loop-over-list-items-correctly","Fix 2: loop over list items correctly",[14,85791,85792],{},"Another common mistake is treating each list value as if it were an index.",[14,85794,9709],{},[91,85796,85798],{"className":93,"code":85797,"language":95,"meta":96,"style":96},"my_list = [10, 20, 30]\n\nfor i in my_list:\n    print(my_list[i])\n",[17,85799,85800,85820,85824,85836],{"__ignoreMap":96},[100,85801,85802,85804,85806,85808,85810,85812,85814,85816,85818],{"class":102,"line":103},[100,85803,7405],{"class":106},[100,85805,111],{"class":110},[100,85807,594],{"class":118},[100,85809,356],{"class":122},[100,85811,126],{"class":118},[100,85813,361],{"class":122},[100,85815,126],{"class":118},[100,85817,16342],{"class":122},[100,85819,603],{"class":118},[100,85821,85822],{"class":102,"line":135},[100,85823,139],{"emptyLinePlaceholder":138},[100,85825,85826,85828,85830,85832,85834],{"class":102,"line":142},[100,85827,71],{"class":145},[100,85829,29838],{"class":106},[100,85831,617],{"class":145},[100,85833,7573],{"class":106},[100,85835,149],{"class":118},[100,85837,85838,85840,85842,85844,85846,85848],{"class":102,"line":152},[100,85839,200],{"class":114},[100,85841,170],{"class":118},[100,85843,7441],{"class":178},[100,85845,3166],{"class":118},[100,85847,29858],{"class":178},[100,85849,132],{"class":118},[14,85851,25013],{},[40,85853,85854,85866],{},[43,85855,85856,85858,85859,5870,85861,5629,85863],{},[17,85857,29858],{}," takes the values ",[17,85860,356],{},[17,85862,6152],{},[17,85864,85865],{},"30",[43,85867,85868],{},"those are list items, not valid positions for this list",[14,85870,85871],{},"If you want the values, use them directly:",[91,85873,85875],{"className":93,"code":85874,"language":95,"meta":96,"style":96},"my_list = [10, 20, 30]\n\nfor i in my_list:\n    print(i)\n",[17,85876,85877,85897,85901,85913],{"__ignoreMap":96},[100,85878,85879,85881,85883,85885,85887,85889,85891,85893,85895],{"class":102,"line":103},[100,85880,7405],{"class":106},[100,85882,111],{"class":110},[100,85884,594],{"class":118},[100,85886,356],{"class":122},[100,85888,126],{"class":118},[100,85890,361],{"class":122},[100,85892,126],{"class":118},[100,85894,16342],{"class":122},[100,85896,603],{"class":118},[100,85898,85899],{"class":102,"line":135},[100,85900,139],{"emptyLinePlaceholder":138},[100,85902,85903,85905,85907,85909,85911],{"class":102,"line":142},[100,85904,71],{"class":145},[100,85906,29838],{"class":106},[100,85908,617],{"class":145},[100,85910,7573],{"class":106},[100,85912,149],{"class":118},[100,85914,85915,85917,85919,85921],{"class":102,"line":152},[100,85916,200],{"class":114},[100,85918,170],{"class":118},[100,85920,29858],{"class":178},[100,85922,215],{"class":118},[14,85924,85925,85926,89],{},"If you need positions, use ",[17,85927,85928],{},"range(len(...))",[91,85930,85932],{"className":93,"code":85931,"language":95,"meta":96,"style":96},"my_list = [10, 20, 30]\n\nfor index in range(len(my_list)):\n    print(index, my_list[index])\n",[17,85933,85934,85954,85958,85978],{"__ignoreMap":96},[100,85935,85936,85938,85940,85942,85944,85946,85948,85950,85952],{"class":102,"line":103},[100,85937,7405],{"class":106},[100,85939,111],{"class":110},[100,85941,594],{"class":118},[100,85943,356],{"class":122},[100,85945,126],{"class":118},[100,85947,361],{"class":122},[100,85949,126],{"class":118},[100,85951,16342],{"class":122},[100,85953,603],{"class":118},[100,85955,85956],{"class":102,"line":135},[100,85957,139],{"emptyLinePlaceholder":138},[100,85959,85960,85962,85964,85966,85968,85970,85972,85974,85976],{"class":102,"line":142},[100,85961,71],{"class":145},[100,85963,3136],{"class":106},[100,85965,617],{"class":145},[100,85967,29843],{"class":114},[100,85969,170],{"class":118},[100,85971,17779],{"class":114},[100,85973,170],{"class":118},[100,85975,7441],{"class":178},[100,85977,33665],{"class":118},[100,85979,85980,85982,85984,85986,85988,85990,85992,85994],{"class":102,"line":152},[100,85981,200],{"class":114},[100,85983,170],{"class":118},[100,85985,3169],{"class":178},[100,85987,126],{"class":118},[100,85989,7573],{"class":178},[100,85991,3166],{"class":118},[100,85993,3169],{"class":178},[100,85995,132],{"class":118},[14,85997,85998,85999,89],{},"A better beginner-friendly option is ",[295,86000,86001],{"href":33712},[17,86002,1575],{},[91,86004,86006],{"className":93,"code":86005,"language":95,"meta":96,"style":96},"my_list = [10, 20, 30]\n\nfor index, value in enumerate(my_list):\n    print(index, value)\n",[17,86007,86008,86028,86032,86052],{"__ignoreMap":96},[100,86009,86010,86012,86014,86016,86018,86020,86022,86024,86026],{"class":102,"line":103},[100,86011,7405],{"class":106},[100,86013,111],{"class":110},[100,86015,594],{"class":118},[100,86017,356],{"class":122},[100,86019,126],{"class":118},[100,86021,361],{"class":122},[100,86023,126],{"class":118},[100,86025,16342],{"class":122},[100,86027,603],{"class":118},[100,86029,86030],{"class":102,"line":135},[100,86031,139],{"emptyLinePlaceholder":138},[100,86033,86034,86036,86038,86040,86042,86044,86046,86048,86050],{"class":102,"line":142},[100,86035,71],{"class":145},[100,86037,36322],{"class":106},[100,86039,126],{"class":118},[100,86041,989],{"class":106},[100,86043,617],{"class":145},[100,86045,33771],{"class":114},[100,86047,170],{"class":118},[100,86049,7441],{"class":178},[100,86051,522],{"class":118},[100,86053,86054,86056,86058,86060,86062,86064],{"class":102,"line":152},[100,86055,200],{"class":114},[100,86057,170],{"class":118},[100,86059,3169],{"class":178},[100,86061,126],{"class":118},[100,86063,6747],{"class":178},[100,86065,215],{"class":118},[14,86067,218],{},[91,86069,86071],{"className":93,"code":86070,"language":95,"meta":96,"style":96},"0 10\n1 20\n2 30\n",[17,86072,86073,86079,86085],{"__ignoreMap":96},[100,86074,86075,86077],{"class":102,"line":103},[100,86076,2279],{"class":122},[100,86078,2181],{"class":122},[100,86080,86081,86083],{"class":102,"line":135},[100,86082,123],{"class":122},[100,86084,1669],{"class":122},[100,86086,86087,86089],{"class":102,"line":142},[100,86088,559],{"class":122},[100,86090,86091],{"class":122}," 30\n",[77,86093,86095],{"id":86094},"fix-3-use-dictionary-access-if-your-data-uses-keys","Fix 3: use dictionary access if your data uses keys",[14,86097,86098,86099,290],{},"Lists use ",[22,86100,86101],{},"numeric positions",[14,86103,86104,86105,86107],{},"Dictionaries use ",[22,86106,6185],{},", which are often strings.",[14,86109,86110],{},"This works with a dictionary:",[91,86112,86114],{"className":93,"code":86113,"language":95,"meta":96,"style":96},"person = {\"name\": \"Sam\", \"age\": 25}\nprint(person[\"name\"])\n",[17,86115,86116,86152],{"__ignoreMap":96},[100,86117,86118,86120,86122,86124,86126,86128,86130,86132,86134,86136,86138,86140,86142,86144,86146,86148,86150],{"class":102,"line":103},[100,86119,43766],{"class":106},[100,86121,111],{"class":110},[100,86123,12080],{"class":118},[100,86125,206],{"class":205},[100,86127,2853],{"class":209},[100,86129,206],{"class":205},[100,86131,89],{"class":118},[100,86133,1708],{"class":205},[100,86135,29329],{"class":209},[100,86137,206],{"class":205},[100,86139,126],{"class":118},[100,86141,1708],{"class":205},[100,86143,2770],{"class":209},[100,86145,206],{"class":205},[100,86147,89],{"class":118},[100,86149,16782],{"class":122},[100,86151,12093],{"class":118},[100,86153,86154,86156,86158,86160,86162,86164,86166,86168],{"class":102,"line":135},[100,86155,372],{"class":114},[100,86157,170],{"class":118},[100,86159,43801],{"class":178},[100,86161,3166],{"class":118},[100,86163,206],{"class":205},[100,86165,2853],{"class":209},[100,86167,206],{"class":205},[100,86169,132],{"class":118},[14,86171,86172,86173,86175],{},"But this does not work if ",[17,86174,43801],{}," is actually a list:",[91,86177,86179],{"className":93,"code":86178,"language":95,"meta":96,"style":96},"person = [\"Sam\", 25]\nprint(person[\"name\"])\n",[17,86180,86181,86201],{"__ignoreMap":96},[100,86182,86183,86185,86187,86189,86191,86193,86195,86197,86199],{"class":102,"line":103},[100,86184,43766],{"class":106},[100,86186,111],{"class":110},[100,86188,594],{"class":118},[100,86190,206],{"class":205},[100,86192,29329],{"class":209},[100,86194,206],{"class":205},[100,86196,126],{"class":118},[100,86198,16782],{"class":122},[100,86200,603],{"class":118},[100,86202,86203,86205,86207,86209,86211,86213,86215,86217],{"class":102,"line":135},[100,86204,372],{"class":114},[100,86206,170],{"class":118},[100,86208,43801],{"class":178},[100,86210,3166],{"class":118},[100,86212,206],{"class":205},[100,86214,2853],{"class":209},[100,86216,206],{"class":205},[100,86218,132],{"class":118},[14,86220,86221,86222,5870,86224,5894,86227,86229],{},"If your data uses names like ",[17,86223,42657],{},[17,86225,86226],{},"\"title\"",[17,86228,12456],{},", a dictionary may be the right structure.",[14,86231,86232,86233,290],{},"If you are seeing a missing-key problem instead, see ",[295,86234,17238],{"href":17237},[77,86236,86238],{"id":86237},"fix-4-check-for-accidental-tuple-or-list-indexing-mistakes","Fix 4: check for accidental tuple or list indexing mistakes",[14,86240,86241],{},"Nested data can be confusing at first. Make sure you know what each level contains.",[14,86243,86244],{},"This can work:",[91,86246,86248],{"className":93,"code":86247,"language":95,"meta":96,"style":96},"data = [{\"name\": \"Alice\"}, {\"name\": \"Bob\"}]\nprint(data[0][\"name\"])\n",[17,86249,86250,86295],{"__ignoreMap":96},[100,86251,86252,86254,86256,86259,86261,86263,86265,86267,86269,86271,86273,86276,86278,86280,86282,86284,86286,86288,86290,86292],{"class":102,"line":103},[100,86253,7909],{"class":106},[100,86255,111],{"class":110},[100,86257,86258],{"class":118}," [{",[100,86260,206],{"class":205},[100,86262,2853],{"class":209},[100,86264,206],{"class":205},[100,86266,89],{"class":118},[100,86268,1708],{"class":205},[100,86270,15343],{"class":209},[100,86272,206],{"class":205},[100,86274,86275],{"class":118},"},",[100,86277,12080],{"class":118},[100,86279,206],{"class":205},[100,86281,2853],{"class":209},[100,86283,206],{"class":205},[100,86285,89],{"class":118},[100,86287,1708],{"class":205},[100,86289,75318],{"class":209},[100,86291,206],{"class":205},[100,86293,86294],{"class":118},"}]\n",[100,86296,86297,86299,86301,86303,86305,86307,86310,86312,86314,86316],{"class":102,"line":135},[100,86298,372],{"class":114},[100,86300,170],{"class":118},[100,86302,7934],{"class":178},[100,86304,3166],{"class":118},[100,86306,2279],{"class":122},[100,86308,86309],{"class":118},"][",[100,86311,206],{"class":205},[100,86313,2853],{"class":209},[100,86315,206],{"class":205},[100,86317,132],{"class":118},[14,86319,86320],{},"Why it works:",[40,86322,86323,86327,86332],{},[43,86324,86325,4101],{},[17,86326,7934],{},[43,86328,86329,47377],{},[17,86330,86331],{},"data[0]",[43,86333,86334,86336],{},[17,86335,12456],{}," is a valid dictionary key",[14,86338,2932],{},[91,86340,86342],{"className":93,"code":86341,"language":95,"meta":96,"style":96},"data = [{\"name\": \"Alice\"}, {\"name\": \"Bob\"}]\nprint(data[\"name\"])\n",[17,86343,86344,86386],{"__ignoreMap":96},[100,86345,86346,86348,86350,86352,86354,86356,86358,86360,86362,86364,86366,86368,86370,86372,86374,86376,86378,86380,86382,86384],{"class":102,"line":103},[100,86347,7909],{"class":106},[100,86349,111],{"class":110},[100,86351,86258],{"class":118},[100,86353,206],{"class":205},[100,86355,2853],{"class":209},[100,86357,206],{"class":205},[100,86359,89],{"class":118},[100,86361,1708],{"class":205},[100,86363,15343],{"class":209},[100,86365,206],{"class":205},[100,86367,86275],{"class":118},[100,86369,12080],{"class":118},[100,86371,206],{"class":205},[100,86373,2853],{"class":209},[100,86375,206],{"class":205},[100,86377,89],{"class":118},[100,86379,1708],{"class":205},[100,86381,75318],{"class":209},[100,86383,206],{"class":205},[100,86385,86294],{"class":118},[100,86387,86388,86390,86392,86394,86396,86398,86400,86402],{"class":102,"line":135},[100,86389,372],{"class":114},[100,86391,170],{"class":118},[100,86393,7934],{"class":178},[100,86395,3166],{"class":118},[100,86397,206],{"class":205},[100,86399,2853],{"class":209},[100,86401,206],{"class":205},[100,86403,132],{"class":118},[14,86405,7194,86406,86408,86409,290],{},[17,86407,7934],{}," is a list, and lists cannot be indexed with the string ",[17,86410,12456],{},[14,86412,86413],{},"When working with nested data, check each step carefully.",[77,86415,77202],{"id":77201},[14,86417,86418],{},"When you see this error, inspect both:",[40,86420,86421,86424],{},[43,86422,86423],{},"the value used as the index",[43,86425,86426],{},"the object you are indexing",[14,86428,66053],{},[91,86430,86432],{"className":93,"code":86431,"language":95,"meta":96,"style":96},"print(index)\nprint(type(index))\nprint(my_list)\nprint(type(my_list))\nprint(len(my_list))\n",[17,86433,86434,86444,86458,86468,86482],{"__ignoreMap":96},[100,86435,86436,86438,86440,86442],{"class":102,"line":103},[100,86437,372],{"class":114},[100,86439,170],{"class":118},[100,86441,3169],{"class":178},[100,86443,215],{"class":118},[100,86445,86446,86448,86450,86452,86454,86456],{"class":102,"line":135},[100,86447,372],{"class":114},[100,86449,170],{"class":118},[100,86451,1250],{"class":191},[100,86453,170],{"class":118},[100,86455,3169],{"class":178},[100,86457,182],{"class":118},[100,86459,86460,86462,86464,86466],{"class":102,"line":142},[100,86461,372],{"class":114},[100,86463,170],{"class":118},[100,86465,7441],{"class":178},[100,86467,215],{"class":118},[100,86469,86470,86472,86474,86476,86478,86480],{"class":102,"line":152},[100,86471,372],{"class":114},[100,86473,170],{"class":118},[100,86475,1250],{"class":191},[100,86477,170],{"class":118},[100,86479,7441],{"class":178},[100,86481,182],{"class":118},[100,86483,86484,86486,86488,86490,86492,86494],{"class":102,"line":164},[100,86485,372],{"class":114},[100,86487,170],{"class":118},[100,86489,17779],{"class":114},[100,86491,170],{"class":118},[100,86493,7441],{"class":178},[100,86495,182],{"class":118},[14,86497,1844],{},[91,86499,86501],{"className":93,"code":86500,"language":95,"meta":96,"style":96},"my_list = [\"a\", \"b\", \"c\"]\nindex = \"1\"\n\nprint(index)\nprint(type(index))\nprint(my_list)\nprint(type(my_list))\nprint(len(my_list))\n\nprint(my_list[index])\n",[17,86502,86503,86535,86547,86551,86561,86575,86585,86599,86613,86617],{"__ignoreMap":96},[100,86504,86505,86507,86509,86511,86513,86515,86517,86519,86521,86523,86525,86527,86529,86531,86533],{"class":102,"line":103},[100,86506,7405],{"class":106},[100,86508,111],{"class":110},[100,86510,594],{"class":118},[100,86512,206],{"class":205},[100,86514,295],{"class":209},[100,86516,206],{"class":205},[100,86518,126],{"class":118},[100,86520,1708],{"class":205},[100,86522,4027],{"class":209},[100,86524,206],{"class":205},[100,86526,126],{"class":118},[100,86528,1708],{"class":205},[100,86530,4036],{"class":209},[100,86532,206],{"class":205},[100,86534,603],{"class":118},[100,86536,86537,86539,86541,86543,86545],{"class":102,"line":135},[100,86538,3116],{"class":106},[100,86540,111],{"class":110},[100,86542,1708],{"class":205},[100,86544,123],{"class":209},[100,86546,1714],{"class":205},[100,86548,86549],{"class":102,"line":142},[100,86550,139],{"emptyLinePlaceholder":138},[100,86552,86553,86555,86557,86559],{"class":102,"line":152},[100,86554,372],{"class":114},[100,86556,170],{"class":118},[100,86558,3169],{"class":178},[100,86560,215],{"class":118},[100,86562,86563,86565,86567,86569,86571,86573],{"class":102,"line":164},[100,86564,372],{"class":114},[100,86566,170],{"class":118},[100,86568,1250],{"class":191},[100,86570,170],{"class":118},[100,86572,3169],{"class":178},[100,86574,182],{"class":118},[100,86576,86577,86579,86581,86583],{"class":102,"line":185},[100,86578,372],{"class":114},[100,86580,170],{"class":118},[100,86582,7441],{"class":178},[100,86584,215],{"class":118},[100,86586,86587,86589,86591,86593,86595,86597],{"class":102,"line":197},[100,86588,372],{"class":114},[100,86590,170],{"class":118},[100,86592,1250],{"class":191},[100,86594,170],{"class":118},[100,86596,7441],{"class":178},[100,86598,182],{"class":118},[100,86600,86601,86603,86605,86607,86609,86611],{"class":102,"line":771},[100,86602,372],{"class":114},[100,86604,170],{"class":118},[100,86606,17779],{"class":114},[100,86608,170],{"class":118},[100,86610,7441],{"class":178},[100,86612,182],{"class":118},[100,86614,86615],{"class":102,"line":787},[100,86616,139],{"emptyLinePlaceholder":138},[100,86618,86619,86621,86623,86625,86627,86629],{"class":102,"line":5816},[100,86620,372],{"class":114},[100,86622,170],{"class":118},[100,86624,7441],{"class":178},[100,86626,3166],{"class":118},[100,86628,3169],{"class":178},[100,86630,132],{"class":118},[14,86632,38743],{},[91,86634,86636],{"className":93,"code":86635,"language":95,"meta":96,"style":96},"1\n\u003Cclass 'str'>\n['a', 'b', 'c']\n\u003Cclass 'list'>\n3\n",[17,86637,86638,86642,86656,86684,86698],{"__ignoreMap":96},[100,86639,86640],{"class":102,"line":103},[100,86641,228],{"class":122},[100,86643,86644,86646,86648,86650,86652,86654],{"class":102,"line":135},[100,86645,2782],{"class":110},[100,86647,3417],{"class":1077},[100,86649,1274],{"class":205},[100,86651,3422],{"class":209},[100,86653,1280],{"class":205},[100,86655,1980],{"class":110},[100,86657,86658,86660,86662,86664,86666,86668,86670,86672,86674,86676,86678,86680,86682],{"class":102,"line":142},[100,86659,3166],{"class":118},[100,86661,1280],{"class":205},[100,86663,295],{"class":209},[100,86665,1280],{"class":205},[100,86667,126],{"class":118},[100,86669,1274],{"class":205},[100,86671,4027],{"class":209},[100,86673,1280],{"class":205},[100,86675,126],{"class":118},[100,86677,1274],{"class":205},[100,86679,4036],{"class":209},[100,86681,1280],{"class":205},[100,86683,603],{"class":118},[100,86685,86686,86688,86690,86692,86694,86696],{"class":102,"line":152},[100,86687,2782],{"class":110},[100,86689,3417],{"class":1077},[100,86691,1274],{"class":205},[100,86693,1235],{"class":209},[100,86695,1280],{"class":205},[100,86697,1980],{"class":110},[100,86699,86700],{"class":102,"line":164},[100,86701,1500],{"class":122},[14,86703,86704],{},"This shows the problem clearly:",[40,86706,86707,86711,86716],{},[43,86708,86709,4101],{},[17,86710,7441],{},[43,86712,86713,86715],{},[17,86714,3169],{}," is a string",[43,86717,86718],{},"a string cannot be used as a list index",[14,86720,86721,86722,86724,86725,290],{},"If you want to understand ",[17,86723,4430],{}," better, see ",[295,86726,1572,86727,9570],{"href":4427},[17,86728,4430],{},[77,86730,86732],{"id":86731},"what-to-remember","What to remember",[40,86734,86735,86738,86744,86751],{},[43,86736,86737],{},"Lists need integer positions or slices",[43,86739,86740,86741,86743],{},"Strings from ",[17,86742,14906],{}," usually need conversion before being used as indices",[43,86745,86746,86747,86750],{},"Floats like ",[17,86748,86749],{},"1.0"," are not valid list indices",[43,86752,86753],{},"Use dictionaries when you need named keys instead of numeric positions",[77,86755,7117],{"id":7116},[14,86757,86758],{},"Here are the most common causes of this error:",[40,86760,86761,86768,86777,86780,86783,86789],{},[43,86762,1357,86763,86765,86766],{},[17,86764,14906],{}," directly as a list index without ",[17,86767,6119],{},[43,86769,86770,86771,86774,86775],{},"Using a string like ",[17,86772,86773],{},"'0'"," instead of the integer ",[17,86776,2279],{},[43,86778,86779],{},"Looping through list values and then using those values as indices",[43,86781,86782],{},"Confusing lists with dictionaries",[43,86784,86785,86786,86788],{},"Using a float such as ",[17,86787,86749],{}," as an index",[43,86790,86791],{},"Indexing the wrong level of nested data",[77,86793,1514],{"id":1513},[675,86795,9137,86797,86799],{"id":86796},"why-does-input-cause-this-error",[17,86798,14906],{}," cause this error?",[14,86801,14478,86802,86804],{},[17,86803,14906],{}," returns a string, and list indices must be integers or slices.",[675,86806,86808],{"id":86807},"can-i-use-a-float-as-a-list-index","Can I use a float as a list index?",[14,86810,86811,86812,86814],{},"No. Even ",[17,86813,86749],{}," is not valid. Convert it to an integer first if appropriate.",[675,86816,86818],{"id":86817},"what-is-a-slice-in-a-list","What is a slice in a list?",[14,86820,86821,86822,290],{},"A slice selects a range of items, such as ",[17,86823,86824],{},"my_list[1:4]",[675,86826,86828],{"id":86827},"when-should-i-use-a-dictionary-instead-of-a-list","When should I use a dictionary instead of a list?",[14,86830,86831,86832,3178,86834,86836],{},"Use a dictionary when you want to access values by names or keys like ",[17,86833,12456],{},[17,86835,42657],{}," instead of numeric positions.",[77,86838,1554],{"id":1553},[40,86840,86841,86845,86851,86857,86862,86866],{},[43,86842,86843],{},[295,86844,35359],{"href":8110},[43,86846,86847],{},[295,86848,1572,86849,1576],{"href":81194},[17,86850,6119],{},[43,86852,86853],{},[295,86854,1572,86855,1576],{"href":85586},[17,86856,14906],{},[43,86858,86859],{},[295,86860,86861],{"href":85784},"How to convert user input to numbers in Python",[43,86863,86864],{},[295,86865,37665],{"href":34297},[43,86867,86868],{},[295,86869,17232],{"href":17231},[1589,86871,86872],{},"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 .sP7_E, html code.shiki .sP7_E{--shiki-light:#39ADB5;--shiki-default:#24292E;--shiki-dark:#E1E4E8}html pre.shiki code .srdBf, html code.shiki .srdBf{--shiki-light:#F76D47;--shiki-default:#005CC5;--shiki-dark:#79B8FF}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 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 .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 .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 .sbsja, html code.shiki .sbsja{--shiki-light:#9C3EDA;--shiki-default:#D73A49;--shiki-dark:#F97583}",{"title":96,"searchDepth":135,"depth":135,"links":86874},[86875,86876,86877,86878,86879,86880,86881,86882,86883,86884,86885,86892],{"id":79,"depth":135,"text":80},{"id":5880,"depth":135,"text":5881},{"id":24957,"depth":135,"text":24958},{"id":85677,"depth":135,"text":85678},{"id":85788,"depth":135,"text":85789},{"id":86094,"depth":135,"text":86095},{"id":86237,"depth":135,"text":86238},{"id":77201,"depth":135,"text":77202},{"id":86731,"depth":135,"text":86732},{"id":7116,"depth":135,"text":7117},{"id":1513,"depth":135,"text":1514,"children":86886},[86887,86889,86890,86891],{"id":86796,"depth":142,"text":86888},"Why does input() cause this error?",{"id":86807,"depth":142,"text":86808},{"id":86817,"depth":142,"text":86818},{"id":86827,"depth":142,"text":86828},{"id":1553,"depth":135,"text":1554},"Master typeerror list indices must be integers or slices fix in our comprehensive Python beginner guide.",{},{"title":85165,"description":86893},"errors\u002Ftypeerror-list-indices-must-be-integers-or-slices-fix","xMAqlkEc83stiKPh6T63xx9NK2lLufVp-aAjAEKQNh4",{"id":86899,"title":86900,"body":86901,"description":88323,"extension":1623,"meta":88324,"navigation":138,"path":88325,"seo":88326,"stem":88327,"__hash__":88328},"content\u002Ferrors\u002Ftypeerror-list-object-is-not-callable-fix.md","TypeError: 'list' object is not callable (Fix)",{"type":7,"value":86902,"toc":88303},[86903,86908,86916,86918,86969,86977,86979,86985,86988,87009,87011,87050,87052,87076,87082,87085,87124,87126,87134,87140,87143,87146,87164,87166,87217,87219,87270,87272,87280,87288,87294,87299,87308,87310,87353,87355,87379,87381,87405,87407,87450,87452,87484,87487,87505,87513,87517,87520,87526,87528,87598,87600,87624,87626,87646,87648,87731,87733,87758,87761,87763,87766,87786,87789,87863,87866,87909,87912,87914,87916,87944,87946,88023,88025,88110,88113,88139,88144,88148,88151,88183,88186,88188,88190,88212,88220,88222,88226,88232,88236,88241,88249,88258,88262,88265,88267,88300],[10,86904,74872,86906,7375],{"id":86905},"typeerror-list-object-is-not-callable-fix",[17,86907,80715],{},[14,86909,31738,86910,86915],{},[22,86911,86912],{},[17,86913,86914],{},"TypeError: 'list' object is not callable",". This page shows what the error means, the most common causes, and simple ways to correct your code.",[77,86917,80],{"id":79},[91,86919,86921],{"className":93,"code":86920,"language":95,"meta":96,"style":96},"numbers = [1, 2, 3]\nprint(numbers[0])   # correct: use square brackets for indexing\n\n# not numbers(0)\n",[17,86922,86923,86943,86960,86964],{"__ignoreMap":96},[100,86924,86925,86927,86929,86931,86933,86935,86937,86939,86941],{"class":102,"line":103},[100,86926,107],{"class":106},[100,86928,111],{"class":110},[100,86930,594],{"class":118},[100,86932,123],{"class":122},[100,86934,126],{"class":118},[100,86936,129],{"class":122},[100,86938,126],{"class":118},[100,86940,1405],{"class":122},[100,86942,603],{"class":118},[100,86944,86945,86947,86949,86951,86953,86955,86957],{"class":102,"line":135},[100,86946,372],{"class":114},[100,86948,170],{"class":118},[100,86950,179],{"class":178},[100,86952,3166],{"class":118},[100,86954,2279],{"class":122},[100,86956,32896],{"class":118},[100,86958,86959],{"class":414},"   # correct: use square brackets for indexing\n",[100,86961,86962],{"class":102,"line":142},[100,86963,139],{"emptyLinePlaceholder":138},[100,86965,86966],{"class":102,"line":152},[100,86967,86968],{"class":414},"# not numbers(0)\n",[14,86970,86971,86972,86974,86975,290],{},"This error usually happens when you use round brackets ",[17,86973,7562],{}," on a list, or when you overwrite a built-in function name like ",[17,86976,1235],{},[77,86978,5881],{"id":5880},[14,86980,86981,86982,290],{},"Python is trying to ",[22,86983,86984],{},"call a list like a function",[14,86986,86987],{},"A list and a function use different syntax:",[40,86989,86990,86996,87002],{},[43,86991,86992,86993,86995],{},"Lists use square brackets ",[17,86994,45218],{}," for indexing",[43,86997,86998,86999,87001],{},"Functions use round brackets ",[17,87000,7562],{}," for calling",[43,87003,87004,87005,87008],{},"If a variable holds a list, writing ",[17,87006,87007],{},"name()"," causes this error",[14,87010,580],{},[91,87012,87014],{"className":93,"code":87013,"language":95,"meta":96,"style":96},"numbers = [10, 20, 30]\nprint(numbers(0))\n",[17,87015,87016,87036],{"__ignoreMap":96},[100,87017,87018,87020,87022,87024,87026,87028,87030,87032,87034],{"class":102,"line":103},[100,87019,107],{"class":106},[100,87021,111],{"class":110},[100,87023,594],{"class":118},[100,87025,356],{"class":122},[100,87027,126],{"class":118},[100,87029,361],{"class":122},[100,87031,126],{"class":118},[100,87033,16342],{"class":122},[100,87035,603],{"class":118},[100,87037,87038,87040,87042,87044,87046,87048],{"class":102,"line":135},[100,87039,372],{"class":114},[100,87041,170],{"class":118},[100,87043,179],{"class":178},[100,87045,170],{"class":118},[100,87047,2279],{"class":122},[100,87049,182],{"class":118},[14,87051,218],{},[91,87053,87054],{"className":93,"code":16862,"language":95,"meta":96,"style":96},[17,87055,87056],{"__ignoreMap":96},[100,87057,87058,87060,87062,87064,87066,87068,87070,87072,87074],{"class":102,"line":103},[100,87059,3538],{"class":191},[100,87061,89],{"class":118},[100,87063,1274],{"class":205},[100,87065,1235],{"class":209},[100,87067,1280],{"class":205},[100,87069,3914],{"class":191},[100,87071,16702],{"class":110},[100,87073,4921],{"class":110},[100,87075,16885],{"class":114},[14,87077,87078,87079,87081],{},"Python raises the error because ",[17,87080,179],{}," is a list, not a function.",[14,87083,87084],{},"If you want the first item, use:",[91,87086,87088],{"className":93,"code":87087,"language":95,"meta":96,"style":96},"numbers = [10, 20, 30]\nprint(numbers[0])\n",[17,87089,87090,87110],{"__ignoreMap":96},[100,87091,87092,87094,87096,87098,87100,87102,87104,87106,87108],{"class":102,"line":103},[100,87093,107],{"class":106},[100,87095,111],{"class":110},[100,87097,594],{"class":118},[100,87099,356],{"class":122},[100,87101,126],{"class":118},[100,87103,361],{"class":122},[100,87105,126],{"class":118},[100,87107,16342],{"class":122},[100,87109,603],{"class":118},[100,87111,87112,87114,87116,87118,87120,87122],{"class":102,"line":135},[100,87113,372],{"class":114},[100,87115,170],{"class":118},[100,87117,179],{"class":178},[100,87119,3166],{"class":118},[100,87121,2279],{"class":122},[100,87123,132],{"class":118},[14,87125,218],{},[91,87127,87128],{"className":93,"code":3410,"language":95,"meta":96,"style":96},[17,87129,87130],{"__ignoreMap":96},[100,87131,87132],{"class":102,"line":103},[100,87133,3410],{"class":122},[77,87135,79704,87136,15957,87138],{"id":79703},[17,87137,7562],{},[17,87139,45218],{},[14,87141,87142],{},"This is the most common beginner mistake.",[14,87144,87145],{},"Use square brackets to get an item from a list:",[40,87147,87148,87153,87158],{},[43,87149,241,87150,87152],{},[17,87151,36138],{}," to get the first item",[43,87154,241,87155,87157],{},[17,87156,85326],{}," to get the second item",[43,87159,87160,87163],{},[17,87161,87162],{},"my_list(0)"," is invalid because the list is not a function",[14,87165,9709],{},[91,87167,87169],{"className":93,"code":87168,"language":95,"meta":96,"style":96},"fruits = [\"apple\", \"banana\", \"cherry\"]\nprint(fruits(1))\n",[17,87170,87171,87203],{"__ignoreMap":96},[100,87172,87173,87175,87177,87179,87181,87183,87185,87187,87189,87191,87193,87195,87197,87199,87201],{"class":102,"line":103},[100,87174,8762],{"class":106},[100,87176,111],{"class":110},[100,87178,594],{"class":118},[100,87180,206],{"class":205},[100,87182,4148],{"class":209},[100,87184,206],{"class":205},[100,87186,126],{"class":118},[100,87188,1708],{"class":205},[100,87190,4157],{"class":209},[100,87192,206],{"class":205},[100,87194,126],{"class":118},[100,87196,1708],{"class":205},[100,87198,8949],{"class":209},[100,87200,206],{"class":205},[100,87202,603],{"class":118},[100,87204,87205,87207,87209,87211,87213,87215],{"class":102,"line":135},[100,87206,372],{"class":114},[100,87208,170],{"class":118},[100,87210,8802],{"class":178},[100,87212,170],{"class":118},[100,87214,123],{"class":122},[100,87216,182],{"class":118},[14,87218,9750],{},[91,87220,87222],{"className":93,"code":87221,"language":95,"meta":96,"style":96},"fruits = [\"apple\", \"banana\", \"cherry\"]\nprint(fruits[1])\n",[17,87223,87224,87256],{"__ignoreMap":96},[100,87225,87226,87228,87230,87232,87234,87236,87238,87240,87242,87244,87246,87248,87250,87252,87254],{"class":102,"line":103},[100,87227,8762],{"class":106},[100,87229,111],{"class":110},[100,87231,594],{"class":118},[100,87233,206],{"class":205},[100,87235,4148],{"class":209},[100,87237,206],{"class":205},[100,87239,126],{"class":118},[100,87241,1708],{"class":205},[100,87243,4157],{"class":209},[100,87245,206],{"class":205},[100,87247,126],{"class":118},[100,87249,1708],{"class":205},[100,87251,8949],{"class":209},[100,87253,206],{"class":205},[100,87255,603],{"class":118},[100,87257,87258,87260,87262,87264,87266,87268],{"class":102,"line":135},[100,87259,372],{"class":114},[100,87261,170],{"class":118},[100,87263,8802],{"class":178},[100,87265,3166],{"class":118},[100,87267,123],{"class":122},[100,87269,132],{"class":118},[14,87271,218],{},[91,87273,87274],{"className":93,"code":8817,"language":95,"meta":96,"style":96},[17,87275,87276],{"__ignoreMap":96},[100,87277,87278],{"class":102,"line":103},[100,87279,8817],{"class":106},[14,87281,8107,87282,3266,87284,290],{},[295,87283,35359],{"href":8110},[295,87285,87287],{"href":87286},"\u002Freference\u002Fpython-list-creating-a-list\u002F","how to create a list in Python",[77,87289,87291,87292],{"id":87290},"common-cause-naming-a-variable-list","Common cause: naming a variable ",[17,87293,1235],{},[14,87295,1740,87296,87298],{},[17,87297,1235],{}," as a variable name.",[14,87300,87301,87302,87304,87305,87307],{},"Python already has a built-in function called ",[17,87303,76220],{},". If you assign a list to the name ",[17,87306,1235],{},", you replace that built-in function in your current scope.",[14,87309,9709],{},[91,87311,87313],{"className":93,"code":87312,"language":95,"meta":96,"style":96},"list = [1, 2, 3]\nprint(list(\"abc\"))\n",[17,87314,87315,87335],{"__ignoreMap":96},[100,87316,87317,87319,87321,87323,87325,87327,87329,87331,87333],{"class":102,"line":103},[100,87318,1235],{"class":191},[100,87320,47743],{"class":110},[100,87322,594],{"class":118},[100,87324,123],{"class":122},[100,87326,126],{"class":118},[100,87328,129],{"class":122},[100,87330,126],{"class":118},[100,87332,1405],{"class":122},[100,87334,603],{"class":118},[100,87336,87337,87339,87341,87343,87345,87347,87349,87351],{"class":102,"line":135},[100,87338,372],{"class":114},[100,87340,170],{"class":118},[100,87342,1235],{"class":191},[100,87344,170],{"class":118},[100,87346,206],{"class":205},[100,87348,83226],{"class":209},[100,87350,206],{"class":205},[100,87352,182],{"class":118},[14,87354,218],{},[91,87356,87357],{"className":93,"code":16862,"language":95,"meta":96,"style":96},[17,87358,87359],{"__ignoreMap":96},[100,87360,87361,87363,87365,87367,87369,87371,87373,87375,87377],{"class":102,"line":103},[100,87362,3538],{"class":191},[100,87364,89],{"class":118},[100,87366,1274],{"class":205},[100,87368,1235],{"class":209},[100,87370,1280],{"class":205},[100,87372,3914],{"class":191},[100,87374,16702],{"class":110},[100,87376,4921],{"class":110},[100,87378,16885],{"class":114},[14,87380,4094],{},[40,87382,87383,87391,87399],{},[43,87384,87385,87388,87389],{},[17,87386,87387],{},"list = [1, 2, 3]"," stores a list in the name ",[17,87390,1235],{},[43,87392,87393,87394,87396,87397,6354],{},"After that, ",[17,87395,1235],{}," no longer refers to the built-in ",[17,87398,76220],{},[43,87400,35458,87401,87404],{},[17,87402,87403],{},"list(\"abc\")"," tries to call a list object",[14,87406,9750],{},[91,87408,87410],{"className":93,"code":87409,"language":95,"meta":96,"style":96},"items = [1, 2, 3]\nprint(list(\"abc\"))\n",[17,87411,87412,87432],{"__ignoreMap":96},[100,87413,87414,87416,87418,87420,87422,87424,87426,87428,87430],{"class":102,"line":103},[100,87415,3095],{"class":106},[100,87417,111],{"class":110},[100,87419,594],{"class":118},[100,87421,123],{"class":122},[100,87423,126],{"class":118},[100,87425,129],{"class":122},[100,87427,126],{"class":118},[100,87429,1405],{"class":122},[100,87431,603],{"class":118},[100,87433,87434,87436,87438,87440,87442,87444,87446,87448],{"class":102,"line":135},[100,87435,372],{"class":114},[100,87437,170],{"class":118},[100,87439,1235],{"class":191},[100,87441,170],{"class":118},[100,87443,206],{"class":205},[100,87445,83226],{"class":209},[100,87447,206],{"class":205},[100,87449,182],{"class":118},[14,87451,218],{},[91,87453,87454],{"className":93,"code":37293,"language":95,"meta":96,"style":96},[17,87455,87456],{"__ignoreMap":96},[100,87457,87458,87460,87462,87464,87466,87468,87470,87472,87474,87476,87478,87480,87482],{"class":102,"line":103},[100,87459,3166],{"class":118},[100,87461,1280],{"class":205},[100,87463,295],{"class":209},[100,87465,1280],{"class":205},[100,87467,126],{"class":118},[100,87469,1274],{"class":205},[100,87471,4027],{"class":209},[100,87473,1280],{"class":205},[100,87475,126],{"class":118},[100,87477,1274],{"class":205},[100,87479,4036],{"class":209},[100,87481,1280],{"class":205},[100,87483,603],{"class":118},[14,87485,87486],{},"Better variable names:",[40,87488,87489,87493,87497,87501],{},[43,87490,87491],{},[17,87492,3146],{},[43,87494,87495],{},[17,87496,179],{},[43,87498,87499],{},[17,87500,4049],{},[43,87502,87503],{},[17,87504,7820],{},[14,87506,87507,87508,87512],{},"You can learn more about checking object types on the ",[295,87509,1572,87510,1576],{"href":4427},[17,87511,4430],{}," page.",[77,87514,87516],{"id":87515},"common-cause-reusing-a-function-name-for-a-list","Common cause: reusing a function name for a list",[14,87518,87519],{},"You may create a function, then later assign a list to the same name.",[14,87521,87522,87523,87525],{},"After that, calling the name with ",[17,87524,7562],{}," fails.",[14,87527,9709],{},[91,87529,87531],{"className":93,"code":87530,"language":95,"meta":96,"style":96},"def get_numbers():\n    return [1, 2, 3]\n\nget_numbers = [10, 20, 30]\n\nprint(get_numbers())\n",[17,87532,87533,87541,87559,87563,87584,87588],{"__ignoreMap":96},[100,87534,87535,87537,87539],{"class":102,"line":103},[100,87536,1078],{"class":1077},[100,87538,82235],{"class":1081},[100,87540,1085],{"class":118},[100,87542,87543,87545,87547,87549,87551,87553,87555,87557],{"class":102,"line":135},[100,87544,2552],{"class":145},[100,87546,594],{"class":118},[100,87548,123],{"class":122},[100,87550,126],{"class":118},[100,87552,129],{"class":122},[100,87554,126],{"class":118},[100,87556,1405],{"class":122},[100,87558,603],{"class":118},[100,87560,87561],{"class":102,"line":142},[100,87562,139],{"emptyLinePlaceholder":138},[100,87564,87565,87568,87570,87572,87574,87576,87578,87580,87582],{"class":102,"line":152},[100,87566,87567],{"class":106},"get_numbers ",[100,87569,111],{"class":110},[100,87571,594],{"class":118},[100,87573,356],{"class":122},[100,87575,126],{"class":118},[100,87577,361],{"class":122},[100,87579,126],{"class":118},[100,87581,16342],{"class":122},[100,87583,603],{"class":118},[100,87585,87586],{"class":102,"line":164},[100,87587,139],{"emptyLinePlaceholder":138},[100,87589,87590,87592,87594,87596],{"class":102,"line":185},[100,87591,372],{"class":114},[100,87593,170],{"class":118},[100,87595,82473],{"class":178},[100,87597,3370],{"class":118},[14,87599,218],{},[91,87601,87602],{"className":93,"code":16862,"language":95,"meta":96,"style":96},[17,87603,87604],{"__ignoreMap":96},[100,87605,87606,87608,87610,87612,87614,87616,87618,87620,87622],{"class":102,"line":103},[100,87607,3538],{"class":191},[100,87609,89],{"class":118},[100,87611,1274],{"class":205},[100,87613,1235],{"class":209},[100,87615,1280],{"class":205},[100,87617,3914],{"class":191},[100,87619,16702],{"class":110},[100,87621,4921],{"class":110},[100,87623,16885],{"class":114},[14,87625,4094],{},[40,87627,87628,87633,87640],{},[43,87629,87630,87631,83621],{},"At first, ",[17,87632,82473],{},[43,87634,87635,87636,87639],{},"Later, ",[17,87637,87638],{},"get_numbers = [10, 20, 30]"," replaces that function with a list",[43,87641,87642,87643,87645],{},"Then ",[17,87644,82479],{}," tries to call the list",[14,87647,9750],{},[91,87649,87651],{"className":93,"code":87650,"language":95,"meta":96,"style":96},"def get_numbers():\n    return [1, 2, 3]\n\nnumbers = [10, 20, 30]\n\nprint(get_numbers())\nprint(numbers[0])\n",[17,87652,87653,87661,87679,87683,87703,87707,87717],{"__ignoreMap":96},[100,87654,87655,87657,87659],{"class":102,"line":103},[100,87656,1078],{"class":1077},[100,87658,82235],{"class":1081},[100,87660,1085],{"class":118},[100,87662,87663,87665,87667,87669,87671,87673,87675,87677],{"class":102,"line":135},[100,87664,2552],{"class":145},[100,87666,594],{"class":118},[100,87668,123],{"class":122},[100,87670,126],{"class":118},[100,87672,129],{"class":122},[100,87674,126],{"class":118},[100,87676,1405],{"class":122},[100,87678,603],{"class":118},[100,87680,87681],{"class":102,"line":142},[100,87682,139],{"emptyLinePlaceholder":138},[100,87684,87685,87687,87689,87691,87693,87695,87697,87699,87701],{"class":102,"line":152},[100,87686,107],{"class":106},[100,87688,111],{"class":110},[100,87690,594],{"class":118},[100,87692,356],{"class":122},[100,87694,126],{"class":118},[100,87696,361],{"class":122},[100,87698,126],{"class":118},[100,87700,16342],{"class":122},[100,87702,603],{"class":118},[100,87704,87705],{"class":102,"line":164},[100,87706,139],{"emptyLinePlaceholder":138},[100,87708,87709,87711,87713,87715],{"class":102,"line":185},[100,87710,372],{"class":114},[100,87712,170],{"class":118},[100,87714,82473],{"class":178},[100,87716,3370],{"class":118},[100,87718,87719,87721,87723,87725,87727,87729],{"class":102,"line":197},[100,87720,372],{"class":114},[100,87722,170],{"class":118},[100,87724,179],{"class":178},[100,87726,3166],{"class":118},[100,87728,2279],{"class":122},[100,87730,132],{"class":118},[14,87732,218],{},[91,87734,87736],{"className":93,"code":87735,"language":95,"meta":96,"style":96},"[1, 2, 3]\n10\n",[17,87737,87738,87754],{"__ignoreMap":96},[100,87739,87740,87742,87744,87746,87748,87750,87752],{"class":102,"line":103},[100,87741,3166],{"class":118},[100,87743,123],{"class":122},[100,87745,126],{"class":118},[100,87747,129],{"class":122},[100,87749,126],{"class":118},[100,87751,1405],{"class":122},[100,87753,603],{"class":118},[100,87755,87756],{"class":102,"line":135},[100,87757,3410],{"class":122},[14,87759,87760],{},"If the error started after you renamed or reassigned something, check the lines above the traceback carefully.",[77,87762,6244],{"id":6243},[14,87764,87765],{},"Use the fix that matches your situation:",[40,87767,87768,87775,87780,87783],{},[43,87769,52216,87770,42154,87772,87774],{},[17,87771,7562],{},[17,87773,45218],{}," when accessing list items",[43,87776,87777,87778],{},"Rename variables that shadow built-in names like ",[17,87779,1235],{},[43,87781,87782],{},"Rename variables that reuse function names",[43,87784,87785],{},"Restart your interpreter or notebook if an old bad assignment still exists",[14,87787,87788],{},"Example fix for indexing:",[91,87790,87792],{"className":93,"code":87791,"language":95,"meta":96,"style":96},"letters = [\"a\", \"b\", \"c\"]\n\n# wrong:\n# print(letters(0))\n\n# correct:\nprint(letters[0])\n",[17,87793,87794,87826,87830,87835,87840,87844,87849],{"__ignoreMap":96},[100,87795,87796,87798,87800,87802,87804,87806,87808,87810,87812,87814,87816,87818,87820,87822,87824],{"class":102,"line":103},[100,87797,7960],{"class":106},[100,87799,111],{"class":110},[100,87801,594],{"class":118},[100,87803,206],{"class":205},[100,87805,295],{"class":209},[100,87807,206],{"class":205},[100,87809,126],{"class":118},[100,87811,1708],{"class":205},[100,87813,4027],{"class":209},[100,87815,206],{"class":205},[100,87817,126],{"class":118},[100,87819,1708],{"class":205},[100,87821,4036],{"class":209},[100,87823,206],{"class":205},[100,87825,603],{"class":118},[100,87827,87828],{"class":102,"line":135},[100,87829,139],{"emptyLinePlaceholder":138},[100,87831,87832],{"class":102,"line":142},[100,87833,87834],{"class":414},"# wrong:\n",[100,87836,87837],{"class":102,"line":152},[100,87838,87839],{"class":414},"# print(letters(0))\n",[100,87841,87842],{"class":102,"line":164},[100,87843,139],{"emptyLinePlaceholder":138},[100,87845,87846],{"class":102,"line":185},[100,87847,87848],{"class":414},"# correct:\n",[100,87850,87851,87853,87855,87857,87859,87861],{"class":102,"line":197},[100,87852,372],{"class":114},[100,87854,170],{"class":118},[100,87856,7997],{"class":178},[100,87858,3166],{"class":118},[100,87860,2279],{"class":122},[100,87862,132],{"class":118},[14,87864,87865],{},"Example fix for a shadowed built-in:",[91,87867,87869],{"className":93,"code":87868,"language":95,"meta":96,"style":96},"values = [1, 2, 3]\nprint(list(\"hi\"))\n",[17,87870,87871,87891],{"__ignoreMap":96},[100,87872,87873,87875,87877,87879,87881,87883,87885,87887,87889],{"class":102,"line":103},[100,87874,4010],{"class":106},[100,87876,111],{"class":110},[100,87878,594],{"class":118},[100,87880,123],{"class":122},[100,87882,126],{"class":118},[100,87884,129],{"class":122},[100,87886,126],{"class":118},[100,87888,1405],{"class":122},[100,87890,603],{"class":118},[100,87892,87893,87895,87897,87899,87901,87903,87905,87907],{"class":102,"line":135},[100,87894,372],{"class":114},[100,87896,170],{"class":118},[100,87898,1235],{"class":191},[100,87900,170],{"class":118},[100,87902,206],{"class":205},[100,87904,79381],{"class":209},[100,87906,206],{"class":205},[100,87908,182],{"class":118},[14,87910,87911],{},"If you are working in Jupyter, IPython, or a Python shell, restarting can help after you fix the code. Old variable assignments may still be stored in memory.",[77,87913,12157],{"id":12156},[14,87915,83361],{},[3282,87917,87918,87921,87926,87929,87935],{},[43,87919,87920],{},"Read the exact line named in the traceback",[43,87922,87923,87924],{},"Look for a name followed by ",[17,87925,7562],{},[43,87927,87928],{},"Check what that name currently stores",[43,87930,241,87931,87934],{},[17,87932,87933],{},"print(type(name))"," if you are unsure",[43,87936,87937,87938,87941,87942],{},"If the result is ",[17,87939,87940],{},"\u003Cclass 'list'>",", do not call it with ",[17,87943,7562],{},[14,87945,5295],{},[91,87947,87949],{"className":93,"code":87948,"language":95,"meta":96,"style":96},"my_list = [1, 2, 3]\n\nprint(type(my_list))\nprint(my_list)\nprint(type(list))\nprint(dir())\n",[17,87950,87951,87971,87975,87989,87999,88013],{"__ignoreMap":96},[100,87952,87953,87955,87957,87959,87961,87963,87965,87967,87969],{"class":102,"line":103},[100,87954,7405],{"class":106},[100,87956,111],{"class":110},[100,87958,594],{"class":118},[100,87960,123],{"class":122},[100,87962,126],{"class":118},[100,87964,129],{"class":122},[100,87966,126],{"class":118},[100,87968,1405],{"class":122},[100,87970,603],{"class":118},[100,87972,87973],{"class":102,"line":135},[100,87974,139],{"emptyLinePlaceholder":138},[100,87976,87977,87979,87981,87983,87985,87987],{"class":102,"line":142},[100,87978,372],{"class":114},[100,87980,170],{"class":118},[100,87982,1250],{"class":191},[100,87984,170],{"class":118},[100,87986,7441],{"class":178},[100,87988,182],{"class":118},[100,87990,87991,87993,87995,87997],{"class":102,"line":152},[100,87992,372],{"class":114},[100,87994,170],{"class":118},[100,87996,7441],{"class":178},[100,87998,215],{"class":118},[100,88000,88001,88003,88005,88007,88009,88011],{"class":102,"line":164},[100,88002,372],{"class":114},[100,88004,170],{"class":118},[100,88006,1250],{"class":191},[100,88008,170],{"class":118},[100,88010,1235],{"class":191},[100,88012,182],{"class":118},[100,88014,88015,88017,88019,88021],{"class":102,"line":185},[100,88016,372],{"class":114},[100,88018,170],{"class":118},[100,88020,3822],{"class":114},[100,88022,3370],{"class":118},[14,88024,52521],{},[91,88026,88028],{"className":93,"code":88027,"language":95,"meta":96,"style":96},"\u003Cclass 'list'>\n[1, 2, 3]\n\u003Cclass 'type'>\n['__annotations__', '__builtins__', '__doc__', ...]\n",[17,88029,88030,88044,88060,88074],{"__ignoreMap":96},[100,88031,88032,88034,88036,88038,88040,88042],{"class":102,"line":103},[100,88033,2782],{"class":110},[100,88035,3417],{"class":1077},[100,88037,1274],{"class":205},[100,88039,1235],{"class":209},[100,88041,1280],{"class":205},[100,88043,1980],{"class":110},[100,88045,88046,88048,88050,88052,88054,88056,88058],{"class":102,"line":135},[100,88047,3166],{"class":118},[100,88049,123],{"class":122},[100,88051,126],{"class":118},[100,88053,129],{"class":122},[100,88055,126],{"class":118},[100,88057,1405],{"class":122},[100,88059,603],{"class":118},[100,88061,88062,88064,88066,88068,88070,88072],{"class":102,"line":142},[100,88063,2782],{"class":110},[100,88065,3417],{"class":1077},[100,88067,1274],{"class":205},[100,88069,1250],{"class":209},[100,88071,1280],{"class":205},[100,88073,1980],{"class":110},[100,88075,88076,88078,88080,88083,88085,88087,88089,88092,88094,88096,88098,88101,88103,88105,88108],{"class":102,"line":152},[100,88077,3166],{"class":118},[100,88079,1280],{"class":205},[100,88081,88082],{"class":209},"__annotations__",[100,88084,1280],{"class":205},[100,88086,126],{"class":118},[100,88088,1274],{"class":205},[100,88090,88091],{"class":209},"__builtins__",[100,88093,1280],{"class":205},[100,88095,126],{"class":118},[100,88097,1274],{"class":205},[100,88099,88100],{"class":209},"__doc__",[100,88102,1280],{"class":205},[100,88104,126],{"class":118},[100,88106,88107],{"class":527}," ...",[100,88109,603],{"class":118},[14,88111,88112],{},"What these commands help you find:",[40,88114,88115,88120,88125,88134],{},[43,88116,88117,88119],{},[17,88118,8691],{}," shows whether a variable is a list",[43,88121,88122,88124],{},[17,88123,8686],{}," shows the actual value",[43,88126,88127,88130,88131,88133],{},[17,88128,88129],{},"print(type(list))"," helps you check whether ",[17,88132,1235],{}," still refers to the built-in",[43,88135,88136,88138],{},[17,88137,4565],{}," shows names currently defined in your scope",[14,88140,88141,88142,290],{},"If you want more help reading tracebacks and checking variables, see this ",[295,88143,7077],{"href":3852},[77,88145,88147],{"id":88146},"how-to-avoid-this-error","How to avoid this error",[14,88149,88150],{},"A few habits can prevent this problem:",[40,88152,88153,88160,88171,88180],{},[43,88154,88155,88156,3178,88158],{},"Use clear variable names like ",[17,88157,7441],{},[17,88159,179],{},[43,88161,88162,88163,5870,88165,5870,88167,5894,88169],{},"Do not name variables ",[17,88164,1235],{},[17,88166,3422],{},[17,88168,16046],{},[17,88170,75244],{},[43,88172,88173,88174,88176,88177,88179],{},"Remember: ",[17,88175,45218],{}," accesses items, ",[17,88178,7562],{}," calls functions",[43,88181,88182],{},"Test small pieces of code as you write them",[14,88184,88185],{},"This is especially important in notebooks and interactive shells, where old assignments can stay around longer than you expect.",[77,88187,7117],{"id":7116},[14,88189,74691],{},[40,88191,88192,88198,88206,88209],{},[43,88193,1357,88194,15957,88196],{},[17,88195,87162],{},[17,88197,36138],{},[43,88199,88200,88201,88203,88204],{},"Assigning a list to the name ",[17,88202,1235],{}," and then calling ",[17,88205,76220],{},[43,88207,88208],{},"Overwriting a function name with a list",[43,88210,88211],{},"Keeping an old variable assignment in a notebook or interactive shell",[14,88213,88214,88215,290],{},"A related error can happen with dictionaries too. See ",[295,88216,74872,88218],{"href":88217},"\u002Ferrors\u002Ftypeerror-dict-object-is-not-callable-fix\u002F",[17,88219,79515],{},[77,88221,1514],{"id":1513},[675,88223,88225],{"id":88224},"why-does-python-say-a-list-is-not-callable","Why does Python say a list is not callable?",[14,88227,88228,88229,88231],{},"Because only functions and other callable objects can be used with ",[17,88230,7562],{},". A list cannot be called like a function.",[675,88233,88235],{"id":88234},"how-do-i-get-an-item-from-a-list-correctly","How do I get an item from a list correctly?",[14,88237,88238,88239,290],{},"Use square brackets with an index, like ",[17,88240,36138],{},[675,88242,88244,88245,88248],{"id":88243},"why-did-listabc-stop-working","Why did ",[17,88246,88247],{},"list('abc')"," stop working?",[14,88250,88251,88252,88254,88255,88257],{},"You probably assigned a list to the name ",[17,88253,1235],{}," earlier, which replaced the built-in ",[17,88256,76220],{}," function in your current scope.",[675,88259,88261],{"id":88260},"do-i-need-to-restart-python-after-fixing-the-name","Do I need to restart Python after fixing the name?",[14,88263,88264],{},"Sometimes yes, especially in notebooks or interactive shells where the bad assignment still exists.",[77,88266,1554],{"id":1553},[40,88268,88269,88273,88278,88284,88288,88294],{},[43,88270,88271],{},[295,88272,35359],{"href":8110},[43,88274,88275],{},[295,88276,88277],{"href":87286},"How to create a list in Python",[43,88279,88280],{},[295,88281,1572,88282,1576],{"href":4427},[17,88283,4430],{},[43,88285,88286],{},[295,88287,42267],{"href":3852},[43,88289,88290],{},[295,88291,74872,88292],{"href":88217},[17,88293,79515],{},[43,88295,88296],{},[295,88297,5728,88298],{"href":4118},[17,88299,5477],{},[1589,88301,88302],{},"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 .sP7_E, html code.shiki .sP7_E{--shiki-light:#39ADB5;--shiki-default:#24292E;--shiki-dark:#E1E4E8}html pre.shiki code .srdBf, html code.shiki .srdBf{--shiki-light:#F76D47;--shiki-default:#005CC5;--shiki-dark:#79B8FF}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 .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 .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}html pre.shiki code .s_hVV, html code.shiki .s_hVV{--shiki-light:#90A4AE;--shiki-default:#005CC5;--shiki-dark:#79B8FF}",{"title":96,"searchDepth":135,"depth":135,"links":88304},[88305,88306,88307,88308,88310,88311,88312,88313,88314,88315,88322],{"id":79,"depth":135,"text":80},{"id":5880,"depth":135,"text":5881},{"id":79703,"depth":135,"text":80736},{"id":87290,"depth":135,"text":88309},"Common cause: naming a variable list",{"id":87515,"depth":135,"text":87516},{"id":6243,"depth":135,"text":6244},{"id":12156,"depth":135,"text":12157},{"id":88146,"depth":135,"text":88147},{"id":7116,"depth":135,"text":7117},{"id":1513,"depth":135,"text":1514,"children":88316},[88317,88318,88319,88321],{"id":88224,"depth":142,"text":88225},{"id":88234,"depth":142,"text":88235},{"id":88243,"depth":142,"text":88320},"Why did list('abc') stop working?",{"id":88260,"depth":142,"text":88261},{"id":1553,"depth":135,"text":1554},"Master typeerror list object is not callable fix in our comprehensive Python beginner guide.",{},"\u002Ferrors\u002Ftypeerror-list-object-is-not-callable-fix",{"title":86900,"description":88323},"errors\u002Ftypeerror-list-object-is-not-callable-fix","qI3OK9oRGdLzaEM29WSCKiPN10NC_C4OEkqWvfHWREM",{"id":88330,"title":88331,"body":88332,"description":89664,"extension":1623,"meta":89665,"navigation":138,"path":89666,"seo":89667,"stem":89668,"__hash__":89669},"content\u002Ferrors\u002Ftypeerror-missing-required-positional-argument-fix.md","TypeError: missing required positional argument (Fix)",{"type":7,"value":88333,"toc":89636},[88334,88337,88340,88343,88345,88401,88404,88406,88411,88425,88428,88483,88485,88493,88496,88498,88500,88521,88532,88534,88537,88588,88590,88621,88623,88647,88649,88653,88656,88709,88711,88719,88723,88726,88798,88801,88805,88808,88886,88889,88893,88896,88964,88966,88981,88989,88993,88996,89080,89082,89100,89104,89107,89113,89118,89189,89198,89202,89205,89284,89290,89294,89300,89363,89369,89373,89375,89398,89400,89454,89457,89538,89540,89542,89559,89566,89568,89572,89575,89579,89582,89586,89592,89596,89602,89604,89633],[10,88335,88331],{"id":88336},"typeerror-missing-required-positional-argument-fix",[14,88338,88339],{},"This error happens when Python expects an argument, but your code does not provide it.",[14,88341,88342],{},"It usually appears when you call a function or method with too few values. The message often tells you exactly which parameter is missing.",[77,88344,80],{"id":79},[91,88346,88348],{"className":93,"code":88347,"language":95,"meta":96,"style":96},"def greet(name):\n    print(\"Hello\", name)\n\ngreet(\"Sam\")  # pass the required argument\n",[17,88349,88350,88362,88380,88384],{"__ignoreMap":96},[100,88351,88352,88354,88356,88358,88360],{"class":102,"line":103},[100,88353,1078],{"class":1077},[100,88355,2848],{"class":1081},[100,88357,170],{"class":118},[100,88359,2853],{"class":2519},[100,88361,522],{"class":118},[100,88363,88364,88366,88368,88370,88372,88374,88376,88378],{"class":102,"line":135},[100,88365,200],{"class":114},[100,88367,170],{"class":118},[100,88369,206],{"class":205},[100,88371,7683],{"class":209},[100,88373,206],{"class":205},[100,88375,126],{"class":118},[100,88377,1703],{"class":178},[100,88379,215],{"class":118},[100,88381,88382],{"class":102,"line":142},[100,88383,139],{"emptyLinePlaceholder":138},[100,88385,88386,88388,88390,88392,88394,88396,88398],{"class":102,"line":152},[100,88387,2911],{"class":178},[100,88389,170],{"class":118},[100,88391,206],{"class":205},[100,88393,29329],{"class":209},[100,88395,206],{"class":205},[100,88397,6155],{"class":118},[100,88399,88400],{"class":414},"  # pass the required argument\n",[14,88402,88403],{},"If a function expects one or more required positional arguments, you must provide them when calling it unless the function defines default values.",[77,88405,5881],{"id":5880},[14,88407,88408,24911],{},[17,88409,88410],{},"TypeError: missing required positional argument",[40,88412,88413,88416,88419,88422],{},[43,88414,88415],{},"Python expected an argument that was not provided",[43,88417,88418],{},"A positional argument is matched by its position in the function call",[43,88420,88421],{},"The error usually names the missing parameter",[43,88423,88424],{},"It can happen with your own functions, class methods, or built-in\u002Flibrary code",[14,88426,88427],{},"For example, if a function needs two arguments, you must pass both:",[91,88429,88431],{"className":93,"code":88430,"language":95,"meta":96,"style":96},"def add(a, b):\n    return a + b\n\nprint(add(2, 3))\n",[17,88432,88433,88450,88460,88464],{"__ignoreMap":96},[100,88434,88435,88437,88440,88442,88444,88446,88448],{"class":102,"line":103},[100,88436,1078],{"class":1077},[100,88438,88439],{"class":1081}," add",[100,88441,170],{"class":118},[100,88443,295],{"class":2519},[100,88445,126],{"class":118},[100,88447,15869],{"class":2519},[100,88449,522],{"class":118},[100,88451,88452,88454,88456,88458],{"class":102,"line":135},[100,88453,2552],{"class":145},[100,88455,25407],{"class":106},[100,88457,6547],{"class":110},[100,88459,33812],{"class":106},[100,88461,88462],{"class":102,"line":142},[100,88463,139],{"emptyLinePlaceholder":138},[100,88465,88466,88468,88470,88473,88475,88477,88479,88481],{"class":102,"line":152},[100,88467,372],{"class":114},[100,88469,170],{"class":118},[100,88471,88472],{"class":178},"add",[100,88474,170],{"class":118},[100,88476,559],{"class":122},[100,88478,126],{"class":118},[100,88480,1405],{"class":122},[100,88482,182],{"class":118},[14,88484,218],{},[91,88486,88487],{"className":93,"code":1909,"language":95,"meta":96,"style":96},[17,88488,88489],{"__ignoreMap":96},[100,88490,88491],{"class":102,"line":103},[100,88492,1909],{"class":122},[14,88494,88495],{},"If you leave one out, Python cannot complete the call.",[77,88497,25091],{"id":25090},[14,88499,38066],{},[40,88501,88502,88505,88508,88515,88518],{},[43,88503,88504],{},"You called a function with too few arguments",[43,88506,88507],{},"You forgot that a method also needs values after the object name",[43,88509,88510,88511,88514],{},"You created a class method but forgot the ",[17,88512,88513],{},"self"," parameter in the method definition or misused it when calling",[43,88516,88517],{},"You changed a function definition but did not update all places that call it",[43,88519,88520],{},"You passed keyword arguments for some parameters but still left a required one out",[14,88522,88523,88524,3266,88528,290],{},"If you are new to this topic, it helps to review ",[295,88525,88527],{"href":88526},"\u002Flearn\u002Ffunction-parameters-and-arguments-in-python\u002F","function parameters and arguments in Python",[295,88529,88531],{"href":88530},"\u002Flearn\u002Fdefault-and-keyword-arguments-explained\u002F","default and keyword arguments",[77,88533,11031],{"id":11030},[14,88535,88536],{},"Here is a simple example.",[91,88538,88540],{"className":93,"code":88539,"language":95,"meta":96,"style":96},"def multiply(a, b):\n    return a * b\n\nprint(multiply(4))\n",[17,88541,88542,88559,88569,88573],{"__ignoreMap":96},[100,88543,88544,88546,88549,88551,88553,88555,88557],{"class":102,"line":103},[100,88545,1078],{"class":1077},[100,88547,88548],{"class":1081}," multiply",[100,88550,170],{"class":118},[100,88552,295],{"class":2519},[100,88554,126],{"class":118},[100,88556,15869],{"class":2519},[100,88558,522],{"class":118},[100,88560,88561,88563,88565,88567],{"class":102,"line":135},[100,88562,2552],{"class":145},[100,88564,25407],{"class":106},[100,88566,3252],{"class":110},[100,88568,33812],{"class":106},[100,88570,88571],{"class":102,"line":142},[100,88572,139],{"emptyLinePlaceholder":138},[100,88574,88575,88577,88579,88582,88584,88586],{"class":102,"line":152},[100,88576,372],{"class":114},[100,88578,170],{"class":118},[100,88580,88581],{"class":178},"multiply",[100,88583,170],{"class":118},[100,88585,8322],{"class":122},[100,88587,182],{"class":118},[14,88589,218],{},[91,88591,88593],{"className":93,"code":88592,"language":95,"meta":96,"style":96},"TypeError: multiply() missing 1 required positional argument: 'b'\n",[17,88594,88595],{"__ignoreMap":96},[100,88596,88597,88599,88601,88603,88605,88608,88610,88613,88615,88617,88619],{"class":102,"line":103},[100,88598,3538],{"class":191},[100,88600,89],{"class":118},[100,88602,88548],{"class":178},[100,88604,7562],{"class":118},[100,88606,88607],{"class":106}," missing ",[100,88609,123],{"class":122},[100,88611,88612],{"class":106}," required positional argument",[100,88614,89],{"class":118},[100,88616,1274],{"class":205},[100,88618,4027],{"class":209},[100,88620,3925],{"class":205},[14,88622,11192],{},[40,88624,88625,88632,88639],{},[43,88626,88627,88628,88631],{},"The function ",[17,88629,88630],{},"multiply(a, b)"," requires two values",[43,88633,88634,88635,88638],{},"The call ",[17,88636,88637],{},"multiply(4)"," only provides one",[43,88640,3860,88641,88643,88644,88646],{},[17,88642,3538],{}," because the argument for ",[17,88645,4027],{}," is missing",[77,88648,6244],{"id":6243},[675,88650,88652],{"id":88651},"pass-all-required-arguments","Pass all required arguments",[14,88654,88655],{},"The most direct fix is to pass every required argument.",[91,88657,88659],{"className":93,"code":88658,"language":95,"meta":96,"style":96},"def multiply(a, b):\n    return a * b\n\nprint(multiply(4, 5))\n",[17,88660,88661,88677,88687,88691],{"__ignoreMap":96},[100,88662,88663,88665,88667,88669,88671,88673,88675],{"class":102,"line":103},[100,88664,1078],{"class":1077},[100,88666,88548],{"class":1081},[100,88668,170],{"class":118},[100,88670,295],{"class":2519},[100,88672,126],{"class":118},[100,88674,15869],{"class":2519},[100,88676,522],{"class":118},[100,88678,88679,88681,88683,88685],{"class":102,"line":135},[100,88680,2552],{"class":145},[100,88682,25407],{"class":106},[100,88684,3252],{"class":110},[100,88686,33812],{"class":106},[100,88688,88689],{"class":102,"line":142},[100,88690,139],{"emptyLinePlaceholder":138},[100,88692,88693,88695,88697,88699,88701,88703,88705,88707],{"class":102,"line":152},[100,88694,372],{"class":114},[100,88696,170],{"class":118},[100,88698,88581],{"class":178},[100,88700,170],{"class":118},[100,88702,8322],{"class":122},[100,88704,126],{"class":118},[100,88706,31164],{"class":122},[100,88708,182],{"class":118},[14,88710,218],{},[91,88712,88713],{"className":93,"code":79076,"language":95,"meta":96,"style":96},[17,88714,88715],{"__ignoreMap":96},[100,88716,88717],{"class":102,"line":103},[100,88718,79076],{"class":122},[675,88720,88722],{"id":88721},"check-the-function-definition","Check the function definition",[14,88724,88725],{},"Look at the function definition and count the required parameters.",[91,88727,88729],{"className":93,"code":88728,"language":95,"meta":96,"style":96},"def greet(first_name, last_name):\n    print(\"Hello\", first_name, last_name)\n\ngreet(\"Sam\", \"Lee\")\n",[17,88730,88731,88748,88771,88775],{"__ignoreMap":96},[100,88732,88733,88735,88737,88739,88741,88743,88746],{"class":102,"line":103},[100,88734,1078],{"class":1077},[100,88736,2848],{"class":1081},[100,88738,170],{"class":118},[100,88740,43775],{"class":2519},[100,88742,126],{"class":118},[100,88744,88745],{"class":2519}," last_name",[100,88747,522],{"class":118},[100,88749,88750,88752,88754,88756,88758,88760,88762,88765,88767,88769],{"class":102,"line":135},[100,88751,200],{"class":114},[100,88753,170],{"class":118},[100,88755,206],{"class":205},[100,88757,7683],{"class":209},[100,88759,206],{"class":205},[100,88761,126],{"class":118},[100,88763,88764],{"class":178}," first_name",[100,88766,126],{"class":118},[100,88768,88745],{"class":178},[100,88770,215],{"class":118},[100,88772,88773],{"class":102,"line":142},[100,88774,139],{"emptyLinePlaceholder":138},[100,88776,88777,88779,88781,88783,88785,88787,88789,88791,88794,88796],{"class":102,"line":152},[100,88778,2911],{"class":178},[100,88780,170],{"class":118},[100,88782,206],{"class":205},[100,88784,29329],{"class":209},[100,88786,206],{"class":205},[100,88788,126],{"class":118},[100,88790,1708],{"class":205},[100,88792,88793],{"class":209},"Lee",[100,88795,206],{"class":205},[100,88797,215],{"class":118},[14,88799,88800],{},"If the function has two required parameters, the call must include two arguments.",[675,88802,88804],{"id":88803},"use-keyword-arguments-for-clarity","Use keyword arguments for clarity",[14,88806,88807],{},"Keyword arguments can make the call easier to read.",[91,88809,88811],{"className":93,"code":88810,"language":95,"meta":96,"style":96},"def greet(first_name, last_name):\n    print(\"Hello\", first_name, last_name)\n\ngreet(last_name=\"Lee\", first_name=\"Sam\")\n",[17,88812,88813,88829,88851,88855],{"__ignoreMap":96},[100,88814,88815,88817,88819,88821,88823,88825,88827],{"class":102,"line":103},[100,88816,1078],{"class":1077},[100,88818,2848],{"class":1081},[100,88820,170],{"class":118},[100,88822,43775],{"class":2519},[100,88824,126],{"class":118},[100,88826,88745],{"class":2519},[100,88828,522],{"class":118},[100,88830,88831,88833,88835,88837,88839,88841,88843,88845,88847,88849],{"class":102,"line":135},[100,88832,200],{"class":114},[100,88834,170],{"class":118},[100,88836,206],{"class":205},[100,88838,7683],{"class":209},[100,88840,206],{"class":205},[100,88842,126],{"class":118},[100,88844,88764],{"class":178},[100,88846,126],{"class":118},[100,88848,88745],{"class":178},[100,88850,215],{"class":118},[100,88852,88853],{"class":102,"line":142},[100,88854,139],{"emptyLinePlaceholder":138},[100,88856,88857,88859,88861,88864,88866,88868,88870,88872,88874,88876,88878,88880,88882,88884],{"class":102,"line":152},[100,88858,2911],{"class":178},[100,88860,170],{"class":118},[100,88862,88863],{"class":18141},"last_name",[100,88865,111],{"class":110},[100,88867,206],{"class":205},[100,88869,88793],{"class":209},[100,88871,206],{"class":205},[100,88873,126],{"class":118},[100,88875,88764],{"class":18141},[100,88877,111],{"class":110},[100,88879,206],{"class":205},[100,88881,29329],{"class":209},[100,88883,206],{"class":205},[100,88885,215],{"class":118},[14,88887,88888],{},"This still passes all required values, but by name instead of position.",[675,88890,88892],{"id":88891},"add-a-default-value-if-the-argument-should-be-optional","Add a default value if the argument should be optional",[14,88894,88895],{},"If a parameter should not be required, give it a default value.",[91,88897,88899],{"className":93,"code":88898,"language":95,"meta":96,"style":96},"def greet(name=\"Guest\"):\n    print(\"Hello\", name)\n\ngreet()\ngreet(\"Sam\")\n",[17,88900,88901,88922,88940,88944,88950],{"__ignoreMap":96},[100,88902,88903,88905,88907,88909,88911,88913,88915,88918,88920],{"class":102,"line":103},[100,88904,1078],{"class":1077},[100,88906,2848],{"class":1081},[100,88908,170],{"class":118},[100,88910,2853],{"class":2519},[100,88912,111],{"class":110},[100,88914,206],{"class":205},[100,88916,88917],{"class":209},"Guest",[100,88919,206],{"class":205},[100,88921,522],{"class":118},[100,88923,88924,88926,88928,88930,88932,88934,88936,88938],{"class":102,"line":135},[100,88925,200],{"class":114},[100,88927,170],{"class":118},[100,88929,206],{"class":205},[100,88931,7683],{"class":209},[100,88933,206],{"class":205},[100,88935,126],{"class":118},[100,88937,1703],{"class":178},[100,88939,215],{"class":118},[100,88941,88942],{"class":102,"line":142},[100,88943,139],{"emptyLinePlaceholder":138},[100,88945,88946,88948],{"class":102,"line":152},[100,88947,2911],{"class":178},[100,88949,4734],{"class":118},[100,88951,88952,88954,88956,88958,88960,88962],{"class":102,"line":164},[100,88953,2911],{"class":178},[100,88955,170],{"class":118},[100,88957,206],{"class":205},[100,88959,29329],{"class":209},[100,88961,206],{"class":205},[100,88963,215],{"class":118},[14,88965,218],{},[91,88967,88969],{"className":93,"code":88968,"language":95,"meta":96,"style":96},"Hello Guest\nHello Sam\n",[17,88970,88971,88976],{"__ignoreMap":96},[100,88972,88973],{"class":102,"line":103},[100,88974,88975],{"class":106},"Hello Guest\n",[100,88977,88978],{"class":102,"line":135},[100,88979,88980],{"class":106},"Hello Sam\n",[14,88982,88983,88984,88988],{},"This is a common fix when you are designing your own functions. See ",[295,88985,88987],{"href":88986},"\u002Fhow-to\u002Fhow-to-create-a-simple-function-in-python\u002F","how to create a simple function in Python"," for a beginner-friendly walkthrough.",[675,88990,88992],{"id":88991},"if-the-error-comes-from-a-method-call-it-correctly","If the error comes from a method, call it correctly",[14,88994,88995],{},"Methods belong to objects. You usually call them on an instance.",[91,88997,88999],{"className":93,"code":88998,"language":95,"meta":96,"style":96},"class User:\n    def show(self, name):\n        print(\"User:\", name)\n\nuser = User()\nuser.show(\"Sam\")\n",[17,89000,89001,89010,89028,89047,89051,89061],{"__ignoreMap":96},[100,89002,89003,89005,89008],{"class":102,"line":103},[100,89004,3417],{"class":1077},[100,89006,89007],{"class":10361}," User",[100,89009,149],{"class":118},[100,89011,89012,89015,89017,89019,89022,89024,89026],{"class":102,"line":135},[100,89013,89014],{"class":1077},"    def",[100,89016,25875],{"class":1081},[100,89018,170],{"class":118},[100,89020,88513],{"class":89021},"smCYv",[100,89023,126],{"class":118},[100,89025,1703],{"class":2519},[100,89027,522],{"class":118},[100,89029,89030,89032,89034,89036,89039,89041,89043,89045],{"class":102,"line":142},[100,89031,167],{"class":114},[100,89033,170],{"class":118},[100,89035,206],{"class":205},[100,89037,89038],{"class":209},"User:",[100,89040,206],{"class":205},[100,89042,126],{"class":118},[100,89044,1703],{"class":178},[100,89046,215],{"class":118},[100,89048,89049],{"class":102,"line":152},[100,89050,139],{"emptyLinePlaceholder":138},[100,89052,89053,89055,89057,89059],{"class":102,"line":164},[100,89054,15336],{"class":106},[100,89056,111],{"class":110},[100,89058,89007],{"class":178},[100,89060,4734],{"class":118},[100,89062,89063,89065,89067,89070,89072,89074,89076,89078],{"class":102,"line":185},[100,89064,15354],{"class":106},[100,89066,290],{"class":118},[100,89068,89069],{"class":178},"show",[100,89071,170],{"class":118},[100,89073,206],{"class":205},[100,89075,29329],{"class":209},[100,89077,206],{"class":205},[100,89079,215],{"class":118},[14,89081,30338],{},[40,89083,89084,89089,89094],{},[43,89085,89086,89088],{},[17,89087,15354],{}," is the instance",[43,89090,89091,89092,11208],{},"Python passes ",[17,89093,88513],{},[43,89095,89096,89097],{},"You only need to pass the remaining required argument: ",[17,89098,89099],{},"\"Sam\"",[77,89101,89103],{"id":89102},"special-case-methods-and-self","Special case: methods and self",[14,89105,89106],{},"This error is common when working with classes.",[675,89108,89110,89111,11208],{"id":89109},"instance-methods-receive-self-automatically","Instance methods receive ",[17,89112,88513],{},[14,89114,89115,89116,290],{},"When you define an instance method, the first parameter is usually ",[17,89117,88513],{},[91,89119,89121],{"className":93,"code":89120,"language":95,"meta":96,"style":96},"class User:\n    def show(self, name):\n        print(name)\n\nuser = User()\nuser.show(\"Sam\")\n",[17,89122,89123,89131,89147,89157,89161,89171],{"__ignoreMap":96},[100,89124,89125,89127,89129],{"class":102,"line":103},[100,89126,3417],{"class":1077},[100,89128,89007],{"class":10361},[100,89130,149],{"class":118},[100,89132,89133,89135,89137,89139,89141,89143,89145],{"class":102,"line":135},[100,89134,89014],{"class":1077},[100,89136,25875],{"class":1081},[100,89138,170],{"class":118},[100,89140,88513],{"class":89021},[100,89142,126],{"class":118},[100,89144,1703],{"class":2519},[100,89146,522],{"class":118},[100,89148,89149,89151,89153,89155],{"class":102,"line":142},[100,89150,167],{"class":114},[100,89152,170],{"class":118},[100,89154,2853],{"class":178},[100,89156,215],{"class":118},[100,89158,89159],{"class":102,"line":152},[100,89160,139],{"emptyLinePlaceholder":138},[100,89162,89163,89165,89167,89169],{"class":102,"line":164},[100,89164,15336],{"class":106},[100,89166,111],{"class":110},[100,89168,89007],{"class":178},[100,89170,4734],{"class":118},[100,89172,89173,89175,89177,89179,89181,89183,89185,89187],{"class":102,"line":185},[100,89174,15354],{"class":106},[100,89176,290],{"class":118},[100,89178,89069],{"class":178},[100,89180,170],{"class":118},[100,89182,206],{"class":205},[100,89184,29329],{"class":209},[100,89186,206],{"class":205},[100,89188,215],{"class":118},[14,89190,89191,89192,89194,89195,89197],{},"You do ",[22,89193,1059],{}," pass ",[17,89196,88513],{}," yourself in a normal method call.",[675,89199,89201],{"id":89200},"do-not-call-the-method-incorrectly-on-the-class","Do not call the method incorrectly on the class",[14,89203,89204],{},"This can cause an argument mismatch.",[91,89206,89208],{"className":93,"code":89207,"language":95,"meta":96,"style":96},"class User:\n    def show(self, name):\n        print(name)\n\nuser = User()\n\n# Correct\nuser.show(\"Sam\")\n",[17,89209,89210,89218,89234,89244,89248,89258,89262,89266],{"__ignoreMap":96},[100,89211,89212,89214,89216],{"class":102,"line":103},[100,89213,3417],{"class":1077},[100,89215,89007],{"class":10361},[100,89217,149],{"class":118},[100,89219,89220,89222,89224,89226,89228,89230,89232],{"class":102,"line":135},[100,89221,89014],{"class":1077},[100,89223,25875],{"class":1081},[100,89225,170],{"class":118},[100,89227,88513],{"class":89021},[100,89229,126],{"class":118},[100,89231,1703],{"class":2519},[100,89233,522],{"class":118},[100,89235,89236,89238,89240,89242],{"class":102,"line":142},[100,89237,167],{"class":114},[100,89239,170],{"class":118},[100,89241,2853],{"class":178},[100,89243,215],{"class":118},[100,89245,89246],{"class":102,"line":152},[100,89247,139],{"emptyLinePlaceholder":138},[100,89249,89250,89252,89254,89256],{"class":102,"line":164},[100,89251,15336],{"class":106},[100,89253,111],{"class":110},[100,89255,89007],{"class":178},[100,89257,4734],{"class":118},[100,89259,89260],{"class":102,"line":185},[100,89261,139],{"emptyLinePlaceholder":138},[100,89263,89264],{"class":102,"line":197},[100,89265,79582],{"class":414},[100,89267,89268,89270,89272,89274,89276,89278,89280,89282],{"class":102,"line":771},[100,89269,15354],{"class":106},[100,89271,290],{"class":118},[100,89273,89069],{"class":178},[100,89275,170],{"class":118},[100,89277,206],{"class":205},[100,89279,29329],{"class":209},[100,89281,206],{"class":205},[100,89283,215],{"class":118},[14,89285,89286,89287,89289],{},"If you call the method on the class instead of the instance, you may get confusing errors because Python is no longer filling ",[17,89288,88513],{}," for you in the usual way.",[675,89291,89293],{"id":89292},"make-sure-the-method-definition-is-correct","Make sure the method definition is correct",[14,89295,89296,89297,89299],{},"If you forget ",[17,89298,88513],{}," in the method definition, the parameters will not line up correctly.",[91,89301,89303],{"className":93,"code":89302,"language":95,"meta":96,"style":96},"class User:\n    def show(name):  # incorrect\n        print(name)\n\nuser = User()\nuser.show()\n",[17,89304,89305,89313,89329,89339,89343,89353],{"__ignoreMap":96},[100,89306,89307,89309,89311],{"class":102,"line":103},[100,89308,3417],{"class":1077},[100,89310,89007],{"class":10361},[100,89312,149],{"class":118},[100,89314,89315,89317,89319,89321,89323,89326],{"class":102,"line":135},[100,89316,89014],{"class":1077},[100,89318,25875],{"class":1081},[100,89320,170],{"class":118},[100,89322,2853],{"class":2519},[100,89324,89325],{"class":118},"):",[100,89327,89328],{"class":414},"  # incorrect\n",[100,89330,89331,89333,89335,89337],{"class":102,"line":142},[100,89332,167],{"class":114},[100,89334,170],{"class":118},[100,89336,2853],{"class":178},[100,89338,215],{"class":118},[100,89340,89341],{"class":102,"line":152},[100,89342,139],{"emptyLinePlaceholder":138},[100,89344,89345,89347,89349,89351],{"class":102,"line":164},[100,89346,15336],{"class":106},[100,89348,111],{"class":110},[100,89350,89007],{"class":178},[100,89352,4734],{"class":118},[100,89354,89355,89357,89359,89361],{"class":102,"line":185},[100,89356,15354],{"class":106},[100,89358,290],{"class":118},[100,89360,89069],{"class":178},[100,89362,4734],{"class":118},[14,89364,89365,89366,89368],{},"This method definition is wrong for a normal instance method. If you are learning functions first, start with ",[295,89367,29749],{"href":29189}," before moving deeper into classes.",[77,89370,89372],{"id":89371},"how-to-debug-it-step-by-step","How to debug it step by step",[14,89374,32241],{},[3282,89376,89377,89380,89383,89386,89389,89392],{},[43,89378,89379],{},"Read the full error message and note the missing parameter name",[43,89381,89382],{},"Find the exact line where the function or method is called",[43,89384,89385],{},"Compare the call with the function definition",[43,89387,89388],{},"Count how many required parameters exist",[43,89390,89391],{},"Check whether some parameters already have default values and which do not",[43,89393,89394,89395,89397],{},"If using an editor, hover over the function name or use ",[17,89396,9796],{}," to inspect the signature",[14,89399,5295],{},[91,89401,89403],{"className":93,"code":89402,"language":95,"meta":96,"style":96},"help(function_name)\nprint(function_name)\nprint(type(object_name))\nprint(vars(object_name))\n",[17,89404,89405,89415,89425,89439],{"__ignoreMap":96},[100,89406,89407,89409,89411,89413],{"class":102,"line":103},[100,89408,2478],{"class":114},[100,89410,170],{"class":118},[100,89412,17820],{"class":178},[100,89414,215],{"class":118},[100,89416,89417,89419,89421,89423],{"class":102,"line":135},[100,89418,372],{"class":114},[100,89420,170],{"class":118},[100,89422,17820],{"class":178},[100,89424,215],{"class":118},[100,89426,89427,89429,89431,89433,89435,89437],{"class":102,"line":142},[100,89428,372],{"class":114},[100,89430,170],{"class":118},[100,89432,1250],{"class":191},[100,89434,170],{"class":118},[100,89436,17809],{"class":178},[100,89438,182],{"class":118},[100,89440,89441,89443,89445,89448,89450,89452],{"class":102,"line":152},[100,89442,372],{"class":114},[100,89444,170],{"class":118},[100,89446,89447],{"class":114},"vars",[100,89449,170],{"class":118},[100,89451,17809],{"class":178},[100,89453,182],{"class":118},[14,89455,89456],{},"If you want to print the full traceback inside a larger program:",[91,89458,89460],{"className":93,"code":89459,"language":95,"meta":96,"style":96},"import traceback\n\ntry:\n    def greet(name):\n        print(\"Hello\", name)\n\n    greet()\nexcept Exception:\n    traceback.print_exc()\n",[17,89461,89462,89468,89472,89478,89490,89508,89512,89518,89526],{"__ignoreMap":96},[100,89463,89464,89466],{"class":102,"line":103},[100,89465,9259],{"class":145},[100,89467,66101],{"class":106},[100,89469,89470],{"class":102,"line":135},[100,89471,139],{"emptyLinePlaceholder":138},[100,89473,89474,89476],{"class":102,"line":142},[100,89475,146],{"class":145},[100,89477,149],{"class":118},[100,89479,89480,89482,89484,89486,89488],{"class":102,"line":152},[100,89481,89014],{"class":1077},[100,89483,2848],{"class":1081},[100,89485,170],{"class":118},[100,89487,2853],{"class":2519},[100,89489,522],{"class":118},[100,89491,89492,89494,89496,89498,89500,89502,89504,89506],{"class":102,"line":164},[100,89493,167],{"class":114},[100,89495,170],{"class":118},[100,89497,206],{"class":205},[100,89499,7683],{"class":209},[100,89501,206],{"class":205},[100,89503,126],{"class":118},[100,89505,1703],{"class":178},[100,89507,215],{"class":118},[100,89509,89510],{"class":102,"line":185},[100,89511,139],{"emptyLinePlaceholder":138},[100,89513,89514,89516],{"class":102,"line":197},[100,89515,63912],{"class":178},[100,89517,4734],{"class":118},[100,89519,89520,89522,89524],{"class":102,"line":771},[100,89521,188],{"class":145},[100,89523,16221],{"class":191},[100,89525,149],{"class":118},[100,89527,89528,89531,89533,89536],{"class":102,"line":787},[100,89529,89530],{"class":106},"    traceback",[100,89532,290],{"class":118},[100,89534,89535],{"class":178},"print_exc",[100,89537,4734],{"class":118},[77,89539,7117],{"id":7116},[14,89541,9001],{},[40,89543,89544,89547,89550,89553,89556],{},[43,89545,89546],{},"Calling a function with too few arguments",[43,89548,89549],{},"Forgetting one argument after editing a function definition",[43,89551,89552],{},"Calling an instance method incorrectly",[43,89554,89555],{},"Assuming a parameter is optional when it is not",[43,89557,89558],{},"Mixing positional and keyword arguments and leaving one out",[14,89560,89561,89562,290],{},"A related problem is passing the wrong parameter name. If that happens, see ",[295,89563,89565],{"href":89564},"\u002Ferrors\u002Ftypeerror-unexpected-keyword-argument-fix\u002F","TypeError: unexpected keyword argument",[77,89567,1514],{"id":1513},[675,89569,89571],{"id":89570},"what-is-a-positional-argument-in-python","What is a positional argument in Python?",[14,89573,89574],{},"It is an argument matched to a parameter by its position in the function call, such as the first or second value passed.",[675,89576,89578],{"id":89577},"can-i-fix-this-by-using-keyword-arguments","Can I fix this by using keyword arguments?",[14,89580,89581],{},"Yes. If you provide the missing required parameter by name, the call can be clearer and easier to read.",[675,89583,89585],{"id":89584},"why-does-this-happen-with-class-methods","Why does this happen with class methods?",[14,89587,89588,89589,89591],{},"Methods have specific parameters too. For instance methods, Python passes ",[17,89590,88513],{}," automatically when you call the method on an object.",[675,89593,89595],{"id":89594},"how-do-i-make-an-argument-optional","How do I make an argument optional?",[14,89597,89598,89599,290],{},"Give the parameter a default value in the function definition, such as ",[17,89600,89601],{},"name=\"Guest\"",[77,89603,1554],{"id":1553},[40,89605,89606,89611,89616,89620,89625,89629],{},[43,89607,89608],{},[295,89609,89610],{"href":88526},"Function parameters and arguments in Python",[43,89612,89613],{},[295,89614,89615],{"href":88530},"Default and keyword arguments explained",[43,89617,89618],{},[295,89619,29749],{"href":29189},[43,89621,89622],{},[295,89623,89624],{"href":88986},"How to create a simple function in Python",[43,89626,89627],{},[295,89628,89565],{"href":89564},[43,89630,89631],{},[295,89632,86914],{"href":80712},[1589,89634,89635],{},"html pre.shiki code .sbsja, html code.shiki .sbsja{--shiki-light:#9C3EDA;--shiki-default:#D73A49;--shiki-dark:#F97583}html pre.shiki code .sGLFI, html code.shiki .sGLFI{--shiki-light:#6182B8;--shiki-default:#6F42C1;--shiki-dark:#B392F0}html pre.shiki code .sP7_E, html code.shiki .sP7_E{--shiki-light:#39ADB5;--shiki-default:#24292E;--shiki-dark:#E1E4E8}html pre.shiki code .sFwrP, html code.shiki .sFwrP{--shiki-light:#90A4AE;--shiki-light-font-style:italic;--shiki-default:#24292E;--shiki-default-font-style:inherit;--shiki-dark:#E1E4E8;--shiki-dark-font-style:inherit}html pre.shiki code .sptTA, html code.shiki .sptTA{--shiki-light:#6182B8;--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 .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 .sVHd0, html code.shiki .sVHd0{--shiki-light:#39ADB5;--shiki-light-font-style:italic;--shiki-default:#D73A49;--shiki-default-font-style:inherit;--shiki-dark:#F97583;--shiki-dark-font-style:inherit}html pre.shiki code .su5hD, html code.shiki .su5hD{--shiki-light:#90A4AE;--shiki-default:#24292E;--shiki-dark:#E1E4E8}html pre.shiki code .smGrS, html code.shiki .smGrS{--shiki-light:#39ADB5;--shiki-default:#D73A49;--shiki-dark:#F97583}html pre.shiki code .srdBf, html code.shiki .srdBf{--shiki-light:#F76D47;--shiki-default:#005CC5;--shiki-dark:#79B8FF}html pre.shiki code .sZMiF, html code.shiki .sZMiF{--shiki-light:#E2931D;--shiki-default:#005CC5;--shiki-dark:#79B8FF}html pre.shiki code .s99_P, html code.shiki .s99_P{--shiki-light:#90A4AE;--shiki-light-font-style:italic;--shiki-default:#E36209;--shiki-default-font-style:inherit;--shiki-dark:#FFAB70;--shiki-dark-font-style:inherit}html pre.shiki code .sbgvK, html code.shiki .sbgvK{--shiki-light:#E2931D;--shiki-default:#6F42C1;--shiki-dark:#B392F0}html pre.shiki code .smCYv, html code.shiki .smCYv{--shiki-light:#E53935;--shiki-light-font-style:italic;--shiki-default:#24292E;--shiki-default-font-style:inherit;--shiki-dark:#E1E4E8;--shiki-dark-font-style:inherit}",{"title":96,"searchDepth":135,"depth":135,"links":89637},[89638,89639,89640,89641,89642,89649,89655,89656,89657,89663],{"id":79,"depth":135,"text":80},{"id":5880,"depth":135,"text":5881},{"id":25090,"depth":135,"text":25091},{"id":11030,"depth":135,"text":11031},{"id":6243,"depth":135,"text":6244,"children":89643},[89644,89645,89646,89647,89648],{"id":88651,"depth":142,"text":88652},{"id":88721,"depth":142,"text":88722},{"id":88803,"depth":142,"text":88804},{"id":88891,"depth":142,"text":88892},{"id":88991,"depth":142,"text":88992},{"id":89102,"depth":135,"text":89103,"children":89650},[89651,89653,89654],{"id":89109,"depth":142,"text":89652},"Instance methods receive self automatically",{"id":89200,"depth":142,"text":89201},{"id":89292,"depth":142,"text":89293},{"id":89371,"depth":135,"text":89372},{"id":7116,"depth":135,"text":7117},{"id":1513,"depth":135,"text":1514,"children":89658},[89659,89660,89661,89662],{"id":89570,"depth":142,"text":89571},{"id":89577,"depth":142,"text":89578},{"id":89584,"depth":142,"text":89585},{"id":89594,"depth":142,"text":89595},{"id":1553,"depth":135,"text":1554},"Master typeerror missing required positional argument fix in our comprehensive Python beginner guide.",{},"\u002Ferrors\u002Ftypeerror-missing-required-positional-argument-fix",{"title":88331,"description":89664},"errors\u002Ftypeerror-missing-required-positional-argument-fix","EdIX3FcLrS886ey_5k51JRVvq3KDfiyP97T2h8_202w",{"id":89671,"title":89672,"body":89673,"description":91435,"extension":1623,"meta":91436,"navigation":138,"path":91437,"seo":91438,"stem":91439,"__hash__":91440},"content\u002Ferrors\u002Ftypeerror-module-object-is-not-callable-fix.md","TypeError: 'module' object is not callable (Fix)",{"type":7,"value":89674,"toc":91392},[89675,89680,89688,89703,89705,89766,89768,89776,89783,89785,89792,89798,89807,89812,89814,89843,89845,89864,89871,89873,89876,89905,89907,89932,89934,89955,89960,89964,89970,89976,90009,90011,90020,90025,90062,90064,90072,90077,90132,90134,90159,90173,90177,90180,90213,90215,90223,90231,90234,90263,90292,90298,90303,90307,90310,90314,90348,90354,90358,90361,90368,90371,90393,90398,90431,90434,90444,90447,90464,90467,90503,90508,90512,90515,90538,90541,90562,90587,90613,90616,90645,90678,90711,90718,90720,90723,90727,90730,90745,90750,90753,90757,90760,90771,90773,90787,90790,90807,90811,90850,90853,90894,90900,90904,90932,90943,90947,90979,90983,90996,90999,91024,91027,91031,91037,91043,91046,91048,91083,91088,91093,91096,91098,91127,91133,91138,91140,91143,91165,91167,91170,91232,91235,91247,91249,91253,91256,91260,91269,91290,91294,91297,91301,91304,91311,91343,91347,91350,91352,91386,91389],[10,89676,74872,89678,7375],{"id":89677},"typeerror-module-object-is-not-callable-fix",[17,89679,80723],{},[14,89681,7378,89682,89684,89685,89687],{},[22,89683,1977],{}," where it expected something it can ",[22,89686,82327],{}," with parentheses.",[14,89689,89690,89691,3178,89694,89697,89698,3266,89700,89702],{},"Beginners often run into this after writing code like ",[17,89692,89693],{},"math(16)",[17,89695,89696],{},"json(text)",". The problem is that ",[17,89699,9271],{},[17,89701,9997],{}," are modules. They contain functions and classes, but the module itself is not a function.",[77,89704,80],{"id":79},[91,89706,89708],{"className":93,"code":89707,"language":95,"meta":96,"style":96},"import math\n\n# Wrong:\n# result = math(16)\n\n# Right:\nresult = math.sqrt(16)\nprint(result)\n",[17,89709,89710,89716,89720,89724,89729,89733,89738,89756],{"__ignoreMap":96},[100,89711,89712,89714],{"class":102,"line":103},[100,89713,9259],{"class":145},[100,89715,9262],{"class":106},[100,89717,89718],{"class":102,"line":135},[100,89719,139],{"emptyLinePlaceholder":138},[100,89721,89722],{"class":102,"line":142},[100,89723,74927],{"class":414},[100,89725,89726],{"class":102,"line":152},[100,89727,89728],{"class":414},"# result = math(16)\n",[100,89730,89731],{"class":102,"line":164},[100,89732,139],{"emptyLinePlaceholder":138},[100,89734,89735],{"class":102,"line":185},[100,89736,89737],{"class":414},"# Right:\n",[100,89739,89740,89742,89744,89746,89748,89750,89752,89754],{"class":102,"line":197},[100,89741,11556],{"class":106},[100,89743,111],{"class":110},[100,89745,53666],{"class":106},[100,89747,290],{"class":118},[100,89749,9276],{"class":178},[100,89751,170],{"class":118},[100,89753,10180],{"class":122},[100,89755,215],{"class":118},[100,89757,89758,89760,89762,89764],{"class":102,"line":771},[100,89759,372],{"class":114},[100,89761,170],{"class":118},[100,89763,11580],{"class":178},[100,89765,215],{"class":118},[14,89767,218],{},[91,89769,89770],{"className":93,"code":25067,"language":95,"meta":96,"style":96},[17,89771,89772],{"__ignoreMap":96},[100,89773,89774],{"class":102,"line":103},[100,89775,25067],{"class":122},[14,89777,89778,89779,89782],{},"A module is a container for functions, classes, and variables. Call something ",[22,89780,89781],{},"inside"," the module, not the module itself.",[77,89784,5881],{"id":5880},[14,89786,89787,89788,89791],{},"Python found a ",[22,89789,89790],{},"module object"," where it expected something callable.",[14,89793,571,89794,89797],{},[22,89795,89796],{},"callable"," is something you can use with parentheses, such as:",[40,89799,89800,89802,89804],{},[43,89801,9353],{},[43,89803,9356],{},[43,89805,89806],{},"some special objects that behave like functions",[14,89808,89809,89810,290],{},"A module is different. It groups related code together, but you do not call a module with ",[17,89811,7562],{},[14,89813,580],{},[91,89815,89817],{"className":93,"code":89816,"language":95,"meta":96,"style":96},"import math\n\nprint(type(math))\n",[17,89818,89819,89825,89829],{"__ignoreMap":96},[100,89820,89821,89823],{"class":102,"line":103},[100,89822,9259],{"class":145},[100,89824,9262],{"class":106},[100,89826,89827],{"class":102,"line":135},[100,89828,139],{"emptyLinePlaceholder":138},[100,89830,89831,89833,89835,89837,89839,89841],{"class":102,"line":142},[100,89832,372],{"class":114},[100,89834,170],{"class":118},[100,89836,1250],{"class":191},[100,89838,170],{"class":118},[100,89840,9271],{"class":178},[100,89842,182],{"class":118},[14,89844,218],{},[91,89846,89848],{"className":93,"code":89847,"language":95,"meta":96,"style":96},"\u003Cclass 'module'>\n",[17,89849,89850],{"__ignoreMap":96},[100,89851,89852,89854,89856,89858,89860,89862],{"class":102,"line":103},[100,89853,2782],{"class":110},[100,89855,3417],{"class":1077},[100,89857,1274],{"class":205},[100,89859,1977],{"class":209},[100,89861,1280],{"class":205},[100,89863,1980],{"class":110},[14,89865,89866,89867,290],{},"If you want to inspect this more, see ",[295,89868,1572,89869,9570],{"href":4427},[17,89870,4430],{},[77,89872,24958],{"id":24957},[14,89874,89875],{},"A very common mistake looks like this:",[91,89877,89879],{"className":93,"code":89878,"language":95,"meta":96,"style":96},"import math\n\nprint(math(10))\n",[17,89880,89881,89887,89891],{"__ignoreMap":96},[100,89882,89883,89885],{"class":102,"line":103},[100,89884,9259],{"class":145},[100,89886,9262],{"class":106},[100,89888,89889],{"class":102,"line":135},[100,89890,139],{"emptyLinePlaceholder":138},[100,89892,89893,89895,89897,89899,89901,89903],{"class":102,"line":142},[100,89894,372],{"class":114},[100,89896,170],{"class":118},[100,89898,9271],{"class":178},[100,89900,170],{"class":118},[100,89902,356],{"class":122},[100,89904,182],{"class":118},[14,89906,7710],{},[91,89908,89910],{"className":93,"code":89909,"language":95,"meta":96,"style":96},"TypeError: 'module' object is not callable\n",[17,89911,89912],{"__ignoreMap":96},[100,89913,89914,89916,89918,89920,89922,89924,89926,89928,89930],{"class":102,"line":103},[100,89915,3538],{"class":191},[100,89917,89],{"class":118},[100,89919,1274],{"class":205},[100,89921,1977],{"class":209},[100,89923,1280],{"class":205},[100,89925,3914],{"class":191},[100,89927,16702],{"class":110},[100,89929,4921],{"class":110},[100,89931,16885],{"class":114},[14,89933,32043],{},[40,89935,89936,89944,89949],{},[43,89937,89938,89940,89941,89943],{},[17,89939,26216],{}," imports the ",[17,89942,9271],{}," module",[43,89945,89946,89948],{},[17,89947,9271],{}," is now the name of that module",[43,89950,89951,89954],{},[17,89952,89953],{},"math(10)"," tries to call the module like a function",[14,89956,38150,89957,89959],{},[17,89958,9271],{}," is not a function. It is a module.",[77,89961,89963],{"id":89962},"fix-1-call-the-function-inside-the-module","Fix 1: Call the function inside the module",[14,89965,241,89966,89969],{},[22,89967,89968],{},"dot notation"," to access a function inside the module.",[675,89971,89973,89974],{"id":89972},"example-with-math","Example with ",[17,89975,9271],{},[91,89977,89979],{"className":93,"code":89978,"language":95,"meta":96,"style":96},"import math\n\nprint(math.sqrt(10))\n",[17,89980,89981,89987,89991],{"__ignoreMap":96},[100,89982,89983,89985],{"class":102,"line":103},[100,89984,9259],{"class":145},[100,89986,9262],{"class":106},[100,89988,89989],{"class":102,"line":135},[100,89990,139],{"emptyLinePlaceholder":138},[100,89992,89993,89995,89997,89999,90001,90003,90005,90007],{"class":102,"line":142},[100,89994,372],{"class":114},[100,89996,170],{"class":118},[100,89998,9271],{"class":178},[100,90000,290],{"class":118},[100,90002,9276],{"class":178},[100,90004,170],{"class":118},[100,90006,356],{"class":122},[100,90008,182],{"class":118},[14,90010,218],{},[91,90012,90014],{"className":93,"code":90013,"language":95,"meta":96,"style":96},"3.1622776601683795\n",[17,90015,90016],{"__ignoreMap":96},[100,90017,90018],{"class":102,"line":103},[100,90019,90013],{"class":122},[675,90021,89973,90023],{"id":90022},"example-with-random",[17,90024,9411],{},[91,90026,90028],{"className":93,"code":90027,"language":95,"meta":96,"style":96},"import random\n\nprint(random.randint(1, 5))\n",[17,90029,90030,90036,90040],{"__ignoreMap":96},[100,90031,90032,90034],{"class":102,"line":103},[100,90033,9259],{"class":145},[100,90035,9486],{"class":106},[100,90037,90038],{"class":102,"line":135},[100,90039,139],{"emptyLinePlaceholder":138},[100,90041,90042,90044,90046,90048,90050,90052,90054,90056,90058,90060],{"class":102,"line":142},[100,90043,372],{"class":114},[100,90045,170],{"class":118},[100,90047,9411],{"class":178},[100,90049,290],{"class":118},[100,90051,9737],{"class":178},[100,90053,170],{"class":118},[100,90055,123],{"class":122},[100,90057,126],{"class":118},[100,90059,31164],{"class":122},[100,90061,182],{"class":118},[14,90063,52521],{},[91,90065,90066],{"className":93,"code":1500,"language":95,"meta":96,"style":96},[17,90067,90068],{"__ignoreMap":96},[100,90069,90070],{"class":102,"line":103},[100,90071,1500],{"class":122},[675,90073,89973,90075],{"id":90074},"example-with-json",[17,90076,9997],{},[91,90078,90080],{"className":93,"code":90079,"language":95,"meta":96,"style":96},"import json\n\ntext = '{\"name\": \"Ana\"}'\ndata = json.loads(text)\nprint(data)\n",[17,90081,90082,90088,90092,90104,90122],{"__ignoreMap":96},[100,90083,90084,90086],{"class":102,"line":103},[100,90085,9259],{"class":145},[100,90087,9984],{"class":106},[100,90089,90090],{"class":102,"line":135},[100,90091,139],{"emptyLinePlaceholder":138},[100,90093,90094,90096,90098,90100,90102],{"class":102,"line":142},[100,90095,4331],{"class":106},[100,90097,111],{"class":110},[100,90099,1274],{"class":205},[100,90101,15144],{"class":209},[100,90103,3925],{"class":205},[100,90105,90106,90108,90110,90112,90114,90116,90118,90120],{"class":102,"line":152},[100,90107,7909],{"class":106},[100,90109,111],{"class":110},[100,90111,15213],{"class":106},[100,90113,290],{"class":118},[100,90115,15218],{"class":178},[100,90117,170],{"class":118},[100,90119,4468],{"class":178},[100,90121,215],{"class":118},[100,90123,90124,90126,90128,90130],{"class":102,"line":164},[100,90125,372],{"class":114},[100,90127,170],{"class":118},[100,90129,7934],{"class":178},[100,90131,215],{"class":118},[14,90133,218],{},[91,90135,90137],{"className":93,"code":90136,"language":95,"meta":96,"style":96},"{'name': 'Ana'}\n",[17,90138,90139],{"__ignoreMap":96},[100,90140,90141,90143,90145,90147,90149,90151,90153,90155,90157],{"class":102,"line":103},[100,90142,16490],{"class":118},[100,90144,1280],{"class":205},[100,90146,2853],{"class":209},[100,90148,1280],{"class":205},[100,90150,89],{"class":118},[100,90152,1274],{"class":205},[100,90154,34819],{"class":209},[100,90156,1280],{"class":205},[100,90158,12093],{"class":118},[14,90160,90161,90162,90166,90167,3178,90169,290],{},"If you are not sure what a module contains, you can inspect it with ",[295,90163,90164],{"href":4562},[17,90165,4565],{}," or read a module overview such as the ",[295,90168,59942],{"href":59941},[295,90170,90172],{"href":90171},"\u002Fstandard-library\u002Fpython-json-module-overview\u002F","Python json module overview",[77,90174,90176],{"id":90175},"fix-2-import-the-function-directly","Fix 2: Import the function directly",[14,90178,90179],{},"If you only need one function, you can import that function directly.",[91,90181,90183],{"className":93,"code":90182,"language":95,"meta":96,"style":96},"from math import sqrt\n\nprint(sqrt(10))\n",[17,90184,90185,90195,90199],{"__ignoreMap":96},[100,90186,90187,90189,90191,90193],{"class":102,"line":103},[100,90188,10201],{"class":145},[100,90190,10204],{"class":106},[100,90192,9259],{"class":145},[100,90194,10209],{"class":106},[100,90196,90197],{"class":102,"line":135},[100,90198,139],{"emptyLinePlaceholder":138},[100,90200,90201,90203,90205,90207,90209,90211],{"class":102,"line":142},[100,90202,372],{"class":114},[100,90204,170],{"class":118},[100,90206,9276],{"class":178},[100,90208,170],{"class":118},[100,90210,356],{"class":122},[100,90212,182],{"class":118},[14,90214,218],{},[91,90216,90217],{"className":93,"code":90013,"language":95,"meta":96,"style":96},[17,90218,90219],{"__ignoreMap":96},[100,90220,90221],{"class":102,"line":103},[100,90222,90013],{"class":122},[14,90224,2925,90225,80359,90227,90230],{},[17,90226,9276],{},[17,90228,90229],{},"sqrt(10)"," is valid.",[14,90232,90233],{},"Compare these two styles:",[91,90235,90237],{"className":93,"code":90236,"language":95,"meta":96,"style":96},"import math\nprint(math.sqrt(10))\n",[17,90238,90239,90245],{"__ignoreMap":96},[100,90240,90241,90243],{"class":102,"line":103},[100,90242,9259],{"class":145},[100,90244,9262],{"class":106},[100,90246,90247,90249,90251,90253,90255,90257,90259,90261],{"class":102,"line":135},[100,90248,372],{"class":114},[100,90250,170],{"class":118},[100,90252,9271],{"class":178},[100,90254,290],{"class":118},[100,90256,9276],{"class":178},[100,90258,170],{"class":118},[100,90260,356],{"class":122},[100,90262,182],{"class":118},[91,90264,90266],{"className":93,"code":90265,"language":95,"meta":96,"style":96},"from math import sqrt\nprint(sqrt(10))\n",[17,90267,90268,90278],{"__ignoreMap":96},[100,90269,90270,90272,90274,90276],{"class":102,"line":103},[100,90271,10201],{"class":145},[100,90273,10204],{"class":106},[100,90275,9259],{"class":145},[100,90277,10209],{"class":106},[100,90279,90280,90282,90284,90286,90288,90290],{"class":102,"line":135},[100,90281,372],{"class":114},[100,90283,170],{"class":118},[100,90285,9276],{"class":178},[100,90287,170],{"class":118},[100,90289,356],{"class":122},[100,90291,182],{"class":118},[14,90293,90294,90295,90297],{},"Both are correct. The important part is that you call the ",[22,90296,82454],{},", not the module.",[14,90299,90300,90301,290],{},"If imports still feel confusing, read ",[295,90302,28180],{"href":25158},[77,90304,90306],{"id":90305},"fix-3-check-for-name-collisions","Fix 3: Check for name collisions",[14,90308,90309],{},"Sometimes the problem is not your function call. The problem is that a name points to a module when you did not expect it to.",[675,90311,90313],{"id":90312},"case-1-reusing-a-name","Case 1: Reusing a name",[91,90315,90317],{"className":93,"code":90316,"language":95,"meta":96,"style":96},"import math\n\n# math is the module here\nprint(type(math))\n",[17,90318,90319,90325,90329,90334],{"__ignoreMap":96},[100,90320,90321,90323],{"class":102,"line":103},[100,90322,9259],{"class":145},[100,90324,9262],{"class":106},[100,90326,90327],{"class":102,"line":135},[100,90328,139],{"emptyLinePlaceholder":138},[100,90330,90331],{"class":102,"line":142},[100,90332,90333],{"class":414},"# math is the module here\n",[100,90335,90336,90338,90340,90342,90344,90346],{"class":102,"line":152},[100,90337,372],{"class":114},[100,90339,170],{"class":118},[100,90341,1250],{"class":191},[100,90343,170],{"class":118},[100,90345,9271],{"class":178},[100,90347,182],{"class":118},[14,90349,90350,90351,90353],{},"If you thought ",[17,90352,9271],{}," was a function, the import statement shows otherwise.",[675,90355,90357],{"id":90356},"case-2-your-file-name-shadows-a-real-module","Case 2: Your file name shadows a real module",[14,90359,90360],{},"This is a very common beginner problem.",[14,90362,26690,90363,90365,90366,9918],{},[17,90364,9846],{},", this can interfere with importing Python's real ",[17,90367,9411],{},[14,90369,90370],{},"A file structure like this can cause trouble:",[91,90372,90374],{"className":93,"code":90373,"language":95,"meta":96,"style":96},"random.py\nmain.py\n",[17,90375,90376,90384],{"__ignoreMap":96},[100,90377,90378,90380,90382],{"class":102,"line":103},[100,90379,9411],{"class":106},[100,90381,290],{"class":118},[100,90383,2495],{"class":2494},[100,90385,90386,90389,90391],{"class":102,"line":135},[100,90387,90388],{"class":106},"main",[100,90390,290],{"class":118},[100,90392,2495],{"class":2494},[14,90394,90395,90396,89],{},"And in ",[17,90397,21078],{},[91,90399,90401],{"className":93,"code":90400,"language":95,"meta":96,"style":96},"import random\n\nprint(random(1, 5))\n",[17,90402,90403,90409,90413],{"__ignoreMap":96},[100,90404,90405,90407],{"class":102,"line":103},[100,90406,9259],{"class":145},[100,90408,9486],{"class":106},[100,90410,90411],{"class":102,"line":135},[100,90412,139],{"emptyLinePlaceholder":138},[100,90414,90415,90417,90419,90421,90423,90425,90427,90429],{"class":102,"line":142},[100,90416,372],{"class":114},[100,90418,170],{"class":118},[100,90420,9411],{"class":178},[100,90422,170],{"class":118},[100,90424,123],{"class":122},[100,90426,126],{"class":118},[100,90428,31164],{"class":122},[100,90430,182],{"class":118},[14,90432,90433],{},"This is wrong for two reasons:",[40,90435,90436,90441],{},[43,90437,90438,90440],{},[17,90439,9411],{}," is being called like a function",[43,90442,90443],{},"your local file name may be shadowing the real module",[14,90445,90446],{},"A better fix is:",[40,90448,90449,90455,90461],{},[43,90450,90451,90452],{},"rename your file to something else, like ",[17,90453,90454],{},"my_random_tools.py",[43,90456,90457,90458,90460],{},"delete any ",[17,90459,10130],{}," folder if needed",[43,90462,90463],{},"import the real module again",[14,90465,90466],{},"Then use the correct function:",[91,90468,90469],{"className":93,"code":90027,"language":95,"meta":96,"style":96},[17,90470,90471,90477,90481],{"__ignoreMap":96},[100,90472,90473,90475],{"class":102,"line":103},[100,90474,9259],{"class":145},[100,90476,9486],{"class":106},[100,90478,90479],{"class":102,"line":135},[100,90480,139],{"emptyLinePlaceholder":138},[100,90482,90483,90485,90487,90489,90491,90493,90495,90497,90499,90501],{"class":102,"line":142},[100,90484,372],{"class":114},[100,90486,170],{"class":118},[100,90488,9411],{"class":178},[100,90490,290],{"class":118},[100,90492,9737],{"class":178},[100,90494,170],{"class":118},[100,90496,123],{"class":122},[100,90498,126],{"class":118},[100,90500,31164],{"class":122},[100,90502,182],{"class":118},[14,90504,90505,90506,290],{},"If Python cannot import the module you expect, you may also want to read ",[295,90507,9961],{"href":26001},[77,90509,90511],{"id":90510},"common-modules-beginners-misuse-this-way","Common modules beginners misuse this way",[14,90513,90514],{},"These modules are often called by mistake:",[40,90516,90517,90521,90525,90529,90534],{},[43,90518,90519],{},[17,90520,9271],{},[43,90522,90523],{},[17,90524,9411],{},[43,90526,90527],{},[17,90528,9997],{},[43,90530,90531],{},[17,90532,90533],{},"datetime",[43,90535,90536],{},[17,90537,18035],{},[14,90539,90540],{},"Examples of wrong code:",[91,90542,90544],{"className":93,"code":90543,"language":95,"meta":96,"style":96},"import math\nmath(5)\n",[17,90545,90546,90552],{"__ignoreMap":96},[100,90547,90548,90550],{"class":102,"line":103},[100,90549,9259],{"class":145},[100,90551,9262],{"class":106},[100,90553,90554,90556,90558,90560],{"class":102,"line":135},[100,90555,9271],{"class":178},[100,90557,170],{"class":118},[100,90559,3557],{"class":122},[100,90561,215],{"class":118},[91,90563,90565],{"className":93,"code":90564,"language":95,"meta":96,"style":96},"import random\nrandom(1, 10)\n",[17,90566,90567,90573],{"__ignoreMap":96},[100,90568,90569,90571],{"class":102,"line":103},[100,90570,9259],{"class":145},[100,90572,9486],{"class":106},[100,90574,90575,90577,90579,90581,90583,90585],{"class":102,"line":135},[100,90576,9411],{"class":178},[100,90578,170],{"class":118},[100,90580,123],{"class":122},[100,90582,126],{"class":118},[100,90584,6550],{"class":122},[100,90586,215],{"class":118},[91,90588,90590],{"className":93,"code":90589,"language":95,"meta":96,"style":96},"import json\njson('{\"a\": 1}')\n",[17,90591,90592,90598],{"__ignoreMap":96},[100,90593,90594,90596],{"class":102,"line":103},[100,90595,9259],{"class":145},[100,90597,9984],{"class":106},[100,90599,90600,90602,90604,90606,90609,90611],{"class":102,"line":135},[100,90601,9997],{"class":178},[100,90603,170],{"class":118},[100,90605,1280],{"class":205},[100,90607,90608],{"class":209},"{\"a\": 1}",[100,90610,1280],{"class":205},[100,90612,215],{"class":118},[14,90614,90615],{},"Correct versions:",[91,90617,90619],{"className":93,"code":90618,"language":95,"meta":96,"style":96},"import math\nprint(math.sqrt(5))\n",[17,90620,90621,90627],{"__ignoreMap":96},[100,90622,90623,90625],{"class":102,"line":103},[100,90624,9259],{"class":145},[100,90626,9262],{"class":106},[100,90628,90629,90631,90633,90635,90637,90639,90641,90643],{"class":102,"line":135},[100,90630,372],{"class":114},[100,90632,170],{"class":118},[100,90634,9271],{"class":178},[100,90636,290],{"class":118},[100,90638,9276],{"class":178},[100,90640,170],{"class":118},[100,90642,3557],{"class":122},[100,90644,182],{"class":118},[91,90646,90648],{"className":93,"code":90647,"language":95,"meta":96,"style":96},"import random\nprint(random.randint(1, 10))\n",[17,90649,90650,90656],{"__ignoreMap":96},[100,90651,90652,90654],{"class":102,"line":103},[100,90653,9259],{"class":145},[100,90655,9486],{"class":106},[100,90657,90658,90660,90662,90664,90666,90668,90670,90672,90674,90676],{"class":102,"line":135},[100,90659,372],{"class":114},[100,90661,170],{"class":118},[100,90663,9411],{"class":178},[100,90665,290],{"class":118},[100,90667,9737],{"class":178},[100,90669,170],{"class":118},[100,90671,123],{"class":122},[100,90673,126],{"class":118},[100,90675,6550],{"class":122},[100,90677,182],{"class":118},[91,90679,90681],{"className":93,"code":90680,"language":95,"meta":96,"style":96},"import json\nprint(json.loads('{\"a\": 1}'))\n",[17,90682,90683,90689],{"__ignoreMap":96},[100,90684,90685,90687],{"class":102,"line":103},[100,90686,9259],{"class":145},[100,90688,9984],{"class":106},[100,90690,90691,90693,90695,90697,90699,90701,90703,90705,90707,90709],{"class":102,"line":135},[100,90692,372],{"class":114},[100,90694,170],{"class":118},[100,90696,9997],{"class":178},[100,90698,290],{"class":118},[100,90700,15218],{"class":178},[100,90702,170],{"class":118},[100,90704,1280],{"class":205},[100,90706,90608],{"class":209},[100,90708,1280],{"class":205},[100,90710,182],{"class":118},[14,90712,90713,90714,290],{},"For more examples, see the ",[295,90715,90717],{"href":90716},"\u002Fstandard-library\u002Fpython-random-module-overview\u002F","Python random module overview",[77,90719,89372],{"id":89371},[14,90721,90722],{},"When you see this error, use this checklist.",[675,90724,90726],{"id":90725},"_1-find-the-name-directly-before-the-parentheses","1. Find the name directly before the parentheses",[14,90728,90729],{},"In code like this:",[91,90731,90733],{"className":93,"code":90732,"language":95,"meta":96,"style":96},"math(10)\n",[17,90734,90735],{"__ignoreMap":96},[100,90736,90737,90739,90741,90743],{"class":102,"line":103},[100,90738,9271],{"class":178},[100,90740,170],{"class":118},[100,90742,356],{"class":122},[100,90744,215],{"class":118},[14,90746,90747,90748,290],{},"The name before the parentheses is ",[17,90749,9271],{},[14,90751,90752],{},"That is the object Python is trying to call.",[675,90754,90756],{"id":90755},"_2-check-how-that-name-was-imported","2. Check how that name was imported",[14,90758,90759],{},"Look for lines like:",[91,90761,90763],{"className":93,"code":90762,"language":95,"meta":96,"style":96},"import math\n",[17,90764,90765],{"__ignoreMap":96},[100,90766,90767,90769],{"class":102,"line":103},[100,90768,9259],{"class":145},[100,90770,9262],{"class":106},[14,90772,69359],{},[91,90774,90775],{"className":93,"code":26467,"language":95,"meta":96,"style":96},[17,90776,90777],{"__ignoreMap":96},[100,90778,90779,90781,90783,90785],{"class":102,"line":103},[100,90780,10201],{"class":145},[100,90782,10204],{"class":106},[100,90784,9259],{"class":145},[100,90786,10209],{"class":106},[14,90788,90789],{},"These two imports create different names.",[40,90791,90792,90799],{},[43,90793,90794,90796,90797],{},[17,90795,26216],{}," gives you the module name ",[17,90798,9271],{},[43,90800,90801,90804,90805],{},[17,90802,90803],{},"from math import sqrt"," gives you the function name ",[17,90806,9276],{},[675,90808,90810],{"id":90809},"_3-print-the-type","3. Print the type",[91,90812,90814],{"className":93,"code":90813,"language":95,"meta":96,"style":96},"import math\n\nprint(type(math))\nprint(math)\n",[17,90815,90816,90822,90826,90840],{"__ignoreMap":96},[100,90817,90818,90820],{"class":102,"line":103},[100,90819,9259],{"class":145},[100,90821,9262],{"class":106},[100,90823,90824],{"class":102,"line":135},[100,90825,139],{"emptyLinePlaceholder":138},[100,90827,90828,90830,90832,90834,90836,90838],{"class":102,"line":142},[100,90829,372],{"class":114},[100,90831,170],{"class":118},[100,90833,1250],{"class":191},[100,90835,170],{"class":118},[100,90837,9271],{"class":178},[100,90839,182],{"class":118},[100,90841,90842,90844,90846,90848],{"class":102,"line":152},[100,90843,372],{"class":114},[100,90845,170],{"class":118},[100,90847,9271],{"class":178},[100,90849,215],{"class":118},[14,90851,90852],{},"Possible output:",[91,90854,90856],{"className":93,"code":90855,"language":95,"meta":96,"style":96},"\u003Cclass 'module'>\n\u003Cmodule 'math' from '...'>\n",[17,90857,90858,90872],{"__ignoreMap":96},[100,90859,90860,90862,90864,90866,90868,90870],{"class":102,"line":103},[100,90861,2782],{"class":110},[100,90863,3417],{"class":1077},[100,90865,1274],{"class":205},[100,90867,1977],{"class":209},[100,90869,1280],{"class":205},[100,90871,1980],{"class":110},[100,90873,90874,90876,90878,90880,90882,90884,90886,90888,90890,90892],{"class":102,"line":135},[100,90875,2782],{"class":110},[100,90877,10014],{"class":106},[100,90879,1280],{"class":205},[100,90881,9271],{"class":209},[100,90883,1280],{"class":205},[100,90885,10023],{"class":145},[100,90887,1274],{"class":205},[100,90889,83565],{"class":209},[100,90891,1280],{"class":205},[100,90893,1980],{"class":110},[14,90895,90896,90897,90899],{},"If it says ",[17,90898,1977],{},", then you know why calling it fails.",[675,90901,90903],{"id":90902},"_4-see-what-the-module-contains","4. See what the module contains",[91,90905,90906],{"className":93,"code":9574,"language":95,"meta":96,"style":96},[17,90907,90908,90914,90918],{"__ignoreMap":96},[100,90909,90910,90912],{"class":102,"line":103},[100,90911,9259],{"class":145},[100,90913,9262],{"class":106},[100,90915,90916],{"class":102,"line":135},[100,90917,139],{"emptyLinePlaceholder":138},[100,90919,90920,90922,90924,90926,90928,90930],{"class":102,"line":142},[100,90921,372],{"class":114},[100,90923,170],{"class":118},[100,90925,3822],{"class":114},[100,90927,170],{"class":118},[100,90929,9271],{"class":178},[100,90931,182],{"class":118},[14,90933,90934,90935,5870,90937,5629,90940,290],{},"This shows the names inside the module, such as ",[17,90936,9276],{},[17,90938,90939],{},"ceil",[17,90941,90942],{},"floor",[675,90944,90946],{"id":90945},"_5-call-a-function-inside-the-module-instead","5. Call a function inside the module instead",[91,90948,90949],{"className":93,"code":10151,"language":95,"meta":96,"style":96},[17,90950,90951,90957,90961],{"__ignoreMap":96},[100,90952,90953,90955],{"class":102,"line":103},[100,90954,9259],{"class":145},[100,90956,9262],{"class":106},[100,90958,90959],{"class":102,"line":135},[100,90960,139],{"emptyLinePlaceholder":138},[100,90962,90963,90965,90967,90969,90971,90973,90975,90977],{"class":102,"line":142},[100,90964,372],{"class":114},[100,90966,170],{"class":118},[100,90968,9271],{"class":178},[100,90970,290],{"class":118},[100,90972,9276],{"class":178},[100,90974,170],{"class":118},[100,90976,10180],{"class":122},[100,90978,182],{"class":118},[675,90980,90982],{"id":90981},"_6-check-for-local-files-that-shadow-real-modules","6. Check for local files that shadow real modules",[14,90984,90985,90986,5870,90988,5870,90990,5894,90992,90995],{},"If you have files named ",[17,90987,9851],{},[17,90989,9856],{},[17,90991,9846],{},[17,90993,90994],{},"datetime.py",", rename them.",[14,90997,90998],{},"You can also inspect Python's import path:",[91,91000,91002],{"className":93,"code":91001,"language":95,"meta":96,"style":96},"import sys\nprint(sys.path)\n",[17,91003,91004,91010],{"__ignoreMap":96},[100,91005,91006,91008],{"class":102,"line":103},[100,91007,9259],{"class":145},[100,91009,26131],{"class":106},[100,91011,91012,91014,91016,91018,91020,91022],{"class":102,"line":135},[100,91013,372],{"class":114},[100,91015,170],{"class":118},[100,91017,26140],{"class":178},[100,91019,290],{"class":118},[100,91021,19017],{"class":2494},[100,91023,215],{"class":118},[14,91025,91026],{},"That helps when Python is importing from a place you did not expect.",[77,91028,91030],{"id":91029},"how-this-differs-from-similar-errors","How this differs from similar errors",[14,91032,91033,91034,290],{},"This error is specific: it means ",[22,91035,91036],{},"the module itself was called",[675,91038,91040,91041],{"id":91039},"different-from-typeerror-list-object-is-not-callable","Different from ",[17,91042,86914],{},[14,91044,91045],{},"That error means you called a list like a function.",[14,91047,1844],{},[91,91049,91051],{"className":93,"code":91050,"language":95,"meta":96,"style":96},"numbers = [1, 2, 3]\nnumbers(0)\n",[17,91052,91053,91073],{"__ignoreMap":96},[100,91054,91055,91057,91059,91061,91063,91065,91067,91069,91071],{"class":102,"line":103},[100,91056,107],{"class":106},[100,91058,111],{"class":110},[100,91060,594],{"class":118},[100,91062,123],{"class":122},[100,91064,126],{"class":118},[100,91066,129],{"class":122},[100,91068,126],{"class":118},[100,91070,1405],{"class":122},[100,91072,603],{"class":118},[100,91074,91075,91077,91079,91081],{"class":102,"line":135},[100,91076,179],{"class":178},[100,91078,170],{"class":118},[100,91080,2279],{"class":122},[100,91082,215],{"class":118},[14,91084,91085,91086,290],{},"That is a different problem. See ",[295,91087,86914],{"href":80712},[675,91089,91040,91091],{"id":91090},"different-from-attributeerror-module-has-no-attribute",[17,91092,9246],{},[14,91094,91095],{},"That error means the module exists, but the name after the dot is wrong.",[14,91097,1844],{},[91,91099,91101],{"className":93,"code":91100,"language":95,"meta":96,"style":96},"import math\nprint(math.square_root(4))\n",[17,91102,91103,91109],{"__ignoreMap":96},[100,91104,91105,91107],{"class":102,"line":103},[100,91106,9259],{"class":145},[100,91108,9262],{"class":106},[100,91110,91111,91113,91115,91117,91119,91121,91123,91125],{"class":102,"line":135},[100,91112,372],{"class":114},[100,91114,170],{"class":118},[100,91116,9271],{"class":178},[100,91118,290],{"class":118},[100,91120,26350],{"class":178},[100,91122,170],{"class":118},[100,91124,8322],{"class":122},[100,91126,182],{"class":118},[14,91128,91129,91130,91132],{},"Here, Python found the module, but ",[17,91131,26350],{}," is not a valid attribute.",[14,91134,91135,91136,290],{},"That is different from calling the module itself. See ",[295,91137,9246],{"href":5541},[77,91139,1344],{"id":1343},[14,91141,91142],{},"This error usually comes from one of these mistakes:",[40,91144,91145,91153,91156,91159,91162],{},[43,91146,1357,91147,88203,91150],{},[17,91148,91149],{},"import module_name",[17,91151,91152],{},"module_name()",[43,91154,91155],{},"Forgetting the function name after the module name",[43,91157,91158],{},"Confusing a module with a class or function",[43,91160,91161],{},"Importing the module when you meant to import a function",[43,91163,91164],{},"Naming your own file the same as a module and creating import confusion",[77,91166,20239],{"id":20238},[14,91168,91169],{},"These commands can help you confirm what a name refers to:",[91,91171,91173],{"className":93,"code":91172,"language":95,"meta":96,"style":96},"print(type(math))\nprint(math)\nprint(dir(math))\nimport sys; print(sys.path)\n",[17,91174,91175,91189,91199,91213],{"__ignoreMap":96},[100,91176,91177,91179,91181,91183,91185,91187],{"class":102,"line":103},[100,91178,372],{"class":114},[100,91180,170],{"class":118},[100,91182,1250],{"class":191},[100,91184,170],{"class":118},[100,91186,9271],{"class":178},[100,91188,182],{"class":118},[100,91190,91191,91193,91195,91197],{"class":102,"line":135},[100,91192,372],{"class":114},[100,91194,170],{"class":118},[100,91196,9271],{"class":178},[100,91198,215],{"class":118},[100,91200,91201,91203,91205,91207,91209,91211],{"class":102,"line":142},[100,91202,372],{"class":114},[100,91204,170],{"class":118},[100,91206,3822],{"class":114},[100,91208,170],{"class":118},[100,91210,9271],{"class":178},[100,91212,182],{"class":118},[100,91214,91215,91217,91220,91222,91224,91226,91228,91230],{"class":102,"line":152},[100,91216,9259],{"class":145},[100,91218,91219],{"class":106}," sys; ",[100,91221,372],{"class":114},[100,91223,170],{"class":118},[100,91225,26140],{"class":178},[100,91227,290],{"class":118},[100,91229,19017],{"class":2494},[100,91231,215],{"class":118},[14,91233,91234],{},"Use them when you are not sure whether something is:",[40,91236,91237,91240,91242,91244],{},[43,91238,91239],{},"a module",[43,91241,9353],{},[43,91243,9356],{},[43,91245,91246],{},"a local file being imported by mistake",[77,91248,1514],{"id":1513},[675,91250,91252],{"id":91251},"why-does-python-say-a-module-is-not-callable","Why does Python say a module is not callable?",[14,91254,91255],{},"Because you used parentheses on a module name, but only functions, classes, and some other objects can be called.",[675,91257,91259],{"id":91258},"how-do-i-know-if-something-is-a-module","How do I know if something is a module?",[14,91261,241,91262,3178,91265,91268],{},[17,91263,91264],{},"type(name)",[17,91266,91267],{},"print(name)",". A module often appears like this:",[91,91270,91272],{"className":93,"code":91271,"language":95,"meta":96,"style":96},"\u003Cmodule 'math' ...>\n",[17,91273,91274],{"__ignoreMap":96},[100,91275,91276,91278,91280,91282,91284,91286,91288],{"class":102,"line":103},[100,91277,2782],{"class":110},[100,91279,10014],{"class":106},[100,91281,1280],{"class":205},[100,91283,9271],{"class":209},[100,91285,1280],{"class":205},[100,91287,88107],{"class":527},[100,91289,1980],{"class":110},[675,91291,91293],{"id":91292},"can-i-ever-call-a-module-directly","Can I ever call a module directly?",[14,91295,91296],{},"No. You call functions or classes inside the module, not the module itself.",[675,91298,91300],{"id":91299},"what-if-i-imported-the-wrong-thing","What if I imported the wrong thing?",[14,91302,91303],{},"Change the import statement.",[14,91305,91306,91307,91310],{},"For example, if you want to call ",[17,91308,91309],{},"sqrt()"," directly, use:",[91,91312,91313],{"className":93,"code":26359,"language":95,"meta":96,"style":96},[17,91314,91315,91325,91329],{"__ignoreMap":96},[100,91316,91317,91319,91321,91323],{"class":102,"line":103},[100,91318,10201],{"class":145},[100,91320,10204],{"class":106},[100,91322,9259],{"class":145},[100,91324,10209],{"class":106},[100,91326,91327],{"class":102,"line":135},[100,91328,139],{"emptyLinePlaceholder":138},[100,91330,91331,91333,91335,91337,91339,91341],{"class":102,"line":142},[100,91332,372],{"class":114},[100,91334,170],{"class":118},[100,91336,9276],{"class":178},[100,91338,170],{"class":118},[100,91340,9281],{"class":122},[100,91342,182],{"class":118},[675,91344,91346],{"id":91345},"why-does-this-happen-with-my-own-file-names","Why does this happen with my own file names?",[14,91348,91349],{},"If your file has the same name as a real module, Python may import your file instead of the standard library module. That can lead to confusing behavior and hard-to-read errors.",[77,91351,1554],{"id":1553},[40,91353,91354,91358,91364,91370,91374,91378,91382],{},[43,91355,91356],{},[295,91357,28750],{"href":25158},[43,91359,91360],{},[295,91361,1572,91362,9570],{"href":4427},[17,91363,4430],{},[43,91365,91366],{},[295,91367,1572,91368,9570],{"href":4562},[17,91369,4565],{},[43,91371,91372],{},[295,91373,59942],{"href":59941},[43,91375,91376],{},[295,91377,90717],{"href":90716},[43,91379,91380],{},[295,91381,90172],{"href":90171},[43,91383,91384],{},[295,91385,9246],{"href":5541},[14,91387,91388],{},"If you want to avoid this error in future code, start by checking the object's type, fixing the import, and learning how Python resolves module names during import.",[1589,91390,91391],{},"html pre.shiki code .sVHd0, html code.shiki .sVHd0{--shiki-light:#39ADB5;--shiki-light-font-style:italic;--shiki-default:#D73A49;--shiki-default-font-style:inherit;--shiki-dark:#F97583;--shiki-dark-font-style:inherit}html pre.shiki code .su5hD, html code.shiki .su5hD{--shiki-light:#90A4AE;--shiki-default:#24292E;--shiki-dark:#E1E4E8}html pre.shiki code .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 .smGrS, html code.shiki .smGrS{--shiki-light:#39ADB5;--shiki-default:#D73A49;--shiki-dark:#F97583}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 pre.shiki code .srdBf, html code.shiki .srdBf{--shiki-light:#F76D47;--shiki-default:#005CC5;--shiki-dark:#79B8FF}html pre.shiki code .sptTA, html code.shiki .sptTA{--shiki-light:#6182B8;--shiki-default:#005CC5;--shiki-dark:#79B8FF}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 .sbsja, html code.shiki .sbsja{--shiki-light:#9C3EDA;--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 .skxfh, html code.shiki .skxfh{--shiki-light:#E53935;--shiki-default:#24292E;--shiki-dark:#E1E4E8}html pre.shiki code .s_hVV, html code.shiki .s_hVV{--shiki-light:#90A4AE;--shiki-default:#005CC5;--shiki-dark:#79B8FF}",{"title":96,"searchDepth":135,"depth":135,"links":91393},[91394,91395,91396,91397,91405,91406,91410,91411,91419,91425,91426,91427,91434],{"id":79,"depth":135,"text":80},{"id":5880,"depth":135,"text":5881},{"id":24957,"depth":135,"text":24958},{"id":89962,"depth":135,"text":89963,"children":91398},[91399,91401,91403],{"id":89972,"depth":142,"text":91400},"Example with math",{"id":90022,"depth":142,"text":91402},"Example with random",{"id":90074,"depth":142,"text":91404},"Example with json",{"id":90175,"depth":135,"text":90176},{"id":90305,"depth":135,"text":90306,"children":91407},[91408,91409],{"id":90312,"depth":142,"text":90313},{"id":90356,"depth":142,"text":90357},{"id":90510,"depth":135,"text":90511},{"id":89371,"depth":135,"text":89372,"children":91412},[91413,91414,91415,91416,91417,91418],{"id":90725,"depth":142,"text":90726},{"id":90755,"depth":142,"text":90756},{"id":90809,"depth":142,"text":90810},{"id":90902,"depth":142,"text":90903},{"id":90945,"depth":142,"text":90946},{"id":90981,"depth":142,"text":90982},{"id":91029,"depth":135,"text":91030,"children":91420},[91421,91423],{"id":91039,"depth":142,"text":91422},"Different from TypeError: 'list' object is not callable",{"id":91090,"depth":142,"text":91424},"Different from AttributeError: module has no attribute",{"id":1343,"depth":135,"text":1344},{"id":20238,"depth":135,"text":20239},{"id":1513,"depth":135,"text":1514,"children":91428},[91429,91430,91431,91432,91433],{"id":91251,"depth":142,"text":91252},{"id":91258,"depth":142,"text":91259},{"id":91292,"depth":142,"text":91293},{"id":91299,"depth":142,"text":91300},{"id":91345,"depth":142,"text":91346},{"id":1553,"depth":135,"text":1554},"Master typeerror module object is not callable fix in our comprehensive Python beginner guide.",{},"\u002Ferrors\u002Ftypeerror-module-object-is-not-callable-fix",{"title":89672,"description":91435},"errors\u002Ftypeerror-module-object-is-not-callable-fix","RggJn92uR9X_pzv9-RlwWRe9CV8Wc1e0lKVwer1MqU4",{"id":91442,"title":79463,"body":91443,"description":93776,"extension":1623,"meta":93777,"navigation":138,"path":93778,"seo":93779,"stem":93780,"__hash__":93781},"content\u002Ferrors\u002Ftypeerror-nonetype-object-is-not-iterable-fix.md",{"type":7,"value":91444,"toc":93739},[91445,91450,91460,91462,91528,91536,91538,91550,91554,91565,91568,91602,91607,91628,91645,91660,91664,91667,91751,91753,91820,91822,91828,91837,91840,91855,91862,91865,91882,91886,91889,91972,91974,91993,91998,92000,92006,92008,92030,92033,92139,92142,92203,92207,92212,92239,92241,92280,92327,92354,92381,92385,92446,92453,92517,92522,92524,92527,92556,92560,92632,92638,92693,92699,92703,92747,92753,92757,92858,92864,92869,92872,92905,92907,92988,92992,93093,93095,93159,93167,93172,93176,93181,93183,93201,93204,93206,93271,93283,93285,93346,93348,93364,93369,93446,93448,93521,93525,93529,93532,93535,93552,93555,93558,93576,93582,93607,93610,93613,93618,93621,93628,93630,93632,93658,93660,93662,93672,93676,93682,93686,93691,93695,93698,93702,93714,93716,93736],[10,91446,74872,91448,7375],{"id":91447},"typeerror-nonetype-object-is-not-iterable-fix",[17,91449,76379],{},[14,91451,31738,91452,91456,91457,91459],{},[22,91453,91454],{},[17,91455,78632],{},". This page explains what it means, why it happens, how to find where ",[17,91458,930],{}," came from, and simple ways to fix it.",[77,91461,80],{"id":79},[91,91463,91465],{"className":93,"code":91464,"language":95,"meta":96,"style":96},"items = get_items()\n\nif items is None:\n    items = []\n\nfor item in items:\n    print(item)\n",[17,91466,91467,91477,91481,91493,91502,91506,91518],{"__ignoreMap":96},[100,91468,91469,91471,91473,91475],{"class":102,"line":103},[100,91470,3095],{"class":106},[100,91472,111],{"class":110},[100,91474,82945],{"class":178},[100,91476,4734],{"class":118},[100,91478,91479],{"class":102,"line":135},[100,91480,139],{"emptyLinePlaceholder":138},[100,91482,91483,91485,91487,91489,91491],{"class":102,"line":142},[100,91484,2736],{"class":145},[100,91486,81230],{"class":106},[100,91488,4918],{"class":110},[100,91490,866],{"class":158},[100,91492,149],{"class":118},[100,91494,91495,91498,91500],{"class":102,"line":152},[100,91496,91497],{"class":106},"    items ",[100,91499,111],{"class":110},[100,91501,33843],{"class":118},[100,91503,91504],{"class":102,"line":164},[100,91505,139],{"emptyLinePlaceholder":138},[100,91507,91508,91510,91512,91514,91516],{"class":102,"line":185},[100,91509,71],{"class":145},[100,91511,7568],{"class":106},[100,91513,617],{"class":145},[100,91515,12000],{"class":106},[100,91517,149],{"class":118},[100,91519,91520,91522,91524,91526],{"class":102,"line":197},[100,91521,200],{"class":114},[100,91523,170],{"class":118},[100,91525,7555],{"class":178},[100,91527,215],{"class":118},[14,91529,91530,91531,290],{},"Use this only when an empty list makes sense. The real fix is to find out ",[22,91532,91533,91534],{},"why the value is ",[17,91535,930],{},[77,91537,5881],{"id":5880},[14,91539,91540,91541,3178,91544,91547,91548,10873],{},"This error happens when Python expects something it can ",[22,91542,91543],{},"loop over",[22,91545,91546],{},"check membership in",", but it gets ",[17,91549,930],{},[14,91551,91552,24911],{},[17,91553,930],{},[40,91555,91556,91559,91562],{},[43,91557,91558],{},"no value",[43,91560,91561],{},"nothing was returned",[43,91563,91564],{},"the value is missing",[14,91566,91567],{},"This often happens in places like:",[40,91569,91570,91574,91578,91582,91586,91590,91594,91598],{},[43,91571,91572,75013],{},[17,91573,71],{},[43,91575,91576],{},[17,91577,76220],{},[43,91579,91580],{},[17,91581,76227],{},[43,91583,91584],{},[17,91585,82362],{},[43,91587,91588],{},[17,91589,82365],{},[43,91591,91592],{},[17,91593,82368],{},[43,91595,91596],{},[17,91597,82371],{},[43,91599,9421,91600,83136],{},[17,91601,617],{},[14,91603,91604,91605,89],{},"For example, all of these can fail if the value is ",[17,91606,930],{},[91,91608,91610],{"className":93,"code":91609,"language":95,"meta":96,"style":96},"for item in None:\n    pass\n",[17,91611,91612,91624],{"__ignoreMap":96},[100,91613,91614,91616,91618,91620,91622],{"class":102,"line":103},[100,91615,71],{"class":145},[100,91617,7568],{"class":106},[100,91619,617],{"class":145},[100,91621,866],{"class":158},[100,91623,149],{"class":118},[100,91625,91626],{"class":102,"line":135},[100,91627,16214],{"class":145},[91,91629,91631],{"className":93,"code":91630,"language":95,"meta":96,"style":96},"'a' in None\n",[17,91632,91633],{"__ignoreMap":96},[100,91634,91635,91637,91639,91641,91643],{"class":102,"line":103},[100,91636,1280],{"class":3553},[100,91638,295],{"class":3556},[100,91640,1280],{"class":3553},[100,91642,1971],{"class":110},[100,91644,4904],{"class":158},[91,91646,91648],{"className":93,"code":91647,"language":95,"meta":96,"style":96},"list(None)\n",[17,91649,91650],{"__ignoreMap":96},[100,91651,91652,91654,91656,91658],{"class":102,"line":103},[100,91653,1235],{"class":191},[100,91655,170],{"class":118},[100,91657,930],{"class":158},[100,91659,215],{"class":118},[77,91661,91663],{"id":91662},"a-simple-example-that-causes-the-error","A simple example that causes the error",[14,91665,91666],{},"A very common cause is a function that prints something but does not return anything.",[91,91668,91670],{"className":93,"code":91669,"language":95,"meta":96,"style":96},"def get_names():\n    print([\"Ana\", \"Ben\", \"Cara\"])\n\nnames = get_names()\n\nfor name in names:\n    print(name)\n",[17,91671,91672,91680,91711,91715,91725,91729,91741],{"__ignoreMap":96},[100,91673,91674,91676,91678],{"class":102,"line":103},[100,91675,1078],{"class":1077},[100,91677,75397],{"class":1081},[100,91679,1085],{"class":118},[100,91681,91682,91684,91686,91688,91690,91692,91694,91696,91698,91700,91702,91704,91707,91709],{"class":102,"line":135},[100,91683,200],{"class":114},[100,91685,119],{"class":118},[100,91687,206],{"class":205},[100,91689,34819],{"class":209},[100,91691,206],{"class":205},[100,91693,126],{"class":118},[100,91695,1708],{"class":205},[100,91697,83099],{"class":209},[100,91699,206],{"class":205},[100,91701,126],{"class":118},[100,91703,1708],{"class":205},[100,91705,91706],{"class":209},"Cara",[100,91708,206],{"class":205},[100,91710,132],{"class":118},[100,91712,91713],{"class":102,"line":142},[100,91714,139],{"emptyLinePlaceholder":138},[100,91716,91717,91719,91721,91723],{"class":102,"line":152},[100,91718,7789],{"class":106},[100,91720,111],{"class":110},[100,91722,75397],{"class":178},[100,91724,4734],{"class":118},[100,91726,91727],{"class":102,"line":164},[100,91728,139],{"emptyLinePlaceholder":138},[100,91730,91731,91733,91735,91737,91739],{"class":102,"line":185},[100,91732,71],{"class":145},[100,91734,4915],{"class":106},[100,91736,617],{"class":145},[100,91738,75276],{"class":106},[100,91740,149],{"class":118},[100,91742,91743,91745,91747,91749],{"class":102,"line":197},[100,91744,200],{"class":114},[100,91746,170],{"class":118},[100,91748,2853],{"class":178},[100,91750,215],{"class":118},[14,91752,218],{},[91,91754,91756],{"className":93,"code":91755,"language":95,"meta":96,"style":96},"['Ana', 'Ben', 'Cara']\nTraceback (most recent call last):\n  ...\nTypeError: 'NoneType' object is not iterable\n",[17,91757,91758,91786,91796,91800],{"__ignoreMap":96},[100,91759,91760,91762,91764,91766,91768,91770,91772,91774,91776,91778,91780,91782,91784],{"class":102,"line":103},[100,91761,3166],{"class":118},[100,91763,1280],{"class":205},[100,91765,34819],{"class":209},[100,91767,1280],{"class":205},[100,91769,126],{"class":118},[100,91771,1274],{"class":205},[100,91773,83099],{"class":209},[100,91775,1280],{"class":205},[100,91777,126],{"class":118},[100,91779,1274],{"class":205},[100,91781,91706],{"class":209},[100,91783,1280],{"class":205},[100,91785,603],{"class":118},[100,91787,91788,91790,91792,91794],{"class":102,"line":135},[100,91789,514],{"class":178},[100,91791,170],{"class":118},[100,91793,519],{"class":178},[100,91795,522],{"class":118},[100,91797,91798],{"class":102,"line":142},[100,91799,528],{"class":527},[100,91801,91802,91804,91806,91808,91810,91812,91814,91816,91818],{"class":102,"line":152},[100,91803,3538],{"class":191},[100,91805,89],{"class":118},[100,91807,1274],{"class":205},[100,91809,4665],{"class":209},[100,91811,1280],{"class":205},[100,91813,3914],{"class":191},[100,91815,16702],{"class":110},[100,91817,4921],{"class":110},[100,91819,75106],{"class":106},[675,91821,7741],{"id":7740},[14,91823,91824,91825,91827],{},"The function prints a list, but it does ",[22,91826,1059],{}," return one.",[14,91829,91830,91831,91833,91834,91836],{},"In Python, if a function has no ",[17,91832,1053],{}," statement, it returns ",[17,91835,930],{}," by default.",[14,91838,91839],{},"So this line:",[91,91841,91843],{"className":93,"code":91842,"language":95,"meta":96,"style":96},"names = get_names()\n",[17,91844,91845],{"__ignoreMap":96},[100,91846,91847,91849,91851,91853],{"class":102,"line":103},[100,91848,7789],{"class":106},[100,91850,111],{"class":110},[100,91852,75397],{"class":178},[100,91854,4734],{"class":118},[14,91856,91857,91858,681,91860,290],{},"stores ",[17,91859,930],{},[17,91861,7820],{},[14,91863,91864],{},"Then this line fails:",[91,91866,91868],{"className":93,"code":91867,"language":95,"meta":96,"style":96},"for name in names:\n",[17,91869,91870],{"__ignoreMap":96},[100,91871,91872,91874,91876,91878,91880],{"class":102,"line":103},[100,91873,71],{"class":145},[100,91875,4915],{"class":106},[100,91877,617],{"class":145},[100,91879,75276],{"class":106},[100,91881,149],{"class":118},[675,91883,91885],{"id":91884},"the-fix","The fix",[14,91887,91888],{},"Return the list from the function:",[91,91890,91892],{"className":93,"code":91891,"language":95,"meta":96,"style":96},"def get_names():\n    return [\"Ana\", \"Ben\", \"Cara\"]\n\nnames = get_names()\n\nfor name in names:\n    print(name)\n",[17,91893,91894,91902,91932,91936,91946,91950,91962],{"__ignoreMap":96},[100,91895,91896,91898,91900],{"class":102,"line":103},[100,91897,1078],{"class":1077},[100,91899,75397],{"class":1081},[100,91901,1085],{"class":118},[100,91903,91904,91906,91908,91910,91912,91914,91916,91918,91920,91922,91924,91926,91928,91930],{"class":102,"line":135},[100,91905,2552],{"class":145},[100,91907,594],{"class":118},[100,91909,206],{"class":205},[100,91911,34819],{"class":209},[100,91913,206],{"class":205},[100,91915,126],{"class":118},[100,91917,1708],{"class":205},[100,91919,83099],{"class":209},[100,91921,206],{"class":205},[100,91923,126],{"class":118},[100,91925,1708],{"class":205},[100,91927,91706],{"class":209},[100,91929,206],{"class":205},[100,91931,603],{"class":118},[100,91933,91934],{"class":102,"line":142},[100,91935,139],{"emptyLinePlaceholder":138},[100,91937,91938,91940,91942,91944],{"class":102,"line":152},[100,91939,7789],{"class":106},[100,91941,111],{"class":110},[100,91943,75397],{"class":178},[100,91945,4734],{"class":118},[100,91947,91948],{"class":102,"line":164},[100,91949,139],{"emptyLinePlaceholder":138},[100,91951,91952,91954,91956,91958,91960],{"class":102,"line":185},[100,91953,71],{"class":145},[100,91955,4915],{"class":106},[100,91957,617],{"class":145},[100,91959,75276],{"class":106},[100,91961,149],{"class":118},[100,91963,91964,91966,91968,91970],{"class":102,"line":197},[100,91965,200],{"class":114},[100,91967,170],{"class":118},[100,91969,2853],{"class":178},[100,91971,215],{"class":118},[14,91973,218],{},[91,91975,91977],{"className":93,"code":91976,"language":95,"meta":96,"style":96},"Ana\nBen\nCara\n",[17,91978,91979,91983,91988],{"__ignoreMap":96},[100,91980,91981],{"class":102,"line":103},[100,91982,15252],{"class":106},[100,91984,91985],{"class":102,"line":135},[100,91986,91987],{"class":106},"Ben\n",[100,91989,91990],{"class":102,"line":142},[100,91991,91992],{"class":106},"Cara\n",[14,91994,91995,91996,290],{},"If function return values are confusing, see ",[295,91997,11292],{"href":11291},[77,91999,25091],{"id":25090},[14,92001,92002,92003,92005],{},"This error usually means ",[17,92004,930],{}," appeared earlier in your code.",[14,92007,14889],{},[40,92009,92010,92013,92018,92021,92024],{},[43,92011,92012],{},"A function forgot to return a value.",[43,92014,10989,92015,92017],{},[17,92016,930],{}," earlier in the program.",[43,92019,92020],{},"A method was used incorrectly and its return value was assigned.",[43,92022,92023],{},"Data was missing from a file, API, or dictionary lookup.",[43,92025,92026,92027,92029],{},"One branch of an ",[17,92028,2736],{}," statement returns a list, but another branch returns nothing.",[14,92031,92032],{},"Here is an example of inconsistent returns:",[91,92034,92036],{"className":93,"code":92035,"language":95,"meta":96,"style":96},"def get_items(include):\n    if include:\n        return [\"a\", \"b\", \"c\"]\n    # no return here\n\nitems = get_items(False)\n\nfor item in items:\n    print(item)\n",[17,92037,92038,92051,92060,92090,92095,92099,92113,92117,92129],{"__ignoreMap":96},[100,92039,92040,92042,92044,92046,92049],{"class":102,"line":103},[100,92041,1078],{"class":1077},[100,92043,82945],{"class":1081},[100,92045,170],{"class":118},[100,92047,92048],{"class":2519},"include",[100,92050,522],{"class":118},[100,92052,92053,92055,92058],{"class":102,"line":135},[100,92054,2777],{"class":145},[100,92056,92057],{"class":106}," include",[100,92059,149],{"class":118},[100,92061,92062,92064,92066,92068,92070,92072,92074,92076,92078,92080,92082,92084,92086,92088],{"class":102,"line":142},[100,92063,46371],{"class":145},[100,92065,594],{"class":118},[100,92067,206],{"class":205},[100,92069,295],{"class":209},[100,92071,206],{"class":205},[100,92073,126],{"class":118},[100,92075,1708],{"class":205},[100,92077,4027],{"class":209},[100,92079,206],{"class":205},[100,92081,126],{"class":118},[100,92083,1708],{"class":205},[100,92085,4036],{"class":209},[100,92087,206],{"class":205},[100,92089,603],{"class":118},[100,92091,92092],{"class":102,"line":152},[100,92093,92094],{"class":414},"    # no return here\n",[100,92096,92097],{"class":102,"line":164},[100,92098,139],{"emptyLinePlaceholder":138},[100,92100,92101,92103,92105,92107,92109,92111],{"class":102,"line":185},[100,92102,3095],{"class":106},[100,92104,111],{"class":110},[100,92106,82945],{"class":178},[100,92108,170],{"class":118},[100,92110,1649],{"class":158},[100,92112,215],{"class":118},[100,92114,92115],{"class":102,"line":197},[100,92116,139],{"emptyLinePlaceholder":138},[100,92118,92119,92121,92123,92125,92127],{"class":102,"line":771},[100,92120,71],{"class":145},[100,92122,7568],{"class":106},[100,92124,617],{"class":145},[100,92126,12000],{"class":106},[100,92128,149],{"class":118},[100,92130,92131,92133,92135,92137],{"class":102,"line":787},[100,92132,200],{"class":114},[100,92134,170],{"class":118},[100,92136,7555],{"class":178},[100,92138,215],{"class":118},[14,92140,92141],{},"The fix is to make every branch return the same kind of value:",[91,92143,92145],{"className":93,"code":92144,"language":95,"meta":96,"style":96},"def get_items(include):\n    if include:\n        return [\"a\", \"b\", \"c\"]\n    return []\n",[17,92146,92147,92159,92167,92197],{"__ignoreMap":96},[100,92148,92149,92151,92153,92155,92157],{"class":102,"line":103},[100,92150,1078],{"class":1077},[100,92152,82945],{"class":1081},[100,92154,170],{"class":118},[100,92156,92048],{"class":2519},[100,92158,522],{"class":118},[100,92160,92161,92163,92165],{"class":102,"line":135},[100,92162,2777],{"class":145},[100,92164,92057],{"class":106},[100,92166,149],{"class":118},[100,92168,92169,92171,92173,92175,92177,92179,92181,92183,92185,92187,92189,92191,92193,92195],{"class":102,"line":142},[100,92170,46371],{"class":145},[100,92172,594],{"class":118},[100,92174,206],{"class":205},[100,92176,295],{"class":209},[100,92178,206],{"class":205},[100,92180,126],{"class":118},[100,92182,1708],{"class":205},[100,92184,4027],{"class":209},[100,92186,206],{"class":205},[100,92188,126],{"class":118},[100,92190,1708],{"class":205},[100,92192,4036],{"class":209},[100,92194,206],{"class":205},[100,92196,603],{"class":118},[100,92198,92199,92201],{"class":102,"line":152},[100,92200,2552],{"class":145},[100,92202,33843],{"class":118},[77,92204,92206],{"id":92205},"common-code-patterns-that-produce-this-error","Common code patterns that produce this error",[14,92208,92209,92210,89],{},"These patterns often lead to this error when the value is ",[17,92211,930],{},[40,92213,92214,92219,92224,92229,92234],{},[43,92215,92216],{},[17,92217,92218],{},"for item in result",[43,92220,92221],{},[17,92222,92223],{},"if 'a' in text",[43,92225,92226],{},[17,92227,92228],{},"list(value)",[43,92230,92231],{},[17,92232,92233],{},"sum(numbers)",[43,92235,92236],{},[17,92237,92238],{},"for key in data.get('items')",[14,92240,5500],{},[91,92242,92244],{"className":93,"code":92243,"language":95,"meta":96,"style":96},"result = None\n\nfor item in result:\n    print(item)\n",[17,92245,92246,92254,92258,92270],{"__ignoreMap":96},[100,92247,92248,92250,92252],{"class":102,"line":103},[100,92249,11556],{"class":106},[100,92251,111],{"class":110},[100,92253,4904],{"class":158},[100,92255,92256],{"class":102,"line":135},[100,92257,139],{"emptyLinePlaceholder":138},[100,92259,92260,92262,92264,92266,92268],{"class":102,"line":142},[100,92261,71],{"class":145},[100,92263,7568],{"class":106},[100,92265,617],{"class":145},[100,92267,75984],{"class":106},[100,92269,149],{"class":118},[100,92271,92272,92274,92276,92278],{"class":102,"line":152},[100,92273,200],{"class":114},[100,92275,170],{"class":118},[100,92277,7555],{"class":178},[100,92279,215],{"class":118},[91,92281,92283],{"className":93,"code":92282,"language":95,"meta":96,"style":96},"text = None\n\nif \"a\" in text:\n    print(\"found\")\n",[17,92284,92285,92293,92297,92313],{"__ignoreMap":96},[100,92286,92287,92289,92291],{"class":102,"line":103},[100,92288,4331],{"class":106},[100,92290,111],{"class":110},[100,92292,4904],{"class":158},[100,92294,92295],{"class":102,"line":135},[100,92296,139],{"emptyLinePlaceholder":138},[100,92298,92299,92301,92303,92305,92307,92309,92311],{"class":102,"line":142},[100,92300,2736],{"class":145},[100,92302,1708],{"class":205},[100,92304,295],{"class":209},[100,92306,206],{"class":205},[100,92308,1971],{"class":110},[100,92310,4350],{"class":106},[100,92312,149],{"class":118},[100,92314,92315,92317,92319,92321,92323,92325],{"class":102,"line":152},[100,92316,200],{"class":114},[100,92318,170],{"class":118},[100,92320,206],{"class":205},[100,92322,76247],{"class":209},[100,92324,206],{"class":205},[100,92326,215],{"class":118},[91,92328,92330],{"className":93,"code":92329,"language":95,"meta":96,"style":96},"value = None\nprint(list(value))\n",[17,92331,92332,92340],{"__ignoreMap":96},[100,92333,92334,92336,92338],{"class":102,"line":103},[100,92335,3318],{"class":106},[100,92337,111],{"class":110},[100,92339,4904],{"class":158},[100,92341,92342,92344,92346,92348,92350,92352],{"class":102,"line":135},[100,92343,372],{"class":114},[100,92345,170],{"class":118},[100,92347,1235],{"class":191},[100,92349,170],{"class":118},[100,92351,757],{"class":178},[100,92353,182],{"class":118},[91,92355,92357],{"className":93,"code":92356,"language":95,"meta":96,"style":96},"numbers = None\nprint(sum(numbers))\n",[17,92358,92359,92367],{"__ignoreMap":96},[100,92360,92361,92363,92365],{"class":102,"line":103},[100,92362,107],{"class":106},[100,92364,111],{"class":110},[100,92366,4904],{"class":158},[100,92368,92369,92371,92373,92375,92377,92379],{"class":102,"line":135},[100,92370,372],{"class":114},[100,92372,170],{"class":118},[100,92374,83295],{"class":114},[100,92376,170],{"class":118},[100,92378,179],{"class":178},[100,92380,182],{"class":118},[675,92382,92384],{"id":92383},"missing-dictionary-key-example","Missing dictionary key example",[91,92386,92388],{"className":93,"code":92387,"language":95,"meta":96,"style":96},"data = {}\nitems = data.get(\"items\")\n\nfor item in items:\n    print(item)\n",[17,92389,92390,92398,92420,92424,92436],{"__ignoreMap":96},[100,92391,92392,92394,92396],{"class":102,"line":103},[100,92393,7909],{"class":106},[100,92395,111],{"class":110},[100,92397,45623],{"class":118},[100,92399,92400,92402,92404,92406,92408,92410,92412,92414,92416,92418],{"class":102,"line":135},[100,92401,3095],{"class":106},[100,92403,111],{"class":110},[100,92405,12102],{"class":106},[100,92407,290],{"class":118},[100,92409,12412],{"class":178},[100,92411,170],{"class":118},[100,92413,206],{"class":205},[100,92415,3146],{"class":209},[100,92417,206],{"class":205},[100,92419,215],{"class":118},[100,92421,92422],{"class":102,"line":142},[100,92423,139],{"emptyLinePlaceholder":138},[100,92425,92426,92428,92430,92432,92434],{"class":102,"line":152},[100,92427,71],{"class":145},[100,92429,7568],{"class":106},[100,92431,617],{"class":145},[100,92433,12000],{"class":106},[100,92435,149],{"class":118},[100,92437,92438,92440,92442,92444],{"class":102,"line":164},[100,92439,200],{"class":114},[100,92441,170],{"class":118},[100,92443,7555],{"class":178},[100,92445,215],{"class":118},[14,92447,92448,551,92450,92452],{},[17,92449,11018],{},[17,92451,930],{}," by default when the key is missing. If you want a safe fallback, you can pass a default value:",[91,92454,92456],{"className":93,"code":92455,"language":95,"meta":96,"style":96},"data = {}\nitems = data.get(\"items\", [])\n\nfor item in items:\n    print(item)\n",[17,92457,92458,92466,92491,92495,92507],{"__ignoreMap":96},[100,92459,92460,92462,92464],{"class":102,"line":103},[100,92461,7909],{"class":106},[100,92463,111],{"class":110},[100,92465,45623],{"class":118},[100,92467,92468,92470,92472,92474,92476,92478,92480,92482,92484,92486,92488],{"class":102,"line":135},[100,92469,3095],{"class":106},[100,92471,111],{"class":110},[100,92473,12102],{"class":106},[100,92475,290],{"class":118},[100,92477,12412],{"class":178},[100,92479,170],{"class":118},[100,92481,206],{"class":205},[100,92483,3146],{"class":209},[100,92485,206],{"class":205},[100,92487,126],{"class":118},[100,92489,92490],{"class":118}," [])\n",[100,92492,92493],{"class":102,"line":142},[100,92494,139],{"emptyLinePlaceholder":138},[100,92496,92497,92499,92501,92503,92505],{"class":102,"line":152},[100,92498,71],{"class":145},[100,92500,7568],{"class":106},[100,92502,617],{"class":145},[100,92504,12000],{"class":106},[100,92506,149],{"class":118},[100,92508,92509,92511,92513,92515],{"class":102,"line":164},[100,92510,200],{"class":114},[100,92512,170],{"class":118},[100,92514,7555],{"class":178},[100,92516,215],{"class":118},[14,92518,92519,92520,290],{},"To learn more, see the ",[295,92521,12577],{"href":11015},[77,92523,6244],{"id":6243},[14,92525,92526],{},"Here are the main ways to fix this error:",[40,92528,92529,92535,92542,92548,92551],{},[43,92530,92531,92532,92534],{},"Add a ",[17,92533,1053],{}," statement to the function that should return a list or other iterable.",[43,92536,31359,92537,92539,92540,290],{},[17,92538,930],{}," before looping or using ",[17,92541,617],{},[43,92543,92544,92545,92547],{},"Provide a safe default like ",[17,92546,45218],{}," when that matches your program logic.",[43,92549,92550],{},"Make sure every branch of a function returns the same kind of value.",[43,92552,92553,92554,290],{},"Trace the variable backward to where it first became ",[17,92555,930],{},[675,92557,92559],{"id":92558},"fix-1-return-a-real-value","Fix 1: Return a real value",[91,92561,92563],{"className":93,"code":92562,"language":95,"meta":96,"style":96},"def load_numbers():\n    return [1, 2, 3]\n\nnumbers = load_numbers()\n\nfor number in numbers:\n    print(number)\n",[17,92564,92565,92574,92592,92596,92606,92610,92622],{"__ignoreMap":96},[100,92566,92567,92569,92572],{"class":102,"line":103},[100,92568,1078],{"class":1077},[100,92570,92571],{"class":1081}," load_numbers",[100,92573,1085],{"class":118},[100,92575,92576,92578,92580,92582,92584,92586,92588,92590],{"class":102,"line":135},[100,92577,2552],{"class":145},[100,92579,594],{"class":118},[100,92581,123],{"class":122},[100,92583,126],{"class":118},[100,92585,129],{"class":122},[100,92587,126],{"class":118},[100,92589,1405],{"class":122},[100,92591,603],{"class":118},[100,92593,92594],{"class":102,"line":142},[100,92595,139],{"emptyLinePlaceholder":138},[100,92597,92598,92600,92602,92604],{"class":102,"line":152},[100,92599,107],{"class":106},[100,92601,111],{"class":110},[100,92603,92571],{"class":178},[100,92605,4734],{"class":118},[100,92607,92608],{"class":102,"line":164},[100,92609,139],{"emptyLinePlaceholder":138},[100,92611,92612,92614,92616,92618,92620],{"class":102,"line":185},[100,92613,71],{"class":145},[100,92615,614],{"class":106},[100,92617,617],{"class":145},[100,92619,620],{"class":106},[100,92621,149],{"class":118},[100,92623,92624,92626,92628,92630],{"class":102,"line":197},[100,92625,200],{"class":114},[100,92627,170],{"class":118},[100,92629,631],{"class":178},[100,92631,215],{"class":118},[675,92633,92635,92636],{"id":92634},"fix-2-check-for-none","Fix 2: Check for ",[17,92637,930],{},[91,92639,92641],{"className":93,"code":92640,"language":95,"meta":96,"style":96},"items = get_items()\n\nif items is not None:\n    for item in items:\n        print(item)\n",[17,92642,92643,92653,92657,92671,92683],{"__ignoreMap":96},[100,92644,92645,92647,92649,92651],{"class":102,"line":103},[100,92646,3095],{"class":106},[100,92648,111],{"class":110},[100,92650,82945],{"class":178},[100,92652,4734],{"class":118},[100,92654,92655],{"class":102,"line":135},[100,92656,139],{"emptyLinePlaceholder":138},[100,92658,92659,92661,92663,92665,92667,92669],{"class":102,"line":142},[100,92660,2736],{"class":145},[100,92662,81230],{"class":106},[100,92664,4918],{"class":110},[100,92666,4921],{"class":110},[100,92668,866],{"class":158},[100,92670,149],{"class":118},[100,92672,92673,92675,92677,92679,92681],{"class":102,"line":152},[100,92674,51393],{"class":145},[100,92676,7568],{"class":106},[100,92678,617],{"class":145},[100,92680,12000],{"class":106},[100,92682,149],{"class":118},[100,92684,92685,92687,92689,92691],{"class":102,"line":164},[100,92686,167],{"class":114},[100,92688,170],{"class":118},[100,92690,7555],{"class":178},[100,92692,215],{"class":118},[14,92694,92695,92696,92698],{},"This is a good choice when ",[17,92697,930],{}," has a special meaning, such as “data not available”.",[675,92700,92702],{"id":92701},"fix-3-use-a-default-value","Fix 3: Use a default value",[91,92704,92706],{"className":93,"code":92705,"language":95,"meta":96,"style":96},"items = get_items()\n\nfor item in items or []:\n    print(item)\n",[17,92707,92708,92718,92722,92737],{"__ignoreMap":96},[100,92709,92710,92712,92714,92716],{"class":102,"line":103},[100,92711,3095],{"class":106},[100,92713,111],{"class":110},[100,92715,82945],{"class":178},[100,92717,4734],{"class":118},[100,92719,92720],{"class":102,"line":135},[100,92721,139],{"emptyLinePlaceholder":138},[100,92723,92724,92726,92728,92730,92732,92734],{"class":102,"line":142},[100,92725,71],{"class":145},[100,92727,7568],{"class":106},[100,92729,617],{"class":145},[100,92731,81230],{"class":106},[100,92733,11841],{"class":110},[100,92735,92736],{"class":118}," []:\n",[100,92738,92739,92741,92743,92745],{"class":102,"line":152},[100,92740,200],{"class":114},[100,92742,170],{"class":118},[100,92744,7555],{"class":178},[100,92746,215],{"class":118},[14,92748,92749,92750,92752],{},"This works when ",[17,92751,930],{}," and an empty list should be treated the same way.",[675,92754,92756],{"id":92755},"fix-4-make-all-branches-return-a-value","Fix 4: Make all branches return a value",[91,92758,92760],{"className":93,"code":92759,"language":95,"meta":96,"style":96},"def find_even_numbers(numbers):\n    if numbers is None:\n        return []\n\n    result = []\n    for number in numbers:\n        if number % 2 == 0:\n            result.append(number)\n    return result\n",[17,92761,92762,92775,92788,92794,92798,92806,92818,92836,92851],{"__ignoreMap":96},[100,92763,92764,92766,92769,92771,92773],{"class":102,"line":103},[100,92765,1078],{"class":1077},[100,92767,92768],{"class":1081}," find_even_numbers",[100,92770,170],{"class":118},[100,92772,179],{"class":2519},[100,92774,522],{"class":118},[100,92776,92777,92779,92782,92784,92786],{"class":102,"line":135},[100,92778,2777],{"class":145},[100,92780,92781],{"class":106}," numbers ",[100,92783,4918],{"class":110},[100,92785,866],{"class":158},[100,92787,149],{"class":118},[100,92789,92790,92792],{"class":102,"line":142},[100,92791,46371],{"class":145},[100,92793,33843],{"class":118},[100,92795,92796],{"class":102,"line":152},[100,92797,139],{"emptyLinePlaceholder":138},[100,92799,92800,92802,92804],{"class":102,"line":164},[100,92801,59692],{"class":106},[100,92803,111],{"class":110},[100,92805,33843],{"class":118},[100,92807,92808,92810,92812,92814,92816],{"class":102,"line":185},[100,92809,51393],{"class":145},[100,92811,614],{"class":106},[100,92813,617],{"class":145},[100,92815,620],{"class":106},[100,92817,149],{"class":118},[100,92819,92820,92823,92825,92828,92830,92832,92834],{"class":102,"line":197},[100,92821,92822],{"class":145},"        if",[100,92824,614],{"class":106},[100,92826,92827],{"class":110},"%",[100,92829,129],{"class":122},[100,92831,34516],{"class":110},[100,92833,1921],{"class":122},[100,92835,149],{"class":118},[100,92837,92838,92841,92843,92845,92847,92849],{"class":102,"line":771},[100,92839,92840],{"class":106},"            result",[100,92842,290],{"class":118},[100,92844,4254],{"class":178},[100,92846,170],{"class":118},[100,92848,631],{"class":178},[100,92850,215],{"class":118},[100,92852,92853,92855],{"class":102,"line":787},[100,92854,2552],{"class":145},[100,92856,92857],{"class":106}," result\n",[77,92859,92861,92862,10771],{"id":92860},"how-to-debug-where-none-came-from","How to debug where ",[17,92863,930],{},[14,92865,92866,92867,290],{},"The fastest fix is usually to find the exact line where the value became ",[17,92868,930],{},[14,92870,92871],{},"Try these steps:",[40,92873,92874,92877,92884,92887,92894],{},[43,92875,92876],{},"Print the variable right before the failing line.",[43,92878,5163,92879,92881,92882,290],{},[17,92880,5283],{}," to confirm it is ",[17,92883,4665],{},[43,92885,92886],{},"Check function return values step by step.",[43,92888,92889,92890,92893],{},"Look for assignments like ",[17,92891,92892],{},"value = some_method()"," where the method changes data in place.",[43,92895,92896,92897,2558,92899,92901,92902,92904],{},"Review ",[17,92898,2736],{},[17,92900,4944],{}," branches for missing ",[17,92903,1053],{}," statements.",[14,92906,5295],{},[91,92908,92910],{"className":93,"code":92909,"language":95,"meta":96,"style":96},"print(items)\nprint(type(items))\nprint(result is None)\nprint(my_function())\nhelp(list.sort)\nhelp(dict.update)\n",[17,92911,92912,92922,92936,92950,92960,92974],{"__ignoreMap":96},[100,92913,92914,92916,92918,92920],{"class":102,"line":103},[100,92915,372],{"class":114},[100,92917,170],{"class":118},[100,92919,3146],{"class":178},[100,92921,215],{"class":118},[100,92923,92924,92926,92928,92930,92932,92934],{"class":102,"line":135},[100,92925,372],{"class":114},[100,92927,170],{"class":118},[100,92929,1250],{"class":191},[100,92931,170],{"class":118},[100,92933,3146],{"class":178},[100,92935,182],{"class":118},[100,92937,92938,92940,92942,92944,92946,92948],{"class":102,"line":142},[100,92939,372],{"class":114},[100,92941,170],{"class":118},[100,92943,11556],{"class":178},[100,92945,4918],{"class":145},[100,92947,866],{"class":158},[100,92949,215],{"class":118},[100,92951,92952,92954,92956,92958],{"class":102,"line":152},[100,92953,372],{"class":114},[100,92955,170],{"class":118},[100,92957,24899],{"class":178},[100,92959,3370],{"class":118},[100,92961,92962,92964,92966,92968,92970,92972],{"class":102,"line":164},[100,92963,2478],{"class":114},[100,92965,170],{"class":118},[100,92967,1235],{"class":191},[100,92969,290],{"class":118},[100,92971,11565],{"class":2494},[100,92973,215],{"class":118},[100,92975,92976,92978,92980,92982,92984,92986],{"class":102,"line":185},[100,92977,2478],{"class":114},[100,92979,170],{"class":118},[100,92981,16046],{"class":191},[100,92983,290],{"class":118},[100,92985,12107],{"class":2494},[100,92987,215],{"class":118},[675,92989,92991],{"id":92990},"example-debugging-process","Example debugging process",[91,92993,92995],{"className":93,"code":92994,"language":95,"meta":96,"style":96},"def get_items():\n    print(\"Loading items...\")\n    # forgot to return a value\n\nitems = get_items()\n\nprint(items)\nprint(type(items))\n\nfor item in items:\n    print(item)\n",[17,92996,92997,93005,93020,93025,93029,93039,93043,93053,93067,93071,93083],{"__ignoreMap":96},[100,92998,92999,93001,93003],{"class":102,"line":103},[100,93000,1078],{"class":1077},[100,93002,82945],{"class":1081},[100,93004,1085],{"class":118},[100,93006,93007,93009,93011,93013,93016,93018],{"class":102,"line":135},[100,93008,200],{"class":114},[100,93010,170],{"class":118},[100,93012,206],{"class":205},[100,93014,93015],{"class":209},"Loading items...",[100,93017,206],{"class":205},[100,93019,215],{"class":118},[100,93021,93022],{"class":102,"line":142},[100,93023,93024],{"class":414},"    # forgot to return a value\n",[100,93026,93027],{"class":102,"line":152},[100,93028,139],{"emptyLinePlaceholder":138},[100,93030,93031,93033,93035,93037],{"class":102,"line":164},[100,93032,3095],{"class":106},[100,93034,111],{"class":110},[100,93036,82945],{"class":178},[100,93038,4734],{"class":118},[100,93040,93041],{"class":102,"line":185},[100,93042,139],{"emptyLinePlaceholder":138},[100,93044,93045,93047,93049,93051],{"class":102,"line":197},[100,93046,372],{"class":114},[100,93048,170],{"class":118},[100,93050,3146],{"class":178},[100,93052,215],{"class":118},[100,93054,93055,93057,93059,93061,93063,93065],{"class":102,"line":771},[100,93056,372],{"class":114},[100,93058,170],{"class":118},[100,93060,1250],{"class":191},[100,93062,170],{"class":118},[100,93064,3146],{"class":178},[100,93066,182],{"class":118},[100,93068,93069],{"class":102,"line":787},[100,93070,139],{"emptyLinePlaceholder":138},[100,93072,93073,93075,93077,93079,93081],{"class":102,"line":5816},[100,93074,71],{"class":145},[100,93076,7568],{"class":106},[100,93078,617],{"class":145},[100,93080,12000],{"class":106},[100,93082,149],{"class":118},[100,93084,93085,93087,93089,93091],{"class":102,"line":5833},[100,93086,200],{"class":114},[100,93088,170],{"class":118},[100,93090,7555],{"class":178},[100,93092,215],{"class":118},[14,93094,218],{},[91,93096,93098],{"className":93,"code":93097,"language":95,"meta":96,"style":96},"Loading items...\nNone\n\u003Cclass 'NoneType'>\nTraceback (most recent call last):\n  ...\nTypeError: 'NoneType' object is not iterable\n",[17,93099,93100,93107,93111,93125,93135,93139],{"__ignoreMap":96},[100,93101,93102,93105],{"class":102,"line":103},[100,93103,93104],{"class":106},"Loading items",[100,93106,2422],{"class":527},[100,93108,93109],{"class":102,"line":135},[100,93110,924],{"class":158},[100,93112,93113,93115,93117,93119,93121,93123],{"class":102,"line":142},[100,93114,2782],{"class":110},[100,93116,3417],{"class":1077},[100,93118,1274],{"class":205},[100,93120,4665],{"class":209},[100,93122,1280],{"class":205},[100,93124,1980],{"class":110},[100,93126,93127,93129,93131,93133],{"class":102,"line":152},[100,93128,514],{"class":178},[100,93130,170],{"class":118},[100,93132,519],{"class":178},[100,93134,522],{"class":118},[100,93136,93137],{"class":102,"line":164},[100,93138,528],{"class":527},[100,93140,93141,93143,93145,93147,93149,93151,93153,93155,93157],{"class":102,"line":185},[100,93142,3538],{"class":191},[100,93144,89],{"class":118},[100,93146,1274],{"class":205},[100,93148,4665],{"class":209},[100,93150,1280],{"class":205},[100,93152,3914],{"class":191},[100,93154,16702],{"class":110},[100,93156,4921],{"class":110},[100,93158,75106],{"class":106},[14,93160,93161,93162,12449,93165,290],{},"From this output, you can see that ",[17,93163,93164],{},"get_items()",[17,93166,930],{},[14,93168,93169,93170,290],{},"If you are handling data from files or APIs, careful error handling can also help. See ",[295,93171,12762],{"href":12761},[77,93173,93175],{"id":93174},"methods-that-change-data-in-place","Methods that change data in place",[14,93177,93178,93179,290],{},"Some methods modify an object directly and return ",[17,93180,930],{},[14,93182,13223],{},[40,93184,93185,93189,93193,93197],{},[43,93186,93187],{},[17,93188,10981],{},[43,93190,93191],{},[17,93192,11958],{},[43,93194,93195],{},[17,93196,11963],{},[43,93198,93199],{},[17,93200,11968],{},[14,93202,93203],{},"A common mistake is assigning the result of these methods to a variable.",[675,93205,6371],{"id":6370},[91,93207,93209],{"className":93,"code":93208,"language":95,"meta":96,"style":96},"numbers = [3, 1, 2]\nnumbers = numbers.sort()\n\nfor number in numbers:\n    print(number)\n",[17,93210,93211,93231,93245,93249,93261],{"__ignoreMap":96},[100,93212,93213,93215,93217,93219,93221,93223,93225,93227,93229],{"class":102,"line":103},[100,93214,107],{"class":106},[100,93216,111],{"class":110},[100,93218,594],{"class":118},[100,93220,11541],{"class":122},[100,93222,126],{"class":118},[100,93224,6915],{"class":122},[100,93226,126],{"class":118},[100,93228,129],{"class":122},[100,93230,603],{"class":118},[100,93232,93233,93235,93237,93239,93241,93243],{"class":102,"line":135},[100,93234,107],{"class":106},[100,93236,111],{"class":110},[100,93238,620],{"class":106},[100,93240,290],{"class":118},[100,93242,11565],{"class":178},[100,93244,4734],{"class":118},[100,93246,93247],{"class":102,"line":142},[100,93248,139],{"emptyLinePlaceholder":138},[100,93250,93251,93253,93255,93257,93259],{"class":102,"line":152},[100,93252,71],{"class":145},[100,93254,614],{"class":106},[100,93256,617],{"class":145},[100,93258,620],{"class":106},[100,93260,149],{"class":118},[100,93262,93263,93265,93267,93269],{"class":102,"line":164},[100,93264,200],{"class":114},[100,93266,170],{"class":118},[100,93268,631],{"class":178},[100,93270,215],{"class":118},[14,93272,93273,93276,93277,36202,93279,34119,93281,290],{},[17,93274,93275],{},"numbers.sort()"," sorts the list in place and returns ",[17,93278,930],{},[17,93280,179],{},[17,93282,930],{},[675,93284,75292],{"id":75291},[91,93286,93288],{"className":93,"code":93287,"language":95,"meta":96,"style":96},"numbers = [3, 1, 2]\nnumbers.sort()\n\nfor number in numbers:\n    print(number)\n",[17,93289,93290,93310,93320,93324,93336],{"__ignoreMap":96},[100,93291,93292,93294,93296,93298,93300,93302,93304,93306,93308],{"class":102,"line":103},[100,93293,107],{"class":106},[100,93295,111],{"class":110},[100,93297,594],{"class":118},[100,93299,11541],{"class":122},[100,93301,126],{"class":118},[100,93303,6915],{"class":122},[100,93305,126],{"class":118},[100,93307,129],{"class":122},[100,93309,603],{"class":118},[100,93311,93312,93314,93316,93318],{"class":102,"line":135},[100,93313,179],{"class":106},[100,93315,290],{"class":118},[100,93317,11565],{"class":178},[100,93319,4734],{"class":118},[100,93321,93322],{"class":102,"line":142},[100,93323,139],{"emptyLinePlaceholder":138},[100,93325,93326,93328,93330,93332,93334],{"class":102,"line":152},[100,93327,71],{"class":145},[100,93329,614],{"class":106},[100,93331,617],{"class":145},[100,93333,620],{"class":106},[100,93335,149],{"class":118},[100,93337,93338,93340,93342,93344],{"class":102,"line":164},[100,93339,200],{"class":114},[100,93341,170],{"class":118},[100,93343,631],{"class":178},[100,93345,215],{"class":118},[14,93347,218],{},[91,93349,93350],{"className":93,"code":73745,"language":95,"meta":96,"style":96},[17,93351,93352,93356,93360],{"__ignoreMap":96},[100,93353,93354],{"class":102,"line":103},[100,93355,228],{"class":122},[100,93357,93358],{"class":102,"line":135},[100,93359,233],{"class":122},[100,93361,93362],{"class":102,"line":142},[100,93363,1500],{"class":122},[14,93365,93366,93367,89],{},"The same problem can happen with ",[17,93368,11968],{},[91,93370,93372],{"className":93,"code":93371,"language":95,"meta":96,"style":96},"data = {\"a\": 1}\ndata = data.update({\"b\": 2})\n\nfor key in data:\n    print(key)\n",[17,93373,93374,93394,93420,93424,93436],{"__ignoreMap":96},[100,93375,93376,93378,93380,93382,93384,93386,93388,93390,93392],{"class":102,"line":103},[100,93377,7909],{"class":106},[100,93379,111],{"class":110},[100,93381,12080],{"class":118},[100,93383,206],{"class":205},[100,93385,295],{"class":209},[100,93387,206],{"class":205},[100,93389,89],{"class":118},[100,93391,6915],{"class":122},[100,93393,12093],{"class":118},[100,93395,93396,93398,93400,93402,93404,93406,93408,93410,93412,93414,93416,93418],{"class":102,"line":135},[100,93397,7909],{"class":106},[100,93399,111],{"class":110},[100,93401,12102],{"class":106},[100,93403,290],{"class":118},[100,93405,12107],{"class":178},[100,93407,12110],{"class":118},[100,93409,206],{"class":205},[100,93411,4027],{"class":209},[100,93413,206],{"class":205},[100,93415,89],{"class":118},[100,93417,129],{"class":122},[100,93419,12123],{"class":118},[100,93421,93422],{"class":102,"line":142},[100,93423,139],{"emptyLinePlaceholder":138},[100,93425,93426,93428,93430,93432,93434],{"class":102,"line":152},[100,93427,71],{"class":145},[100,93429,66693],{"class":106},[100,93431,617],{"class":145},[100,93433,12102],{"class":106},[100,93435,149],{"class":118},[100,93437,93438,93440,93442,93444],{"class":102,"line":164},[100,93439,200],{"class":114},[100,93441,170],{"class":118},[100,93443,43824],{"class":178},[100,93445,215],{"class":118},[14,93447,6031],{},[91,93449,93451],{"className":93,"code":93450,"language":95,"meta":96,"style":96},"data = {\"a\": 1}\ndata.update({\"b\": 2})\n\nfor key in data:\n    print(key)\n",[17,93452,93453,93473,93495,93499,93511],{"__ignoreMap":96},[100,93454,93455,93457,93459,93461,93463,93465,93467,93469,93471],{"class":102,"line":103},[100,93456,7909],{"class":106},[100,93458,111],{"class":110},[100,93460,12080],{"class":118},[100,93462,206],{"class":205},[100,93464,295],{"class":209},[100,93466,206],{"class":205},[100,93468,89],{"class":118},[100,93470,6915],{"class":122},[100,93472,12093],{"class":118},[100,93474,93475,93477,93479,93481,93483,93485,93487,93489,93491,93493],{"class":102,"line":135},[100,93476,7934],{"class":106},[100,93478,290],{"class":118},[100,93480,12107],{"class":178},[100,93482,12110],{"class":118},[100,93484,206],{"class":205},[100,93486,4027],{"class":209},[100,93488,206],{"class":205},[100,93490,89],{"class":118},[100,93492,129],{"class":122},[100,93494,12123],{"class":118},[100,93496,93497],{"class":102,"line":142},[100,93498,139],{"emptyLinePlaceholder":138},[100,93500,93501,93503,93505,93507,93509],{"class":102,"line":152},[100,93502,71],{"class":145},[100,93504,66693],{"class":106},[100,93506,617],{"class":145},[100,93508,12102],{"class":106},[100,93510,149],{"class":118},[100,93512,93513,93515,93517,93519],{"class":102,"line":164},[100,93514,200],{"class":114},[100,93516,170],{"class":118},[100,93518,43824],{"class":178},[100,93520,215],{"class":118},[14,93522,49414,93523,290],{},[295,93524,12840],{"href":10978},[77,93526,93528],{"id":93527},"using-a-default-value-safely","Using a default value safely",[14,93530,93531],{},"Sometimes a default value is fine. Sometimes it hides a bug.",[14,93533,93534],{},"This pattern is common:",[91,93536,93538],{"className":93,"code":93537,"language":95,"meta":96,"style":96},"items = items or []\n",[17,93539,93540],{"__ignoreMap":96},[100,93541,93542,93544,93546,93548,93550],{"class":102,"line":103},[100,93543,3095],{"class":106},[100,93545,111],{"class":110},[100,93547,81230],{"class":106},[100,93549,11841],{"class":110},[100,93551,33843],{"class":118},[14,93553,93554],{},"It can be useful when all “empty” values should behave the same way.",[14,93556,93557],{},"But be careful. This also replaces other falsey values, such as:",[40,93559,93560,93564,93568,93572],{},[43,93561,93562],{},[17,93563,45218],{},[43,93565,93566],{},[17,93567,11875],{},[43,93569,93570],{},[17,93571,2279],{},[43,93573,93574],{},[17,93575,1649],{},[14,93577,93578,93579,93581],{},"If you only want to replace ",[17,93580,930],{},", use an explicit check:",[91,93583,93585],{"className":93,"code":93584,"language":95,"meta":96,"style":96},"if items is None:\n    items = []\n",[17,93586,93587,93599],{"__ignoreMap":96},[100,93588,93589,93591,93593,93595,93597],{"class":102,"line":103},[100,93590,2736],{"class":145},[100,93592,81230],{"class":106},[100,93594,4918],{"class":110},[100,93596,866],{"class":158},[100,93598,149],{"class":118},[100,93600,93601,93603,93605],{"class":102,"line":135},[100,93602,91497],{"class":106},[100,93604,111],{"class":110},[100,93606,33843],{"class":118},[14,93608,93609],{},"Use a default only when it matches the meaning of your program.",[14,93611,93612],{},"Good use case:",[40,93614,93615],{},[43,93616,93617],{},"missing items should behave like an empty list",[14,93619,93620],{},"Bad use case:",[40,93622,93623],{},[43,93624,93625,93627],{},[17,93626,930],{}," means “data failed to load” and should be handled differently",[77,93629,30537],{"id":30536},[14,93631,76361],{},[40,93633,93634,93640,93646,93652],{},[43,93635,93636],{},[295,93637,74872,93638],{"href":76368},[17,93639,76371],{},[43,93641,93642],{},[295,93643,74872,93644],{"href":83882},[17,93645,74875],{},[43,93647,93648],{},[295,93649,74872,93650],{"href":85112},[17,93651,82208],{},[43,93653,93654],{},[295,93655,5728,93656],{"href":4967},[17,93657,4970],{},[77,93659,1514],{"id":1513},[675,93661,12770],{"id":12769},[14,93663,93664,10947,93666,93668,93669,93671],{},[17,93665,4665],{},[17,93667,930],{},". ",[17,93670,930],{}," is a special value that means no value.",[675,93673,93675],{"id":93674},"why-does-python-say-none-is-not-iterable","Why does Python say None is not iterable?",[14,93677,93678,93679,93681],{},"Because looping and membership checks need an iterable object like a list, tuple, string, set, or dictionary. ",[17,93680,930],{}," is not one of these.",[675,93683,93685],{"id":93684},"can-i-fix-this-by-using-an-empty-list","Can I fix this by using an empty list?",[14,93687,93688,93689,63848],{},"Yes, but only if an empty list is the correct fallback. It is better to understand why the value became ",[17,93690,930],{},[675,93692,93694],{"id":93693},"how-do-i-know-which-line-caused-none","How do I know which line caused None?",[14,93696,93697],{},"Check the traceback, then print the variable before the failing line and trace it back to the function or assignment that created it.",[675,93699,93701],{"id":93700},"do-list-methods-return-new-lists","Do list methods return new lists?",[14,93703,93704,93705,5870,93707,5629,93709,93711,93712,290],{},"Not all of them. Methods like ",[17,93706,7659],{},[17,93708,8134],{},[17,93710,8139],{}," change the list in place and return ",[17,93713,930],{},[77,93715,1554],{"id":1553},[40,93717,93718,93722,93726,93730],{},[43,93719,93720],{},[295,93721,12835],{"href":11291},[43,93723,93724],{},[295,93725,12577],{"href":11015},[43,93727,93728],{},[295,93729,12840],{"href":10978},[43,93731,93732],{},[295,93733,5728,93734],{"href":4967},[17,93735,4970],{},[1589,93737,93738],{},"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 .slqww, html code.shiki .slqww{--shiki-light:#6182B8;--shiki-default:#24292E;--shiki-dark:#E1E4E8}html pre.shiki code .sP7_E, html code.shiki .sP7_E{--shiki-light:#39ADB5;--shiki-default:#24292E;--shiki-dark:#E1E4E8}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 .s39Yj, html code.shiki .s39Yj{--shiki-light:#39ADB5;--shiki-default:#005CC5;--shiki-dark:#79B8FF}html pre.shiki code .sptTA, html code.shiki .sptTA{--shiki-light:#6182B8;--shiki-default:#005CC5;--shiki-dark:#79B8FF}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 .s2W-s, html code.shiki .s2W-s{--shiki-light:#39ADB5;--shiki-light-font-style:italic;--shiki-default:#032F62;--shiki-default-font-style:inherit;--shiki-dark:#9ECBFF;--shiki-dark-font-style:inherit}html pre.shiki code .sithA, html code.shiki .sithA{--shiki-light:#90A4AE;--shiki-light-font-style:italic;--shiki-default:#032F62;--shiki-default-font-style:inherit;--shiki-dark:#9ECBFF;--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 .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 .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 .s_hVV, html code.shiki .s_hVV{--shiki-light:#90A4AE;--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 .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 .srdBf, html code.shiki .srdBf{--shiki-light:#F76D47;--shiki-default:#005CC5;--shiki-dark:#79B8FF}html pre.shiki code .skxfh, html code.shiki .skxfh{--shiki-light:#E53935;--shiki-default:#24292E;--shiki-dark:#E1E4E8}",{"title":96,"searchDepth":135,"depth":135,"links":93740},[93741,93742,93743,93747,93748,93751,93758,93762,93766,93767,93768,93775],{"id":79,"depth":135,"text":80},{"id":5880,"depth":135,"text":5881},{"id":91662,"depth":135,"text":91663,"children":93744},[93745,93746],{"id":7740,"depth":142,"text":7741},{"id":91884,"depth":142,"text":91885},{"id":25090,"depth":135,"text":25091},{"id":92205,"depth":135,"text":92206,"children":93749},[93750],{"id":92383,"depth":142,"text":92384},{"id":6243,"depth":135,"text":6244,"children":93752},[93753,93754,93756,93757],{"id":92558,"depth":142,"text":92559},{"id":92634,"depth":142,"text":93755},"Fix 2: Check for None",{"id":92701,"depth":142,"text":92702},{"id":92755,"depth":142,"text":92756},{"id":92860,"depth":135,"text":93759,"children":93760},"How to debug where None came from",[93761],{"id":92990,"depth":142,"text":92991},{"id":93174,"depth":135,"text":93175,"children":93763},[93764,93765],{"id":6370,"depth":142,"text":6371},{"id":75291,"depth":142,"text":75292},{"id":93527,"depth":135,"text":93528},{"id":30536,"depth":135,"text":30537},{"id":1513,"depth":135,"text":1514,"children":93769},[93770,93771,93772,93773,93774],{"id":12769,"depth":142,"text":12770},{"id":93674,"depth":142,"text":93675},{"id":93684,"depth":142,"text":93685},{"id":93693,"depth":142,"text":93694},{"id":93700,"depth":142,"text":93701},{"id":1553,"depth":135,"text":1554},"Master typeerror nonetype object is not iterable fix in our comprehensive Python beginner guide.",{},"\u002Ferrors\u002Ftypeerror-nonetype-object-is-not-iterable-fix",{"title":79463,"description":93776},"errors\u002Ftypeerror-nonetype-object-is-not-iterable-fix","RLLX2ujSuEinYej8Ka_dde77YlhGmDVubJGtS8qXMbM",{"id":93783,"title":93784,"body":93785,"description":95815,"extension":1623,"meta":95816,"navigation":138,"path":95817,"seo":95818,"stem":95819,"__hash__":95820},"content\u002Ferrors\u002Ftypeerror-sequence-item-0-expected-str-instance-fix.md","TypeError: sequence item 0 expected str instance (Fix)",{"type":7,"value":93786,"toc":95780},[93787,93790,93797,93807,93810,93821,93823,93895,93897,93910,93913,93915,93924,93927,93958,93960,93976,93987,93989,94046,94048,94076,94078,94097,94100,94102,94108,94111,94137,94140,94157,94161,94164,94242,94244,94267,94279,94281,94292,94296,94299,94382,94384,94404,94407,94410,94520,94522,94542,94549,94555,94560,94566,94572,94671,94673,94682,94688,94691,94780,94782,94791,94797,94802,94899,94901,94924,94931,94935,94940,94942,95048,95050,95189,95192,95209,95217,95221,95224,95228,95295,95297,95315,95319,95411,95413,95427,95431,95506,95508,95532,95536,95638,95640,95658,95660,95663,95666,95687,95689,95693,95696,95702,95707,95713,95716,95726,95733,95739,95741,95777],[10,93788,93784],{"id":93789},"typeerror-sequence-item-0-expected-str-instance-fix",[14,93791,93792,93793,93796],{},"This error usually appears when you use ",[17,93794,93795],{},"str.join()"," on a list or other sequence that contains at least one non-string value.",[14,93798,93799,93801,93802,93804,93805,290],{},[17,93800,16071],{}," only works with strings. If your data contains integers, ",[17,93803,930],{},", floats, booleans, or other types, Python raises a ",[17,93806,3538],{},[14,93808,93809],{},"The good news is that this is usually easy to fix:",[40,93811,93812,93815,93818],{},[43,93813,93814],{},"find the non-string values",[43,93816,93817],{},"convert them to strings",[43,93819,93820],{},"or remove values that should not be joined",[77,93822,80],{"id":79},[91,93824,93826],{"className":93,"code":93825,"language":95,"meta":96,"style":96},"items = [\"a\", 1, \"b\"]\nresult = \" \".join(map(str, items))\nprint(result)\n",[17,93827,93828,93856,93885],{"__ignoreMap":96},[100,93829,93830,93832,93834,93836,93838,93840,93842,93844,93846,93848,93850,93852,93854],{"class":102,"line":103},[100,93831,3095],{"class":106},[100,93833,111],{"class":110},[100,93835,594],{"class":118},[100,93837,206],{"class":205},[100,93839,295],{"class":209},[100,93841,206],{"class":205},[100,93843,126],{"class":118},[100,93845,6915],{"class":122},[100,93847,126],{"class":118},[100,93849,1708],{"class":205},[100,93851,4027],{"class":209},[100,93853,206],{"class":205},[100,93855,603],{"class":118},[100,93857,93858,93860,93862,93864,93866,93868,93870,93872,93875,93877,93879,93881,93883],{"class":102,"line":135},[100,93859,11556],{"class":106},[100,93861,111],{"class":110},[100,93863,1708],{"class":205},[100,93865,1708],{"class":205},[100,93867,290],{"class":118},[100,93869,19022],{"class":178},[100,93871,170],{"class":118},[100,93873,93874],{"class":114},"map",[100,93876,170],{"class":118},[100,93878,3422],{"class":191},[100,93880,126],{"class":118},[100,93882,12000],{"class":178},[100,93884,182],{"class":118},[100,93886,93887,93889,93891,93893],{"class":102,"line":142},[100,93888,372],{"class":114},[100,93890,170],{"class":118},[100,93892,11580],{"class":178},[100,93894,215],{"class":118},[14,93896,218],{},[91,93898,93900],{"className":93,"code":93899,"language":95,"meta":96,"style":96},"a 1 b\n",[17,93901,93902],{"__ignoreMap":96},[100,93903,93904,93906,93908],{"class":102,"line":103},[100,93905,82313],{"class":106},[100,93907,123],{"class":122},[100,93909,33812],{"class":106},[14,93911,93912],{},"Use this when you want to join mixed values into one string. It converts every item to a string first.",[77,93914,5881],{"id":5880},[14,93916,93917,93918,290],{},"This error usually happens with the ",[295,93919,93921,93923],{"href":93920},"\u002Freference\u002Fpython-string-join-method",[17,93922,16071],{}," string method",[14,93925,93926],{},"For example, Python may show:",[91,93928,93930],{"className":93,"code":93929,"language":95,"meta":96,"style":96},"TypeError: sequence item 0: expected str instance, int found\n",[17,93931,93932],{"__ignoreMap":96},[100,93933,93934,93936,93938,93941,93943,93945,93947,93949,93952,93954,93956],{"class":102,"line":103},[100,93935,3538],{"class":191},[100,93937,89],{"class":118},[100,93939,93940],{"class":106}," sequence item ",[100,93942,2279],{"class":122},[100,93944,89],{"class":118},[100,93946,16595],{"class":106},[100,93948,3422],{"class":191},[100,93950,93951],{"class":106}," instance",[100,93953,126],{"class":118},[100,93955,3389],{"class":191},[100,93957,43722],{"class":106},[14,93959,19648],{},[40,93961,93962,93967,93970,93973],{},[43,93963,93964,93965],{},"you called ",[17,93966,16071],{},[43,93968,93969],{},"Python checked the items in the sequence",[43,93971,93972],{},"one of the items was not a string",[43,93974,93975],{},"Python stopped at the first bad item it found",[14,93977,93978,93979,93982,93983,93986],{},"If the message says ",[17,93980,93981],{},"item 0",", the first item was the problem. If it says ",[17,93984,93985],{},"item 2",", the third item was the first invalid value Python found.",[77,93988,11031],{"id":11030},[91,93990,93992],{"className":93,"code":93991,"language":95,"meta":96,"style":96},"numbers = [1, 2, 3]\nresult = \"-\".join(numbers)\nprint(result)\n",[17,93993,93994,94014,94036],{"__ignoreMap":96},[100,93995,93996,93998,94000,94002,94004,94006,94008,94010,94012],{"class":102,"line":103},[100,93997,107],{"class":106},[100,93999,111],{"class":110},[100,94001,594],{"class":118},[100,94003,123],{"class":122},[100,94005,126],{"class":118},[100,94007,129],{"class":122},[100,94009,126],{"class":118},[100,94011,1405],{"class":122},[100,94013,603],{"class":118},[100,94015,94016,94018,94020,94022,94024,94026,94028,94030,94032,94034],{"class":102,"line":135},[100,94017,11556],{"class":106},[100,94019,111],{"class":110},[100,94021,1708],{"class":205},[100,94023,2193],{"class":209},[100,94025,206],{"class":205},[100,94027,290],{"class":118},[100,94029,19022],{"class":178},[100,94031,170],{"class":118},[100,94033,179],{"class":178},[100,94035,215],{"class":118},[100,94037,94038,94040,94042,94044],{"class":102,"line":142},[100,94039,372],{"class":114},[100,94041,170],{"class":118},[100,94043,11580],{"class":178},[100,94045,215],{"class":118},[14,94047,7710],{},[91,94049,94050],{"className":93,"code":93929,"language":95,"meta":96,"style":96},[17,94051,94052],{"__ignoreMap":96},[100,94053,94054,94056,94058,94060,94062,94064,94066,94068,94070,94072,94074],{"class":102,"line":103},[100,94055,3538],{"class":191},[100,94057,89],{"class":118},[100,94059,93940],{"class":106},[100,94061,2279],{"class":122},[100,94063,89],{"class":118},[100,94065,16595],{"class":106},[100,94067,3422],{"class":191},[100,94069,93951],{"class":106},[100,94071,126],{"class":118},[100,94073,3389],{"class":191},[100,94075,43722],{"class":106},[14,94077,32043],{},[40,94079,94080,94086,94089],{},[43,94081,94082,94085],{},[17,94083,94084],{},"\"-\""," is a string separator",[43,94087,94088],{},"but the list items are integers",[43,94090,94091,94093,94094,94096],{},[17,94092,16071],{}," does ",[22,94095,1059],{}," convert values automatically",[14,94098,94099],{},"If you want to combine numbers as text, you must convert them first.",[77,94101,25091],{"id":25090},[14,94103,94104,94105,94107],{},"The separator in ",[17,94106,16071],{}," is a string, but the items being joined must also be strings.",[14,94109,94110],{},"Common values that cause this error are:",[40,94112,94113,94117,94121,94125,94129,94133],{},[43,94114,94115],{},[17,94116,3499],{},[43,94118,94119],{},[17,94120,60476],{},[43,94122,94123],{},[17,94124,930],{},[43,94126,94127],{},[17,94128,46487],{},[43,94130,94131],{},[17,94132,15319],{},[43,94134,94135],{},[17,94136,1235],{},[14,94138,94139],{},"Beginners often see this error when:",[40,94141,94142,94145,94148,94151,94154],{},[43,94143,94144],{},"reading data from a file",[43,94146,94147],{},"building a list in a loop",[43,94149,94150],{},"mixing labels and numbers in one list",[43,94152,94153],{},"joining dictionary values",[43,94155,94156],{},"working with user input and calculated values together",[77,94158,94160],{"id":94159},"fix-1-convert-all-items-to-strings","Fix 1: Convert all items to strings",[14,94162,94163],{},"The simplest fix is to convert every item to a string before joining.",[91,94165,94167],{"className":93,"code":94166,"language":95,"meta":96,"style":96},"items = [\"apple\", 10, \"banana\", 3.5]\nresult = \", \".join(map(str, items))\nprint(result)\n",[17,94168,94169,94202,94232],{"__ignoreMap":96},[100,94170,94171,94173,94175,94177,94179,94181,94183,94185,94187,94189,94191,94193,94195,94197,94200],{"class":102,"line":103},[100,94172,3095],{"class":106},[100,94174,111],{"class":110},[100,94176,594],{"class":118},[100,94178,206],{"class":205},[100,94180,4148],{"class":209},[100,94182,206],{"class":205},[100,94184,126],{"class":118},[100,94186,6550],{"class":122},[100,94188,126],{"class":118},[100,94190,1708],{"class":205},[100,94192,4157],{"class":209},[100,94194,206],{"class":205},[100,94196,126],{"class":118},[100,94198,94199],{"class":122}," 3.5",[100,94201,603],{"class":118},[100,94203,94204,94206,94208,94210,94212,94214,94216,94218,94220,94222,94224,94226,94228,94230],{"class":102,"line":135},[100,94205,11556],{"class":106},[100,94207,111],{"class":110},[100,94209,1708],{"class":205},[100,94211,5870],{"class":209},[100,94213,206],{"class":205},[100,94215,290],{"class":118},[100,94217,19022],{"class":178},[100,94219,170],{"class":118},[100,94221,93874],{"class":114},[100,94223,170],{"class":118},[100,94225,3422],{"class":191},[100,94227,126],{"class":118},[100,94229,12000],{"class":178},[100,94231,182],{"class":118},[100,94233,94234,94236,94238,94240],{"class":102,"line":142},[100,94235,372],{"class":114},[100,94237,170],{"class":118},[100,94239,11580],{"class":178},[100,94241,215],{"class":118},[14,94243,218],{},[91,94245,94247],{"className":93,"code":94246,"language":95,"meta":96,"style":96},"apple, 10, banana, 3.5\n",[17,94248,94249],{"__ignoreMap":96},[100,94250,94251,94253,94255,94257,94259,94262,94264],{"class":102,"line":103},[100,94252,4148],{"class":106},[100,94254,126],{"class":118},[100,94256,6550],{"class":122},[100,94258,126],{"class":118},[100,94260,94261],{"class":106}," banana",[100,94263,126],{"class":118},[100,94265,94266],{"class":122}," 3.5\n",[14,94268,94269,94272,94273,94278],{},[17,94270,94271],{},"map(str, items)"," applies the ",[295,94274,94276,6354],{"href":94275},"\u002Freference\u002Fpython-str-function-explained",[17,94277,69900],{}," to each item.",[14,94280,49530],{},[40,94282,94283,94286,94289],{},[43,94284,94285],{},"your sequence contains mixed types",[43,94287,94288],{},"you want every value shown as text",[43,94290,94291],{},"you want a short and clean fix",[77,94293,94295],{"id":94294},"fix-2-use-a-list-comprehension","Fix 2: Use a list comprehension",[14,94297,94298],{},"A list comprehension does the same job, but gives you more control.",[91,94300,94302],{"className":93,"code":94301,"language":95,"meta":96,"style":96},"items = [\"apple\", 10, \"banana\", 3.5]\nresult = \", \".join([str(x) for x in items])\nprint(result)\n",[17,94303,94304,94336,94372],{"__ignoreMap":96},[100,94305,94306,94308,94310,94312,94314,94316,94318,94320,94322,94324,94326,94328,94330,94332,94334],{"class":102,"line":103},[100,94307,3095],{"class":106},[100,94309,111],{"class":110},[100,94311,594],{"class":118},[100,94313,206],{"class":205},[100,94315,4148],{"class":209},[100,94317,206],{"class":205},[100,94319,126],{"class":118},[100,94321,6550],{"class":122},[100,94323,126],{"class":118},[100,94325,1708],{"class":205},[100,94327,4157],{"class":209},[100,94329,206],{"class":205},[100,94331,126],{"class":118},[100,94333,94199],{"class":122},[100,94335,603],{"class":118},[100,94337,94338,94340,94342,94344,94346,94348,94350,94352,94354,94356,94358,94360,94362,94364,94366,94368,94370],{"class":102,"line":135},[100,94339,11556],{"class":106},[100,94341,111],{"class":110},[100,94343,1708],{"class":205},[100,94345,5870],{"class":209},[100,94347,206],{"class":205},[100,94349,290],{"class":118},[100,94351,19022],{"class":178},[100,94353,119],{"class":118},[100,94355,3422],{"class":191},[100,94357,170],{"class":118},[100,94359,39024],{"class":178},[100,94361,6155],{"class":118},[100,94363,7565],{"class":145},[100,94365,59659],{"class":178},[100,94367,617],{"class":145},[100,94369,12000],{"class":178},[100,94371,132],{"class":118},[100,94373,94374,94376,94378,94380],{"class":102,"line":142},[100,94375,372],{"class":114},[100,94377,170],{"class":118},[100,94379,11580],{"class":178},[100,94381,215],{"class":118},[14,94383,218],{},[91,94385,94386],{"className":93,"code":94246,"language":95,"meta":96,"style":96},[17,94387,94388],{"__ignoreMap":96},[100,94389,94390,94392,94394,94396,94398,94400,94402],{"class":102,"line":103},[100,94391,4148],{"class":106},[100,94393,126],{"class":118},[100,94395,6550],{"class":122},[100,94397,126],{"class":118},[100,94399,94261],{"class":106},[100,94401,126],{"class":118},[100,94403,94266],{"class":122},[14,94405,94406],{},"This is useful when you want to clean or format values during conversion.",[14,94408,94409],{},"For example, you can add custom formatting:",[91,94411,94413],{"className":93,"code":94412,"language":95,"meta":96,"style":96},"items = [\"Total\", 25, None]\n\nresult = \" | \".join(\n    [\"missing\" if x is None else str(x) for x in items]\n)\n\nprint(result)\n",[17,94414,94415,94439,94443,94463,94502,94506,94510],{"__ignoreMap":96},[100,94416,94417,94419,94421,94423,94425,94427,94429,94431,94433,94435,94437],{"class":102,"line":103},[100,94418,3095],{"class":106},[100,94420,111],{"class":110},[100,94422,594],{"class":118},[100,94424,206],{"class":205},[100,94426,76987],{"class":209},[100,94428,206],{"class":205},[100,94430,126],{"class":118},[100,94432,16782],{"class":122},[100,94434,126],{"class":118},[100,94436,866],{"class":158},[100,94438,603],{"class":118},[100,94440,94441],{"class":102,"line":135},[100,94442,139],{"emptyLinePlaceholder":138},[100,94444,94445,94447,94449,94451,94454,94456,94458,94460],{"class":102,"line":142},[100,94446,11556],{"class":106},[100,94448,111],{"class":110},[100,94450,1708],{"class":205},[100,94452,94453],{"class":209}," | ",[100,94455,206],{"class":205},[100,94457,290],{"class":118},[100,94459,19022],{"class":178},[100,94461,94462],{"class":118},"(\n",[100,94464,94465,94468,94470,94472,94474,94476,94478,94480,94482,94484,94486,94488,94490,94492,94494,94496,94498,94500],{"class":102,"line":152},[100,94466,94467],{"class":118},"    [",[100,94469,206],{"class":205},[100,94471,31493],{"class":209},[100,94473,206],{"class":205},[100,94475,11904],{"class":145},[100,94477,59659],{"class":178},[100,94479,4918],{"class":110},[100,94481,866],{"class":158},[100,94483,11913],{"class":145},[100,94485,2871],{"class":191},[100,94487,170],{"class":118},[100,94489,39024],{"class":178},[100,94491,6155],{"class":118},[100,94493,7565],{"class":145},[100,94495,59659],{"class":178},[100,94497,617],{"class":145},[100,94499,12000],{"class":178},[100,94501,603],{"class":118},[100,94503,94504],{"class":102,"line":164},[100,94505,215],{"class":118},[100,94507,94508],{"class":102,"line":185},[100,94509,139],{"emptyLinePlaceholder":138},[100,94511,94512,94514,94516,94518],{"class":102,"line":197},[100,94513,372],{"class":114},[100,94515,170],{"class":118},[100,94517,11580],{"class":178},[100,94519,215],{"class":118},[14,94521,218],{},[91,94523,94525],{"className":93,"code":94524,"language":95,"meta":96,"style":96},"Total | 25 | missing\n",[17,94526,94527],{"__ignoreMap":96},[100,94528,94529,94532,94535,94537,94540],{"class":102,"line":103},[100,94530,94531],{"class":106},"Total ",[100,94533,94534],{"class":110},"|",[100,94536,16782],{"class":122},[100,94538,94539],{"class":110}," |",[100,94541,43090],{"class":106},[14,94543,94544,94545,290],{},"If you are new to this, see also ",[295,94546,94548],{"href":94547},"\u002Fhow-to\u002Fhow-to-join-strings-in-python","how to join strings in Python",[77,94550,94552,94553,32991],{"id":94551},"fix-3-remove-or-replace-none-values","Fix 3: Remove or replace ",[17,94554,930],{},[14,94556,94557,94559],{},[17,94558,930],{}," is a very common cause of this error.",[675,94561,94563,94564,32991],{"id":94562},"remove-none-values","Remove ",[17,94565,930],{},[14,94567,94568,94569,94571],{},"Use this if ",[17,94570,930],{}," should be skipped:",[91,94573,94575],{"className":93,"code":94574,"language":95,"meta":96,"style":96},"items = [\"a\", None, \"b\", None, \"c\"]\nresult = \" \".join(str(x) for x in items if x is not None)\nprint(result)\n",[17,94576,94577,94617,94661],{"__ignoreMap":96},[100,94578,94579,94581,94583,94585,94587,94589,94591,94593,94595,94597,94599,94601,94603,94605,94607,94609,94611,94613,94615],{"class":102,"line":103},[100,94580,3095],{"class":106},[100,94582,111],{"class":110},[100,94584,594],{"class":118},[100,94586,206],{"class":205},[100,94588,295],{"class":209},[100,94590,206],{"class":205},[100,94592,126],{"class":118},[100,94594,866],{"class":158},[100,94596,126],{"class":118},[100,94598,1708],{"class":205},[100,94600,4027],{"class":209},[100,94602,206],{"class":205},[100,94604,126],{"class":118},[100,94606,866],{"class":158},[100,94608,126],{"class":118},[100,94610,1708],{"class":205},[100,94612,4036],{"class":209},[100,94614,206],{"class":205},[100,94616,603],{"class":118},[100,94618,94619,94621,94623,94625,94627,94629,94631,94633,94635,94637,94639,94641,94643,94645,94647,94649,94651,94653,94655,94657,94659],{"class":102,"line":135},[100,94620,11556],{"class":106},[100,94622,111],{"class":110},[100,94624,1708],{"class":205},[100,94626,1708],{"class":205},[100,94628,290],{"class":118},[100,94630,19022],{"class":178},[100,94632,170],{"class":118},[100,94634,3422],{"class":191},[100,94636,170],{"class":118},[100,94638,39024],{"class":178},[100,94640,6155],{"class":118},[100,94642,7565],{"class":145},[100,94644,59659],{"class":178},[100,94646,617],{"class":145},[100,94648,81230],{"class":178},[100,94650,2736],{"class":145},[100,94652,59659],{"class":178},[100,94654,4918],{"class":145},[100,94656,4921],{"class":145},[100,94658,866],{"class":158},[100,94660,215],{"class":118},[100,94662,94663,94665,94667,94669],{"class":102,"line":142},[100,94664,372],{"class":114},[100,94666,170],{"class":118},[100,94668,11580],{"class":178},[100,94670,215],{"class":118},[14,94672,218],{},[91,94674,94676],{"className":93,"code":94675,"language":95,"meta":96,"style":96},"a b c\n",[17,94677,94678],{"__ignoreMap":96},[100,94679,94680],{"class":102,"line":103},[100,94681,94675],{"class":106},[675,94683,52216,94685,94687],{"id":94684},"replace-none-with-an-empty-string",[17,94686,930],{}," with an empty string",[14,94689,94690],{},"Use this if you want to keep the position but not show text:",[91,94692,94694],{"className":93,"code":94693,"language":95,"meta":96,"style":96},"items = [\"a\", None, \"b\"]\nresult = \" \".join(\"\" if x is None else str(x) for x in items)\nprint(result)\n",[17,94695,94696,94724,94770],{"__ignoreMap":96},[100,94697,94698,94700,94702,94704,94706,94708,94710,94712,94714,94716,94718,94720,94722],{"class":102,"line":103},[100,94699,3095],{"class":106},[100,94701,111],{"class":110},[100,94703,594],{"class":118},[100,94705,206],{"class":205},[100,94707,295],{"class":209},[100,94709,206],{"class":205},[100,94711,126],{"class":118},[100,94713,866],{"class":158},[100,94715,126],{"class":118},[100,94717,1708],{"class":205},[100,94719,4027],{"class":209},[100,94721,206],{"class":205},[100,94723,603],{"class":118},[100,94725,94726,94728,94730,94732,94734,94736,94738,94740,94742,94744,94746,94748,94750,94752,94754,94756,94758,94760,94762,94764,94766,94768],{"class":102,"line":135},[100,94727,11556],{"class":106},[100,94729,111],{"class":110},[100,94731,1708],{"class":205},[100,94733,1708],{"class":205},[100,94735,290],{"class":118},[100,94737,19022],{"class":178},[100,94739,170],{"class":118},[100,94741,11875],{"class":205},[100,94743,11904],{"class":145},[100,94745,59659],{"class":178},[100,94747,4918],{"class":145},[100,94749,866],{"class":158},[100,94751,11913],{"class":145},[100,94753,2871],{"class":191},[100,94755,170],{"class":118},[100,94757,39024],{"class":178},[100,94759,6155],{"class":118},[100,94761,7565],{"class":145},[100,94763,59659],{"class":178},[100,94765,617],{"class":145},[100,94767,12000],{"class":178},[100,94769,215],{"class":118},[100,94771,94772,94774,94776,94778],{"class":102,"line":142},[100,94773,372],{"class":114},[100,94775,170],{"class":118},[100,94777,11580],{"class":178},[100,94779,215],{"class":118},[14,94781,218],{},[91,94783,94785],{"className":93,"code":94784,"language":95,"meta":96,"style":96},"a  b\n",[17,94786,94787],{"__ignoreMap":96},[100,94788,94789],{"class":102,"line":103},[100,94790,94784],{"class":106},[675,94792,52216,94794,94796],{"id":94793},"replace-none-with-a-label",[17,94795,930],{}," with a label",[14,94798,94568,94799,94801],{},[17,94800,930],{}," has meaning in your data:",[91,94803,94805],{"className":93,"code":94804,"language":95,"meta":96,"style":96},"items = [\"name\", None, \"done\"]\nresult = \" | \".join(\"N\u002FA\" if x is None else str(x) for x in items)\nprint(result)\n",[17,94806,94807,94836,94889],{"__ignoreMap":96},[100,94808,94809,94811,94813,94815,94817,94819,94821,94823,94825,94827,94829,94832,94834],{"class":102,"line":103},[100,94810,3095],{"class":106},[100,94812,111],{"class":110},[100,94814,594],{"class":118},[100,94816,206],{"class":205},[100,94818,2853],{"class":209},[100,94820,206],{"class":205},[100,94822,126],{"class":118},[100,94824,866],{"class":158},[100,94826,126],{"class":118},[100,94828,1708],{"class":205},[100,94830,94831],{"class":209},"done",[100,94833,206],{"class":205},[100,94835,603],{"class":118},[100,94837,94838,94840,94842,94844,94846,94848,94850,94852,94854,94856,94859,94861,94863,94865,94867,94869,94871,94873,94875,94877,94879,94881,94883,94885,94887],{"class":102,"line":135},[100,94839,11556],{"class":106},[100,94841,111],{"class":110},[100,94843,1708],{"class":205},[100,94845,94453],{"class":209},[100,94847,206],{"class":205},[100,94849,290],{"class":118},[100,94851,19022],{"class":178},[100,94853,170],{"class":118},[100,94855,206],{"class":205},[100,94857,94858],{"class":209},"N\u002FA",[100,94860,206],{"class":205},[100,94862,11904],{"class":145},[100,94864,59659],{"class":178},[100,94866,4918],{"class":145},[100,94868,866],{"class":158},[100,94870,11913],{"class":145},[100,94872,2871],{"class":191},[100,94874,170],{"class":118},[100,94876,39024],{"class":178},[100,94878,6155],{"class":118},[100,94880,7565],{"class":145},[100,94882,59659],{"class":178},[100,94884,617],{"class":145},[100,94886,12000],{"class":178},[100,94888,215],{"class":118},[100,94890,94891,94893,94895,94897],{"class":102,"line":142},[100,94892,372],{"class":114},[100,94894,170],{"class":118},[100,94896,11580],{"class":178},[100,94898,215],{"class":118},[14,94900,218],{},[91,94902,94904],{"className":93,"code":94903,"language":95,"meta":96,"style":96},"name | N\u002FA | done\n",[17,94905,94906],{"__ignoreMap":96},[100,94907,94908,94910,94912,94915,94917,94919,94921],{"class":102,"line":103},[100,94909,1691],{"class":106},[100,94911,94534],{"class":110},[100,94913,94914],{"class":106}," N",[100,94916,2558],{"class":110},[100,94918,571],{"class":106},[100,94920,94534],{"class":110},[100,94922,94923],{"class":106}," done\n",[14,94925,11734,94926,94928,94929,290],{},[17,94927,930],{}," appears because your code expected a list but got something else, you may also run into ",[295,94930,78632],{"href":93778},[77,94932,94934],{"id":94933},"fix-4-check-your-data-before-joining","Fix 4: Check your data before joining",[14,94936,94937,94938,290],{},"Sometimes the bad value comes from user input, a file, API data, or a loop. In that case, inspect the sequence before calling ",[17,94939,16071],{},[14,94941,1206],{},[91,94943,94945],{"className":93,"code":94944,"language":95,"meta":96,"style":96},"items = [\"a\", 1, None, [\"x\"]]\n\nprint(items)\nprint(type(items))\nprint([type(x) for x in items])\nprint(repr(items))\n",[17,94946,94947,94982,94986,94996,95010,95034],{"__ignoreMap":96},[100,94948,94949,94951,94953,94955,94957,94959,94961,94963,94965,94967,94969,94971,94973,94975,94977,94979],{"class":102,"line":103},[100,94950,3095],{"class":106},[100,94952,111],{"class":110},[100,94954,594],{"class":118},[100,94956,206],{"class":205},[100,94958,295],{"class":209},[100,94960,206],{"class":205},[100,94962,126],{"class":118},[100,94964,6915],{"class":122},[100,94966,126],{"class":118},[100,94968,866],{"class":158},[100,94970,126],{"class":118},[100,94972,594],{"class":118},[100,94974,206],{"class":205},[100,94976,39024],{"class":209},[100,94978,206],{"class":205},[100,94980,94981],{"class":118},"]]\n",[100,94983,94984],{"class":102,"line":135},[100,94985,139],{"emptyLinePlaceholder":138},[100,94987,94988,94990,94992,94994],{"class":102,"line":142},[100,94989,372],{"class":114},[100,94991,170],{"class":118},[100,94993,3146],{"class":178},[100,94995,215],{"class":118},[100,94997,94998,95000,95002,95004,95006,95008],{"class":102,"line":152},[100,94999,372],{"class":114},[100,95001,170],{"class":118},[100,95003,1250],{"class":191},[100,95005,170],{"class":118},[100,95007,3146],{"class":178},[100,95009,182],{"class":118},[100,95011,95012,95014,95016,95018,95020,95022,95024,95026,95028,95030,95032],{"class":102,"line":164},[100,95013,372],{"class":114},[100,95015,119],{"class":118},[100,95017,1250],{"class":191},[100,95019,170],{"class":118},[100,95021,39024],{"class":178},[100,95023,6155],{"class":118},[100,95025,7565],{"class":145},[100,95027,59659],{"class":178},[100,95029,617],{"class":145},[100,95031,12000],{"class":178},[100,95033,132],{"class":118},[100,95035,95036,95038,95040,95042,95044,95046],{"class":102,"line":185},[100,95037,372],{"class":114},[100,95039,170],{"class":118},[100,95041,12228],{"class":114},[100,95043,170],{"class":118},[100,95045,3146],{"class":178},[100,95047,182],{"class":118},[14,95049,90852],{},[91,95051,95053],{"className":93,"code":95052,"language":95,"meta":96,"style":96},"['a', 1, None, ['x']]\n\u003Cclass 'list'>\n[\u003Cclass 'str'>, \u003Cclass 'int'>, \u003Cclass 'NoneType'>, \u003Cclass 'list'>]\n['a', 1, None, ['x']]\n",[17,95054,95055,95085,95099,95159],{"__ignoreMap":96},[100,95056,95057,95059,95061,95063,95065,95067,95069,95071,95073,95075,95077,95079,95081,95083],{"class":102,"line":103},[100,95058,3166],{"class":118},[100,95060,1280],{"class":205},[100,95062,295],{"class":209},[100,95064,1280],{"class":205},[100,95066,126],{"class":118},[100,95068,6915],{"class":122},[100,95070,126],{"class":118},[100,95072,866],{"class":158},[100,95074,126],{"class":118},[100,95076,594],{"class":118},[100,95078,1280],{"class":205},[100,95080,39024],{"class":209},[100,95082,1280],{"class":205},[100,95084,94981],{"class":118},[100,95086,95087,95089,95091,95093,95095,95097],{"class":102,"line":135},[100,95088,2782],{"class":110},[100,95090,3417],{"class":1077},[100,95092,1274],{"class":205},[100,95094,1235],{"class":209},[100,95096,1280],{"class":205},[100,95098,1980],{"class":110},[100,95100,95101,95103,95105,95107,95109,95111,95113,95115,95117,95119,95121,95123,95125,95127,95129,95131,95133,95135,95137,95139,95141,95143,95145,95147,95149,95151,95153,95155,95157],{"class":102,"line":142},[100,95102,3166],{"class":118},[100,95104,2782],{"class":110},[100,95106,3417],{"class":145},[100,95108,1274],{"class":205},[100,95110,3422],{"class":209},[100,95112,1280],{"class":205},[100,95114,1918],{"class":110},[100,95116,126],{"class":118},[100,95118,1974],{"class":110},[100,95120,3417],{"class":145},[100,95122,1274],{"class":205},[100,95124,3499],{"class":209},[100,95126,1280],{"class":205},[100,95128,1918],{"class":110},[100,95130,126],{"class":118},[100,95132,1974],{"class":110},[100,95134,3417],{"class":145},[100,95136,1274],{"class":205},[100,95138,4665],{"class":209},[100,95140,1280],{"class":205},[100,95142,1918],{"class":110},[100,95144,126],{"class":118},[100,95146,1974],{"class":110},[100,95148,3417],{"class":145},[100,95150,1274],{"class":205},[100,95152,1235],{"class":209},[100,95154,1280],{"class":205},[100,95156,1918],{"class":110},[100,95158,603],{"class":118},[100,95160,95161,95163,95165,95167,95169,95171,95173,95175,95177,95179,95181,95183,95185,95187],{"class":102,"line":152},[100,95162,3166],{"class":118},[100,95164,1280],{"class":205},[100,95166,295],{"class":209},[100,95168,1280],{"class":205},[100,95170,126],{"class":118},[100,95172,6915],{"class":122},[100,95174,126],{"class":118},[100,95176,866],{"class":158},[100,95178,126],{"class":118},[100,95180,594],{"class":118},[100,95182,1280],{"class":205},[100,95184,39024],{"class":209},[100,95186,1280],{"class":205},[100,95188,94981],{"class":118},[14,95190,95191],{},"These checks help you see:",[40,95193,95194,95200,95203],{},[43,95195,95196,95197,95199],{},"whether ",[17,95198,3146],{}," is really a list",[43,95201,95202],{},"what types are inside it",[43,95204,95205,95206,95208],{},"whether a nested list or ",[17,95207,930],{}," is present",[14,95210,95211,95212,95216],{},"If needed, use the ",[295,95213,95214,6354],{"href":6351},[17,95215,4430],{}," to inspect values one by one.",[77,95218,95220],{"id":95219},"common-situations-where-beginners-see-this","Common situations where beginners see this",[14,95222,95223],{},"Here are some common examples.",[675,95225,95227],{"id":95226},"joining-numbers-into-csv-style-text","Joining numbers into CSV-style text",[91,95229,95231],{"className":93,"code":95230,"language":95,"meta":96,"style":96},"numbers = [10, 20, 30]\ncsv_line = \",\".join(map(str, numbers))\nprint(csv_line)\n",[17,95232,95233,95253,95284],{"__ignoreMap":96},[100,95234,95235,95237,95239,95241,95243,95245,95247,95249,95251],{"class":102,"line":103},[100,95236,107],{"class":106},[100,95238,111],{"class":110},[100,95240,594],{"class":118},[100,95242,356],{"class":122},[100,95244,126],{"class":118},[100,95246,361],{"class":122},[100,95248,126],{"class":118},[100,95250,16342],{"class":122},[100,95252,603],{"class":118},[100,95254,95255,95258,95260,95262,95264,95266,95268,95270,95272,95274,95276,95278,95280,95282],{"class":102,"line":135},[100,95256,95257],{"class":106},"csv_line ",[100,95259,111],{"class":110},[100,95261,1708],{"class":205},[100,95263,126],{"class":209},[100,95265,206],{"class":205},[100,95267,290],{"class":118},[100,95269,19022],{"class":178},[100,95271,170],{"class":118},[100,95273,93874],{"class":114},[100,95275,170],{"class":118},[100,95277,3422],{"class":191},[100,95279,126],{"class":118},[100,95281,620],{"class":178},[100,95283,182],{"class":118},[100,95285,95286,95288,95290,95293],{"class":102,"line":142},[100,95287,372],{"class":114},[100,95289,170],{"class":118},[100,95291,95292],{"class":178},"csv_line",[100,95294,215],{"class":118},[14,95296,218],{},[91,95298,95300],{"className":93,"code":95299,"language":95,"meta":96,"style":96},"10,20,30\n",[17,95301,95302],{"__ignoreMap":96},[100,95303,95304,95306,95308,95310,95312],{"class":102,"line":103},[100,95305,356],{"class":122},[100,95307,126],{"class":118},[100,95309,6152],{"class":122},[100,95311,126],{"class":118},[100,95313,95314],{"class":122},"30\n",[675,95316,95318],{"id":95317},"joining-dictionary-values-that-are-not-all-strings","Joining dictionary values that are not all strings",[91,95320,95322],{"className":93,"code":95321,"language":95,"meta":96,"style":96},"data = {\"name\": \"Ana\", \"age\": 25}\nresult = \" - \".join(str(value) for value in data.values())\nprint(result)\n",[17,95323,95324,95360,95401],{"__ignoreMap":96},[100,95325,95326,95328,95330,95332,95334,95336,95338,95340,95342,95344,95346,95348,95350,95352,95354,95356,95358],{"class":102,"line":103},[100,95327,7909],{"class":106},[100,95329,111],{"class":110},[100,95331,12080],{"class":118},[100,95333,206],{"class":205},[100,95335,2853],{"class":209},[100,95337,206],{"class":205},[100,95339,89],{"class":118},[100,95341,1708],{"class":205},[100,95343,34819],{"class":209},[100,95345,206],{"class":205},[100,95347,126],{"class":118},[100,95349,1708],{"class":205},[100,95351,2770],{"class":209},[100,95353,206],{"class":205},[100,95355,89],{"class":118},[100,95357,16782],{"class":122},[100,95359,12093],{"class":118},[100,95361,95362,95364,95366,95368,95371,95373,95375,95377,95379,95381,95383,95385,95387,95389,95391,95393,95395,95397,95399],{"class":102,"line":135},[100,95363,11556],{"class":106},[100,95365,111],{"class":110},[100,95367,1708],{"class":205},[100,95369,95370],{"class":209}," - ",[100,95372,206],{"class":205},[100,95374,290],{"class":118},[100,95376,19022],{"class":178},[100,95378,170],{"class":118},[100,95380,3422],{"class":191},[100,95382,170],{"class":118},[100,95384,757],{"class":178},[100,95386,6155],{"class":118},[100,95388,7565],{"class":145},[100,95390,989],{"class":178},[100,95392,617],{"class":145},[100,95394,12102],{"class":178},[100,95396,290],{"class":118},[100,95398,4049],{"class":178},[100,95400,3370],{"class":118},[100,95402,95403,95405,95407,95409],{"class":102,"line":142},[100,95404,372],{"class":114},[100,95406,170],{"class":118},[100,95408,11580],{"class":178},[100,95410,215],{"class":118},[14,95412,218],{},[91,95414,95416],{"className":93,"code":95415,"language":95,"meta":96,"style":96},"Ana - 25\n",[17,95417,95418],{"__ignoreMap":96},[100,95419,95420,95423,95425],{"class":102,"line":103},[100,95421,95422],{"class":106},"Ana ",[100,95424,2193],{"class":110},[100,95426,16667],{"class":122},[675,95428,95430],{"id":95429},"joining-file-path-parts-with-numbers-mixed-in","Joining file path parts with numbers mixed in",[91,95432,95434],{"className":93,"code":95433,"language":95,"meta":96,"style":96},"parts = [\"user\", 42, \"report.txt\"]\npath = \"\u002F\".join(map(str, parts))\nprint(path)\n",[17,95435,95436,95465,95496],{"__ignoreMap":96},[100,95437,95438,95440,95442,95444,95446,95448,95450,95452,95455,95457,95459,95461,95463],{"class":102,"line":103},[100,95439,4345],{"class":106},[100,95441,111],{"class":110},[100,95443,594],{"class":118},[100,95445,206],{"class":205},[100,95447,15354],{"class":209},[100,95449,206],{"class":205},[100,95451,126],{"class":118},[100,95453,95454],{"class":122}," 42",[100,95456,126],{"class":118},[100,95458,1708],{"class":205},[100,95460,18326],{"class":209},[100,95462,206],{"class":205},[100,95464,603],{"class":118},[100,95466,95467,95469,95471,95473,95475,95477,95479,95481,95483,95485,95487,95489,95491,95494],{"class":102,"line":135},[100,95468,19365],{"class":106},[100,95470,111],{"class":110},[100,95472,1708],{"class":205},[100,95474,2558],{"class":209},[100,95476,206],{"class":205},[100,95478,290],{"class":118},[100,95480,19022],{"class":178},[100,95482,170],{"class":118},[100,95484,93874],{"class":114},[100,95486,170],{"class":118},[100,95488,3422],{"class":191},[100,95490,126],{"class":118},[100,95492,95493],{"class":178}," parts",[100,95495,182],{"class":118},[100,95497,95498,95500,95502,95504],{"class":102,"line":142},[100,95499,372],{"class":114},[100,95501,170],{"class":118},[100,95503,19017],{"class":178},[100,95505,215],{"class":118},[14,95507,218],{},[91,95509,95511],{"className":93,"code":95510,"language":95,"meta":96,"style":96},"user\u002F42\u002Freport.txt\n",[17,95512,95513],{"__ignoreMap":96},[100,95514,95515,95517,95519,95522,95524,95527,95529],{"class":102,"line":103},[100,95516,15354],{"class":106},[100,95518,2558],{"class":110},[100,95520,95521],{"class":122},"42",[100,95523,2558],{"class":110},[100,95525,95526],{"class":106},"report",[100,95528,290],{"class":118},[100,95530,95531],{"class":2494},"txt\n",[675,95533,95535],{"id":95534},"joining-a-list-built-from-input-and-calculations","Joining a list built from input and calculations",[91,95537,95539],{"className":93,"code":95538,"language":95,"meta":96,"style":96},"name = \"Sam\"\nscore = 95\n\nitems = [\"Name:\", name, \"Score:\", score]\nresult = \" \".join(map(str, items))\nprint(result)\n",[17,95540,95541,95553,95561,95565,95600,95628],{"__ignoreMap":96},[100,95542,95543,95545,95547,95549,95551],{"class":102,"line":103},[100,95544,1691],{"class":106},[100,95546,111],{"class":110},[100,95548,1708],{"class":205},[100,95550,29329],{"class":209},[100,95552,1714],{"class":205},[100,95554,95555,95557,95559],{"class":102,"line":135},[100,95556,2249],{"class":106},[100,95558,111],{"class":110},[100,95560,55862],{"class":122},[100,95562,95563],{"class":102,"line":142},[100,95564,139],{"emptyLinePlaceholder":138},[100,95566,95567,95569,95571,95573,95575,95578,95580,95582,95584,95586,95588,95591,95593,95595,95598],{"class":102,"line":152},[100,95568,3095],{"class":106},[100,95570,111],{"class":110},[100,95572,594],{"class":118},[100,95574,206],{"class":205},[100,95576,95577],{"class":209},"Name:",[100,95579,206],{"class":205},[100,95581,126],{"class":118},[100,95583,1703],{"class":106},[100,95585,126],{"class":118},[100,95587,1708],{"class":205},[100,95589,95590],{"class":209},"Score:",[100,95592,206],{"class":205},[100,95594,126],{"class":118},[100,95596,95597],{"class":106}," score",[100,95599,603],{"class":118},[100,95601,95602,95604,95606,95608,95610,95612,95614,95616,95618,95620,95622,95624,95626],{"class":102,"line":164},[100,95603,11556],{"class":106},[100,95605,111],{"class":110},[100,95607,1708],{"class":205},[100,95609,1708],{"class":205},[100,95611,290],{"class":118},[100,95613,19022],{"class":178},[100,95615,170],{"class":118},[100,95617,93874],{"class":114},[100,95619,170],{"class":118},[100,95621,3422],{"class":191},[100,95623,126],{"class":118},[100,95625,12000],{"class":178},[100,95627,182],{"class":118},[100,95629,95630,95632,95634,95636],{"class":102,"line":185},[100,95631,372],{"class":114},[100,95633,170],{"class":118},[100,95635,11580],{"class":178},[100,95637,215],{"class":118},[14,95639,218],{},[91,95641,95643],{"className":93,"code":95642,"language":95,"meta":96,"style":96},"Name: Sam Score: 95\n",[17,95644,95645],{"__ignoreMap":96},[100,95646,95647,95649,95651,95654,95656],{"class":102,"line":103},[100,95648,42750],{"class":106},[100,95650,89],{"class":118},[100,95652,95653],{"class":106}," Sam Score",[100,95655,89],{"class":118},[100,95657,55862],{"class":122},[77,95659,30537],{"id":30536},[14,95661,95662],{},"This error is closely related to other string conversion problems.",[14,95664,95665],{},"You may also see:",[40,95667,95668,95673,95677,95682],{},[43,95669,95670],{},[295,95671,95672],{"href":77673},"TypeError: can only concatenate str (not \"int\") to str",[43,95674,95675],{},[295,95676,78632],{"href":93778},[43,95678,95679,95681],{},[17,95680,3483],{}," when converting text to numbers",[43,95683,95684,95686],{},[17,95685,3738],{}," when calling string methods on non-strings",[77,95688,1514],{"id":1513},[675,95690,95692],{"id":95691},"why-does-the-error-say-sequence-item-0","Why does the error say sequence item 0?",[14,95694,95695],{},"It means the first item Python checked was not a string. The number may change if a later item is the first invalid one.",[675,95697,20428,95699,95701],{"id":95698},"does-join-convert-integers-to-strings-automatically",[17,95700,16071],{}," convert integers to strings automatically?",[14,95703,95704,95705,290],{},"No. You must convert each item yourself before calling ",[17,95706,16071],{},[675,95708,39391,95710,95712],{"id":95709},"should-i-use-mapstr-items-or-a-list-comprehension",[17,95711,94271],{}," or a list comprehension?",[14,95714,95715],{},"Both work.",[40,95717,95718,95723],{},[43,95719,95720,95722],{},[17,95721,94271],{}," is shorter",[43,95724,95725],{},"a list comprehension is better when you also want to filter, replace, or format values",[675,95727,95729,95730,95732],{"id":95728},"what-if-my-list-contains-none-values","What if my list contains ",[17,95731,930],{}," values?",[14,95734,95735,95736,95738],{},"Either remove ",[17,95737,930],{}," values or replace them before joining.",[77,95740,1554],{"id":1553},[40,95742,95743,95750,95755,95761,95767,95772],{},[43,95744,95745],{},[295,95746,95747,95749],{"href":93920},[17,95748,16071],{}," string method reference",[43,95751,95752],{},[295,95753,95754],{"href":94547},"How to join strings in Python",[43,95756,95757],{},[295,95758,95759,1576],{"href":94275},[17,95760,69900],{},[43,95762,95763],{},[295,95764,95765,1576],{"href":6351},[17,95766,4430],{},[43,95768,95769],{},[295,95770,95771],{"href":77673},"Fix: can only concatenate str (not \"int\") to str",[43,95773,95774],{},[295,95775,95776],{"href":93778},"Fix: 'NoneType' object is not iterable",[1589,95778,95779],{},"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 .sP7_E, html code.shiki .sP7_E{--shiki-light:#39ADB5;--shiki-default:#24292E;--shiki-dark:#E1E4E8}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 .srdBf, html code.shiki .srdBf{--shiki-light:#F76D47;--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 .sptTA, html code.shiki .sptTA{--shiki-light:#6182B8;--shiki-default:#005CC5;--shiki-dark:#79B8FF}html pre.shiki code .sZMiF, html code.shiki .sZMiF{--shiki-light:#E2931D;--shiki-default:#005CC5;--shiki-dark:#79B8FF}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 .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 .s39Yj, html code.shiki .s39Yj{--shiki-light:#39ADB5;--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 .skxfh, html code.shiki .skxfh{--shiki-light:#E53935;--shiki-default:#24292E;--shiki-dark:#E1E4E8}",{"title":96,"searchDepth":135,"depth":135,"links":95781},[95782,95783,95784,95785,95786,95787,95788,95797,95798,95804,95805,95814],{"id":79,"depth":135,"text":80},{"id":5880,"depth":135,"text":5881},{"id":11030,"depth":135,"text":11031},{"id":25090,"depth":135,"text":25091},{"id":94159,"depth":135,"text":94160},{"id":94294,"depth":135,"text":94295},{"id":94551,"depth":135,"text":95789,"children":95790},"Fix 3: Remove or replace None values",[95791,95793,95795],{"id":94562,"depth":142,"text":95792},"Remove None values",{"id":94684,"depth":142,"text":95794},"Replace None with an empty string",{"id":94793,"depth":142,"text":95796},"Replace None with a label",{"id":94933,"depth":135,"text":94934},{"id":95219,"depth":135,"text":95220,"children":95799},[95800,95801,95802,95803],{"id":95226,"depth":142,"text":95227},{"id":95317,"depth":142,"text":95318},{"id":95429,"depth":142,"text":95430},{"id":95534,"depth":142,"text":95535},{"id":30536,"depth":135,"text":30537},{"id":1513,"depth":135,"text":1514,"children":95806},[95807,95808,95810,95812],{"id":95691,"depth":142,"text":95692},{"id":95698,"depth":142,"text":95809},"Does join() convert integers to strings automatically?",{"id":95709,"depth":142,"text":95811},"Should I use map(str, items) or a list comprehension?",{"id":95728,"depth":142,"text":95813},"What if my list contains None values?",{"id":1553,"depth":135,"text":1554},"Master typeerror sequence item 0 expected str instance fix in our comprehensive Python beginner guide.",{},"\u002Ferrors\u002Ftypeerror-sequence-item-0-expected-str-instance-fix",{"title":93784,"description":95815},"errors\u002Ftypeerror-sequence-item-0-expected-str-instance-fix","I1nIksWL7gwDFaZO4cpgnuIk6ryx3y9aKJRxdTkl5eM",{"id":95822,"title":95823,"body":95824,"description":96921,"extension":1623,"meta":96922,"navigation":138,"path":82157,"seo":96923,"stem":96924,"__hash__":96925},"content\u002Ferrors\u002Ftypeerror-str-object-cannot-be-interpreted-as-an-integer-fix.md","TypeError: 'str' object cannot be interpreted as an integer (Fix)",{"type":7,"value":95825,"toc":96892},[95826,95831,95838,95840,95901,95904,95906,95909,95925,95928,95946,95948,95951,95953,95972,95974,96008,96010,96028,96035,96039,96042,96060,96062,96129,96132,96134,96142,96196,96200,96209,96211,96220,96277,96282,96306,96310,96359,96361,96370,96374,96391,96397,96403,96406,96437,96445,96450,96465,96471,96476,96478,96481,96483,96544,96548,96571,96575,96688,96693,96695,96697,96717,96720,96775,96781,96783,96789,96799,96803,96806,96813,96827,96835,96840,96844,96852,96854,96889],[10,95827,74872,95829,7375],{"id":95828},"typeerror-str-object-cannot-be-interpreted-as-an-integer-fix",[17,95830,82160],{},[14,95832,31738,95833,95837],{},[22,95834,95835],{},[17,95836,76389],{},". This page explains what the error means, common causes, and the fastest ways to correct your code.",[77,95839,80],{"id":79},[91,95841,95843],{"className":93,"code":95842,"language":95,"meta":96,"style":96},"text = \"5\"\nnumber = int(text)\n\nfor i in range(number):\n    print(i)\n",[17,95844,95845,95857,95871,95875,95891],{"__ignoreMap":96},[100,95846,95847,95849,95851,95853,95855],{"class":102,"line":103},[100,95848,4331],{"class":106},[100,95850,111],{"class":110},[100,95852,1708],{"class":205},[100,95854,3557],{"class":209},[100,95856,1714],{"class":205},[100,95858,95859,95861,95863,95865,95867,95869],{"class":102,"line":135},[100,95860,1901],{"class":106},[100,95862,111],{"class":110},[100,95864,3389],{"class":191},[100,95866,170],{"class":118},[100,95868,4468],{"class":178},[100,95870,215],{"class":118},[100,95872,95873],{"class":102,"line":142},[100,95874,139],{"emptyLinePlaceholder":138},[100,95876,95877,95879,95881,95883,95885,95887,95889],{"class":102,"line":152},[100,95878,71],{"class":145},[100,95880,29838],{"class":106},[100,95882,617],{"class":145},[100,95884,29843],{"class":114},[100,95886,170],{"class":118},[100,95888,631],{"class":178},[100,95890,522],{"class":118},[100,95892,95893,95895,95897,95899],{"class":102,"line":164},[100,95894,200],{"class":114},[100,95896,170],{"class":118},[100,95898,29858],{"class":178},[100,95900,215],{"class":118},[14,95902,95903],{},"This error happens when Python expects an integer, but you give it a string instead. Convert the string first if it contains a number.",[77,95905,5881],{"id":5880},[14,95907,95908],{},"Python is telling you that:",[40,95910,95911,95917,95922],{},[43,95912,95913,95914],{},"It expected an ",[22,95915,95916],{},"integer",[43,95918,95919,95920],{},"It received a ",[22,95921,75241],{},[43,95923,95924],{},"It cannot use that string where a whole number is required",[14,95926,95927],{},"This often appears when working with:",[40,95929,95930,95937,95940,95943],{},[43,95931,95932],{},[295,95933,95935],{"href":95934},"\u002Freference\u002Fpython-range-function-explained",[17,95936,32990],{},[43,95938,95939],{},"list indexes or slices",[43,95941,95942],{},"repetition operations",[43,95944,95945],{},"functions that require integer arguments",[77,95947,3944],{"id":3943},[14,95949,95950],{},"This error usually happens because a value looks like a number, but is actually text.",[14,95952,23481],{},[40,95954,95955,95963,95966,95969],{},[43,95956,95957,95962],{},[295,95958,95960],{"href":95959},"\u002Freference\u002Fpython-input-function-explained",[17,95961,14906],{}," always returns a string",[43,95964,95965],{},"Numbers read from files or APIs may arrive as strings",[43,95967,95968],{},"Quotes around a number make it a string, not an integer",[43,95970,95971],{},"A variable may contain text when you think it contains a number",[14,95973,580],{},[91,95975,95977],{"className":93,"code":95976,"language":95,"meta":96,"style":96},"count = \"5\"   # string\nprint(type(count))\n",[17,95978,95979,95994],{"__ignoreMap":96},[100,95980,95981,95983,95985,95987,95989,95991],{"class":102,"line":103},[100,95982,2176],{"class":106},[100,95984,111],{"class":110},[100,95986,1708],{"class":205},[100,95988,3557],{"class":209},[100,95990,206],{"class":205},[100,95992,95993],{"class":414},"   # string\n",[100,95995,95996,95998,96000,96002,96004,96006],{"class":102,"line":135},[100,95997,372],{"class":114},[100,95999,170],{"class":118},[100,96001,1250],{"class":191},[100,96003,170],{"class":118},[100,96005,51844],{"class":178},[100,96007,182],{"class":118},[14,96009,218],{},[91,96011,96012],{"className":93,"code":14960,"language":95,"meta":96,"style":96},[17,96013,96014],{"__ignoreMap":96},[100,96015,96016,96018,96020,96022,96024,96026],{"class":102,"line":103},[100,96017,2782],{"class":110},[100,96019,3417],{"class":1077},[100,96021,1274],{"class":205},[100,96023,3422],{"class":209},[100,96025,1280],{"class":205},[100,96027,1980],{"class":110},[14,96029,96030,96031,96034],{},"Even though ",[17,96032,96033],{},"\"5\""," looks numeric, Python treats it as text.",[77,96036,96038],{"id":96037},"common-places-where-it-appears","Common places where it appears",[14,96040,96041],{},"You will often see this error in code like this:",[40,96043,96044,96051,96054,96057],{},[43,96045,96046,15957,96049],{},[17,96047,96048],{},"range(\"5\")",[17,96050,84233],{},[43,96052,96053],{},"using a string as a list index",[43,96055,96056],{},"passing a string to a function that needs an integer",[43,96058,96059],{},"using user input directly in a loop",[14,96061,1844],{},[91,96063,96065],{"className":93,"code":96064,"language":95,"meta":96,"style":96},"items = [\"a\", \"b\", \"c\"]\nindex = \"1\"\n\nprint(items[index])\n",[17,96066,96067,96099,96111,96115],{"__ignoreMap":96},[100,96068,96069,96071,96073,96075,96077,96079,96081,96083,96085,96087,96089,96091,96093,96095,96097],{"class":102,"line":103},[100,96070,3095],{"class":106},[100,96072,111],{"class":110},[100,96074,594],{"class":118},[100,96076,206],{"class":205},[100,96078,295],{"class":209},[100,96080,206],{"class":205},[100,96082,126],{"class":118},[100,96084,1708],{"class":205},[100,96086,4027],{"class":209},[100,96088,206],{"class":205},[100,96090,126],{"class":118},[100,96092,1708],{"class":205},[100,96094,4036],{"class":209},[100,96096,206],{"class":205},[100,96098,603],{"class":118},[100,96100,96101,96103,96105,96107,96109],{"class":102,"line":135},[100,96102,3116],{"class":106},[100,96104,111],{"class":110},[100,96106,1708],{"class":205},[100,96108,123],{"class":209},[100,96110,1714],{"class":205},[100,96112,96113],{"class":102,"line":142},[100,96114,139],{"emptyLinePlaceholder":138},[100,96116,96117,96119,96121,96123,96125,96127],{"class":102,"line":152},[100,96118,372],{"class":114},[100,96120,170],{"class":118},[100,96122,3146],{"class":178},[100,96124,3166],{"class":118},[100,96126,3169],{"class":178},[100,96128,132],{"class":118},[14,96130,96131],{},"This fails because list indexes must be integers, not strings.",[77,96133,11031],{"id":11030},[14,96135,96136,96137,96139,96140,290],{},"A very common case is using ",[17,96138,14906],{}," directly inside ",[17,96141,32990],{},[91,96143,96145],{"className":93,"code":96144,"language":95,"meta":96,"style":96},"count = input(\"How many times? \")\n\nfor i in range(count):\n    print(i)\n",[17,96146,96147,96166,96170,96186],{"__ignoreMap":96},[100,96148,96149,96151,96153,96155,96157,96159,96162,96164],{"class":102,"line":103},[100,96150,2176],{"class":106},[100,96152,111],{"class":110},[100,96154,7221],{"class":114},[100,96156,170],{"class":118},[100,96158,206],{"class":205},[100,96160,96161],{"class":209},"How many times? ",[100,96163,206],{"class":205},[100,96165,215],{"class":118},[100,96167,96168],{"class":102,"line":135},[100,96169,139],{"emptyLinePlaceholder":138},[100,96171,96172,96174,96176,96178,96180,96182,96184],{"class":102,"line":142},[100,96173,71],{"class":145},[100,96175,29838],{"class":106},[100,96177,617],{"class":145},[100,96179,29843],{"class":114},[100,96181,170],{"class":118},[100,96183,51844],{"class":178},[100,96185,522],{"class":118},[100,96187,96188,96190,96192,96194],{"class":102,"line":152},[100,96189,200],{"class":114},[100,96191,170],{"class":118},[100,96193,29858],{"class":178},[100,96195,215],{"class":118},[14,96197,87078,96198,2929],{},[17,96199,51844],{},[14,96201,14954,96202,96204,96205,9472,96207,290],{},[17,96203,3557],{},", the value is still ",[17,96206,96033],{},[17,96208,3557],{},[77,96210,6244],{"id":6243},[14,96212,96213,96214,96219],{},"The usual fix is to convert the string with ",[295,96215,96217],{"href":96216},"\u002Freference\u002Fpython-int-function-explained",[17,96218,6119],{}," before using it.",[91,96221,96223],{"className":93,"code":96222,"language":95,"meta":96,"style":96},"count = int(input(\"How many times? \"))\n\nfor i in range(count):\n    print(i)\n",[17,96224,96225,96247,96251,96267],{"__ignoreMap":96},[100,96226,96227,96229,96231,96233,96235,96237,96239,96241,96243,96245],{"class":102,"line":103},[100,96228,2176],{"class":106},[100,96230,111],{"class":110},[100,96232,3389],{"class":191},[100,96234,170],{"class":118},[100,96236,6210],{"class":114},[100,96238,170],{"class":118},[100,96240,206],{"class":205},[100,96242,96161],{"class":209},[100,96244,206],{"class":205},[100,96246,182],{"class":118},[100,96248,96249],{"class":102,"line":135},[100,96250,139],{"emptyLinePlaceholder":138},[100,96252,96253,96255,96257,96259,96261,96263,96265],{"class":102,"line":142},[100,96254,71],{"class":145},[100,96256,29838],{"class":106},[100,96258,617],{"class":145},[100,96260,29843],{"class":114},[100,96262,170],{"class":118},[100,96264,51844],{"class":178},[100,96266,522],{"class":118},[100,96268,96269,96271,96273,96275],{"class":102,"line":152},[100,96270,200],{"class":114},[100,96272,170],{"class":118},[100,96274,29858],{"class":178},[100,96276,215],{"class":118},[14,96278,96279,96280,89],{},"Output if the user enters ",[17,96281,3557],{},[91,96283,96284],{"className":93,"code":80837,"language":95,"meta":96,"style":96},[17,96285,96286,96290,96294,96298,96302],{"__ignoreMap":96},[100,96287,96288],{"class":102,"line":103},[100,96289,44572],{"class":122},[100,96291,96292],{"class":102,"line":135},[100,96293,228],{"class":122},[100,96295,96296],{"class":102,"line":142},[100,96297,233],{"class":122},[100,96299,96300],{"class":102,"line":152},[100,96301,1500],{"class":122},[100,96303,96304],{"class":102,"line":164},[100,96305,63793],{"class":122},[675,96307,96309],{"id":96308},"another-example","Another example",[91,96311,96313],{"className":93,"code":96312,"language":95,"meta":96,"style":96},"text = \"10\"\nnumber = int(text)\n\nprint(number + 2)\n",[17,96314,96315,96327,96341,96345],{"__ignoreMap":96},[100,96316,96317,96319,96321,96323,96325],{"class":102,"line":103},[100,96318,4331],{"class":106},[100,96320,111],{"class":110},[100,96322,1708],{"class":205},[100,96324,356],{"class":209},[100,96326,1714],{"class":205},[100,96328,96329,96331,96333,96335,96337,96339],{"class":102,"line":135},[100,96330,1901],{"class":106},[100,96332,111],{"class":110},[100,96334,3389],{"class":191},[100,96336,170],{"class":118},[100,96338,4468],{"class":178},[100,96340,215],{"class":118},[100,96342,96343],{"class":102,"line":142},[100,96344,139],{"emptyLinePlaceholder":138},[100,96346,96347,96349,96351,96353,96355,96357],{"class":102,"line":152},[100,96348,372],{"class":114},[100,96350,170],{"class":118},[100,96352,1901],{"class":178},[100,96354,6547],{"class":110},[100,96356,129],{"class":122},[100,96358,215],{"class":118},[14,96360,218],{},[91,96362,96364],{"className":93,"code":96363,"language":95,"meta":96,"style":96},"12\n",[17,96365,96366],{"__ignoreMap":96},[100,96367,96368],{"class":102,"line":103},[100,96369,96363],{"class":122},[675,96371,96373],{"id":96372},"good-things-to-check","Good things to check",[40,96375,96376,96381,96385,96388],{},[43,96377,96378,96379],{},"Convert the value with ",[17,96380,6119],{},[43,96382,15645,96383],{},[17,96384,4430],{},[43,96386,96387],{},"Make sure the string contains a valid whole number",[43,96389,96390],{},"Validate input before conversion",[14,96392,96393,96394,290],{},"If you need more help with conversion, see ",[295,96395,14983],{"href":96396},"\u002Fhow-to\u002Fhow-to-convert-string-to-int-in-python",[77,96398,11734,96400,96402],{"id":96399},"if-int-also-fails",[17,96401,6119],{}," also fails",[14,96404,96405],{},"Sometimes you fix one problem and get a new one:",[91,96407,96409],{"className":93,"code":96408,"language":95,"meta":96,"style":96},"text = \"3.5\"\nnumber = int(text)\n",[17,96410,96411,96423],{"__ignoreMap":96},[100,96412,96413,96415,96417,96419,96421],{"class":102,"line":103},[100,96414,4331],{"class":106},[100,96416,111],{"class":110},[100,96418,1708],{"class":205},[100,96420,80892],{"class":209},[100,96422,1714],{"class":205},[100,96424,96425,96427,96429,96431,96433,96435],{"class":102,"line":135},[100,96426,1901],{"class":106},[100,96428,111],{"class":110},[100,96430,3389],{"class":191},[100,96432,170],{"class":118},[100,96434,4468],{"class":178},[100,96436,215],{"class":118},[14,96438,67137,96439,80946,96441,96444],{},[17,96440,3483],{},[17,96442,96443],{},"\"3.5\""," is not a valid integer string.",[14,96446,96447,96448,89],{},"Other examples that fail with ",[17,96449,6119],{},[40,96451,96452,96456,96461],{},[43,96453,96454],{},[17,96455,96443],{},[43,96457,96458],{},[17,96459,96460],{},"\"ten\"",[43,96462,96463],{},[17,96464,11875],{},[14,96466,96467,96468,96470],{},"If decimals are allowed, use ",[17,96469,59778],{}," instead. If not, clean and validate the input first.",[14,96472,31531,96473],{},[295,96474,17024],{"href":96475},"\u002Ferrors\u002Fvalueerror-invalid-literal-for-int-with-base-10-fix",[77,96477,3274],{"id":3273},[14,96479,96480],{},"If you are not sure where the problem is coming from, check the value before the line that fails.",[14,96482,5295],{},[91,96484,96486],{"className":93,"code":96485,"language":95,"meta":96,"style":96},"print(value)\nprint(type(value))\nprint(repr(value))\nprint(isinstance(value, int))\n",[17,96487,96488,96498,96512,96526],{"__ignoreMap":96},[100,96489,96490,96492,96494,96496],{"class":102,"line":103},[100,96491,372],{"class":114},[100,96493,170],{"class":118},[100,96495,757],{"class":178},[100,96497,215],{"class":118},[100,96499,96500,96502,96504,96506,96508,96510],{"class":102,"line":135},[100,96501,372],{"class":114},[100,96503,170],{"class":118},[100,96505,1250],{"class":191},[100,96507,170],{"class":118},[100,96509,757],{"class":178},[100,96511,182],{"class":118},[100,96513,96514,96516,96518,96520,96522,96524],{"class":102,"line":142},[100,96515,372],{"class":114},[100,96517,170],{"class":118},[100,96519,12228],{"class":114},[100,96521,170],{"class":118},[100,96523,757],{"class":178},[100,96525,182],{"class":118},[100,96527,96528,96530,96532,96534,96536,96538,96540,96542],{"class":102,"line":152},[100,96529,372],{"class":114},[100,96531,170],{"class":118},[100,96533,6994],{"class":114},[100,96535,170],{"class":118},[100,96537,757],{"class":178},[100,96539,126],{"class":118},[100,96541,3389],{"class":191},[100,96543,182],{"class":118},[675,96545,96547],{"id":96546},"what-each-one-helps-you-see","What each one helps you see",[40,96549,96550,96554,96559,96565],{},[43,96551,96552,7047],{},[17,96553,7046],{},[43,96555,96556,96558],{},[17,96557,7052],{}," shows whether it is a string or integer",[43,96560,96561,96564],{},[17,96562,96563],{},"print(repr(value))"," helps reveal spaces or hidden characters",[43,96566,96567,96570],{},[17,96568,96569],{},"isinstance(value, int)"," checks whether the value is an integer",[675,96572,96574],{"id":96573},"example-debugging-session","Example debugging session",[91,96576,96578],{"className":93,"code":96577,"language":95,"meta":96,"style":96},"value = input(\"Enter a number: \")\n\nprint(value)\nprint(type(value))\nprint(repr(value))\nprint(isinstance(value, int))\n\nfor i in range(value):\n    print(i)\n",[17,96579,96580,96598,96602,96612,96626,96640,96658,96662,96678],{"__ignoreMap":96},[100,96581,96582,96584,96586,96588,96590,96592,96594,96596],{"class":102,"line":103},[100,96583,3318],{"class":106},[100,96585,111],{"class":110},[100,96587,7221],{"class":114},[100,96589,170],{"class":118},[100,96591,206],{"class":205},[100,96593,6217],{"class":209},[100,96595,206],{"class":205},[100,96597,215],{"class":118},[100,96599,96600],{"class":102,"line":135},[100,96601,139],{"emptyLinePlaceholder":138},[100,96603,96604,96606,96608,96610],{"class":102,"line":142},[100,96605,372],{"class":114},[100,96607,170],{"class":118},[100,96609,757],{"class":178},[100,96611,215],{"class":118},[100,96613,96614,96616,96618,96620,96622,96624],{"class":102,"line":152},[100,96615,372],{"class":114},[100,96617,170],{"class":118},[100,96619,1250],{"class":191},[100,96621,170],{"class":118},[100,96623,757],{"class":178},[100,96625,182],{"class":118},[100,96627,96628,96630,96632,96634,96636,96638],{"class":102,"line":164},[100,96629,372],{"class":114},[100,96631,170],{"class":118},[100,96633,12228],{"class":114},[100,96635,170],{"class":118},[100,96637,757],{"class":178},[100,96639,182],{"class":118},[100,96641,96642,96644,96646,96648,96650,96652,96654,96656],{"class":102,"line":185},[100,96643,372],{"class":114},[100,96645,170],{"class":118},[100,96647,6994],{"class":114},[100,96649,170],{"class":118},[100,96651,757],{"class":178},[100,96653,126],{"class":118},[100,96655,3389],{"class":191},[100,96657,182],{"class":118},[100,96659,96660],{"class":102,"line":197},[100,96661,139],{"emptyLinePlaceholder":138},[100,96663,96664,96666,96668,96670,96672,96674,96676],{"class":102,"line":771},[100,96665,71],{"class":145},[100,96667,29838],{"class":106},[100,96669,617],{"class":145},[100,96671,29843],{"class":114},[100,96673,170],{"class":118},[100,96675,757],{"class":178},[100,96677,522],{"class":118},[100,96679,96680,96682,96684,96686],{"class":102,"line":787},[100,96681,200],{"class":114},[100,96683,170],{"class":118},[100,96685,29858],{"class":178},[100,96687,215],{"class":118},[14,96689,85669,96690,96692],{},[17,96691,3557],{},", the debug output will show that the value is still a string.",[77,96694,7117],{"id":7116},[14,96696,15922],{},[40,96698,96699,96705,96708,96711,96714],{},[43,96700,1357,96701,96139,96703],{},[17,96702,14906],{},[17,96704,32990],{},[43,96706,96707],{},"Storing numbers as quoted strings",[43,96709,96710],{},"Reading numeric text from a file and not converting it",[43,96712,96713],{},"Passing a string index to a list or tuple operation",[43,96715,96716],{},"Using command-line arguments without conversion",[14,96718,96719],{},"Also watch for cases where a variable is reassigned:",[91,96721,96723],{"className":93,"code":96722,"language":95,"meta":96,"style":96},"count = 5\ncount = \"5\"\n\nfor i in range(count):\n    print(i)\n",[17,96724,96725,96733,96745,96749,96765],{"__ignoreMap":96},[100,96726,96727,96729,96731],{"class":102,"line":103},[100,96728,2176],{"class":106},[100,96730,111],{"class":110},[100,96732,37580],{"class":122},[100,96734,96735,96737,96739,96741,96743],{"class":102,"line":135},[100,96736,2176],{"class":106},[100,96738,111],{"class":110},[100,96740,1708],{"class":205},[100,96742,3557],{"class":209},[100,96744,1714],{"class":205},[100,96746,96747],{"class":102,"line":142},[100,96748,139],{"emptyLinePlaceholder":138},[100,96750,96751,96753,96755,96757,96759,96761,96763],{"class":102,"line":152},[100,96752,71],{"class":145},[100,96754,29838],{"class":106},[100,96756,617],{"class":145},[100,96758,29843],{"class":114},[100,96760,170],{"class":118},[100,96762,51844],{"class":178},[100,96764,522],{"class":118},[100,96766,96767,96769,96771,96773],{"class":102,"line":164},[100,96768,200],{"class":114},[100,96770,170],{"class":118},[100,96772,29858],{"class":178},[100,96774,215],{"class":118},[14,96776,96777,96778,96780],{},"The second assignment changes ",[17,96779,51844],{}," from an integer to a string.",[77,96782,1514],{"id":1513},[675,96784,9137,96786,96788],{"id":96785},"why-does-input-cause-this-error-so-often",[17,96787,14906],{}," cause this error so often?",[14,96790,14478,96791,96795,96796,96798],{},[295,96792,96793],{"href":95959},[17,96794,14906],{}," always returns a string. You usually need ",[17,96797,7143],{}," when asking for a whole number.",[675,96800,96802],{"id":96801},"what-is-the-difference-between-a-string-and-an-integer","What is the difference between a string and an integer?",[14,96804,96805],{},"A string is text, even if it looks like a number. An integer is a whole number value Python can use in numeric operations.",[675,96807,39391,96809,3178,96811,11353],{"id":96808},"should-i-use-int-or-float",[17,96810,6119],{},[17,96812,59778],{},[14,96814,241,96815,96817,96818,96820,96821,96823,96824,290],{},[17,96816,6119],{}," for whole numbers like ",[17,96819,3557],{},". Use ",[17,96822,59778],{}," for decimal numbers like ",[17,96825,96826],{},"5.2",[675,96828,9137,96830,96832,96833,11353],{"id":96829},"why-does-5-not-work-the-same-as-5",[17,96831,96033],{}," not work the same as ",[17,96834,3557],{},[14,96836,14478,96837,96839],{},[17,96838,96033],{}," is text inside quotes. Python treats it as a string, not a number.",[675,96841,96843],{"id":96842},"can-i-fix-this-with-type-checking-first","Can I fix this with type checking first?",[14,96845,96846,96847,3178,96849,96851],{},"Yes. You can inspect the value with ",[17,96848,4430],{},[17,96850,5256],{}," before using it where an integer is required.",[77,96853,1554],{"id":1553},[40,96855,96856,96862,96867,96873,96879,96883],{},[43,96857,96858],{},[295,96859,1572,96860,1576],{"href":96216},[17,96861,6119],{},[43,96863,96864],{},[295,96865,96866],{"href":96396},"How to convert a string to int in Python",[43,96868,96869],{},[295,96870,1572,96871,1576],{"href":95959},[17,96872,14906],{},[43,96874,96875],{},[295,96876,1572,96877,1576],{"href":95934},[17,96878,32990],{},[43,96880,96881],{},[295,96882,17024],{"href":96475},[43,96884,96885],{},[295,96886,74872,96887],{"href":82194},[17,96888,80771],{},[1589,96890,96891],{},"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 .sZMiF, html code.shiki .sZMiF{--shiki-light:#E2931D;--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 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 .sptTA, html code.shiki .sptTA{--shiki-light:#6182B8;--shiki-default:#005CC5;--shiki-dark:#79B8FF}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 .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 .sbsja, html code.shiki .sbsja{--shiki-light:#9C3EDA;--shiki-default:#D73A49;--shiki-dark:#F97583}html pre.shiki code .srdBf, html code.shiki .srdBf{--shiki-light:#F76D47;--shiki-default:#005CC5;--shiki-dark:#79B8FF}",{"title":96,"searchDepth":135,"depth":135,"links":96893},[96894,96895,96896,96897,96898,96899,96903,96905,96909,96910,96920],{"id":79,"depth":135,"text":80},{"id":5880,"depth":135,"text":5881},{"id":3943,"depth":135,"text":3944},{"id":96037,"depth":135,"text":96038},{"id":11030,"depth":135,"text":11031},{"id":6243,"depth":135,"text":6244,"children":96900},[96901,96902],{"id":96308,"depth":142,"text":96309},{"id":96372,"depth":142,"text":96373},{"id":96399,"depth":135,"text":96904},"If int() also fails",{"id":3273,"depth":135,"text":3274,"children":96906},[96907,96908],{"id":96546,"depth":142,"text":96547},{"id":96573,"depth":142,"text":96574},{"id":7116,"depth":135,"text":7117},{"id":1513,"depth":135,"text":1514,"children":96911},[96912,96914,96915,96917,96919],{"id":96785,"depth":142,"text":96913},"Why does input() cause this error so often?",{"id":96801,"depth":142,"text":96802},{"id":96808,"depth":142,"text":96916},"Should I use int() or float()?",{"id":96829,"depth":142,"text":96918},"Why does \"5\" not work the same as 5?",{"id":96842,"depth":142,"text":96843},{"id":1553,"depth":135,"text":1554},"Master typeerror str object cannot be interpreted as an integer fix in our comprehensive Python beginner guide.",{},{"title":95823,"description":96921},"errors\u002Ftypeerror-str-object-cannot-be-interpreted-as-an-integer-fix","LTYZZ29TNQ-_-11__UQUAAE3nW2dIe7nFSGgxdxIx1g",{"id":96927,"title":96928,"body":96929,"description":98651,"extension":1623,"meta":98652,"navigation":138,"path":98653,"seo":98654,"stem":98655,"__hash__":98656},"content\u002Ferrors\u002Ftypeerror-unexpected-keyword-argument-fix.md","TypeError: unexpected keyword argument (Fix)",{"type":7,"value":96930,"toc":98627},[96931,96934,96937,96951,96953,96964,96966,97031,97034,97036,97039,97042,97061,97067,97069,97071,97128,97130,97154,97156,97175,97177,97234,97236,97244,97246,97248,97265,97267,97270,97287,97293,97297,97299,97302,97362,97369,97372,97432,97435,97450,97453,97508,97512,97515,97517,97570,97572,97580,97582,97593,97599,97617,97620,97629,97632,97636,97639,97645,97702,97705,97843,97845,97864,97867,97872,97879,97884,97887,97900,97902,98007,98009,98055,98058,98181,98183,98204,98209,98213,98216,98219,98307,98309,98317,98320,98340,98343,98347,98349,98369,98372,98426,98428,98501,98509,98511,98514,98533,98539,98541,98544,98564,98566,98570,98577,98581,98584,98590,98593,98597,98600,98602,98624],[10,96932,96928],{"id":96933},"typeerror-unexpected-keyword-argument-fix",[14,96935,96936],{},"This error happens when you call a function, method, or class constructor with a keyword name that it does not accept.",[14,96938,96939,96940,96943,96944,96946,96947,77898,96949,290],{},"In Python, a keyword argument is a named argument like ",[17,96941,96942],{},"name=\"Sam\"",". If the function expects ",[17,96945,2853],{}," but you pass ",[17,96948,11330],{},[17,96950,3538],{},[14,96952,93809],{},[40,96954,96955,96958,96961],{},[43,96956,96957],{},"Use the exact parameter name the function expects",[43,96959,96960],{},"Or pass the value without a keyword if positional arguments are allowed",[43,96962,96963],{},"Or update your own function definition if needed",[77,96965,80],{"id":79},[91,96967,96969],{"className":93,"code":96968,"language":95,"meta":96,"style":96},"def greet(name):\n    print(\"Hello\", name)\n\ngreet(name=\"Sam\")   # works\n# greet(username=\"Sam\")  # TypeError: unexpected keyword argument 'username'\n",[17,96970,96971,96983,97001,97005,97026],{"__ignoreMap":96},[100,96972,96973,96975,96977,96979,96981],{"class":102,"line":103},[100,96974,1078],{"class":1077},[100,96976,2848],{"class":1081},[100,96978,170],{"class":118},[100,96980,2853],{"class":2519},[100,96982,522],{"class":118},[100,96984,96985,96987,96989,96991,96993,96995,96997,96999],{"class":102,"line":135},[100,96986,200],{"class":114},[100,96988,170],{"class":118},[100,96990,206],{"class":205},[100,96992,7683],{"class":209},[100,96994,206],{"class":205},[100,96996,126],{"class":118},[100,96998,1703],{"class":178},[100,97000,215],{"class":118},[100,97002,97003],{"class":102,"line":142},[100,97004,139],{"emptyLinePlaceholder":138},[100,97006,97007,97009,97011,97013,97015,97017,97019,97021,97023],{"class":102,"line":152},[100,97008,2911],{"class":178},[100,97010,170],{"class":118},[100,97012,2853],{"class":18141},[100,97014,111],{"class":110},[100,97016,206],{"class":205},[100,97018,29329],{"class":209},[100,97020,206],{"class":205},[100,97022,6155],{"class":118},[100,97024,97025],{"class":414},"   # works\n",[100,97027,97028],{"class":102,"line":164},[100,97029,97030],{"class":414},"# greet(username=\"Sam\")  # TypeError: unexpected keyword argument 'username'\n",[14,97032,97033],{},"Use the exact parameter name from the function definition or documentation.",[77,97035,5881],{"id":5880},[14,97037,97038],{},"Python found a keyword argument name that the function does not accept.",[14,97040,97041],{},"This usually means:",[40,97043,97044,97050,97055,97058],{},[43,97045,97046,97047],{},"You passed something like ",[17,97048,97049],{},"username=\"Sam\"",[43,97051,97052,97053],{},"But the function does not have a parameter named ",[17,97054,11330],{},[43,97056,97057],{},"The function may still accept the value in another way, such as a positional argument",[43,97059,97060],{},"The traceback usually points to the line where you called the function",[14,97062,97063,97064,97066],{},"If you are new to this topic, it helps to understand how ",[295,97065,88527],{"href":88526}," work.",[77,97068,91663],{"id":91662},[14,97070,5948],{},[91,97072,97074],{"className":93,"code":97073,"language":95,"meta":96,"style":96},"def greet(name):\n    print(\"Hello\", name)\n\ngreet(username=\"Sam\")\n",[17,97075,97076,97088,97106,97110],{"__ignoreMap":96},[100,97077,97078,97080,97082,97084,97086],{"class":102,"line":103},[100,97079,1078],{"class":1077},[100,97081,2848],{"class":1081},[100,97083,170],{"class":118},[100,97085,2853],{"class":2519},[100,97087,522],{"class":118},[100,97089,97090,97092,97094,97096,97098,97100,97102,97104],{"class":102,"line":135},[100,97091,200],{"class":114},[100,97093,170],{"class":118},[100,97095,206],{"class":205},[100,97097,7683],{"class":209},[100,97099,206],{"class":205},[100,97101,126],{"class":118},[100,97103,1703],{"class":178},[100,97105,215],{"class":118},[100,97107,97108],{"class":102,"line":142},[100,97109,139],{"emptyLinePlaceholder":138},[100,97111,97112,97114,97116,97118,97120,97122,97124,97126],{"class":102,"line":152},[100,97113,2911],{"class":178},[100,97115,170],{"class":118},[100,97117,11330],{"class":18141},[100,97119,111],{"class":110},[100,97121,206],{"class":205},[100,97123,29329],{"class":209},[100,97125,206],{"class":205},[100,97127,215],{"class":118},[14,97129,218],{},[91,97131,97133],{"className":93,"code":97132,"language":95,"meta":96,"style":96},"TypeError: greet() got an unexpected keyword argument 'username'\n",[17,97134,97135],{"__ignoreMap":96},[100,97136,97137,97139,97141,97143,97145,97148,97150,97152],{"class":102,"line":103},[100,97138,3538],{"class":191},[100,97140,89],{"class":118},[100,97142,2848],{"class":178},[100,97144,7562],{"class":118},[100,97146,97147],{"class":106}," got an unexpected keyword argument ",[100,97149,1280],{"class":205},[100,97151,11330],{"class":209},[100,97153,3925],{"class":205},[14,97155,25013],{},[40,97157,97158,97163,97168],{},[43,97159,97160,97161],{},"The function defines one parameter: ",[17,97162,2853],{},[43,97164,97165,97166],{},"The call uses ",[17,97167,11330],{},[43,97169,97170,97172,97173],{},[17,97171,11330],{}," does not match ",[17,97174,2853],{},[14,97176,6031],{},[91,97178,97180],{"className":93,"code":97179,"language":95,"meta":96,"style":96},"def greet(name):\n    print(\"Hello\", name)\n\ngreet(name=\"Sam\")\n",[17,97181,97182,97194,97212,97216],{"__ignoreMap":96},[100,97183,97184,97186,97188,97190,97192],{"class":102,"line":103},[100,97185,1078],{"class":1077},[100,97187,2848],{"class":1081},[100,97189,170],{"class":118},[100,97191,2853],{"class":2519},[100,97193,522],{"class":118},[100,97195,97196,97198,97200,97202,97204,97206,97208,97210],{"class":102,"line":135},[100,97197,200],{"class":114},[100,97199,170],{"class":118},[100,97201,206],{"class":205},[100,97203,7683],{"class":209},[100,97205,206],{"class":205},[100,97207,126],{"class":118},[100,97209,1703],{"class":178},[100,97211,215],{"class":118},[100,97213,97214],{"class":102,"line":142},[100,97215,139],{"emptyLinePlaceholder":138},[100,97217,97218,97220,97222,97224,97226,97228,97230,97232],{"class":102,"line":152},[100,97219,2911],{"class":178},[100,97221,170],{"class":118},[100,97223,2853],{"class":18141},[100,97225,111],{"class":110},[100,97227,206],{"class":205},[100,97229,29329],{"class":209},[100,97231,206],{"class":205},[100,97233,215],{"class":118},[14,97235,218],{},[91,97237,97238],{"className":93,"code":88980,"language":95,"meta":96,"style":96},[17,97239,97240],{"__ignoreMap":96},[100,97241,97242],{"class":102,"line":103},[100,97243,88980],{"class":106},[77,97245,25091],{"id":25090},[14,97247,38066],{},[40,97249,97250,97253,97256,97259,97262],{},[43,97251,97252],{},"You used the wrong parameter name",[43,97254,97255],{},"You guessed the keyword instead of checking the function definition",[43,97257,97258],{},"The function only accepts positional arguments",[43,97260,97261],{},"You passed a keyword to a built-in function or method that does not support it",[43,97263,97264],{},"You are calling a class or library function with an old or incorrect argument name",[77,97266,6244],{"id":6243},[14,97268,97269],{},"Start with the simplest checks:",[40,97271,97272,97275,97278,97281,97284],{},[43,97273,97274],{},"Look at the function definition",[43,97276,97277],{},"Compare the accepted parameter names with your function call",[43,97279,97280],{},"Fix spelling and capitalization",[43,97282,97283],{},"Remove the keyword if positional arguments are allowed",[43,97285,97286],{},"Update your own function definition if you want that keyword to be accepted",[14,97288,97289,97290,290],{},"If you want a deeper explanation of named arguments, see ",[295,97291,97292],{"href":88530},"default and keyword arguments explained",[77,97294,97296],{"id":97295},"fix-1-use-the-correct-parameter-name","Fix 1: Use the correct parameter name",[14,97298,82571],{},[14,97300,97301],{},"Compare the function definition and the call side by side:",[91,97303,97305],{"className":93,"code":97304,"language":95,"meta":96,"style":96},"def greet(name):\n    print(\"Hello\", name)\n\ngreet(username=\"Sam\")   # wrong\n",[17,97306,97307,97319,97337,97341],{"__ignoreMap":96},[100,97308,97309,97311,97313,97315,97317],{"class":102,"line":103},[100,97310,1078],{"class":1077},[100,97312,2848],{"class":1081},[100,97314,170],{"class":118},[100,97316,2853],{"class":2519},[100,97318,522],{"class":118},[100,97320,97321,97323,97325,97327,97329,97331,97333,97335],{"class":102,"line":135},[100,97322,200],{"class":114},[100,97324,170],{"class":118},[100,97326,206],{"class":205},[100,97328,7683],{"class":209},[100,97330,206],{"class":205},[100,97332,126],{"class":118},[100,97334,1703],{"class":178},[100,97336,215],{"class":118},[100,97338,97339],{"class":102,"line":142},[100,97340,139],{"emptyLinePlaceholder":138},[100,97342,97343,97345,97347,97349,97351,97353,97355,97357,97359],{"class":102,"line":152},[100,97344,2911],{"class":178},[100,97346,170],{"class":118},[100,97348,11330],{"class":18141},[100,97350,111],{"class":110},[100,97352,206],{"class":205},[100,97354,29329],{"class":209},[100,97356,206],{"class":205},[100,97358,6155],{"class":118},[100,97360,97361],{"class":414},"   # wrong\n",[14,97363,97364,97365,9472,97367,290],{},"The function accepts ",[17,97366,2853],{},[17,97368,11330],{},[14,97370,97371],{},"Corrected version:",[91,97373,97375],{"className":93,"code":97374,"language":95,"meta":96,"style":96},"def greet(name):\n    print(\"Hello\", name)\n\ngreet(name=\"Sam\")   # correct\n",[17,97376,97377,97389,97407,97411],{"__ignoreMap":96},[100,97378,97379,97381,97383,97385,97387],{"class":102,"line":103},[100,97380,1078],{"class":1077},[100,97382,2848],{"class":1081},[100,97384,170],{"class":118},[100,97386,2853],{"class":2519},[100,97388,522],{"class":118},[100,97390,97391,97393,97395,97397,97399,97401,97403,97405],{"class":102,"line":135},[100,97392,200],{"class":114},[100,97394,170],{"class":118},[100,97396,206],{"class":205},[100,97398,7683],{"class":209},[100,97400,206],{"class":205},[100,97402,126],{"class":118},[100,97404,1703],{"class":178},[100,97406,215],{"class":118},[100,97408,97409],{"class":102,"line":142},[100,97410,139],{"emptyLinePlaceholder":138},[100,97412,97413,97415,97417,97419,97421,97423,97425,97427,97429],{"class":102,"line":152},[100,97414,2911],{"class":178},[100,97416,170],{"class":118},[100,97418,2853],{"class":18141},[100,97420,111],{"class":110},[100,97422,206],{"class":205},[100,97424,29329],{"class":209},[100,97426,206],{"class":205},[100,97428,6155],{"class":118},[100,97430,97431],{"class":414},"   # correct\n",[14,97433,97434],{},"Important details:",[40,97436,97437,97440,97443],{},[43,97438,97439],{},"Spelling must match exactly",[43,97441,97442],{},"Parameter names are case-sensitive",[43,97444,97445,3266,97447,97449],{},[17,97446,42750],{},[17,97448,2853],{}," are different",[14,97451,97452],{},"Example with capitalization:",[91,97454,97456],{"className":93,"code":97455,"language":95,"meta":96,"style":96},"def show_age(age):\n    print(age)\n\n# show_age(Age=20)   # wrong\nshow_age(age=20)     # correct\n",[17,97457,97458,97471,97481,97485,97490],{"__ignoreMap":96},[100,97459,97460,97462,97465,97467,97469],{"class":102,"line":103},[100,97461,1078],{"class":1077},[100,97463,97464],{"class":1081}," show_age",[100,97466,170],{"class":118},[100,97468,2770],{"class":2519},[100,97470,522],{"class":118},[100,97472,97473,97475,97477,97479],{"class":102,"line":135},[100,97474,200],{"class":114},[100,97476,170],{"class":118},[100,97478,2770],{"class":178},[100,97480,215],{"class":118},[100,97482,97483],{"class":102,"line":142},[100,97484,139],{"emptyLinePlaceholder":138},[100,97486,97487],{"class":102,"line":152},[100,97488,97489],{"class":414},"# show_age(Age=20)   # wrong\n",[100,97491,97492,97495,97497,97499,97501,97503,97505],{"class":102,"line":164},[100,97493,97494],{"class":178},"show_age",[100,97496,170],{"class":118},[100,97498,2770],{"class":18141},[100,97500,111],{"class":110},[100,97502,6152],{"class":122},[100,97504,6155],{"class":118},[100,97506,97507],{"class":414},"     # correct\n",[77,97509,97511],{"id":97510},"fix-2-pass-the-value-without-a-keyword","Fix 2: Pass the value without a keyword",[14,97513,97514],{},"Some functions accept positional arguments, which means you pass values by order instead of by name.",[14,97516,1844],{},[91,97518,97520],{"className":93,"code":97519,"language":95,"meta":96,"style":96},"def greet(name):\n    print(\"Hello\", name)\n\ngreet(\"Sam\")\n",[17,97521,97522,97534,97552,97556],{"__ignoreMap":96},[100,97523,97524,97526,97528,97530,97532],{"class":102,"line":103},[100,97525,1078],{"class":1077},[100,97527,2848],{"class":1081},[100,97529,170],{"class":118},[100,97531,2853],{"class":2519},[100,97533,522],{"class":118},[100,97535,97536,97538,97540,97542,97544,97546,97548,97550],{"class":102,"line":135},[100,97537,200],{"class":114},[100,97539,170],{"class":118},[100,97541,206],{"class":205},[100,97543,7683],{"class":209},[100,97545,206],{"class":205},[100,97547,126],{"class":118},[100,97549,1703],{"class":178},[100,97551,215],{"class":118},[100,97553,97554],{"class":102,"line":142},[100,97555,139],{"emptyLinePlaceholder":138},[100,97557,97558,97560,97562,97564,97566,97568],{"class":102,"line":152},[100,97559,2911],{"class":178},[100,97561,170],{"class":118},[100,97563,206],{"class":205},[100,97565,29329],{"class":209},[100,97567,206],{"class":205},[100,97569,215],{"class":118},[14,97571,218],{},[91,97573,97574],{"className":93,"code":88980,"language":95,"meta":96,"style":96},[17,97575,97576],{"__ignoreMap":96},[100,97577,97578],{"class":102,"line":103},[100,97579,88980],{"class":106},[14,97581,49650],{},[40,97583,97584,97587,97590],{},[43,97585,97586],{},"The function accepts positional arguments",[43,97588,97589],{},"You are not sure whether the keyword form is supported",[43,97591,97592],{},"You keep the argument order correct",[14,97594,97595,97596,97598],{},"For example, with ",[17,97597,13235],{}," you usually do this:",[91,97600,97601],{"className":93,"code":54665,"language":95,"meta":96,"style":96},[17,97602,97603],{"__ignoreMap":96},[100,97604,97605,97607,97609,97611,97613,97615],{"class":102,"line":103},[100,97606,372],{"class":114},[100,97608,170],{"class":118},[100,97610,206],{"class":205},[100,97612,3506],{"class":209},[100,97614,206],{"class":205},[100,97616,215],{"class":118},[14,97618,97619],{},"Not this:",[91,97621,97623],{"className":93,"code":97622,"language":95,"meta":96,"style":96},"# print(msg=\"hello\")  # TypeError in Python\n",[17,97624,97625],{"__ignoreMap":96},[100,97626,97627],{"class":102,"line":103},[100,97628,97622],{"class":414},[14,97630,97631],{},"Be careful: positional arguments must be in the correct order.",[77,97633,97635],{"id":97634},"fix-3-change-the-function-definition","Fix 3: Change the function definition",[14,97637,97638],{},"Only do this if the function is your own code.",[14,97640,97641,97642,97644],{},"If you wrote the function and want it to accept ",[17,97643,11330],{},", you can rename the parameter:",[91,97646,97648],{"className":93,"code":97647,"language":95,"meta":96,"style":96},"def greet(username):\n    print(\"Hello\", username)\n\ngreet(username=\"Sam\")\n",[17,97649,97650,97662,97680,97684],{"__ignoreMap":96},[100,97651,97652,97654,97656,97658,97660],{"class":102,"line":103},[100,97653,1078],{"class":1077},[100,97655,2848],{"class":1081},[100,97657,170],{"class":118},[100,97659,11330],{"class":2519},[100,97661,522],{"class":118},[100,97663,97664,97666,97668,97670,97672,97674,97676,97678],{"class":102,"line":135},[100,97665,200],{"class":114},[100,97667,170],{"class":118},[100,97669,206],{"class":205},[100,97671,7683],{"class":209},[100,97673,206],{"class":205},[100,97675,126],{"class":118},[100,97677,2720],{"class":178},[100,97679,215],{"class":118},[100,97681,97682],{"class":102,"line":142},[100,97683,139],{"emptyLinePlaceholder":138},[100,97685,97686,97688,97690,97692,97694,97696,97698,97700],{"class":102,"line":152},[100,97687,2911],{"class":178},[100,97689,170],{"class":118},[100,97691,11330],{"class":18141},[100,97693,111],{"class":110},[100,97695,206],{"class":205},[100,97697,29329],{"class":209},[100,97699,206],{"class":205},[100,97701,215],{"class":118},[14,97703,97704],{},"Or add another parameter if the function should support more values:",[91,97706,97708],{"className":93,"code":97707,"language":95,"meta":96,"style":96},"def greet(name, title=None):\n    if title is None:\n        print(\"Hello\", name)\n    else:\n        print(\"Hello\", title, name)\n\ngreet(name=\"Sam\")\ngreet(name=\"Sam\", title=\"Mr.\")\n",[17,97709,97710,97731,97744,97762,97768,97790,97794,97812],{"__ignoreMap":96},[100,97711,97712,97714,97716,97718,97720,97722,97725,97727,97729],{"class":102,"line":103},[100,97713,1078],{"class":1077},[100,97715,2848],{"class":1081},[100,97717,170],{"class":118},[100,97719,2853],{"class":2519},[100,97721,126],{"class":118},[100,97723,97724],{"class":2519}," title",[100,97726,111],{"class":110},[100,97728,930],{"class":158},[100,97730,522],{"class":118},[100,97732,97733,97735,97738,97740,97742],{"class":102,"line":135},[100,97734,2777],{"class":145},[100,97736,97737],{"class":106}," title ",[100,97739,4918],{"class":110},[100,97741,866],{"class":158},[100,97743,149],{"class":118},[100,97745,97746,97748,97750,97752,97754,97756,97758,97760],{"class":102,"line":142},[100,97747,167],{"class":114},[100,97749,170],{"class":118},[100,97751,206],{"class":205},[100,97753,7683],{"class":209},[100,97755,206],{"class":205},[100,97757,126],{"class":118},[100,97759,1703],{"class":178},[100,97761,215],{"class":118},[100,97763,97764,97766],{"class":102,"line":152},[100,97765,40531],{"class":145},[100,97767,149],{"class":118},[100,97769,97770,97772,97774,97776,97778,97780,97782,97784,97786,97788],{"class":102,"line":164},[100,97771,167],{"class":114},[100,97773,170],{"class":118},[100,97775,206],{"class":205},[100,97777,7683],{"class":209},[100,97779,206],{"class":205},[100,97781,126],{"class":118},[100,97783,97724],{"class":178},[100,97785,126],{"class":118},[100,97787,1703],{"class":178},[100,97789,215],{"class":118},[100,97791,97792],{"class":102,"line":185},[100,97793,139],{"emptyLinePlaceholder":138},[100,97795,97796,97798,97800,97802,97804,97806,97808,97810],{"class":102,"line":197},[100,97797,2911],{"class":178},[100,97799,170],{"class":118},[100,97801,2853],{"class":18141},[100,97803,111],{"class":110},[100,97805,206],{"class":205},[100,97807,29329],{"class":209},[100,97809,206],{"class":205},[100,97811,215],{"class":118},[100,97813,97814,97816,97818,97820,97822,97824,97826,97828,97830,97832,97834,97836,97839,97841],{"class":102,"line":771},[100,97815,2911],{"class":178},[100,97817,170],{"class":118},[100,97819,2853],{"class":18141},[100,97821,111],{"class":110},[100,97823,206],{"class":205},[100,97825,29329],{"class":209},[100,97827,206],{"class":205},[100,97829,126],{"class":118},[100,97831,97724],{"class":18141},[100,97833,111],{"class":110},[100,97835,206],{"class":205},[100,97837,97838],{"class":209},"Mr.",[100,97840,206],{"class":205},[100,97842,215],{"class":118},[14,97844,218],{},[91,97846,97848],{"className":93,"code":97847,"language":95,"meta":96,"style":96},"Hello Sam\nHello Mr. Sam\n",[17,97849,97850,97854],{"__ignoreMap":96},[100,97851,97852],{"class":102,"line":103},[100,97853,88980],{"class":106},[100,97855,97856,97859,97861],{"class":102,"line":135},[100,97857,97858],{"class":106},"Hello Mr",[100,97860,290],{"class":118},[100,97862,97863],{"class":2494}," Sam\n",[14,97865,97866],{},"Do not change parameter names carelessly if other parts of your program already depend on them.",[14,97868,97869,97870,290],{},"If you are still learning to write functions, see ",[295,97871,88987],{"href":88986},[77,97873,97875,97876],{"id":97874},"fix-4-accept-extra-keyword-arguments-with-kwargs","Fix 4: Accept extra keyword arguments with ",[17,97877,97878],{},"**kwargs",[14,97880,241,97881,97883],{},[17,97882,97878],{}," when your own function should accept extra named arguments.",[14,97885,97886],{},"Beginner-friendly explanation:",[40,97888,97889,97894,97897],{},[43,97890,97891,97893],{},[17,97892,97878],{}," collects extra keyword arguments into a dictionary",[43,97895,97896],{},"That means your function will not crash just because it receives an extra named value",[43,97898,97899],{},"But this can also hide mistakes if you use it too freely",[14,97901,1844],{},[91,97903,97905],{"className":93,"code":97904,"language":95,"meta":96,"style":96},"def greet(name, **kwargs):\n    print(\"Hello\", name)\n    print(\"Extra values:\", kwargs)\n\ngreet(name=\"Sam\", age=20, city=\"Boston\")\n",[17,97906,97907,97926,97944,97964,97968],{"__ignoreMap":96},[100,97908,97909,97911,97913,97915,97917,97919,97921,97924],{"class":102,"line":103},[100,97910,1078],{"class":1077},[100,97912,2848],{"class":1081},[100,97914,170],{"class":118},[100,97916,2853],{"class":2519},[100,97918,126],{"class":118},[100,97920,60446],{"class":110},[100,97922,97923],{"class":2519},"kwargs",[100,97925,522],{"class":118},[100,97927,97928,97930,97932,97934,97936,97938,97940,97942],{"class":102,"line":135},[100,97929,200],{"class":114},[100,97931,170],{"class":118},[100,97933,206],{"class":205},[100,97935,7683],{"class":209},[100,97937,206],{"class":205},[100,97939,126],{"class":118},[100,97941,1703],{"class":178},[100,97943,215],{"class":118},[100,97945,97946,97948,97950,97952,97955,97957,97959,97962],{"class":102,"line":142},[100,97947,200],{"class":114},[100,97949,170],{"class":118},[100,97951,206],{"class":205},[100,97953,97954],{"class":209},"Extra values:",[100,97956,206],{"class":205},[100,97958,126],{"class":118},[100,97960,97961],{"class":178}," kwargs",[100,97963,215],{"class":118},[100,97965,97966],{"class":102,"line":152},[100,97967,139],{"emptyLinePlaceholder":138},[100,97969,97970,97972,97974,97976,97978,97980,97982,97984,97986,97988,97990,97992,97994,97997,97999,98001,98003,98005],{"class":102,"line":164},[100,97971,2911],{"class":178},[100,97973,170],{"class":118},[100,97975,2853],{"class":18141},[100,97977,111],{"class":110},[100,97979,206],{"class":205},[100,97981,29329],{"class":209},[100,97983,206],{"class":205},[100,97985,126],{"class":118},[100,97987,31058],{"class":18141},[100,97989,111],{"class":110},[100,97991,6152],{"class":122},[100,97993,126],{"class":118},[100,97995,97996],{"class":18141}," city",[100,97998,111],{"class":110},[100,98000,206],{"class":205},[100,98002,44177],{"class":209},[100,98004,206],{"class":205},[100,98006,215],{"class":118},[14,98008,218],{},[91,98010,98012],{"className":93,"code":98011,"language":95,"meta":96,"style":96},"Hello Sam\nExtra values: {'age': 20, 'city': 'Boston'}\n",[17,98013,98014,98018],{"__ignoreMap":96},[100,98015,98016],{"class":102,"line":103},[100,98017,88980],{"class":106},[100,98019,98020,98023,98025,98027,98029,98031,98033,98035,98037,98039,98041,98043,98045,98047,98049,98051,98053],{"class":102,"line":135},[100,98021,98022],{"class":106},"Extra values",[100,98024,89],{"class":118},[100,98026,12080],{"class":118},[100,98028,1280],{"class":205},[100,98030,2770],{"class":209},[100,98032,1280],{"class":205},[100,98034,89],{"class":118},[100,98036,361],{"class":122},[100,98038,126],{"class":118},[100,98040,1274],{"class":205},[100,98042,43636],{"class":209},[100,98044,1280],{"class":205},[100,98046,89],{"class":118},[100,98048,1274],{"class":205},[100,98050,44177],{"class":209},[100,98052,1280],{"class":205},[100,98054,12093],{"class":118},[14,98056,98057],{},"You can also check for expected keys:",[91,98059,98061],{"className":93,"code":98060,"language":95,"meta":96,"style":96},"def greet(name, **kwargs):\n    print(\"Hello\", name)\n\n    if \"title\" in kwargs:\n        print(\"Title:\", kwargs[\"title\"])\n\ngreet(name=\"Sam\", title=\"Dr.\")\n",[17,98062,98063,98081,98099,98103,98119,98146,98150],{"__ignoreMap":96},[100,98064,98065,98067,98069,98071,98073,98075,98077,98079],{"class":102,"line":103},[100,98066,1078],{"class":1077},[100,98068,2848],{"class":1081},[100,98070,170],{"class":118},[100,98072,2853],{"class":2519},[100,98074,126],{"class":118},[100,98076,60446],{"class":110},[100,98078,97923],{"class":2519},[100,98080,522],{"class":118},[100,98082,98083,98085,98087,98089,98091,98093,98095,98097],{"class":102,"line":135},[100,98084,200],{"class":114},[100,98086,170],{"class":118},[100,98088,206],{"class":205},[100,98090,7683],{"class":209},[100,98092,206],{"class":205},[100,98094,126],{"class":118},[100,98096,1703],{"class":178},[100,98098,215],{"class":118},[100,98100,98101],{"class":102,"line":142},[100,98102,139],{"emptyLinePlaceholder":138},[100,98104,98105,98107,98109,98111,98113,98115,98117],{"class":102,"line":152},[100,98106,2777],{"class":145},[100,98108,1708],{"class":205},[100,98110,11392],{"class":209},[100,98112,206],{"class":205},[100,98114,1971],{"class":110},[100,98116,97961],{"class":106},[100,98118,149],{"class":118},[100,98120,98121,98123,98125,98127,98130,98132,98134,98136,98138,98140,98142,98144],{"class":102,"line":164},[100,98122,167],{"class":114},[100,98124,170],{"class":118},[100,98126,206],{"class":205},[100,98128,98129],{"class":209},"Title:",[100,98131,206],{"class":205},[100,98133,126],{"class":118},[100,98135,97961],{"class":178},[100,98137,3166],{"class":118},[100,98139,206],{"class":205},[100,98141,11392],{"class":209},[100,98143,206],{"class":205},[100,98145,132],{"class":118},[100,98147,98148],{"class":102,"line":185},[100,98149,139],{"emptyLinePlaceholder":138},[100,98151,98152,98154,98156,98158,98160,98162,98164,98166,98168,98170,98172,98174,98177,98179],{"class":102,"line":197},[100,98153,2911],{"class":178},[100,98155,170],{"class":118},[100,98157,2853],{"class":18141},[100,98159,111],{"class":110},[100,98161,206],{"class":205},[100,98163,29329],{"class":209},[100,98165,206],{"class":205},[100,98167,126],{"class":118},[100,98169,97724],{"class":18141},[100,98171,111],{"class":110},[100,98173,206],{"class":205},[100,98175,98176],{"class":209},"Dr.",[100,98178,206],{"class":205},[100,98180,215],{"class":118},[14,98182,218],{},[91,98184,98186],{"className":93,"code":98185,"language":95,"meta":96,"style":96},"Hello Sam\nTitle: Dr.\n",[17,98187,98188,98192],{"__ignoreMap":96},[100,98189,98190],{"class":102,"line":103},[100,98191,88980],{"class":106},[100,98193,98194,98197,98199,98202],{"class":102,"line":135},[100,98195,98196],{"class":106},"Title",[100,98198,89],{"class":118},[100,98200,98201],{"class":106}," Dr",[100,98203,75606],{"class":118},[14,98205,241,98206,98208],{},[17,98207,97878],{}," only when flexibility is helpful. If you want strict parameter checking, regular parameters are usually better.",[77,98210,98212],{"id":98211},"methods-and-class-constructors","Methods and class constructors",[14,98214,98215],{},"The same error can happen with methods and class constructors.",[14,98217,98218],{},"Example with a class:",[91,98220,98222],{"className":93,"code":98221,"language":95,"meta":96,"style":96},"class Person:\n    def __init__(self, name):\n        self.name = name\n\n# person = Person(name=\"Sam\", age=20)  # TypeError\nperson = Person(name=\"Sam\")\nprint(person.name)\n",[17,98223,98224,98232,98249,98262,98266,98271,98293],{"__ignoreMap":96},[100,98225,98226,98228,98230],{"class":102,"line":103},[100,98227,3417],{"class":1077},[100,98229,71096],{"class":10361},[100,98231,149],{"class":118},[100,98233,98234,98236,98239,98241,98243,98245,98247],{"class":102,"line":135},[100,98235,89014],{"class":1077},[100,98237,98238],{"class":114}," __init__",[100,98240,170],{"class":118},[100,98242,88513],{"class":89021},[100,98244,126],{"class":118},[100,98246,1703],{"class":2519},[100,98248,522],{"class":118},[100,98250,98251,98254,98256,98258,98260],{"class":102,"line":142},[100,98252,98253],{"class":527},"        self",[100,98255,290],{"class":118},[100,98257,2853],{"class":2494},[100,98259,47743],{"class":110},[100,98261,2898],{"class":106},[100,98263,98264],{"class":102,"line":152},[100,98265,139],{"emptyLinePlaceholder":138},[100,98267,98268],{"class":102,"line":164},[100,98269,98270],{"class":414},"# person = Person(name=\"Sam\", age=20)  # TypeError\n",[100,98272,98273,98275,98277,98279,98281,98283,98285,98287,98289,98291],{"class":102,"line":185},[100,98274,43766],{"class":106},[100,98276,111],{"class":110},[100,98278,71096],{"class":178},[100,98280,170],{"class":118},[100,98282,2853],{"class":18141},[100,98284,111],{"class":110},[100,98286,206],{"class":205},[100,98288,29329],{"class":209},[100,98290,206],{"class":205},[100,98292,215],{"class":118},[100,98294,98295,98297,98299,98301,98303,98305],{"class":102,"line":197},[100,98296,372],{"class":114},[100,98298,170],{"class":118},[100,98300,43801],{"class":178},[100,98302,290],{"class":118},[100,98304,2853],{"class":2494},[100,98306,215],{"class":118},[14,98308,218],{},[91,98310,98311],{"className":93,"code":16526,"language":95,"meta":96,"style":96},[17,98312,98313],{"__ignoreMap":96},[100,98314,98315],{"class":102,"line":103},[100,98316,16526],{"class":106},[14,98318,98319],{},"Why the error happens:",[40,98321,98322,98330,98335],{},[43,98323,98324,98327,98328],{},[17,98325,98326],{},"__init__"," accepts only ",[17,98329,2853],{},[43,98331,98332,98333],{},"The call also passes ",[17,98334,2770],{},[43,98336,98337,98338],{},"Python does not know what to do with ",[17,98339,2770],{},[14,98341,98342],{},"This is common when using library classes, because their constructor argument names are often strict.",[77,98344,98346],{"id":98345},"how-to-debug-this-error-step-by-step","How to debug this error step by step",[14,98348,6927],{},[3282,98350,98351,98354,98357,98360,98363,98366],{},[43,98352,98353],{},"Read the function call shown in the traceback",[43,98355,98356],{},"Find the keyword named in the error message",[43,98358,98359],{},"Find the function, method, or class definition",[43,98361,98362],{},"Compare the accepted parameters with the keyword you passed",[43,98364,98365],{},"Check for spelling mistakes and wrong capitalization",[43,98367,98368],{},"Look at the documentation if needed",[14,98370,98371],{},"Useful tools:",[91,98373,98375],{"className":93,"code":98374,"language":95,"meta":96,"style":96},"help(function_name)\nprint(function_name.__doc__)\nprint(type(obj))\ndir(obj)\n",[17,98376,98377,98387,98401,98416],{"__ignoreMap":96},[100,98378,98379,98381,98383,98385],{"class":102,"line":103},[100,98380,2478],{"class":114},[100,98382,170],{"class":118},[100,98384,17820],{"class":178},[100,98386,215],{"class":118},[100,98388,98389,98391,98393,98395,98397,98399],{"class":102,"line":135},[100,98390,372],{"class":114},[100,98392,170],{"class":118},[100,98394,17820],{"class":178},[100,98396,290],{"class":118},[100,98398,88100],{"class":527},[100,98400,215],{"class":118},[100,98402,98403,98405,98407,98409,98411,98414],{"class":102,"line":142},[100,98404,372],{"class":114},[100,98406,170],{"class":118},[100,98408,1250],{"class":191},[100,98410,170],{"class":118},[100,98412,98413],{"class":178},"obj",[100,98415,182],{"class":118},[100,98417,98418,98420,98422,98424],{"class":102,"line":152},[100,98419,3822],{"class":114},[100,98421,170],{"class":118},[100,98423,98413],{"class":178},[100,98425,215],{"class":118},[14,98427,1844],{},[91,98429,98431],{"className":93,"code":98430,"language":95,"meta":96,"style":96},"def greet(name):\n    \"\"\"Print a greeting.\"\"\"\n    print(\"Hello\", name)\n\nhelp(greet)\nprint(greet.__doc__)\n",[17,98432,98433,98445,98455,98473,98477,98487],{"__ignoreMap":96},[100,98434,98435,98437,98439,98441,98443],{"class":102,"line":103},[100,98436,1078],{"class":1077},[100,98438,2848],{"class":1081},[100,98440,170],{"class":118},[100,98442,2853],{"class":2519},[100,98444,522],{"class":118},[100,98446,98447,98450,98453],{"class":102,"line":135},[100,98448,98449],{"class":3553},"    \"\"\"",[100,98451,98452],{"class":3556},"Print a greeting.",[100,98454,69590],{"class":3553},[100,98456,98457,98459,98461,98463,98465,98467,98469,98471],{"class":102,"line":142},[100,98458,200],{"class":114},[100,98460,170],{"class":118},[100,98462,206],{"class":205},[100,98464,7683],{"class":209},[100,98466,206],{"class":205},[100,98468,126],{"class":118},[100,98470,1703],{"class":178},[100,98472,215],{"class":118},[100,98474,98475],{"class":102,"line":152},[100,98476,139],{"emptyLinePlaceholder":138},[100,98478,98479,98481,98483,98485],{"class":102,"line":164},[100,98480,2478],{"class":114},[100,98482,170],{"class":118},[100,98484,2911],{"class":178},[100,98486,215],{"class":118},[100,98488,98489,98491,98493,98495,98497,98499],{"class":102,"line":185},[100,98490,372],{"class":114},[100,98492,170],{"class":118},[100,98494,2911],{"class":178},[100,98496,290],{"class":118},[100,98498,88100],{"class":527},[100,98500,215],{"class":118},[14,98502,98503,98504,12574,98506,290],{},"If you want to learn more about ",[17,98505,9796],{},[295,98507,98508],{"href":26533},"Python help() function explained",[77,98510,14113],{"id":14112},[14,98512,98513],{},"These are very common situations where beginners see this error:",[40,98515,98516,98524,98527,98530],{},[43,98517,1357,98518,42154,98521,98523],{},[17,98519,98520],{},"msg=",[17,98522,13235],{}," instead of just passing a normal value",[43,98525,98526],{},"Using the wrong keyword with list, string, or dictionary methods",[43,98528,98529],{},"Passing a keyword to your own function after renaming its parameters",[43,98531,98532],{},"Copying example code from a different version of a library",[14,98534,98535,98536,290],{},"Another common related error is giving too few arguments. If that happens, see ",[295,98537,88410],{"href":98538},"\u002Ferrors\u002Ftypeerror-missing-required-positional-argument-fix\u002F",[77,98540,1344],{"id":1343},[14,98542,98543],{},"Here are the most common causes in one list:",[40,98545,98546,98549,98552,98555,98558,98561],{},[43,98547,98548],{},"Wrong parameter name in the function call",[43,98550,98551],{},"Misspelled keyword argument",[43,98553,98554],{},"Wrong capitalization in the argument name",[43,98556,98557],{},"Passing a keyword to a function that only accepts positional arguments",[43,98559,98560],{},"Using old documentation or code from another version of a library",[43,98562,98563],{},"Calling a class constructor with unsupported named arguments",[77,98565,1514],{"id":1513},[675,98567,98569],{"id":98568},"what-is-a-keyword-argument-in-python","What is a keyword argument in Python?",[14,98571,98572,98573,98576],{},"It is an argument passed by name, like ",[17,98574,98575],{},"age=20",", instead of only by position.",[675,98578,98580],{"id":98579},"can-i-fix-this-by-changing-the-function-call-only","Can I fix this by changing the function call only?",[14,98582,98583],{},"Usually yes. The most common fix is to use the correct parameter name or pass the value positionally.",[675,98585,98587,98588,11353],{"id":98586},"when-should-i-use-kwargs","When should I use ",[17,98589,97878],{},[14,98591,98592],{},"Use it when your own function should accept flexible extra named arguments. Avoid it if you want strict parameter checking.",[675,98594,98596],{"id":98595},"why-does-this-happen-with-library-code","Why does this happen with library code?",[14,98598,98599],{},"Libraries often require exact argument names. A typo or version difference can cause this error.",[77,98601,1554],{"id":1553},[40,98603,98604,98608,98612,98616,98620],{},[43,98605,98606],{},[295,98607,89610],{"href":88526},[43,98609,98610],{},[295,98611,89624],{"href":88986},[43,98613,98614],{},[295,98615,88410],{"href":98538},[43,98617,98618],{},[295,98619,89615],{"href":88530},[43,98621,98622],{},[295,98623,98508],{"href":26533},[1589,98625,98626],{},"html pre.shiki code .sbsja, html code.shiki .sbsja{--shiki-light:#9C3EDA;--shiki-default:#D73A49;--shiki-dark:#F97583}html pre.shiki code .sGLFI, html code.shiki .sGLFI{--shiki-light:#6182B8;--shiki-default:#6F42C1;--shiki-dark:#B392F0}html pre.shiki code .sP7_E, html code.shiki .sP7_E{--shiki-light:#39ADB5;--shiki-default:#24292E;--shiki-dark:#E1E4E8}html pre.shiki code .sFwrP, html code.shiki .sFwrP{--shiki-light:#90A4AE;--shiki-light-font-style:italic;--shiki-default:#24292E;--shiki-default-font-style:inherit;--shiki-dark:#E1E4E8;--shiki-dark-font-style:inherit}html pre.shiki code .sptTA, html code.shiki .sptTA{--shiki-light:#6182B8;--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 .slqww, html code.shiki .slqww{--shiki-light:#6182B8;--shiki-default:#24292E;--shiki-dark:#E1E4E8}html pre.shiki code .s99_P, html code.shiki .s99_P{--shiki-light:#90A4AE;--shiki-light-font-style:italic;--shiki-default:#E36209;--shiki-default-font-style:inherit;--shiki-dark:#FFAB70;--shiki-dark-font-style:inherit}html pre.shiki code .smGrS, html code.shiki .smGrS{--shiki-light:#39ADB5;--shiki-default:#D73A49;--shiki-dark:#F97583}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 .sZMiF, html code.shiki .sZMiF{--shiki-light:#E2931D;--shiki-default:#005CC5;--shiki-dark:#79B8FF}html pre.shiki code .su5hD, html code.shiki .su5hD{--shiki-light:#90A4AE;--shiki-default:#24292E;--shiki-dark:#E1E4E8}html pre.shiki code .srdBf, html code.shiki .srdBf{--shiki-light:#F76D47;--shiki-default:#005CC5;--shiki-dark:#79B8FF}html pre.shiki code .s39Yj, html code.shiki .s39Yj{--shiki-light:#39ADB5;--shiki-default:#005CC5;--shiki-dark:#79B8FF}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 .skxfh, html code.shiki .skxfh{--shiki-light:#E53935;--shiki-default:#24292E;--shiki-dark:#E1E4E8}html pre.shiki code .sbgvK, html code.shiki .sbgvK{--shiki-light:#E2931D;--shiki-default:#6F42C1;--shiki-dark:#B392F0}html pre.shiki code .smCYv, html code.shiki .smCYv{--shiki-light:#E53935;--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 .s_hVV, html code.shiki .s_hVV{--shiki-light:#90A4AE;--shiki-default:#005CC5;--shiki-dark:#79B8FF}html pre.shiki code .s2W-s, html code.shiki .s2W-s{--shiki-light:#39ADB5;--shiki-light-font-style:italic;--shiki-default:#032F62;--shiki-default-font-style:inherit;--shiki-dark:#9ECBFF;--shiki-dark-font-style:inherit}html pre.shiki code .sithA, html code.shiki .sithA{--shiki-light:#90A4AE;--shiki-light-font-style:italic;--shiki-default:#032F62;--shiki-default-font-style:inherit;--shiki-dark:#9ECBFF;--shiki-dark-font-style:inherit}",{"title":96,"searchDepth":135,"depth":135,"links":98628},[98629,98630,98631,98632,98633,98634,98635,98636,98637,98639,98640,98641,98642,98643,98650],{"id":79,"depth":135,"text":80},{"id":5880,"depth":135,"text":5881},{"id":91662,"depth":135,"text":91663},{"id":25090,"depth":135,"text":25091},{"id":6243,"depth":135,"text":6244},{"id":97295,"depth":135,"text":97296},{"id":97510,"depth":135,"text":97511},{"id":97634,"depth":135,"text":97635},{"id":97874,"depth":135,"text":98638},"Fix 4: Accept extra keyword arguments with **kwargs",{"id":98211,"depth":135,"text":98212},{"id":98345,"depth":135,"text":98346},{"id":14112,"depth":135,"text":14113},{"id":1343,"depth":135,"text":1344},{"id":1513,"depth":135,"text":1514,"children":98644},[98645,98646,98647,98649],{"id":98568,"depth":142,"text":98569},{"id":98579,"depth":142,"text":98580},{"id":98586,"depth":142,"text":98648},"When should I use **kwargs?",{"id":98595,"depth":142,"text":98596},{"id":1553,"depth":135,"text":1554},"Master typeerror unexpected keyword argument fix in our comprehensive Python beginner guide.",{},"\u002Ferrors\u002Ftypeerror-unexpected-keyword-argument-fix",{"title":96928,"description":98651},"errors\u002Ftypeerror-unexpected-keyword-argument-fix","FTkeVIw-2ZnBLlfZZSAd0wyl5OHyBG1-d__C6ovzlWA",{"id":98658,"title":98659,"body":98660,"description":100656,"extension":1623,"meta":100657,"navigation":138,"path":99064,"seo":100658,"stem":100659,"__hash__":100660},"content\u002Ferrors\u002Ftypeerror-vs-valueerror-in-python-explained.md","TypeError vs ValueError in Python Explained",{"type":7,"value":98661,"toc":100616},[98662,98665,98672,98681,98685,98758,98766,98778,98780,98783,98795,98798,98810,98813,98817,98822,98825,98844,98848,98892,98894,98937,98948,98952,99007,99009,99033,99036,99040,99043,99059,99066,99070,99075,99077,99095,99099,99130,99132,99165,99182,99186,99215,99217,99243,99246,99250,99252,99269,99274,99278,99281,99285,99308,99310,99350,99352,99369,99371,99422,99424,99439,99443,99466,99468,99501,99503,99519,99521,99544,99546,99578,99584,99591,99595,99598,99621,99624,99669,99671,99694,99700,99732,99734,99747,99750,99754,99759,99780,99784,99833,99835,99847,99851,99917,99919,99932,99936,99987,99989,99998,100005,100009,100014,100039,100043,100117,100119,100128,100132,100182,100184,100192,100196,100309,100311,100320,100329,100333,100341,100358,100360,100468,100471,100496,100505,100509,100512,100534,100539,100541,100548,100556,100560,100566,100570,100573,100577,100580,100584,100589,100591,100613],[10,98663,98659],{"id":98664},"typeerror-vs-valueerror-in-python-explained",[14,98666,98667,3266,98669,98671],{},[17,98668,3538],{},[17,98670,3483],{}," are two common Python errors that beginners often mix up.",[14,98673,98674,98675,98677,98678,98680],{},"This page explains when Python raises ",[17,98676,3538],{}," and when it raises ",[17,98679,3483],{},". It will help you tell them apart, read error messages more clearly, and choose the right fix.",[77,98682,98684],{"id":98683},"quick-answer","Quick answer",[91,98686,98688],{"className":93,"code":98687,"language":95,"meta":96,"style":96},"try:\n    number = int(user_input)\nexcept TypeError:\n    print(\"Wrong type was passed\")\nexcept ValueError:\n    print(\"Right type function, but bad value\")\n",[17,98689,98690,98696,98711,98720,98735,98743],{"__ignoreMap":96},[100,98691,98692,98694],{"class":102,"line":103},[100,98693,146],{"class":145},[100,98695,149],{"class":118},[100,98697,98698,98701,98703,98705,98707,98709],{"class":102,"line":135},[100,98699,98700],{"class":106},"    number ",[100,98702,111],{"class":110},[100,98704,3389],{"class":191},[100,98706,170],{"class":118},[100,98708,6230],{"class":178},[100,98710,215],{"class":118},[100,98712,98713,98715,98718],{"class":102,"line":142},[100,98714,188],{"class":145},[100,98716,98717],{"class":191}," TypeError",[100,98719,149],{"class":118},[100,98721,98722,98724,98726,98728,98731,98733],{"class":102,"line":152},[100,98723,200],{"class":114},[100,98725,170],{"class":118},[100,98727,206],{"class":205},[100,98729,98730],{"class":209},"Wrong type was passed",[100,98732,206],{"class":205},[100,98734,215],{"class":118},[100,98736,98737,98739,98741],{"class":102,"line":164},[100,98738,188],{"class":145},[100,98740,2794],{"class":191},[100,98742,149],{"class":118},[100,98744,98745,98747,98749,98751,98754,98756],{"class":102,"line":185},[100,98746,200],{"class":114},[100,98748,170],{"class":118},[100,98750,206],{"class":205},[100,98752,98753],{"class":209},"Right type function, but bad value",[100,98755,206],{"class":205},[100,98757,215],{"class":118},[14,98759,241,98760,18346,98762,98765],{},[17,98761,3538],{},[22,98763,98764],{},"kind of object"," is wrong.",[14,98767,241,98768,18346,98770,98773,98774,98777],{},[17,98769,3483],{},[22,98771,98772],{},"object type is acceptable",", but the ",[22,98775,98776],{},"actual value"," cannot be used.",[77,98779,17923],{"id":17922},[14,98781,98782],{},"The simplest way to remember it is:",[40,98784,98785,98790],{},[43,98786,98787,98789],{},[22,98788,3538],{}," = wrong kind of data",[43,98791,98792,98794],{},[22,98793,3483],{}," = right kind of data, wrong value",[14,98796,98797],{},"In other words:",[40,98799,98800,98805],{},[43,98801,3860,98802,98804],{},[17,98803,3538],{}," when an operation is used with an object of a type it does not support.",[43,98806,3860,98807,98809],{},[17,98808,3483],{}," when the type is allowed, but the value itself is not valid for that operation.",[14,98811,98812],{},"This page focuses on comparing these two errors so you can identify the right problem faster.",[77,98814,98816],{"id":98815},"when-python-raises-typeerror","When Python raises TypeError",[14,98818,3860,98819,98821],{},[17,98820,3538],{}," when your code uses an object in a way that its type does not support.",[14,98823,98824],{},"Common situations:",[40,98826,98827,98832,98835,98838],{},[43,98828,98829,98830],{},"Adding a string and an integer with ",[17,98831,6547],{},[43,98833,98834],{},"Passing a non-iterable where Python expects something iterable",[43,98836,98837],{},"Using a string as a list index",[43,98839,98840,98841,98843],{},"Passing ",[17,98842,930],{}," where a real object is expected",[675,98845,98847],{"id":98846},"example-adding-incompatible-types","Example: adding incompatible types",[91,98849,98851],{"className":93,"code":98850,"language":95,"meta":96,"style":96},"x = 5\ny = \"3\"\n\nprint(x + y)\n",[17,98852,98853,98861,98874,98878],{"__ignoreMap":96},[100,98854,98855,98857,98859],{"class":102,"line":103},[100,98856,54291],{"class":106},[100,98858,111],{"class":110},[100,98860,37580],{"class":122},[100,98862,98863,98866,98868,98870,98872],{"class":102,"line":135},[100,98864,98865],{"class":106},"y ",[100,98867,111],{"class":110},[100,98869,1708],{"class":205},[100,98871,11541],{"class":209},[100,98873,1714],{"class":205},[100,98875,98876],{"class":102,"line":142},[100,98877,139],{"emptyLinePlaceholder":138},[100,98879,98880,98882,98884,98886,98888,98890],{"class":102,"line":152},[100,98881,372],{"class":114},[100,98883,170],{"class":118},[100,98885,54291],{"class":178},[100,98887,6547],{"class":110},[100,98889,78293],{"class":178},[100,98891,215],{"class":118},[14,98893,218],{},[91,98895,98897],{"className":93,"code":98896,"language":95,"meta":96,"style":96},"TypeError: unsupported operand type(s) for +: 'int' and 'str'\n",[17,98898,98899],{"__ignoreMap":96},[100,98900,98901,98903,98905,98908,98910,98912,98915,98917,98919,98921,98923,98925,98927,98929,98931,98933,98935],{"class":102,"line":103},[100,98902,3538],{"class":191},[100,98904,89],{"class":118},[100,98906,98907],{"class":106}," unsupported operand ",[100,98909,1250],{"class":191},[100,98911,170],{"class":118},[100,98913,98914],{"class":178},"s",[100,98916,6155],{"class":118},[100,98918,7565],{"class":145},[100,98920,2895],{"class":110},[100,98922,89],{"class":118},[100,98924,1274],{"class":205},[100,98926,3499],{"class":209},[100,98928,1280],{"class":205},[100,98930,62025],{"class":110},[100,98932,1274],{"class":205},[100,98934,3422],{"class":209},[100,98936,3925],{"class":205},[14,98938,7194,98939,98941,98942,98945,98946,290],{},[17,98940,3557],{}," is an integer and ",[17,98943,98944],{},"\"3\""," is a string. Python does not automatically combine them with ",[17,98947,6547],{},[675,98949,98951],{"id":98950},"example-using-the-wrong-type-as-a-list-index","Example: using the wrong type as a list index",[91,98953,98955],{"className":93,"code":98954,"language":95,"meta":96,"style":96},"numbers = [10, 20, 30]\nindex = \"1\"\n\nprint(numbers[index])\n",[17,98956,98957,98977,98989,98993],{"__ignoreMap":96},[100,98958,98959,98961,98963,98965,98967,98969,98971,98973,98975],{"class":102,"line":103},[100,98960,107],{"class":106},[100,98962,111],{"class":110},[100,98964,594],{"class":118},[100,98966,356],{"class":122},[100,98968,126],{"class":118},[100,98970,361],{"class":122},[100,98972,126],{"class":118},[100,98974,16342],{"class":122},[100,98976,603],{"class":118},[100,98978,98979,98981,98983,98985,98987],{"class":102,"line":135},[100,98980,3116],{"class":106},[100,98982,111],{"class":110},[100,98984,1708],{"class":205},[100,98986,123],{"class":209},[100,98988,1714],{"class":205},[100,98990,98991],{"class":102,"line":142},[100,98992,139],{"emptyLinePlaceholder":138},[100,98994,98995,98997,98999,99001,99003,99005],{"class":102,"line":152},[100,98996,372],{"class":114},[100,98998,170],{"class":118},[100,99000,179],{"class":178},[100,99002,3166],{"class":118},[100,99004,3169],{"class":178},[100,99006,132],{"class":118},[14,99008,218],{},[91,99010,99011],{"className":93,"code":85479,"language":95,"meta":96,"style":96},[17,99012,99013],{"__ignoreMap":96},[100,99014,99015,99017,99019,99021,99023,99025,99027,99029,99031],{"class":102,"line":103},[100,99016,3538],{"class":191},[100,99018,89],{"class":118},[100,99020,14704],{"class":191},[100,99022,85492],{"class":106},[100,99024,11841],{"class":110},[100,99026,85497],{"class":106},[100,99028,126],{"class":118},[100,99030,4921],{"class":110},[100,99032,85504],{"class":191},[14,99034,99035],{},"A list index must be an integer, not a string.",[675,99037,99039],{"id":99038},"typical-fix-for-typeerror","Typical fix for TypeError",[14,99041,99042],{},"The fix is usually one of these:",[40,99044,99045,99048,99051,99056],{},[43,99046,99047],{},"Convert the value to the correct type",[43,99049,99050],{},"Pass a different object",[43,99052,99053,99054],{},"Check whether a variable is ",[17,99055,930],{},[43,99057,99058],{},"Make sure the function gets the type it expects",[14,99060,99061,99062,290],{},"If you need more detail, see ",[295,99063,99065],{"href":99064},"\u002Ferrors\u002Ftypeerror-vs-valueerror-in-python-explained","TypeError in Python: causes and fixes",[77,99067,99069],{"id":99068},"when-python-raises-valueerror","When Python raises ValueError",[14,99071,3860,99072,99074],{},[17,99073,3483],{}," when the type is acceptable, but the specific value is not.",[14,99076,98824],{},[40,99078,99079,99084,99089,99092],{},[43,99080,99081],{},[17,99082,99083],{},"int(\"abc\")",[43,99085,99086],{},[17,99087,99088],{},"float(\"hello\")",[43,99090,99091],{},"Unpacking the wrong number of values",[43,99093,99094],{},"Passing a value format that a function does not accept",[675,99096,99098],{"id":99097},"example-converting-invalid-text-to-int","Example: converting invalid text to int",[91,99100,99102],{"className":93,"code":99101,"language":95,"meta":96,"style":96},"text = \"abc\"\nnumber = int(text)\n",[17,99103,99104,99116],{"__ignoreMap":96},[100,99105,99106,99108,99110,99112,99114],{"class":102,"line":103},[100,99107,4331],{"class":106},[100,99109,111],{"class":110},[100,99111,1708],{"class":205},[100,99113,83226],{"class":209},[100,99115,1714],{"class":205},[100,99117,99118,99120,99122,99124,99126,99128],{"class":102,"line":135},[100,99119,1901],{"class":106},[100,99121,111],{"class":110},[100,99123,3389],{"class":191},[100,99125,170],{"class":118},[100,99127,4468],{"class":178},[100,99129,215],{"class":118},[14,99131,218],{},[91,99133,99135],{"className":93,"code":99134,"language":95,"meta":96,"style":96},"ValueError: invalid literal for int() with base 10: 'abc'\n",[17,99136,99137],{"__ignoreMap":96},[100,99138,99139,99141,99143,99145,99147,99149,99151,99153,99155,99157,99159,99161,99163],{"class":102,"line":103},[100,99140,3483],{"class":191},[100,99142,89],{"class":118},[100,99144,16983],{"class":106},[100,99146,71],{"class":145},[100,99148,3389],{"class":191},[100,99150,7562],{"class":118},[100,99152,16992],{"class":145},[100,99154,16995],{"class":106},[100,99156,356],{"class":122},[100,99158,89],{"class":118},[100,99160,1274],{"class":205},[100,99162,83226],{"class":209},[100,99164,3925],{"class":205},[14,99166,99167,99168,99170,99171,80946,99173,99175,99176,99178,99179,96444],{},"This is a ",[17,99169,3483],{},", not a ",[17,99172,3538],{},[17,99174,6119],{}," ",[22,99177,82486],{}," accept strings. The problem is that ",[17,99180,99181],{},"\"abc\"",[675,99183,99185],{"id":99184},"example-unpacking-the-wrong-number-of-values","Example: unpacking the wrong number of values",[91,99187,99189],{"className":93,"code":99188,"language":95,"meta":96,"style":96},"a, b = [1, 2, 3]\n",[17,99190,99191],{"__ignoreMap":96},[100,99192,99193,99195,99197,99199,99201,99203,99205,99207,99209,99211,99213],{"class":102,"line":103},[100,99194,295],{"class":106},[100,99196,126],{"class":118},[100,99198,25360],{"class":106},[100,99200,111],{"class":110},[100,99202,594],{"class":118},[100,99204,123],{"class":122},[100,99206,126],{"class":118},[100,99208,129],{"class":122},[100,99210,126],{"class":118},[100,99212,1405],{"class":122},[100,99214,603],{"class":118},[14,99216,218],{},[91,99218,99220],{"className":93,"code":99219,"language":95,"meta":96,"style":96},"ValueError: too many values to unpack (expected 2)\n",[17,99221,99222],{"__ignoreMap":96},[100,99223,99224,99226,99228,99231,99234,99236,99239,99241],{"class":102,"line":103},[100,99225,3483],{"class":191},[100,99227,89],{"class":118},[100,99229,99230],{"class":106}," too many values to ",[100,99232,99233],{"class":178},"unpack ",[100,99235,170],{"class":118},[100,99237,99238],{"class":178},"expected ",[100,99240,559],{"class":122},[100,99242,215],{"class":118},[14,99244,99245],{},"The list type is fine. The problem is that the number of values does not match what the code expects.",[675,99247,99249],{"id":99248},"typical-fix-for-valueerror","Typical fix for ValueError",[14,99251,99042],{},[40,99253,99254,99257,99260,99266],{},[43,99255,99256],{},"Clean or validate the input",[43,99258,99259],{},"Check the data before converting it",[43,99261,241,99262,3266,99264],{},[17,99263,146],{},[17,99265,188],{},[43,99267,99268],{},"Make sure the value format matches what the function expects",[14,99270,99271,99272,290],{},"For a full guide, see ",[295,99273,3265],{"href":3264},[77,99275,99277],{"id":99276},"side-by-side-examples","Side-by-side examples",[14,99279,99280],{},"Looking at the two errors next to each other makes the difference easier to see.",[675,99282,99284],{"id":99283},"typeerror-example","TypeError example",[91,99286,99288],{"className":93,"code":99287,"language":95,"meta":96,"style":96},"print(5 + \"3\")\n",[17,99289,99290],{"__ignoreMap":96},[100,99291,99292,99294,99296,99298,99300,99302,99304,99306],{"class":102,"line":103},[100,99293,372],{"class":114},[100,99295,170],{"class":118},[100,99297,3557],{"class":122},[100,99299,2895],{"class":110},[100,99301,1708],{"class":205},[100,99303,11541],{"class":209},[100,99305,206],{"class":205},[100,99307,215],{"class":118},[14,99309,218],{},[91,99311,99312],{"className":93,"code":98896,"language":95,"meta":96,"style":96},[17,99313,99314],{"__ignoreMap":96},[100,99315,99316,99318,99320,99322,99324,99326,99328,99330,99332,99334,99336,99338,99340,99342,99344,99346,99348],{"class":102,"line":103},[100,99317,3538],{"class":191},[100,99319,89],{"class":118},[100,99321,98907],{"class":106},[100,99323,1250],{"class":191},[100,99325,170],{"class":118},[100,99327,98914],{"class":178},[100,99329,6155],{"class":118},[100,99331,7565],{"class":145},[100,99333,2895],{"class":110},[100,99335,89],{"class":118},[100,99337,1274],{"class":205},[100,99339,3499],{"class":209},[100,99341,1280],{"class":205},[100,99343,62025],{"class":110},[100,99345,1274],{"class":205},[100,99347,3422],{"class":209},[100,99349,3925],{"class":205},[14,99351,32043],{},[40,99353,99354,99363],{},[43,99355,99356,99358,99359,3266,99361,10322],{},[17,99357,6547],{}," does not support combining ",[17,99360,3499],{},[17,99362,3422],{},[43,99364,99365,99366,290],{},"The problem is the ",[22,99367,99368],{},"types",[14,99370,15034],{},[91,99372,99374],{"className":93,"code":99373,"language":95,"meta":96,"style":96},"print(5 + int(\"3\"))\nprint(str(5) + \"3\")\n",[17,99375,99376,99398],{"__ignoreMap":96},[100,99377,99378,99380,99382,99384,99386,99388,99390,99392,99394,99396],{"class":102,"line":103},[100,99379,372],{"class":114},[100,99381,170],{"class":118},[100,99383,3557],{"class":122},[100,99385,2895],{"class":110},[100,99387,3389],{"class":191},[100,99389,170],{"class":118},[100,99391,206],{"class":205},[100,99393,11541],{"class":209},[100,99395,206],{"class":205},[100,99397,182],{"class":118},[100,99399,99400,99402,99404,99406,99408,99410,99412,99414,99416,99418,99420],{"class":102,"line":135},[100,99401,372],{"class":114},[100,99403,170],{"class":118},[100,99405,3422],{"class":191},[100,99407,170],{"class":118},[100,99409,3557],{"class":122},[100,99411,6155],{"class":118},[100,99413,2895],{"class":110},[100,99415,1708],{"class":205},[100,99417,11541],{"class":209},[100,99419,206],{"class":205},[100,99421,215],{"class":118},[14,99423,218],{},[91,99425,99427],{"className":93,"code":99426,"language":95,"meta":96,"style":96},"8\n53\n",[17,99428,99429,99434],{"__ignoreMap":96},[100,99430,99431],{"class":102,"line":103},[100,99432,99433],{"class":122},"8\n",[100,99435,99436],{"class":102,"line":135},[100,99437,99438],{"class":122},"53\n",[675,99440,99442],{"id":99441},"valueerror-example","ValueError example",[91,99444,99446],{"className":93,"code":99445,"language":95,"meta":96,"style":96},"print(int(\"3.5\"))\n",[17,99447,99448],{"__ignoreMap":96},[100,99449,99450,99452,99454,99456,99458,99460,99462,99464],{"class":102,"line":103},[100,99451,372],{"class":114},[100,99453,170],{"class":118},[100,99455,3499],{"class":191},[100,99457,170],{"class":118},[100,99459,206],{"class":205},[100,99461,80892],{"class":209},[100,99463,206],{"class":205},[100,99465,182],{"class":118},[14,99467,218],{},[91,99469,99471],{"className":93,"code":99470,"language":95,"meta":96,"style":96},"ValueError: invalid literal for int() with base 10: '3.5'\n",[17,99472,99473],{"__ignoreMap":96},[100,99474,99475,99477,99479,99481,99483,99485,99487,99489,99491,99493,99495,99497,99499],{"class":102,"line":103},[100,99476,3483],{"class":191},[100,99478,89],{"class":118},[100,99480,16983],{"class":106},[100,99482,71],{"class":145},[100,99484,3389],{"class":191},[100,99486,7562],{"class":118},[100,99488,16992],{"class":145},[100,99490,16995],{"class":106},[100,99492,356],{"class":122},[100,99494,89],{"class":118},[100,99496,1274],{"class":205},[100,99498,80892],{"class":209},[100,99500,3925],{"class":205},[14,99502,32043],{},[40,99504,99505,99510,99515],{},[43,99506,99507,99509],{},[17,99508,6119],{}," accepts strings",[43,99511,38150,99512,99514],{},[17,99513,96443],{}," is not a valid integer string",[43,99516,99365,99517],{},[22,99518,757],{},[14,99520,12066],{},[91,99522,99524],{"className":93,"code":99523,"language":95,"meta":96,"style":96},"print(int(\"abc\"))\n",[17,99525,99526],{"__ignoreMap":96},[100,99527,99528,99530,99532,99534,99536,99538,99540,99542],{"class":102,"line":103},[100,99529,372],{"class":114},[100,99531,170],{"class":118},[100,99533,3499],{"class":191},[100,99535,170],{"class":118},[100,99537,206],{"class":205},[100,99539,83226],{"class":209},[100,99541,206],{"class":205},[100,99543,182],{"class":118},[14,99545,218],{},[91,99547,99548],{"className":93,"code":99134,"language":95,"meta":96,"style":96},[17,99549,99550],{"__ignoreMap":96},[100,99551,99552,99554,99556,99558,99560,99562,99564,99566,99568,99570,99572,99574,99576],{"class":102,"line":103},[100,99553,3483],{"class":191},[100,99555,89],{"class":118},[100,99557,16983],{"class":106},[100,99559,71],{"class":145},[100,99561,3389],{"class":191},[100,99563,7562],{"class":118},[100,99565,16992],{"class":145},[100,99567,16995],{"class":106},[100,99569,356],{"class":122},[100,99571,89],{"class":118},[100,99573,1274],{"class":205},[100,99575,83226],{"class":209},[100,99577,3925],{"class":205},[14,99579,99580,99581,99583],{},"This is why input handling often leads to ",[17,99582,3483],{},". The type may be correct, but the text does not contain a usable value.",[14,99585,99586,99587,3266,99589,290],{},"If you are working with input conversion, see ",[295,99588,14983],{"href":14982},[295,99590,81195],{"href":81194},[77,99592,99594],{"id":99593},"how-to-decide-which-one-you-have","How to decide which one you have",[14,99596,99597],{},"When you see one of these errors, use this quick check:",[3282,99599,99600,99603,99609,99615,99618],{},[43,99601,99602],{},"Read the exception name first.",[43,99604,99605,99606],{},"Ask: ",[22,99607,99608],{},"Is the object itself the wrong type?",[43,99610,99611,99612],{},"If not, ask: ",[22,99613,99614],{},"Is the type allowed, but the value invalid?",[43,99616,99617],{},"Check the exact line that failed.",[43,99619,99620],{},"Inspect the variables involved.",[14,99622,99623],{},"A very useful beginner debugging trick is printing both the value and its type.",[91,99625,99627],{"className":93,"code":99626,"language":95,"meta":96,"style":96},"value = \"123\"\n\nprint(type(value))\nprint(value)\n",[17,99628,99629,99641,99645,99659],{"__ignoreMap":96},[100,99630,99631,99633,99635,99637,99639],{"class":102,"line":103},[100,99632,3318],{"class":106},[100,99634,111],{"class":110},[100,99636,1708],{"class":205},[100,99638,3002],{"class":209},[100,99640,1714],{"class":205},[100,99642,99643],{"class":102,"line":135},[100,99644,139],{"emptyLinePlaceholder":138},[100,99646,99647,99649,99651,99653,99655,99657],{"class":102,"line":142},[100,99648,372],{"class":114},[100,99650,170],{"class":118},[100,99652,1250],{"class":191},[100,99654,170],{"class":118},[100,99656,757],{"class":178},[100,99658,182],{"class":118},[100,99660,99661,99663,99665,99667],{"class":102,"line":152},[100,99662,372],{"class":114},[100,99664,170],{"class":118},[100,99666,757],{"class":178},[100,99668,215],{"class":118},[14,99670,218],{},[91,99672,99674],{"className":93,"code":99673,"language":95,"meta":96,"style":96},"\u003Cclass 'str'>\n123\n",[17,99675,99676,99690],{"__ignoreMap":96},[100,99677,99678,99680,99682,99684,99686,99688],{"class":102,"line":103},[100,99679,2782],{"class":110},[100,99681,3417],{"class":1077},[100,99683,1274],{"class":205},[100,99685,3422],{"class":209},[100,99687,1280],{"class":205},[100,99689,1980],{"class":110},[100,99691,99692],{"class":102,"line":135},[100,99693,6067],{"class":122},[14,99695,4424,99696,99699],{},[17,99697,99698],{},"repr()"," to see the exact contents more clearly:",[91,99701,99703],{"className":93,"code":99702,"language":95,"meta":96,"style":96},"value = \" 123 \"\nprint(repr(value))\n",[17,99704,99705,99718],{"__ignoreMap":96},[100,99706,99707,99709,99711,99713,99716],{"class":102,"line":103},[100,99708,3318],{"class":106},[100,99710,111],{"class":110},[100,99712,1708],{"class":205},[100,99714,99715],{"class":209}," 123 ",[100,99717,1714],{"class":205},[100,99719,99720,99722,99724,99726,99728,99730],{"class":102,"line":135},[100,99721,372],{"class":114},[100,99723,170],{"class":118},[100,99725,12228],{"class":114},[100,99727,170],{"class":118},[100,99729,757],{"class":178},[100,99731,182],{"class":118},[14,99733,218],{},[91,99735,99737],{"className":93,"code":99736,"language":95,"meta":96,"style":96},"' 123 '\n",[17,99738,99739],{"__ignoreMap":96},[100,99740,99741,99743,99745],{"class":102,"line":103},[100,99742,1280],{"class":3553},[100,99744,99715],{"class":3556},[100,99746,3925],{"class":3553},[14,99748,99749],{},"That helps you notice spaces, quotes, or unexpected characters.",[77,99751,99753],{"id":99752},"common-fixes-for-typeerror","Common fixes for TypeError",[14,99755,99756,99757,89],{},"Here are some common ways to fix ",[17,99758,3538],{},[40,99760,99761,99764,99767,99772,99775],{},[43,99762,99763],{},"Convert values before combining them",[43,99765,99766],{},"Pass the expected type to functions",[43,99768,99053,99769,99771],{},[17,99770,930],{}," before looping or indexing",[43,99773,99774],{},"Do not call lists or dictionaries like functions",[43,99776,241,99777,99779],{},[17,99778,5256],{}," when you need to verify a type",[675,99781,99783],{"id":99782},"example-convert-before-combining","Example: convert before combining",[91,99785,99787],{"className":93,"code":99786,"language":95,"meta":96,"style":96},"age = 25\nmessage = \"Age: \" + str(age)\n\nprint(message)\n",[17,99788,99789,99797,99819,99823],{"__ignoreMap":96},[100,99790,99791,99793,99795],{"class":102,"line":103},[100,99792,1664],{"class":106},[100,99794,111],{"class":110},[100,99796,16667],{"class":122},[100,99798,99799,99801,99803,99805,99807,99809,99811,99813,99815,99817],{"class":102,"line":135},[100,99800,6868],{"class":106},[100,99802,111],{"class":110},[100,99804,1708],{"class":205},[100,99806,16775],{"class":209},[100,99808,206],{"class":205},[100,99810,2895],{"class":110},[100,99812,2871],{"class":191},[100,99814,170],{"class":118},[100,99816,2770],{"class":178},[100,99818,215],{"class":118},[100,99820,99821],{"class":102,"line":142},[100,99822,139],{"emptyLinePlaceholder":138},[100,99824,99825,99827,99829,99831],{"class":102,"line":152},[100,99826,372],{"class":114},[100,99828,170],{"class":118},[100,99830,1836],{"class":178},[100,99832,215],{"class":118},[14,99834,218],{},[91,99836,99837],{"className":93,"code":76912,"language":95,"meta":96,"style":96},[17,99838,99839],{"__ignoreMap":96},[100,99840,99841,99843,99845],{"class":102,"line":103},[100,99842,76919],{"class":106},[100,99844,89],{"class":118},[100,99846,16667],{"class":122},[675,99848,99850],{"id":99849},"example-check-for-none","Example: check for None",[91,99852,99854],{"className":93,"code":99853,"language":95,"meta":96,"style":96},"items = None\n\nif items is not None:\n    print(len(items))\nelse:\n    print(\"items is None\")\n",[17,99855,99856,99864,99868,99882,99896,99902],{"__ignoreMap":96},[100,99857,99858,99860,99862],{"class":102,"line":103},[100,99859,3095],{"class":106},[100,99861,111],{"class":110},[100,99863,4904],{"class":158},[100,99865,99866],{"class":102,"line":135},[100,99867,139],{"emptyLinePlaceholder":138},[100,99869,99870,99872,99874,99876,99878,99880],{"class":102,"line":142},[100,99871,2736],{"class":145},[100,99873,81230],{"class":106},[100,99875,4918],{"class":110},[100,99877,4921],{"class":110},[100,99879,866],{"class":158},[100,99881,149],{"class":118},[100,99883,99884,99886,99888,99890,99892,99894],{"class":102,"line":152},[100,99885,200],{"class":114},[100,99887,170],{"class":118},[100,99889,17779],{"class":114},[100,99891,170],{"class":118},[100,99893,3146],{"class":178},[100,99895,182],{"class":118},[100,99897,99898,99900],{"class":102,"line":164},[100,99899,4944],{"class":145},[100,99901,149],{"class":118},[100,99903,99904,99906,99908,99910,99913,99915],{"class":102,"line":185},[100,99905,200],{"class":114},[100,99907,170],{"class":118},[100,99909,206],{"class":205},[100,99911,99912],{"class":209},"items is None",[100,99914,206],{"class":205},[100,99916,215],{"class":118},[14,99918,218],{},[91,99920,99922],{"className":93,"code":99921,"language":95,"meta":96,"style":96},"items is None\n",[17,99923,99924],{"__ignoreMap":96},[100,99925,99926,99928,99930],{"class":102,"line":103},[100,99927,3095],{"class":106},[100,99929,4918],{"class":110},[100,99931,4904],{"class":158},[675,99933,99935],{"id":99934},"example-verify-a-type","Example: verify a type",[91,99937,99939],{"className":93,"code":99938,"language":95,"meta":96,"style":96},"value = \"42\"\n\nif isinstance(value, str):\n    print(int(value))\n",[17,99940,99941,99953,99957,99973],{"__ignoreMap":96},[100,99942,99943,99945,99947,99949,99951],{"class":102,"line":103},[100,99944,3318],{"class":106},[100,99946,111],{"class":110},[100,99948,1708],{"class":205},[100,99950,95521],{"class":209},[100,99952,1714],{"class":205},[100,99954,99955],{"class":102,"line":135},[100,99956,139],{"emptyLinePlaceholder":138},[100,99958,99959,99961,99963,99965,99967,99969,99971],{"class":102,"line":142},[100,99960,2736],{"class":145},[100,99962,2862],{"class":114},[100,99964,170],{"class":118},[100,99966,757],{"class":178},[100,99968,126],{"class":118},[100,99970,2871],{"class":191},[100,99972,522],{"class":118},[100,99974,99975,99977,99979,99981,99983,99985],{"class":102,"line":152},[100,99976,200],{"class":114},[100,99978,170],{"class":118},[100,99980,3499],{"class":191},[100,99982,170],{"class":118},[100,99984,757],{"class":178},[100,99986,182],{"class":118},[14,99988,218],{},[91,99990,99992],{"className":93,"code":99991,"language":95,"meta":96,"style":96},"42\n",[17,99993,99994],{"__ignoreMap":96},[100,99995,99996],{"class":102,"line":103},[100,99997,99991],{"class":122},[14,99999,100000,100001,100004],{},"If your error says something like ",[17,100002,100003],{},"unsupported operand type(s)",", this guide may help: fix \"TypeError: unsupported operand type(s) for +\" in Python.",[77,100006,100008],{"id":100007},"common-fixes-for-valueerror","Common fixes for ValueError",[14,100010,100011,100012,89],{},"Here are common ways to fix ",[17,100013,3483],{},[40,100015,100016,100019,100030,100033,100036],{},[43,100017,100018],{},"Validate user input before conversion",[43,100020,241,100021,2558,100023,100025,100026,3266,100028],{},[17,100022,146],{},[17,100024,188],{}," around ",[17,100027,6119],{},[17,100029,59778],{},[43,100031,100032],{},"Check list sizes before unpacking",[43,100034,100035],{},"Make sure the value exists before using methods that require a match",[43,100037,100038],{},"Remove extra spaces or unexpected characters",[675,100040,100042],{"id":100041},"example-safe-integer-conversion","Example: safe integer conversion",[91,100044,100046],{"className":93,"code":100045,"language":95,"meta":96,"style":96},"user_input = \"abc\"\n\ntry:\n    number = int(user_input)\n    print(number)\nexcept ValueError:\n    print(\"Please enter a valid whole number\")\n",[17,100047,100048,100060,100064,100070,100084,100094,100102],{"__ignoreMap":96},[100,100049,100050,100052,100054,100056,100058],{"class":102,"line":103},[100,100051,6201],{"class":106},[100,100053,111],{"class":110},[100,100055,1708],{"class":205},[100,100057,83226],{"class":209},[100,100059,1714],{"class":205},[100,100061,100062],{"class":102,"line":135},[100,100063,139],{"emptyLinePlaceholder":138},[100,100065,100066,100068],{"class":102,"line":142},[100,100067,146],{"class":145},[100,100069,149],{"class":118},[100,100071,100072,100074,100076,100078,100080,100082],{"class":102,"line":152},[100,100073,98700],{"class":106},[100,100075,111],{"class":110},[100,100077,3389],{"class":191},[100,100079,170],{"class":118},[100,100081,6230],{"class":178},[100,100083,215],{"class":118},[100,100085,100086,100088,100090,100092],{"class":102,"line":164},[100,100087,200],{"class":114},[100,100089,170],{"class":118},[100,100091,631],{"class":178},[100,100093,215],{"class":118},[100,100095,100096,100098,100100],{"class":102,"line":185},[100,100097,188],{"class":145},[100,100099,2794],{"class":191},[100,100101,149],{"class":118},[100,100103,100104,100106,100108,100110,100113,100115],{"class":102,"line":197},[100,100105,200],{"class":114},[100,100107,170],{"class":118},[100,100109,206],{"class":205},[100,100111,100112],{"class":209},"Please enter a valid whole number",[100,100114,206],{"class":205},[100,100116,215],{"class":118},[14,100118,218],{},[91,100120,100122],{"className":93,"code":100121,"language":95,"meta":96,"style":96},"Please enter a valid whole number\n",[17,100123,100124],{"__ignoreMap":96},[100,100125,100126],{"class":102,"line":103},[100,100127,100121],{"class":106},[675,100129,100131],{"id":100130},"example-clean-input-first","Example: clean input first",[91,100133,100135],{"className":93,"code":100134,"language":95,"meta":96,"style":96},"user_input = \" 42 \"\nnumber = int(user_input.strip())\n\nprint(number)\n",[17,100136,100137,100150,100168,100172],{"__ignoreMap":96},[100,100138,100139,100141,100143,100145,100148],{"class":102,"line":103},[100,100140,6201],{"class":106},[100,100142,111],{"class":110},[100,100144,1708],{"class":205},[100,100146,100147],{"class":209}," 42 ",[100,100149,1714],{"class":205},[100,100151,100152,100154,100156,100158,100160,100162,100164,100166],{"class":102,"line":135},[100,100153,1901],{"class":106},[100,100155,111],{"class":110},[100,100157,3389],{"class":191},[100,100159,170],{"class":118},[100,100161,6230],{"class":178},[100,100163,290],{"class":118},[100,100165,6235],{"class":178},[100,100167,3370],{"class":118},[100,100169,100170],{"class":102,"line":142},[100,100171,139],{"emptyLinePlaceholder":138},[100,100173,100174,100176,100178,100180],{"class":102,"line":152},[100,100175,372],{"class":114},[100,100177,170],{"class":118},[100,100179,631],{"class":178},[100,100181,215],{"class":118},[14,100183,218],{},[91,100185,100186],{"className":93,"code":99991,"language":95,"meta":96,"style":96},[17,100187,100188],{"__ignoreMap":96},[100,100189,100190],{"class":102,"line":103},[100,100191,99991],{"class":122},[675,100193,100195],{"id":100194},"example-check-unpacking-size","Example: check unpacking size",[91,100197,100199],{"className":93,"code":100198,"language":95,"meta":96,"style":96},"parts = \"red,green,blue\".split(\",\")\n\nif len(parts) == 3:\n    a, b, c = parts\n    print(a, b, c)\nelse:\n    print(\"Expected exactly 3 values\")\n",[17,100200,100201,100228,100232,100250,100269,100288,100294],{"__ignoreMap":96},[100,100202,100203,100205,100207,100209,100212,100214,100216,100218,100220,100222,100224,100226],{"class":102,"line":103},[100,100204,4345],{"class":106},[100,100206,111],{"class":110},[100,100208,1708],{"class":205},[100,100210,100211],{"class":209},"red,green,blue",[100,100213,206],{"class":205},[100,100215,290],{"class":118},[100,100217,3922],{"class":178},[100,100219,170],{"class":118},[100,100221,206],{"class":205},[100,100223,126],{"class":209},[100,100225,206],{"class":205},[100,100227,215],{"class":118},[100,100229,100230],{"class":102,"line":135},[100,100231,139],{"emptyLinePlaceholder":138},[100,100233,100234,100236,100238,100240,100242,100244,100246,100248],{"class":102,"line":142},[100,100235,2736],{"class":145},[100,100237,3141],{"class":114},[100,100239,170],{"class":118},[100,100241,4373],{"class":178},[100,100243,6155],{"class":118},[100,100245,34516],{"class":110},[100,100247,1405],{"class":122},[100,100249,149],{"class":118},[100,100251,100252,100255,100257,100259,100261,100264,100266],{"class":102,"line":152},[100,100253,100254],{"class":106},"    a",[100,100256,126],{"class":118},[100,100258,15869],{"class":106},[100,100260,126],{"class":118},[100,100262,100263],{"class":106}," c ",[100,100265,111],{"class":110},[100,100267,100268],{"class":106}," parts\n",[100,100270,100271,100273,100275,100277,100279,100281,100283,100286],{"class":102,"line":164},[100,100272,200],{"class":114},[100,100274,170],{"class":118},[100,100276,295],{"class":178},[100,100278,126],{"class":118},[100,100280,15869],{"class":178},[100,100282,126],{"class":118},[100,100284,100285],{"class":178}," c",[100,100287,215],{"class":118},[100,100289,100290,100292],{"class":102,"line":185},[100,100291,4944],{"class":145},[100,100293,149],{"class":118},[100,100295,100296,100298,100300,100302,100305,100307],{"class":102,"line":197},[100,100297,200],{"class":114},[100,100299,170],{"class":118},[100,100301,206],{"class":205},[100,100303,100304],{"class":209},"Expected exactly 3 values",[100,100306,206],{"class":205},[100,100308,215],{"class":118},[14,100310,218],{},[91,100312,100314],{"className":93,"code":100313,"language":95,"meta":96,"style":96},"red green blue\n",[17,100315,100316],{"__ignoreMap":96},[100,100317,100318],{"class":102,"line":103},[100,100319,100313],{"class":106},[14,100321,100322,100323,12574,100326,290],{},"If your error message mentions ",[17,100324,100325],{},"invalid literal for int()",[295,100327,100328],{"href":17023},"how to fix \"ValueError: invalid literal for int() with base 10\"",[77,100330,100332],{"id":100331},"beginner-debugging-checklist","Beginner debugging checklist",[14,100334,100335,100336,3178,100338,100340],{},"When you are not sure whether the problem is ",[17,100337,3538],{},[17,100339,3483],{},", go through this checklist:",[40,100342,100343,100346,100349,100352,100355],{},[43,100344,100345],{},"Read the full traceback and find the failing line",[43,100347,100348],{},"Print both the value and its type",[43,100350,100351],{},"Look at the function you called and what it expects",[43,100353,100354],{},"Check whether the data shape matches what the code expects",[43,100356,100357],{},"If converting input, test with simple known-good values first",[14,100359,1206],{},[91,100361,100363],{"className":93,"code":100362,"language":95,"meta":96,"style":96},"print(type(value))\nprint(repr(value))\nprint(type(a), repr(a), type(b), repr(b))\nprint(isinstance(value, str))\nprint(isinstance(value, int))\n",[17,100364,100365,100379,100393,100432,100450],{"__ignoreMap":96},[100,100366,100367,100369,100371,100373,100375,100377],{"class":102,"line":103},[100,100368,372],{"class":114},[100,100370,170],{"class":118},[100,100372,1250],{"class":191},[100,100374,170],{"class":118},[100,100376,757],{"class":178},[100,100378,182],{"class":118},[100,100380,100381,100383,100385,100387,100389,100391],{"class":102,"line":135},[100,100382,372],{"class":114},[100,100384,170],{"class":118},[100,100386,12228],{"class":114},[100,100388,170],{"class":118},[100,100390,757],{"class":178},[100,100392,182],{"class":118},[100,100394,100395,100397,100399,100401,100403,100405,100407,100410,100412,100414,100416,100418,100420,100422,100424,100426,100428,100430],{"class":102,"line":142},[100,100396,372],{"class":114},[100,100398,170],{"class":118},[100,100400,1250],{"class":191},[100,100402,170],{"class":118},[100,100404,295],{"class":178},[100,100406,2874],{"class":118},[100,100408,100409],{"class":114}," repr",[100,100411,170],{"class":118},[100,100413,295],{"class":178},[100,100415,2874],{"class":118},[100,100417,6767],{"class":191},[100,100419,170],{"class":118},[100,100421,4027],{"class":178},[100,100423,2874],{"class":118},[100,100425,100409],{"class":114},[100,100427,170],{"class":118},[100,100429,4027],{"class":178},[100,100431,182],{"class":118},[100,100433,100434,100436,100438,100440,100442,100444,100446,100448],{"class":102,"line":152},[100,100435,372],{"class":114},[100,100437,170],{"class":118},[100,100439,6994],{"class":114},[100,100441,170],{"class":118},[100,100443,757],{"class":178},[100,100445,126],{"class":118},[100,100447,2871],{"class":191},[100,100449,182],{"class":118},[100,100451,100452,100454,100456,100458,100460,100462,100464,100466],{"class":102,"line":164},[100,100453,372],{"class":114},[100,100455,170],{"class":118},[100,100457,6994],{"class":114},[100,100459,170],{"class":118},[100,100461,757],{"class":178},[100,100463,126],{"class":118},[100,100465,3389],{"class":191},[100,100467,182],{"class":118},[14,100469,100470],{},"You can also inspect built-in functions:",[91,100472,100474],{"className":93,"code":100473,"language":95,"meta":96,"style":96},"help(int)\nhelp(float)\n",[17,100475,100476,100486],{"__ignoreMap":96},[100,100477,100478,100480,100482,100484],{"class":102,"line":103},[100,100479,2478],{"class":114},[100,100481,170],{"class":118},[100,100483,3499],{"class":191},[100,100485,215],{"class":118},[100,100487,100488,100490,100492,100494],{"class":102,"line":135},[100,100489,2478],{"class":114},[100,100491,170],{"class":118},[100,100493,60476],{"class":191},[100,100495,215],{"class":118},[14,100497,100498,100499,3266,100503,290],{},"That can help you understand what kinds of input those functions accept. For more background, see ",[295,100500,100502],{"href":100501},"\u002Flearn\u002Ftype-conversion-in-python-casting-between-types\u002F","type conversion in Python",[295,100504,60492],{"href":60491},[77,100506,100508],{"id":100507},"related-errors-beginners-confuse-with-these","Related errors beginners confuse with these",[14,100510,100511],{},"Some errors look similar but mean something different:",[40,100513,100514,100519,100524,100529],{},[43,100515,100516,100518],{},[17,100517,3738],{},": the object does not have the attribute or method you tried to use",[43,100520,100521,100523],{},[17,100522,16420],{},": a list position is outside the valid range",[43,100525,100526,100528],{},[17,100527,17063],{},": a dictionary key does not exist",[43,100530,100531,100533],{},[17,100532,5616],{},": a variable name is not defined",[14,100535,100536,100537,290],{},"These are different from type and value problems. If you want a broader overview, see ",[295,100538,1587],{"href":1749},[77,100540,1514],{"id":1513},[675,100542,5449,100544,100547],{"id":100543},"is-int123-a-typeerror-or-valueerror-if-it-fails",[17,100545,100546],{},"int(\"123\")"," a TypeError or ValueError if it fails?",[14,100549,100550,100551,80946,100553,100555],{},"It is usually a ",[17,100552,3483],{},[17,100554,6119],{}," accepts strings, but the string value must look like a valid integer.",[675,100557,100559],{"id":100558},"why-is-adding-a-string-and-an-integer-a-typeerror","Why is adding a string and an integer a TypeError?",[14,100561,100562,100563,100565],{},"Because the ",[17,100564,6547],{}," operation does not support those two types together without conversion.",[675,100567,100569],{"id":100568},"can-the-same-line-sometimes-raise-typeerror-and-sometimes-valueerror","Can the same line sometimes raise TypeError and sometimes ValueError?",[14,100571,100572],{},"Yes. It depends on the actual object passed at runtime. A function may reject one input because of type and another because of value.",[675,100574,100576],{"id":100575},"should-i-catch-both-typeerror-and-valueerror","Should I catch both TypeError and ValueError?",[14,100578,100579],{},"Only if both can realistically happen in that part of your code. Catch the most specific errors that fit the situation.",[675,100581,100583],{"id":100582},"which-error-is-more-common-with-user-input","Which error is more common with user input?",[14,100585,100586,100588],{},[17,100587,3483],{}," is very common with user input because text often needs conversion and may contain invalid values.",[77,100590,1554],{"id":1553},[40,100592,100593,100597,100601,100605,100609],{},[43,100594,100595],{},[295,100596,99065],{"href":99064},[43,100598,100599],{},[295,100600,3265],{"href":3264},[43,100602,100603],{},[295,100604,96866],{"href":14982},[43,100606,100607],{},[295,100608,81195],{"href":81194},[43,100610,100611],{},[295,100612,1587],{"href":1749},[1589,100614,100615],{},"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 .sP7_E, html code.shiki .sP7_E{--shiki-light:#39ADB5;--shiki-default:#24292E;--shiki-dark:#E1E4E8}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 .sZMiF, html code.shiki .sZMiF{--shiki-light:#E2931D;--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 .sptTA, html code.shiki .sptTA{--shiki-light:#6182B8;--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 .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 .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 .s2W-s, html code.shiki .s2W-s{--shiki-light:#39ADB5;--shiki-light-font-style:italic;--shiki-default:#032F62;--shiki-default-font-style:inherit;--shiki-dark:#9ECBFF;--shiki-dark-font-style:inherit}html pre.shiki code .sithA, html code.shiki .sithA{--shiki-light:#90A4AE;--shiki-light-font-style:italic;--shiki-default:#032F62;--shiki-default-font-style:inherit;--shiki-dark:#9ECBFF;--shiki-dark-font-style:inherit}html pre.shiki code .s39Yj, html code.shiki .s39Yj{--shiki-light:#39ADB5;--shiki-default:#005CC5;--shiki-dark:#79B8FF}",{"title":96,"searchDepth":135,"depth":135,"links":100617},[100618,100619,100620,100625,100630,100634,100635,100640,100645,100646,100647,100655],{"id":98683,"depth":135,"text":98684},{"id":17922,"depth":135,"text":17923},{"id":98815,"depth":135,"text":98816,"children":100621},[100622,100623,100624],{"id":98846,"depth":142,"text":98847},{"id":98950,"depth":142,"text":98951},{"id":99038,"depth":142,"text":99039},{"id":99068,"depth":135,"text":99069,"children":100626},[100627,100628,100629],{"id":99097,"depth":142,"text":99098},{"id":99184,"depth":142,"text":99185},{"id":99248,"depth":142,"text":99249},{"id":99276,"depth":135,"text":99277,"children":100631},[100632,100633],{"id":99283,"depth":142,"text":99284},{"id":99441,"depth":142,"text":99442},{"id":99593,"depth":135,"text":99594},{"id":99752,"depth":135,"text":99753,"children":100636},[100637,100638,100639],{"id":99782,"depth":142,"text":99783},{"id":99849,"depth":142,"text":99850},{"id":99934,"depth":142,"text":99935},{"id":100007,"depth":135,"text":100008,"children":100641},[100642,100643,100644],{"id":100041,"depth":142,"text":100042},{"id":100130,"depth":142,"text":100131},{"id":100194,"depth":142,"text":100195},{"id":100331,"depth":135,"text":100332},{"id":100507,"depth":135,"text":100508},{"id":1513,"depth":135,"text":1514,"children":100648},[100649,100651,100652,100653,100654],{"id":100543,"depth":142,"text":100650},"Is int(\"123\") a TypeError or ValueError if it fails?",{"id":100558,"depth":142,"text":100559},{"id":100568,"depth":142,"text":100569},{"id":100575,"depth":142,"text":100576},{"id":100582,"depth":142,"text":100583},{"id":1553,"depth":135,"text":1554},"Master typeerror vs valueerror in python explained in our comprehensive Python beginner guide.",{},{"title":98659,"description":100656},"errors\u002Ftypeerror-vs-valueerror-in-python-explained","dVGd-Ata6cLtw7KCEjsq-lKd_NlBNBBXEPhNr30f498",{"id":100662,"title":100663,"body":100664,"description":102171,"extension":1623,"meta":102172,"navigation":138,"path":53908,"seo":102173,"stem":102174,"__hash__":102175},"content\u002Ferrors\u002Funboundlocalerror-local-variable-referenced-before-assignment-fix.md","UnboundLocalError: local variable referenced before assignment (Fix)",{"type":7,"value":100665,"toc":102141},[100666,100669,100674,100676,100724,100734,100736,100740,100761,100763,100815,100833,100837,100840,100846,100849,100910,100924,100930,100942,100947,100949,100954,101011,101013,101036,101038,101063,101066,101135,101146,101150,101153,101156,101197,101199,101207,101210,101221,101226,101230,101233,101300,101302,101310,101313,101324,101328,101332,101337,101401,101403,101411,101417,101421,101432,101437,101441,101450,101453,101543,101545,101557,101571,101575,101583,101586,101591,101688,101690,101705,101710,101836,101838,101856,101864,101866,101868,101903,101905,101915,101962,101965,101983,101988,101990,101993,101995,102002,102006,102015,102019,102022,102032,102041,102043,102046,102069,102071,102075,102083,102087,102090,102094,102097,102101,102106,102108,102138],[10,100667,100663],{"id":100668},"unboundlocalerror-local-variable-referenced-before-assignment-fix",[14,100670,31738,100671,100673],{},[17,100672,53909],{},". This page explains what the error means, why Python raises it inside functions, and the safest ways to fix it.",[77,100675,80],{"id":79},[91,100677,100679],{"className":93,"code":100678,"language":95,"meta":96,"style":96},"count = 10\n\ndef show_count():\n    print(count)  # works because we only read the global value\n\nshow_count()\n",[17,100680,100681,100689,100693,100701,100714,100718],{"__ignoreMap":96},[100,100682,100683,100685,100687],{"class":102,"line":103},[100,100684,2176],{"class":106},[100,100686,111],{"class":110},[100,100688,2181],{"class":122},[100,100690,100691],{"class":102,"line":135},[100,100692,139],{"emptyLinePlaceholder":138},[100,100694,100695,100697,100699],{"class":102,"line":142},[100,100696,1078],{"class":1077},[100,100698,55138],{"class":1081},[100,100700,1085],{"class":118},[100,100702,100703,100705,100707,100709,100711],{"class":102,"line":152},[100,100704,200],{"class":114},[100,100706,170],{"class":118},[100,100708,51844],{"class":178},[100,100710,6155],{"class":118},[100,100712,100713],{"class":414},"  # works because we only read the global value\n",[100,100715,100716],{"class":102,"line":164},[100,100717,139],{"emptyLinePlaceholder":138},[100,100719,100720,100722],{"class":102,"line":185},[100,100721,55167],{"class":178},[100,100723,4734],{"class":118},[14,100725,100726,100727,2558,100730,100733],{},"If you assign to a variable anywhere inside a function, Python treats it as local in that function. Use a parameter, return value, or ",[17,100728,100729],{},"global",[17,100731,100732],{},"nonlocal"," only when appropriate.",[77,100735,5881],{"id":5880},[14,100737,100738,24911],{},[17,100739,16651],{},[40,100741,100742,100748,100754],{},[43,100743,100744,100745],{},"Python thinks the variable is ",[22,100746,100747],{},"local to the current function",[43,100749,100750,100751],{},"Your code tries to ",[22,100752,100753],{},"use that local variable before it gets a value",[43,100755,100756,100757,100760],{},"This often happens when you ",[22,100758,100759],{},"read a variable first and assign to it later"," in the same function",[14,100762,27671],{},[91,100764,100765],{"className":93,"code":55117,"language":95,"meta":96,"style":96},[17,100766,100767,100775,100779,100787,100797,100805,100809],{"__ignoreMap":96},[100,100768,100769,100771,100773],{"class":102,"line":103},[100,100770,2176],{"class":106},[100,100772,111],{"class":110},[100,100774,2181],{"class":122},[100,100776,100777],{"class":102,"line":135},[100,100778,139],{"emptyLinePlaceholder":138},[100,100780,100781,100783,100785],{"class":102,"line":142},[100,100782,1078],{"class":1077},[100,100784,55138],{"class":1081},[100,100786,1085],{"class":118},[100,100788,100789,100791,100793,100795],{"class":102,"line":152},[100,100790,200],{"class":114},[100,100792,170],{"class":118},[100,100794,51844],{"class":178},[100,100796,215],{"class":118},[100,100798,100799,100801,100803],{"class":102,"line":164},[100,100800,41602],{"class":106},[100,100802,111],{"class":110},[100,100804,1669],{"class":122},[100,100806,100807],{"class":102,"line":185},[100,100808,139],{"emptyLinePlaceholder":138},[100,100810,100811,100813],{"class":102,"line":197},[100,100812,55167],{"class":178},[100,100814,4734],{"class":118},[14,100816,100817,100818,100821,100822,100824,100825,100828,100829,100832],{},"This raises an error because ",[17,100819,100820],{},"count = 20"," makes Python treat ",[17,100823,51844],{}," as a local variable inside ",[17,100826,100827],{},"show_count()",". Then ",[17,100830,100831],{},"print(count)"," tries to read that local variable before it has been assigned.",[77,100834,100836],{"id":100835},"why-python-raises-this-error","Why Python raises this error",[14,100838,100839],{},"Python decides variable scope for a function when it reads the function body.",[14,100841,100842,100843,290],{},"If a function assigns to a variable, Python marks that name as ",[22,100844,100845],{},"local for the whole function",[14,100847,100848],{},"That means this code fails:",[91,100850,100852],{"className":93,"code":100851,"language":95,"meta":96,"style":96},"message = \"Hello\"\n\ndef greet():\n    print(message)\n    message = \"Hi\"\n\ngreet()\n",[17,100853,100854,100866,100870,100878,100888,100900,100904],{"__ignoreMap":96},[100,100855,100856,100858,100860,100862,100864],{"class":102,"line":103},[100,100857,6868],{"class":106},[100,100859,111],{"class":110},[100,100861,1708],{"class":205},[100,100863,7683],{"class":209},[100,100865,1714],{"class":205},[100,100867,100868],{"class":102,"line":135},[100,100869,139],{"emptyLinePlaceholder":138},[100,100871,100872,100874,100876],{"class":102,"line":142},[100,100873,1078],{"class":1077},[100,100875,2848],{"class":1081},[100,100877,1085],{"class":118},[100,100879,100880,100882,100884,100886],{"class":102,"line":152},[100,100881,200],{"class":114},[100,100883,170],{"class":118},[100,100885,1836],{"class":178},[100,100887,215],{"class":118},[100,100889,100890,100892,100894,100896,100898],{"class":102,"line":164},[100,100891,53759],{"class":106},[100,100893,111],{"class":110},[100,100895,1708],{"class":205},[100,100897,29041],{"class":209},[100,100899,1714],{"class":205},[100,100901,100902],{"class":102,"line":185},[100,100903,139],{"emptyLinePlaceholder":138},[100,100905,100906,100908],{"class":102,"line":197},[100,100907,2911],{"class":178},[100,100909,4734],{"class":118},[14,100911,96030,100912,100914,100915,100918,100919,100921,100922,290],{},[17,100913,1836],{}," exists outside the function, Python sees ",[17,100916,100917],{},"message = \"Hi\""," and treats ",[17,100920,1836],{}," as local inside ",[17,100923,53825],{},[14,100925,24950,100926,89],{},[295,100927,100928],{"href":54194},[17,100929,5616],{},[40,100931,100932,100937],{},[43,100933,100934,100936],{},[17,100935,16651],{},": the variable is treated as local, but used before assignment",[43,100938,100939,100941],{},[17,100940,5616],{},": the variable name does not exist at all",[14,100943,100944,100945,290],{},"If function scope feels unclear, see ",[295,100946,29749],{"href":29189},[77,100948,11031],{"id":11030},[14,100950,100951,100952,290],{},"A very common case is trying to update a global variable inside a function without ",[17,100953,100729],{},[91,100955,100957],{"className":93,"code":100956,"language":95,"meta":96,"style":96},"total = 0\n\ndef add_one():\n    total = total + 1\n    return total\n\nadd_one()\n",[17,100958,100959,100967,100971,100980,100993,101000,101004],{"__ignoreMap":96},[100,100960,100961,100963,100965],{"class":102,"line":103},[100,100962,53376],{"class":106},[100,100964,111],{"class":110},[100,100966,2069],{"class":122},[100,100968,100969],{"class":102,"line":135},[100,100970,139],{"emptyLinePlaceholder":138},[100,100972,100973,100975,100978],{"class":102,"line":142},[100,100974,1078],{"class":1077},[100,100976,100977],{"class":1081}," add_one",[100,100979,1085],{"class":118},[100,100981,100982,100985,100987,100989,100991],{"class":102,"line":152},[100,100983,100984],{"class":106},"    total ",[100,100986,111],{"class":110},[100,100988,2555],{"class":106},[100,100990,6547],{"class":110},[100,100992,1093],{"class":122},[100,100994,100995,100997],{"class":102,"line":164},[100,100996,2552],{"class":145},[100,100998,100999],{"class":106}," total\n",[100,101001,101002],{"class":102,"line":185},[100,101003,139],{"emptyLinePlaceholder":138},[100,101005,101006,101009],{"class":102,"line":197},[100,101007,101008],{"class":178},"add_one",[100,101010,4734],{"class":118},[14,101012,28421],{},[91,101014,101016],{"className":93,"code":101015,"language":95,"meta":96,"style":96},"UnboundLocalError: local variable 'total' referenced before assignment\n",[17,101017,101018],{"__ignoreMap":96},[100,101019,101020,101022,101024,101027,101029,101031,101033],{"class":102,"line":103},[100,101021,16651],{"class":191},[100,101023,89],{"class":118},[100,101025,101026],{"class":106}," local variable ",[100,101028,1280],{"class":205},[100,101030,2520],{"class":209},[100,101032,1280],{"class":205},[100,101034,101035],{"class":106}," referenced before assignment\n",[14,101037,47370],{},[40,101039,101040,101048,101057],{},[43,101041,101042,101045,101046],{},[17,101043,101044],{},"total = total + 1"," assigns to ",[17,101047,2520],{},[43,101049,101050,101051,101053,101054],{},"So Python treats ",[17,101052,2520],{}," as local in ",[17,101055,101056],{},"add_one()",[43,101058,101059,101060,101062],{},"But the right side tries to read ",[17,101061,2520],{}," before the local variable has a value",[14,101064,101065],{},"Another common case is assigning in only one branch:",[91,101067,101069],{"className":93,"code":101068,"language":95,"meta":96,"style":96},"def check_score(score):\n    if score >= 50:\n        result = \"pass\"\n    print(result)\n\ncheck_score(20)\n",[17,101070,101071,101084,101097,101110,101120,101124],{"__ignoreMap":96},[100,101072,101073,101075,101078,101080,101082],{"class":102,"line":103},[100,101074,1078],{"class":1077},[100,101076,101077],{"class":1081}," check_score",[100,101079,170],{"class":118},[100,101081,54425],{"class":2519},[100,101083,522],{"class":118},[100,101085,101086,101088,101090,101092,101095],{"class":102,"line":135},[100,101087,2777],{"class":145},[100,101089,2260],{"class":106},[100,101091,1679],{"class":110},[100,101093,101094],{"class":122}," 50",[100,101096,149],{"class":118},[100,101098,101099,101102,101104,101106,101108],{"class":102,"line":142},[100,101100,101101],{"class":106},"        result ",[100,101103,111],{"class":110},[100,101105,1708],{"class":205},[100,101107,29209],{"class":209},[100,101109,1714],{"class":205},[100,101111,101112,101114,101116,101118],{"class":102,"line":152},[100,101113,200],{"class":114},[100,101115,170],{"class":118},[100,101117,11580],{"class":178},[100,101119,215],{"class":118},[100,101121,101122],{"class":102,"line":164},[100,101123,139],{"emptyLinePlaceholder":138},[100,101125,101126,101129,101131,101133],{"class":102,"line":185},[100,101127,101128],{"class":178},"check_score",[100,101130,170],{"class":118},[100,101132,6152],{"class":122},[100,101134,215],{"class":118},[14,101136,11734,101137,101139,101140,5870,101143,101145],{},[17,101138,54425],{}," is less than ",[17,101141,101142],{},"50",[17,101144,11580],{}," never gets assigned.",[77,101147,101149],{"id":101148},"fix-1-pass-the-value-into-the-function","Fix 1: Pass the value into the function",[14,101151,101152],{},"Use a parameter instead of relying on an outer variable.",[14,101154,101155],{},"This is usually the clearest fix for beginners.",[91,101157,101159],{"className":93,"code":101158,"language":95,"meta":96,"style":96},"def show_count(count):\n    print(count)\n\nshow_count(10)\n",[17,101160,101161,101173,101183,101187],{"__ignoreMap":96},[100,101162,101163,101165,101167,101169,101171],{"class":102,"line":103},[100,101164,1078],{"class":1077},[100,101166,55138],{"class":1081},[100,101168,170],{"class":118},[100,101170,51844],{"class":2519},[100,101172,522],{"class":118},[100,101174,101175,101177,101179,101181],{"class":102,"line":135},[100,101176,200],{"class":114},[100,101178,170],{"class":118},[100,101180,51844],{"class":178},[100,101182,215],{"class":118},[100,101184,101185],{"class":102,"line":142},[100,101186,139],{"emptyLinePlaceholder":138},[100,101188,101189,101191,101193,101195],{"class":102,"line":152},[100,101190,55167],{"class":178},[100,101192,170],{"class":118},[100,101194,356],{"class":122},[100,101196,215],{"class":118},[14,101198,218],{},[91,101200,101201],{"className":93,"code":3410,"language":95,"meta":96,"style":96},[17,101202,101203],{"__ignoreMap":96},[100,101204,101205],{"class":102,"line":103},[100,101206,3410],{"class":122},[14,101208,101209],{},"This works well because:",[40,101211,101212,101215,101218],{},[43,101213,101214],{},"the function gets everything it needs directly",[43,101216,101217],{},"it is easier to test",[43,101219,101220],{},"it avoids hidden dependence on outer variables",[14,101222,101223,101224,290],{},"If you are new to this idea, see ",[295,101225,88527],{"href":88526},[77,101227,101229],{"id":101228},"fix-2-return-the-new-value","Fix 2: Return the new value",[14,101231,101232],{},"Instead of changing an outer variable inside the function, let the function return a value.",[91,101234,101236],{"className":93,"code":101235,"language":95,"meta":96,"style":96},"def add_one(total):\n    return total + 1\n\ntotal = 0\ntotal = add_one(total)\n\nprint(total)\n",[17,101237,101238,101250,101260,101264,101272,101286,101290],{"__ignoreMap":96},[100,101239,101240,101242,101244,101246,101248],{"class":102,"line":103},[100,101241,1078],{"class":1077},[100,101243,100977],{"class":1081},[100,101245,170],{"class":118},[100,101247,2520],{"class":2519},[100,101249,522],{"class":118},[100,101251,101252,101254,101256,101258],{"class":102,"line":135},[100,101253,2552],{"class":145},[100,101255,2555],{"class":106},[100,101257,6547],{"class":110},[100,101259,1093],{"class":122},[100,101261,101262],{"class":102,"line":142},[100,101263,139],{"emptyLinePlaceholder":138},[100,101265,101266,101268,101270],{"class":102,"line":152},[100,101267,53376],{"class":106},[100,101269,111],{"class":110},[100,101271,2069],{"class":122},[100,101273,101274,101276,101278,101280,101282,101284],{"class":102,"line":164},[100,101275,53376],{"class":106},[100,101277,111],{"class":110},[100,101279,100977],{"class":178},[100,101281,170],{"class":118},[100,101283,2520],{"class":178},[100,101285,215],{"class":118},[100,101287,101288],{"class":102,"line":185},[100,101289,139],{"emptyLinePlaceholder":138},[100,101291,101292,101294,101296,101298],{"class":102,"line":197},[100,101293,372],{"class":114},[100,101295,170],{"class":118},[100,101297,2520],{"class":178},[100,101299,215],{"class":118},[14,101301,218],{},[91,101303,101304],{"className":93,"code":228,"language":95,"meta":96,"style":96},[17,101305,101306],{"__ignoreMap":96},[100,101307,101308],{"class":102,"line":103},[100,101309,228],{"class":122},[14,101311,101312],{},"This is often the best fix because:",[40,101314,101315,101318,101321],{},[43,101316,101317],{},"the function is predictable",[43,101319,101320],{},"it avoids hidden changes",[43,101322,101323],{},"the flow of data is easier to understand",[14,101325,48145,101326,290],{},[295,101327,11292],{"href":11291},[77,101329,101331],{"id":101330},"fix-3-use-global-only-when-you-really-need-it","Fix 3: Use global only when you really need it",[14,101333,101334,101336],{},[17,101335,100729],{}," tells Python that you want to use the module-level variable.",[91,101338,101340],{"className":93,"code":101339,"language":95,"meta":96,"style":96},"total = 0\n\ndef add_one():\n    global total\n    total = total + 1\n\nadd_one()\nprint(total)\n",[17,101341,101342,101350,101354,101362,101369,101381,101385,101391],{"__ignoreMap":96},[100,101343,101344,101346,101348],{"class":102,"line":103},[100,101345,53376],{"class":106},[100,101347,111],{"class":110},[100,101349,2069],{"class":122},[100,101351,101352],{"class":102,"line":135},[100,101353,139],{"emptyLinePlaceholder":138},[100,101355,101356,101358,101360],{"class":102,"line":142},[100,101357,1078],{"class":1077},[100,101359,100977],{"class":1081},[100,101361,1085],{"class":118},[100,101363,101364,101367],{"class":102,"line":152},[100,101365,101366],{"class":1077},"    global",[100,101368,100999],{"class":106},[100,101370,101371,101373,101375,101377,101379],{"class":102,"line":164},[100,101372,100984],{"class":106},[100,101374,111],{"class":110},[100,101376,2555],{"class":106},[100,101378,6547],{"class":110},[100,101380,1093],{"class":122},[100,101382,101383],{"class":102,"line":185},[100,101384,139],{"emptyLinePlaceholder":138},[100,101386,101387,101389],{"class":102,"line":197},[100,101388,101008],{"class":178},[100,101390,4734],{"class":118},[100,101392,101393,101395,101397,101399],{"class":102,"line":771},[100,101394,372],{"class":114},[100,101396,170],{"class":118},[100,101398,2520],{"class":178},[100,101400,215],{"class":118},[14,101402,218],{},[91,101404,101405],{"className":93,"code":228,"language":95,"meta":96,"style":96},[17,101406,101407],{"__ignoreMap":96},[100,101408,101409],{"class":102,"line":103},[100,101410,228],{"class":122},[14,101412,101413,101414,101416],{},"This fixes the error because ",[17,101415,2520],{}," is no longer treated as a local variable inside the function.",[14,101418,56449,101419,89],{},[17,101420,100729],{},[40,101422,101423,101426,101429],{},[43,101424,101425],{},"it can make code harder to follow",[43,101427,101428],{},"many functions changing the same global variable can cause bugs",[43,101430,101431],{},"passing values and returning results is often cleaner",[14,101433,241,101434,101436],{},[17,101435,100729],{}," only if you really want to change a module-level variable.",[77,101438,101440],{"id":101439},"fix-4-use-nonlocal-in-nested-functions","Fix 4: Use nonlocal in nested functions",[14,101442,101443,101445,101446,101449],{},[17,101444,100732],{}," is for variables in an ",[22,101447,101448],{},"enclosing function",", not the global scope.",[14,101451,101452],{},"Use it when an inner function needs to update a variable from the outer function.",[91,101454,101456],{"className":93,"code":101455,"language":95,"meta":96,"style":96},"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",[17,101457,101458,101467,101475,101479,101488,101496,101505,101515,101519,101526,101532,101536],{"__ignoreMap":96},[100,101459,101460,101462,101465],{"class":102,"line":103},[100,101461,1078],{"class":1077},[100,101463,101464],{"class":1081}," outer",[100,101466,1085],{"class":118},[100,101468,101469,101471,101473],{"class":102,"line":135},[100,101470,41602],{"class":106},[100,101472,111],{"class":110},[100,101474,2069],{"class":122},[100,101476,101477],{"class":102,"line":142},[100,101478,139],{"emptyLinePlaceholder":138},[100,101480,101481,101483,101486],{"class":102,"line":152},[100,101482,89014],{"class":1077},[100,101484,101485],{"class":1081}," inner",[100,101487,1085],{"class":118},[100,101489,101490,101493],{"class":102,"line":164},[100,101491,101492],{"class":1077},"        nonlocal",[100,101494,101495],{"class":106}," count\n",[100,101497,101498,101501,101503],{"class":102,"line":185},[100,101499,101500],{"class":106},"        count ",[100,101502,41605],{"class":110},[100,101504,1093],{"class":122},[100,101506,101507,101509,101511,101513],{"class":102,"line":197},[100,101508,167],{"class":114},[100,101510,170],{"class":118},[100,101512,51844],{"class":178},[100,101514,215],{"class":118},[100,101516,101517],{"class":102,"line":771},[100,101518,139],{"emptyLinePlaceholder":138},[100,101520,101521,101524],{"class":102,"line":787},[100,101522,101523],{"class":178},"    inner",[100,101525,4734],{"class":118},[100,101527,101528,101530],{"class":102,"line":5816},[100,101529,101523],{"class":178},[100,101531,4734],{"class":118},[100,101533,101534],{"class":102,"line":5833},[100,101535,139],{"emptyLinePlaceholder":138},[100,101537,101538,101541],{"class":102,"line":5848},[100,101539,101540],{"class":178},"outer",[100,101542,4734],{"class":118},[14,101544,218],{},[91,101546,101547],{"className":93,"code":638,"language":95,"meta":96,"style":96},[17,101548,101549,101553],{"__ignoreMap":96},[100,101550,101551],{"class":102,"line":103},[100,101552,228],{"class":122},[100,101554,101555],{"class":102,"line":135},[100,101556,233],{"class":122},[14,101558,29471,101559,101561,101562,101564,101565,101568,101569,290],{},[17,101560,100732],{},", Python would treat ",[17,101563,51844],{}," inside ",[17,101566,101567],{},"inner()"," as a new local variable and raise ",[17,101570,16651],{},[77,101572,101574],{"id":101573},"fix-5-make-sure-every-code-path-assigns-a-value","Fix 5: Make sure every code path assigns a value",[14,101576,101577,101578,101580,101581,25128],{},"Sometimes the variable is only assigned inside an ",[17,101579,2736],{}," block or ",[17,101582,146],{},[14,101584,101585],{},"Set a default value before the conditional logic.",[675,101587,89973,101589],{"id":101588},"example-with-if",[17,101590,2736],{},[91,101592,101594],{"className":93,"code":101593,"language":95,"meta":96,"style":96},"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",[17,101595,101596,101608,101621,101625,101637,101649,101653,101663,101667,101677],{"__ignoreMap":96},[100,101597,101598,101600,101602,101604,101606],{"class":102,"line":103},[100,101599,1078],{"class":1077},[100,101601,101077],{"class":1081},[100,101603,170],{"class":118},[100,101605,54425],{"class":2519},[100,101607,522],{"class":118},[100,101609,101610,101612,101614,101616,101619],{"class":102,"line":135},[100,101611,59692],{"class":106},[100,101613,111],{"class":110},[100,101615,1708],{"class":205},[100,101617,101618],{"class":209},"fail",[100,101620,1714],{"class":205},[100,101622,101623],{"class":102,"line":142},[100,101624,139],{"emptyLinePlaceholder":138},[100,101626,101627,101629,101631,101633,101635],{"class":102,"line":152},[100,101628,2777],{"class":145},[100,101630,2260],{"class":106},[100,101632,1679],{"class":110},[100,101634,101094],{"class":122},[100,101636,149],{"class":118},[100,101638,101639,101641,101643,101645,101647],{"class":102,"line":164},[100,101640,101101],{"class":106},[100,101642,111],{"class":110},[100,101644,1708],{"class":205},[100,101646,29209],{"class":209},[100,101648,1714],{"class":205},[100,101650,101651],{"class":102,"line":185},[100,101652,139],{"emptyLinePlaceholder":138},[100,101654,101655,101657,101659,101661],{"class":102,"line":197},[100,101656,200],{"class":114},[100,101658,170],{"class":118},[100,101660,11580],{"class":178},[100,101662,215],{"class":118},[100,101664,101665],{"class":102,"line":771},[100,101666,139],{"emptyLinePlaceholder":138},[100,101668,101669,101671,101673,101675],{"class":102,"line":787},[100,101670,101128],{"class":178},[100,101672,170],{"class":118},[100,101674,6152],{"class":122},[100,101676,215],{"class":118},[100,101678,101679,101681,101683,101686],{"class":102,"line":5816},[100,101680,101128],{"class":178},[100,101682,170],{"class":118},[100,101684,101685],{"class":122},"80",[100,101687,215],{"class":118},[14,101689,218],{},[91,101691,101693],{"className":93,"code":101692,"language":95,"meta":96,"style":96},"fail\npass\n",[17,101694,101695,101700],{"__ignoreMap":96},[100,101696,101697],{"class":102,"line":103},[100,101698,101699],{"class":106},"fail\n",[100,101701,101702],{"class":102,"line":135},[100,101703,101704],{"class":145},"pass\n",[675,101706,89973,101708],{"id":101707},"example-with-try",[17,101709,146],{},[91,101711,101713],{"className":93,"code":101712,"language":95,"meta":96,"style":96},"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",[17,101714,101715,101728,101736,101740,101746,101761,101769,101784,101788,101795,101799,101818],{"__ignoreMap":96},[100,101716,101717,101719,101722,101724,101726],{"class":102,"line":103},[100,101718,1078],{"class":1077},[100,101720,101721],{"class":1081}," read_number",[100,101723,170],{"class":118},[100,101725,4468],{"class":2519},[100,101727,522],{"class":118},[100,101729,101730,101732,101734],{"class":102,"line":135},[100,101731,98700],{"class":106},[100,101733,111],{"class":110},[100,101735,4904],{"class":158},[100,101737,101738],{"class":102,"line":142},[100,101739,139],{"emptyLinePlaceholder":138},[100,101741,101742,101744],{"class":102,"line":152},[100,101743,730],{"class":145},[100,101745,149],{"class":118},[100,101747,101748,101751,101753,101755,101757,101759],{"class":102,"line":164},[100,101749,101750],{"class":106},"        number ",[100,101752,111],{"class":110},[100,101754,3389],{"class":191},[100,101756,170],{"class":118},[100,101758,4468],{"class":178},[100,101760,215],{"class":118},[100,101762,101763,101765,101767],{"class":102,"line":185},[100,101764,764],{"class":145},[100,101766,2794],{"class":191},[100,101768,149],{"class":118},[100,101770,101771,101773,101775,101777,101780,101782],{"class":102,"line":197},[100,101772,167],{"class":114},[100,101774,170],{"class":118},[100,101776,206],{"class":205},[100,101778,101779],{"class":209},"Not a valid number",[100,101781,206],{"class":205},[100,101783,215],{"class":118},[100,101785,101786],{"class":102,"line":771},[100,101787,139],{"emptyLinePlaceholder":138},[100,101789,101790,101792],{"class":102,"line":787},[100,101791,2552],{"class":145},[100,101793,101794],{"class":106}," number\n",[100,101796,101797],{"class":102,"line":5816},[100,101798,139],{"emptyLinePlaceholder":138},[100,101800,101801,101803,101805,101808,101810,101812,101814,101816],{"class":102,"line":5833},[100,101802,372],{"class":114},[100,101804,170],{"class":118},[100,101806,101807],{"class":178},"read_number",[100,101809,170],{"class":118},[100,101811,206],{"class":205},[100,101813,27135],{"class":209},[100,101815,206],{"class":205},[100,101817,182],{"class":118},[100,101819,101820,101822,101824,101826,101828,101830,101832,101834],{"class":102,"line":5848},[100,101821,372],{"class":114},[100,101823,170],{"class":118},[100,101825,101807],{"class":178},[100,101827,170],{"class":118},[100,101829,206],{"class":205},[100,101831,83226],{"class":209},[100,101833,206],{"class":205},[100,101835,182],{"class":118},[14,101837,218],{},[91,101839,101841],{"className":93,"code":101840,"language":95,"meta":96,"style":96},"25\nNot a valid number\nNone\n",[17,101842,101843,101847,101852],{"__ignoreMap":96},[100,101844,101845],{"class":102,"line":103},[100,101846,77356],{"class":122},[100,101848,101849],{"class":102,"line":135},[100,101850,101851],{"class":106},"Not a valid number\n",[100,101853,101854],{"class":102,"line":142},[100,101855,924],{"class":158},[14,101857,101858,101859,101861,101862,290],{},"If you use ",[17,101860,146],{}," blocks often, see ",[295,101863,12762],{"href":12761},[77,101865,89372],{"id":89371},[14,101867,14373],{},[3282,101869,101870,101873,101876,101884,101887],{},[43,101871,101872],{},"Find the line named in the traceback.",[43,101874,101875],{},"Check whether the variable is assigned anywhere in that function.",[43,101877,101878,101879,82051,101881,290],{},"Look for code like ",[17,101880,15642],{},[17,101882,101883],{},"variable = ...",[43,101885,101886],{},"Check if assignment happens only in some branches.",[43,101888,101889,101890],{},"Decide whether the variable should be:\n",[40,101891,101892,101895,101897,101900],{},[43,101893,101894],{},"local",[43,101896,100729],{},[43,101898,101899],{},"passed in as a parameter",[43,101901,101902],{},"returned from the function",[14,101904,5295],{},[91,101906,101907],{"className":10352,"code":31396,"language":10354,"meta":96,"style":96},[17,101908,101909],{"__ignoreMap":96},[100,101910,101911,101913],{"class":102,"line":103},[100,101912,95],{"class":10361},[100,101914,30457],{"class":209},[91,101916,101918],{"className":93,"code":101917,"language":95,"meta":96,"style":96},"print(variable_name)\nprint(locals())\nprint(globals().get(\"variable_name\"))\n",[17,101919,101920,101930,101940],{"__ignoreMap":96},[100,101921,101922,101924,101926,101928],{"class":102,"line":103},[100,101923,372],{"class":114},[100,101925,170],{"class":118},[100,101927,53946],{"class":178},[100,101929,215],{"class":118},[100,101931,101932,101934,101936,101938],{"class":102,"line":135},[100,101933,372],{"class":114},[100,101935,170],{"class":118},[100,101937,53957],{"class":114},[100,101939,3370],{"class":118},[100,101941,101942,101944,101946,101948,101950,101952,101954,101956,101958,101960],{"class":102,"line":142},[100,101943,372],{"class":114},[100,101945,170],{"class":118},[100,101947,53968],{"class":114},[100,101949,8886],{"class":118},[100,101951,12412],{"class":178},[100,101953,170],{"class":118},[100,101955,206],{"class":205},[100,101957,53946],{"class":209},[100,101959,206],{"class":205},[100,101961,182],{"class":118},[14,101963,101964],{},"What these help with:",[40,101966,101967,101972,101977],{},[43,101968,101969,101971],{},[17,101970,55276],{}," shows the current value if it exists",[43,101973,101974,101976],{},[17,101975,56430],{}," shows local variables inside the function",[43,101978,101979,101982],{},[17,101980,101981],{},"print(globals().get(\"variable_name\"))"," checks the global value safely",[14,101984,101985,101986,290],{},"For a broader beginner walkthrough, see the ",[295,101987,7077],{"href":3852},[77,101989,7081],{"id":7080},[14,101991,101992],{},"This error is often confused with other errors.",[675,101994,5616],{"id":55071},[14,101996,101997,102001],{},[295,101998,101999],{"href":54194},[17,102000,16728],{}," happens when Python cannot find the variable at all.",[675,102003,102005],{"id":102004},"unboundlocalerror-vs-nameerror","UnboundLocalError vs NameError",[14,102007,102008,102009,102011,102012,102014],{},"If the message is confusing, it helps to compare the two directly. ",[17,102010,16651],{}," means the name is considered local but has no value yet. ",[17,102013,5616],{}," means the name does not exist in the current scope.",[675,102016,102018],{"id":102017},"other-errors-after-a-bad-quick-fix","Other errors after a bad quick fix",[14,102020,102021],{},"If you change your code in the wrong way, you might start seeing:",[40,102023,102024,102028],{},[43,102025,102026],{},[17,102027,3738],{},[43,102029,102030],{},[17,102031,3538],{},[14,102033,102034,102035,102037,102038,102040],{},"For example, if a variable becomes ",[17,102036,930],{}," or a different type than expected, a new error may appear even though ",[17,102039,16651],{}," is gone.",[77,102042,1344],{"id":1343},[14,102044,102045],{},"Here are the most common reasons this error appears:",[40,102047,102048,102051,102056,102061,102066],{},[43,102049,102050],{},"Reading a global variable inside a function and assigning to it later",[43,102052,102053,102054,30766],{},"Assigning a variable only inside an ",[17,102055,2736],{},[43,102057,102058,102059,30766],{},"Assigning a variable only inside a ",[17,102060,146],{},[43,102062,102063,102064],{},"Updating an outer variable inside a nested function without ",[17,102065,100732],{},[43,102067,102068],{},"Using the same variable name for both outer and local values",[77,102070,1514],{"id":1513},[675,102072,102074],{"id":102073},"is-unboundlocalerror-the-same-as-nameerror","Is UnboundLocalError the same as NameError?",[14,102076,14506,102077,102079,102080,102082],{},[17,102078,16651],{}," means Python treats the name as local in the function, but you used it before assigning a value. ",[17,102081,5616],{}," means the name was not found at all.",[675,102084,102086],{"id":102085},"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,102088,102089],{},"Because assigning to that name inside the function makes Python treat it as a local variable for the whole function body.",[675,102091,102093],{"id":102092},"should-i-use-global-to-fix-it","Should I use global to fix it?",[14,102095,102096],{},"Only if you really want to change a module-level variable. In many cases, passing arguments and returning values is a better fix.",[675,102098,102100],{"id":102099},"when-should-i-use-nonlocal","When should I use nonlocal?",[14,102102,241,102103,102105],{},[17,102104,100732],{}," only inside nested functions when you need to change a variable from the enclosing function.",[77,102107,1554],{"id":1553},[40,102109,102110,102114,102118,102122,102126,102130,102134],{},[43,102111,102112],{},[295,102113,16728],{"href":54194},[43,102115,102116],{},[295,102117,29749],{"href":29189},[43,102119,102120],{},[295,102121,89610],{"href":88526},[43,102123,102124],{},[295,102125,12835],{"href":11291},[43,102127,102128],{},[295,102129,1587],{"href":1749},[43,102131,102132],{},[295,102133,42247],{"href":12761},[43,102135,102136],{},[295,102137,42267],{"href":3852},[1589,102139,102140],{},"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":96,"searchDepth":135,"depth":135,"links":102142},[102143,102144,102145,102146,102147,102148,102149,102150,102151,102157,102158,102163,102164,102170],{"id":79,"depth":135,"text":80},{"id":5880,"depth":135,"text":5881},{"id":100835,"depth":135,"text":100836},{"id":11030,"depth":135,"text":11031},{"id":101148,"depth":135,"text":101149},{"id":101228,"depth":135,"text":101229},{"id":101330,"depth":135,"text":101331},{"id":101439,"depth":135,"text":101440},{"id":101573,"depth":135,"text":101574,"children":102152},[102153,102155],{"id":101588,"depth":142,"text":102154},"Example with if",{"id":101707,"depth":142,"text":102156},"Example with try",{"id":89371,"depth":135,"text":89372},{"id":7080,"depth":135,"text":7081,"children":102159},[102160,102161,102162],{"id":55071,"depth":142,"text":5616},{"id":102004,"depth":142,"text":102005},{"id":102017,"depth":142,"text":102018},{"id":1343,"depth":135,"text":1344},{"id":1513,"depth":135,"text":1514,"children":102165},[102166,102167,102168,102169],{"id":102073,"depth":142,"text":102074},{"id":102085,"depth":142,"text":102086},{"id":102092,"depth":142,"text":102093},{"id":102099,"depth":142,"text":102100},{"id":1553,"depth":135,"text":1554},"Master unboundlocalerror local variable referenced before assignment fix in our comprehensive Python beginner guide.",{},{"title":100663,"description":102171},"errors\u002Funboundlocalerror-local-variable-referenced-before-assignment-fix","LnSYT9LKTR6p4vaE3JSDCpLwG1H_I9a97mpahtHRYRk",{"id":102177,"title":102178,"body":102179,"description":103638,"extension":1623,"meta":103639,"navigation":138,"path":103640,"seo":103641,"stem":103642,"__hash__":103643},"content\u002Ferrors\u002Funboundlocalerror-vs-nameerror-explained.md","UnboundLocalError vs NameError Explained",{"type":7,"value":102180,"toc":103603},[102181,102184,102191,102197,102200,102212,102215,102217,102269,102275,102280,102284,102309,102313,102318,102321,102323,102337,102342,102346,102351,102354,102362,102365,102372,102376,102388,102392,102394,102409,102414,102417,102442,102448,102451,102462,102469,102473,102476,102528,102530,102551,102553,102565,102567,102581,102587,102593,102597,102600,102620,102625,102629,102632,102651,102655,102658,102685,102687,102713,102717,102719,102742,102744,102767,102771,102773,102795,102797,102829,102833,102837,102840,102861,102864,102866,102925,102928,103009,103011,103025,103028,103032,103093,103095,103103,103111,103115,103156,103158,103166,103169,103173,103180,103201,103205,103270,103272,103280,103287,103291,103372,103374,103382,103385,103389,103392,103412,103415,103492,103497,103499,103506,103525,103527,103529,103537,103541,103544,103548,103551,103555,103560,103564,103572,103574,103600],[10,102182,102178],{"id":102183},"unboundlocalerror-vs-nameerror-explained",[14,102185,102186,3266,102188,102190],{},[17,102187,16651],{},[17,102189,5616],{}," can look very similar at first.",[14,102192,102193,102194,102196],{},"Both happen when Python has a problem with a variable name. But they do ",[22,102195,1059],{}," mean the same thing.",[14,102198,102199],{},"This page helps you quickly tell the difference:",[40,102201,102202,102207],{},[43,102203,102204,102206],{},[17,102205,5616],{}," means Python cannot find the name at all",[43,102208,102209,102211],{},[17,102210,16651],{}," means Python treats the name as a local variable, but you used it before it got a value",[14,102213,102214],{},"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.",[77,102216,80],{"id":79},[91,102218,102220],{"className":93,"code":102219,"language":95,"meta":96,"style":96},"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",[17,102221,102222,102230,102234,102243,102248,102258,102262],{"__ignoreMap":96},[100,102223,102224,102226,102228],{"class":102,"line":103},[100,102225,54291],{"class":106},[100,102227,111],{"class":110},[100,102229,2181],{"class":122},[100,102231,102232],{"class":102,"line":135},[100,102233,139],{"emptyLinePlaceholder":138},[100,102235,102236,102238,102241],{"class":102,"line":142},[100,102237,1078],{"class":1077},[100,102239,102240],{"class":1081}," show_value",[100,102242,1085],{"class":118},[100,102244,102245],{"class":102,"line":152},[100,102246,102247],{"class":414},"    # If you only want to read x, do not assign to x in this function\n",[100,102249,102250,102252,102254,102256],{"class":102,"line":164},[100,102251,200],{"class":114},[100,102253,170],{"class":118},[100,102255,39024],{"class":178},[100,102257,215],{"class":118},[100,102259,102260],{"class":102,"line":185},[100,102261,139],{"emptyLinePlaceholder":138},[100,102263,102264,102267],{"class":102,"line":197},[100,102265,102266],{"class":178},"show_value",[100,102268,4734],{"class":118},[14,102270,102271,102272,102274],{},"If Python says ",[17,102273,5616],{},", the variable name usually does not exist in the current scope.",[14,102276,102271,102277,102279],{},[17,102278,16651],{},", Python thinks the name is local in the function, but you used it before assigning a value.",[77,102281,102283],{"id":102282},"what-this-page-helps-you-fix","What this page helps you fix",[40,102285,102286,102294,102303,102306],{},[43,102287,102288,102289,3266,102291,102293],{},"Understand why ",[17,102290,16651],{},[17,102292,5616],{}," look similar",[43,102295,102296,102297,3266,102300],{},"Learn the key difference between ",[22,102298,102299],{},"\"not defined\"",[22,102301,102302],{},"\"local before assignment\"",[43,102304,102305],{},"Use simple rules to identify the right fix",[43,102307,102308],{},"See short examples for both errors",[77,102310,102312],{"id":102311},"what-nameerror-means","What NameError means",[14,102314,102315,102317],{},[17,102316,5616],{}," happens when Python cannot find a variable or function name.",[14,102319,102320],{},"In simple terms, Python looks for the name and does not find it where it expects to.",[14,102322,14127],{},[40,102324,102325,102328,102331,102334],{},[43,102326,102327],{},"A typo in a variable name",[43,102329,102330],{},"Using a variable before creating it",[43,102332,102333],{},"Calling a function that was never defined",[43,102335,102336],{},"Forgetting to import something before using it",[14,102338,102339,102340,290],{},"If you need a full guide, see ",[295,102341,54200],{"href":56646},[77,102343,102345],{"id":102344},"what-unboundlocalerror-means","What UnboundLocalError means",[14,102347,102348,102350],{},[17,102349,16651],{}," is a more specific scope-related error.",[14,102352,102353],{},"It usually happens inside a function.",[14,102355,102356,102357,102359,102360,290],{},"Python decides a name is ",[22,102358,101894],{}," if you assign to it anywhere in that function. If you try to read that name before the assignment line runs, Python raises ",[17,102361,16651],{},[14,102363,102364],{},"This often surprises beginners when a global variable has the same name.",[14,102366,102367,102368,3266,102370,290],{},"If you want more background first, see ",[295,102369,53345],{"href":54746},[295,102371,29749],{"href":29189},[77,102373,102375],{"id":102374},"main-difference-in-one-sentence","Main difference in one sentence",[40,102377,102378,102383],{},[43,102379,102380,102382],{},[17,102381,5616],{},": Python cannot find the name",[43,102384,102385,102387],{},[17,102386,16651],{},": Python found the name as a local variable, but it has no value yet",[77,102389,102391],{"id":102390},"example-that-causes-nameerror","Example that causes NameError",[14,102393,1891],{},[91,102395,102397],{"className":93,"code":102396,"language":95,"meta":96,"style":96},"print(user_name)\n",[17,102398,102399],{"__ignoreMap":96},[100,102400,102401,102403,102405,102407],{"class":102,"line":103},[100,102402,372],{"class":114},[100,102404,170],{"class":118},[100,102406,53589],{"class":178},[100,102408,215],{"class":118},[14,102410,7194,102411,102413],{},[17,102412,53589],{}," was never created.",[14,102415,102416],{},"You would get an error like this:",[91,102418,102420],{"className":93,"code":102419,"language":95,"meta":96,"style":96},"NameError: name 'user_name' is not defined\n",[17,102421,102422],{"__ignoreMap":96},[100,102423,102424,102426,102428,102430,102432,102434,102436,102438,102440],{"class":102,"line":103},[100,102425,5616],{"class":191},[100,102427,89],{"class":118},[100,102429,4915],{"class":106},[100,102431,1280],{"class":205},[100,102433,53589],{"class":209},[100,102435,1280],{"class":205},[100,102437,16702],{"class":110},[100,102439,4921],{"class":110},[100,102441,16707],{"class":106},[14,102443,102444,102445,102447],{},"Python checks for ",[17,102446,53589],{}," and cannot find it.",[14,102449,102450],{},"Another common example is calling a function that does not exist:",[91,102452,102454],{"className":93,"code":102453,"language":95,"meta":96,"style":96},"say_hello()\n",[17,102455,102456],{"__ignoreMap":96},[100,102457,102458,102460],{"class":102,"line":103},[100,102459,52978],{"class":178},[100,102461,4734],{"class":118},[14,102463,11734,102464,102466,102467,290],{},[17,102465,52978],{}," was never defined or imported, Python raises ",[17,102468,5616],{},[77,102470,102472],{"id":102471},"example-that-causes-unboundlocalerror","Example that causes UnboundLocalError",[14,102474,102475],{},"This example uses a global variable:",[91,102477,102478],{"className":93,"code":55117,"language":95,"meta":96,"style":96},[17,102479,102480,102488,102492,102500,102510,102518,102522],{"__ignoreMap":96},[100,102481,102482,102484,102486],{"class":102,"line":103},[100,102483,2176],{"class":106},[100,102485,111],{"class":110},[100,102487,2181],{"class":122},[100,102489,102490],{"class":102,"line":135},[100,102491,139],{"emptyLinePlaceholder":138},[100,102493,102494,102496,102498],{"class":102,"line":142},[100,102495,1078],{"class":1077},[100,102497,55138],{"class":1081},[100,102499,1085],{"class":118},[100,102501,102502,102504,102506,102508],{"class":102,"line":152},[100,102503,200],{"class":114},[100,102505,170],{"class":118},[100,102507,51844],{"class":178},[100,102509,215],{"class":118},[100,102511,102512,102514,102516],{"class":102,"line":164},[100,102513,41602],{"class":106},[100,102515,111],{"class":110},[100,102517,1669],{"class":122},[100,102519,102520],{"class":102,"line":185},[100,102521,139],{"emptyLinePlaceholder":138},[100,102523,102524,102526],{"class":102,"line":197},[100,102525,55167],{"class":178},[100,102527,4734],{"class":118},[14,102529,7710],{},[91,102531,102533],{"className":93,"code":102532,"language":95,"meta":96,"style":96},"UnboundLocalError: local variable 'count' referenced before assignment\n",[17,102534,102535],{"__ignoreMap":96},[100,102536,102537,102539,102541,102543,102545,102547,102549],{"class":102,"line":103},[100,102538,16651],{"class":191},[100,102540,89],{"class":118},[100,102542,101026],{"class":106},[100,102544,1280],{"class":205},[100,102546,51844],{"class":209},[100,102548,1280],{"class":205},[100,102550,101035],{"class":106},[14,102552,32043],{},[14,102554,102555,102556,102558,102559,25407,102561,102564],{},"Because Python sees ",[17,102557,100820],{}," inside the function. That makes ",[17,102560,51844],{},[22,102562,102563],{},"local variable"," for the whole function.",[14,102566,91839],{},[91,102568,102569],{"className":93,"code":55083,"language":95,"meta":96,"style":96},[17,102570,102571],{"__ignoreMap":96},[100,102572,102573,102575,102577,102579],{"class":102,"line":103},[100,102574,372],{"class":114},[100,102576,170],{"class":118},[100,102578,51844],{"class":178},[100,102580,215],{"class":118},[14,102582,102583,102584,102586],{},"tries to read the local ",[17,102585,51844],{}," before it has been assigned a value.",[14,102588,102589,102590,102592],{},"Even though there is a global ",[17,102591,51844],{},", Python does not use it here.",[675,102594,102596],{"id":102595},"a-simple-way-to-think-about-it","A simple way to think about it",[14,102598,102599],{},"Inside a function:",[40,102601,102602,102609,102615],{},[43,102603,102604,102605,102608],{},"If you ",[22,102606,102607],{},"only read"," a variable, Python may use an outer value",[43,102610,102604,102611,102614],{},[22,102612,102613],{},"assign"," to that variable in the function, Python treats it as local",[43,102616,102617,102618],{},"If you read it before assigning it, you get ",[17,102619,16651],{},[14,102621,102622,102623,290],{},"For a full fix guide, see ",[295,102624,53909],{"href":55181},[77,102626,102628],{"id":102627},"how-to-fix-nameerror","How to fix NameError",[14,102630,102631],{},"Try these checks:",[40,102633,102634,102637,102640,102643,102646],{},[43,102635,102636],{},"Check for spelling mistakes",[43,102638,102639],{},"Make sure the variable is assigned before you use it",[43,102641,102642],{},"Make sure the function or module was defined or imported",[43,102644,102645],{},"Check whether the name is inside the correct scope",[43,102647,241,102648,102650],{},[17,102649,13235],{}," to confirm what exists before the failing line",[675,102652,102654],{"id":102653},"example-fix-a-typo","Example: fix a typo",[14,102656,102657],{},"Bad code:",[91,102659,102661],{"className":93,"code":102660,"language":95,"meta":96,"style":96},"message = \"Hello\"\nprint(mesage)\n",[17,102662,102663,102675],{"__ignoreMap":96},[100,102664,102665,102667,102669,102671,102673],{"class":102,"line":103},[100,102666,6868],{"class":106},[100,102668,111],{"class":110},[100,102670,1708],{"class":205},[100,102672,7683],{"class":209},[100,102674,1714],{"class":205},[100,102676,102677,102679,102681,102683],{"class":102,"line":135},[100,102678,372],{"class":114},[100,102680,170],{"class":118},[100,102682,54828],{"class":178},[100,102684,215],{"class":118},[14,102686,29249],{},[91,102688,102689],{"className":93,"code":53230,"language":95,"meta":96,"style":96},[17,102690,102691,102703],{"__ignoreMap":96},[100,102692,102693,102695,102697,102699,102701],{"class":102,"line":103},[100,102694,6868],{"class":106},[100,102696,111],{"class":110},[100,102698,1708],{"class":205},[100,102700,7683],{"class":209},[100,102702,1714],{"class":205},[100,102704,102705,102707,102709,102711],{"class":102,"line":135},[100,102706,372],{"class":114},[100,102708,170],{"class":118},[100,102710,1836],{"class":178},[100,102712,215],{"class":118},[675,102714,102716],{"id":102715},"example-assign-before-use","Example: assign before use",[14,102718,102657],{},[91,102720,102722],{"className":93,"code":102721,"language":95,"meta":96,"style":96},"print(total)\ntotal = 5\n",[17,102723,102724,102734],{"__ignoreMap":96},[100,102725,102726,102728,102730,102732],{"class":102,"line":103},[100,102727,372],{"class":114},[100,102729,170],{"class":118},[100,102731,2520],{"class":178},[100,102733,215],{"class":118},[100,102735,102736,102738,102740],{"class":102,"line":135},[100,102737,53376],{"class":106},[100,102739,111],{"class":110},[100,102741,37580],{"class":122},[14,102743,29249],{},[91,102745,102747],{"className":93,"code":102746,"language":95,"meta":96,"style":96},"total = 5\nprint(total)\n",[17,102748,102749,102757],{"__ignoreMap":96},[100,102750,102751,102753,102755],{"class":102,"line":103},[100,102752,53376],{"class":106},[100,102754,111],{"class":110},[100,102756,37580],{"class":122},[100,102758,102759,102761,102763,102765],{"class":102,"line":135},[100,102760,372],{"class":114},[100,102762,170],{"class":118},[100,102764,2520],{"class":178},[100,102766,215],{"class":118},[675,102768,102770],{"id":102769},"example-import-before-use","Example: import before use",[14,102772,102657],{},[91,102774,102775],{"className":93,"code":54688,"language":95,"meta":96,"style":96},[17,102776,102777],{"__ignoreMap":96},[100,102778,102779,102781,102783,102785,102787,102789,102791,102793],{"class":102,"line":103},[100,102780,372],{"class":114},[100,102782,170],{"class":118},[100,102784,9271],{"class":178},[100,102786,290],{"class":118},[100,102788,9276],{"class":178},[100,102790,170],{"class":118},[100,102792,10180],{"class":122},[100,102794,182],{"class":118},[14,102796,29249],{},[91,102798,102799],{"className":93,"code":10151,"language":95,"meta":96,"style":96},[17,102800,102801,102807,102811],{"__ignoreMap":96},[100,102802,102803,102805],{"class":102,"line":103},[100,102804,9259],{"class":145},[100,102806,9262],{"class":106},[100,102808,102809],{"class":102,"line":135},[100,102810,139],{"emptyLinePlaceholder":138},[100,102812,102813,102815,102817,102819,102821,102823,102825,102827],{"class":102,"line":142},[100,102814,372],{"class":114},[100,102816,170],{"class":118},[100,102818,9271],{"class":178},[100,102820,290],{"class":118},[100,102822,9276],{"class":178},[100,102824,170],{"class":118},[100,102826,10180],{"class":122},[100,102828,182],{"class":118},[14,102830,53520,102831,290],{},[295,102832,10137],{"href":25158},[77,102834,102836],{"id":102835},"how-to-fix-unboundlocalerror","How to fix UnboundLocalError",[14,102838,102839],{},"Good fixes usually include one of these:",[40,102841,102842,102845,102848,102851,102854],{},[43,102843,102844],{},"Assign the variable before reading it in the function",[43,102846,102847],{},"Use a different local variable name",[43,102849,102850],{},"Pass the value into the function as an argument",[43,102852,102853],{},"Return a new value instead of modifying outer state",[43,102855,241,102856,3178,102858,102860],{},[17,102857,100729],{},[17,102859,100732],{}," only when you truly need shared state",[675,102862,102863],{"id":101148},"Fix 1: pass the value into the function",[14,102865,102657],{},[91,102867,102869],{"className":93,"code":102868,"language":95,"meta":96,"style":96},"count = 10\n\ndef update_count():\n    print(count)\n    count = count + 1\n\nupdate_count()\n",[17,102870,102871,102879,102883,102892,102902,102914,102918],{"__ignoreMap":96},[100,102872,102873,102875,102877],{"class":102,"line":103},[100,102874,2176],{"class":106},[100,102876,111],{"class":110},[100,102878,2181],{"class":122},[100,102880,102881],{"class":102,"line":135},[100,102882,139],{"emptyLinePlaceholder":138},[100,102884,102885,102887,102890],{"class":102,"line":142},[100,102886,1078],{"class":1077},[100,102888,102889],{"class":1081}," update_count",[100,102891,1085],{"class":118},[100,102893,102894,102896,102898,102900],{"class":102,"line":152},[100,102895,200],{"class":114},[100,102897,170],{"class":118},[100,102899,51844],{"class":178},[100,102901,215],{"class":118},[100,102903,102904,102906,102908,102910,102912],{"class":102,"line":164},[100,102905,41602],{"class":106},[100,102907,111],{"class":110},[100,102909,2190],{"class":106},[100,102911,6547],{"class":110},[100,102913,1093],{"class":122},[100,102915,102916],{"class":102,"line":185},[100,102917,139],{"emptyLinePlaceholder":138},[100,102919,102920,102923],{"class":102,"line":197},[100,102921,102922],{"class":178},"update_count",[100,102924,4734],{"class":118},[14,102926,102927],{},"Better code:",[91,102929,102931],{"className":93,"code":102930,"language":95,"meta":96,"style":96},"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,102932,102933,102945,102955,102967,102973,102977,102985,102999],{"__ignoreMap":96},[100,102934,102935,102937,102939,102941,102943],{"class":102,"line":103},[100,102936,1078],{"class":1077},[100,102938,102889],{"class":1081},[100,102940,170],{"class":118},[100,102942,51844],{"class":2519},[100,102944,522],{"class":118},[100,102946,102947,102949,102951,102953],{"class":102,"line":135},[100,102948,200],{"class":114},[100,102950,170],{"class":118},[100,102952,51844],{"class":178},[100,102954,215],{"class":118},[100,102956,102957,102959,102961,102963,102965],{"class":102,"line":142},[100,102958,41602],{"class":106},[100,102960,111],{"class":110},[100,102962,2190],{"class":106},[100,102964,6547],{"class":110},[100,102966,1093],{"class":122},[100,102968,102969,102971],{"class":102,"line":152},[100,102970,2552],{"class":145},[100,102972,101495],{"class":106},[100,102974,102975],{"class":102,"line":164},[100,102976,139],{"emptyLinePlaceholder":138},[100,102978,102979,102981,102983],{"class":102,"line":185},[100,102980,2176],{"class":106},[100,102982,111],{"class":110},[100,102984,2181],{"class":122},[100,102986,102987,102989,102991,102993,102995,102997],{"class":102,"line":197},[100,102988,2176],{"class":106},[100,102990,111],{"class":110},[100,102992,102889],{"class":178},[100,102994,170],{"class":118},[100,102996,51844],{"class":178},[100,102998,215],{"class":118},[100,103000,103001,103003,103005,103007],{"class":102,"line":771},[100,103002,372],{"class":114},[100,103004,170],{"class":118},[100,103006,51844],{"class":178},[100,103008,215],{"class":118},[14,103010,14742],{},[91,103012,103014],{"className":93,"code":103013,"language":95,"meta":96,"style":96},"10\n11\n",[17,103015,103016,103020],{"__ignoreMap":96},[100,103017,103018],{"class":102,"line":103},[100,103019,3410],{"class":122},[100,103021,103022],{"class":102,"line":135},[100,103023,103024],{"class":122},"11\n",[14,103026,103027],{},"This is often the best beginner-friendly solution.",[675,103029,103031],{"id":103030},"fix-2-use-a-different-local-variable-name","Fix 2: use a different local variable name",[91,103033,103035],{"className":93,"code":103034,"language":95,"meta":96,"style":96},"count = 10\n\ndef show_new_count():\n    new_count = count + 1\n    print(new_count)\n\nshow_new_count()\n",[17,103036,103037,103045,103049,103058,103071,103082,103086],{"__ignoreMap":96},[100,103038,103039,103041,103043],{"class":102,"line":103},[100,103040,2176],{"class":106},[100,103042,111],{"class":110},[100,103044,2181],{"class":122},[100,103046,103047],{"class":102,"line":135},[100,103048,139],{"emptyLinePlaceholder":138},[100,103050,103051,103053,103056],{"class":102,"line":142},[100,103052,1078],{"class":1077},[100,103054,103055],{"class":1081}," show_new_count",[100,103057,1085],{"class":118},[100,103059,103060,103063,103065,103067,103069],{"class":102,"line":152},[100,103061,103062],{"class":106},"    new_count ",[100,103064,111],{"class":110},[100,103066,2190],{"class":106},[100,103068,6547],{"class":110},[100,103070,1093],{"class":122},[100,103072,103073,103075,103077,103080],{"class":102,"line":164},[100,103074,200],{"class":114},[100,103076,170],{"class":118},[100,103078,103079],{"class":178},"new_count",[100,103081,215],{"class":118},[100,103083,103084],{"class":102,"line":185},[100,103085,139],{"emptyLinePlaceholder":138},[100,103087,103088,103091],{"class":102,"line":197},[100,103089,103090],{"class":178},"show_new_count",[100,103092,4734],{"class":118},[14,103094,14742],{},[91,103096,103097],{"className":93,"code":103024,"language":95,"meta":96,"style":96},[17,103098,103099],{"__ignoreMap":96},[100,103100,103101],{"class":102,"line":103},[100,103102,103024],{"class":122},[14,103104,17008,103105,103107,103108,103110],{},[17,103106,51844],{}," is read from outside the function, and ",[17,103109,103079],{}," is local.",[675,103112,103114],{"id":103113},"fix-3-assign-before-reading-the-local-variable","Fix 3: assign before reading the local variable",[91,103116,103118],{"className":93,"code":103117,"language":95,"meta":96,"style":96},"def show_count():\n    count = 20\n    print(count)\n\nshow_count()\n",[17,103119,103120,103128,103136,103146,103150],{"__ignoreMap":96},[100,103121,103122,103124,103126],{"class":102,"line":103},[100,103123,1078],{"class":1077},[100,103125,55138],{"class":1081},[100,103127,1085],{"class":118},[100,103129,103130,103132,103134],{"class":102,"line":135},[100,103131,41602],{"class":106},[100,103133,111],{"class":110},[100,103135,1669],{"class":122},[100,103137,103138,103140,103142,103144],{"class":102,"line":142},[100,103139,200],{"class":114},[100,103141,170],{"class":118},[100,103143,51844],{"class":178},[100,103145,215],{"class":118},[100,103147,103148],{"class":102,"line":152},[100,103149,139],{"emptyLinePlaceholder":138},[100,103151,103152,103154],{"class":102,"line":164},[100,103153,55167],{"class":178},[100,103155,4734],{"class":118},[14,103157,14742],{},[91,103159,103160],{"className":93,"code":79076,"language":95,"meta":96,"style":96},[17,103161,103162],{"__ignoreMap":96},[100,103163,103164],{"class":102,"line":103},[100,103165,79076],{"class":122},[14,103167,103168],{},"This works because the local variable gets its value before it is used.",[77,103170,103172],{"id":103171},"when-global-and-nonlocal-are-involved","When global and nonlocal are involved",[14,103174,46235,103175,3266,103177,103179],{},[17,103176,100729],{},[17,103178,100732],{}," are part of the fix.",[40,103181,103182,103187,103192,103198],{},[43,103183,103184,103186],{},[17,103185,100729],{}," tells Python to use a module-level variable",[43,103188,103189,103191],{},[17,103190,100732],{}," tells Python to use a variable from an outer function",[43,103193,103194,103195,103197],{},"These can fix some ",[17,103196,16651],{}," cases",[43,103199,103200],{},"Beginners should usually prefer arguments and return values when possible",[675,103202,103204],{"id":103203},"example-with-global","Example with global",[91,103206,103208],{"className":93,"code":103207,"language":95,"meta":96,"style":96},"count = 10\n\ndef increase():\n    global count\n    count = count + 1\n    print(count)\n\nincrease()\n",[17,103209,103210,103218,103222,103231,103237,103249,103259,103263],{"__ignoreMap":96},[100,103211,103212,103214,103216],{"class":102,"line":103},[100,103213,2176],{"class":106},[100,103215,111],{"class":110},[100,103217,2181],{"class":122},[100,103219,103220],{"class":102,"line":135},[100,103221,139],{"emptyLinePlaceholder":138},[100,103223,103224,103226,103229],{"class":102,"line":142},[100,103225,1078],{"class":1077},[100,103227,103228],{"class":1081}," increase",[100,103230,1085],{"class":118},[100,103232,103233,103235],{"class":102,"line":152},[100,103234,101366],{"class":1077},[100,103236,101495],{"class":106},[100,103238,103239,103241,103243,103245,103247],{"class":102,"line":164},[100,103240,41602],{"class":106},[100,103242,111],{"class":110},[100,103244,2190],{"class":106},[100,103246,6547],{"class":110},[100,103248,1093],{"class":122},[100,103250,103251,103253,103255,103257],{"class":102,"line":185},[100,103252,200],{"class":114},[100,103254,170],{"class":118},[100,103256,51844],{"class":178},[100,103258,215],{"class":118},[100,103260,103261],{"class":102,"line":197},[100,103262,139],{"emptyLinePlaceholder":138},[100,103264,103265,103268],{"class":102,"line":771},[100,103266,103267],{"class":178},"increase",[100,103269,4734],{"class":118},[14,103271,14742],{},[91,103273,103274],{"className":93,"code":103024,"language":95,"meta":96,"style":96},[17,103275,103276],{"__ignoreMap":96},[100,103277,103278],{"class":102,"line":103},[100,103279,103024],{"class":122},[14,103281,103282,103283,103286],{},"Because of ",[17,103284,103285],{},"global count",", Python uses the module-level variable.",[675,103288,103290],{"id":103289},"example-with-nonlocal","Example with nonlocal",[91,103292,103294],{"className":93,"code":103293,"language":95,"meta":96,"style":96},"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,103295,103296,103304,103312,103316,103324,103330,103342,103352,103356,103362,103366],{"__ignoreMap":96},[100,103297,103298,103300,103302],{"class":102,"line":103},[100,103299,1078],{"class":1077},[100,103301,101464],{"class":1081},[100,103303,1085],{"class":118},[100,103305,103306,103308,103310],{"class":102,"line":135},[100,103307,41602],{"class":106},[100,103309,111],{"class":110},[100,103311,2181],{"class":122},[100,103313,103314],{"class":102,"line":142},[100,103315,139],{"emptyLinePlaceholder":138},[100,103317,103318,103320,103322],{"class":102,"line":152},[100,103319,89014],{"class":1077},[100,103321,101485],{"class":1081},[100,103323,1085],{"class":118},[100,103325,103326,103328],{"class":102,"line":164},[100,103327,101492],{"class":1077},[100,103329,101495],{"class":106},[100,103331,103332,103334,103336,103338,103340],{"class":102,"line":185},[100,103333,101500],{"class":106},[100,103335,111],{"class":110},[100,103337,2190],{"class":106},[100,103339,6547],{"class":110},[100,103341,1093],{"class":122},[100,103343,103344,103346,103348,103350],{"class":102,"line":197},[100,103345,167],{"class":114},[100,103347,170],{"class":118},[100,103349,51844],{"class":178},[100,103351,215],{"class":118},[100,103353,103354],{"class":102,"line":771},[100,103355,139],{"emptyLinePlaceholder":138},[100,103357,103358,103360],{"class":102,"line":787},[100,103359,101523],{"class":178},[100,103361,4734],{"class":118},[100,103363,103364],{"class":102,"line":5816},[100,103365,139],{"emptyLinePlaceholder":138},[100,103367,103368,103370],{"class":102,"line":5833},[100,103369,101540],{"class":178},[100,103371,4734],{"class":118},[14,103373,14742],{},[91,103375,103376],{"className":93,"code":103024,"language":95,"meta":96,"style":96},[17,103377,103378],{"__ignoreMap":96},[100,103379,103380],{"class":102,"line":103},[100,103381,103024],{"class":122},[14,103383,103384],{},"Use these keywords carefully. They can work, but they can also make code harder to follow.",[77,103386,103388],{"id":103387},"quick-debugging-checklist","Quick debugging checklist",[14,103390,103391],{},"When you see one of these errors, check this list:",[40,103393,103394,103397,103400,103403,103406,103409],{},[43,103395,103396],{},"Read the exact error name carefully",[43,103398,103399],{},"Check whether the problem line is inside a function",[43,103401,103402],{},"Look for assignments to the same variable later in that function",[43,103404,103405],{},"Check whether the variable was ever created",[43,103407,103408],{},"Print values before the failing line if needed",[43,103410,103411],{},"Rename variables to avoid confusing shadowing",[14,103413,103414],{},"Helpful debugging commands:",[91,103416,103418],{"className":93,"code":103417,"language":95,"meta":96,"style":96},"print(variable_name)\nprint(locals())\nprint(globals().keys())\ntype(variable_name)\nhelp('global')\nhelp('nonlocal')\n",[17,103419,103420,103430,103440,103454,103464,103478],{"__ignoreMap":96},[100,103421,103422,103424,103426,103428],{"class":102,"line":103},[100,103423,372],{"class":114},[100,103425,170],{"class":118},[100,103427,53946],{"class":178},[100,103429,215],{"class":118},[100,103431,103432,103434,103436,103438],{"class":102,"line":135},[100,103433,372],{"class":114},[100,103435,170],{"class":118},[100,103437,53957],{"class":114},[100,103439,3370],{"class":118},[100,103441,103442,103444,103446,103448,103450,103452],{"class":102,"line":142},[100,103443,372],{"class":114},[100,103445,170],{"class":118},[100,103447,53968],{"class":114},[100,103449,8886],{"class":118},[100,103451,6185],{"class":178},[100,103453,3370],{"class":118},[100,103455,103456,103458,103460,103462],{"class":102,"line":152},[100,103457,1250],{"class":191},[100,103459,170],{"class":118},[100,103461,53946],{"class":178},[100,103463,215],{"class":118},[100,103465,103466,103468,103470,103472,103474,103476],{"class":102,"line":164},[100,103467,2478],{"class":114},[100,103469,170],{"class":118},[100,103471,1280],{"class":205},[100,103473,100729],{"class":209},[100,103475,1280],{"class":205},[100,103477,215],{"class":118},[100,103479,103480,103482,103484,103486,103488,103490],{"class":102,"line":185},[100,103481,2478],{"class":114},[100,103483,170],{"class":118},[100,103485,1280],{"class":205},[100,103487,100732],{"class":209},[100,103489,1280],{"class":205},[100,103491,215],{"class":118},[14,103493,103494,103495,290],{},"If you want a broader step-by-step process, see ",[295,103496,3853],{"href":3852},[77,103498,7117],{"id":7116},[14,103500,103501,103502,3178,103504,89],{},"These are the most common reasons beginners see ",[17,103503,5616],{},[17,103505,16651],{},[40,103507,103508,103510,103513,103516,103519,103522],{},[43,103509,54071],{},[43,103511,103512],{},"Using a variable before assigning a value",[43,103514,103515],{},"Assigning to a variable inside a function after reading it",[43,103517,103518],{},"Confusing global and local variables",[43,103520,103521],{},"Forgetting to import a name before using it",[43,103523,103524],{},"Reusing the same variable name in nested scopes",[77,103526,1514],{"id":1513},[675,103528,102074],{"id":102073},[14,103530,14506,103531,103533,103534,103536],{},[17,103532,16651],{}," is a more specific scope-related error. ",[17,103535,5616],{}," means the name cannot be found at all.",[675,103538,103540],{"id":103539},"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,103542,103543],{},"Because assigning to that name inside the function makes Python treat it as a local variable for the whole function.",[675,103545,103547],{"id":103546},"should-i-use-global-to-fix-unboundlocalerror","Should I use global to fix UnboundLocalError?",[14,103549,103550],{},"Sometimes, but beginners should usually pass values into functions and return results instead.",[675,103552,103554],{"id":103553},"can-nameerror-happen-with-function-names-too","Can NameError happen with function names too?",[14,103556,103557,103558,290],{},"Yes. If you call a function that was not defined or imported, Python raises ",[17,103559,5616],{},[675,103561,103563],{"id":103562},"can-a-typo-cause-both-errors","Can a typo cause both errors?",[14,103565,103566,103567,93668,103569,103571],{},"A typo usually causes ",[17,103568,5616],{},[17,103570,16651],{}," is more often caused by scope and assignment inside a function.",[77,103573,1554],{"id":1553},[40,103575,103576,103580,103584,103588,103592,103596],{},[43,103577,103578],{},[295,103579,54200],{"href":56646},[43,103581,103582],{},[295,103583,53909],{"href":55181},[43,103585,103586],{},[295,103587,53345],{"href":54746},[43,103589,103590],{},[295,103591,29749],{"href":29189},[43,103593,103594],{},[295,103595,5690],{"href":3852},[43,103597,103598],{},[295,103599,10666],{"href":25158},[1589,103601,103602],{},"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":96,"searchDepth":135,"depth":135,"links":103604},[103605,103606,103607,103608,103609,103610,103611,103614,103619,103624,103628,103629,103630,103637],{"id":79,"depth":135,"text":80},{"id":102282,"depth":135,"text":102283},{"id":102311,"depth":135,"text":102312},{"id":102344,"depth":135,"text":102345},{"id":102374,"depth":135,"text":102375},{"id":102390,"depth":135,"text":102391},{"id":102471,"depth":135,"text":102472,"children":103612},[103613],{"id":102595,"depth":142,"text":102596},{"id":102627,"depth":135,"text":102628,"children":103615},[103616,103617,103618],{"id":102653,"depth":142,"text":102654},{"id":102715,"depth":142,"text":102716},{"id":102769,"depth":142,"text":102770},{"id":102835,"depth":135,"text":102836,"children":103620},[103621,103622,103623],{"id":101148,"depth":142,"text":102863},{"id":103030,"depth":142,"text":103031},{"id":103113,"depth":142,"text":103114},{"id":103171,"depth":135,"text":103172,"children":103625},[103626,103627],{"id":103203,"depth":142,"text":103204},{"id":103289,"depth":142,"text":103290},{"id":103387,"depth":135,"text":103388},{"id":7116,"depth":135,"text":7117},{"id":1513,"depth":135,"text":1514,"children":103631},[103632,103633,103634,103635,103636],{"id":102073,"depth":142,"text":102074},{"id":103539,"depth":142,"text":103540},{"id":103546,"depth":142,"text":103547},{"id":103553,"depth":142,"text":103554},{"id":103562,"depth":142,"text":103563},{"id":1553,"depth":135,"text":1554},"Master unboundlocalerror vs nameerror explained in our comprehensive Python beginner guide.",{},"\u002Ferrors\u002Funboundlocalerror-vs-nameerror-explained",{"title":102178,"description":103638},"errors\u002Funboundlocalerror-vs-nameerror-explained","SVzBQ-8S03NEsFKd0WNFWcpinj2hRhvvLSjer-p0B24",{"id":103645,"title":103646,"body":103647,"description":105157,"extension":1623,"meta":105158,"navigation":138,"path":105159,"seo":105160,"stem":105161,"__hash__":105162},"content\u002Ferrors\u002Funicodedecodeerror-utf-8-codec-cant-decode-byte-fix.md","UnicodeDecodeError: 'utf-8' codec can't decode byte (Fix)",{"type":7,"value":103648,"toc":105122},[103649,103656,103659,103685,103688,103690,103704,103711,103713,103789,103792,103794,103796,103835,103837,103849,103852,103854,103857,103879,103887,103889,103892,103967,103973,103979,104003,104005,104019,104023,104027,104032,104107,104113,104187,104194,104201,104204,104279,104282,104292,104295,104298,104386,104389,104477,104480,104495,104498,104502,104505,104507,104569,104571,104639,104641,104660,104664,104667,104670,104690,104692,104706,104713,104715,104718,104722,104725,104779,104781,104792,104798,104859,104865,104926,104932,104993,104999,105003,105006,105022,105024,105026,105048,105050,105054,105057,105063,105066,105073,105082,105086,105092,105094,105119],[10,103650,103652,103653,7375],{"id":103651},"unicodedecodeerror-utf-8-codec-cant-decode-byte-fix","UnicodeDecodeError: ",[17,103654,103655],{},"'utf-8' codec can't decode byte",[14,103657,103658],{},"If you see a Python error like this:",[91,103660,103662],{"className":93,"code":103661,"language":95,"meta":96,"style":96},"UnicodeDecodeError: 'utf-8' codec can't decode byte ...\n",[17,103663,103664],{"__ignoreMap":96},[100,103665,103666,103669,103671,103673,103675,103677,103680,103682],{"class":102,"line":103},[100,103667,103668],{"class":191},"UnicodeDecodeError",[100,103670,89],{"class":118},[100,103672,1274],{"class":205},[100,103674,15307],{"class":209},[100,103676,1280],{"class":205},[100,103678,103679],{"class":106}," codec can",[100,103681,1280],{"class":205},[100,103683,103684],{"class":209},"t decode byte ...\n",[14,103686,103687],{},"Python is trying to read data as UTF-8 text, but the bytes do not match valid UTF-8.",[14,103689,30678],{},[40,103691,103692,103695,103698,103701],{},[43,103693,103694],{},"you open a text file with the wrong encoding",[43,103696,103697],{},"a CSV file came from Excel or another Windows program",[43,103699,103700],{},"you decode bytes from a file, download, or API using the wrong encoding",[43,103702,103703],{},"the file is actually binary, not text",[14,103705,103706,103707,103710],{},"In most cases, the fix is to open the file with the ",[22,103708,103709],{},"correct encoding"," instead of relying on the default.",[77,103712,80],{"id":79},[91,103714,103716],{"className":93,"code":103715,"language":95,"meta":96,"style":96},"with open('data.txt', 'r', encoding='latin-1') as file:\n    text = file.read()\n\nprint(text)\n",[17,103717,103718,103761,103775,103779],{"__ignoreMap":96},[100,103719,103720,103722,103724,103726,103728,103730,103732,103734,103736,103738,103740,103742,103744,103746,103748,103751,103753,103755,103757,103759],{"class":102,"line":103},[100,103721,17521],{"class":145},[100,103723,17524],{"class":114},[100,103725,170],{"class":118},[100,103727,1280],{"class":205},[100,103729,18094],{"class":209},[100,103731,1280],{"class":205},[100,103733,126],{"class":118},[100,103735,1274],{"class":205},[100,103737,17540],{"class":209},[100,103739,1280],{"class":205},[100,103741,126],{"class":118},[100,103743,18142],{"class":18141},[100,103745,111],{"class":110},[100,103747,1280],{"class":205},[100,103749,103750],{"class":209},"latin-1",[100,103752,1280],{"class":205},[100,103754,6155],{"class":118},[100,103756,16224],{"class":145},[100,103758,17550],{"class":17549},[100,103760,149],{"class":118},[100,103762,103763,103765,103767,103769,103771,103773],{"class":102,"line":135},[100,103764,41656],{"class":106},[100,103766,111],{"class":110},[100,103768,17550],{"class":17549},[100,103770,290],{"class":118},[100,103772,17566],{"class":178},[100,103774,4734],{"class":118},[100,103776,103777],{"class":102,"line":142},[100,103778,139],{"emptyLinePlaceholder":138},[100,103780,103781,103783,103785,103787],{"class":102,"line":152},[100,103782,372],{"class":114},[100,103784,170],{"class":118},[100,103786,4468],{"class":178},[100,103788,215],{"class":118},[14,103790,103791],{},"Use the correct file encoding instead of the default UTF-8. If you do not know the encoding, first check where the file came from.",[77,103793,5881],{"id":5880},[14,103795,9328],{},[40,103797,103798,103806,103809,103812],{},[43,103799,103800,103801,77694,103803,103805],{},"Python is trying to turn ",[22,103802,15319],{},[22,103804,4468],{}," using UTF-8",[43,103807,103808],{},"at least one byte is not valid in UTF-8",[43,103810,103811],{},"the file or data was probably saved with a different encoding",[43,103813,103814,103815],{},"common encodings include:\n",[40,103816,103817,103821,103825,103830],{},[43,103818,103819],{},[17,103820,15307],{},[43,103822,103823],{},[17,103824,103750],{},[43,103826,103827],{},[17,103828,103829],{},"cp1252",[43,103831,103832],{},[17,103833,103834],{},"utf-16",[14,103836,23448],{},[40,103838,103839,103844],{},[43,103840,103841,103843],{},[22,103842,15319],{}," are raw data",[43,103845,103846,103848],{},[22,103847,19977],{}," tells Python how to interpret those bytes as text",[14,103850,103851],{},"If Python uses the wrong encoding, it cannot read the text correctly.",[77,103853,18290],{"id":18289},[14,103855,103856],{},"You will often see this error when:",[40,103858,103859,103867,103870,103873],{},[43,103860,103861,103862,103864,103865],{},"reading a text file with ",[17,103863,18032],{}," without the right ",[17,103866,19977],{},[43,103868,103869],{},"loading CSV or text data created on another system",[43,103871,103872],{},"decoding raw bytes from a network response or binary source",[43,103874,103875,103876,103878],{},"reading Windows-created files that use ",[17,103877,103829],{}," instead of UTF-8",[14,103880,103881,103882,3266,103884,290],{},"If you are new to file reading, see ",[295,103883,21207],{"href":20590},[295,103885,103886],{"href":22656},"Python file handling basics",[77,103888,11031],{"id":11030},[14,103890,103891],{},"Here is a typical example.",[91,103893,103895],{"className":93,"code":103894,"language":95,"meta":96,"style":96},"with open('data.txt', 'r', encoding='utf-8') as file:\n    text = file.read()\n\nprint(text)\n",[17,103896,103897,103939,103953,103957],{"__ignoreMap":96},[100,103898,103899,103901,103903,103905,103907,103909,103911,103913,103915,103917,103919,103921,103923,103925,103927,103929,103931,103933,103935,103937],{"class":102,"line":103},[100,103900,17521],{"class":145},[100,103902,17524],{"class":114},[100,103904,170],{"class":118},[100,103906,1280],{"class":205},[100,103908,18094],{"class":209},[100,103910,1280],{"class":205},[100,103912,126],{"class":118},[100,103914,1274],{"class":205},[100,103916,17540],{"class":209},[100,103918,1280],{"class":205},[100,103920,126],{"class":118},[100,103922,18142],{"class":18141},[100,103924,111],{"class":110},[100,103926,1280],{"class":205},[100,103928,15307],{"class":209},[100,103930,1280],{"class":205},[100,103932,6155],{"class":118},[100,103934,16224],{"class":145},[100,103936,17550],{"class":17549},[100,103938,149],{"class":118},[100,103940,103941,103943,103945,103947,103949,103951],{"class":102,"line":135},[100,103942,41656],{"class":106},[100,103944,111],{"class":110},[100,103946,17550],{"class":17549},[100,103948,290],{"class":118},[100,103950,17566],{"class":178},[100,103952,4734],{"class":118},[100,103954,103955],{"class":102,"line":142},[100,103956,139],{"emptyLinePlaceholder":138},[100,103958,103959,103961,103963,103965],{"class":102,"line":152},[100,103960,372],{"class":114},[100,103962,170],{"class":118},[100,103964,4468],{"class":178},[100,103966,215],{"class":118},[14,103968,103969,103970,103972],{},"This code works only if ",[17,103971,18094],{}," is really saved as UTF-8.",[14,103974,103975,103976,103978],{},"If the file actually uses another encoding such as ",[17,103977,103829],{},", Python may raise:",[91,103980,103982],{"className":93,"code":103981,"language":95,"meta":96,"style":96},"UnicodeDecodeError: 'utf-8' codec can't decode byte 0x92 in position 10: invalid start byte\n",[17,103983,103984],{"__ignoreMap":96},[100,103985,103986,103988,103990,103992,103994,103996,103998,104000],{"class":102,"line":103},[100,103987,103668],{"class":191},[100,103989,89],{"class":118},[100,103991,1274],{"class":205},[100,103993,15307],{"class":209},[100,103995,1280],{"class":205},[100,103997,103679],{"class":106},[100,103999,1280],{"class":205},[100,104001,104002],{"class":209},"t decode byte 0x92 in position 10: invalid start byte\n",[14,104004,11192],{},[40,104006,104007,104010,104013,104016],{},[43,104008,104009],{},"the file was opened in text mode",[43,104011,104012],{},"Python tried to decode it as UTF-8",[43,104014,104015],{},"one or more bytes were invalid for UTF-8",[43,104017,104018],{},"reading failed",[77,104020,104022],{"id":104021},"main-ways-to-fix-it","Main ways to fix it",[675,104024,104026],{"id":104025},"open-the-file-with-the-correct-encoding","Open the file with the correct encoding",[14,104028,104029,104030,290],{},"If you know the file encoding, pass it to ",[17,104031,18032],{},[91,104033,104035],{"className":93,"code":104034,"language":95,"meta":96,"style":96},"with open('data.txt', 'r', encoding='cp1252') as file:\n    text = file.read()\n\nprint(text)\n",[17,104036,104037,104079,104093,104097],{"__ignoreMap":96},[100,104038,104039,104041,104043,104045,104047,104049,104051,104053,104055,104057,104059,104061,104063,104065,104067,104069,104071,104073,104075,104077],{"class":102,"line":103},[100,104040,17521],{"class":145},[100,104042,17524],{"class":114},[100,104044,170],{"class":118},[100,104046,1280],{"class":205},[100,104048,18094],{"class":209},[100,104050,1280],{"class":205},[100,104052,126],{"class":118},[100,104054,1274],{"class":205},[100,104056,17540],{"class":209},[100,104058,1280],{"class":205},[100,104060,126],{"class":118},[100,104062,18142],{"class":18141},[100,104064,111],{"class":110},[100,104066,1280],{"class":205},[100,104068,103829],{"class":209},[100,104070,1280],{"class":205},[100,104072,6155],{"class":118},[100,104074,16224],{"class":145},[100,104076,17550],{"class":17549},[100,104078,149],{"class":118},[100,104080,104081,104083,104085,104087,104089,104091],{"class":102,"line":135},[100,104082,41656],{"class":106},[100,104084,111],{"class":110},[100,104086,17550],{"class":17549},[100,104088,290],{"class":118},[100,104090,17566],{"class":178},[100,104092,4734],{"class":118},[100,104094,104095],{"class":102,"line":142},[100,104096,139],{"emptyLinePlaceholder":138},[100,104098,104099,104101,104103,104105],{"class":102,"line":152},[100,104100,372],{"class":114},[100,104102,170],{"class":118},[100,104104,4468],{"class":178},[100,104106,215],{"class":118},[14,104108,104109,104110,104112],{},"You can also try ",[17,104111,103750],{}," if that matches the source:",[91,104114,104115],{"className":93,"code":103715,"language":95,"meta":96,"style":96},[17,104116,104117,104159,104173,104177],{"__ignoreMap":96},[100,104118,104119,104121,104123,104125,104127,104129,104131,104133,104135,104137,104139,104141,104143,104145,104147,104149,104151,104153,104155,104157],{"class":102,"line":103},[100,104120,17521],{"class":145},[100,104122,17524],{"class":114},[100,104124,170],{"class":118},[100,104126,1280],{"class":205},[100,104128,18094],{"class":209},[100,104130,1280],{"class":205},[100,104132,126],{"class":118},[100,104134,1274],{"class":205},[100,104136,17540],{"class":209},[100,104138,1280],{"class":205},[100,104140,126],{"class":118},[100,104142,18142],{"class":18141},[100,104144,111],{"class":110},[100,104146,1280],{"class":205},[100,104148,103750],{"class":209},[100,104150,1280],{"class":205},[100,104152,6155],{"class":118},[100,104154,16224],{"class":145},[100,104156,17550],{"class":17549},[100,104158,149],{"class":118},[100,104160,104161,104163,104165,104167,104169,104171],{"class":102,"line":135},[100,104162,41656],{"class":106},[100,104164,111],{"class":110},[100,104166,17550],{"class":17549},[100,104168,290],{"class":118},[100,104170,17566],{"class":178},[100,104172,4734],{"class":118},[100,104174,104175],{"class":102,"line":142},[100,104176,139],{"emptyLinePlaceholder":138},[100,104178,104179,104181,104183,104185],{"class":102,"line":152},[100,104180,372],{"class":114},[100,104182,170],{"class":118},[100,104184,4468],{"class":178},[100,104186,215],{"class":118},[14,104188,104189,104190,12574,104192,290],{},"For a beginner-friendly explanation of ",[17,104191,18032],{},[295,104193,24736],{"href":18280},[675,104195,104197,104198],{"id":104196},"if-the-file-has-a-utf-8-bom-try-utf-8-sig","If the file has a UTF-8 BOM, try ",[17,104199,104200],{},"utf-8-sig",[14,104202,104203],{},"Some files are UTF-8 but include a special marker at the start called a BOM.",[91,104205,104207],{"className":93,"code":104206,"language":95,"meta":96,"style":96},"with open('data.txt', 'r', encoding='utf-8-sig') as file:\n    text = file.read()\n\nprint(text)\n",[17,104208,104209,104251,104265,104269],{"__ignoreMap":96},[100,104210,104211,104213,104215,104217,104219,104221,104223,104225,104227,104229,104231,104233,104235,104237,104239,104241,104243,104245,104247,104249],{"class":102,"line":103},[100,104212,17521],{"class":145},[100,104214,17524],{"class":114},[100,104216,170],{"class":118},[100,104218,1280],{"class":205},[100,104220,18094],{"class":209},[100,104222,1280],{"class":205},[100,104224,126],{"class":118},[100,104226,1274],{"class":205},[100,104228,17540],{"class":209},[100,104230,1280],{"class":205},[100,104232,126],{"class":118},[100,104234,18142],{"class":18141},[100,104236,111],{"class":110},[100,104238,1280],{"class":205},[100,104240,104200],{"class":209},[100,104242,1280],{"class":205},[100,104244,6155],{"class":118},[100,104246,16224],{"class":145},[100,104248,17550],{"class":17549},[100,104250,149],{"class":118},[100,104252,104253,104255,104257,104259,104261,104263],{"class":102,"line":135},[100,104254,41656],{"class":106},[100,104256,111],{"class":110},[100,104258,17550],{"class":17549},[100,104260,290],{"class":118},[100,104262,17566],{"class":178},[100,104264,4734],{"class":118},[100,104266,104267],{"class":102,"line":142},[100,104268,139],{"emptyLinePlaceholder":138},[100,104270,104271,104273,104275,104277],{"class":102,"line":152},[100,104272,372],{"class":114},[100,104274,170],{"class":118},[100,104276,4468],{"class":178},[100,104278,215],{"class":118},[14,104280,104281],{},"This is often useful for text files saved by certain editors or export tools.",[675,104283,241,104285,3178,104288,104291],{"id":104284},"use-errorsignore-or-errorsreplace-only-if-needed",[17,104286,104287],{},"errors='ignore'",[17,104289,104290],{},"errors='replace'"," only if needed",[14,104293,104294],{},"If you must keep reading even when some characters are bad, you can tell Python how to handle decode errors.",[14,104296,104297],{},"Ignore bad characters:",[91,104299,104301],{"className":93,"code":104300,"language":95,"meta":96,"style":96},"with open('data.txt', 'r', encoding='utf-8', errors='ignore') as file:\n    text = file.read()\n\nprint(text)\n",[17,104302,104303,104358,104372,104376],{"__ignoreMap":96},[100,104304,104305,104307,104309,104311,104313,104315,104317,104319,104321,104323,104325,104327,104329,104331,104333,104335,104337,104339,104341,104343,104345,104348,104350,104352,104354,104356],{"class":102,"line":103},[100,104306,17521],{"class":145},[100,104308,17524],{"class":114},[100,104310,170],{"class":118},[100,104312,1280],{"class":205},[100,104314,18094],{"class":209},[100,104316,1280],{"class":205},[100,104318,126],{"class":118},[100,104320,1274],{"class":205},[100,104322,17540],{"class":209},[100,104324,1280],{"class":205},[100,104326,126],{"class":118},[100,104328,18142],{"class":18141},[100,104330,111],{"class":110},[100,104332,1280],{"class":205},[100,104334,15307],{"class":209},[100,104336,1280],{"class":205},[100,104338,126],{"class":118},[100,104340,4123],{"class":18141},[100,104342,111],{"class":110},[100,104344,1280],{"class":205},[100,104346,104347],{"class":209},"ignore",[100,104349,1280],{"class":205},[100,104351,6155],{"class":118},[100,104353,16224],{"class":145},[100,104355,17550],{"class":17549},[100,104357,149],{"class":118},[100,104359,104360,104362,104364,104366,104368,104370],{"class":102,"line":135},[100,104361,41656],{"class":106},[100,104363,111],{"class":110},[100,104365,17550],{"class":17549},[100,104367,290],{"class":118},[100,104369,17566],{"class":178},[100,104371,4734],{"class":118},[100,104373,104374],{"class":102,"line":142},[100,104375,139],{"emptyLinePlaceholder":138},[100,104377,104378,104380,104382,104384],{"class":102,"line":152},[100,104379,372],{"class":114},[100,104381,170],{"class":118},[100,104383,4468],{"class":178},[100,104385,215],{"class":118},[14,104387,104388],{},"Replace bad characters:",[91,104390,104392],{"className":93,"code":104391,"language":95,"meta":96,"style":96},"with open('data.txt', 'r', encoding='utf-8', errors='replace') as file:\n    text = file.read()\n\nprint(text)\n",[17,104393,104394,104449,104463,104467],{"__ignoreMap":96},[100,104395,104396,104398,104400,104402,104404,104406,104408,104410,104412,104414,104416,104418,104420,104422,104424,104426,104428,104430,104432,104434,104436,104439,104441,104443,104445,104447],{"class":102,"line":103},[100,104397,17521],{"class":145},[100,104399,17524],{"class":114},[100,104401,170],{"class":118},[100,104403,1280],{"class":205},[100,104405,18094],{"class":209},[100,104407,1280],{"class":205},[100,104409,126],{"class":118},[100,104411,1274],{"class":205},[100,104413,17540],{"class":209},[100,104415,1280],{"class":205},[100,104417,126],{"class":118},[100,104419,18142],{"class":18141},[100,104421,111],{"class":110},[100,104423,1280],{"class":205},[100,104425,15307],{"class":209},[100,104427,1280],{"class":205},[100,104429,126],{"class":118},[100,104431,4123],{"class":18141},[100,104433,111],{"class":110},[100,104435,1280],{"class":205},[100,104437,104438],{"class":209},"replace",[100,104440,1280],{"class":205},[100,104442,6155],{"class":118},[100,104444,16224],{"class":145},[100,104446,17550],{"class":17549},[100,104448,149],{"class":118},[100,104450,104451,104453,104455,104457,104459,104461],{"class":102,"line":135},[100,104452,41656],{"class":106},[100,104454,111],{"class":110},[100,104456,17550],{"class":17549},[100,104458,290],{"class":118},[100,104460,17566],{"class":178},[100,104462,4734],{"class":118},[100,104464,104465],{"class":102,"line":142},[100,104466,139],{"emptyLinePlaceholder":138},[100,104468,104469,104471,104473,104475],{"class":102,"line":152},[100,104470,372],{"class":114},[100,104472,170],{"class":118},[100,104474,4468],{"class":178},[100,104476,215],{"class":118},[14,104478,104479],{},"Use this carefully:",[40,104481,104482,104487,104492],{},[43,104483,104484,104486],{},[17,104485,104347],{}," silently removes characters",[43,104488,104489,104491],{},[17,104490,104438],{}," changes them to replacement symbols",[43,104493,104494],{},"both can hide the real problem",[14,104496,104497],{},"If the text matters, finding the correct encoding is safer.",[675,104499,104501],{"id":104500},"open-binary-files-in-binary-mode","Open binary files in binary mode",[14,104503,104504],{},"If the file is not really text, do not open it in text mode.",[14,104506,9709],{},[91,104508,104510],{"className":93,"code":104509,"language":95,"meta":96,"style":96},"with open('image.jpg', 'r', encoding='utf-8') as file:\n    data = file.read()\n",[17,104511,104512,104555],{"__ignoreMap":96},[100,104513,104514,104516,104518,104520,104522,104525,104527,104529,104531,104533,104535,104537,104539,104541,104543,104545,104547,104549,104551,104553],{"class":102,"line":103},[100,104515,17521],{"class":145},[100,104517,17524],{"class":114},[100,104519,170],{"class":118},[100,104521,1280],{"class":205},[100,104523,104524],{"class":209},"image.jpg",[100,104526,1280],{"class":205},[100,104528,126],{"class":118},[100,104530,1274],{"class":205},[100,104532,17540],{"class":209},[100,104534,1280],{"class":205},[100,104536,126],{"class":118},[100,104538,18142],{"class":18141},[100,104540,111],{"class":110},[100,104542,1280],{"class":205},[100,104544,15307],{"class":209},[100,104546,1280],{"class":205},[100,104548,6155],{"class":118},[100,104550,16224],{"class":145},[100,104552,17550],{"class":17549},[100,104554,149],{"class":118},[100,104556,104557,104559,104561,104563,104565,104567],{"class":102,"line":135},[100,104558,17557],{"class":106},[100,104560,111],{"class":110},[100,104562,17550],{"class":17549},[100,104564,290],{"class":118},[100,104566,17566],{"class":178},[100,104568,4734],{"class":118},[14,104570,9750],{},[91,104572,104574],{"className":93,"code":104573,"language":95,"meta":96,"style":96},"with open('image.jpg', 'rb') as file:\n    data = file.read()\n\nprint(type(data))\n",[17,104575,104576,104607,104621,104625],{"__ignoreMap":96},[100,104577,104578,104580,104582,104584,104586,104588,104590,104592,104594,104597,104599,104601,104603,104605],{"class":102,"line":103},[100,104579,17521],{"class":145},[100,104581,17524],{"class":114},[100,104583,170],{"class":118},[100,104585,1280],{"class":205},[100,104587,104524],{"class":209},[100,104589,1280],{"class":205},[100,104591,126],{"class":118},[100,104593,1274],{"class":205},[100,104595,104596],{"class":209},"rb",[100,104598,1280],{"class":205},[100,104600,6155],{"class":118},[100,104602,16224],{"class":145},[100,104604,17550],{"class":17549},[100,104606,149],{"class":118},[100,104608,104609,104611,104613,104615,104617,104619],{"class":102,"line":135},[100,104610,17557],{"class":106},[100,104612,111],{"class":110},[100,104614,17550],{"class":17549},[100,104616,290],{"class":118},[100,104618,17566],{"class":178},[100,104620,4734],{"class":118},[100,104622,104623],{"class":102,"line":142},[100,104624,139],{"emptyLinePlaceholder":138},[100,104626,104627,104629,104631,104633,104635,104637],{"class":102,"line":152},[100,104628,372],{"class":114},[100,104630,170],{"class":118},[100,104632,1250],{"class":191},[100,104634,170],{"class":118},[100,104636,7934],{"class":178},[100,104638,182],{"class":118},[14,104640,218],{},[91,104642,104644],{"className":93,"code":104643,"language":95,"meta":96,"style":96},"\u003Cclass 'bytes'>\n",[17,104645,104646],{"__ignoreMap":96},[100,104647,104648,104650,104652,104654,104656,104658],{"class":102,"line":103},[100,104649,2782],{"class":110},[100,104651,3417],{"class":1077},[100,104653,1274],{"class":205},[100,104655,15319],{"class":209},[100,104657,1280],{"class":205},[100,104659,1980],{"class":110},[77,104661,104663],{"id":104662},"how-to-find-the-right-encoding","How to find the right encoding",[14,104665,104666],{},"The best way is to check where the file came from.",[14,104668,104669],{},"Useful clues:",[40,104671,104672,104675,104680,104685],{},[43,104673,104674],{},"files from modern tools and APIs are often UTF-8",[43,104676,104677,104678],{},"text files from Windows programs often use ",[17,104679,103829],{},[43,104681,104682,104683],{},"some exported files use ",[17,104684,104200],{},[43,104686,104687,104688],{},"older data may use ",[17,104689,103750],{},[14,104691,92871],{},[3282,104693,104694,104697,104700,104703],{},[43,104695,104696],{},"Check how the file was created or exported.",[43,104698,104699],{},"Look at the program that produced it.",[43,104701,104702],{},"Test common encodings used by that source.",[43,104704,104705],{},"If it is a CSV file, check the export settings.",[14,104707,104708,104709,290],{},"CSV files are a common cause of this issue. If that is your case, see ",[295,104710,104712],{"href":104711},"\u002Fhow-to\u002Fhow-to-read-a-csv-file-in-python\u002F","how to read a CSV file in Python",[77,104714,3274],{"id":3273},[14,104716,104717],{},"These quick tests can help you find the problem.",[675,104719,104721],{"id":104720},"_1-print-the-raw-bytes","1. Print the raw bytes",[14,104723,104724],{},"Open the file in binary mode and inspect the start of the file:",[91,104726,104728],{"className":93,"code":104727,"language":95,"meta":96,"style":96},"with open('data.txt', 'rb') as f:\n    print(f.read(40))\n",[17,104729,104730,104760],{"__ignoreMap":96},[100,104731,104732,104734,104736,104738,104740,104742,104744,104746,104748,104750,104752,104754,104756,104758],{"class":102,"line":103},[100,104733,17521],{"class":145},[100,104735,17524],{"class":114},[100,104737,170],{"class":118},[100,104739,1280],{"class":205},[100,104741,18094],{"class":209},[100,104743,1280],{"class":205},[100,104745,126],{"class":118},[100,104747,1274],{"class":205},[100,104749,104596],{"class":209},[100,104751,1280],{"class":205},[100,104753,6155],{"class":118},[100,104755,16224],{"class":145},[100,104757,61684],{"class":106},[100,104759,149],{"class":118},[100,104761,104762,104764,104766,104768,104770,104772,104774,104777],{"class":102,"line":135},[100,104763,200],{"class":114},[100,104765,170],{"class":118},[100,104767,18199],{"class":178},[100,104769,290],{"class":118},[100,104771,17566],{"class":178},[100,104773,170],{"class":118},[100,104775,104776],{"class":122},"40",[100,104778,182],{"class":118},[14,104780,52530],{},[40,104782,104783,104786,104789],{},[43,104784,104785],{},"the file exists",[43,104787,104788],{},"the file contains bytes",[43,104790,104791],{},"the content may not be plain UTF-8 text",[675,104793,104795,104796],{"id":104794},"_2-try-utf-8-sig","2. Try ",[17,104797,104200],{},[91,104799,104801],{"className":93,"code":104800,"language":95,"meta":96,"style":96},"with open('data.txt', 'r', encoding='utf-8-sig') as f:\n    print(f.read())\n",[17,104802,104803,104845],{"__ignoreMap":96},[100,104804,104805,104807,104809,104811,104813,104815,104817,104819,104821,104823,104825,104827,104829,104831,104833,104835,104837,104839,104841,104843],{"class":102,"line":103},[100,104806,17521],{"class":145},[100,104808,17524],{"class":114},[100,104810,170],{"class":118},[100,104812,1280],{"class":205},[100,104814,18094],{"class":209},[100,104816,1280],{"class":205},[100,104818,126],{"class":118},[100,104820,1274],{"class":205},[100,104822,17540],{"class":209},[100,104824,1280],{"class":205},[100,104826,126],{"class":118},[100,104828,18142],{"class":18141},[100,104830,111],{"class":110},[100,104832,1280],{"class":205},[100,104834,104200],{"class":209},[100,104836,1280],{"class":205},[100,104838,6155],{"class":118},[100,104840,16224],{"class":145},[100,104842,61684],{"class":106},[100,104844,149],{"class":118},[100,104846,104847,104849,104851,104853,104855,104857],{"class":102,"line":135},[100,104848,200],{"class":114},[100,104850,170],{"class":118},[100,104852,18199],{"class":178},[100,104854,290],{"class":118},[100,104856,17566],{"class":178},[100,104858,3370],{"class":118},[675,104860,104862,104863],{"id":104861},"_3-try-cp1252","3. Try ",[17,104864,103829],{},[91,104866,104868],{"className":93,"code":104867,"language":95,"meta":96,"style":96},"with open('data.txt', 'r', encoding='cp1252') as f:\n    print(f.read())\n",[17,104869,104870,104912],{"__ignoreMap":96},[100,104871,104872,104874,104876,104878,104880,104882,104884,104886,104888,104890,104892,104894,104896,104898,104900,104902,104904,104906,104908,104910],{"class":102,"line":103},[100,104873,17521],{"class":145},[100,104875,17524],{"class":114},[100,104877,170],{"class":118},[100,104879,1280],{"class":205},[100,104881,18094],{"class":209},[100,104883,1280],{"class":205},[100,104885,126],{"class":118},[100,104887,1274],{"class":205},[100,104889,17540],{"class":209},[100,104891,1280],{"class":205},[100,104893,126],{"class":118},[100,104895,18142],{"class":18141},[100,104897,111],{"class":110},[100,104899,1280],{"class":205},[100,104901,103829],{"class":209},[100,104903,1280],{"class":205},[100,104905,6155],{"class":118},[100,104907,16224],{"class":145},[100,104909,61684],{"class":106},[100,104911,149],{"class":118},[100,104913,104914,104916,104918,104920,104922,104924],{"class":102,"line":135},[100,104915,200],{"class":114},[100,104917,170],{"class":118},[100,104919,18199],{"class":178},[100,104921,290],{"class":118},[100,104923,17566],{"class":178},[100,104925,3370],{"class":118},[675,104927,104929,104930],{"id":104928},"_4-try-latin-1","4. Try ",[17,104931,103750],{},[91,104933,104935],{"className":93,"code":104934,"language":95,"meta":96,"style":96},"with open('data.txt', 'r', encoding='latin-1') as f:\n    print(f.read())\n",[17,104936,104937,104979],{"__ignoreMap":96},[100,104938,104939,104941,104943,104945,104947,104949,104951,104953,104955,104957,104959,104961,104963,104965,104967,104969,104971,104973,104975,104977],{"class":102,"line":103},[100,104940,17521],{"class":145},[100,104942,17524],{"class":114},[100,104944,170],{"class":118},[100,104946,1280],{"class":205},[100,104948,18094],{"class":209},[100,104950,1280],{"class":205},[100,104952,126],{"class":118},[100,104954,1274],{"class":205},[100,104956,17540],{"class":209},[100,104958,1280],{"class":205},[100,104960,126],{"class":118},[100,104962,18142],{"class":18141},[100,104964,111],{"class":110},[100,104966,1280],{"class":205},[100,104968,103750],{"class":209},[100,104970,1280],{"class":205},[100,104972,6155],{"class":118},[100,104974,16224],{"class":145},[100,104976,61684],{"class":106},[100,104978,149],{"class":118},[100,104980,104981,104983,104985,104987,104989,104991],{"class":102,"line":135},[100,104982,200],{"class":114},[100,104984,170],{"class":118},[100,104986,18199],{"class":178},[100,104988,290],{"class":118},[100,104990,17566],{"class":178},[100,104992,3370],{"class":118},[14,104994,104995,104996,290],{},"Also make sure you are opening the correct file path. If Python cannot find the file at all, see ",[295,104997,104998],{"href":17620},"FileNotFoundError: No such file or directory",[77,105000,105002],{"id":105001},"what-not-to-do","What not to do",[14,105004,105005],{},"Common mistakes:",[40,105007,105008,105013,105016,105019],{},[43,105009,1740,105010,105012],{},[17,105011,104287],{}," as your first fix if the text matters.",[43,105014,105015],{},"Do not guess random encodings without checking the data source.",[43,105017,105018],{},"Do not open binary files like images, PDFs, or Excel files in text mode.",[43,105020,105021],{},"Do not assume every CSV file is UTF-8.",[77,105023,1344],{"id":1343},[14,105025,84864],{},[40,105027,105028,105036,105039,105042,105045],{},[43,105029,105030,105031,3178,105033,105035],{},"The file was saved in ",[17,105032,103829],{},[17,105034,103750],{},", not UTF-8.",[43,105037,105038],{},"The file contains mixed or damaged text encoding.",[43,105040,105041],{},"A binary file was opened as if it were a text file.",[43,105043,105044],{},"Bytes from an API or download were decoded with the wrong encoding.",[43,105046,105047],{},"A CSV file came from Excel or another tool using a non-UTF-8 encoding.",[77,105049,1514],{"id":1513},[675,105051,105053],{"id":105052},"why-does-python-try-utf-8-by-default","Why does Python try UTF-8 by default?",[14,105055,105056],{},"UTF-8 is a common text encoding and is the default in many Python setups and tools.",[675,105058,39391,105060,105062],{"id":105059},"should-i-use-errorsignore-to-fix-this",[17,105061,104287],{}," to fix this?",[14,105064,105065],{},"Only if losing some characters is acceptable. It hides the problem and can remove important text.",[675,105067,50591,105069,3266,105071,11353],{"id":105068},"what-is-the-difference-between-latin-1-and-cp1252",[17,105070,103750],{},[17,105072,103829],{},[14,105074,105075,105076,105078,105079,105081],{},"They are similar single-byte encodings, but ",[17,105077,103829],{}," supports some extra printable characters where ",[17,105080,103750],{}," has control codes.",[675,105083,105085],{"id":105084},"why-does-this-happen-more-with-csv-files-from-excel","Why does this happen more with CSV files from Excel?",[14,105087,105088,105089,105091],{},"Some CSV exports use encodings like ",[17,105090,103829],{}," instead of UTF-8, especially on Windows.",[77,105093,1554],{"id":1553},[40,105095,105096,105100,105104,105109,105113],{},[43,105097,105098],{},[295,105099,24736],{"href":18280},[43,105101,105102],{},[295,105103,20591],{"href":20590},[43,105105,105106],{},[295,105107,105108],{"href":104711},"How to read a CSV file in Python",[43,105110,105111],{},[295,105112,22657],{"href":22656},[43,105114,105115],{},[295,105116,105118],{"href":105117},"\u002Ferrors\u002Funicodeencodeerror-in-python-causes-and-fixes\u002F","UnicodeEncodeError in Python: causes and fixes",[1589,105120,105121],{},"html pre.shiki code .sZMiF, html code.shiki .sZMiF{--shiki-light:#E2931D;--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 .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 .su5hD, html code.shiki .su5hD{--shiki-light:#90A4AE;--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 .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 .sptTA, html code.shiki .sptTA{--shiki-light:#6182B8;--shiki-default:#005CC5;--shiki-dark:#79B8FF}html pre.shiki code .s99_P, html code.shiki .s99_P{--shiki-light:#90A4AE;--shiki-light-font-style:italic;--shiki-default:#E36209;--shiki-default-font-style:inherit;--shiki-dark:#FFAB70;--shiki-dark-font-style:inherit}html pre.shiki code .smGrS, html code.shiki .smGrS{--shiki-light:#39ADB5;--shiki-default:#D73A49;--shiki-dark:#F97583}html pre.shiki code .sMMDD, html code.shiki .sMMDD{--shiki-light:#90A4AE;--shiki-default:#E36209;--shiki-dark:#FFAB70}html pre.shiki code .slqww, html code.shiki .slqww{--shiki-light:#6182B8;--shiki-default:#24292E;--shiki-dark:#E1E4E8}html pre.shiki code .sbsja, html code.shiki .sbsja{--shiki-light:#9C3EDA;--shiki-default:#D73A49;--shiki-dark:#F97583}html pre.shiki code .srdBf, html code.shiki .srdBf{--shiki-light:#F76D47;--shiki-default:#005CC5;--shiki-dark:#79B8FF}",{"title":96,"searchDepth":135,"depth":135,"links":105123},[105124,105125,105126,105127,105128,105136,105137,105146,105147,105148,105156],{"id":79,"depth":135,"text":80},{"id":5880,"depth":135,"text":5881},{"id":18289,"depth":135,"text":18290},{"id":11030,"depth":135,"text":11031},{"id":104021,"depth":135,"text":104022,"children":105129},[105130,105131,105133,105135],{"id":104025,"depth":142,"text":104026},{"id":104196,"depth":142,"text":105132},"If the file has a UTF-8 BOM, try utf-8-sig",{"id":104284,"depth":142,"text":105134},"Use errors='ignore' or errors='replace' only if needed",{"id":104500,"depth":142,"text":104501},{"id":104662,"depth":135,"text":104663},{"id":3273,"depth":135,"text":3274,"children":105138},[105139,105140,105142,105144],{"id":104720,"depth":142,"text":104721},{"id":104794,"depth":142,"text":105141},"2. Try utf-8-sig",{"id":104861,"depth":142,"text":105143},"3. Try cp1252",{"id":104928,"depth":142,"text":105145},"4. Try latin-1",{"id":105001,"depth":135,"text":105002},{"id":1343,"depth":135,"text":1344},{"id":1513,"depth":135,"text":1514,"children":105149},[105150,105151,105153,105155],{"id":105052,"depth":142,"text":105053},{"id":105059,"depth":142,"text":105152},"Should I use errors='ignore' to fix this?",{"id":105068,"depth":142,"text":105154},"What is the difference between latin-1 and cp1252?",{"id":105084,"depth":142,"text":105085},{"id":1553,"depth":135,"text":1554},"Master unicodedecodeerror utf 8 codec cant decode byte fix in our comprehensive Python beginner guide.",{},"\u002Ferrors\u002Funicodedecodeerror-utf-8-codec-cant-decode-byte-fix",{"title":103646,"description":105157},"errors\u002Funicodedecodeerror-utf-8-codec-cant-decode-byte-fix","lNmITUUU1_F2-DgT3VtNktjPx_DnxLxbJoRxAzGeLmA",{"id":105164,"title":105165,"body":105166,"description":106687,"extension":1623,"meta":106688,"navigation":138,"path":106689,"seo":106690,"stem":106691,"__hash__":106692},"content\u002Ferrors\u002Funicodeencodeerror-in-python-causes-and-fixes.md","UnicodeEncodeError in Python: Causes and Fixes",{"type":7,"value":105167,"toc":106653},[105168,105171,105177,105179,105196,105199,105201,105280,105288,105290,105295,105298,105321,105324,105336,105342,105347,105349,105352,105372,105375,105377,105382,105432,105434,105466,105468,105492,105495,105545,105547,105562,105565,105567,105571,105574,105650,105653,105661,105668,105671,105724,105726,105748,105751,105755,105758,105766,105816,105823,105825,105874,105879,105887,105890,105979,105981,106008,106010,106023,106026,106030,106036,106063,106069,106072,106086,106090,106093,106097,106099,106112,106115,106119,106122,106146,106150,106155,106186,106188,106201,106210,106214,106220,106222,106275,106277,106342,106348,106354,106357,106380,106383,106394,106398,106401,106423,106426,106444,106449,106451,106456,106485,106489,106492,106495,106505,106508,106526,106529,106547,106550,106568,106570,106574,106579,106584,106590,106594,106597,106600,106604,106607,106610,106616,106619,106622,106624,106650],[10,105169,105165],{"id":105170},"unicodeencodeerror-in-python-causes-and-fixes",[14,105172,105173,105176],{},[17,105174,105175],{},"UnicodeEncodeError"," happens when Python tries to convert text into bytes, but the target encoding cannot represent one or more characters.",[14,105178,50834],{},[40,105180,105181,105184,105187,105190],{},[43,105182,105183],{},"writing text to a file",[43,105185,105186],{},"printing text to a terminal",[43,105188,105189],{},"sending text to another system",[43,105191,45,105192,105195],{},[17,105193,105194],{},".encode()"," with the wrong encoding",[14,105197,105198],{},"For beginners, the fastest fix is usually to use UTF-8 explicitly.",[77,105200,80],{"id":79},[91,105202,105204],{"className":93,"code":105203,"language":95,"meta":96,"style":96},"text = \"café ☕\"\n\nwith open(\"output.txt\", \"w\", encoding=\"utf-8\") as file:\n    file.write(text)\n",[17,105205,105206,105219,105223,105266],{"__ignoreMap":96},[100,105207,105208,105210,105212,105214,105217],{"class":102,"line":103},[100,105209,4331],{"class":106},[100,105211,111],{"class":110},[100,105213,1708],{"class":205},[100,105215,105216],{"class":209},"café ☕",[100,105218,1714],{"class":205},[100,105220,105221],{"class":102,"line":135},[100,105222,139],{"emptyLinePlaceholder":138},[100,105224,105225,105227,105229,105231,105233,105236,105238,105240,105242,105244,105246,105248,105250,105252,105254,105256,105258,105260,105262,105264],{"class":102,"line":142},[100,105226,17521],{"class":145},[100,105228,17524],{"class":114},[100,105230,170],{"class":118},[100,105232,206],{"class":205},[100,105234,105235],{"class":209},"output.txt",[100,105237,206],{"class":205},[100,105239,126],{"class":118},[100,105241,1708],{"class":205},[100,105243,19143],{"class":209},[100,105245,206],{"class":205},[100,105247,126],{"class":118},[100,105249,18142],{"class":18141},[100,105251,111],{"class":110},[100,105253,206],{"class":205},[100,105255,15307],{"class":209},[100,105257,206],{"class":205},[100,105259,6155],{"class":118},[100,105261,16224],{"class":145},[100,105263,17550],{"class":17549},[100,105265,149],{"class":118},[100,105267,105268,105270,105272,105274,105276,105278],{"class":102,"line":152},[100,105269,19170],{"class":17549},[100,105271,290],{"class":118},[100,105273,19175],{"class":178},[100,105275,170],{"class":118},[100,105277,4468],{"class":178},[100,105279,215],{"class":118},[14,105281,105282,105283,3178,105286,290],{},"Most fixes start by using UTF-8 explicitly. The error often happens when Python uses a narrower encoding like ",[17,105284,105285],{},"ascii",[17,105287,103829],{},[77,105289,5881],{"id":5880},[14,105291,105292,105294],{},[17,105293,105175],{}," happens when Python tries to turn a string into bytes using an encoding that cannot represent one or more characters.",[14,105296,105297],{},"A few important ideas:",[40,105299,105300,105305,105308,105311],{},[43,105301,71034,105302,105304],{},[17,105303,3422],{}," stores text as Unicode characters.",[43,105306,105307],{},"An encoding is the rule used to save or send that text as bytes.",[43,105309,105310],{},"Not every encoding supports every character.",[43,105312,3883,105313],{},[40,105314,105315,105318],{},[43,105316,105317],{},"which encoding Python tried to use",[43,105319,105320],{},"which character caused the problem",[14,105322,105323],{},"For example, this text contains characters outside plain ASCII:",[40,105325,105326,105331],{},[43,105327,105328],{},[17,105329,105330],{},"é",[43,105332,105333],{},[17,105334,105335],{},"☕",[14,105337,105338,105339,105341],{},"If Python tries to save that text using ",[17,105340,105285],{},", it fails because ASCII only supports a small set of characters.",[14,105343,105344,105345,290],{},"If you need a refresher on text values in Python, see ",[295,105346,6080],{"href":6079},[77,105348,18290],{"id":18289},[14,105350,105351],{},"This error commonly appears in these situations:",[40,105353,105354,105359,105362,105365],{},[43,105355,105356,105357],{},"Writing text to a file without setting ",[17,105358,19977],{},[43,105360,105361],{},"Printing text to a terminal that does not support the character",[43,105363,105364],{},"Sending text to an API, socket, or external tool with the wrong encoding",[43,105366,315,105367,105369,105370],{},[17,105368,105194],{}," with a limited encoding such as ",[17,105371,105285],{},[14,105373,105374],{},"A very common beginner mistake is assuming Python will always pick the right encoding automatically. Sometimes it does, but sometimes it uses the system default instead.",[77,105376,11031],{"id":11030},[14,105378,105379,105380,89],{},"Here is a simple example that raises ",[17,105381,105175],{},[91,105383,105385],{"className":93,"code":105384,"language":95,"meta":96,"style":96},"text = \"café ☕\"\ndata = text.encode(\"ascii\")\nprint(data)\n",[17,105386,105387,105399,105422],{"__ignoreMap":96},[100,105388,105389,105391,105393,105395,105397],{"class":102,"line":103},[100,105390,4331],{"class":106},[100,105392,111],{"class":110},[100,105394,1708],{"class":205},[100,105396,105216],{"class":209},[100,105398,1714],{"class":205},[100,105400,105401,105403,105405,105407,105409,105412,105414,105416,105418,105420],{"class":102,"line":135},[100,105402,7909],{"class":106},[100,105404,111],{"class":110},[100,105406,4350],{"class":106},[100,105408,290],{"class":118},[100,105410,105411],{"class":178},"encode",[100,105413,170],{"class":118},[100,105415,206],{"class":205},[100,105417,105285],{"class":209},[100,105419,206],{"class":205},[100,105421,215],{"class":118},[100,105423,105424,105426,105428,105430],{"class":102,"line":142},[100,105425,372],{"class":114},[100,105427,170],{"class":118},[100,105429,7934],{"class":178},[100,105431,215],{"class":118},[14,105433,218],{},[91,105435,105437],{"className":93,"code":105436,"language":95,"meta":96,"style":96},"UnicodeEncodeError: 'ascii' codec can't encode character '\\xe9' in position 3: ordinal not in range(128)\n",[17,105438,105439],{"__ignoreMap":96},[100,105440,105441,105443,105445,105447,105449,105451,105453,105455,105458,105460,105463],{"class":102,"line":103},[100,105442,105175],{"class":191},[100,105444,89],{"class":118},[100,105446,1274],{"class":205},[100,105448,105285],{"class":209},[100,105450,1280],{"class":205},[100,105452,103679],{"class":106},[100,105454,1280],{"class":205},[100,105456,105457],{"class":209},"t encode character ",[100,105459,1280],{"class":205},[100,105461,105462],{"class":118},"\\",[100,105464,105465],{"class":71695},"xe9' in position 3: ordinal not in range(128)\n",[14,105467,25013],{},[40,105469,105470,105475,105481,105488],{},[43,105471,105472,105474],{},[17,105473,4468],{}," is a Python string",[43,105476,105477,105480],{},[17,105478,105479],{},".encode(\"ascii\")"," tries to convert it to bytes using ASCII",[43,105482,105483,105484,3178,105486],{},"ASCII cannot represent ",[17,105485,105330],{},[17,105487,105335],{},[43,105489,3860,105490],{},[17,105491,105175],{},[14,105493,105494],{},"Here is the same idea with a character that ASCII can handle:",[91,105496,105498],{"className":93,"code":105497,"language":95,"meta":96,"style":96},"text = \"cafe\"\ndata = text.encode(\"ascii\")\nprint(data)\n",[17,105499,105500,105513,105535],{"__ignoreMap":96},[100,105501,105502,105504,105506,105508,105511],{"class":102,"line":103},[100,105503,4331],{"class":106},[100,105505,111],{"class":110},[100,105507,1708],{"class":205},[100,105509,105510],{"class":209},"cafe",[100,105512,1714],{"class":205},[100,105514,105515,105517,105519,105521,105523,105525,105527,105529,105531,105533],{"class":102,"line":135},[100,105516,7909],{"class":106},[100,105518,111],{"class":110},[100,105520,4350],{"class":106},[100,105522,290],{"class":118},[100,105524,105411],{"class":178},[100,105526,170],{"class":118},[100,105528,206],{"class":205},[100,105530,105285],{"class":209},[100,105532,206],{"class":205},[100,105534,215],{"class":118},[100,105536,105537,105539,105541,105543],{"class":102,"line":142},[100,105538,372],{"class":114},[100,105540,170],{"class":118},[100,105542,7934],{"class":178},[100,105544,215],{"class":118},[14,105546,218],{},[91,105548,105550],{"className":93,"code":105549,"language":95,"meta":96,"style":96},"b'cafe'\n",[17,105551,105552],{"__ignoreMap":96},[100,105553,105554,105556,105558,105560],{"class":102,"line":103},[100,105555,4027],{"class":1077},[100,105557,1280],{"class":205},[100,105559,105510],{"class":209},[100,105561,3925],{"class":205},[14,105563,105564],{},"That works because all characters are valid in ASCII.",[77,105566,6244],{"id":6243},[675,105568,105570],{"id":105569},"use-utf-8-when-opening-files","Use UTF-8 when opening files",[14,105572,105573],{},"When writing text files, explicitly set the encoding.",[91,105575,105576],{"className":93,"code":105203,"language":95,"meta":96,"style":96},[17,105577,105578,105590,105594,105636],{"__ignoreMap":96},[100,105579,105580,105582,105584,105586,105588],{"class":102,"line":103},[100,105581,4331],{"class":106},[100,105583,111],{"class":110},[100,105585,1708],{"class":205},[100,105587,105216],{"class":209},[100,105589,1714],{"class":205},[100,105591,105592],{"class":102,"line":135},[100,105593,139],{"emptyLinePlaceholder":138},[100,105595,105596,105598,105600,105602,105604,105606,105608,105610,105612,105614,105616,105618,105620,105622,105624,105626,105628,105630,105632,105634],{"class":102,"line":142},[100,105597,17521],{"class":145},[100,105599,17524],{"class":114},[100,105601,170],{"class":118},[100,105603,206],{"class":205},[100,105605,105235],{"class":209},[100,105607,206],{"class":205},[100,105609,126],{"class":118},[100,105611,1708],{"class":205},[100,105613,19143],{"class":209},[100,105615,206],{"class":205},[100,105617,126],{"class":118},[100,105619,18142],{"class":18141},[100,105621,111],{"class":110},[100,105623,206],{"class":205},[100,105625,15307],{"class":209},[100,105627,206],{"class":205},[100,105629,6155],{"class":118},[100,105631,16224],{"class":145},[100,105633,17550],{"class":17549},[100,105635,149],{"class":118},[100,105637,105638,105640,105642,105644,105646,105648],{"class":102,"line":152},[100,105639,19170],{"class":17549},[100,105641,290],{"class":118},[100,105643,19175],{"class":178},[100,105645,170],{"class":118},[100,105647,4468],{"class":178},[100,105649,215],{"class":118},[14,105651,105652],{},"This is the safest default for most modern Python programs.",[14,105654,105655,105656,28239,105659,290],{},"If you want to learn more about file handling, see ",[295,105657,62829],{"href":105658},"\u002Fhow-to\u002Fhow-to-write-to-a-file-in-python",[295,105660,24736],{"href":24735},[675,105662,105664,105665],{"id":105663},"use-utf-8-with-encode","Use UTF-8 with ",[17,105666,105667],{},"encode()",[14,105669,105670],{},"If you need bytes, use UTF-8 unless another system requires something else.",[91,105672,105674],{"className":93,"code":105673,"language":95,"meta":96,"style":96},"text = \"café ☕\"\ndata = text.encode(\"utf-8\")\n\nprint(data)\n",[17,105675,105676,105688,105710,105714],{"__ignoreMap":96},[100,105677,105678,105680,105682,105684,105686],{"class":102,"line":103},[100,105679,4331],{"class":106},[100,105681,111],{"class":110},[100,105683,1708],{"class":205},[100,105685,105216],{"class":209},[100,105687,1714],{"class":205},[100,105689,105690,105692,105694,105696,105698,105700,105702,105704,105706,105708],{"class":102,"line":135},[100,105691,7909],{"class":106},[100,105693,111],{"class":110},[100,105695,4350],{"class":106},[100,105697,290],{"class":118},[100,105699,105411],{"class":178},[100,105701,170],{"class":118},[100,105703,206],{"class":205},[100,105705,15307],{"class":209},[100,105707,206],{"class":205},[100,105709,215],{"class":118},[100,105711,105712],{"class":102,"line":142},[100,105713,139],{"emptyLinePlaceholder":138},[100,105715,105716,105718,105720,105722],{"class":102,"line":152},[100,105717,372],{"class":114},[100,105719,170],{"class":118},[100,105721,7934],{"class":178},[100,105723,215],{"class":118},[14,105725,218],{},[91,105727,105729],{"className":93,"code":105728,"language":95,"meta":96,"style":96},"b'caf\\xc3\\xa9 \\xe2\\x98\\x95'\n",[17,105730,105731],{"__ignoreMap":96},[100,105732,105733,105735,105737,105740,105743,105746],{"class":102,"line":103},[100,105734,4027],{"class":1077},[100,105736,1280],{"class":205},[100,105738,105739],{"class":209},"caf",[100,105741,105742],{"class":527},"\\xc3\\xa9",[100,105744,105745],{"class":527}," \\xe2\\x98\\x95",[100,105747,3925],{"class":205},[14,105749,105750],{},"This works because UTF-8 can represent these characters.",[675,105752,105754],{"id":105753},"choose-the-correct-encoding-for-the-destination","Choose the correct encoding for the destination",[14,105756,105757],{},"Sometimes you must match another system.",[14,105759,105760,105761,3178,105763,105765],{},"For example, a tool or file format may require a specific encoding such as ",[17,105762,103829],{},[17,105764,103750],{},". In that case, use the encoding that system expects, but make sure it supports your characters.",[91,105767,105769],{"className":93,"code":105768,"language":95,"meta":96,"style":96},"text = \"café\"\ndata = text.encode(\"cp1252\")\nprint(data)\n",[17,105770,105771,105784,105806],{"__ignoreMap":96},[100,105772,105773,105775,105777,105779,105782],{"class":102,"line":103},[100,105774,4331],{"class":106},[100,105776,111],{"class":110},[100,105778,1708],{"class":205},[100,105780,105781],{"class":209},"café",[100,105783,1714],{"class":205},[100,105785,105786,105788,105790,105792,105794,105796,105798,105800,105802,105804],{"class":102,"line":135},[100,105787,7909],{"class":106},[100,105789,111],{"class":110},[100,105791,4350],{"class":106},[100,105793,290],{"class":118},[100,105795,105411],{"class":178},[100,105797,170],{"class":118},[100,105799,206],{"class":205},[100,105801,103829],{"class":209},[100,105803,206],{"class":205},[100,105805,215],{"class":118},[100,105807,105808,105810,105812,105814],{"class":102,"line":142},[100,105809,372],{"class":114},[100,105811,170],{"class":118},[100,105813,7934],{"class":178},[100,105815,215],{"class":118},[14,105817,2925,105818,105820,105821,290],{},[17,105819,103829],{}," supports ",[17,105822,105330],{},[14,105824,2932],{},[91,105826,105828],{"className":93,"code":105827,"language":95,"meta":96,"style":96},"text = \"café ☕\"\ndata = text.encode(\"cp1252\")\nprint(data)\n",[17,105829,105830,105842,105864],{"__ignoreMap":96},[100,105831,105832,105834,105836,105838,105840],{"class":102,"line":103},[100,105833,4331],{"class":106},[100,105835,111],{"class":110},[100,105837,1708],{"class":205},[100,105839,105216],{"class":209},[100,105841,1714],{"class":205},[100,105843,105844,105846,105848,105850,105852,105854,105856,105858,105860,105862],{"class":102,"line":135},[100,105845,7909],{"class":106},[100,105847,111],{"class":110},[100,105849,4350],{"class":106},[100,105851,290],{"class":118},[100,105853,105411],{"class":178},[100,105855,170],{"class":118},[100,105857,206],{"class":205},[100,105859,103829],{"class":209},[100,105861,206],{"class":205},[100,105863,215],{"class":118},[100,105865,105866,105868,105870,105872],{"class":102,"line":142},[100,105867,372],{"class":114},[100,105869,170],{"class":118},[100,105871,7934],{"class":178},[100,105873,215],{"class":118},[14,105875,105876,105877,290],{},"The coffee cup character is not available in ",[17,105878,103829],{},[675,105880,241,105882,3178,105884,105886],{"id":105881},"use-errorsignore-or-errorsreplace-carefully",[17,105883,104287],{},[17,105885,104290],{}," carefully",[14,105888,105889],{},"If you cannot preserve all characters, Python lets you skip or replace unsupported ones.",[91,105891,105893],{"className":93,"code":105892,"language":95,"meta":96,"style":96},"text = \"café ☕\"\n\nprint(text.encode(\"ascii\", errors=\"ignore\"))\nprint(text.encode(\"ascii\", errors=\"replace\"))\n",[17,105894,105895,105907,105911,105945],{"__ignoreMap":96},[100,105896,105897,105899,105901,105903,105905],{"class":102,"line":103},[100,105898,4331],{"class":106},[100,105900,111],{"class":110},[100,105902,1708],{"class":205},[100,105904,105216],{"class":209},[100,105906,1714],{"class":205},[100,105908,105909],{"class":102,"line":135},[100,105910,139],{"emptyLinePlaceholder":138},[100,105912,105913,105915,105917,105919,105921,105923,105925,105927,105929,105931,105933,105935,105937,105939,105941,105943],{"class":102,"line":142},[100,105914,372],{"class":114},[100,105916,170],{"class":118},[100,105918,4468],{"class":178},[100,105920,290],{"class":118},[100,105922,105411],{"class":178},[100,105924,170],{"class":118},[100,105926,206],{"class":205},[100,105928,105285],{"class":209},[100,105930,206],{"class":205},[100,105932,126],{"class":118},[100,105934,4123],{"class":18141},[100,105936,111],{"class":110},[100,105938,206],{"class":205},[100,105940,104347],{"class":209},[100,105942,206],{"class":205},[100,105944,182],{"class":118},[100,105946,105947,105949,105951,105953,105955,105957,105959,105961,105963,105965,105967,105969,105971,105973,105975,105977],{"class":102,"line":152},[100,105948,372],{"class":114},[100,105950,170],{"class":118},[100,105952,4468],{"class":178},[100,105954,290],{"class":118},[100,105956,105411],{"class":178},[100,105958,170],{"class":118},[100,105960,206],{"class":205},[100,105962,105285],{"class":209},[100,105964,206],{"class":205},[100,105966,126],{"class":118},[100,105968,4123],{"class":18141},[100,105970,111],{"class":110},[100,105972,206],{"class":205},[100,105974,104438],{"class":209},[100,105976,206],{"class":205},[100,105978,182],{"class":118},[14,105980,218],{},[91,105982,105984],{"className":93,"code":105983,"language":95,"meta":96,"style":96},"b'caf '\nb'caf? ?'\n",[17,105985,105986,105997],{"__ignoreMap":96},[100,105987,105988,105990,105992,105995],{"class":102,"line":103},[100,105989,4027],{"class":1077},[100,105991,1280],{"class":205},[100,105993,105994],{"class":209},"caf ",[100,105996,3925],{"class":205},[100,105998,105999,106001,106003,106006],{"class":102,"line":135},[100,106000,4027],{"class":1077},[100,106002,1280],{"class":205},[100,106004,106005],{"class":209},"caf? ?",[100,106007,3925],{"class":205},[14,106009,81130],{},[40,106011,106012,106016],{},[43,106013,106014,104486],{},[17,106015,104347],{},[43,106017,106018,106020,106021],{},[17,106019,104438],{}," changes characters, usually to ",[17,106022,11353],{},[14,106024,106025],{},"These options can be useful, but they may lose data. It is better to use the correct encoding first.",[675,106027,106029],{"id":106028},"check-terminal-or-editor-encoding","Check terminal or editor encoding",[14,106031,106032,106033,106035],{},"Sometimes the error happens during ",[17,106034,13235],{}," instead of file writing.",[91,106037,106039],{"className":93,"code":106038,"language":95,"meta":96,"style":96},"text = \"café ☕\"\nprint(text)\n",[17,106040,106041,106053],{"__ignoreMap":96},[100,106042,106043,106045,106047,106049,106051],{"class":102,"line":103},[100,106044,4331],{"class":106},[100,106046,111],{"class":110},[100,106048,1708],{"class":205},[100,106050,105216],{"class":209},[100,106052,1714],{"class":205},[100,106054,106055,106057,106059,106061],{"class":102,"line":135},[100,106056,372],{"class":114},[100,106058,170],{"class":118},[100,106060,4468],{"class":178},[100,106062,215],{"class":118},[14,106064,106065,106066,106068],{},"If this raises ",[17,106067,105175],{},", the problem may be your terminal output encoding, not your string.",[14,106070,106071],{},"In that case:",[40,106073,106074,106077,106080,106083],{},[43,106075,106076],{},"check your terminal settings",[43,106078,106079],{},"check Python's stdout encoding",[43,106081,106082],{},"try running in a UTF-8 terminal",[43,106084,106085],{},"make sure your editor and environment are using UTF-8",[77,106087,106089],{"id":106088},"how-to-debug-the-problem","How to debug the problem",[14,106091,106092],{},"When you see this error, use a simple process.",[675,106094,106096],{"id":106095},"_1-read-the-full-error-message","1. Read the full error message",[14,106098,25531],{},[40,106100,106101,106106,106109],{},[43,106102,106103,106104],{},"the encoding name, such as ",[17,106105,105285],{},[43,106107,106108],{},"the character that failed",[43,106110,106111],{},"the position where it happened",[14,106113,106114],{},"That tells you what Python was trying to do.",[675,106116,106118],{"id":106117},"_2-find-where-python-is-converting-text-to-bytes","2. Find where Python is converting text to bytes",[14,106120,106121],{},"Look for code like:",[40,106123,106124,106129,106137,106143],{},[43,106125,106126],{},[17,106127,106128],{},"text.encode(...)",[43,106130,106131,106133,106134],{},[17,106132,22217],{}," without ",[17,106135,106136],{},"encoding=...",[43,106138,106139,106142],{},[17,106140,106141],{},"print(...)"," in a limited terminal",[43,106144,106145],{},"library calls that send text to another system",[675,106147,106149],{"id":106148},"_3-inspect-the-text","3. Inspect the text",[14,106151,106152,106153,290],{},"If the problem is hard to see, print the text with ",[17,106154,99698],{},[91,106156,106158],{"className":93,"code":106157,"language":95,"meta":96,"style":96},"text = \"café ☕\"\nprint(repr(text))\n",[17,106159,106160,106172],{"__ignoreMap":96},[100,106161,106162,106164,106166,106168,106170],{"class":102,"line":103},[100,106163,4331],{"class":106},[100,106165,111],{"class":110},[100,106167,1708],{"class":205},[100,106169,105216],{"class":209},[100,106171,1714],{"class":205},[100,106173,106174,106176,106178,106180,106182,106184],{"class":102,"line":135},[100,106175,372],{"class":114},[100,106177,170],{"class":118},[100,106179,12228],{"class":114},[100,106181,170],{"class":118},[100,106183,4468],{"class":178},[100,106185,182],{"class":118},[14,106187,218],{},[91,106189,106191],{"className":93,"code":106190,"language":95,"meta":96,"style":96},"'café ☕'\n",[17,106192,106193],{"__ignoreMap":96},[100,106194,106195,106197,106199],{"class":102,"line":103},[100,106196,1280],{"class":3553},[100,106198,105216],{"class":3556},[100,106200,3925],{"class":3553},[14,106202,106203,106204,5870,106206,106209],{},"This helps you spot hidden characters such as ",[17,106205,20187],{},[17,106207,106208],{},"\\t",", or unexpected Unicode characters.",[675,106211,106213],{"id":106212},"_4-check-file-operations","4. Check file operations",[14,106215,106216,106217,106219],{},"If you are writing a file, review your ",[17,106218,18032],{}," call.",[14,106221,36873],{},[91,106223,106225],{"className":93,"code":106224,"language":95,"meta":96,"style":96},"with open(\"output.txt\", \"w\") as file:\n    file.write(\"café ☕\")\n",[17,106226,106227,106257],{"__ignoreMap":96},[100,106228,106229,106231,106233,106235,106237,106239,106241,106243,106245,106247,106249,106251,106253,106255],{"class":102,"line":103},[100,106230,17521],{"class":145},[100,106232,17524],{"class":114},[100,106234,170],{"class":118},[100,106236,206],{"class":205},[100,106238,105235],{"class":209},[100,106240,206],{"class":205},[100,106242,126],{"class":118},[100,106244,1708],{"class":205},[100,106246,19143],{"class":209},[100,106248,206],{"class":205},[100,106250,6155],{"class":118},[100,106252,16224],{"class":145},[100,106254,17550],{"class":17549},[100,106256,149],{"class":118},[100,106258,106259,106261,106263,106265,106267,106269,106271,106273],{"class":102,"line":135},[100,106260,19170],{"class":17549},[100,106262,290],{"class":118},[100,106264,19175],{"class":178},[100,106266,170],{"class":118},[100,106268,206],{"class":205},[100,106270,105216],{"class":209},[100,106272,206],{"class":205},[100,106274,215],{"class":118},[14,106276,48022],{},[91,106278,106280],{"className":93,"code":106279,"language":95,"meta":96,"style":96},"with open(\"output.txt\", \"w\", encoding=\"utf-8\") as file:\n    file.write(\"café ☕\")\n",[17,106281,106282,106324],{"__ignoreMap":96},[100,106283,106284,106286,106288,106290,106292,106294,106296,106298,106300,106302,106304,106306,106308,106310,106312,106314,106316,106318,106320,106322],{"class":102,"line":103},[100,106285,17521],{"class":145},[100,106287,17524],{"class":114},[100,106289,170],{"class":118},[100,106291,206],{"class":205},[100,106293,105235],{"class":209},[100,106295,206],{"class":205},[100,106297,126],{"class":118},[100,106299,1708],{"class":205},[100,106301,19143],{"class":209},[100,106303,206],{"class":205},[100,106305,126],{"class":118},[100,106307,18142],{"class":18141},[100,106309,111],{"class":110},[100,106311,206],{"class":205},[100,106313,15307],{"class":209},[100,106315,206],{"class":205},[100,106317,6155],{"class":118},[100,106319,16224],{"class":145},[100,106321,17550],{"class":17549},[100,106323,149],{"class":118},[100,106325,106326,106328,106330,106332,106334,106336,106338,106340],{"class":102,"line":135},[100,106327,19170],{"class":17549},[100,106329,290],{"class":118},[100,106331,19175],{"class":178},[100,106333,170],{"class":118},[100,106335,206],{"class":205},[100,106337,105216],{"class":209},[100,106339,206],{"class":205},[100,106341,215],{"class":118},[14,106343,106344,106345,290],{},"If you also read text from files, see ",[295,106346,21207],{"href":106347},"\u002Fhow-to\u002Fhow-to-read-a-file-in-python",[675,106349,106351,106352,1197],{"id":106350},"_5-check-explicit-encode-calls","5. Check explicit ",[17,106353,105194],{},[14,106355,106356],{},"If you wrote something like this:",[91,106358,106360],{"className":93,"code":106359,"language":95,"meta":96,"style":96},"text.encode(\"ascii\")\n",[17,106361,106362],{"__ignoreMap":96},[100,106363,106364,106366,106368,106370,106372,106374,106376,106378],{"class":102,"line":103},[100,106365,4468],{"class":106},[100,106367,290],{"class":118},[100,106369,105411],{"class":178},[100,106371,170],{"class":118},[100,106373,206],{"class":205},[100,106375,105285],{"class":209},[100,106377,206],{"class":205},[100,106379,215],{"class":118},[14,106381,106382],{},"ask yourself:",[40,106384,106385,106388,106391],{},[43,106386,106387],{},"Do I really need ASCII?",[43,106389,106390],{},"Should this be UTF-8 instead?",[43,106392,106393],{},"Does the other system require a specific encoding?",[77,106395,106397],{"id":106396},"safe-beginner-guidance","Safe beginner guidance",[14,106399,106400],{},"For most modern Python code, these rules are safe and practical:",[40,106402,106403,106406,106414,106417],{},[43,106404,106405],{},"Use UTF-8 as your default encoding",[43,106407,106408,106409,3266,106411,106413],{},"Do not mix ",[17,106410,3422],{},[17,106412,15319],{}," unless you understand the difference",[43,106415,106416],{},"Keep track of encoding from input to output",[43,106418,106419,106420,106422],{},"Avoid using ",[17,106421,105285],{}," unless you specifically need it",[14,106424,106425],{},"A useful mental model:",[40,106427,106428,106433,106438,106441],{},[43,106429,106430,106432],{},[17,106431,3422],{}," = text",[43,106434,106435,106437],{},[17,106436,15319],{}," = raw binary data",[43,106439,106440],{},"encoding = turning text into bytes",[43,106442,106443],{},"decoding = turning bytes into text",[14,106445,106446,106447,290],{},"If you want to understand Python errors more generally, see ",[295,106448,1587],{"href":1586},[77,106450,7117],{"id":7116},[14,106452,106453,106454,89],{},"These are common causes of ",[17,106455,105175],{},[40,106457,106458,106464,106470,106473,106476],{},[43,106459,1357,106460,106463],{},[17,106461,106462],{},"text.encode('ascii')"," on text that contains accented letters or emoji",[43,106465,106466,106467],{},"Writing a file without ",[17,106468,106469],{},"encoding='utf-8'",[43,106471,106472],{},"Running Python in a terminal with a limited default encoding",[43,106474,106475],{},"Passing text to a library or tool that expects a different encoding",[43,106477,106478,106479,106481,106482,106484],{},"Confusing ",[17,106480,3422],{}," objects with ",[17,106483,15319],{}," objects",[77,106486,106488],{"id":106487},"useful-commands-for-debugging","Useful commands for debugging",[14,106490,106491],{},"These commands can help you inspect your Python environment.",[14,106493,106494],{},"Check your Python version:",[91,106496,106497],{"className":10352,"code":10353,"language":10354,"meta":96,"style":96},[17,106498,106499],{"__ignoreMap":96},[100,106500,106501,106503],{"class":102,"line":103},[100,106502,95],{"class":10361},[100,106504,10365],{"class":10364},[14,106506,106507],{},"Check your terminal output encoding:",[91,106509,106511],{"className":10352,"code":106510,"language":10354,"meta":96,"style":96},"python -c \"import sys; print(sys.stdout.encoding)\"\n",[17,106512,106513],{"__ignoreMap":96},[100,106514,106515,106517,106519,106521,106524],{"class":102,"line":103},[100,106516,95],{"class":10361},[100,106518,10450],{"class":10364},[100,106520,1708],{"class":205},[100,106522,106523],{"class":209},"import sys; print(sys.stdout.encoding)",[100,106525,1714],{"class":205},[14,106527,106528],{},"Check your system's preferred encoding:",[91,106530,106532],{"className":10352,"code":106531,"language":10354,"meta":96,"style":96},"python -c \"import locale; print(locale.getpreferredencoding(False))\"\n",[17,106533,106534],{"__ignoreMap":96},[100,106535,106536,106538,106540,106542,106545],{"class":102,"line":103},[100,106537,95],{"class":10361},[100,106539,10450],{"class":10364},[100,106541,1708],{"class":205},[100,106543,106544],{"class":209},"import locale; print(locale.getpreferredencoding(False))",[100,106546,1714],{"class":205},[14,106548,106549],{},"Test a Unicode string and UTF-8 encoding:",[91,106551,106553],{"className":10352,"code":106552,"language":10354,"meta":96,"style":96},"python -c \"text='café ☕'; print(repr(text)); print(text.encode('utf-8'))\"\n",[17,106554,106555],{"__ignoreMap":96},[100,106556,106557,106559,106561,106563,106566],{"class":102,"line":103},[100,106558,95],{"class":10361},[100,106560,10450],{"class":10364},[100,106562,1708],{"class":205},[100,106564,106565],{"class":209},"text='café ☕'; print(repr(text)); print(text.encode('utf-8'))",[100,106567,1714],{"class":205},[77,106569,1514],{"id":1513},[675,106571,106573],{"id":106572},"what-is-the-difference-between-unicodeencodeerror-and-unicodedecodeerror","What is the difference between UnicodeEncodeError and UnicodeDecodeError?",[14,106575,106576,106578],{},[17,106577,105175],{}," happens when Python turns text into bytes.",[14,106580,106581,106583],{},[17,106582,103668],{}," happens when Python turns bytes into text.",[14,106585,106586,106587,290],{},"If that is your problem instead, see ",[295,106588,106589],{"href":105159},"UnicodeDecodeError: utf-8 codec can't decode byte",[675,106591,106593],{"id":106592},"why-does-this-happen-only-on-some-computers","Why does this happen only on some computers?",[14,106595,106596],{},"Different systems and terminals may use different default encodings.",[14,106598,106599],{},"One machine may default to UTF-8, while another uses a more limited encoding. That is why the same code may work on one computer and fail on another.",[675,106601,106603],{"id":106602},"should-i-always-use-utf-8","Should I always use UTF-8?",[14,106605,106606],{},"Usually yes.",[14,106608,106609],{},"UTF-8 is the safest default for modern Python code unless you must match a specific external format.",[675,106611,106613,106614,11353],{"id":106612},"can-i-fix-this-with-errorsignore","Can I fix this with ",[17,106615,104287],{},[14,106617,106618],{},"Yes, but it can silently remove characters.",[14,106620,106621],{},"It is better to use the correct encoding first.",[77,106623,1554],{"id":1553},[40,106625,106626,106630,106634,106638,106642,106646],{},[43,106627,106628],{},[295,106629,6080],{"href":6079},[43,106631,106632],{},[295,106633,24736],{"href":24735},[43,106635,106636],{},[295,106637,63612],{"href":105658},[43,106639,106640],{},[295,106641,20591],{"href":106347},[43,106643,106644],{},[295,106645,106589],{"href":105159},[43,106647,106648],{},[295,106649,1587],{"href":1586},[1589,106651,106652],{},"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 .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 .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 .s99_P, html code.shiki .s99_P{--shiki-light:#90A4AE;--shiki-light-font-style:italic;--shiki-default:#E36209;--shiki-default-font-style:inherit;--shiki-dark:#FFAB70;--shiki-dark-font-style:inherit}html pre.shiki code .sMMDD, html code.shiki .sMMDD{--shiki-light:#90A4AE;--shiki-default:#E36209;--shiki-dark:#FFAB70}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 .srjyR, html code.shiki .srjyR{--shiki-light:#90A4AE;--shiki-light-font-style:inherit;--shiki-default:#B31D28;--shiki-default-font-style:italic;--shiki-dark:#FDAEB7;--shiki-dark-font-style:italic}html pre.shiki code .sbsja, html code.shiki .sbsja{--shiki-light:#9C3EDA;--shiki-default:#D73A49;--shiki-dark:#F97583}html pre.shiki code .s_hVV, html code.shiki .s_hVV{--shiki-light:#90A4AE;--shiki-default:#005CC5;--shiki-dark:#79B8FF}html pre.shiki code .s2W-s, html code.shiki .s2W-s{--shiki-light:#39ADB5;--shiki-light-font-style:italic;--shiki-default:#032F62;--shiki-default-font-style:inherit;--shiki-dark:#9ECBFF;--shiki-dark-font-style:inherit}html pre.shiki code .sithA, html code.shiki .sithA{--shiki-light:#90A4AE;--shiki-light-font-style:italic;--shiki-default:#032F62;--shiki-default-font-style:inherit;--shiki-dark:#9ECBFF;--shiki-dark-font-style:inherit}html pre.shiki code .sbgvK, html code.shiki .sbgvK{--shiki-light:#E2931D;--shiki-default:#6F42C1;--shiki-dark:#B392F0}html pre.shiki code .stzsN, html code.shiki .stzsN{--shiki-light:#91B859;--shiki-default:#005CC5;--shiki-dark:#79B8FF}",{"title":96,"searchDepth":135,"depth":135,"links":106654},[106655,106656,106657,106658,106659,106668,106676,106677,106678,106679,106686],{"id":79,"depth":135,"text":80},{"id":5880,"depth":135,"text":5881},{"id":18289,"depth":135,"text":18290},{"id":11030,"depth":135,"text":11031},{"id":6243,"depth":135,"text":6244,"children":106660},[106661,106662,106664,106665,106667],{"id":105569,"depth":142,"text":105570},{"id":105663,"depth":142,"text":106663},"Use UTF-8 with encode()",{"id":105753,"depth":142,"text":105754},{"id":105881,"depth":142,"text":106666},"Use errors='ignore' or errors='replace' carefully",{"id":106028,"depth":142,"text":106029},{"id":106088,"depth":135,"text":106089,"children":106669},[106670,106671,106672,106673,106674],{"id":106095,"depth":142,"text":106096},{"id":106117,"depth":142,"text":106118},{"id":106148,"depth":142,"text":106149},{"id":106212,"depth":142,"text":106213},{"id":106350,"depth":142,"text":106675},"5. Check explicit .encode() calls",{"id":106396,"depth":135,"text":106397},{"id":7116,"depth":135,"text":7117},{"id":106487,"depth":135,"text":106488},{"id":1513,"depth":135,"text":1514,"children":106680},[106681,106682,106683,106684],{"id":106572,"depth":142,"text":106573},{"id":106592,"depth":142,"text":106593},{"id":106602,"depth":142,"text":106603},{"id":106612,"depth":142,"text":106685},"Can I fix this with errors='ignore'?",{"id":1553,"depth":135,"text":1554},"Master unicodeencodeerror in python causes and fixes in our comprehensive Python beginner guide.",{},"\u002Ferrors\u002Funicodeencodeerror-in-python-causes-and-fixes",{"title":105165,"description":106687},"errors\u002Funicodeencodeerror-in-python-causes-and-fixes","yQHm5GuQiF78wRWhGJbNxWx8xkkSLLIzN87yJuMOm9Q",{"id":106694,"title":106695,"body":106696,"description":107829,"extension":1623,"meta":107830,"navigation":138,"path":107831,"seo":107832,"stem":107833,"__hash__":107834},"content\u002Ferrors\u002Fvalueerror-could-not-convert-string-to-float-fix.md","ValueError: could not convert string to float (Fix)",{"type":7,"value":106697,"toc":107808},[106698,106701,106707,106710,106712,106823,106831,106833,106839,106844,106861,106864,106881,106888,106890,106893,106911,106917,106919,106921,106952,106954,106983,106988,106990,106993,107029,107032,107059,107061,107064,107066,107089,107094,107098,107101,107104,107199,107201,107210,107213,107238,107247,107251,107254,107257,107353,107356,107361,107365,107370,107373,107461,107464,107475,107483,107485,107490,107492,107567,107570,107592,107594,107670,107672,107701,107704,107706,107709,107733,107735,107741,107749,107753,107758,107762,107765,107769,107772,107774,107805],[10,106699,106695],{"id":106700},"valueerror-could-not-convert-string-to-float-fix",[14,106702,106703,106704,106706],{},"This error happens when you call ",[17,106705,59778],{}," with text that is not a valid number.",[14,106708,106709],{},"It often appears when you read input from a user, a file, a CSV, or an API. The fix is usually to inspect the string, clean it if needed, and handle bad input safely.",[77,106711,80],{"id":79},[91,106713,106715],{"className":93,"code":106714,"language":95,"meta":96,"style":96},"text = input(\"Enter a number: \").strip()\ntext = text.replace(\",\", \"\")\n\ntry:\n    number = float(text)\n    print(number)\nexcept ValueError:\n    print(\"Please enter a valid number, such as 3.14 or 10\")\n",[17,106716,106717,106739,106765,106769,106775,106790,106800,106808],{"__ignoreMap":96},[100,106718,106719,106721,106723,106725,106727,106729,106731,106733,106735,106737],{"class":102,"line":103},[100,106720,4331],{"class":106},[100,106722,111],{"class":110},[100,106724,7221],{"class":114},[100,106726,170],{"class":118},[100,106728,206],{"class":205},[100,106730,6217],{"class":209},[100,106732,206],{"class":205},[100,106734,5799],{"class":118},[100,106736,6235],{"class":178},[100,106738,4734],{"class":118},[100,106740,106741,106743,106745,106747,106749,106751,106753,106755,106757,106759,106761,106763],{"class":102,"line":135},[100,106742,4331],{"class":106},[100,106744,111],{"class":110},[100,106746,4350],{"class":106},[100,106748,290],{"class":118},[100,106750,104438],{"class":178},[100,106752,170],{"class":118},[100,106754,206],{"class":205},[100,106756,126],{"class":209},[100,106758,206],{"class":205},[100,106760,126],{"class":118},[100,106762,11901],{"class":205},[100,106764,215],{"class":118},[100,106766,106767],{"class":102,"line":142},[100,106768,139],{"emptyLinePlaceholder":138},[100,106770,106771,106773],{"class":102,"line":152},[100,106772,146],{"class":145},[100,106774,149],{"class":118},[100,106776,106777,106779,106781,106784,106786,106788],{"class":102,"line":164},[100,106778,98700],{"class":106},[100,106780,111],{"class":110},[100,106782,106783],{"class":191}," float",[100,106785,170],{"class":118},[100,106787,4468],{"class":178},[100,106789,215],{"class":118},[100,106791,106792,106794,106796,106798],{"class":102,"line":185},[100,106793,200],{"class":114},[100,106795,170],{"class":118},[100,106797,631],{"class":178},[100,106799,215],{"class":118},[100,106801,106802,106804,106806],{"class":102,"line":197},[100,106803,188],{"class":145},[100,106805,2794],{"class":191},[100,106807,149],{"class":118},[100,106809,106810,106812,106814,106816,106819,106821],{"class":102,"line":771},[100,106811,200],{"class":114},[100,106813,170],{"class":118},[100,106815,206],{"class":205},[100,106817,106818],{"class":209},"Please enter a valid number, such as 3.14 or 10",[100,106820,206],{"class":205},[100,106822,215],{"class":118},[14,106824,241,106825,106827,106828,106830],{},[17,106826,8739],{}," to remove extra spaces, clean the input if needed, and catch ",[17,106829,3483],{}," when the text is not a valid decimal number.",[77,106832,5881],{"id":5880},[14,106834,106835,106836,106838],{},"Python raises this error when ",[17,106837,59778],{}," receives a string that does not match a valid floating-point number.",[14,106840,106841,106843],{},[17,106842,59778],{}," can convert strings like:",[40,106845,106846,106851,106856],{},[43,106847,106848],{},[17,106849,106850],{},"'3.14'",[43,106852,106853],{},[17,106854,106855],{},"'10'",[43,106857,106858],{},[17,106859,106860],{},"' 5.0 '",[14,106862,106863],{},"But it cannot convert strings like:",[40,106865,106866,106871,106876],{},[43,106867,106868],{},[17,106869,106870],{},"'hello'",[43,106872,106873],{},[17,106874,106875],{},"'3,14'",[43,106877,106878],{},[17,106879,106880],{},"''",[14,106882,106883,106884,290],{},"If you need a basic explanation of how conversion works, see the ",[295,106885,1572,106886,1576],{"href":60491},[17,106887,59778],{},[77,106889,26289],{"id":26288},[14,106891,106892],{},"This error commonly appears when you are:",[40,106894,106895,106902,106905,106908],{},[43,106896,106897,106898],{},"Converting user input from ",[295,106899,106900],{"href":85586},[17,106901,14906],{},[43,106903,106904],{},"Reading numbers from a file or CSV",[43,106906,106907],{},"Parsing text from an API or web page",[43,106909,106910],{},"Trying to convert strings that contain symbols, commas, or words",[14,106912,106913,106914,106916],{},"In short, the problem is usually not ",[17,106915,59778],{}," itself. The problem is the string you passed into it.",[77,106918,11031],{"id":11030},[14,106920,18361],{},[91,106922,106924],{"className":93,"code":106923,"language":95,"meta":96,"style":96},"value = \"abc\"\nnumber = float(value)\n",[17,106925,106926,106938],{"__ignoreMap":96},[100,106927,106928,106930,106932,106934,106936],{"class":102,"line":103},[100,106929,3318],{"class":106},[100,106931,111],{"class":110},[100,106933,1708],{"class":205},[100,106935,83226],{"class":209},[100,106937,1714],{"class":205},[100,106939,106940,106942,106944,106946,106948,106950],{"class":102,"line":135},[100,106941,1901],{"class":106},[100,106943,111],{"class":110},[100,106945,106783],{"class":191},[100,106947,170],{"class":118},[100,106949,757],{"class":178},[100,106951,215],{"class":118},[14,106953,218],{},[91,106955,106957],{"className":93,"code":106956,"language":95,"meta":96,"style":96},"ValueError: could not convert string to float: 'abc'\n",[17,106958,106959],{"__ignoreMap":96},[100,106960,106961,106963,106965,106968,106970,106973,106975,106977,106979,106981],{"class":102,"line":103},[100,106962,3483],{"class":191},[100,106964,89],{"class":118},[100,106966,106967],{"class":106}," could ",[100,106969,1059],{"class":110},[100,106971,106972],{"class":106}," convert string to ",[100,106974,60476],{"class":191},[100,106976,89],{"class":118},[100,106978,1274],{"class":205},[100,106980,83226],{"class":209},[100,106982,3925],{"class":205},[14,106984,7194,106985,106987],{},[17,106986,99181],{}," is text, not a valid number format.",[77,106989,1344],{"id":1343},[14,106991,106992],{},"These are some of the most common reasons this error happens:",[40,106994,106995,107001,107006,107014,107020,107026],{},[43,106996,106997,106998],{},"Letters inside the string, such as ",[17,106999,107000],{},"'12a'",[43,107002,107003,107004],{},"Empty strings, such as ",[17,107005,106880],{},[43,107007,107008,107009,3178,107012],{},"Commas used as separators, such as ",[17,107010,107011],{},"'1,234'",[17,107013,106875],{},[43,107015,107016,107017],{},"Currency symbols, such as ",[17,107018,107019],{},"'$19.99'",[43,107021,107022,107023],{},"Trailing text, such as ",[17,107024,107025],{},"'4.2kg'",[43,107027,107028],{},"Input with only spaces",[14,107030,107031],{},"Other common cases include:",[40,107033,107034,107041,107044,107053,107056],{},[43,107035,315,107036,107038,107039],{},[17,107037,59778],{}," on text like ",[17,107040,106870],{},[43,107042,107043],{},"Trying to convert an empty string",[43,107045,107046,107047,3178,107050],{},"Including symbols like ",[17,107048,107049],{},"'$'",[17,107051,107052],{},"'%'",[43,107054,107055],{},"Reading file data with hidden newline characters or extra text",[43,107057,107058],{},"Assuming all user input is numeric",[77,107060,6244],{"id":6243},[14,107062,107063],{},"The best fix depends on where the string comes from.",[14,107065,59464],{},[40,107067,107068,107071,107076,107079,107084],{},[43,107069,107070],{},"Check the original string before converting it",[43,107072,241,107073,107075],{},[17,107074,8739],{}," to remove leading and trailing spaces",[43,107077,107078],{},"Remove formatting characters only if you expect them",[43,107080,241,107081,107083],{},[17,107082,244],{}," to handle invalid input safely",[43,107085,107086,107087],{},"Validate user input before calling ",[17,107088,59778],{},[14,107090,107091,107092,290],{},"If your goal is general conversion, see ",[295,107093,82080],{"href":82079},[77,107095,107097],{"id":107096},"fix-option-1-clean-the-string-first","Fix option 1: Clean the string first",[14,107099,107100],{},"This is useful when the number may include known formatting characters such as spaces, commas, or a currency symbol.",[14,107102,107103],{},"Only remove characters you are sure should not be there.",[91,107105,107107],{"className":93,"code":107106,"language":95,"meta":96,"style":96},"value = \" $1,234.50 \\n\"\ncleaned = value.strip().replace(\"$\", \"\").replace(\",\", \"\")\nnumber = float(cleaned)\n\nprint(number)\n",[17,107108,107109,107124,107171,107185,107189],{"__ignoreMap":96},[100,107110,107111,107113,107115,107117,107120,107122],{"class":102,"line":103},[100,107112,3318],{"class":106},[100,107114,111],{"class":110},[100,107116,1708],{"class":205},[100,107118,107119],{"class":209}," $1,234.50 ",[100,107121,20187],{"class":527},[100,107123,1714],{"class":205},[100,107125,107126,107128,107130,107132,107134,107136,107138,107140,107142,107144,107147,107149,107151,107153,107155,107157,107159,107161,107163,107165,107167,107169],{"class":102,"line":135},[100,107127,8872],{"class":106},[100,107129,111],{"class":110},[100,107131,6747],{"class":106},[100,107133,290],{"class":118},[100,107135,6235],{"class":178},[100,107137,8886],{"class":118},[100,107139,104438],{"class":178},[100,107141,170],{"class":118},[100,107143,206],{"class":205},[100,107145,107146],{"class":209},"$",[100,107148,206],{"class":205},[100,107150,126],{"class":118},[100,107152,11901],{"class":205},[100,107154,5799],{"class":118},[100,107156,104438],{"class":178},[100,107158,170],{"class":118},[100,107160,206],{"class":205},[100,107162,126],{"class":209},[100,107164,206],{"class":205},[100,107166,126],{"class":118},[100,107168,11901],{"class":205},[100,107170,215],{"class":118},[100,107172,107173,107175,107177,107179,107181,107183],{"class":102,"line":142},[100,107174,1901],{"class":106},[100,107176,111],{"class":110},[100,107178,106783],{"class":191},[100,107180,170],{"class":118},[100,107182,8915],{"class":178},[100,107184,215],{"class":118},[100,107186,107187],{"class":102,"line":152},[100,107188,139],{"emptyLinePlaceholder":138},[100,107190,107191,107193,107195,107197],{"class":102,"line":164},[100,107192,372],{"class":114},[100,107194,170],{"class":118},[100,107196,631],{"class":178},[100,107198,215],{"class":118},[14,107200,218],{},[91,107202,107204],{"className":93,"code":107203,"language":95,"meta":96,"style":96},"1234.5\n",[17,107205,107206],{"__ignoreMap":96},[100,107207,107208],{"class":102,"line":103},[100,107209,107203],{"class":122},[14,107211,107212],{},"What this code does:",[40,107214,107215,107220,107226,107232],{},[43,107216,107217,107219],{},[17,107218,8739],{}," removes spaces and newline characters at the start and end",[43,107221,107222,107225],{},[17,107223,107224],{},"replace(\"$\", \"\")"," removes the dollar sign",[43,107227,107228,107231],{},[17,107229,107230],{},"replace(\",\", \"\")"," removes the thousands separator",[43,107233,107234,107237],{},[17,107235,107236],{},"float(cleaned)"," converts the cleaned string",[14,107239,107240,107241,107243,107244,107246],{},"Be careful with values like ",[17,107242,106875],{},". In normal Python usage, ",[17,107245,59778],{}," expects a dot for decimals, not a comma.",[77,107248,107250],{"id":107249},"fix-option-2-use-try-except","Fix option 2: Use try-except",[14,107252,107253],{},"This is the safest choice when the input may be invalid.",[14,107255,107256],{},"It prevents your program from crashing and lets you show a helpful message.",[91,107258,107260],{"className":93,"code":107259,"language":95,"meta":96,"style":96},"value = input(\"Enter a price: \").strip()\n\ntry:\n    price = float(value)\n    print(\"You entered:\", price)\nexcept ValueError:\n    print(\"That is not a valid number.\")\n",[17,107261,107262,107285,107289,107295,107310,107330,107338],{"__ignoreMap":96},[100,107263,107264,107266,107268,107270,107272,107274,107277,107279,107281,107283],{"class":102,"line":103},[100,107265,3318],{"class":106},[100,107267,111],{"class":110},[100,107269,7221],{"class":114},[100,107271,170],{"class":118},[100,107273,206],{"class":205},[100,107275,107276],{"class":209},"Enter a price: ",[100,107278,206],{"class":205},[100,107280,5799],{"class":118},[100,107282,6235],{"class":178},[100,107284,4734],{"class":118},[100,107286,107287],{"class":102,"line":135},[100,107288,139],{"emptyLinePlaceholder":138},[100,107290,107291,107293],{"class":102,"line":142},[100,107292,146],{"class":145},[100,107294,149],{"class":118},[100,107296,107297,107300,107302,107304,107306,107308],{"class":102,"line":152},[100,107298,107299],{"class":106},"    price ",[100,107301,111],{"class":110},[100,107303,106783],{"class":191},[100,107305,170],{"class":118},[100,107307,757],{"class":178},[100,107309,215],{"class":118},[100,107311,107312,107314,107316,107318,107321,107323,107325,107328],{"class":102,"line":164},[100,107313,200],{"class":114},[100,107315,170],{"class":118},[100,107317,206],{"class":205},[100,107319,107320],{"class":209},"You entered:",[100,107322,206],{"class":205},[100,107324,126],{"class":118},[100,107326,107327],{"class":178}," price",[100,107329,215],{"class":118},[100,107331,107332,107334,107336],{"class":102,"line":185},[100,107333,188],{"class":145},[100,107335,2794],{"class":191},[100,107337,149],{"class":118},[100,107339,107340,107342,107344,107346,107349,107351],{"class":102,"line":197},[100,107341,200],{"class":114},[100,107343,170],{"class":118},[100,107345,206],{"class":205},[100,107347,107348],{"class":209},"That is not a valid number.",[100,107350,206],{"class":205},[100,107352,215],{"class":118},[14,107354,107355],{},"This approach is especially useful for user input, files, and external data.",[14,107357,107358,107359,290],{},"If you often work with user-entered values, see ",[295,107360,85785],{"href":85784},[77,107362,107364],{"id":107363},"fix-option-3-check-for-empty-input","Fix option 3: Check for empty input",[14,107366,107367,107368,290],{},"An empty string cannot be converted with ",[17,107369,59778],{},[14,107371,107372],{},"Handle blank input before conversion:",[91,107374,107376],{"className":93,"code":107375,"language":95,"meta":96,"style":96},"value = input(\"Enter a number: \").strip()\n\nif value == \"\":\n    print(\"Input cannot be empty.\")\nelse:\n    number = float(value)\n    print(number)\n",[17,107377,107378,107400,107404,107416,107431,107437,107451],{"__ignoreMap":96},[100,107379,107380,107382,107384,107386,107388,107390,107392,107394,107396,107398],{"class":102,"line":103},[100,107381,3318],{"class":106},[100,107383,111],{"class":110},[100,107385,7221],{"class":114},[100,107387,170],{"class":118},[100,107389,206],{"class":205},[100,107391,6217],{"class":209},[100,107393,206],{"class":205},[100,107395,5799],{"class":118},[100,107397,6235],{"class":178},[100,107399,4734],{"class":118},[100,107401,107402],{"class":102,"line":135},[100,107403,139],{"emptyLinePlaceholder":138},[100,107405,107406,107408,107410,107412,107414],{"class":102,"line":142},[100,107407,2736],{"class":145},[100,107409,989],{"class":106},[100,107411,32169],{"class":110},[100,107413,11901],{"class":205},[100,107415,149],{"class":118},[100,107417,107418,107420,107422,107424,107427,107429],{"class":102,"line":152},[100,107419,200],{"class":114},[100,107421,170],{"class":118},[100,107423,206],{"class":205},[100,107425,107426],{"class":209},"Input cannot be empty.",[100,107428,206],{"class":205},[100,107430,215],{"class":118},[100,107432,107433,107435],{"class":102,"line":164},[100,107434,4944],{"class":145},[100,107436,149],{"class":118},[100,107438,107439,107441,107443,107445,107447,107449],{"class":102,"line":185},[100,107440,98700],{"class":106},[100,107442,111],{"class":110},[100,107444,106783],{"class":191},[100,107446,170],{"class":118},[100,107448,757],{"class":178},[100,107450,215],{"class":118},[100,107452,107453,107455,107457,107459],{"class":102,"line":197},[100,107454,200],{"class":114},[100,107456,170],{"class":118},[100,107458,631],{"class":178},[100,107460,215],{"class":118},[14,107462,107463],{},"This is useful in:",[40,107465,107466,107469,107472],{},[43,107467,107468],{},"Forms",[43,107470,107471],{},"Command-line tools",[43,107473,107474],{},"User prompts",[14,107476,107477,107478,107480,107481,290],{},"It also helps with strings that contain only spaces, because ",[17,107479,8739],{}," turns them into ",[17,107482,106880],{},[77,107484,3274],{"id":3273},[14,107486,107487,107488,290],{},"If you are not sure why the conversion fails, inspect the value before calling ",[17,107489,59778],{},[14,107491,5295],{},[91,107493,107495],{"className":93,"code":107494,"language":95,"meta":96,"style":96},"print(value)\nprint(repr(value))\nprint(type(value))\n\ncleaned = value.strip()\nprint(repr(cleaned))\n",[17,107496,107497,107507,107521,107535,107539,107553],{"__ignoreMap":96},[100,107498,107499,107501,107503,107505],{"class":102,"line":103},[100,107500,372],{"class":114},[100,107502,170],{"class":118},[100,107504,757],{"class":178},[100,107506,215],{"class":118},[100,107508,107509,107511,107513,107515,107517,107519],{"class":102,"line":135},[100,107510,372],{"class":114},[100,107512,170],{"class":118},[100,107514,12228],{"class":114},[100,107516,170],{"class":118},[100,107518,757],{"class":178},[100,107520,182],{"class":118},[100,107522,107523,107525,107527,107529,107531,107533],{"class":102,"line":142},[100,107524,372],{"class":114},[100,107526,170],{"class":118},[100,107528,1250],{"class":191},[100,107530,170],{"class":118},[100,107532,757],{"class":178},[100,107534,182],{"class":118},[100,107536,107537],{"class":102,"line":152},[100,107538,139],{"emptyLinePlaceholder":138},[100,107540,107541,107543,107545,107547,107549,107551],{"class":102,"line":164},[100,107542,8872],{"class":106},[100,107544,111],{"class":110},[100,107546,6747],{"class":106},[100,107548,290],{"class":118},[100,107550,6235],{"class":178},[100,107552,4734],{"class":118},[100,107554,107555,107557,107559,107561,107563,107565],{"class":102,"line":185},[100,107556,372],{"class":114},[100,107558,170],{"class":118},[100,107560,12228],{"class":114},[100,107562,170],{"class":118},[100,107564,8915],{"class":178},[100,107566,182],{"class":118},[14,107568,107569],{},"What to check:",[40,107571,107572,107578,107584,107587],{},[43,107573,107574,107575,107577],{},"Print the value before ",[17,107576,59778],{}," to see what it really contains",[43,107579,241,107580,107583],{},[17,107581,107582],{},"repr(value)"," to reveal hidden spaces or newline characters",[43,107585,107586],{},"Check the exact source of the string: user input, file, API, or calculation",[43,107588,107589,107590],{},"Test with a known valid value like ",[17,107591,106850],{},[14,107593,1844],{},[91,107595,107597],{"className":93,"code":107596,"language":95,"meta":96,"style":96},"value = \"12.5\\n\"\n\nprint(value)\nprint(repr(value))\n\nnumber = float(value)\nprint(number)\n",[17,107598,107599,107614,107618,107628,107642,107646,107660],{"__ignoreMap":96},[100,107600,107601,107603,107605,107607,107610,107612],{"class":102,"line":103},[100,107602,3318],{"class":106},[100,107604,111],{"class":110},[100,107606,1708],{"class":205},[100,107608,107609],{"class":209},"12.5",[100,107611,20187],{"class":527},[100,107613,1714],{"class":205},[100,107615,107616],{"class":102,"line":135},[100,107617,139],{"emptyLinePlaceholder":138},[100,107619,107620,107622,107624,107626],{"class":102,"line":142},[100,107621,372],{"class":114},[100,107623,170],{"class":118},[100,107625,757],{"class":178},[100,107627,215],{"class":118},[100,107629,107630,107632,107634,107636,107638,107640],{"class":102,"line":152},[100,107631,372],{"class":114},[100,107633,170],{"class":118},[100,107635,12228],{"class":114},[100,107637,170],{"class":118},[100,107639,757],{"class":178},[100,107641,182],{"class":118},[100,107643,107644],{"class":102,"line":164},[100,107645,139],{"emptyLinePlaceholder":138},[100,107647,107648,107650,107652,107654,107656,107658],{"class":102,"line":185},[100,107649,1901],{"class":106},[100,107651,111],{"class":110},[100,107653,106783],{"class":191},[100,107655,170],{"class":118},[100,107657,757],{"class":178},[100,107659,215],{"class":118},[100,107661,107662,107664,107666,107668],{"class":102,"line":197},[100,107663,372],{"class":114},[100,107665,170],{"class":118},[100,107667,631],{"class":178},[100,107669,215],{"class":118},[14,107671,218],{},[91,107673,107675],{"className":93,"code":107674,"language":95,"meta":96,"style":96},"12.5\n\n'12.5\\n'\n12.5\n",[17,107676,107677,107682,107686,107697],{"__ignoreMap":96},[100,107678,107679],{"class":102,"line":103},[100,107680,107681],{"class":122},"12.5\n",[100,107683,107684],{"class":102,"line":135},[100,107685,139],{"emptyLinePlaceholder":138},[100,107687,107688,107690,107692,107695],{"class":102,"line":142},[100,107689,1280],{"class":3553},[100,107691,107609],{"class":3556},[100,107693,20187],{"class":107694},"sfi6f",[100,107696,3925],{"class":3553},[100,107698,107699],{"class":102,"line":152},[100,107700,107681],{"class":122},[14,107702,107703],{},"In this case, the newline does not cause a problem. But hidden characters plus extra text often do.",[77,107705,7081],{"id":7080},[14,107707,107708],{},"Sometimes a similar error has a different cause.",[40,107710,107711,107723,107728],{},[43,107712,107713,107717,107718,107720,107721],{},[295,107714,107715],{"href":17023},[17,107716,17024],{}," happens when ",[17,107719,6119],{}," fails instead of ",[17,107722,59778],{},[43,107724,107725,107727],{},[295,107726,3265],{"href":3264}," explains the broader exception type",[43,107729,107730,107732],{},[17,107731,3538],{}," can happen when the problem is the wrong data type, not bad string content",[77,107734,1514],{"id":1513},[675,107736,107738,107739,11353],{"id":107737},"can-float-convert-an-integer-string-like-10","Can float() convert an integer string like ",[17,107740,106855],{},[14,107742,10626,107743,107746,107747,290],{},[17,107744,107745],{},"float('10')"," works and returns ",[17,107748,80895],{},[675,107750,107752],{"id":107751},"why-does-float314-fail","Why does float('3,14') fail?",[14,107754,14478,107755,107757],{},[17,107756,59778],{}," expects a dot as the decimal separator in normal Python usage, not a comma.",[675,107759,107761],{"id":107760},"does-float-ignore-spaces","Does float() ignore spaces?",[14,107763,107764],{},"Leading and trailing spaces are usually fine, but strings with only spaces still fail after stripping.",[675,107766,107768],{"id":107767},"should-i-always-use-try-except-with-float","Should I always use try-except with float()?",[14,107770,107771],{},"Use it when the input may be invalid, especially with user input, files, or external data.",[77,107773,1554],{"id":1553},[40,107775,107776,107782,107787,107793,107797,107801],{},[43,107777,107778],{},[295,107779,1572,107780,1576],{"href":60491},[17,107781,59778],{},[43,107783,107784],{},[295,107785,107786],{"href":82079},"How to convert string to float in Python",[43,107788,107789],{},[295,107790,1572,107791,1576],{"href":85586},[17,107792,14906],{},[43,107794,107795],{},[295,107796,17024],{"href":17023},[43,107798,107799],{},[295,107800,3265],{"href":3264},[43,107802,107803],{},[295,107804,86861],{"href":85784},[1589,107806,107807],{},"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 .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 .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 .slqww, html code.shiki .slqww{--shiki-light:#6182B8;--shiki-default:#24292E;--shiki-dark:#E1E4E8}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 .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 .s_hVV, html code.shiki .s_hVV{--shiki-light:#90A4AE;--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 .s2W-s, html code.shiki .s2W-s{--shiki-light:#39ADB5;--shiki-light-font-style:italic;--shiki-default:#032F62;--shiki-default-font-style:inherit;--shiki-dark:#9ECBFF;--shiki-dark-font-style:inherit}html pre.shiki code .sithA, html code.shiki .sithA{--shiki-light:#90A4AE;--shiki-light-font-style:italic;--shiki-default:#032F62;--shiki-default-font-style:inherit;--shiki-dark:#9ECBFF;--shiki-dark-font-style:inherit}html pre.shiki code .sfi6f, html code.shiki .sfi6f{--shiki-light:#90A4AE;--shiki-light-font-style:italic;--shiki-default:#005CC5;--shiki-default-font-style:inherit;--shiki-dark:#79B8FF;--shiki-dark-font-style:inherit}",{"title":96,"searchDepth":135,"depth":135,"links":107809},[107810,107811,107812,107813,107814,107815,107816,107817,107818,107819,107820,107821,107828],{"id":79,"depth":135,"text":80},{"id":5880,"depth":135,"text":5881},{"id":26288,"depth":135,"text":26289},{"id":11030,"depth":135,"text":11031},{"id":1343,"depth":135,"text":1344},{"id":6243,"depth":135,"text":6244},{"id":107096,"depth":135,"text":107097},{"id":107249,"depth":135,"text":107250},{"id":107363,"depth":135,"text":107364},{"id":3273,"depth":135,"text":3274},{"id":7080,"depth":135,"text":7081},{"id":1513,"depth":135,"text":1514,"children":107822},[107823,107825,107826,107827],{"id":107737,"depth":142,"text":107824},"Can float() convert an integer string like '10'?",{"id":107751,"depth":142,"text":107752},{"id":107760,"depth":142,"text":107761},{"id":107767,"depth":142,"text":107768},{"id":1553,"depth":135,"text":1554},"Master valueerror could not convert string to float fix in our comprehensive Python beginner guide.",{},"\u002Ferrors\u002Fvalueerror-could-not-convert-string-to-float-fix",{"title":106695,"description":107829},"errors\u002Fvalueerror-could-not-convert-string-to-float-fix","VFbLKHlrAqK258JJDm3XpXxEcPXZ3qvHBvr7W2A0kP0",{"id":107836,"title":107837,"body":107838,"description":109234,"extension":1623,"meta":109235,"navigation":138,"path":109236,"seo":109237,"stem":109238,"__hash__":109239},"content\u002Ferrors\u002Fvalueerror-empty-separator-fix.md","ValueError: empty separator (Fix)",{"type":7,"value":107839,"toc":109209},[107840,107843,107850,107852,107940,107942,108004,108014,108016,108021,108024,108043,108052,108054,108057,108102,108104,108118,108124,108126,108129,108132,108150,108156,108158,108162,108167,108216,108218,108251,108257,108263,108265,108307,108309,108342,108347,108353,108362,108367,108408,108410,108459,108463,108466,108491,108493,108596,108598,108687,108689,108692,108710,108712,108766,108769,108870,108872,108908,108914,108916,108919,108944,108949,108951,108953,108975,108978,109039,109045,109106,109108,109140,109142,109148,109151,109155,109160,109164,109169,109173,109176,109178,109206],[10,107841,107837],{"id":107842},"valueerror-empty-separator-fix",[14,107844,107845,107846,107849],{},"This page helps beginners fix the Python error ",[17,107847,107848],{},"ValueError: empty separator",". It explains why it happens, shows a small example that causes it, and gives simple ways to solve it.",[77,107851,80],{"id":79},[91,107853,107855],{"className":93,"code":107854,"language":95,"meta":96,"style":96},"text = \"apple,banana,orange\"\nparts = text.split(\",\")  # use a real separator\nprint(parts)\n\nwords = \"one two three\".split()  # no argument splits on whitespace\nprint(words)\n",[17,107856,107857,107869,107894,107904,107908,107930],{"__ignoreMap":96},[100,107858,107859,107861,107863,107865,107867],{"class":102,"line":103},[100,107860,4331],{"class":106},[100,107862,111],{"class":110},[100,107864,1708],{"class":205},[100,107866,4338],{"class":209},[100,107868,1714],{"class":205},[100,107870,107871,107873,107875,107877,107879,107881,107883,107885,107887,107889,107891],{"class":102,"line":135},[100,107872,4345],{"class":106},[100,107874,111],{"class":110},[100,107876,4350],{"class":106},[100,107878,290],{"class":118},[100,107880,3922],{"class":178},[100,107882,170],{"class":118},[100,107884,206],{"class":205},[100,107886,126],{"class":209},[100,107888,206],{"class":205},[100,107890,6155],{"class":118},[100,107892,107893],{"class":414},"  # use a real separator\n",[100,107895,107896,107898,107900,107902],{"class":102,"line":142},[100,107897,372],{"class":114},[100,107899,170],{"class":118},[100,107901,4373],{"class":178},[100,107903,215],{"class":118},[100,107905,107906],{"class":102,"line":152},[100,107907,139],{"emptyLinePlaceholder":138},[100,107909,107910,107912,107914,107916,107919,107921,107923,107925,107927],{"class":102,"line":164},[100,107911,7674],{"class":106},[100,107913,111],{"class":110},[100,107915,1708],{"class":205},[100,107917,107918],{"class":209},"one two three",[100,107920,206],{"class":205},[100,107922,290],{"class":118},[100,107924,3922],{"class":178},[100,107926,7562],{"class":118},[100,107928,107929],{"class":414},"  # no argument splits on whitespace\n",[100,107931,107932,107934,107936,107938],{"class":102,"line":185},[100,107933,372],{"class":114},[100,107935,170],{"class":118},[100,107937,7701],{"class":178},[100,107939,215],{"class":118},[14,107941,218],{},[91,107943,107945],{"className":93,"code":107944,"language":95,"meta":96,"style":96},"['apple', 'banana', 'orange']\n['one', 'two', 'three']\n",[17,107946,107947,107975],{"__ignoreMap":96},[100,107948,107949,107951,107953,107955,107957,107959,107961,107963,107965,107967,107969,107971,107973],{"class":102,"line":103},[100,107950,3166],{"class":118},[100,107952,1280],{"class":205},[100,107954,4148],{"class":209},[100,107956,1280],{"class":205},[100,107958,126],{"class":118},[100,107960,1274],{"class":205},[100,107962,4157],{"class":209},[100,107964,1280],{"class":205},[100,107966,126],{"class":118},[100,107968,1274],{"class":205},[100,107970,4166],{"class":209},[100,107972,1280],{"class":205},[100,107974,603],{"class":118},[100,107976,107977,107979,107981,107983,107985,107987,107989,107991,107993,107995,107997,108000,108002],{"class":102,"line":135},[100,107978,3166],{"class":118},[100,107980,1280],{"class":205},[100,107982,50398],{"class":209},[100,107984,1280],{"class":205},[100,107986,126],{"class":118},[100,107988,1274],{"class":205},[100,107990,50411],{"class":209},[100,107992,1280],{"class":205},[100,107994,126],{"class":118},[100,107996,1274],{"class":205},[100,107998,107999],{"class":209},"three",[100,108001,1280],{"class":205},[100,108003,603],{"class":118},[14,108005,108006,108007,108009,108010,108013],{},"Do not pass an empty string to ",[17,108008,8090],{},". Use a real separator like ",[17,108011,108012],{},"\",\""," or leave the argument out to split on whitespace.",[77,108015,5881],{"id":5880},[14,108017,108018,108020],{},[17,108019,107848],{}," means Python was asked to split text using an empty string.",[14,108022,108023],{},"This most often happens with:",[91,108025,108027],{"className":93,"code":108026,"language":95,"meta":96,"style":96},"text.split(\"\")\n",[17,108028,108029],{"__ignoreMap":96},[100,108030,108031,108033,108035,108037,108039,108041],{"class":102,"line":103},[100,108032,4468],{"class":106},[100,108034,290],{"class":118},[100,108036,3922],{"class":178},[100,108038,170],{"class":118},[100,108040,11875],{"class":205},[100,108042,215],{"class":118},[14,108044,108045,108046,290],{},"Python does not allow an empty separator for ",[295,108047,108049],{"href":108048},"\u002Freference\u002Fpython-string-split-method\u002F",[17,108050,108051],{},"str.split()",[77,108053,11031],{"id":11030},[14,108055,108056],{},"A common failing example is:",[91,108058,108060],{"className":93,"code":108059,"language":95,"meta":96,"style":96},"text = \"hello\"\nparts = text.split(\"\")\nprint(parts)\n",[17,108061,108062,108074,108092],{"__ignoreMap":96},[100,108063,108064,108066,108068,108070,108072],{"class":102,"line":103},[100,108065,4331],{"class":106},[100,108067,111],{"class":110},[100,108069,1708],{"class":205},[100,108071,3506],{"class":209},[100,108073,1714],{"class":205},[100,108075,108076,108078,108080,108082,108084,108086,108088,108090],{"class":102,"line":135},[100,108077,4345],{"class":106},[100,108079,111],{"class":110},[100,108081,4350],{"class":106},[100,108083,290],{"class":118},[100,108085,3922],{"class":178},[100,108087,170],{"class":118},[100,108089,11875],{"class":205},[100,108091,215],{"class":118},[100,108093,108094,108096,108098,108100],{"class":102,"line":142},[100,108095,372],{"class":114},[100,108097,170],{"class":118},[100,108099,4373],{"class":178},[100,108101,215],{"class":118},[14,108103,218],{},[91,108105,108107],{"className":93,"code":108106,"language":95,"meta":96,"style":96},"ValueError: empty separator\n",[17,108108,108109],{"__ignoreMap":96},[100,108110,108111,108113,108115],{"class":102,"line":103},[100,108112,3483],{"class":191},[100,108114,89],{"class":118},[100,108116,108117],{"class":106}," empty separator\n",[14,108119,108120,108121,290],{},"The problem is the empty string inside ",[17,108122,108123],{},"split(\"\")",[77,108125,25091],{"id":25090},[14,108127,108128],{},"A separator must be at least one character long.",[14,108130,108131],{},"Python needs a real boundary such as:",[40,108133,108134,108140,108145],{},[43,108135,108136,108137],{},"a space: ",[17,108138,108139],{},"\" \"",[43,108141,108142,108143],{},"a comma: ",[17,108144,108012],{},[43,108146,108147,108148],{},"a dash: ",[17,108149,94084],{},[14,108151,108152,108153,108155],{},"An empty string ",[17,108154,11875],{}," does not give Python a valid place to split.",[77,108157,6244],{"id":6243},[675,108159,108161],{"id":108160},"use-a-real-separator","Use a real separator",[14,108163,108164,108165,290],{},"If your text uses commas, spaces, or another character between values, pass that character to ",[17,108166,8090],{},[91,108168,108170],{"className":93,"code":108169,"language":95,"meta":96,"style":96},"text = \"red,green,blue\"\ncolors = text.split(\",\")\nprint(colors)\n",[17,108171,108172,108184,108206],{"__ignoreMap":96},[100,108173,108174,108176,108178,108180,108182],{"class":102,"line":103},[100,108175,4331],{"class":106},[100,108177,111],{"class":110},[100,108179,1708],{"class":205},[100,108181,100211],{"class":209},[100,108183,1714],{"class":205},[100,108185,108186,108188,108190,108192,108194,108196,108198,108200,108202,108204],{"class":102,"line":135},[100,108187,35257],{"class":106},[100,108189,111],{"class":110},[100,108191,4350],{"class":106},[100,108193,290],{"class":118},[100,108195,3922],{"class":178},[100,108197,170],{"class":118},[100,108199,206],{"class":205},[100,108201,126],{"class":209},[100,108203,206],{"class":205},[100,108205,215],{"class":118},[100,108207,108208,108210,108212,108214],{"class":102,"line":142},[100,108209,372],{"class":114},[100,108211,170],{"class":118},[100,108213,37826],{"class":178},[100,108215,215],{"class":118},[14,108217,218],{},[91,108219,108221],{"className":93,"code":108220,"language":95,"meta":96,"style":96},"['red', 'green', 'blue']\n",[17,108222,108223],{"__ignoreMap":96},[100,108224,108225,108227,108229,108231,108233,108235,108237,108239,108241,108243,108245,108247,108249],{"class":102,"line":103},[100,108226,3166],{"class":118},[100,108228,1280],{"class":205},[100,108230,35266],{"class":209},[100,108232,1280],{"class":205},[100,108234,126],{"class":118},[100,108236,1274],{"class":205},[100,108238,35275],{"class":209},[100,108240,1280],{"class":205},[100,108242,126],{"class":118},[100,108244,1274],{"class":205},[100,108246,35284],{"class":209},[100,108248,1280],{"class":205},[100,108250,603],{"class":118},[675,108252,241,108254,108256],{"id":108253},"use-split-with-no-argument-for-whitespace",[17,108255,8090],{}," with no argument for whitespace",[14,108258,108259,108260,108262],{},"If you want to split words in a sentence, do not use ",[17,108261,108139],{}," unless you specifically need a single-space separator.",[14,108264,27749],{},[91,108266,108268],{"className":93,"code":108267,"language":95,"meta":96,"style":96},"text = \"one two   three\"\nwords = text.split()\nprint(words)\n",[17,108269,108270,108283,108297],{"__ignoreMap":96},[100,108271,108272,108274,108276,108278,108281],{"class":102,"line":103},[100,108273,4331],{"class":106},[100,108275,111],{"class":110},[100,108277,1708],{"class":205},[100,108279,108280],{"class":209},"one two   three",[100,108282,1714],{"class":205},[100,108284,108285,108287,108289,108291,108293,108295],{"class":102,"line":135},[100,108286,7674],{"class":106},[100,108288,111],{"class":110},[100,108290,4350],{"class":106},[100,108292,290],{"class":118},[100,108294,3922],{"class":178},[100,108296,4734],{"class":118},[100,108298,108299,108301,108303,108305],{"class":102,"line":142},[100,108300,372],{"class":114},[100,108302,170],{"class":118},[100,108304,7701],{"class":178},[100,108306,215],{"class":118},[14,108308,218],{},[91,108310,108312],{"className":93,"code":108311,"language":95,"meta":96,"style":96},"['one', 'two', 'three']\n",[17,108313,108314],{"__ignoreMap":96},[100,108315,108316,108318,108320,108322,108324,108326,108328,108330,108332,108334,108336,108338,108340],{"class":102,"line":103},[100,108317,3166],{"class":118},[100,108319,1280],{"class":205},[100,108321,50398],{"class":209},[100,108323,1280],{"class":205},[100,108325,126],{"class":118},[100,108327,1274],{"class":205},[100,108329,50411],{"class":209},[100,108331,1280],{"class":205},[100,108333,126],{"class":118},[100,108335,1274],{"class":205},[100,108337,107999],{"class":209},[100,108339,1280],{"class":205},[100,108341,603],{"class":118},[14,108343,108344,108345,290],{},"This is the usual solution for splitting text into words. You can learn more in ",[295,108346,8960],{"href":8959},[675,108348,241,108350,108352],{"id":108349},"use-listtext-for-individual-characters",[17,108351,15420],{}," for individual characters",[14,108354,108355,108356,108358,108359,108361],{},"Some beginners expect ",[17,108357,108123],{}," to return each character. That is not how ",[17,108360,8090],{}," works.",[14,108363,108364,108365,89],{},"If you want characters, use ",[17,108366,15420],{},[91,108368,108370],{"className":93,"code":108369,"language":95,"meta":96,"style":96},"text = \"hello\"\nchars = list(text)\nprint(chars)\n",[17,108371,108372,108384,108398],{"__ignoreMap":96},[100,108373,108374,108376,108378,108380,108382],{"class":102,"line":103},[100,108375,4331],{"class":106},[100,108377,111],{"class":110},[100,108379,1708],{"class":205},[100,108381,3506],{"class":209},[100,108383,1714],{"class":205},[100,108385,108386,108388,108390,108392,108394,108396],{"class":102,"line":135},[100,108387,14699],{"class":106},[100,108389,111],{"class":110},[100,108391,14704],{"class":191},[100,108393,170],{"class":118},[100,108395,4468],{"class":178},[100,108397,215],{"class":118},[100,108399,108400,108402,108404,108406],{"class":102,"line":142},[100,108401,372],{"class":114},[100,108403,170],{"class":118},[100,108405,14715],{"class":178},[100,108407,215],{"class":118},[14,108409,218],{},[91,108411,108413],{"className":93,"code":108412,"language":95,"meta":96,"style":96},"['h', 'e', 'l', 'l', 'o']\n",[17,108414,108415],{"__ignoreMap":96},[100,108416,108417,108419,108421,108423,108425,108427,108429,108431,108433,108435,108437,108439,108441,108443,108445,108447,108449,108451,108453,108455,108457],{"class":102,"line":103},[100,108418,3166],{"class":118},[100,108420,1280],{"class":205},[100,108422,14761],{"class":209},[100,108424,1280],{"class":205},[100,108426,126],{"class":118},[100,108428,1274],{"class":205},[100,108430,14770],{"class":209},[100,108432,1280],{"class":205},[100,108434,126],{"class":118},[100,108436,1274],{"class":205},[100,108438,14779],{"class":209},[100,108440,1280],{"class":205},[100,108442,126],{"class":118},[100,108444,1274],{"class":205},[100,108446,14779],{"class":209},[100,108448,1280],{"class":205},[100,108450,126],{"class":118},[100,108452,1274],{"class":205},[100,108454,14796],{"class":209},[100,108456,1280],{"class":205},[100,108458,603],{"class":118},[77,108460,108462],{"id":108461},"choose-the-right-fix-for-your-goal","Choose the right fix for your goal",[14,108464,108465],{},"Use the fix that matches what you want to do:",[40,108467,108468,108476,108484],{},[43,108469,108470,108473,108474],{},[22,108471,108472],{},"To split words in a sentence",", use ",[17,108475,8090],{},[43,108477,108478,108473,108481],{},[22,108479,108480],{},"To split comma-separated values",[17,108482,108483],{},"split(\",\")",[43,108485,108486,108473,108489],{},[22,108487,108488],{},"To turn a string into characters",[17,108490,15420],{},[14,108492,5500],{},[91,108494,108496],{"className":93,"code":108495,"language":95,"meta":96,"style":96},"sentence = \"Python is fun\"\nprint(sentence.split())\n\ncsv_text = \"a,b,c\"\nprint(csv_text.split(\",\"))\n\ntext = \"cat\"\nprint(list(text))\n",[17,108497,108498,108511,108526,108530,108543,108566,108570,108582],{"__ignoreMap":96},[100,108499,108500,108503,108505,108507,108509],{"class":102,"line":103},[100,108501,108502],{"class":106},"sentence ",[100,108504,111],{"class":110},[100,108506,1708],{"class":205},[100,108508,69744],{"class":209},[100,108510,1714],{"class":205},[100,108512,108513,108515,108517,108520,108522,108524],{"class":102,"line":135},[100,108514,372],{"class":114},[100,108516,170],{"class":118},[100,108518,108519],{"class":178},"sentence",[100,108521,290],{"class":118},[100,108523,3922],{"class":178},[100,108525,3370],{"class":118},[100,108527,108528],{"class":102,"line":142},[100,108529,139],{"emptyLinePlaceholder":138},[100,108531,108532,108535,108537,108539,108541],{"class":102,"line":152},[100,108533,108534],{"class":106},"csv_text ",[100,108536,111],{"class":110},[100,108538,1708],{"class":205},[100,108540,15465],{"class":209},[100,108542,1714],{"class":205},[100,108544,108545,108547,108549,108552,108554,108556,108558,108560,108562,108564],{"class":102,"line":164},[100,108546,372],{"class":114},[100,108548,170],{"class":118},[100,108550,108551],{"class":178},"csv_text",[100,108553,290],{"class":118},[100,108555,3922],{"class":178},[100,108557,170],{"class":118},[100,108559,206],{"class":205},[100,108561,126],{"class":209},[100,108563,206],{"class":205},[100,108565,182],{"class":118},[100,108567,108568],{"class":102,"line":185},[100,108569,139],{"emptyLinePlaceholder":138},[100,108571,108572,108574,108576,108578,108580],{"class":102,"line":197},[100,108573,4331],{"class":106},[100,108575,111],{"class":110},[100,108577,1708],{"class":205},[100,108579,33220],{"class":209},[100,108581,1714],{"class":205},[100,108583,108584,108586,108588,108590,108592,108594],{"class":102,"line":771},[100,108585,372],{"class":114},[100,108587,170],{"class":118},[100,108589,1235],{"class":191},[100,108591,170],{"class":118},[100,108593,4468],{"class":178},[100,108595,182],{"class":118},[14,108597,218],{},[91,108599,108601],{"className":93,"code":108600,"language":95,"meta":96,"style":96},"['Python', 'is', 'fun']\n['a', 'b', 'c']\n['c', 'a', 't']\n",[17,108602,108603,108631,108659],{"__ignoreMap":96},[100,108604,108605,108607,108609,108611,108613,108615,108617,108619,108621,108623,108625,108627,108629],{"class":102,"line":103},[100,108606,3166],{"class":118},[100,108608,1280],{"class":205},[100,108610,12937],{"class":209},[100,108612,1280],{"class":205},[100,108614,126],{"class":118},[100,108616,1274],{"class":205},[100,108618,4918],{"class":209},[100,108620,1280],{"class":205},[100,108622,126],{"class":118},[100,108624,1274],{"class":205},[100,108626,13296],{"class":209},[100,108628,1280],{"class":205},[100,108630,603],{"class":118},[100,108632,108633,108635,108637,108639,108641,108643,108645,108647,108649,108651,108653,108655,108657],{"class":102,"line":135},[100,108634,3166],{"class":118},[100,108636,1280],{"class":205},[100,108638,295],{"class":209},[100,108640,1280],{"class":205},[100,108642,126],{"class":118},[100,108644,1274],{"class":205},[100,108646,4027],{"class":209},[100,108648,1280],{"class":205},[100,108650,126],{"class":118},[100,108652,1274],{"class":205},[100,108654,4036],{"class":209},[100,108656,1280],{"class":205},[100,108658,603],{"class":118},[100,108660,108661,108663,108665,108667,108669,108671,108673,108675,108677,108679,108681,108683,108685],{"class":102,"line":142},[100,108662,3166],{"class":118},[100,108664,1280],{"class":205},[100,108666,4036],{"class":209},[100,108668,1280],{"class":205},[100,108670,126],{"class":118},[100,108672,1274],{"class":205},[100,108674,295],{"class":209},[100,108676,1280],{"class":205},[100,108678,126],{"class":118},[100,108680,1274],{"class":205},[100,108682,69966],{"class":209},[100,108684,1280],{"class":205},[100,108686,603],{"class":118},[77,108688,3274],{"id":3273},[14,108690,108691],{},"If the error comes from a larger program, use these steps:",[3282,108693,108694,108699,108702,108705],{},[43,108695,108696,108697],{},"Find the line that calls ",[17,108698,8090],{},[43,108700,108701],{},"Check the value passed as the separator",[43,108703,108704],{},"Print the separator if it comes from a variable",[43,108706,52216,108707,108709],{},[17,108708,11875],{}," with a real separator or remove the argument",[14,108711,5295],{},[91,108713,108715],{"className":93,"code":108714,"language":95,"meta":96,"style":96},"print(separator)\nprint(repr(separator))\nprint(type(separator))\nprint(text)\n",[17,108716,108717,108728,108742,108756],{"__ignoreMap":96},[100,108718,108719,108721,108723,108726],{"class":102,"line":103},[100,108720,372],{"class":114},[100,108722,170],{"class":118},[100,108724,108725],{"class":178},"separator",[100,108727,215],{"class":118},[100,108729,108730,108732,108734,108736,108738,108740],{"class":102,"line":135},[100,108731,372],{"class":114},[100,108733,170],{"class":118},[100,108735,12228],{"class":114},[100,108737,170],{"class":118},[100,108739,108725],{"class":178},[100,108741,182],{"class":118},[100,108743,108744,108746,108748,108750,108752,108754],{"class":102,"line":142},[100,108745,372],{"class":114},[100,108747,170],{"class":118},[100,108749,1250],{"class":191},[100,108751,170],{"class":118},[100,108753,108725],{"class":178},[100,108755,182],{"class":118},[100,108757,108758,108760,108762,108764],{"class":102,"line":152},[100,108759,372],{"class":114},[100,108761,170],{"class":118},[100,108763,4468],{"class":178},[100,108765,215],{"class":118},[14,108767,108768],{},"A full example:",[91,108770,108772],{"className":93,"code":108771,"language":95,"meta":96,"style":96},"text = \"a-b-c\"\nseparator = \"\"\n\nprint(separator)\nprint(repr(separator))\nprint(type(separator))\nprint(text)\n\nparts = text.split(separator)\n",[17,108773,108774,108787,108796,108800,108810,108824,108838,108848,108852],{"__ignoreMap":96},[100,108775,108776,108778,108780,108782,108785],{"class":102,"line":103},[100,108777,4331],{"class":106},[100,108779,111],{"class":110},[100,108781,1708],{"class":205},[100,108783,108784],{"class":209},"a-b-c",[100,108786,1714],{"class":205},[100,108788,108789,108792,108794],{"class":102,"line":135},[100,108790,108791],{"class":106},"separator ",[100,108793,111],{"class":110},[100,108795,1696],{"class":205},[100,108797,108798],{"class":102,"line":142},[100,108799,139],{"emptyLinePlaceholder":138},[100,108801,108802,108804,108806,108808],{"class":102,"line":152},[100,108803,372],{"class":114},[100,108805,170],{"class":118},[100,108807,108725],{"class":178},[100,108809,215],{"class":118},[100,108811,108812,108814,108816,108818,108820,108822],{"class":102,"line":164},[100,108813,372],{"class":114},[100,108815,170],{"class":118},[100,108817,12228],{"class":114},[100,108819,170],{"class":118},[100,108821,108725],{"class":178},[100,108823,182],{"class":118},[100,108825,108826,108828,108830,108832,108834,108836],{"class":102,"line":185},[100,108827,372],{"class":114},[100,108829,170],{"class":118},[100,108831,1250],{"class":191},[100,108833,170],{"class":118},[100,108835,108725],{"class":178},[100,108837,182],{"class":118},[100,108839,108840,108842,108844,108846],{"class":102,"line":197},[100,108841,372],{"class":114},[100,108843,170],{"class":118},[100,108845,4468],{"class":178},[100,108847,215],{"class":118},[100,108849,108850],{"class":102,"line":771},[100,108851,139],{"emptyLinePlaceholder":138},[100,108853,108854,108856,108858,108860,108862,108864,108866,108868],{"class":102,"line":787},[100,108855,4345],{"class":106},[100,108857,111],{"class":110},[100,108859,4350],{"class":106},[100,108861,290],{"class":118},[100,108863,3922],{"class":178},[100,108865,170],{"class":118},[100,108867,108725],{"class":178},[100,108869,215],{"class":118},[14,108871,38743],{},[91,108873,108875],{"className":93,"code":108874,"language":95,"meta":96,"style":96},"''\n\u003Cclass 'str'>\na-b-c\n",[17,108876,108877,108882,108896],{"__ignoreMap":96},[100,108878,108879],{"class":102,"line":103},[100,108880,108881],{"class":3553},"''\n",[100,108883,108884,108886,108888,108890,108892,108894],{"class":102,"line":135},[100,108885,2782],{"class":110},[100,108887,3417],{"class":1077},[100,108889,1274],{"class":205},[100,108891,3422],{"class":209},[100,108893,1280],{"class":205},[100,108895,1980],{"class":110},[100,108897,108898,108900,108902,108904,108906],{"class":102,"line":142},[100,108899,295],{"class":106},[100,108901,2193],{"class":110},[100,108903,4027],{"class":106},[100,108905,2193],{"class":110},[100,108907,15565],{"class":106},[14,108909,108910,108913],{},[17,108911,108912],{},"repr(separator)"," is especially helpful because it makes the empty string easier to see.",[77,108915,66110],{"id":66109},[14,108917,108918],{},"This error is different from other common string problems.",[40,108920,108921,108932,108938],{},[43,108922,41219,108923,99175,108925,108931],{},[22,108924,1059],{},[295,108926,108928],{"href":108927},"\u002Ferrors\u002Fvalueerror-substring-not-found-fix\u002F",[17,108929,108930],{},"ValueError: substring not found",", which happens in different string operations",[43,108933,108934,108935,108937],{},"It is also different from a ",[17,108936,3538],{},", which happens when you pass the wrong data type",[43,108939,3464,108940,42154,108942],{},[17,108941,108123],{},[17,108943,15420],{},[14,108945,108946,108947,290],{},"If you want a broader overview of this kind of error, see ",[295,108948,3265],{"href":3264},[77,108950,7117],{"id":7116},[14,108952,9001],{},[40,108954,108955,108960,108965,108970],{},[43,108956,1357,108957,75831],{},[17,108958,108959],{},"text.split(\"\")",[43,108961,108962,108963],{},"Passing a variable that contains an empty string into ",[17,108964,8090],{},[43,108966,11012,108967,108969],{},[17,108968,108123],{}," to return individual characters",[43,108971,108972,108973],{},"Building the separator dynamically and ending up with ",[17,108974,11875],{},[14,108976,108977],{},"Example with a variable:",[91,108979,108981],{"className":93,"code":108980,"language":95,"meta":96,"style":96},"text = \"apple|banana|orange\"\nseparator = \"\"\n\nparts = text.split(separator)  # causes ValueError\nprint(parts)\n",[17,108982,108983,108996,109004,109008,109029],{"__ignoreMap":96},[100,108984,108985,108987,108989,108991,108994],{"class":102,"line":103},[100,108986,4331],{"class":106},[100,108988,111],{"class":110},[100,108990,1708],{"class":205},[100,108992,108993],{"class":209},"apple|banana|orange",[100,108995,1714],{"class":205},[100,108997,108998,109000,109002],{"class":102,"line":135},[100,108999,108791],{"class":106},[100,109001,111],{"class":110},[100,109003,1696],{"class":205},[100,109005,109006],{"class":102,"line":142},[100,109007,139],{"emptyLinePlaceholder":138},[100,109009,109010,109012,109014,109016,109018,109020,109022,109024,109026],{"class":102,"line":152},[100,109011,4345],{"class":106},[100,109013,111],{"class":110},[100,109015,4350],{"class":106},[100,109017,290],{"class":118},[100,109019,3922],{"class":178},[100,109021,170],{"class":118},[100,109023,108725],{"class":178},[100,109025,6155],{"class":118},[100,109027,109028],{"class":414},"  # causes ValueError\n",[100,109030,109031,109033,109035,109037],{"class":102,"line":164},[100,109032,372],{"class":114},[100,109034,170],{"class":118},[100,109036,4373],{"class":178},[100,109038,215],{"class":118},[14,109040,109041,109042,109044],{},"To fix it, make sure ",[17,109043,108725],{}," is not empty:",[91,109046,109048],{"className":93,"code":109047,"language":95,"meta":96,"style":96},"text = \"apple|banana|orange\"\nseparator = \"|\"\n\nparts = text.split(separator)\nprint(parts)\n",[17,109049,109050,109062,109074,109078,109096],{"__ignoreMap":96},[100,109051,109052,109054,109056,109058,109060],{"class":102,"line":103},[100,109053,4331],{"class":106},[100,109055,111],{"class":110},[100,109057,1708],{"class":205},[100,109059,108993],{"class":209},[100,109061,1714],{"class":205},[100,109063,109064,109066,109068,109070,109072],{"class":102,"line":135},[100,109065,108791],{"class":106},[100,109067,111],{"class":110},[100,109069,1708],{"class":205},[100,109071,94534],{"class":209},[100,109073,1714],{"class":205},[100,109075,109076],{"class":102,"line":142},[100,109077,139],{"emptyLinePlaceholder":138},[100,109079,109080,109082,109084,109086,109088,109090,109092,109094],{"class":102,"line":152},[100,109081,4345],{"class":106},[100,109083,111],{"class":110},[100,109085,4350],{"class":106},[100,109087,290],{"class":118},[100,109089,3922],{"class":178},[100,109091,170],{"class":118},[100,109093,108725],{"class":178},[100,109095,215],{"class":118},[100,109097,109098,109100,109102,109104],{"class":102,"line":164},[100,109099,372],{"class":114},[100,109101,170],{"class":118},[100,109103,4373],{"class":178},[100,109105,215],{"class":118},[14,109107,218],{},[91,109109,109110],{"className":93,"code":4380,"language":95,"meta":96,"style":96},[17,109111,109112],{"__ignoreMap":96},[100,109113,109114,109116,109118,109120,109122,109124,109126,109128,109130,109132,109134,109136,109138],{"class":102,"line":103},[100,109115,3166],{"class":118},[100,109117,1280],{"class":205},[100,109119,4148],{"class":209},[100,109121,1280],{"class":205},[100,109123,126],{"class":118},[100,109125,1274],{"class":205},[100,109127,4157],{"class":209},[100,109129,1280],{"class":205},[100,109131,126],{"class":118},[100,109133,1274],{"class":205},[100,109135,4166],{"class":209},[100,109137,1280],{"class":205},[100,109139,603],{"class":118},[77,109141,1514],{"id":1513},[675,109143,9137,109145,109147],{"id":109144},"why-does-split-fail-in-python",[17,109146,108123],{}," fail in Python?",[14,109149,109150],{},"Because Python does not allow an empty string as a separator. The separator must contain at least one character.",[675,109152,109154],{"id":109153},"how-do-i-split-a-string-into-characters","How do I split a string into characters?",[14,109156,241,109157,109159],{},[17,109158,15420],{},". That returns each character as a separate item.",[675,109161,109163],{"id":109162},"how-do-i-split-by-spaces","How do I split by spaces?",[14,109165,241,109166,109168],{},[17,109167,8090],{}," with no argument. It splits on whitespace.",[675,109170,109172],{"id":109171},"can-i-use-a-variable-as-the-separator","Can I use a variable as the separator?",[14,109174,109175],{},"Yes, but make sure the variable is not an empty string.",[77,109177,1554],{"id":1553},[40,109179,109180,109187,109191,109197,109202],{},[43,109181,109182],{},[295,109183,109184,109185,4112],{"href":108048},"Python string ",[17,109186,8090],{},[43,109188,109189],{},[295,109190,16104],{"href":8959},[43,109192,109193],{},[295,109194,109196],{"href":109195},"\u002Fhow-to\u002Fhow-to-check-if-a-string-contains-a-substring-in-python\u002F","How to check if a string contains a substring in Python",[43,109198,109199],{},[295,109200,109201],{"href":108927},"ValueError: substring not found (Fix)",[43,109203,109204],{},[295,109205,3265],{"href":3264},[1589,109207,109208],{},"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 .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 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 .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 .s2W-s, html code.shiki .s2W-s{--shiki-light:#39ADB5;--shiki-light-font-style:italic;--shiki-default:#032F62;--shiki-default-font-style:inherit;--shiki-dark:#9ECBFF;--shiki-dark-font-style:inherit}html pre.shiki code .sbsja, html code.shiki .sbsja{--shiki-light:#9C3EDA;--shiki-default:#D73A49;--shiki-dark:#F97583}",{"title":96,"searchDepth":135,"depth":135,"links":109210},[109211,109212,109213,109214,109215,109222,109223,109224,109225,109226,109233],{"id":79,"depth":135,"text":80},{"id":5880,"depth":135,"text":5881},{"id":11030,"depth":135,"text":11031},{"id":25090,"depth":135,"text":25091},{"id":6243,"depth":135,"text":6244,"children":109216},[109217,109218,109220],{"id":108160,"depth":142,"text":108161},{"id":108253,"depth":142,"text":109219},"Use split() with no argument for whitespace",{"id":108349,"depth":142,"text":109221},"Use list(text) for individual characters",{"id":108461,"depth":135,"text":108462},{"id":3273,"depth":135,"text":3274},{"id":66109,"depth":135,"text":66110},{"id":7116,"depth":135,"text":7117},{"id":1513,"depth":135,"text":1514,"children":109227},[109228,109230,109231,109232],{"id":109144,"depth":142,"text":109229},"Why does split(\"\") fail in Python?",{"id":109153,"depth":142,"text":109154},{"id":109162,"depth":142,"text":109163},{"id":109171,"depth":142,"text":109172},{"id":1553,"depth":135,"text":1554},"Master valueerror empty separator fix in our comprehensive Python beginner guide.",{},"\u002Ferrors\u002Fvalueerror-empty-separator-fix",{"title":107837,"description":109234},"errors\u002Fvalueerror-empty-separator-fix","ht3i8ylO7TO5qMc7LOPgxLkZs0S-msnOeUoMnA8-YGI",{"id":109241,"title":109242,"body":109243,"description":110496,"extension":1623,"meta":110497,"navigation":138,"path":110498,"seo":110499,"stem":110500,"__hash__":110501},"content\u002Ferrors\u002Fvalueerror-in-python-causes-and-fixes.md","ValueError in Python: Causes and Fixes",{"type":7,"value":109244,"toc":110468},[109245,109248,109253,109256,109263,109265,109329,109336,109340,109345,109353,109355,109373,109378,109393,109397,109409,109417,109421,109426,109456,109458,109482,109486,109489,109495,109536,109538,109570,109572,109586,109593,109635,109637,109664,109666,109680,109686,109690,109696,109699,109720,109724,109771,109773,109781,109786,109790,109909,109912,109919,109921,109925,109945,109947,110010,110016,110021,110028,110030,110077,110079,110102,110105,110109,110111,110123,110125,110178,110181,110203,110208,110212,110217,110220,110233,110235,110324,110327,110330,110332,110385,110388,110391,110396,110398,110402,110410,110414,110420,110424,110427,110431,110437,110439,110465],[10,109246,109242],{"id":109247},"valueerror-in-python-causes-and-fixes",[14,109249,109250,109252],{},[17,109251,3483],{}," is a common Python error for beginners.",[14,109254,109255],{},"It means a function received a value of the correct type, but the actual content of that value is not valid for the operation.",[14,109257,55737,109258,109260,109261,290],{},[17,109259,6119],{}," can convert strings to integers. But if the string does not contain a valid whole number, Python raises ",[17,109262,3483],{},[77,109264,80],{"id":79},[91,109266,109268],{"className":93,"code":109267,"language":95,"meta":96,"style":96},"text = \"abc\"\n\ntry:\n    number = int(text)\nexcept ValueError:\n    print(\"Input must be a valid number\")\n",[17,109269,109270,109282,109286,109292,109306,109314],{"__ignoreMap":96},[100,109271,109272,109274,109276,109278,109280],{"class":102,"line":103},[100,109273,4331],{"class":106},[100,109275,111],{"class":110},[100,109277,1708],{"class":205},[100,109279,83226],{"class":209},[100,109281,1714],{"class":205},[100,109283,109284],{"class":102,"line":135},[100,109285,139],{"emptyLinePlaceholder":138},[100,109287,109288,109290],{"class":102,"line":142},[100,109289,146],{"class":145},[100,109291,149],{"class":118},[100,109293,109294,109296,109298,109300,109302,109304],{"class":102,"line":152},[100,109295,98700],{"class":106},[100,109297,111],{"class":110},[100,109299,3389],{"class":191},[100,109301,170],{"class":118},[100,109303,4468],{"class":178},[100,109305,215],{"class":118},[100,109307,109308,109310,109312],{"class":102,"line":164},[100,109309,188],{"class":145},[100,109311,2794],{"class":191},[100,109313,149],{"class":118},[100,109315,109316,109318,109320,109322,109325,109327],{"class":102,"line":185},[100,109317,200],{"class":114},[100,109319,170],{"class":118},[100,109321,206],{"class":205},[100,109323,109324],{"class":209},"Input must be a valid number",[100,109326,206],{"class":205},[100,109328,215],{"class":118},[14,109330,241,109331,109333,109334,290],{},[17,109332,244],{}," when a value might have the right type but the wrong content, such as converting non-numeric text with ",[17,109335,6119],{},[77,109337,109339],{"id":109338},"what-valueerror-means","What ValueError means",[14,109341,571,109342,109344],{},[17,109343,3483],{}," happens when:",[40,109346,109347,109350],{},[43,109348,109349],{},"Python gets the kind of object it expects",[43,109351,109352],{},"But the value inside that object is not acceptable",[14,109354,1844],{},[91,109356,109357],{"className":93,"code":3492,"language":95,"meta":96,"style":96},[17,109358,109359],{"__ignoreMap":96},[100,109360,109361,109363,109365,109367,109369,109371],{"class":102,"line":103},[100,109362,3499],{"class":191},[100,109364,170],{"class":118},[100,109366,206],{"class":205},[100,109368,3506],{"class":209},[100,109370,206],{"class":205},[100,109372,215],{"class":118},[14,109374,3513,109375,109377],{},[17,109376,3483],{}," because:",[40,109379,109380,109384,109388],{},[43,109381,109382,86715],{},[17,109383,3519],{},[43,109385,109386,99509],{},[17,109387,6119],{},[43,109389,38150,109390,109392],{},[17,109391,3519],{}," is not a valid integer",[14,109394,24950,109395,290],{},[17,109396,3538],{},[40,109398,109399,109404],{},[43,109400,109401,109403],{},[17,109402,3483],{},": right type, wrong value",[43,109405,109406,109408],{},[17,109407,3538],{},": wrong type for the operation",[14,109410,109411,109412,109414,109415,290],{},"If you are specifically seeing an ",[17,109413,6119],{}," conversion error, see ",[295,109416,17024],{"href":17023},[77,109418,109420],{"id":109419},"common-places-beginners-see-valueerror","Common places beginners see ValueError",[14,109422,109423,109424,308],{},"Beginners often run into ",[17,109425,3483],{},[40,109427,109428,109439,109441,109444,109453],{},[43,109429,109430,109431,3178,109435],{},"Converting text to numbers with ",[295,109432,109433],{"href":81194},[17,109434,6119],{},[295,109436,109437],{"href":60491},[17,109438,59778],{},[43,109440,99091],{},[43,109442,109443],{},"Using string methods with invalid input",[43,109445,109446,109447,3178,109450],{},"Searching for missing items with methods like ",[17,109448,109449],{},"str.index()",[17,109451,109452],{},"tuple.index()",[43,109454,109455],{},"Passing invalid values to math-related functions",[14,109457,59443],{},[40,109459,109460,109465,109470,109473,109479],{},[43,109461,109462,109463],{},"Trying to convert non-numeric text with ",[17,109464,6119],{},[43,109466,109467,109468],{},"Trying to convert badly formatted text with ",[17,109469,59778],{},[43,109471,109472],{},"Unpacking too many or too few values",[43,109474,1357,109475,109478],{},[17,109476,109477],{},"index()"," for a value that is not present",[43,109480,109481],{},"Passing invalid values to library functions",[77,109483,109485],{"id":109484},"example-that-causes-valueerror","Example that causes ValueError",[14,109487,109488],{},"Here are two short examples.",[675,109490,109492,109493],{"id":109491},"example-1-intabc","Example 1: ",[17,109494,99083],{},[91,109496,109498],{"className":93,"code":109497,"language":95,"meta":96,"style":96},"text = \"abc\"\nnumber = int(text)\nprint(number)\n",[17,109499,109500,109512,109526],{"__ignoreMap":96},[100,109501,109502,109504,109506,109508,109510],{"class":102,"line":103},[100,109503,4331],{"class":106},[100,109505,111],{"class":110},[100,109507,1708],{"class":205},[100,109509,83226],{"class":209},[100,109511,1714],{"class":205},[100,109513,109514,109516,109518,109520,109522,109524],{"class":102,"line":135},[100,109515,1901],{"class":106},[100,109517,111],{"class":110},[100,109519,3389],{"class":191},[100,109521,170],{"class":118},[100,109523,4468],{"class":178},[100,109525,215],{"class":118},[100,109527,109528,109530,109532,109534],{"class":102,"line":142},[100,109529,372],{"class":114},[100,109531,170],{"class":118},[100,109533,631],{"class":178},[100,109535,215],{"class":118},[14,109537,218],{},[91,109539,109540],{"className":93,"code":99134,"language":95,"meta":96,"style":96},[17,109541,109542],{"__ignoreMap":96},[100,109543,109544,109546,109548,109550,109552,109554,109556,109558,109560,109562,109564,109566,109568],{"class":102,"line":103},[100,109545,3483],{"class":191},[100,109547,89],{"class":118},[100,109549,16983],{"class":106},[100,109551,71],{"class":145},[100,109553,3389],{"class":191},[100,109555,7562],{"class":118},[100,109557,16992],{"class":145},[100,109559,16995],{"class":106},[100,109561,356],{"class":122},[100,109563,89],{"class":118},[100,109565,1274],{"class":205},[100,109567,83226],{"class":209},[100,109569,3925],{"class":205},[14,109571,25013],{},[40,109573,109574,109578,109582],{},[43,109575,109576,86715],{},[17,109577,4468],{},[43,109579,109580,99509],{},[17,109581,6119],{},[43,109583,38150,109584,109392],{},[17,109585,99181],{},[675,109587,109589,109590],{"id":109588},"example-2-float125","Example 2: ",[17,109591,109592],{},"float(\"12,5\")",[91,109594,109596],{"className":93,"code":109595,"language":95,"meta":96,"style":96},"text = \"12,5\"\nnumber = float(text)\nprint(number)\n",[17,109597,109598,109611,109625],{"__ignoreMap":96},[100,109599,109600,109602,109604,109606,109609],{"class":102,"line":103},[100,109601,4331],{"class":106},[100,109603,111],{"class":110},[100,109605,1708],{"class":205},[100,109607,109608],{"class":209},"12,5",[100,109610,1714],{"class":205},[100,109612,109613,109615,109617,109619,109621,109623],{"class":102,"line":135},[100,109614,1901],{"class":106},[100,109616,111],{"class":110},[100,109618,106783],{"class":191},[100,109620,170],{"class":118},[100,109622,4468],{"class":178},[100,109624,215],{"class":118},[100,109626,109627,109629,109631,109633],{"class":102,"line":142},[100,109628,372],{"class":114},[100,109630,170],{"class":118},[100,109632,631],{"class":178},[100,109634,215],{"class":118},[14,109636,218],{},[91,109638,109640],{"className":93,"code":109639,"language":95,"meta":96,"style":96},"ValueError: could not convert string to float: '12,5'\n",[17,109641,109642],{"__ignoreMap":96},[100,109643,109644,109646,109648,109650,109652,109654,109656,109658,109660,109662],{"class":102,"line":103},[100,109645,3483],{"class":191},[100,109647,89],{"class":118},[100,109649,106967],{"class":106},[100,109651,1059],{"class":110},[100,109653,106972],{"class":106},[100,109655,60476],{"class":191},[100,109657,89],{"class":118},[100,109659,1274],{"class":205},[100,109661,109608],{"class":209},[100,109663,3925],{"class":205},[14,109665,25013],{},[40,109667,109668,109674],{},[43,109669,109670,109671],{},"Python expects decimal numbers like ",[17,109672,109673],{},"\"12.5\"",[43,109675,109676,109679],{},[17,109677,109678],{},"\"12,5\""," uses a comma instead of a dot",[14,109681,4964,109682,290],{},[295,109683,109685],{"href":109684},"\u002Ferrors\u002Fvalueerror-could-not-convert-string-to-float-fix\u002F","ValueError: could not convert string to float",[77,109687,109689],{"id":109688},"how-to-fix-valueerror","How to fix ValueError",[14,109691,109692,109693,109695],{},"There is no single fix for every ",[17,109694,3483],{},". The correct fix depends on the value that caused the problem.",[14,109697,109698],{},"Common ways to fix it:",[40,109700,109701,109704,109710,109713,109718],{},[43,109702,109703],{},"Check the actual value before passing it to a function",[43,109705,109706,109707,109709],{},"Clean user input with ",[17,109708,8739],{}," before conversion",[43,109711,109712],{},"Validate data before unpacking or parsing",[43,109714,241,109715,109717],{},[17,109716,244],{}," when bad input is possible",[43,109719,5271],{},[675,109721,109723],{"id":109722},"clean-input-before-converting","Clean input before converting",[91,109725,109727],{"className":93,"code":109726,"language":95,"meta":96,"style":96},"text = \" 42 \\n\"\nnumber = int(text.strip())\nprint(number)\n",[17,109728,109729,109743,109761],{"__ignoreMap":96},[100,109730,109731,109733,109735,109737,109739,109741],{"class":102,"line":103},[100,109732,4331],{"class":106},[100,109734,111],{"class":110},[100,109736,1708],{"class":205},[100,109738,100147],{"class":209},[100,109740,20187],{"class":527},[100,109742,1714],{"class":205},[100,109744,109745,109747,109749,109751,109753,109755,109757,109759],{"class":102,"line":135},[100,109746,1901],{"class":106},[100,109748,111],{"class":110},[100,109750,3389],{"class":191},[100,109752,170],{"class":118},[100,109754,4468],{"class":178},[100,109756,290],{"class":118},[100,109758,6235],{"class":178},[100,109760,3370],{"class":118},[100,109762,109763,109765,109767,109769],{"class":102,"line":142},[100,109764,372],{"class":114},[100,109766,170],{"class":118},[100,109768,631],{"class":178},[100,109770,215],{"class":118},[14,109772,218],{},[91,109774,109775],{"className":93,"code":99991,"language":95,"meta":96,"style":96},[17,109776,109777],{"__ignoreMap":96},[100,109778,109779],{"class":102,"line":103},[100,109780,99991],{"class":122},[14,109782,109783,109785],{},[17,109784,8739],{}," removes extra spaces and newline characters from the start and end of the string.",[675,109787,109789],{"id":109788},"validate-before-unpacking","Validate before unpacking",[91,109791,109793],{"className":93,"code":109792,"language":95,"meta":96,"style":96},"data = \"Alice,25\"\n\nparts = data.split(\",\")\n\nif len(parts) == 2:\n    name, age = parts\n    print(name)\n    print(age)\nelse:\n    print(\"Input must contain exactly two values\")\n",[17,109794,109795,109808,109812,109834,109838,109856,109868,109878,109888,109894],{"__ignoreMap":96},[100,109796,109797,109799,109801,109803,109806],{"class":102,"line":103},[100,109798,7909],{"class":106},[100,109800,111],{"class":110},[100,109802,1708],{"class":205},[100,109804,109805],{"class":209},"Alice,25",[100,109807,1714],{"class":205},[100,109809,109810],{"class":102,"line":135},[100,109811,139],{"emptyLinePlaceholder":138},[100,109813,109814,109816,109818,109820,109822,109824,109826,109828,109830,109832],{"class":102,"line":142},[100,109815,4345],{"class":106},[100,109817,111],{"class":110},[100,109819,12102],{"class":106},[100,109821,290],{"class":118},[100,109823,3922],{"class":178},[100,109825,170],{"class":118},[100,109827,206],{"class":205},[100,109829,126],{"class":209},[100,109831,206],{"class":205},[100,109833,215],{"class":118},[100,109835,109836],{"class":102,"line":152},[100,109837,139],{"emptyLinePlaceholder":138},[100,109839,109840,109842,109844,109846,109848,109850,109852,109854],{"class":102,"line":164},[100,109841,2736],{"class":145},[100,109843,3141],{"class":114},[100,109845,170],{"class":118},[100,109847,4373],{"class":178},[100,109849,6155],{"class":118},[100,109851,34516],{"class":110},[100,109853,129],{"class":122},[100,109855,149],{"class":118},[100,109857,109858,109860,109862,109864,109866],{"class":102,"line":185},[100,109859,11387],{"class":106},[100,109861,126],{"class":118},[100,109863,1676],{"class":106},[100,109865,111],{"class":110},[100,109867,100268],{"class":106},[100,109869,109870,109872,109874,109876],{"class":102,"line":197},[100,109871,200],{"class":114},[100,109873,170],{"class":118},[100,109875,2853],{"class":178},[100,109877,215],{"class":118},[100,109879,109880,109882,109884,109886],{"class":102,"line":771},[100,109881,200],{"class":114},[100,109883,170],{"class":118},[100,109885,2770],{"class":178},[100,109887,215],{"class":118},[100,109889,109890,109892],{"class":102,"line":787},[100,109891,4944],{"class":145},[100,109893,149],{"class":118},[100,109895,109896,109898,109900,109902,109905,109907],{"class":102,"line":5816},[100,109897,200],{"class":114},[100,109899,170],{"class":118},[100,109901,206],{"class":205},[100,109903,109904],{"class":209},"Input must contain exactly two values",[100,109906,206],{"class":205},[100,109908,215],{"class":118},[14,109910,109911],{},"This helps prevent unpacking errors when the number of values is wrong.",[14,109913,96393,109914,3178,109916,290],{},[295,109915,14983],{"href":14982},[295,109917,109918],{"href":82079},"how to convert a string to float in Python",[77,109920,8550],{"id":8549},[14,109922,43254,109923,22668],{},[17,109924,3483],{},[3282,109926,109927,109930,109933,109939,109942],{},[43,109928,109929],{},"Find the exact line named in the traceback",[43,109931,109932],{},"Print the value that caused the error",[43,109934,109935,109936,109938],{},"Check what ",[17,109937,5166],{}," returns",[43,109940,109941],{},"Check whether the value format matches what the function expects",[43,109943,109944],{},"Add validation or exception handling if needed",[14,109946,5295],{},[91,109948,109950],{"className":93,"code":109949,"language":95,"meta":96,"style":96},"print(value)\nprint(type(value))\nprint(repr(value))\nhelp(int)\nhelp(float)\n",[17,109951,109952,109962,109976,109990,110000],{"__ignoreMap":96},[100,109953,109954,109956,109958,109960],{"class":102,"line":103},[100,109955,372],{"class":114},[100,109957,170],{"class":118},[100,109959,757],{"class":178},[100,109961,215],{"class":118},[100,109963,109964,109966,109968,109970,109972,109974],{"class":102,"line":135},[100,109965,372],{"class":114},[100,109967,170],{"class":118},[100,109969,1250],{"class":191},[100,109971,170],{"class":118},[100,109973,757],{"class":178},[100,109975,182],{"class":118},[100,109977,109978,109980,109982,109984,109986,109988],{"class":102,"line":142},[100,109979,372],{"class":114},[100,109981,170],{"class":118},[100,109983,12228],{"class":114},[100,109985,170],{"class":118},[100,109987,757],{"class":178},[100,109989,182],{"class":118},[100,109991,109992,109994,109996,109998],{"class":102,"line":152},[100,109993,2478],{"class":114},[100,109995,170],{"class":118},[100,109997,3499],{"class":191},[100,109999,215],{"class":118},[100,110001,110002,110004,110006,110008],{"class":102,"line":164},[100,110003,2478],{"class":114},[100,110005,170],{"class":118},[100,110007,60476],{"class":191},[100,110009,215],{"class":118},[675,110011,37364,110013,110015],{"id":110012},"why-repr-is-useful",[17,110014,99698],{}," is useful",[14,110017,110018,110020],{},[17,110019,7046],{}," shows the value in a normal way.",[14,110022,110023,110025,110026,290],{},[17,110024,96563],{}," shows hidden characters too, such as spaces or ",[17,110027,20187],{},[14,110029,1844],{},[91,110031,110033],{"className":93,"code":110032,"language":95,"meta":96,"style":96},"value = \"42\\n\"\n\nprint(value)\nprint(repr(value))\n",[17,110034,110035,110049,110053,110063],{"__ignoreMap":96},[100,110036,110037,110039,110041,110043,110045,110047],{"class":102,"line":103},[100,110038,3318],{"class":106},[100,110040,111],{"class":110},[100,110042,1708],{"class":205},[100,110044,95521],{"class":209},[100,110046,20187],{"class":527},[100,110048,1714],{"class":205},[100,110050,110051],{"class":102,"line":135},[100,110052,139],{"emptyLinePlaceholder":138},[100,110054,110055,110057,110059,110061],{"class":102,"line":142},[100,110056,372],{"class":114},[100,110058,170],{"class":118},[100,110060,757],{"class":178},[100,110062,215],{"class":118},[100,110064,110065,110067,110069,110071,110073,110075],{"class":102,"line":152},[100,110066,372],{"class":114},[100,110068,170],{"class":118},[100,110070,12228],{"class":114},[100,110072,170],{"class":118},[100,110074,757],{"class":178},[100,110076,182],{"class":118},[14,110078,90852],{},[91,110080,110082],{"className":93,"code":110081,"language":95,"meta":96,"style":96},"42\n\n'42\\n'\n",[17,110083,110084,110088,110092],{"__ignoreMap":96},[100,110085,110086],{"class":102,"line":103},[100,110087,99991],{"class":122},[100,110089,110090],{"class":102,"line":135},[100,110091,139],{"emptyLinePlaceholder":138},[100,110093,110094,110096,110098,110100],{"class":102,"line":142},[100,110095,1280],{"class":3553},[100,110097,95521],{"class":3556},[100,110099,20187],{"class":107694},[100,110101,3925],{"class":3553},[14,110103,110104],{},"This helps you notice formatting problems that may be causing the error.",[77,110106,110108],{"id":110107},"valueerror-vs-typeerror","ValueError vs TypeError",[14,110110,46868],{},[40,110112,110113,110118],{},[43,110114,110115,110117],{},[17,110116,3483],{},": the type is acceptable, but the value is not",[43,110119,110120,110122],{},[17,110121,3538],{},": the type itself is wrong",[14,110124,1844],{},[91,110126,110128],{"className":93,"code":110127,"language":95,"meta":96,"style":96},"print(int(\"123\"))   # works\n\n# ValueError: string is allowed, but the content is invalid\n# int(\"abc\")\n\n# TypeError: None is not a valid type here\n# int(None)\n",[17,110129,110130,110150,110154,110159,110164,110168,110173],{"__ignoreMap":96},[100,110131,110132,110134,110136,110138,110140,110142,110144,110146,110148],{"class":102,"line":103},[100,110133,372],{"class":114},[100,110135,170],{"class":118},[100,110137,3499],{"class":191},[100,110139,170],{"class":118},[100,110141,206],{"class":205},[100,110143,3002],{"class":209},[100,110145,206],{"class":205},[100,110147,411],{"class":118},[100,110149,97025],{"class":414},[100,110151,110152],{"class":102,"line":135},[100,110153,139],{"emptyLinePlaceholder":138},[100,110155,110156],{"class":102,"line":142},[100,110157,110158],{"class":414},"# ValueError: string is allowed, but the content is invalid\n",[100,110160,110161],{"class":102,"line":152},[100,110162,110163],{"class":414},"# int(\"abc\")\n",[100,110165,110166],{"class":102,"line":164},[100,110167,139],{"emptyLinePlaceholder":138},[100,110169,110170],{"class":102,"line":185},[100,110171,110172],{"class":414},"# TypeError: None is not a valid type here\n",[100,110174,110175],{"class":102,"line":197},[100,110176,110177],{"class":414},"# int(None)\n",[14,110179,110180],{},"Another way to think about it:",[40,110182,110183,110193],{},[43,110184,110185,110187,110188,110190,110191],{},[17,110186,99181],{}," is still a string, and ",[17,110189,6119],{}," accepts strings, so this becomes ",[17,110192,3483],{},[43,110194,110195,110197,110198,110200,110201],{},[17,110196,930],{}," is not a string, number, or compatible value for ",[17,110199,6119],{}," in this context, so this becomes ",[17,110202,3538],{},[14,110204,110205,110206,290],{},"For a full comparison, see ",[295,110207,3270],{"href":3269},[77,110209,110211],{"id":110210},"when-to-use-try-except","When to use try-except",[14,110213,241,110214,110216],{},[17,110215,244],{}," when invalid values are possible and normal in your program.",[14,110218,110219],{},"Good examples:",[40,110221,110222,110224,110227,110230],{},[43,110223,43117],{},[43,110225,110226],{},"File content",[43,110228,110229],{},"Data from APIs",[43,110231,110232],{},"Data from external sources",[14,110234,1844],{},[91,110236,110238],{"className":93,"code":110237,"language":95,"meta":96,"style":96},"text = input(\"Enter a whole number: \")\n\ntry:\n    number = int(text)\n    print(\"You entered:\", number)\nexcept ValueError:\n    print(\"That is not a valid whole number\")\n",[17,110239,110240,110259,110263,110269,110283,110301,110309],{"__ignoreMap":96},[100,110241,110242,110244,110246,110248,110250,110252,110255,110257],{"class":102,"line":103},[100,110243,4331],{"class":106},[100,110245,111],{"class":110},[100,110247,7221],{"class":114},[100,110249,170],{"class":118},[100,110251,206],{"class":205},[100,110253,110254],{"class":209},"Enter a whole number: ",[100,110256,206],{"class":205},[100,110258,215],{"class":118},[100,110260,110261],{"class":102,"line":135},[100,110262,139],{"emptyLinePlaceholder":138},[100,110264,110265,110267],{"class":102,"line":142},[100,110266,146],{"class":145},[100,110268,149],{"class":118},[100,110270,110271,110273,110275,110277,110279,110281],{"class":102,"line":152},[100,110272,98700],{"class":106},[100,110274,111],{"class":110},[100,110276,3389],{"class":191},[100,110278,170],{"class":118},[100,110280,4468],{"class":178},[100,110282,215],{"class":118},[100,110284,110285,110287,110289,110291,110293,110295,110297,110299],{"class":102,"line":164},[100,110286,200],{"class":114},[100,110288,170],{"class":118},[100,110290,206],{"class":205},[100,110292,107320],{"class":209},[100,110294,206],{"class":205},[100,110296,126],{"class":118},[100,110298,84021],{"class":178},[100,110300,215],{"class":118},[100,110302,110303,110305,110307],{"class":102,"line":185},[100,110304,188],{"class":145},[100,110306,2794],{"class":191},[100,110308,149],{"class":118},[100,110310,110311,110313,110315,110317,110320,110322],{"class":102,"line":197},[100,110312,200],{"class":114},[100,110314,170],{"class":118},[100,110316,206],{"class":205},[100,110318,110319],{"class":209},"That is not a valid whole number",[100,110321,206],{"class":205},[100,110323,215],{"class":118},[14,110325,110326],{},"This is useful because users may enter unexpected text.",[14,110328,110329],{},"Do not hide the error silently.",[14,110331,13720],{},[91,110333,110335],{"className":93,"code":110334,"language":95,"meta":96,"style":96},"text = \"abc\"\n\ntry:\n    number = int(text)\nexcept ValueError:\n    pass\n",[17,110336,110337,110349,110353,110359,110373,110381],{"__ignoreMap":96},[100,110338,110339,110341,110343,110345,110347],{"class":102,"line":103},[100,110340,4331],{"class":106},[100,110342,111],{"class":110},[100,110344,1708],{"class":205},[100,110346,83226],{"class":209},[100,110348,1714],{"class":205},[100,110350,110351],{"class":102,"line":135},[100,110352,139],{"emptyLinePlaceholder":138},[100,110354,110355,110357],{"class":102,"line":142},[100,110356,146],{"class":145},[100,110358,149],{"class":118},[100,110360,110361,110363,110365,110367,110369,110371],{"class":102,"line":152},[100,110362,98700],{"class":106},[100,110364,111],{"class":110},[100,110366,3389],{"class":191},[100,110368,170],{"class":118},[100,110370,4468],{"class":178},[100,110372,215],{"class":118},[100,110374,110375,110377,110379],{"class":102,"line":164},[100,110376,188],{"class":145},[100,110378,2794],{"class":191},[100,110380,149],{"class":118},[100,110382,110383],{"class":102,"line":185},[100,110384,16214],{"class":145},[14,110386,110387],{},"This makes debugging harder because the program fails quietly.",[14,110389,110390],{},"Instead, show a helpful message or ask for new input.",[14,110392,110393,110394,290],{},"If you want to learn the pattern in more detail, see ",[295,110395,1029],{"href":2824},[77,110397,1514],{"id":1513},[675,110399,110401],{"id":110400},"what-is-the-difference-between-valueerror-and-typeerror","What is the difference between ValueError and TypeError?",[14,110403,110404,110406,110407,110409],{},[17,110405,3483],{}," means the type is accepted but the value is invalid. ",[17,110408,3538],{}," means the type itself is wrong for that operation.",[675,110411,110413],{"id":110412},"can-i-prevent-valueerror-before-it-happens","Can I prevent ValueError before it happens?",[14,110415,110416,110417,110419],{},"Yes. Validate or clean the value first, or use ",[17,110418,244],{}," when invalid input is possible.",[675,110421,110423],{"id":110422},"why-does-int-raise-valueerror","Why does int() raise ValueError?",[14,110425,110426],{},"Because the text you passed does not represent a valid integer.",[675,110428,110430],{"id":110429},"should-i-always-use-try-except-for-valueerror","Should I always use try-except for ValueError?",[14,110432,110433,110434,110436],{},"Not always. If you can validate the value first, that is often clearer. Use ",[17,110435,244],{}," when failure is expected or input is unpredictable.",[77,110438,1554],{"id":1553},[40,110440,110441,110445,110449,110453,110457,110461],{},[43,110442,110443],{},[295,110444,17024],{"href":17023},[43,110446,110447],{},[295,110448,109685],{"href":109684},[43,110450,110451],{},[295,110452,79329],{"href":79328},[43,110454,110455],{},[295,110456,79323],{"href":79322},[43,110458,110459],{},[295,110460,81195],{"href":81194},[43,110462,110463],{},[295,110464,60492],{"href":60491},[1589,110466,110467],{},"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 .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 .sP7_E, html code.shiki .sP7_E{--shiki-light:#39ADB5;--shiki-default:#24292E;--shiki-dark:#E1E4E8}html pre.shiki code .sZMiF, html code.shiki .sZMiF{--shiki-light:#E2931D;--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 .sptTA, html code.shiki .sptTA{--shiki-light:#6182B8;--shiki-default:#005CC5;--shiki-dark:#79B8FF}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 .srdBf, html code.shiki .srdBf{--shiki-light:#F76D47;--shiki-default:#005CC5;--shiki-dark:#79B8FF}html pre.shiki code .s_hVV, html code.shiki .s_hVV{--shiki-light:#90A4AE;--shiki-default:#005CC5;--shiki-dark:#79B8FF}html pre.shiki code .s2W-s, html code.shiki .s2W-s{--shiki-light:#39ADB5;--shiki-light-font-style:italic;--shiki-default:#032F62;--shiki-default-font-style:inherit;--shiki-dark:#9ECBFF;--shiki-dark-font-style:inherit}html pre.shiki code .sithA, html code.shiki .sithA{--shiki-light:#90A4AE;--shiki-light-font-style:italic;--shiki-default:#032F62;--shiki-default-font-style:inherit;--shiki-dark:#9ECBFF;--shiki-dark-font-style:inherit}html pre.shiki code .sfi6f, html code.shiki .sfi6f{--shiki-light:#90A4AE;--shiki-light-font-style:italic;--shiki-default:#005CC5;--shiki-default-font-style:inherit;--shiki-dark:#79B8FF;--shiki-dark-font-style:inherit}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}",{"title":96,"searchDepth":135,"depth":135,"links":110469},[110470,110471,110472,110473,110479,110483,110487,110488,110489,110495],{"id":79,"depth":135,"text":80},{"id":109338,"depth":135,"text":109339},{"id":109419,"depth":135,"text":109420},{"id":109484,"depth":135,"text":109485,"children":110474},[110475,110477],{"id":109491,"depth":142,"text":110476},"Example 1: int(\"abc\")",{"id":109588,"depth":142,"text":110478},"Example 2: float(\"12,5\")",{"id":109688,"depth":135,"text":109689,"children":110480},[110481,110482],{"id":109722,"depth":142,"text":109723},{"id":109788,"depth":142,"text":109789},{"id":8549,"depth":135,"text":8550,"children":110484},[110485],{"id":110012,"depth":142,"text":110486},"Why repr() is useful",{"id":110107,"depth":135,"text":110108},{"id":110210,"depth":135,"text":110211},{"id":1513,"depth":135,"text":1514,"children":110490},[110491,110492,110493,110494],{"id":110400,"depth":142,"text":110401},{"id":110412,"depth":142,"text":110413},{"id":110422,"depth":142,"text":110423},{"id":110429,"depth":142,"text":110430},{"id":1553,"depth":135,"text":1554},"Master valueerror in python causes and fixes in our comprehensive Python beginner guide.",{},"\u002Ferrors\u002Fvalueerror-in-python-causes-and-fixes",{"title":109242,"description":110496},"errors\u002Fvalueerror-in-python-causes-and-fixes","-o2I60oC8oNnBlMmKnrWTVJjj0f9YJQUwN198uQxh3Y",{"id":110503,"title":110504,"body":110505,"description":112326,"extension":1623,"meta":112327,"navigation":138,"path":96475,"seo":112328,"stem":112329,"__hash__":112330},"content\u002Ferrors\u002Fvalueerror-invalid-literal-for-int-with-base-10-fix.md","ValueError: invalid literal for int() with base 10 (Fix)",{"type":7,"value":110506,"toc":112286},[110507,110510,110516,110519,110531,110536,110538,110541,110642,110645,110662,110671,110673,110678,110681,110695,110703,110718,110720,110743,110745,110753,110755,110778,110780,110812,110819,110821,110824,110828,110869,110871,110903,110907,110949,110951,110984,110989,110993,111030,111032,111062,111065,111067,111069,111090,111092,111157,111165,111171,111173,111176,111180,111185,111231,111233,111241,111247,111250,111347,111349,111360,111366,111369,111461,111464,111468,111471,111539,111541,111550,111555,111559,111568,111574,111615,111617,111626,111633,111678,111680,111688,111694,111702,111706,111711,111713,111754,111756,111789,111793,111859,111861,111869,111872,111883,111887,111890,111901,111903,112007,112010,112012,112017,112019,112076,112079,112112,112117,112119,112121,112151,112153,112159,112167,112173,112179,112183,112190,112197,112202,112259,112261,112283],[10,110508,110504],{"id":110509},"valueerror-invalid-literal-for-int-with-base-10-fix",[14,110511,110512,110513,110515],{},"This error happens when Python tries to convert a value to an integer with ",[17,110514,6119],{},", but the value does not look like a valid whole number.",[14,110517,110518],{},"This page shows:",[40,110520,110521,110523,110526,110528],{},[43,110522,85186],{},[43,110524,110525],{},"common examples that cause it",[43,110527,85189],{},[43,110529,110530],{},"simple ways to fix it safely",[14,110532,110533,110534,290],{},"If you want a broader explanation of exceptions, see ",[295,110535,1587],{"href":1749},[77,110537,80],{"id":79},[14,110539,110540],{},"Use this when you expect a whole number from text input:",[91,110542,110544],{"className":93,"code":110543,"language":95,"meta":96,"style":96},"value = input(\"Enter a whole number: \").strip()\n\nif value.lstrip(\"-\").isdigit():\n    number = int(value)\n    print(number)\nelse:\n    print(\"Please enter a valid whole number.\")\n",[17,110545,110546,110568,110572,110597,110611,110621,110627],{"__ignoreMap":96},[100,110547,110548,110550,110552,110554,110556,110558,110560,110562,110564,110566],{"class":102,"line":103},[100,110549,3318],{"class":106},[100,110551,111],{"class":110},[100,110553,7221],{"class":114},[100,110555,170],{"class":118},[100,110557,206],{"class":205},[100,110559,110254],{"class":209},[100,110561,206],{"class":205},[100,110563,5799],{"class":118},[100,110565,6235],{"class":178},[100,110567,4734],{"class":118},[100,110569,110570],{"class":102,"line":135},[100,110571,139],{"emptyLinePlaceholder":138},[100,110573,110574,110576,110578,110580,110583,110585,110587,110589,110591,110593,110595],{"class":102,"line":142},[100,110575,2736],{"class":145},[100,110577,6747],{"class":106},[100,110579,290],{"class":118},[100,110581,110582],{"class":178},"lstrip",[100,110584,170],{"class":118},[100,110586,206],{"class":205},[100,110588,2193],{"class":209},[100,110590,206],{"class":205},[100,110592,5799],{"class":118},[100,110594,3367],{"class":178},[100,110596,1085],{"class":118},[100,110598,110599,110601,110603,110605,110607,110609],{"class":102,"line":152},[100,110600,98700],{"class":106},[100,110602,111],{"class":110},[100,110604,3389],{"class":191},[100,110606,170],{"class":118},[100,110608,757],{"class":178},[100,110610,215],{"class":118},[100,110612,110613,110615,110617,110619],{"class":102,"line":164},[100,110614,200],{"class":114},[100,110616,170],{"class":118},[100,110618,631],{"class":178},[100,110620,215],{"class":118},[100,110622,110623,110625],{"class":102,"line":185},[100,110624,4944],{"class":145},[100,110626,149],{"class":118},[100,110628,110629,110631,110633,110635,110638,110640],{"class":102,"line":197},[100,110630,200],{"class":114},[100,110632,170],{"class":118},[100,110634,206],{"class":205},[100,110636,110637],{"class":209},"Please enter a valid whole number.",[100,110639,206],{"class":205},[100,110641,215],{"class":118},[14,110643,110644],{},"This works well for:",[40,110646,110647,110653,110659],{},[43,110648,110649,110650],{},"numbers like ",[17,110651,110652],{},"\"10\"",[43,110654,110655,110656],{},"negative numbers like ",[17,110657,110658],{},"\"-3\"",[43,110660,110661],{},"values with extra spaces around them",[14,110663,110664,110665,110667,110668,290],{},"It does ",[22,110666,1059],{}," accept decimal input like ",[17,110669,110670],{},"\"3.14\"",[77,110672,5881],{"id":5880},[14,110674,106835,110675,110677],{},[17,110676,6119],{}," cannot turn a value into a whole number.",[14,110679,110680],{},"Valid integer strings look like:",[40,110682,110683,110687,110691],{},[43,110684,110685],{},[17,110686,110652],{},[43,110688,110689],{},[17,110690,110658],{},[43,110692,110693],{},[17,110694,85577],{},[14,110696,110697,110698,110700,110701,10042],{},"These are ",[22,110699,1059],{}," valid for ",[17,110702,6119],{},[40,110704,110705,110709,110713],{},[43,110706,110707],{},[17,110708,110670],{},[43,110710,110711],{},[17,110712,3519],{},[43,110714,110715],{},[17,110716,110717],{},"\"12a\"",[14,110719,580],{},[91,110721,110723],{"className":93,"code":110722,"language":95,"meta":96,"style":96},"print(int(\"42\"))\n",[17,110724,110725],{"__ignoreMap":96},[100,110726,110727,110729,110731,110733,110735,110737,110739,110741],{"class":102,"line":103},[100,110728,372],{"class":114},[100,110730,170],{"class":118},[100,110732,3499],{"class":191},[100,110734,170],{"class":118},[100,110736,206],{"class":205},[100,110738,95521],{"class":209},[100,110740,206],{"class":205},[100,110742,182],{"class":118},[14,110744,218],{},[91,110746,110747],{"className":93,"code":99991,"language":95,"meta":96,"style":96},[17,110748,110749],{"__ignoreMap":96},[100,110750,110751],{"class":102,"line":103},[100,110752,99991],{"class":122},[14,110754,2932],{},[91,110756,110758],{"className":93,"code":110757,"language":95,"meta":96,"style":96},"print(int(\"hello\"))\n",[17,110759,110760],{"__ignoreMap":96},[100,110761,110762,110764,110766,110768,110770,110772,110774,110776],{"class":102,"line":103},[100,110763,372],{"class":114},[100,110765,170],{"class":118},[100,110767,3499],{"class":191},[100,110769,170],{"class":118},[100,110771,206],{"class":205},[100,110773,3506],{"class":209},[100,110775,206],{"class":205},[100,110777,182],{"class":118},[14,110779,218],{},[91,110781,110782],{"className":93,"code":16972,"language":95,"meta":96,"style":96},[17,110783,110784],{"__ignoreMap":96},[100,110785,110786,110788,110790,110792,110794,110796,110798,110800,110802,110804,110806,110808,110810],{"class":102,"line":103},[100,110787,3483],{"class":191},[100,110789,89],{"class":118},[100,110791,16983],{"class":106},[100,110793,71],{"class":145},[100,110795,3389],{"class":191},[100,110797,7562],{"class":118},[100,110799,16992],{"class":145},[100,110801,16995],{"class":106},[100,110803,356],{"class":122},[100,110805,89],{"class":118},[100,110807,1274],{"class":205},[100,110809,3506],{"class":209},[100,110811,3925],{"class":205},[14,110813,110814,110815,12574,110817,290],{},"If you need a full beginner guide to ",[17,110816,6119],{},[295,110818,81195],{"href":81194},[77,110820,24958],{"id":24957},[14,110822,110823],{},"Here are three very common cases.",[675,110825,110827],{"id":110826},"example-1-letters-in-the-string","Example 1: letters in the string",[91,110829,110831],{"className":93,"code":110830,"language":95,"meta":96,"style":96},"value = \"hello\"\nnumber = int(value)\nprint(number)\n",[17,110832,110833,110845,110859],{"__ignoreMap":96},[100,110834,110835,110837,110839,110841,110843],{"class":102,"line":103},[100,110836,3318],{"class":106},[100,110838,111],{"class":110},[100,110840,1708],{"class":205},[100,110842,3506],{"class":209},[100,110844,1714],{"class":205},[100,110846,110847,110849,110851,110853,110855,110857],{"class":102,"line":135},[100,110848,1901],{"class":106},[100,110850,111],{"class":110},[100,110852,3389],{"class":191},[100,110854,170],{"class":118},[100,110856,757],{"class":178},[100,110858,215],{"class":118},[100,110860,110861,110863,110865,110867],{"class":102,"line":142},[100,110862,372],{"class":114},[100,110864,170],{"class":118},[100,110866,631],{"class":178},[100,110868,215],{"class":118},[14,110870,218],{},[91,110872,110873],{"className":93,"code":16972,"language":95,"meta":96,"style":96},[17,110874,110875],{"__ignoreMap":96},[100,110876,110877,110879,110881,110883,110885,110887,110889,110891,110893,110895,110897,110899,110901],{"class":102,"line":103},[100,110878,3483],{"class":191},[100,110880,89],{"class":118},[100,110882,16983],{"class":106},[100,110884,71],{"class":145},[100,110886,3389],{"class":191},[100,110888,7562],{"class":118},[100,110890,16992],{"class":145},[100,110892,16995],{"class":106},[100,110894,356],{"class":122},[100,110896,89],{"class":118},[100,110898,1274],{"class":205},[100,110900,3506],{"class":209},[100,110902,3925],{"class":205},[675,110904,110906],{"id":110905},"example-2-decimal-string","Example 2: decimal string",[91,110908,110910],{"className":93,"code":110909,"language":95,"meta":96,"style":96},"value = \"3.14\"\nnumber = int(value)\nprint(number)\n",[17,110911,110912,110925,110939],{"__ignoreMap":96},[100,110913,110914,110916,110918,110920,110923],{"class":102,"line":103},[100,110915,3318],{"class":106},[100,110917,111],{"class":110},[100,110919,1708],{"class":205},[100,110921,110922],{"class":209},"3.14",[100,110924,1714],{"class":205},[100,110926,110927,110929,110931,110933,110935,110937],{"class":102,"line":135},[100,110928,1901],{"class":106},[100,110930,111],{"class":110},[100,110932,3389],{"class":191},[100,110934,170],{"class":118},[100,110936,757],{"class":178},[100,110938,215],{"class":118},[100,110940,110941,110943,110945,110947],{"class":102,"line":142},[100,110942,372],{"class":114},[100,110944,170],{"class":118},[100,110946,631],{"class":178},[100,110948,215],{"class":118},[14,110950,218],{},[91,110952,110954],{"className":93,"code":110953,"language":95,"meta":96,"style":96},"ValueError: invalid literal for int() with base 10: '3.14'\n",[17,110955,110956],{"__ignoreMap":96},[100,110957,110958,110960,110962,110964,110966,110968,110970,110972,110974,110976,110978,110980,110982],{"class":102,"line":103},[100,110959,3483],{"class":191},[100,110961,89],{"class":118},[100,110963,16983],{"class":106},[100,110965,71],{"class":145},[100,110967,3389],{"class":191},[100,110969,7562],{"class":118},[100,110971,16992],{"class":145},[100,110973,16995],{"class":106},[100,110975,356],{"class":122},[100,110977,89],{"class":118},[100,110979,1274],{"class":205},[100,110981,110922],{"class":209},[100,110983,3925],{"class":205},[14,110985,7194,110986,110988],{},[17,110987,110670],{}," is not an integer string.",[675,110990,110992],{"id":110991},"example-3-empty-string","Example 3: empty string",[91,110994,110996],{"className":93,"code":110995,"language":95,"meta":96,"style":96},"value = \"\"\nnumber = int(value)\nprint(number)\n",[17,110997,110998,111006,111020],{"__ignoreMap":96},[100,110999,111000,111002,111004],{"class":102,"line":103},[100,111001,3318],{"class":106},[100,111003,111],{"class":110},[100,111005,1696],{"class":205},[100,111007,111008,111010,111012,111014,111016,111018],{"class":102,"line":135},[100,111009,1901],{"class":106},[100,111011,111],{"class":110},[100,111013,3389],{"class":191},[100,111015,170],{"class":118},[100,111017,757],{"class":178},[100,111019,215],{"class":118},[100,111021,111022,111024,111026,111028],{"class":102,"line":142},[100,111023,372],{"class":114},[100,111025,170],{"class":118},[100,111027,631],{"class":178},[100,111029,215],{"class":118},[14,111031,218],{},[91,111033,111035],{"className":93,"code":111034,"language":95,"meta":96,"style":96},"ValueError: invalid literal for int() with base 10: ''\n",[17,111036,111037],{"__ignoreMap":96},[100,111038,111039,111041,111043,111045,111047,111049,111051,111053,111055,111057,111059],{"class":102,"line":103},[100,111040,3483],{"class":191},[100,111042,89],{"class":118},[100,111044,16983],{"class":106},[100,111046,71],{"class":145},[100,111048,3389],{"class":191},[100,111050,7562],{"class":118},[100,111052,16992],{"class":145},[100,111054,16995],{"class":106},[100,111056,356],{"class":122},[100,111058,89],{"class":118},[100,111060,111061],{"class":205}," ''\n",[14,111063,111064],{},"An empty string is not a number.",[77,111066,25091],{"id":25090},[14,111068,38066],{},[40,111070,111071,111075,111078,111081,111084,111087],{},[43,111072,111073,95962],{},[17,111074,14906],{},[43,111076,111077],{},"the string contains letters",[43,111079,111080],{},"the string contains a decimal point",[43,111082,111083],{},"the string is empty",[43,111085,111086],{},"the string contains commas or other formatting",[43,111088,111089],{},"the value has unexpected spaces or hidden characters",[14,111091,580],{},[91,111093,111095],{"className":93,"code":111094,"language":95,"meta":96,"style":96},"value = input(\"Enter your age: \")\nprint(type(value))\nprint(repr(value))\nprint(int(value))\n",[17,111096,111097,111115,111129,111143],{"__ignoreMap":96},[100,111098,111099,111101,111103,111105,111107,111109,111111,111113],{"class":102,"line":103},[100,111100,3318],{"class":106},[100,111102,111],{"class":110},[100,111104,7221],{"class":114},[100,111106,170],{"class":118},[100,111108,206],{"class":205},[100,111110,7173],{"class":209},[100,111112,206],{"class":205},[100,111114,215],{"class":118},[100,111116,111117,111119,111121,111123,111125,111127],{"class":102,"line":135},[100,111118,372],{"class":114},[100,111120,170],{"class":118},[100,111122,1250],{"class":191},[100,111124,170],{"class":118},[100,111126,757],{"class":178},[100,111128,182],{"class":118},[100,111130,111131,111133,111135,111137,111139,111141],{"class":102,"line":142},[100,111132,372],{"class":114},[100,111134,170],{"class":118},[100,111136,12228],{"class":114},[100,111138,170],{"class":118},[100,111140,757],{"class":178},[100,111142,182],{"class":118},[100,111144,111145,111147,111149,111151,111153,111155],{"class":102,"line":152},[100,111146,372],{"class":114},[100,111148,170],{"class":118},[100,111150,3499],{"class":191},[100,111152,170],{"class":118},[100,111154,757],{"class":178},[100,111156,182],{"class":118},[14,111158,85669,111159,111162,111163,96444],{},[17,111160,111161],{},"12a",", the conversion fails because ",[17,111164,110717],{},[14,111166,111167,111168,111170],{},"If you are working with input from a user, this is a common problem. See ",[295,111169,85785],{"href":85784}," for more step-by-step examples.",[77,111172,6244],{"id":6243},[14,111174,111175],{},"There are several good ways to fix this, depending on your input.",[675,111177,111179],{"id":111178},"remove-extra-spaces","Remove extra spaces",[14,111181,241,111182,111184],{},[17,111183,6095],{}," to remove spaces around the text:",[91,111186,111188],{"className":93,"code":111187,"language":95,"meta":96,"style":96},"value = \"  25  \"\nnumber = int(value.strip())\nprint(number)\n",[17,111189,111190,111203,111221],{"__ignoreMap":96},[100,111191,111192,111194,111196,111198,111201],{"class":102,"line":103},[100,111193,3318],{"class":106},[100,111195,111],{"class":110},[100,111197,1708],{"class":205},[100,111199,111200],{"class":209},"  25  ",[100,111202,1714],{"class":205},[100,111204,111205,111207,111209,111211,111213,111215,111217,111219],{"class":102,"line":135},[100,111206,1901],{"class":106},[100,111208,111],{"class":110},[100,111210,3389],{"class":191},[100,111212,170],{"class":118},[100,111214,757],{"class":178},[100,111216,290],{"class":118},[100,111218,6235],{"class":178},[100,111220,3370],{"class":118},[100,111222,111223,111225,111227,111229],{"class":102,"line":142},[100,111224,372],{"class":114},[100,111226,170],{"class":118},[100,111228,631],{"class":178},[100,111230,215],{"class":118},[14,111232,218],{},[91,111234,111235],{"className":93,"code":77356,"language":95,"meta":96,"style":96},[17,111236,111237],{"__ignoreMap":96},[100,111238,111239],{"class":102,"line":103},[100,111240,77356],{"class":122},[675,111242,111244,111245],{"id":111243},"validate-before-calling-int","Validate before calling ",[17,111246,6119],{},[14,111248,111249],{},"If you only want whole numbers, check the string first:",[91,111251,111253],{"className":93,"code":111252,"language":95,"meta":96,"style":96},"value = \"-12\".strip()\n\nif value.lstrip(\"-\").isdigit():\n    number = int(value)\n    print(number)\nelse:\n    print(\"Invalid whole number\")\n",[17,111254,111255,111274,111278,111302,111316,111326,111332],{"__ignoreMap":96},[100,111256,111257,111259,111261,111263,111266,111268,111270,111272],{"class":102,"line":103},[100,111258,3318],{"class":106},[100,111260,111],{"class":110},[100,111262,1708],{"class":205},[100,111264,111265],{"class":209},"-12",[100,111267,206],{"class":205},[100,111269,290],{"class":118},[100,111271,6235],{"class":178},[100,111273,4734],{"class":118},[100,111275,111276],{"class":102,"line":135},[100,111277,139],{"emptyLinePlaceholder":138},[100,111279,111280,111282,111284,111286,111288,111290,111292,111294,111296,111298,111300],{"class":102,"line":142},[100,111281,2736],{"class":145},[100,111283,6747],{"class":106},[100,111285,290],{"class":118},[100,111287,110582],{"class":178},[100,111289,170],{"class":118},[100,111291,206],{"class":205},[100,111293,2193],{"class":209},[100,111295,206],{"class":205},[100,111297,5799],{"class":118},[100,111299,3367],{"class":178},[100,111301,1085],{"class":118},[100,111303,111304,111306,111308,111310,111312,111314],{"class":102,"line":152},[100,111305,98700],{"class":106},[100,111307,111],{"class":110},[100,111309,3389],{"class":191},[100,111311,170],{"class":118},[100,111313,757],{"class":178},[100,111315,215],{"class":118},[100,111317,111318,111320,111322,111324],{"class":102,"line":164},[100,111319,200],{"class":114},[100,111321,170],{"class":118},[100,111323,631],{"class":178},[100,111325,215],{"class":118},[100,111327,111328,111330],{"class":102,"line":185},[100,111329,4944],{"class":145},[100,111331,149],{"class":118},[100,111333,111334,111336,111338,111340,111343,111345],{"class":102,"line":197},[100,111335,200],{"class":114},[100,111337,170],{"class":118},[100,111339,206],{"class":205},[100,111341,111342],{"class":209},"Invalid whole number",[100,111344,206],{"class":205},[100,111346,215],{"class":118},[14,111348,218],{},[91,111350,111352],{"className":93,"code":111351,"language":95,"meta":96,"style":96},"-12\n",[17,111353,111354],{"__ignoreMap":96},[100,111355,111356,111358],{"class":102,"line":103},[100,111357,2193],{"class":110},[100,111359,96363],{"class":122},[675,111361,241,111363,111365],{"id":111362},"use-try-except-for-safer-input-handling",[17,111364,244],{}," for safer input handling",[14,111367,111368],{},"This is often the easiest and safest pattern:",[91,111370,111372],{"className":93,"code":111371,"language":95,"meta":96,"style":96},"value = input(\"Enter a whole number: \").strip()\n\ntry:\n    number = int(value)\n    print(\"You entered:\", number)\nexcept ValueError:\n    print(\"That is not a valid whole number.\")\n",[17,111373,111374,111396,111400,111406,111420,111438,111446],{"__ignoreMap":96},[100,111375,111376,111378,111380,111382,111384,111386,111388,111390,111392,111394],{"class":102,"line":103},[100,111377,3318],{"class":106},[100,111379,111],{"class":110},[100,111381,7221],{"class":114},[100,111383,170],{"class":118},[100,111385,206],{"class":205},[100,111387,110254],{"class":209},[100,111389,206],{"class":205},[100,111391,5799],{"class":118},[100,111393,6235],{"class":178},[100,111395,4734],{"class":118},[100,111397,111398],{"class":102,"line":135},[100,111399,139],{"emptyLinePlaceholder":138},[100,111401,111402,111404],{"class":102,"line":142},[100,111403,146],{"class":145},[100,111405,149],{"class":118},[100,111407,111408,111410,111412,111414,111416,111418],{"class":102,"line":152},[100,111409,98700],{"class":106},[100,111411,111],{"class":110},[100,111413,3389],{"class":191},[100,111415,170],{"class":118},[100,111417,757],{"class":178},[100,111419,215],{"class":118},[100,111421,111422,111424,111426,111428,111430,111432,111434,111436],{"class":102,"line":164},[100,111423,200],{"class":114},[100,111425,170],{"class":118},[100,111427,206],{"class":205},[100,111429,107320],{"class":209},[100,111431,206],{"class":205},[100,111433,126],{"class":118},[100,111435,84021],{"class":178},[100,111437,215],{"class":118},[100,111439,111440,111442,111444],{"class":102,"line":185},[100,111441,188],{"class":145},[100,111443,2794],{"class":191},[100,111445,149],{"class":118},[100,111447,111448,111450,111452,111454,111457,111459],{"class":102,"line":197},[100,111449,200],{"class":114},[100,111451,170],{"class":118},[100,111453,206],{"class":205},[100,111455,111456],{"class":209},"That is not a valid whole number.",[100,111458,206],{"class":205},[100,111460,215],{"class":118},[14,111462,111463],{},"This is a good approach when input may be invalid.",[675,111465,111467],{"id":111466},"convert-carefully-when-the-input-may-not-be-clean","Convert carefully when the input may not be clean",[14,111469,111470],{},"If your string may need cleaning first, fix the text before converting:",[91,111472,111474],{"className":93,"code":111473,"language":95,"meta":96,"style":96},"value = \"1,000\"\ncleaned = value.replace(\",\", \"\")\nnumber = int(cleaned)\nprint(number)\n",[17,111475,111476,111489,111515,111529],{"__ignoreMap":96},[100,111477,111478,111480,111482,111484,111487],{"class":102,"line":103},[100,111479,3318],{"class":106},[100,111481,111],{"class":110},[100,111483,1708],{"class":205},[100,111485,111486],{"class":209},"1,000",[100,111488,1714],{"class":205},[100,111490,111491,111493,111495,111497,111499,111501,111503,111505,111507,111509,111511,111513],{"class":102,"line":135},[100,111492,8872],{"class":106},[100,111494,111],{"class":110},[100,111496,6747],{"class":106},[100,111498,290],{"class":118},[100,111500,104438],{"class":178},[100,111502,170],{"class":118},[100,111504,206],{"class":205},[100,111506,126],{"class":209},[100,111508,206],{"class":205},[100,111510,126],{"class":118},[100,111512,11901],{"class":205},[100,111514,215],{"class":118},[100,111516,111517,111519,111521,111523,111525,111527],{"class":102,"line":142},[100,111518,1901],{"class":106},[100,111520,111],{"class":110},[100,111522,3389],{"class":191},[100,111524,170],{"class":118},[100,111526,8915],{"class":178},[100,111528,215],{"class":118},[100,111530,111531,111533,111535,111537],{"class":102,"line":152},[100,111532,372],{"class":114},[100,111534,170],{"class":118},[100,111536,631],{"class":178},[100,111538,215],{"class":118},[14,111540,218],{},[91,111542,111544],{"className":93,"code":111543,"language":95,"meta":96,"style":96},"1000\n",[17,111545,111546],{"__ignoreMap":96},[100,111547,111548],{"class":102,"line":103},[100,111549,111543],{"class":122},[14,111551,43096,111552,290],{},[295,111553,111554],{"href":14982},"how to convert string to int in Python",[77,111556,111558],{"id":111557},"fix-for-decimal-strings","Fix for decimal strings",[14,111560,111561,111564,111565,111567],{},[17,111562,111563],{},"int(\"3.14\")"," fails because ",[17,111566,110670],{}," is a decimal string, not a whole-number string.",[675,111569,241,111571,111573],{"id":111570},"use-float-if-you-need-decimals",[17,111572,59778],{}," if you need decimals",[91,111575,111577],{"className":93,"code":111576,"language":95,"meta":96,"style":96},"value = \"3.14\"\nnumber = float(value)\nprint(number)\n",[17,111578,111579,111591,111605],{"__ignoreMap":96},[100,111580,111581,111583,111585,111587,111589],{"class":102,"line":103},[100,111582,3318],{"class":106},[100,111584,111],{"class":110},[100,111586,1708],{"class":205},[100,111588,110922],{"class":209},[100,111590,1714],{"class":205},[100,111592,111593,111595,111597,111599,111601,111603],{"class":102,"line":135},[100,111594,1901],{"class":106},[100,111596,111],{"class":110},[100,111598,106783],{"class":191},[100,111600,170],{"class":118},[100,111602,757],{"class":178},[100,111604,215],{"class":118},[100,111606,111607,111609,111611,111613],{"class":102,"line":142},[100,111608,372],{"class":114},[100,111610,170],{"class":118},[100,111612,631],{"class":178},[100,111614,215],{"class":118},[14,111616,218],{},[91,111618,111620],{"className":93,"code":111619,"language":95,"meta":96,"style":96},"3.14\n",[17,111621,111622],{"__ignoreMap":96},[100,111623,111624],{"class":102,"line":103},[100,111625,111619],{"class":122},[675,111627,111629,111630,111632],{"id":111628},"convert-to-int-only-if-you-want-to-drop-the-decimal-part","Convert to ",[17,111631,3499],{}," only if you want to drop the decimal part",[91,111634,111636],{"className":93,"code":111635,"language":95,"meta":96,"style":96},"value = \"3.14\"\nnumber = int(float(value))\nprint(number)\n",[17,111637,111638,111650,111668],{"__ignoreMap":96},[100,111639,111640,111642,111644,111646,111648],{"class":102,"line":103},[100,111641,3318],{"class":106},[100,111643,111],{"class":110},[100,111645,1708],{"class":205},[100,111647,110922],{"class":209},[100,111649,1714],{"class":205},[100,111651,111652,111654,111656,111658,111660,111662,111664,111666],{"class":102,"line":135},[100,111653,1901],{"class":106},[100,111655,111],{"class":110},[100,111657,3389],{"class":191},[100,111659,170],{"class":118},[100,111661,60476],{"class":191},[100,111663,170],{"class":118},[100,111665,757],{"class":178},[100,111667,182],{"class":118},[100,111669,111670,111672,111674,111676],{"class":102,"line":142},[100,111671,372],{"class":114},[100,111673,170],{"class":118},[100,111675,631],{"class":178},[100,111677,215],{"class":118},[14,111679,218],{},[91,111681,111682],{"className":93,"code":1500,"language":95,"meta":96,"style":96},[17,111683,111684],{"__ignoreMap":96},[100,111685,111686],{"class":102,"line":103},[100,111687,1500],{"class":122},[14,111689,111690,111691,111693],{},"Be careful: this removes the decimal part. It does ",[22,111692,1059],{}," round the number.",[14,111695,111696,111697,111699,111700,290],{},"If you see a similar error with ",[17,111698,59778],{},", read ",[295,111701,106695],{"href":109684},[77,111703,111705],{"id":111704},"fix-for-formatted-numbers","Fix for formatted numbers",[14,111707,111708,111709,10322],{},"Some strings look like numbers to humans but are not valid for ",[17,111710,6119],{},[14,111712,1844],{},[91,111714,111716],{"className":93,"code":111715,"language":95,"meta":96,"style":96},"value = \"1,000\"\nnumber = int(value)\nprint(number)\n",[17,111717,111718,111730,111744],{"__ignoreMap":96},[100,111719,111720,111722,111724,111726,111728],{"class":102,"line":103},[100,111721,3318],{"class":106},[100,111723,111],{"class":110},[100,111725,1708],{"class":205},[100,111727,111486],{"class":209},[100,111729,1714],{"class":205},[100,111731,111732,111734,111736,111738,111740,111742],{"class":102,"line":135},[100,111733,1901],{"class":106},[100,111735,111],{"class":110},[100,111737,3389],{"class":191},[100,111739,170],{"class":118},[100,111741,757],{"class":178},[100,111743,215],{"class":118},[100,111745,111746,111748,111750,111752],{"class":102,"line":142},[100,111747,372],{"class":114},[100,111749,170],{"class":118},[100,111751,631],{"class":178},[100,111753,215],{"class":118},[14,111755,7710],{},[91,111757,111759],{"className":93,"code":111758,"language":95,"meta":96,"style":96},"ValueError: invalid literal for int() with base 10: '1,000'\n",[17,111760,111761],{"__ignoreMap":96},[100,111762,111763,111765,111767,111769,111771,111773,111775,111777,111779,111781,111783,111785,111787],{"class":102,"line":103},[100,111764,3483],{"class":191},[100,111766,89],{"class":118},[100,111768,16983],{"class":106},[100,111770,71],{"class":145},[100,111772,3389],{"class":191},[100,111774,7562],{"class":118},[100,111776,16992],{"class":145},[100,111778,16995],{"class":106},[100,111780,356],{"class":122},[100,111782,89],{"class":118},[100,111784,1274],{"class":205},[100,111786,111486],{"class":209},[100,111788,3925],{"class":205},[675,111790,111792],{"id":111791},"remove-commas-first","Remove commas first",[91,111794,111795],{"className":93,"code":111473,"language":95,"meta":96,"style":96},[17,111796,111797,111809,111835,111849],{"__ignoreMap":96},[100,111798,111799,111801,111803,111805,111807],{"class":102,"line":103},[100,111800,3318],{"class":106},[100,111802,111],{"class":110},[100,111804,1708],{"class":205},[100,111806,111486],{"class":209},[100,111808,1714],{"class":205},[100,111810,111811,111813,111815,111817,111819,111821,111823,111825,111827,111829,111831,111833],{"class":102,"line":135},[100,111812,8872],{"class":106},[100,111814,111],{"class":110},[100,111816,6747],{"class":106},[100,111818,290],{"class":118},[100,111820,104438],{"class":178},[100,111822,170],{"class":118},[100,111824,206],{"class":205},[100,111826,126],{"class":209},[100,111828,206],{"class":205},[100,111830,126],{"class":118},[100,111832,11901],{"class":205},[100,111834,215],{"class":118},[100,111836,111837,111839,111841,111843,111845,111847],{"class":102,"line":142},[100,111838,1901],{"class":106},[100,111840,111],{"class":110},[100,111842,3389],{"class":191},[100,111844,170],{"class":118},[100,111846,8915],{"class":178},[100,111848,215],{"class":118},[100,111850,111851,111853,111855,111857],{"class":102,"line":152},[100,111852,372],{"class":114},[100,111854,170],{"class":118},[100,111856,631],{"class":178},[100,111858,215],{"class":118},[14,111860,218],{},[91,111862,111863],{"className":93,"code":111543,"language":95,"meta":96,"style":96},[17,111864,111865],{"__ignoreMap":96},[100,111866,111867],{"class":102,"line":103},[100,111868,111543],{"class":122},[14,111870,111871],{},"Be careful when cleaning input:",[40,111873,111874,111877,111880],{},[43,111875,111876],{},"make sure the formatting really should be removed",[43,111878,111879],{},"do not remove characters blindly",[43,111881,111882],{},"confirm the cleaned value still means the same thing",[77,111884,111886],{"id":111885},"safer-pattern-for-user-input","Safer pattern for user input",[14,111888,111889],{},"A beginner-friendly pattern is:",[3282,111891,111892,111895,111898],{},[43,111893,111894],{},"ask for input",[43,111896,111897],{},"try to convert it",[43,111899,111900],{},"show a clear message if it fails",[14,111902,1844],{},[91,111904,111906],{"className":93,"code":111905,"language":95,"meta":96,"style":96},"while True:\n    value = input(\"Enter a whole number: \").strip()\n\n    try:\n        number = int(value)\n        print(\"Valid number:\", number)\n        break\n    except ValueError:\n        print(\"Please enter a valid whole number.\")\n",[17,111907,111908,111916,111938,111942,111948,111962,111981,111985,111993],{"__ignoreMap":96},[100,111909,111910,111912,111914],{"class":102,"line":103},[100,111911,721],{"class":145},[100,111913,159],{"class":158},[100,111915,149],{"class":118},[100,111917,111918,111920,111922,111924,111926,111928,111930,111932,111934,111936],{"class":102,"line":135},[100,111919,53992],{"class":106},[100,111921,111],{"class":110},[100,111923,7221],{"class":114},[100,111925,170],{"class":118},[100,111927,206],{"class":205},[100,111929,110254],{"class":209},[100,111931,206],{"class":205},[100,111933,5799],{"class":118},[100,111935,6235],{"class":178},[100,111937,4734],{"class":118},[100,111939,111940],{"class":102,"line":142},[100,111941,139],{"emptyLinePlaceholder":138},[100,111943,111944,111946],{"class":102,"line":152},[100,111945,730],{"class":145},[100,111947,149],{"class":118},[100,111949,111950,111952,111954,111956,111958,111960],{"class":102,"line":164},[100,111951,101750],{"class":106},[100,111953,111],{"class":110},[100,111955,3389],{"class":191},[100,111957,170],{"class":118},[100,111959,757],{"class":178},[100,111961,215],{"class":118},[100,111963,111964,111966,111968,111970,111973,111975,111977,111979],{"class":102,"line":185},[100,111965,167],{"class":114},[100,111967,170],{"class":118},[100,111969,206],{"class":205},[100,111971,111972],{"class":209},"Valid number:",[100,111974,206],{"class":205},[100,111976,126],{"class":118},[100,111978,84021],{"class":178},[100,111980,215],{"class":118},[100,111982,111983],{"class":102,"line":197},[100,111984,790],{"class":145},[100,111986,111987,111989,111991],{"class":102,"line":771},[100,111988,764],{"class":145},[100,111990,2794],{"class":191},[100,111992,149],{"class":118},[100,111994,111995,111997,111999,112001,112003,112005],{"class":102,"line":787},[100,111996,167],{"class":114},[100,111998,170],{"class":118},[100,112000,206],{"class":205},[100,112002,110637],{"class":209},[100,112004,206],{"class":205},[100,112006,215],{"class":118},[14,112008,112009],{},"This keeps asking until the user enters valid input.",[77,112011,1168],{"id":1167},[14,112013,34387,112014,112016],{},[17,112015,6119],{}," is failing, check the value before converting it.",[14,112018,5295],{},[91,112020,112022],{"className":93,"code":112021,"language":95,"meta":96,"style":96},"print(value)\nprint(repr(value))\nprint(type(value))\nprint(value.strip())\n",[17,112023,112024,112034,112048,112062],{"__ignoreMap":96},[100,112025,112026,112028,112030,112032],{"class":102,"line":103},[100,112027,372],{"class":114},[100,112029,170],{"class":118},[100,112031,757],{"class":178},[100,112033,215],{"class":118},[100,112035,112036,112038,112040,112042,112044,112046],{"class":102,"line":135},[100,112037,372],{"class":114},[100,112039,170],{"class":118},[100,112041,12228],{"class":114},[100,112043,170],{"class":118},[100,112045,757],{"class":178},[100,112047,182],{"class":118},[100,112049,112050,112052,112054,112056,112058,112060],{"class":102,"line":142},[100,112051,372],{"class":114},[100,112053,170],{"class":118},[100,112055,1250],{"class":191},[100,112057,170],{"class":118},[100,112059,757],{"class":178},[100,112061,182],{"class":118},[100,112063,112064,112066,112068,112070,112072,112074],{"class":102,"line":152},[100,112065,372],{"class":114},[100,112067,170],{"class":118},[100,112069,757],{"class":178},[100,112071,290],{"class":118},[100,112073,6235],{"class":178},[100,112075,3370],{"class":118},[14,112077,112078],{},"What to look for:",[40,112080,112081,112084,112090,112095,112101,112106],{},[43,112082,112083],{},"spaces at the beginning or end",[43,112085,112086,112087],{},"commas like ",[17,112088,112089],{},"\"1,000\"",[43,112091,112092,112093],{},"empty strings like ",[17,112094,11875],{},[43,112096,112097,112098],{},"decimal points like ",[17,112099,112100],{},"\"4.5\"",[43,112102,112103,112104],{},"letters like ",[17,112105,99181],{},[43,112107,112108,112109],{},"hidden characters such as ",[17,112110,112111],{},"\"\\n\"",[14,112113,112114,112116],{},[17,112115,107582],{}," is especially useful because it shows hidden characters clearly.",[77,112118,7117],{"id":7116},[14,112120,46698],{},[40,112122,112123,112129,112137,112142,112145],{},[43,112124,45,112125,107038,112127],{},[17,112126,6119],{},[17,112128,99181],{},[43,112130,112131,112132,112134,112135],{},"trying ",[17,112133,6119],{}," on a decimal string like ",[17,112136,112100],{},[43,112138,17889,112139,112141],{},[17,112140,6119],{}," on an empty string",[43,112143,112144],{},"reading user input that contains spaces or unexpected characters",[43,112146,112147,112148,112150],{},"trying to convert formatted numbers like ",[17,112149,112089],{}," without cleaning them first",[77,112152,1514],{"id":1513},[675,112154,9137,112156,112158],{"id":112155},"why-does-int314-fail",[17,112157,111563],{}," fail?",[14,112160,14478,112161,112163,112164,112166],{},[17,112162,110670],{}," is a decimal string, not an integer string. Use ",[17,112165,59778],{}," for decimals.",[675,112168,20428,112170,112172],{"id":112169},"does-int-work-with-spaces",[17,112171,6119],{}," work with spaces?",[14,112174,112175,112176,112178],{},"It can handle some surrounding spaces, but using ",[17,112177,6095],{}," is safer for beginners.",[675,112180,112182],{"id":112181},"how-do-i-convert-user-input-to-an-integer-safely","How do I convert user input to an integer safely?",[14,112184,241,112185,112187,112188,290],{},[17,112186,244],{}," or check the string first before calling ",[17,112189,6119],{},[675,112191,112193,112194,112196],{"id":112192},"how-do-i-convert-1000-to-an-integer","How do I convert ",[17,112195,112089],{}," to an integer?",[14,112198,112199,112200,89],{},"Remove the comma first, then call ",[17,112201,6119],{},[91,112203,112205],{"className":93,"code":112204,"language":95,"meta":96,"style":96},"value = \"1,000\"\nnumber = int(value.replace(\",\", \"\"))\nprint(number)\n",[17,112206,112207,112219,112249],{"__ignoreMap":96},[100,112208,112209,112211,112213,112215,112217],{"class":102,"line":103},[100,112210,3318],{"class":106},[100,112212,111],{"class":110},[100,112214,1708],{"class":205},[100,112216,111486],{"class":209},[100,112218,1714],{"class":205},[100,112220,112221,112223,112225,112227,112229,112231,112233,112235,112237,112239,112241,112243,112245,112247],{"class":102,"line":135},[100,112222,1901],{"class":106},[100,112224,111],{"class":110},[100,112226,3389],{"class":191},[100,112228,170],{"class":118},[100,112230,757],{"class":178},[100,112232,290],{"class":118},[100,112234,104438],{"class":178},[100,112236,170],{"class":118},[100,112238,206],{"class":205},[100,112240,126],{"class":209},[100,112242,206],{"class":205},[100,112244,126],{"class":118},[100,112246,11901],{"class":205},[100,112248,182],{"class":118},[100,112250,112251,112253,112255,112257],{"class":102,"line":142},[100,112252,372],{"class":114},[100,112254,170],{"class":118},[100,112256,631],{"class":178},[100,112258,215],{"class":118},[77,112260,1554],{"id":1553},[40,112262,112263,112267,112271,112275,112279],{},[43,112264,112265],{},[295,112266,81195],{"href":81194},[43,112268,112269],{},[295,112270,16109],{"href":14982},[43,112272,112273],{},[295,112274,86861],{"href":85784},[43,112276,112277],{},[295,112278,106695],{"href":109684},[43,112280,112281],{},[295,112282,1587],{"href":1749},[1589,112284,112285],{},"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 .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 .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 .slqww, html code.shiki .slqww{--shiki-light:#6182B8;--shiki-default:#24292E;--shiki-dark:#E1E4E8}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 .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 .srdBf, html code.shiki .srdBf{--shiki-light:#F76D47;--shiki-default:#005CC5;--shiki-dark:#79B8FF}html pre.shiki code .s39Yj, html code.shiki .s39Yj{--shiki-light:#39ADB5;--shiki-default:#005CC5;--shiki-dark:#79B8FF}",{"title":96,"searchDepth":135,"depth":135,"links":112287},[112288,112289,112290,112295,112296,112304,112310,112313,112314,112315,112316,112325],{"id":79,"depth":135,"text":80},{"id":5880,"depth":135,"text":5881},{"id":24957,"depth":135,"text":24958,"children":112291},[112292,112293,112294],{"id":110826,"depth":142,"text":110827},{"id":110905,"depth":142,"text":110906},{"id":110991,"depth":142,"text":110992},{"id":25090,"depth":135,"text":25091},{"id":6243,"depth":135,"text":6244,"children":112297},[112298,112299,112301,112303],{"id":111178,"depth":142,"text":111179},{"id":111243,"depth":142,"text":112300},"Validate before calling int()",{"id":111362,"depth":142,"text":112302},"Use try-except for safer input handling",{"id":111466,"depth":142,"text":111467},{"id":111557,"depth":135,"text":111558,"children":112305},[112306,112308],{"id":111570,"depth":142,"text":112307},"Use float() if you need decimals",{"id":111628,"depth":142,"text":112309},"Convert to int only if you want to drop the decimal part",{"id":111704,"depth":135,"text":111705,"children":112311},[112312],{"id":111791,"depth":142,"text":111792},{"id":111885,"depth":135,"text":111886},{"id":1167,"depth":135,"text":1168},{"id":7116,"depth":135,"text":7117},{"id":1513,"depth":135,"text":1514,"children":112317},[112318,112320,112322,112323],{"id":112155,"depth":142,"text":112319},"Why does int(\"3.14\") fail?",{"id":112169,"depth":142,"text":112321},"Does int() work with spaces?",{"id":112181,"depth":142,"text":112182},{"id":112192,"depth":142,"text":112324},"How do I convert \"1,000\" to an integer?",{"id":1553,"depth":135,"text":1554},"Master valueerror invalid literal for int with base 10 fix in our comprehensive Python beginner guide.",{},{"title":110504,"description":112326},"errors\u002Fvalueerror-invalid-literal-for-int-with-base-10-fix","NqeubwZqaIH1uGZmOrOuNySLty5gz8HZ1OskVl5li5s",{"id":112332,"title":112333,"body":112334,"description":113669,"extension":1623,"meta":113670,"navigation":138,"path":113671,"seo":113672,"stem":113673,"__hash__":113674},"content\u002Ferrors\u002Fvalueerror-math-domain-error-fix.md","ValueError: math domain error (Fix)",{"type":7,"value":112335,"toc":113637},[112336,112339,112348,112351,112377,112389,112391,112471,112479,112481,112483,112511,112514,112516,112519,112550,112555,112557,112559,112564,112599,112601,112615,112620,112654,112656,112668,112674,112708,112710,112722,112724,112726,112752,112756,112833,112835,112843,112846,112853,112857,112866,112944,112954,112958,112969,113053,113057,113060,113162,113168,113174,113177,113253,113256,113261,113268,113275,113311,113313,113325,113330,113334,113337,113392,113395,113397,113400,113417,113419,113448,113451,113516,113519,113521,113523,113556,113564,113566,113570,113573,113578,113585,113591,113599,113603,113609,113611,113634],[10,112337,112333],{"id":112338},"valueerror-math-domain-error-fix",[14,112340,112341,112344,112345,112347],{},[17,112342,112343],{},"ValueError: math domain error"," happens when you pass a value to a function in Python’s ",[17,112346,9271],{}," module that is outside the valid input range.",[14,112349,112350],{},"This usually happens with functions like:",[40,112352,112353,112357,112362,112367,112372],{},[43,112354,112355],{},[17,112356,54185],{},[43,112358,112359],{},[17,112360,112361],{},"math.log()",[43,112363,112364],{},[17,112365,112366],{},"math.log10()",[43,112368,112369],{},[17,112370,112371],{},"math.asin()",[43,112373,112374],{},[17,112375,112376],{},"math.acos()",[14,112378,55737,112379,112382,112383,112385,112386,112388],{},[17,112380,112381],{},"math.sqrt(-1)"," fails because the ",[17,112384,9271],{}," module expects a real number, and ",[17,112387,35345],{}," is not a valid input for a real square root.",[77,112390,80],{"id":79},[91,112392,112394],{"className":93,"code":112393,"language":95,"meta":96,"style":96},"import math\n\nx = -1\n\nif x >= 0:\n    print(math.sqrt(x))\nelse:\n    print(\"x must be 0 or greater\")\n",[17,112395,112396,112402,112406,112416,112420,112432,112450,112456],{"__ignoreMap":96},[100,112397,112398,112400],{"class":102,"line":103},[100,112399,9259],{"class":145},[100,112401,9262],{"class":106},[100,112403,112404],{"class":102,"line":135},[100,112405,139],{"emptyLinePlaceholder":138},[100,112407,112408,112410,112412,112414],{"class":102,"line":142},[100,112409,54291],{"class":106},[100,112411,111],{"class":110},[100,112413,1906],{"class":110},[100,112415,228],{"class":122},[100,112417,112418],{"class":102,"line":152},[100,112419,139],{"emptyLinePlaceholder":138},[100,112421,112422,112424,112426,112428,112430],{"class":102,"line":164},[100,112423,2736],{"class":145},[100,112425,59659],{"class":106},[100,112427,1679],{"class":110},[100,112429,1921],{"class":122},[100,112431,149],{"class":118},[100,112433,112434,112436,112438,112440,112442,112444,112446,112448],{"class":102,"line":185},[100,112435,200],{"class":114},[100,112437,170],{"class":118},[100,112439,9271],{"class":178},[100,112441,290],{"class":118},[100,112443,9276],{"class":178},[100,112445,170],{"class":118},[100,112447,39024],{"class":178},[100,112449,182],{"class":118},[100,112451,112452,112454],{"class":102,"line":197},[100,112453,4944],{"class":145},[100,112455,149],{"class":118},[100,112457,112458,112460,112462,112464,112467,112469],{"class":102,"line":771},[100,112459,200],{"class":114},[100,112461,170],{"class":118},[100,112463,206],{"class":205},[100,112465,112466],{"class":209},"x must be 0 or greater",[100,112468,206],{"class":205},[100,112470,215],{"class":118},[14,112472,112473,112474,3178,112476,290],{},"This error happens when you pass a value that is outside the valid range for a math function, such as ",[17,112475,112381],{},[17,112477,112478],{},"math.log(0)",[77,112480,5881],{"id":5880},[14,112482,9328],{},[40,112484,112485,112488,112491],{},[43,112486,112487],{},"A math function received a value it cannot use",[43,112489,112490],{},"The value is outside the function's valid input range",[43,112492,112493,112494],{},"Common examples include:\n",[40,112495,112496,112499,112504],{},[43,112497,112498],{},"square root of a negative number",[43,112500,112501,112502],{},"logarithm of ",[17,112503,2279],{},[43,112505,112506,112507,9932,112509],{},"inverse trig functions with values outside ",[17,112508,35345],{},[17,112510,123],{},[14,112512,112513],{},"In short, Python is telling you: “This number is not allowed for this math function.”",[77,112515,26289],{"id":26288},[14,112517,112518],{},"You will often see this error in cases like these:",[40,112520,112521,112526,112535,112547],{},[43,112522,1357,112523,112525],{},[17,112524,54185],{}," with a negative number",[43,112527,1357,112528,3178,112530,42154,112532,112534],{},[17,112529,112361],{},[17,112531,112366],{},[17,112533,2279],{}," or a negative number",[43,112536,1357,112537,3178,112539,112541,112542,112544,112545],{},[17,112538,112376],{},[17,112540,112371],{}," with a value less than ",[17,112543,35345],{}," or greater than ",[17,112546,123],{},[43,112548,112549],{},"Passing user input that was converted into an invalid number",[14,112551,41228,112552,112554],{},[295,112553,1587],{"href":1749}," for a simple overview.",[77,112556,11031],{"id":11030},[14,112558,95223],{},[675,112560,112562],{"id":112561},"mathsqrt-1",[17,112563,112381],{},[91,112565,112567],{"className":93,"code":112566,"language":95,"meta":96,"style":96},"import math\n\nprint(math.sqrt(-1))\n",[17,112568,112569,112575,112579],{"__ignoreMap":96},[100,112570,112571,112573],{"class":102,"line":103},[100,112572,9259],{"class":145},[100,112574,9262],{"class":106},[100,112576,112577],{"class":102,"line":135},[100,112578,139],{"emptyLinePlaceholder":138},[100,112580,112581,112583,112585,112587,112589,112591,112593,112595,112597],{"class":102,"line":142},[100,112582,372],{"class":114},[100,112584,170],{"class":118},[100,112586,9271],{"class":178},[100,112588,290],{"class":118},[100,112590,9276],{"class":178},[100,112592,170],{"class":118},[100,112594,2193],{"class":110},[100,112596,123],{"class":122},[100,112598,182],{"class":118},[14,112600,218],{},[91,112602,112604],{"className":93,"code":112603,"language":95,"meta":96,"style":96},"ValueError: math domain error\n",[17,112605,112606],{"__ignoreMap":96},[100,112607,112608,112610,112612],{"class":102,"line":103},[100,112609,3483],{"class":191},[100,112611,89],{"class":118},[100,112613,112614],{"class":106}," math domain error\n",[675,112616,112618],{"id":112617},"mathlog0",[17,112619,112478],{},[91,112621,112623],{"className":93,"code":112622,"language":95,"meta":96,"style":96},"import math\n\nprint(math.log(0))\n",[17,112624,112625,112631,112635],{"__ignoreMap":96},[100,112626,112627,112629],{"class":102,"line":103},[100,112628,9259],{"class":145},[100,112630,9262],{"class":106},[100,112632,112633],{"class":102,"line":135},[100,112634,139],{"emptyLinePlaceholder":138},[100,112636,112637,112639,112641,112643,112645,112648,112650,112652],{"class":102,"line":142},[100,112638,372],{"class":114},[100,112640,170],{"class":118},[100,112642,9271],{"class":178},[100,112644,290],{"class":118},[100,112646,112647],{"class":178},"log",[100,112649,170],{"class":118},[100,112651,2279],{"class":122},[100,112653,182],{"class":118},[14,112655,218],{},[91,112657,112658],{"className":93,"code":112603,"language":95,"meta":96,"style":96},[17,112659,112660],{"__ignoreMap":96},[100,112661,112662,112664,112666],{"class":102,"line":103},[100,112663,3483],{"class":191},[100,112665,89],{"class":118},[100,112667,112614],{"class":106},[675,112669,112671],{"id":112670},"mathacos2",[17,112672,112673],{},"math.acos(2)",[91,112675,112677],{"className":93,"code":112676,"language":95,"meta":96,"style":96},"import math\n\nprint(math.acos(2))\n",[17,112678,112679,112685,112689],{"__ignoreMap":96},[100,112680,112681,112683],{"class":102,"line":103},[100,112682,9259],{"class":145},[100,112684,9262],{"class":106},[100,112686,112687],{"class":102,"line":135},[100,112688,139],{"emptyLinePlaceholder":138},[100,112690,112691,112693,112695,112697,112699,112702,112704,112706],{"class":102,"line":142},[100,112692,372],{"class":114},[100,112694,170],{"class":118},[100,112696,9271],{"class":178},[100,112698,290],{"class":118},[100,112700,112701],{"class":178},"acos",[100,112703,170],{"class":118},[100,112705,559],{"class":122},[100,112707,182],{"class":118},[14,112709,218],{},[91,112711,112712],{"className":93,"code":112603,"language":95,"meta":96,"style":96},[17,112713,112714],{"__ignoreMap":96},[100,112715,112716,112718,112720],{"class":102,"line":103},[100,112717,3483],{"class":191},[100,112719,89],{"class":118},[100,112721,112614],{"class":106},[77,112723,6244],{"id":6243},[14,112725,45917],{},[40,112727,112728,112731,112734,112739,112744],{},[43,112729,112730],{},"Check the valid range before calling the function",[43,112732,112733],{},"Clean and validate user input first",[43,112735,241,112736,112738],{},[17,112737,2736],{}," statements to block invalid values",[43,112740,241,112741,112743],{},[17,112742,244],{}," if a value may sometimes be invalid",[43,112745,241,112746,15957,112749,112751],{},[17,112747,112748],{},"cmath",[17,112750,9271],{}," if you need complex-number results",[675,112753,112755],{"id":112754},"fix-1-check-the-value-before-calling-the-function","Fix 1: Check the value before calling the function",[91,112757,112759],{"className":93,"code":112758,"language":95,"meta":96,"style":96},"import math\n\nx = 25\n\nif x >= 0:\n    print(math.sqrt(x))\nelse:\n    print(\"x must be 0 or greater\")\n",[17,112760,112761,112767,112771,112779,112783,112795,112813,112819],{"__ignoreMap":96},[100,112762,112763,112765],{"class":102,"line":103},[100,112764,9259],{"class":145},[100,112766,9262],{"class":106},[100,112768,112769],{"class":102,"line":135},[100,112770,139],{"emptyLinePlaceholder":138},[100,112772,112773,112775,112777],{"class":102,"line":142},[100,112774,54291],{"class":106},[100,112776,111],{"class":110},[100,112778,16667],{"class":122},[100,112780,112781],{"class":102,"line":152},[100,112782,139],{"emptyLinePlaceholder":138},[100,112784,112785,112787,112789,112791,112793],{"class":102,"line":164},[100,112786,2736],{"class":145},[100,112788,59659],{"class":106},[100,112790,1679],{"class":110},[100,112792,1921],{"class":122},[100,112794,149],{"class":118},[100,112796,112797,112799,112801,112803,112805,112807,112809,112811],{"class":102,"line":185},[100,112798,200],{"class":114},[100,112800,170],{"class":118},[100,112802,9271],{"class":178},[100,112804,290],{"class":118},[100,112806,9276],{"class":178},[100,112808,170],{"class":118},[100,112810,39024],{"class":178},[100,112812,182],{"class":118},[100,112814,112815,112817],{"class":102,"line":197},[100,112816,4944],{"class":145},[100,112818,149],{"class":118},[100,112820,112821,112823,112825,112827,112829,112831],{"class":102,"line":771},[100,112822,200],{"class":114},[100,112824,170],{"class":118},[100,112826,206],{"class":205},[100,112828,112466],{"class":209},[100,112830,206],{"class":205},[100,112832,215],{"class":118},[14,112834,218],{},[91,112836,112837],{"className":93,"code":55055,"language":95,"meta":96,"style":96},[17,112838,112839],{"__ignoreMap":96},[100,112840,112841],{"class":102,"line":103},[100,112842,55055],{"class":122},[14,112844,112845],{},"This is the safest approach when you know the required range.",[14,112847,112848,112849,290],{},"For more on square roots, see ",[295,112850,112852],{"href":112851},"\u002Fstandard-library\u002Fmath.sqrt-function-explained","math.sqrt() explained",[675,112854,112856],{"id":112855},"fix-2-validate-values-for-logarithms","Fix 2: Validate values for logarithms",[14,112858,112859,3266,112861,112863,112864,290],{},[17,112860,112361],{},[17,112862,112366],{}," require a value greater than ",[17,112865,2279],{},[91,112867,112869],{"className":93,"code":112868,"language":95,"meta":96,"style":96},"import math\n\nx = 10\n\nif x > 0:\n    print(math.log(x))\nelse:\n    print(\"x must be greater than 0\")\n",[17,112870,112871,112877,112881,112889,112893,112905,112923,112929],{"__ignoreMap":96},[100,112872,112873,112875],{"class":102,"line":103},[100,112874,9259],{"class":145},[100,112876,9262],{"class":106},[100,112878,112879],{"class":102,"line":135},[100,112880,139],{"emptyLinePlaceholder":138},[100,112882,112883,112885,112887],{"class":102,"line":142},[100,112884,54291],{"class":106},[100,112886,111],{"class":110},[100,112888,2181],{"class":122},[100,112890,112891],{"class":102,"line":152},[100,112892,139],{"emptyLinePlaceholder":138},[100,112894,112895,112897,112899,112901,112903],{"class":102,"line":164},[100,112896,2736],{"class":145},[100,112898,59659],{"class":106},[100,112900,1918],{"class":110},[100,112902,1921],{"class":122},[100,112904,149],{"class":118},[100,112906,112907,112909,112911,112913,112915,112917,112919,112921],{"class":102,"line":185},[100,112908,200],{"class":114},[100,112910,170],{"class":118},[100,112912,9271],{"class":178},[100,112914,290],{"class":118},[100,112916,112647],{"class":178},[100,112918,170],{"class":118},[100,112920,39024],{"class":178},[100,112922,182],{"class":118},[100,112924,112925,112927],{"class":102,"line":197},[100,112926,4944],{"class":145},[100,112928,149],{"class":118},[100,112930,112931,112933,112935,112937,112940,112942],{"class":102,"line":771},[100,112932,200],{"class":114},[100,112934,170],{"class":118},[100,112936,206],{"class":205},[100,112938,112939],{"class":209},"x must be greater than 0",[100,112941,206],{"class":205},[100,112943,215],{"class":118},[14,112945,11734,112946,2025,112948,112950,112951,290],{},[17,112947,39024],{},[17,112949,2279],{}," or negative, do not call ",[17,112952,112953],{},"math.log(x)",[675,112955,112957],{"id":112956},"fix-3-validate-inverse-trig-input","Fix 3: Validate inverse trig input",[14,112959,112960,3266,112962,112964,112965,9932,112967,290],{},[17,112961,112371],{},[17,112963,112376],{}," only accept values from ",[17,112966,35345],{},[17,112968,123],{},[91,112970,112972],{"className":93,"code":112971,"language":95,"meta":96,"style":96},"import math\n\nx = 0.5\n\nif -1 \u003C= x \u003C= 1:\n    print(math.acos(x))\nelse:\n    print(\"x must be between -1 and 1\")\n",[17,112973,112974,112980,112984,112992,112996,113014,113032,113038],{"__ignoreMap":96},[100,112975,112976,112978],{"class":102,"line":103},[100,112977,9259],{"class":145},[100,112979,9262],{"class":106},[100,112981,112982],{"class":102,"line":135},[100,112983,139],{"emptyLinePlaceholder":138},[100,112985,112986,112988,112990],{"class":102,"line":142},[100,112987,54291],{"class":106},[100,112989,111],{"class":110},[100,112991,81840],{"class":122},[100,112993,112994],{"class":102,"line":152},[100,112995,139],{"emptyLinePlaceholder":138},[100,112997,112998,113000,113002,113004,113006,113008,113010,113012],{"class":102,"line":164},[100,112999,2736],{"class":145},[100,113001,1906],{"class":110},[100,113003,123],{"class":122},[100,113005,3133],{"class":110},[100,113007,59659],{"class":106},[100,113009,63723],{"class":110},[100,113011,6915],{"class":122},[100,113013,149],{"class":118},[100,113015,113016,113018,113020,113022,113024,113026,113028,113030],{"class":102,"line":185},[100,113017,200],{"class":114},[100,113019,170],{"class":118},[100,113021,9271],{"class":178},[100,113023,290],{"class":118},[100,113025,112701],{"class":178},[100,113027,170],{"class":118},[100,113029,39024],{"class":178},[100,113031,182],{"class":118},[100,113033,113034,113036],{"class":102,"line":197},[100,113035,4944],{"class":145},[100,113037,149],{"class":118},[100,113039,113040,113042,113044,113046,113049,113051],{"class":102,"line":771},[100,113041,200],{"class":114},[100,113043,170],{"class":118},[100,113045,206],{"class":205},[100,113047,113048],{"class":209},"x must be between -1 and 1",[100,113050,206],{"class":205},[100,113052,215],{"class":118},[675,113054,113056],{"id":113055},"fix-4-validate-user-input","Fix 4: Validate user input",[14,113058,113059],{},"A bad value often comes from input entered by a user.",[91,113061,113063],{"className":93,"code":113062,"language":95,"meta":96,"style":96},"import math\n\ntext = input(\"Enter a number: \")\nx = float(text)\n\nif x >= 0:\n    print(math.sqrt(x))\nelse:\n    print(\"Please enter a number that is 0 or greater\")\n",[17,113064,113065,113071,113075,113093,113107,113111,113123,113141,113147],{"__ignoreMap":96},[100,113066,113067,113069],{"class":102,"line":103},[100,113068,9259],{"class":145},[100,113070,9262],{"class":106},[100,113072,113073],{"class":102,"line":135},[100,113074,139],{"emptyLinePlaceholder":138},[100,113076,113077,113079,113081,113083,113085,113087,113089,113091],{"class":102,"line":142},[100,113078,4331],{"class":106},[100,113080,111],{"class":110},[100,113082,7221],{"class":114},[100,113084,170],{"class":118},[100,113086,206],{"class":205},[100,113088,6217],{"class":209},[100,113090,206],{"class":205},[100,113092,215],{"class":118},[100,113094,113095,113097,113099,113101,113103,113105],{"class":102,"line":152},[100,113096,54291],{"class":106},[100,113098,111],{"class":110},[100,113100,106783],{"class":191},[100,113102,170],{"class":118},[100,113104,4468],{"class":178},[100,113106,215],{"class":118},[100,113108,113109],{"class":102,"line":164},[100,113110,139],{"emptyLinePlaceholder":138},[100,113112,113113,113115,113117,113119,113121],{"class":102,"line":185},[100,113114,2736],{"class":145},[100,113116,59659],{"class":106},[100,113118,1679],{"class":110},[100,113120,1921],{"class":122},[100,113122,149],{"class":118},[100,113124,113125,113127,113129,113131,113133,113135,113137,113139],{"class":102,"line":197},[100,113126,200],{"class":114},[100,113128,170],{"class":118},[100,113130,9271],{"class":178},[100,113132,290],{"class":118},[100,113134,9276],{"class":178},[100,113136,170],{"class":118},[100,113138,39024],{"class":178},[100,113140,182],{"class":118},[100,113142,113143,113145],{"class":102,"line":771},[100,113144,4944],{"class":145},[100,113146,149],{"class":118},[100,113148,113149,113151,113153,113155,113158,113160],{"class":102,"line":787},[100,113150,200],{"class":114},[100,113152,170],{"class":118},[100,113154,206],{"class":205},[100,113156,113157],{"class":209},"Please enter a number that is 0 or greater",[100,113159,206],{"class":205},[100,113161,215],{"class":118},[14,113163,113164,113165,113167],{},"This prevents calling ",[17,113166,54185],{}," with an invalid number.",[675,113169,113171,113172],{"id":113170},"fix-5-use-try-except","Fix 5: Use ",[17,113173,244],{},[14,113175,113176],{},"If invalid values are possible and you want your program to continue running, catch the error.",[91,113178,113180],{"className":93,"code":113179,"language":95,"meta":96,"style":96},"import math\n\nx = -4\n\ntry:\n    print(math.sqrt(x))\nexcept ValueError:\n    print(\"Invalid value for math.sqrt()\")\n",[17,113181,113182,113188,113192,113202,113206,113212,113230,113238],{"__ignoreMap":96},[100,113183,113184,113186],{"class":102,"line":103},[100,113185,9259],{"class":145},[100,113187,9262],{"class":106},[100,113189,113190],{"class":102,"line":135},[100,113191,139],{"emptyLinePlaceholder":138},[100,113193,113194,113196,113198,113200],{"class":102,"line":142},[100,113195,54291],{"class":106},[100,113197,111],{"class":110},[100,113199,1906],{"class":110},[100,113201,63793],{"class":122},[100,113203,113204],{"class":102,"line":152},[100,113205,139],{"emptyLinePlaceholder":138},[100,113207,113208,113210],{"class":102,"line":164},[100,113209,146],{"class":145},[100,113211,149],{"class":118},[100,113213,113214,113216,113218,113220,113222,113224,113226,113228],{"class":102,"line":185},[100,113215,200],{"class":114},[100,113217,170],{"class":118},[100,113219,9271],{"class":178},[100,113221,290],{"class":118},[100,113223,9276],{"class":178},[100,113225,170],{"class":118},[100,113227,39024],{"class":178},[100,113229,182],{"class":118},[100,113231,113232,113234,113236],{"class":102,"line":197},[100,113233,188],{"class":145},[100,113235,2794],{"class":191},[100,113237,149],{"class":118},[100,113239,113240,113242,113244,113246,113249,113251],{"class":102,"line":771},[100,113241,200],{"class":114},[100,113243,170],{"class":118},[100,113245,206],{"class":205},[100,113247,113248],{"class":209},"Invalid value for math.sqrt()",[100,113250,206],{"class":205},[100,113252,215],{"class":118},[14,113254,113255],{},"This is useful when values come from calculations or unpredictable input.",[14,113257,113258,113259,290],{},"If you want to learn this pattern, read ",[295,113260,1029],{"href":2824},[675,113262,113264,113265,113267],{"id":113263},"fix-6-use-cmath-for-complex-results","Fix 6: Use ",[17,113266,112748],{}," for complex results",[14,113269,43223,113270,113272,113273,290],{},[17,113271,9271],{}," module works with real numbers. If you want the square root of a negative number, use ",[17,113274,112748],{},[91,113276,113278],{"className":93,"code":113277,"language":95,"meta":96,"style":96},"import cmath\n\nprint(cmath.sqrt(-1))\n",[17,113279,113280,113287,113291],{"__ignoreMap":96},[100,113281,113282,113284],{"class":102,"line":103},[100,113283,9259],{"class":145},[100,113285,113286],{"class":106}," cmath\n",[100,113288,113289],{"class":102,"line":135},[100,113290,139],{"emptyLinePlaceholder":138},[100,113292,113293,113295,113297,113299,113301,113303,113305,113307,113309],{"class":102,"line":142},[100,113294,372],{"class":114},[100,113296,170],{"class":118},[100,113298,112748],{"class":178},[100,113300,290],{"class":118},[100,113302,9276],{"class":178},[100,113304,170],{"class":118},[100,113306,2193],{"class":110},[100,113308,123],{"class":122},[100,113310,182],{"class":118},[14,113312,218],{},[91,113314,113316],{"className":93,"code":113315,"language":95,"meta":96,"style":96},"1j\n",[17,113317,113318],{"__ignoreMap":96},[100,113319,113320,113322],{"class":102,"line":103},[100,113321,123],{"class":122},[100,113323,113324],{"class":1077},"j\n",[14,113326,113327,113328,290],{},"If you are working with standard math functions, see the ",[295,113329,59942],{"href":59941},[77,113331,113333],{"id":113332},"common-valid-ranges-to-remember","Common valid ranges to remember",[14,113335,113336],{},"These are the most common valid ranges that cause this error:",[40,113338,113339,113351,113360,113369,113381],{},[43,113340,113341,113344,113345,113347,113348,113350],{},[17,113342,113343],{},"math.sqrt(x)",": ",[17,113346,39024],{}," must be ",[17,113349,2279],{}," or greater",[43,113352,113353,113344,113355,113357,113358],{},[17,113354,112953],{},[17,113356,39024],{}," must be greater than ",[17,113359,2279],{},[43,113361,113362,113344,113365,113357,113367],{},[17,113363,113364],{},"math.log10(x)",[17,113366,39024],{},[17,113368,2279],{},[43,113370,113371,113344,113374,113376,113377,3266,113379],{},[17,113372,113373],{},"math.acos(x)",[17,113375,39024],{}," must be between ",[17,113378,35345],{},[17,113380,123],{},[43,113382,113383,113344,113386,113376,113388,3266,113390],{},[17,113384,113385],{},"math.asin(x)",[17,113387,39024],{},[17,113389,35345],{},[17,113391,123],{},[14,113393,113394],{},"If you are not sure about a function, check its documentation before using it.",[77,113396,3274],{"id":3273},[14,113398,113399],{},"If you are not sure where the error is coming from, try these steps:",[40,113401,113402,113405,113408,113411,113414],{},[43,113403,113404],{},"Print the value before the math function runs",[43,113406,113407],{},"Check which math function caused the error",[43,113409,113410],{},"Read the function's allowed input range",[43,113412,113413],{},"Trace where the bad value came from",[43,113415,113416],{},"Add validation close to the input or calculation",[14,113418,26604],{},[91,113420,113422],{"className":93,"code":113421,"language":95,"meta":96,"style":96},"print(x)\nprint(type(x))\n",[17,113423,113424,113434],{"__ignoreMap":96},[100,113425,113426,113428,113430,113432],{"class":102,"line":103},[100,113427,372],{"class":114},[100,113429,170],{"class":118},[100,113431,39024],{"class":178},[100,113433,215],{"class":118},[100,113435,113436,113438,113440,113442,113444,113446],{"class":102,"line":135},[100,113437,372],{"class":114},[100,113439,170],{"class":118},[100,113441,1250],{"class":191},[100,113443,170],{"class":118},[100,113445,39024],{"class":178},[100,113447,182],{"class":118},[14,113449,113450],{},"You can also inspect help for the function:",[91,113452,113454],{"className":93,"code":113453,"language":95,"meta":96,"style":96},"import math\nprint(help(math.sqrt))\nprint(help(math.log))\nprint(help(math.acos))\n",[17,113455,113456,113462,113480,113498],{"__ignoreMap":96},[100,113457,113458,113460],{"class":102,"line":103},[100,113459,9259],{"class":145},[100,113461,9262],{"class":106},[100,113463,113464,113466,113468,113470,113472,113474,113476,113478],{"class":102,"line":135},[100,113465,372],{"class":114},[100,113467,170],{"class":118},[100,113469,2478],{"class":114},[100,113471,170],{"class":118},[100,113473,9271],{"class":178},[100,113475,290],{"class":118},[100,113477,9276],{"class":2494},[100,113479,182],{"class":118},[100,113481,113482,113484,113486,113488,113490,113492,113494,113496],{"class":102,"line":142},[100,113483,372],{"class":114},[100,113485,170],{"class":118},[100,113487,2478],{"class":114},[100,113489,170],{"class":118},[100,113491,9271],{"class":178},[100,113493,290],{"class":118},[100,113495,112647],{"class":2494},[100,113497,182],{"class":118},[100,113499,113500,113502,113504,113506,113508,113510,113512,113514],{"class":102,"line":152},[100,113501,372],{"class":114},[100,113503,170],{"class":118},[100,113505,2478],{"class":114},[100,113507,170],{"class":118},[100,113509,9271],{"class":178},[100,113511,290],{"class":118},[100,113513,112701],{"class":2494},[100,113515,182],{"class":118},[14,113517,113518],{},"This can help you confirm what values are allowed.",[77,113520,7117],{"id":7116},[14,113522,9001],{},[40,113524,113525,113529,113535,113539,113550,113553],{},[43,113526,315,113527,112525],{},[17,113528,54185],{},[43,113530,315,113531,42154,113533],{},[17,113532,112361],{},[17,113534,2279],{},[43,113536,315,113537,112525],{},[17,113538,112361],{},[43,113540,315,113541,3178,113543,113545,113546,9932,113548],{},[17,113542,112371],{},[17,113544,112376],{}," with a value outside ",[17,113547,35345],{},[17,113549,123],{},[43,113551,113552],{},"Using unexpected user input",[43,113554,113555],{},"Passing a value from an earlier calculation that turned out to be invalid",[14,113557,113558,113559,113561,113562,290],{},"A related point: this is still a ",[17,113560,3483],{},", but it has a specific message. If you want a broader explanation, see ",[295,113563,3265],{"href":3264},[77,113565,1514],{"id":1513},[675,113567,113569],{"id":113568},"why-do-i-get-valueerror-math-domain-error-in-python","Why do I get ValueError: math domain error in Python?",[14,113571,113572],{},"You passed a value to a math function that is outside its allowed range.",[675,113574,9137,113576,112158],{"id":113575},"why-does-mathsqrt-1-fail",[17,113577,112381],{},[14,113579,43223,113580,113582,113583,290],{},[17,113581,9271],{}," module only works with real-number square roots. Negative numbers are not valid for ",[17,113584,54185],{},[675,113586,113588,113589,11353],{"id":113587},"how-do-i-fix-mathlog0","How do I fix ",[17,113590,112478],{},[14,113592,113593,113594,113596,113597,290],{},"Make sure the value is greater than ",[17,113595,2279],{}," before calling ",[17,113598,112361],{},[675,113600,113602],{"id":113601},"can-i-calculate-the-square-root-of-a-negative-number-in-python","Can I calculate the square root of a negative number in Python?",[14,113604,113605,113606,113608],{},"Yes. Use the ",[17,113607,112748],{}," module if you want a complex-number result.",[77,113610,1554],{"id":1553},[40,113612,113613,113617,113622,113626,113630],{},[43,113614,113615],{},[295,113616,59942],{"href":59941},[43,113618,113619],{},[295,113620,113621],{"href":112851},"math.sqrt() function explained",[43,113623,113624],{},[295,113625,3265],{"href":3264},[43,113627,113628],{},[295,113629,1581],{"href":2824},[43,113631,113632],{},[295,113633,1587],{"href":1749},[1589,113635,113636],{},"html pre.shiki code .sVHd0, html code.shiki .sVHd0{--shiki-light:#39ADB5;--shiki-light-font-style:italic;--shiki-default:#D73A49;--shiki-default-font-style:inherit;--shiki-dark:#F97583;--shiki-dark-font-style:inherit}html pre.shiki code .su5hD, html code.shiki .su5hD{--shiki-light:#90A4AE;--shiki-default:#24292E;--shiki-dark:#E1E4E8}html pre.shiki code .smGrS, html code.shiki .smGrS{--shiki-light:#39ADB5;--shiki-default:#D73A49;--shiki-dark:#F97583}html pre.shiki code .srdBf, html code.shiki .srdBf{--shiki-light:#F76D47;--shiki-default:#005CC5;--shiki-dark:#79B8FF}html pre.shiki code .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 .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 .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 .sbsja, html code.shiki .sbsja{--shiki-light:#9C3EDA;--shiki-default:#D73A49;--shiki-dark:#F97583}html pre.shiki code .skxfh, html code.shiki .skxfh{--shiki-light:#E53935;--shiki-default:#24292E;--shiki-dark:#E1E4E8}",{"title":96,"searchDepth":135,"depth":135,"links":113638},[113639,113640,113641,113642,113647,113657,113658,113659,113660,113668],{"id":79,"depth":135,"text":80},{"id":5880,"depth":135,"text":5881},{"id":26288,"depth":135,"text":26289},{"id":11030,"depth":135,"text":11031,"children":113643},[113644,113645,113646],{"id":112561,"depth":142,"text":112381},{"id":112617,"depth":142,"text":112478},{"id":112670,"depth":142,"text":112673},{"id":6243,"depth":135,"text":6244,"children":113648},[113649,113650,113651,113652,113653,113655],{"id":112754,"depth":142,"text":112755},{"id":112855,"depth":142,"text":112856},{"id":112956,"depth":142,"text":112957},{"id":113055,"depth":142,"text":113056},{"id":113170,"depth":142,"text":113654},"Fix 5: Use try-except",{"id":113263,"depth":142,"text":113656},"Fix 6: Use cmath for complex results",{"id":113332,"depth":135,"text":113333},{"id":3273,"depth":135,"text":3274},{"id":7116,"depth":135,"text":7117},{"id":1513,"depth":135,"text":1514,"children":113661},[113662,113663,113665,113667],{"id":113568,"depth":142,"text":113569},{"id":113575,"depth":142,"text":113664},"Why does math.sqrt(-1) fail?",{"id":113587,"depth":142,"text":113666},"How do I fix math.log(0)?",{"id":113601,"depth":142,"text":113602},{"id":1553,"depth":135,"text":1554},"Master valueerror math domain error fix in our comprehensive Python beginner guide.",{},"\u002Ferrors\u002Fvalueerror-math-domain-error-fix",{"title":112333,"description":113669},"errors\u002Fvalueerror-math-domain-error-fix","9qiEJQQJgP4Nn3C_LFWu1cf_y7JMZtjkuEFAGkF24RM",{"id":113676,"title":79453,"body":113677,"description":115865,"extension":1623,"meta":115866,"navigation":138,"path":115867,"seo":115868,"stem":115869,"__hash__":115870},"content\u002Ferrors\u002Fvalueerror-not-enough-values-to-unpack-fix.md",{"type":7,"value":113678,"toc":115830},[113679,113682,113685,113687,113720,113725,113727,113730,113794,113800,113802,113805,113807,113832,113835,113849,113852,113863,113865,113867,113888,113890,113920,113922,113940,113943,113988,113990,114002,114004,114007,114025,114029,114059,114068,114072,114114,114117,114121,114183,114190,114192,114195,114199,114202,114243,114246,114285,114289,114292,114351,114353,114384,114387,114391,114394,114467,114469,114494,114497,114556,114558,114572,114575,114579,114582,114584,114623,114625,114668,114671,114727,114731,114734,114846,114851,114855,114859,114861,114904,114913,114930,114933,114937,114976,114978,114994,114998,115128,115132,115245,115253,115257,115260,115262,115333,115340,115344,115347,115410,115412,115449,115452,115456,115581,115584,115586,115589,115607,115610,115697,115700,115708,115711,115713,115715,115734,115740,115742,115746,115748,115752,115755,115759,115762,115781,115789,115793,115799,115801,115827],[10,113680,79453],{"id":113681},"valueerror-not-enough-values-to-unpack-fix",[14,113683,113684],{},"This error happens when Python tries to unpack values into multiple variables, but there are fewer values than expected.",[14,113686,61927],{},[91,113688,113690],{"className":93,"code":113689,"language":95,"meta":96,"style":96},"ValueError: not enough values to unpack (expected 2, got 1)\n",[17,113691,113692],{"__ignoreMap":96},[100,113693,113694,113696,113698,113700,113703,113705,113707,113709,113711,113713,113716,113718],{"class":102,"line":103},[100,113695,3483],{"class":191},[100,113697,89],{"class":118},[100,113699,4921],{"class":110},[100,113701,113702],{"class":106}," enough values to ",[100,113704,99233],{"class":178},[100,113706,170],{"class":118},[100,113708,99238],{"class":178},[100,113710,559],{"class":122},[100,113712,126],{"class":118},[100,113714,113715],{"class":178}," got ",[100,113717,123],{"class":122},[100,113719,215],{"class":118},[14,113721,113722,113723,290],{},"In this guide, you will learn what this means, why it happens, and how to fix it in common cases like lists, function returns, loops, and ",[17,113724,8090],{},[77,113726,80],{"id":79},[14,113728,113729],{},"Make sure the number of variables matches the number of values.",[91,113731,113733],{"className":93,"code":113732,"language":95,"meta":96,"style":96},"values = [1, 2]\na, b = values   # works\n\n# if the number of items may vary:\nfirst, *rest = values\n",[17,113734,113735,113751,113767,113771,113776],{"__ignoreMap":96},[100,113736,113737,113739,113741,113743,113745,113747,113749],{"class":102,"line":103},[100,113738,4010],{"class":106},[100,113740,111],{"class":110},[100,113742,594],{"class":118},[100,113744,123],{"class":122},[100,113746,126],{"class":118},[100,113748,129],{"class":122},[100,113750,603],{"class":118},[100,113752,113753,113755,113757,113759,113761,113764],{"class":102,"line":135},[100,113754,295],{"class":106},[100,113756,126],{"class":118},[100,113758,25360],{"class":106},[100,113760,111],{"class":110},[100,113762,113763],{"class":106}," values   ",[100,113765,113766],{"class":414},"# works\n",[100,113768,113769],{"class":102,"line":142},[100,113770,139],{"emptyLinePlaceholder":138},[100,113772,113773],{"class":102,"line":152},[100,113774,113775],{"class":414},"# if the number of items may vary:\n",[100,113777,113778,113781,113783,113786,113789,113791],{"class":102,"line":164},[100,113779,113780],{"class":106},"first",[100,113782,126],{"class":118},[100,113784,113785],{"class":110}," *",[100,113787,113788],{"class":106},"rest ",[100,113790,111],{"class":110},[100,113792,113793],{"class":106}," values\n",[14,113795,43223,113796,113799],{},[17,113797,113798],{},"*rest"," part collects any remaining items into a list. This is useful when the number of values may change.",[77,113801,5881],{"id":5880},[14,113803,113804],{},"Unpacking means assigning items from a list, tuple, or other iterable to separate variables.",[14,113806,580],{},[91,113808,113810],{"className":93,"code":113809,"language":95,"meta":96,"style":96},"a, b = [10, 20]\n",[17,113811,113812],{"__ignoreMap":96},[100,113813,113814,113816,113818,113820,113822,113824,113826,113828,113830],{"class":102,"line":103},[100,113815,295],{"class":106},[100,113817,126],{"class":118},[100,113819,25360],{"class":106},[100,113821,111],{"class":110},[100,113823,594],{"class":118},[100,113825,356],{"class":122},[100,113827,126],{"class":118},[100,113829,361],{"class":122},[100,113831,603],{"class":118},[14,113833,113834],{},"Here, Python unpacks:",[40,113836,113837,113843],{},[43,113838,113839,77694,113841],{},[17,113840,356],{},[17,113842,295],{},[43,113844,113845,77694,113847],{},[17,113846,6152],{},[17,113848,4027],{},[14,113850,113851],{},"The error happens when:",[40,113853,113854,113857,113860],{},[43,113855,113856],{},"Python expects multiple values",[43,113858,113859],{},"The right side has fewer items than the left side expects",[43,113861,113862],{},"For example, it expected 2 values but only got 1",[77,113864,5945],{"id":5944},[14,113866,5948],{},[91,113868,113870],{"className":93,"code":113869,"language":95,"meta":96,"style":96},"a, b = [1]\n",[17,113871,113872],{"__ignoreMap":96},[100,113873,113874,113876,113878,113880,113882,113884,113886],{"class":102,"line":103},[100,113875,295],{"class":106},[100,113877,126],{"class":118},[100,113879,25360],{"class":106},[100,113881,111],{"class":110},[100,113883,594],{"class":118},[100,113885,123],{"class":122},[100,113887,603],{"class":118},[14,113889,7710],{},[91,113891,113892],{"className":93,"code":113689,"language":95,"meta":96,"style":96},[17,113893,113894],{"__ignoreMap":96},[100,113895,113896,113898,113900,113902,113904,113906,113908,113910,113912,113914,113916,113918],{"class":102,"line":103},[100,113897,3483],{"class":191},[100,113899,89],{"class":118},[100,113901,4921],{"class":110},[100,113903,113702],{"class":106},[100,113905,99233],{"class":178},[100,113907,170],{"class":118},[100,113909,99238],{"class":178},[100,113911,559],{"class":122},[100,113913,126],{"class":118},[100,113915,113715],{"class":178},[100,113917,123],{"class":122},[100,113919,215],{"class":118},[14,113921,32043],{},[40,113923,113924,113931,113937],{},[43,113925,113926,3266,113928,113930],{},[17,113927,295],{},[17,113929,4027],{}," need 2 values",[43,113932,113933,113936],{},[17,113934,113935],{},"[1]"," contains only 1 item",[43,113938,113939],{},"Python cannot fill both variables",[14,113941,113942],{},"A working version would be:",[91,113944,113946],{"className":93,"code":113945,"language":95,"meta":96,"style":96},"a, b = [1, 2]\nprint(a)\nprint(b)\n",[17,113947,113948,113968,113978],{"__ignoreMap":96},[100,113949,113950,113952,113954,113956,113958,113960,113962,113964,113966],{"class":102,"line":103},[100,113951,295],{"class":106},[100,113953,126],{"class":118},[100,113955,25360],{"class":106},[100,113957,111],{"class":110},[100,113959,594],{"class":118},[100,113961,123],{"class":122},[100,113963,126],{"class":118},[100,113965,129],{"class":122},[100,113967,603],{"class":118},[100,113969,113970,113972,113974,113976],{"class":102,"line":135},[100,113971,372],{"class":114},[100,113973,170],{"class":118},[100,113975,295],{"class":178},[100,113977,215],{"class":118},[100,113979,113980,113982,113984,113986],{"class":102,"line":142},[100,113981,372],{"class":114},[100,113983,170],{"class":118},[100,113985,4027],{"class":178},[100,113987,215],{"class":118},[14,113989,218],{},[91,113991,113992],{"className":93,"code":638,"language":95,"meta":96,"style":96},[17,113993,113994,113998],{"__ignoreMap":96},[100,113995,113996],{"class":102,"line":103},[100,113997,228],{"class":122},[100,113999,114000],{"class":102,"line":135},[100,114001,233],{"class":122},[77,114003,25091],{"id":25090},[14,114005,114006],{},"This error usually appears in one of these situations:",[40,114008,114009,114012,114015,114018],{},[43,114010,114011],{},"A list or tuple has fewer items than expected",[43,114013,114014],{},"A function returns fewer values than your code expects",[43,114016,114017],{},"Loop code unpacks items with the wrong structure",[43,114019,114020,114024],{},[295,114021,114022],{"href":108048},[17,114023,8090],{}," returns fewer parts than expected",[675,114026,114028],{"id":114027},"example-unpacking-a-short-tuple","Example: unpacking a short tuple",[91,114030,114032],{"className":93,"code":114031,"language":95,"meta":96,"style":96},"point = (5,)\nx, y = point\n",[17,114033,114034,114047],{"__ignoreMap":96},[100,114035,114036,114038,114040,114042,114044],{"class":102,"line":103},[100,114037,39003],{"class":106},[100,114039,111],{"class":110},[100,114041,16805],{"class":118},[100,114043,3557],{"class":122},[100,114045,114046],{"class":118},",)\n",[100,114048,114049,114051,114053,114055,114057],{"class":102,"line":135},[100,114050,39024],{"class":106},[100,114052,126],{"class":118},[100,114054,39029],{"class":106},[100,114056,111],{"class":110},[100,114058,39034],{"class":106},[14,114060,114061,114063,114064,114067],{},[17,114062,39096],{}," contains only one value, but ",[17,114065,114066],{},"x, y"," expects two.",[675,114069,114071],{"id":114070},"example-function-returns-too-few-values","Example: function returns too few values",[91,114073,114075],{"className":93,"code":114074,"language":95,"meta":96,"style":96},"def get_user():\n    return \"Alice\"\n\nname, age = get_user()\n",[17,114076,114077,114086,114096,114100],{"__ignoreMap":96},[100,114078,114079,114081,114084],{"class":102,"line":103},[100,114080,1078],{"class":1077},[100,114082,114083],{"class":1081}," get_user",[100,114085,1085],{"class":118},[100,114087,114088,114090,114092,114094],{"class":102,"line":135},[100,114089,2552],{"class":145},[100,114091,1708],{"class":205},[100,114093,15343],{"class":209},[100,114095,1714],{"class":205},[100,114097,114098],{"class":102,"line":142},[100,114099,139],{"emptyLinePlaceholder":138},[100,114101,114102,114104,114106,114108,114110,114112],{"class":102,"line":152},[100,114103,2853],{"class":106},[100,114105,126],{"class":118},[100,114107,1676],{"class":106},[100,114109,111],{"class":110},[100,114111,114083],{"class":178},[100,114113,4734],{"class":118},[14,114115,114116],{},"This fails because the function returns only one value, but the code expects two.",[675,114118,114120],{"id":114119},"example-loop-items-do-not-match","Example: loop items do not match",[91,114122,114124],{"className":93,"code":114123,"language":95,"meta":96,"style":96},"items = [(1, 2), (3,)]\n\nfor a, b in items:\n    print(a, b)\n",[17,114125,114126,114149,114153,114169],{"__ignoreMap":96},[100,114127,114128,114130,114132,114134,114136,114138,114140,114142,114144,114146],{"class":102,"line":103},[100,114129,3095],{"class":106},[100,114131,111],{"class":110},[100,114133,78799],{"class":118},[100,114135,123],{"class":122},[100,114137,126],{"class":118},[100,114139,129],{"class":122},[100,114141,2874],{"class":118},[100,114143,16805],{"class":118},[100,114145,11541],{"class":122},[100,114147,114148],{"class":118},",)]\n",[100,114150,114151],{"class":102,"line":135},[100,114152,139],{"emptyLinePlaceholder":138},[100,114154,114155,114157,114159,114161,114163,114165,114167],{"class":102,"line":142},[100,114156,71],{"class":145},[100,114158,78678],{"class":106},[100,114160,126],{"class":118},[100,114162,25360],{"class":106},[100,114164,617],{"class":145},[100,114166,12000],{"class":106},[100,114168,149],{"class":118},[100,114170,114171,114173,114175,114177,114179,114181],{"class":102,"line":152},[100,114172,200],{"class":114},[100,114174,170],{"class":118},[100,114176,295],{"class":178},[100,114178,126],{"class":118},[100,114180,15869],{"class":178},[100,114182,215],{"class":118},[14,114184,114185,114186,114189],{},"The second item ",[17,114187,114188],{},"(3,)"," has only one value, so unpacking fails.",[77,114191,6244],{"id":6243},[14,114193,114194],{},"Use the fix that matches your situation.",[675,114196,114198],{"id":114197},"_1-match-the-number-of-variables-to-the-number-of-values","1. Match the number of variables to the number of values",[14,114200,114201],{},"If you have one value, unpack into one variable.",[91,114203,114205],{"className":93,"code":114204,"language":95,"meta":96,"style":96},"value = [1]\na = value[0]\nprint(a)\n",[17,114206,114207,114219,114233],{"__ignoreMap":96},[100,114208,114209,114211,114213,114215,114217],{"class":102,"line":103},[100,114210,3318],{"class":106},[100,114212,111],{"class":110},[100,114214,594],{"class":118},[100,114216,123],{"class":122},[100,114218,603],{"class":118},[100,114220,114221,114223,114225,114227,114229,114231],{"class":102,"line":135},[100,114222,82313],{"class":106},[100,114224,111],{"class":110},[100,114226,6747],{"class":106},[100,114228,3166],{"class":118},[100,114230,2279],{"class":122},[100,114232,603],{"class":118},[100,114234,114235,114237,114239,114241],{"class":102,"line":142},[100,114236,372],{"class":114},[100,114238,170],{"class":118},[100,114240,295],{"class":178},[100,114242,215],{"class":118},[14,114244,114245],{},"Or if you expect two values, make sure the data really has two items:",[91,114247,114249],{"className":93,"code":114248,"language":95,"meta":96,"style":96},"a, b = [1, 2]\nprint(a, b)\n",[17,114250,114251,114271],{"__ignoreMap":96},[100,114252,114253,114255,114257,114259,114261,114263,114265,114267,114269],{"class":102,"line":103},[100,114254,295],{"class":106},[100,114256,126],{"class":118},[100,114258,25360],{"class":106},[100,114260,111],{"class":110},[100,114262,594],{"class":118},[100,114264,123],{"class":122},[100,114266,126],{"class":118},[100,114268,129],{"class":122},[100,114270,603],{"class":118},[100,114272,114273,114275,114277,114279,114281,114283],{"class":102,"line":135},[100,114274,372],{"class":114},[100,114276,170],{"class":118},[100,114278,295],{"class":178},[100,114280,126],{"class":118},[100,114282,15869],{"class":178},[100,114284,215],{"class":118},[675,114286,114288],{"id":114287},"_2-check-the-actual-data-before-unpacking","2. Check the actual data before unpacking",[14,114290,114291],{},"Sometimes the data is not what you think it is. Print it first.",[91,114293,114295],{"className":93,"code":114294,"language":95,"meta":96,"style":96},"values = [1]\n\nprint(values)\nprint(type(values))\nprint(len(values))\n",[17,114296,114297,114309,114313,114323,114337],{"__ignoreMap":96},[100,114298,114299,114301,114303,114305,114307],{"class":102,"line":103},[100,114300,4010],{"class":106},[100,114302,111],{"class":110},[100,114304,594],{"class":118},[100,114306,123],{"class":122},[100,114308,603],{"class":118},[100,114310,114311],{"class":102,"line":135},[100,114312,139],{"emptyLinePlaceholder":138},[100,114314,114315,114317,114319,114321],{"class":102,"line":142},[100,114316,372],{"class":114},[100,114318,170],{"class":118},[100,114320,4049],{"class":178},[100,114322,215],{"class":118},[100,114324,114325,114327,114329,114331,114333,114335],{"class":102,"line":152},[100,114326,372],{"class":114},[100,114328,170],{"class":118},[100,114330,1250],{"class":191},[100,114332,170],{"class":118},[100,114334,4049],{"class":178},[100,114336,182],{"class":118},[100,114338,114339,114341,114343,114345,114347,114349],{"class":102,"line":164},[100,114340,372],{"class":114},[100,114342,170],{"class":118},[100,114344,17779],{"class":114},[100,114346,170],{"class":118},[100,114348,4049],{"class":178},[100,114350,182],{"class":118},[14,114352,218],{},[91,114354,114356],{"className":93,"code":114355,"language":95,"meta":96,"style":96},"[1]\n\u003Cclass 'list'>\n1\n",[17,114357,114358,114366,114380],{"__ignoreMap":96},[100,114359,114360,114362,114364],{"class":102,"line":103},[100,114361,3166],{"class":118},[100,114363,123],{"class":122},[100,114365,603],{"class":118},[100,114367,114368,114370,114372,114374,114376,114378],{"class":102,"line":135},[100,114369,2782],{"class":110},[100,114371,3417],{"class":1077},[100,114373,1274],{"class":205},[100,114375,1235],{"class":209},[100,114377,1280],{"class":205},[100,114379,1980],{"class":110},[100,114381,114382],{"class":102,"line":142},[100,114383,228],{"class":122},[14,114385,114386],{},"This quickly shows why unpacking into two variables would fail.",[675,114388,114390],{"id":114389},"_3-use-starred-unpacking-when-length-may-vary","3. Use starred unpacking when length may vary",[14,114392,114393],{},"If the number of items can change, use starred unpacking.",[91,114395,114397],{"className":93,"code":114396,"language":95,"meta":96,"style":96},"values = [10, 20, 30, 40]\n\nfirst, *rest = values\n\nprint(first)\nprint(rest)\n",[17,114398,114399,114424,114428,114442,114446,114456],{"__ignoreMap":96},[100,114400,114401,114403,114405,114407,114409,114411,114413,114415,114417,114419,114422],{"class":102,"line":103},[100,114402,4010],{"class":106},[100,114404,111],{"class":110},[100,114406,594],{"class":118},[100,114408,356],{"class":122},[100,114410,126],{"class":118},[100,114412,361],{"class":122},[100,114414,126],{"class":118},[100,114416,16342],{"class":122},[100,114418,126],{"class":118},[100,114420,114421],{"class":122}," 40",[100,114423,603],{"class":118},[100,114425,114426],{"class":102,"line":135},[100,114427,139],{"emptyLinePlaceholder":138},[100,114429,114430,114432,114434,114436,114438,114440],{"class":102,"line":142},[100,114431,113780],{"class":106},[100,114433,126],{"class":118},[100,114435,113785],{"class":110},[100,114437,113788],{"class":106},[100,114439,111],{"class":110},[100,114441,113793],{"class":106},[100,114443,114444],{"class":102,"line":152},[100,114445,139],{"emptyLinePlaceholder":138},[100,114447,114448,114450,114452,114454],{"class":102,"line":164},[100,114449,372],{"class":114},[100,114451,170],{"class":118},[100,114453,113780],{"class":178},[100,114455,215],{"class":118},[100,114457,114458,114460,114462,114465],{"class":102,"line":185},[100,114459,372],{"class":114},[100,114461,170],{"class":118},[100,114463,114464],{"class":178},"rest",[100,114466,215],{"class":118},[14,114468,218],{},[91,114470,114472],{"className":93,"code":114471,"language":95,"meta":96,"style":96},"10\n[20, 30, 40]\n",[17,114473,114474,114478],{"__ignoreMap":96},[100,114475,114476],{"class":102,"line":103},[100,114477,3410],{"class":122},[100,114479,114480,114482,114484,114486,114488,114490,114492],{"class":102,"line":135},[100,114481,3166],{"class":118},[100,114483,6152],{"class":122},[100,114485,126],{"class":118},[100,114487,16342],{"class":122},[100,114489,126],{"class":118},[100,114491,114421],{"class":122},[100,114493,603],{"class":118},[14,114495,114496],{},"You can also do this:",[91,114498,114500],{"className":93,"code":114499,"language":95,"meta":96,"style":96},"values = [10]\n\nfirst, *rest = values\n\nprint(first)\nprint(rest)\n",[17,114501,114502,114514,114518,114532,114536,114546],{"__ignoreMap":96},[100,114503,114504,114506,114508,114510,114512],{"class":102,"line":103},[100,114505,4010],{"class":106},[100,114507,111],{"class":110},[100,114509,594],{"class":118},[100,114511,356],{"class":122},[100,114513,603],{"class":118},[100,114515,114516],{"class":102,"line":135},[100,114517,139],{"emptyLinePlaceholder":138},[100,114519,114520,114522,114524,114526,114528,114530],{"class":102,"line":142},[100,114521,113780],{"class":106},[100,114523,126],{"class":118},[100,114525,113785],{"class":110},[100,114527,113788],{"class":106},[100,114529,111],{"class":110},[100,114531,113793],{"class":106},[100,114533,114534],{"class":102,"line":152},[100,114535,139],{"emptyLinePlaceholder":138},[100,114537,114538,114540,114542,114544],{"class":102,"line":164},[100,114539,372],{"class":114},[100,114541,170],{"class":118},[100,114543,113780],{"class":178},[100,114545,215],{"class":118},[100,114547,114548,114550,114552,114554],{"class":102,"line":185},[100,114549,372],{"class":114},[100,114551,170],{"class":118},[100,114553,114464],{"class":178},[100,114555,215],{"class":118},[14,114557,218],{},[91,114559,114561],{"className":93,"code":114560,"language":95,"meta":96,"style":96},"10\n[]\n",[17,114562,114563,114567],{"__ignoreMap":96},[100,114564,114565],{"class":102,"line":103},[100,114566,3410],{"class":122},[100,114568,114569],{"class":102,"line":135},[100,114570,114571],{"class":118},"[]\n",[14,114573,114574],{},"This is a safe pattern when you only need the first item and want the rest collected automatically.",[675,114576,114578],{"id":114577},"_4-fix-function-return-values-or-unpack-fewer-variables","4. Fix function return values or unpack fewer variables",[14,114580,114581],{},"If a function returns one value, do not try to unpack two.",[14,114583,9709],{},[91,114585,114587],{"className":93,"code":114586,"language":95,"meta":96,"style":96},"def get_total():\n    return 100\n\nsubtotal, tax = get_total()\n",[17,114588,114589,114597,114603,114607],{"__ignoreMap":96},[100,114590,114591,114593,114595],{"class":102,"line":103},[100,114592,1078],{"class":1077},[100,114594,78335],{"class":1081},[100,114596,1085],{"class":118},[100,114598,114599,114601],{"class":102,"line":135},[100,114600,2552],{"class":145},[100,114602,5377],{"class":122},[100,114604,114605],{"class":102,"line":142},[100,114606,139],{"emptyLinePlaceholder":138},[100,114608,114609,114612,114614,114617,114619,114621],{"class":102,"line":152},[100,114610,114611],{"class":106},"subtotal",[100,114613,126],{"class":118},[100,114615,114616],{"class":106}," tax ",[100,114618,111],{"class":110},[100,114620,78335],{"class":178},[100,114622,4734],{"class":118},[14,114624,15825],{},[91,114626,114628],{"className":93,"code":114627,"language":95,"meta":96,"style":96},"def get_total():\n    return 100\n\ntotal = get_total()\nprint(total)\n",[17,114629,114630,114638,114644,114648,114658],{"__ignoreMap":96},[100,114631,114632,114634,114636],{"class":102,"line":103},[100,114633,1078],{"class":1077},[100,114635,78335],{"class":1081},[100,114637,1085],{"class":118},[100,114639,114640,114642],{"class":102,"line":135},[100,114641,2552],{"class":145},[100,114643,5377],{"class":122},[100,114645,114646],{"class":102,"line":142},[100,114647,139],{"emptyLinePlaceholder":138},[100,114649,114650,114652,114654,114656],{"class":102,"line":152},[100,114651,53376],{"class":106},[100,114653,111],{"class":110},[100,114655,78335],{"class":178},[100,114657,4734],{"class":118},[100,114659,114660,114662,114664,114666],{"class":102,"line":164},[100,114661,372],{"class":114},[100,114663,170],{"class":118},[100,114665,2520],{"class":178},[100,114667,215],{"class":118},[14,114669,114670],{},"Or change the function so it returns the expected number of values:",[91,114672,114674],{"className":93,"code":114673,"language":95,"meta":96,"style":96},"def get_total():\n    return 100, 20\n\nsubtotal, tax = get_total()\nprint(subtotal, tax)\n",[17,114675,114676,114684,114694,114698,114712],{"__ignoreMap":96},[100,114677,114678,114680,114682],{"class":102,"line":103},[100,114679,1078],{"class":1077},[100,114681,78335],{"class":1081},[100,114683,1085],{"class":118},[100,114685,114686,114688,114690,114692],{"class":102,"line":135},[100,114687,2552],{"class":145},[100,114689,51281],{"class":122},[100,114691,126],{"class":118},[100,114693,1669],{"class":122},[100,114695,114696],{"class":102,"line":142},[100,114697,139],{"emptyLinePlaceholder":138},[100,114699,114700,114702,114704,114706,114708,114710],{"class":102,"line":152},[100,114701,114611],{"class":106},[100,114703,126],{"class":118},[100,114705,114616],{"class":106},[100,114707,111],{"class":110},[100,114709,78335],{"class":178},[100,114711,4734],{"class":118},[100,114713,114714,114716,114718,114720,114722,114725],{"class":102,"line":164},[100,114715,372],{"class":114},[100,114717,170],{"class":118},[100,114719,114611],{"class":178},[100,114721,126],{"class":118},[100,114723,114724],{"class":178}," tax",[100,114726,215],{"class":118},[675,114728,114730],{"id":114729},"_5-validate-results-before-assigning","5. Validate results before assigning",[14,114732,114733],{},"This is especially important with user input, file data, or split text.",[91,114735,114737],{"className":93,"code":114736,"language":95,"meta":96,"style":96},"text = \"Alice\"\n\nparts = text.split(\",\")\n\nif len(parts) == 2:\n    name, age = parts\n    print(name, age)\nelse:\n    print(\"Input is missing a comma\")\n",[17,114738,114739,114751,114755,114777,114781,114799,114811,114825,114831],{"__ignoreMap":96},[100,114740,114741,114743,114745,114747,114749],{"class":102,"line":103},[100,114742,4331],{"class":106},[100,114744,111],{"class":110},[100,114746,1708],{"class":205},[100,114748,15343],{"class":209},[100,114750,1714],{"class":205},[100,114752,114753],{"class":102,"line":135},[100,114754,139],{"emptyLinePlaceholder":138},[100,114756,114757,114759,114761,114763,114765,114767,114769,114771,114773,114775],{"class":102,"line":142},[100,114758,4345],{"class":106},[100,114760,111],{"class":110},[100,114762,4350],{"class":106},[100,114764,290],{"class":118},[100,114766,3922],{"class":178},[100,114768,170],{"class":118},[100,114770,206],{"class":205},[100,114772,126],{"class":209},[100,114774,206],{"class":205},[100,114776,215],{"class":118},[100,114778,114779],{"class":102,"line":152},[100,114780,139],{"emptyLinePlaceholder":138},[100,114782,114783,114785,114787,114789,114791,114793,114795,114797],{"class":102,"line":164},[100,114784,2736],{"class":145},[100,114786,3141],{"class":114},[100,114788,170],{"class":118},[100,114790,4373],{"class":178},[100,114792,6155],{"class":118},[100,114794,34516],{"class":110},[100,114796,129],{"class":122},[100,114798,149],{"class":118},[100,114800,114801,114803,114805,114807,114809],{"class":102,"line":185},[100,114802,11387],{"class":106},[100,114804,126],{"class":118},[100,114806,1676],{"class":106},[100,114808,111],{"class":110},[100,114810,100268],{"class":106},[100,114812,114813,114815,114817,114819,114821,114823],{"class":102,"line":197},[100,114814,200],{"class":114},[100,114816,170],{"class":118},[100,114818,2853],{"class":178},[100,114820,126],{"class":118},[100,114822,31058],{"class":178},[100,114824,215],{"class":118},[100,114826,114827,114829],{"class":102,"line":771},[100,114828,4944],{"class":145},[100,114830,149],{"class":118},[100,114832,114833,114835,114837,114839,114842,114844],{"class":102,"line":787},[100,114834,200],{"class":114},[100,114836,170],{"class":118},[100,114838,206],{"class":205},[100,114840,114841],{"class":209},"Input is missing a comma",[100,114843,206],{"class":205},[100,114845,215],{"class":118},[14,114847,114848,114849,290],{},"If you want to learn more about handling unexpected cases, see ",[295,114850,1029],{"href":2824},[77,114852,114854],{"id":114853},"fix-when-using-split","Fix when using split()",[14,114856,114857,94559],{},[17,114858,8090],{},[14,114860,1844],{},[91,114862,114864],{"className":93,"code":114863,"language":95,"meta":96,"style":96},"text = \"Alice\"\nname, age = text.split(\",\")\n",[17,114865,114866,114878],{"__ignoreMap":96},[100,114867,114868,114870,114872,114874,114876],{"class":102,"line":103},[100,114869,4331],{"class":106},[100,114871,111],{"class":110},[100,114873,1708],{"class":205},[100,114875,15343],{"class":209},[100,114877,1714],{"class":205},[100,114879,114880,114882,114884,114886,114888,114890,114892,114894,114896,114898,114900,114902],{"class":102,"line":135},[100,114881,2853],{"class":106},[100,114883,126],{"class":118},[100,114885,1676],{"class":106},[100,114887,111],{"class":110},[100,114889,4350],{"class":106},[100,114891,290],{"class":118},[100,114893,3922],{"class":178},[100,114895,170],{"class":118},[100,114897,206],{"class":205},[100,114899,126],{"class":209},[100,114901,206],{"class":205},[100,114903,215],{"class":118},[14,114905,7194,114906,114909,114910,114912],{},[17,114907,114908],{},"\"Alice\""," does not contain a comma, so ",[17,114911,108483],{}," returns:",[91,114914,114916],{"className":93,"code":114915,"language":95,"meta":96,"style":96},"['Alice']\n",[17,114917,114918],{"__ignoreMap":96},[100,114919,114920,114922,114924,114926,114928],{"class":102,"line":103},[100,114921,3166],{"class":118},[100,114923,1280],{"class":205},[100,114925,15343],{"class":209},[100,114927,1280],{"class":205},[100,114929,603],{"class":118},[14,114931,114932],{},"That is only one part, not two.",[675,114934,114936],{"id":114935},"print-the-result-first","Print the result first",[91,114938,114940],{"className":93,"code":114939,"language":95,"meta":96,"style":96},"text = \"Alice\"\nprint(text.split(\",\"))\n",[17,114941,114942,114954],{"__ignoreMap":96},[100,114943,114944,114946,114948,114950,114952],{"class":102,"line":103},[100,114945,4331],{"class":106},[100,114947,111],{"class":110},[100,114949,1708],{"class":205},[100,114951,15343],{"class":209},[100,114953,1714],{"class":205},[100,114955,114956,114958,114960,114962,114964,114966,114968,114970,114972,114974],{"class":102,"line":135},[100,114957,372],{"class":114},[100,114959,170],{"class":118},[100,114961,4468],{"class":178},[100,114963,290],{"class":118},[100,114965,3922],{"class":178},[100,114967,170],{"class":118},[100,114969,206],{"class":205},[100,114971,126],{"class":209},[100,114973,206],{"class":205},[100,114975,182],{"class":118},[14,114977,218],{},[91,114979,114980],{"className":93,"code":114915,"language":95,"meta":96,"style":96},[17,114981,114982],{"__ignoreMap":96},[100,114983,114984,114986,114988,114990,114992],{"class":102,"line":103},[100,114985,3166],{"class":118},[100,114987,1280],{"class":205},[100,114989,15343],{"class":209},[100,114991,1280],{"class":205},[100,114993,603],{"class":118},[675,114995,114997],{"id":114996},"use-a-condition-before-unpacking","Use a condition before unpacking",[91,114999,115001],{"className":93,"code":115000,"language":95,"meta":96,"style":96},"text = \"Alice,25\"\nparts = text.split(\",\")\n\nif len(parts) == 2:\n    name, age = parts\n    print(\"Name:\", name)\n    print(\"Age:\", age)\nelse:\n    print(\"Text is not in the expected format\")\n",[17,115002,115003,115015,115037,115041,115059,115071,115089,115107,115113],{"__ignoreMap":96},[100,115004,115005,115007,115009,115011,115013],{"class":102,"line":103},[100,115006,4331],{"class":106},[100,115008,111],{"class":110},[100,115010,1708],{"class":205},[100,115012,109805],{"class":209},[100,115014,1714],{"class":205},[100,115016,115017,115019,115021,115023,115025,115027,115029,115031,115033,115035],{"class":102,"line":135},[100,115018,4345],{"class":106},[100,115020,111],{"class":110},[100,115022,4350],{"class":106},[100,115024,290],{"class":118},[100,115026,3922],{"class":178},[100,115028,170],{"class":118},[100,115030,206],{"class":205},[100,115032,126],{"class":209},[100,115034,206],{"class":205},[100,115036,215],{"class":118},[100,115038,115039],{"class":102,"line":142},[100,115040,139],{"emptyLinePlaceholder":138},[100,115042,115043,115045,115047,115049,115051,115053,115055,115057],{"class":102,"line":152},[100,115044,2736],{"class":145},[100,115046,3141],{"class":114},[100,115048,170],{"class":118},[100,115050,4373],{"class":178},[100,115052,6155],{"class":118},[100,115054,34516],{"class":110},[100,115056,129],{"class":122},[100,115058,149],{"class":118},[100,115060,115061,115063,115065,115067,115069],{"class":102,"line":164},[100,115062,11387],{"class":106},[100,115064,126],{"class":118},[100,115066,1676],{"class":106},[100,115068,111],{"class":110},[100,115070,100268],{"class":106},[100,115072,115073,115075,115077,115079,115081,115083,115085,115087],{"class":102,"line":185},[100,115074,200],{"class":114},[100,115076,170],{"class":118},[100,115078,206],{"class":205},[100,115080,95577],{"class":209},[100,115082,206],{"class":205},[100,115084,126],{"class":118},[100,115086,1703],{"class":178},[100,115088,215],{"class":118},[100,115090,115091,115093,115095,115097,115099,115101,115103,115105],{"class":102,"line":197},[100,115092,200],{"class":114},[100,115094,170],{"class":118},[100,115096,206],{"class":205},[100,115098,77165],{"class":209},[100,115100,206],{"class":205},[100,115102,126],{"class":118},[100,115104,31058],{"class":178},[100,115106,215],{"class":118},[100,115108,115109,115111],{"class":102,"line":771},[100,115110,4944],{"class":145},[100,115112,149],{"class":118},[100,115114,115115,115117,115119,115121,115124,115126],{"class":102,"line":787},[100,115116,200],{"class":114},[100,115118,170],{"class":118},[100,115120,206],{"class":205},[100,115122,115123],{"class":209},"Text is not in the expected format",[100,115125,206],{"class":205},[100,115127,215],{"class":118},[675,115129,115131],{"id":115130},"another-safe-approach","Another safe approach",[91,115133,115135],{"className":93,"code":115134,"language":95,"meta":96,"style":96},"text = \"Alice\"\nparts = text.split(\",\")\n\nname = parts[0]\nage = parts[1] if len(parts) > 1 else None\n\nprint(name)\nprint(age)\n",[17,115136,115137,115149,115171,115175,115189,115221,115225,115235],{"__ignoreMap":96},[100,115138,115139,115141,115143,115145,115147],{"class":102,"line":103},[100,115140,4331],{"class":106},[100,115142,111],{"class":110},[100,115144,1708],{"class":205},[100,115146,15343],{"class":209},[100,115148,1714],{"class":205},[100,115150,115151,115153,115155,115157,115159,115161,115163,115165,115167,115169],{"class":102,"line":135},[100,115152,4345],{"class":106},[100,115154,111],{"class":110},[100,115156,4350],{"class":106},[100,115158,290],{"class":118},[100,115160,3922],{"class":178},[100,115162,170],{"class":118},[100,115164,206],{"class":205},[100,115166,126],{"class":209},[100,115168,206],{"class":205},[100,115170,215],{"class":118},[100,115172,115173],{"class":102,"line":142},[100,115174,139],{"emptyLinePlaceholder":138},[100,115176,115177,115179,115181,115183,115185,115187],{"class":102,"line":152},[100,115178,1691],{"class":106},[100,115180,111],{"class":110},[100,115182,95493],{"class":106},[100,115184,3166],{"class":118},[100,115186,2279],{"class":122},[100,115188,603],{"class":118},[100,115190,115191,115193,115195,115197,115199,115201,115203,115205,115207,115209,115211,115213,115215,115217,115219],{"class":102,"line":164},[100,115192,1664],{"class":106},[100,115194,111],{"class":110},[100,115196,95493],{"class":106},[100,115198,3166],{"class":118},[100,115200,123],{"class":122},[100,115202,17591],{"class":118},[100,115204,11904],{"class":145},[100,115206,3141],{"class":114},[100,115208,170],{"class":118},[100,115210,4373],{"class":178},[100,115212,6155],{"class":118},[100,115214,31167],{"class":110},[100,115216,6915],{"class":122},[100,115218,11913],{"class":145},[100,115220,4904],{"class":158},[100,115222,115223],{"class":102,"line":185},[100,115224,139],{"emptyLinePlaceholder":138},[100,115226,115227,115229,115231,115233],{"class":102,"line":197},[100,115228,372],{"class":114},[100,115230,170],{"class":118},[100,115232,2853],{"class":178},[100,115234,215],{"class":118},[100,115236,115237,115239,115241,115243],{"class":102,"line":771},[100,115238,372],{"class":114},[100,115240,170],{"class":118},[100,115242,2770],{"class":178},[100,115244,215],{"class":118},[14,115246,115247,115248,290],{},"If you need a full explanation of this method, see ",[295,115249,1572,115250,4112],{"href":108048},[17,115251,115252],{},"string.split()",[77,115254,115256],{"id":115255},"fix-when-looping","Fix when looping",[14,115258,115259],{},"This error often appears in loops that unpack each item.",[14,115261,70325],{},[91,115263,115265],{"className":93,"code":115264,"language":95,"meta":96,"style":96},"items = [(1, 2), (3, 4), (5,)]\n\nfor a, b in items:\n    print(a, b)\n",[17,115266,115267,115299,115303,115319],{"__ignoreMap":96},[100,115268,115269,115271,115273,115275,115277,115279,115281,115283,115285,115287,115289,115291,115293,115295,115297],{"class":102,"line":103},[100,115270,3095],{"class":106},[100,115272,111],{"class":110},[100,115274,78799],{"class":118},[100,115276,123],{"class":122},[100,115278,126],{"class":118},[100,115280,129],{"class":122},[100,115282,2874],{"class":118},[100,115284,16805],{"class":118},[100,115286,11541],{"class":122},[100,115288,126],{"class":118},[100,115290,2584],{"class":122},[100,115292,2874],{"class":118},[100,115294,16805],{"class":118},[100,115296,3557],{"class":122},[100,115298,114148],{"class":118},[100,115300,115301],{"class":102,"line":135},[100,115302,139],{"emptyLinePlaceholder":138},[100,115304,115305,115307,115309,115311,115313,115315,115317],{"class":102,"line":142},[100,115306,71],{"class":145},[100,115308,78678],{"class":106},[100,115310,126],{"class":118},[100,115312,25360],{"class":106},[100,115314,617],{"class":145},[100,115316,12000],{"class":106},[100,115318,149],{"class":118},[100,115320,115321,115323,115325,115327,115329,115331],{"class":102,"line":152},[100,115322,200],{"class":114},[100,115324,170],{"class":118},[100,115326,295],{"class":178},[100,115328,126],{"class":118},[100,115330,15869],{"class":178},[100,115332,215],{"class":118},[14,115334,115335,115336,115339],{},"The first two items work. The last item does not because ",[17,115337,115338],{},"(5,)"," has only one value.",[675,115341,115343],{"id":115342},"how-to-find-the-bad-item","How to find the bad item",[14,115345,115346],{},"Print each item before unpacking:",[91,115348,115350],{"className":93,"code":115349,"language":95,"meta":96,"style":96},"items = [(1, 2), (3, 4), (5,)]\n\nfor item in items:\n    print(item)\n",[17,115351,115352,115384,115388,115400],{"__ignoreMap":96},[100,115353,115354,115356,115358,115360,115362,115364,115366,115368,115370,115372,115374,115376,115378,115380,115382],{"class":102,"line":103},[100,115355,3095],{"class":106},[100,115357,111],{"class":110},[100,115359,78799],{"class":118},[100,115361,123],{"class":122},[100,115363,126],{"class":118},[100,115365,129],{"class":122},[100,115367,2874],{"class":118},[100,115369,16805],{"class":118},[100,115371,11541],{"class":122},[100,115373,126],{"class":118},[100,115375,2584],{"class":122},[100,115377,2874],{"class":118},[100,115379,16805],{"class":118},[100,115381,3557],{"class":122},[100,115383,114148],{"class":118},[100,115385,115386],{"class":102,"line":135},[100,115387,139],{"emptyLinePlaceholder":138},[100,115389,115390,115392,115394,115396,115398],{"class":102,"line":142},[100,115391,71],{"class":145},[100,115393,7568],{"class":106},[100,115395,617],{"class":145},[100,115397,12000],{"class":106},[100,115399,149],{"class":118},[100,115401,115402,115404,115406,115408],{"class":102,"line":152},[100,115403,200],{"class":114},[100,115405,170],{"class":118},[100,115407,7555],{"class":178},[100,115409,215],{"class":118},[14,115411,218],{},[91,115413,115415],{"className":93,"code":115414,"language":95,"meta":96,"style":96},"(1, 2)\n(3, 4)\n(5,)\n",[17,115416,115417,115429,115441],{"__ignoreMap":96},[100,115418,115419,115421,115423,115425,115427],{"class":102,"line":103},[100,115420,170],{"class":118},[100,115422,123],{"class":122},[100,115424,126],{"class":118},[100,115426,129],{"class":122},[100,115428,215],{"class":118},[100,115430,115431,115433,115435,115437,115439],{"class":102,"line":135},[100,115432,170],{"class":118},[100,115434,11541],{"class":122},[100,115436,126],{"class":118},[100,115438,2584],{"class":122},[100,115440,215],{"class":118},[100,115442,115443,115445,115447],{"class":102,"line":142},[100,115444,170],{"class":118},[100,115446,3557],{"class":122},[100,115448,114046],{"class":118},[14,115450,115451],{},"Now the inconsistent data is easier to spot.",[675,115453,115455],{"id":115454},"safe-version","Safe version",[91,115457,115459],{"className":93,"code":115458,"language":95,"meta":96,"style":96},"items = [(1, 2), (3, 4), (5,)]\n\nfor item in items:\n    if len(item) == 2:\n        a, b = item\n        print(a, b)\n    else:\n        print(\"Bad item:\", item)\n",[17,115460,115461,115493,115497,115509,115527,115541,115555,115561],{"__ignoreMap":96},[100,115462,115463,115465,115467,115469,115471,115473,115475,115477,115479,115481,115483,115485,115487,115489,115491],{"class":102,"line":103},[100,115464,3095],{"class":106},[100,115466,111],{"class":110},[100,115468,78799],{"class":118},[100,115470,123],{"class":122},[100,115472,126],{"class":118},[100,115474,129],{"class":122},[100,115476,2874],{"class":118},[100,115478,16805],{"class":118},[100,115480,11541],{"class":122},[100,115482,126],{"class":118},[100,115484,2584],{"class":122},[100,115486,2874],{"class":118},[100,115488,16805],{"class":118},[100,115490,3557],{"class":122},[100,115492,114148],{"class":118},[100,115494,115495],{"class":102,"line":135},[100,115496,139],{"emptyLinePlaceholder":138},[100,115498,115499,115501,115503,115505,115507],{"class":102,"line":142},[100,115500,71],{"class":145},[100,115502,7568],{"class":106},[100,115504,617],{"class":145},[100,115506,12000],{"class":106},[100,115508,149],{"class":118},[100,115510,115511,115513,115515,115517,115519,115521,115523,115525],{"class":102,"line":152},[100,115512,2777],{"class":145},[100,115514,3141],{"class":114},[100,115516,170],{"class":118},[100,115518,7555],{"class":178},[100,115520,6155],{"class":118},[100,115522,34516],{"class":110},[100,115524,129],{"class":122},[100,115526,149],{"class":118},[100,115528,115529,115532,115534,115536,115538],{"class":102,"line":164},[100,115530,115531],{"class":106},"        a",[100,115533,126],{"class":118},[100,115535,25360],{"class":106},[100,115537,111],{"class":110},[100,115539,115540],{"class":106}," item\n",[100,115542,115543,115545,115547,115549,115551,115553],{"class":102,"line":185},[100,115544,167],{"class":114},[100,115546,170],{"class":118},[100,115548,295],{"class":178},[100,115550,126],{"class":118},[100,115552,15869],{"class":178},[100,115554,215],{"class":118},[100,115556,115557,115559],{"class":102,"line":197},[100,115558,40531],{"class":145},[100,115560,149],{"class":118},[100,115562,115563,115565,115567,115569,115572,115574,115576,115579],{"class":102,"line":771},[100,115564,167],{"class":114},[100,115566,170],{"class":118},[100,115568,206],{"class":205},[100,115570,115571],{"class":209},"Bad item:",[100,115573,206],{"class":205},[100,115575,126],{"class":118},[100,115577,115578],{"class":178}," item",[100,115580,215],{"class":118},[14,115582,115583],{},"This is especially useful when reading rows from files or CSV-like data.",[77,115585,3274],{"id":3273},[14,115587,115588],{},"If you are not sure why the error is happening, use these steps:",[3282,115590,115591,115594,115599,115605],{},[43,115592,115593],{},"Print the value you are unpacking",[43,115595,115596,115597],{},"Check its type with ",[17,115598,4430],{},[43,115600,115601,115602,115604],{},"Check its length with ",[17,115603,32803],{}," if possible",[43,115606,5271],{},[14,115608,115609],{},"Useful commands:",[91,115611,115613],{"className":93,"code":115612,"language":95,"meta":96,"style":96},"print(value)\nprint(type(value))\nprint(len(value))\nprint(text.split(','))\nfor item in items:\n    print(item)\n",[17,115614,115615,115625,115639,115653,115675,115687],{"__ignoreMap":96},[100,115616,115617,115619,115621,115623],{"class":102,"line":103},[100,115618,372],{"class":114},[100,115620,170],{"class":118},[100,115622,757],{"class":178},[100,115624,215],{"class":118},[100,115626,115627,115629,115631,115633,115635,115637],{"class":102,"line":135},[100,115628,372],{"class":114},[100,115630,170],{"class":118},[100,115632,1250],{"class":191},[100,115634,170],{"class":118},[100,115636,757],{"class":178},[100,115638,182],{"class":118},[100,115640,115641,115643,115645,115647,115649,115651],{"class":102,"line":142},[100,115642,372],{"class":114},[100,115644,170],{"class":118},[100,115646,17779],{"class":114},[100,115648,170],{"class":118},[100,115650,757],{"class":178},[100,115652,182],{"class":118},[100,115654,115655,115657,115659,115661,115663,115665,115667,115669,115671,115673],{"class":102,"line":152},[100,115656,372],{"class":114},[100,115658,170],{"class":118},[100,115660,4468],{"class":178},[100,115662,290],{"class":118},[100,115664,3922],{"class":178},[100,115666,170],{"class":118},[100,115668,1280],{"class":205},[100,115670,126],{"class":209},[100,115672,1280],{"class":205},[100,115674,182],{"class":118},[100,115676,115677,115679,115681,115683,115685],{"class":102,"line":164},[100,115678,71],{"class":145},[100,115680,7568],{"class":106},[100,115682,617],{"class":145},[100,115684,12000],{"class":106},[100,115686,149],{"class":118},[100,115688,115689,115691,115693,115695],{"class":102,"line":185},[100,115690,200],{"class":114},[100,115692,170],{"class":118},[100,115694,7555],{"class":178},[100,115696,215],{"class":118},[14,115698,115699],{},"The full error message often tells you exactly what went wrong, such as:",[40,115701,115702,115705],{},[43,115703,115704],{},"expected 2, got 1",[43,115706,115707],{},"expected 3, got 2",[14,115709,115710],{},"That gives you a direct clue about the mismatch.",[77,115712,7117],{"id":7116},[14,115714,46698],{},[40,115716,115717,115720,115725,115728,115731],{},[43,115718,115719],{},"Unpacking a short list or tuple",[43,115721,1357,115722,115724],{},[17,115723,8090],{}," on text that does not contain the expected separator",[43,115726,115727],{},"Expecting a function to return two or more values when it returns one",[43,115729,115730],{},"Looping through nested data with inconsistent item lengths",[43,115732,115733],{},"Reading rows from a file or CSV-like source with missing fields",[14,115735,115736,115737,115739],{},"It can also help to compare this error with ",[295,115738,79329],{"href":79328},", which is the opposite problem.",[77,115741,1514],{"id":1513},[675,115743,115745],{"id":115744},"what-does-unpacking-mean-in-python","What does unpacking mean in Python?",[14,115747,113804],{},[675,115749,115751],{"id":115750},"how-is-this-different-from-too-many-values-to-unpack","How is this different from \"too many values to unpack\"?",[14,115753,115754],{},"This error means there are fewer values than expected. The other error means there are more values than expected.",[675,115756,115758],{"id":115757},"how-do-i-handle-variable-length-data-safely","How do I handle variable-length data safely?",[14,115760,115761],{},"Use starred unpacking, such as:",[91,115763,115765],{"className":93,"code":115764,"language":95,"meta":96,"style":96},"first, *rest = values\n",[17,115766,115767],{"__ignoreMap":96},[100,115768,115769,115771,115773,115775,115777,115779],{"class":102,"line":103},[100,115770,113780],{"class":106},[100,115772,126],{"class":118},[100,115774,113785],{"class":110},[100,115776,113788],{"class":106},[100,115778,111],{"class":110},[100,115780,113793],{"class":106},[14,115782,115783,115784,115786,115787,290],{},"This lets Python put the first item into ",[17,115785,113780],{}," and collect the remaining items into ",[17,115788,114464],{},[675,115790,115792],{"id":115791},"can-split-cause-this-error","Can split() cause this error?",[14,115794,115795,115796,115798],{},"Yes. If ",[17,115797,8090],{}," returns fewer parts than your variables expect, unpacking will fail.",[77,115800,1554],{"id":1553},[40,115802,115803,115807,115813,115817,115821],{},[43,115804,115805],{},[295,115806,79329],{"href":79328},[43,115808,115809],{},[295,115810,1572,115811,4112],{"href":108048},[17,115812,115252],{},[43,115814,115815],{},[295,115816,1581],{"href":2824},[43,115818,115819],{},[295,115820,1587],{"href":1749},[43,115822,115823],{},[295,115824,115826],{"href":115825},"\u002Flearn\u002Funderstanding-python-statements-and-expressions\u002F","Understanding Python statements and expressions",[1589,115828,115829],{},"html pre.shiki code .sZMiF, html code.shiki .sZMiF{--shiki-light:#E2931D;--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 .smGrS, html code.shiki .smGrS{--shiki-light:#39ADB5;--shiki-default:#D73A49;--shiki-dark:#F97583}html pre.shiki code .su5hD, html code.shiki .su5hD{--shiki-light:#90A4AE;--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 pre.shiki code .srdBf, html code.shiki .srdBf{--shiki-light:#F76D47;--shiki-default:#005CC5;--shiki-dark:#79B8FF}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 .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 .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}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 .s39Yj, html code.shiki .s39Yj{--shiki-light:#39ADB5;--shiki-default:#005CC5;--shiki-dark:#79B8FF}",{"title":96,"searchDepth":135,"depth":135,"links":115831},[115832,115833,115834,115835,115840,115847,115852,115856,115857,115858,115864],{"id":79,"depth":135,"text":80},{"id":5880,"depth":135,"text":5881},{"id":5944,"depth":135,"text":5945},{"id":25090,"depth":135,"text":25091,"children":115836},[115837,115838,115839],{"id":114027,"depth":142,"text":114028},{"id":114070,"depth":142,"text":114071},{"id":114119,"depth":142,"text":114120},{"id":6243,"depth":135,"text":6244,"children":115841},[115842,115843,115844,115845,115846],{"id":114197,"depth":142,"text":114198},{"id":114287,"depth":142,"text":114288},{"id":114389,"depth":142,"text":114390},{"id":114577,"depth":142,"text":114578},{"id":114729,"depth":142,"text":114730},{"id":114853,"depth":135,"text":114854,"children":115848},[115849,115850,115851],{"id":114935,"depth":142,"text":114936},{"id":114996,"depth":142,"text":114997},{"id":115130,"depth":142,"text":115131},{"id":115255,"depth":135,"text":115256,"children":115853},[115854,115855],{"id":115342,"depth":142,"text":115343},{"id":115454,"depth":142,"text":115455},{"id":3273,"depth":135,"text":3274},{"id":7116,"depth":135,"text":7117},{"id":1513,"depth":135,"text":1514,"children":115859},[115860,115861,115862,115863],{"id":115744,"depth":142,"text":115745},{"id":115750,"depth":142,"text":115751},{"id":115757,"depth":142,"text":115758},{"id":115791,"depth":142,"text":115792},{"id":1553,"depth":135,"text":1554},"Master valueerror not enough values to unpack fix in our comprehensive Python beginner guide.",{},"\u002Ferrors\u002Fvalueerror-not-enough-values-to-unpack-fix",{"title":79453,"description":115865},"errors\u002Fvalueerror-not-enough-values-to-unpack-fix","UMjuzsRKYGsQOib5E-dndlUplW0Rlb5u5h-xt4F0u0E",{"id":115872,"title":109201,"body":115873,"description":117151,"extension":1623,"meta":117152,"navigation":138,"path":117153,"seo":117154,"stem":117155,"__hash__":117156},"content\u002Ferrors\u002Fvalueerror-substring-not-found-fix.md",{"type":7,"value":115874,"toc":117128},[115875,115878,115883,115889,115891,115903,115905,115910,116010,116012,116026,116043,116045,116048,116054,116063,116065,116067,116089,116091,116093,116133,116142,116144,116162,116166,116171,116266,116268,116276,116278,116298,116304,116308,116316,116323,116426,116428,116440,116442,116453,116456,116472,116476,116479,116482,116495,116499,116592,116594,116603,116611,116615,116701,116703,116712,116727,116731,116739,116816,116818,116845,116848,116850,116852,116869,116871,116957,116960,116964,116967,117003,117006,117008,117011,117030,117038,117040,117044,117050,117054,117062,117066,117075,117079,117082,117084,117125],[10,115876,109201],{"id":115877},"valueerror-substring-not-found-fix",[14,115879,115880,115882],{},[17,115881,108930],{}," means Python could not find the piece of text you asked for inside a string.",[14,115884,115885,115886,115888],{},"This error usually appears when you use string methods like ",[17,115887,109477],{}," that expect a match. If the substring is missing, Python raises an error instead of returning a special value.",[14,115890,110518],{},[40,115892,115893,115895,115897,115900],{},[43,115894,85186],{},[43,115896,85189],{},[43,115898,115899],{},"examples that cause it",[43,115901,115902],{},"simple ways to fix it",[77,115904,80],{"id":79},[14,115906,115907,115908,89],{},"If the substring may be missing, check first before calling ",[17,115909,109477],{},[91,115911,115913],{"className":93,"code":115912,"language":95,"meta":96,"style":96},"text = \"hello world\"\nsub = \"python\"\n\nif sub in text:\n    pos = text.index(sub)\n    print(pos)\nelse:\n    print(\"Substring not found\")\n",[17,115914,115915,115928,115941,115945,115958,115978,115989,115995],{"__ignoreMap":96},[100,115916,115917,115919,115921,115923,115926],{"class":102,"line":103},[100,115918,4331],{"class":106},[100,115920,111],{"class":110},[100,115922,1708],{"class":205},[100,115924,115925],{"class":209},"hello world",[100,115927,1714],{"class":205},[100,115929,115930,115933,115935,115937,115939],{"class":102,"line":135},[100,115931,115932],{"class":106},"sub ",[100,115934,111],{"class":110},[100,115936,1708],{"class":205},[100,115938,95],{"class":209},[100,115940,1714],{"class":205},[100,115942,115943],{"class":102,"line":142},[100,115944,139],{"emptyLinePlaceholder":138},[100,115946,115947,115949,115952,115954,115956],{"class":102,"line":152},[100,115948,2736],{"class":145},[100,115950,115951],{"class":106}," sub ",[100,115953,617],{"class":110},[100,115955,4350],{"class":106},[100,115957,149],{"class":118},[100,115959,115960,115963,115965,115967,115969,115971,115973,115976],{"class":102,"line":164},[100,115961,115962],{"class":106},"    pos ",[100,115964,111],{"class":110},[100,115966,4350],{"class":106},[100,115968,290],{"class":118},[100,115970,3169],{"class":178},[100,115972,170],{"class":118},[100,115974,115975],{"class":178},"sub",[100,115977,215],{"class":118},[100,115979,115980,115982,115984,115987],{"class":102,"line":185},[100,115981,200],{"class":114},[100,115983,170],{"class":118},[100,115985,115986],{"class":178},"pos",[100,115988,215],{"class":118},[100,115990,115991,115993],{"class":102,"line":197},[100,115992,4944],{"class":145},[100,115994,149],{"class":118},[100,115996,115997,115999,116001,116003,116006,116008],{"class":102,"line":771},[100,115998,200],{"class":114},[100,116000,170],{"class":118},[100,116002,206],{"class":205},[100,116004,116005],{"class":209},"Substring not found",[100,116007,206],{"class":205},[100,116009,215],{"class":118},[14,116011,218],{},[91,116013,116015],{"className":93,"code":116014,"language":95,"meta":96,"style":96},"Substring not found\n",[17,116016,116017],{"__ignoreMap":96},[100,116018,116019,116022,116024],{"class":102,"line":103},[100,116020,116021],{"class":106},"Substring ",[100,116023,1059],{"class":110},[100,116025,43722],{"class":106},[14,116027,241,116028,82051,116030,116032,116033,116039,116040,116042],{},[17,116029,617],{},[17,116031,109477],{}," if the substring may be missing. You can also use ",[295,116034,116036],{"href":116035},"\u002Freference\u002Fpython-string-find-method\u002F",[17,116037,116038],{},"find()"," when you want ",[17,116041,35345],{}," instead of an error.",[77,116044,5881],{"id":5880},[14,116046,116047],{},"This error means Python could not find the text you searched for inside a string.",[14,116049,571,116050,116053],{},[22,116051,116052],{},"substring"," is just a smaller piece of text inside a larger string.",[14,116055,116056,116057,116062],{},"This usually happens with ",[295,116058,116060],{"href":116059},"\u002Freference\u002Fpython-string-index-method\u002F",[17,116061,109449],{}," or similar methods that raise an exception when no match is found.",[77,116064,26289],{"id":26288},[14,116066,112518],{},[40,116068,116069,116075,116083,116086],{},[43,116070,315,116071,116074],{},[17,116072,116073],{},"text.index(\"something\")"," when that text is not present",[43,116076,116077,116078,18659,116081],{},"Searching with the wrong capitalization, such as ",[17,116079,116080],{},"\"Hello\"",[17,116082,3519],{},[43,116084,116085],{},"Searching text that includes extra spaces or hidden characters",[43,116087,116088],{},"Using user input that does not match the string exactly",[77,116090,11031],{"id":11030},[14,116092,18361],{},[91,116094,116096],{"className":93,"code":116095,"language":95,"meta":96,"style":96},"word = \"apple\"\nprint(word.index(\"z\"))\n",[17,116097,116098,116110],{"__ignoreMap":96},[100,116099,116100,116102,116104,116106,116108],{"class":102,"line":103},[100,116101,33213],{"class":106},[100,116103,111],{"class":110},[100,116105,1708],{"class":205},[100,116107,4148],{"class":209},[100,116109,1714],{"class":205},[100,116111,116112,116114,116116,116118,116120,116122,116124,116126,116129,116131],{"class":102,"line":135},[100,116113,372],{"class":114},[100,116115,170],{"class":118},[100,116117,8478],{"class":178},[100,116119,290],{"class":118},[100,116121,3169],{"class":178},[100,116123,170],{"class":118},[100,116125,206],{"class":205},[100,116127,116128],{"class":209},"z",[100,116130,206],{"class":205},[100,116132,182],{"class":118},[14,116134,116135,116138,116139,116141],{},[17,116136,116137],{},"\"z\""," is not inside ",[17,116140,33104],{},", so Python raises an error.",[14,116143,28421],{},[91,116145,116147],{"className":93,"code":116146,"language":95,"meta":96,"style":96},"ValueError: substring not found\n",[17,116148,116149],{"__ignoreMap":96},[100,116150,116151,116153,116155,116158,116160],{"class":102,"line":103},[100,116152,3483],{"class":191},[100,116154,89],{"class":118},[100,116156,116157],{"class":106}," substring ",[100,116159,1059],{"class":110},[100,116161,43722],{"class":106},[77,116163,116165],{"id":116164},"fix-1-check-before-using-index","Fix 1: Check before using index()",[14,116167,116168,116169,290],{},"A simple fix is to test whether the substring exists before calling ",[17,116170,109477],{},[91,116172,116174],{"className":93,"code":116173,"language":95,"meta":96,"style":96},"text = \"banana\"\nsub = \"na\"\n\nif sub in text:\n    position = text.index(sub)\n    print(position)\nelse:\n    print(\"Substring not found\")\n",[17,116175,116176,116188,116201,116205,116217,116236,116246,116252],{"__ignoreMap":96},[100,116177,116178,116180,116182,116184,116186],{"class":102,"line":103},[100,116179,4331],{"class":106},[100,116181,111],{"class":110},[100,116183,1708],{"class":205},[100,116185,4157],{"class":209},[100,116187,1714],{"class":205},[100,116189,116190,116192,116194,116196,116199],{"class":102,"line":135},[100,116191,115932],{"class":106},[100,116193,111],{"class":110},[100,116195,1708],{"class":205},[100,116197,116198],{"class":209},"na",[100,116200,1714],{"class":205},[100,116202,116203],{"class":102,"line":142},[100,116204,139],{"emptyLinePlaceholder":138},[100,116206,116207,116209,116211,116213,116215],{"class":102,"line":152},[100,116208,2736],{"class":145},[100,116210,115951],{"class":106},[100,116212,617],{"class":110},[100,116214,4350],{"class":106},[100,116216,149],{"class":118},[100,116218,116219,116222,116224,116226,116228,116230,116232,116234],{"class":102,"line":164},[100,116220,116221],{"class":106},"    position ",[100,116223,111],{"class":110},[100,116225,4350],{"class":106},[100,116227,290],{"class":118},[100,116229,3169],{"class":178},[100,116231,170],{"class":118},[100,116233,115975],{"class":178},[100,116235,215],{"class":118},[100,116237,116238,116240,116242,116244],{"class":102,"line":185},[100,116239,200],{"class":114},[100,116241,170],{"class":118},[100,116243,48175],{"class":178},[100,116245,215],{"class":118},[100,116247,116248,116250],{"class":102,"line":197},[100,116249,4944],{"class":145},[100,116251,149],{"class":118},[100,116253,116254,116256,116258,116260,116262,116264],{"class":102,"line":771},[100,116255,200],{"class":114},[100,116257,170],{"class":118},[100,116259,206],{"class":205},[100,116261,116005],{"class":209},[100,116263,206],{"class":205},[100,116265,215],{"class":118},[14,116267,218],{},[91,116269,116270],{"className":93,"code":233,"language":95,"meta":96,"style":96},[17,116271,116272],{"__ignoreMap":96},[100,116273,116274],{"class":102,"line":103},[100,116275,233],{"class":122},[14,116277,65047],{},[40,116279,116280,116288,116293],{},[43,116281,116282,551,116285,116287],{},[17,116283,116284],{},"sub in text",[17,116286,19246],{}," if the substring exists",[43,116289,116290,116292],{},[17,116291,109477],{}," only runs when the substring is present",[43,116294,116295,116296],{},"this avoids the ",[17,116297,3483],{},[14,116299,116300,116301,290],{},"This is one of the clearest beginner-friendly solutions. If you want more on this approach, see ",[295,116302,116303],{"href":109195},"how to check if a string contains a substring in Python",[77,116305,116307],{"id":116306},"fix-2-use-find-instead-of-index","Fix 2: Use find() instead of index()",[14,116309,116310,116311,10873],{},"If a missing substring is normal in your program, use ",[295,116312,116313],{"href":116035},[17,116314,116315],{},"str.find()",[14,116317,116318,551,116320,116322],{},[17,116319,116038],{},[17,116321,35345],{}," when the substring is not found.",[91,116324,116326],{"className":93,"code":116325,"language":95,"meta":96,"style":96},"text = \"hello world\"\nsub = \"python\"\n\nposition = text.find(sub)\n\nif position != -1:\n    print(position)\nelse:\n    print(\"Substring not found\")\n",[17,116327,116328,116340,116352,116356,116376,116380,116396,116406,116412],{"__ignoreMap":96},[100,116329,116330,116332,116334,116336,116338],{"class":102,"line":103},[100,116331,4331],{"class":106},[100,116333,111],{"class":110},[100,116335,1708],{"class":205},[100,116337,115925],{"class":209},[100,116339,1714],{"class":205},[100,116341,116342,116344,116346,116348,116350],{"class":102,"line":135},[100,116343,115932],{"class":106},[100,116345,111],{"class":110},[100,116347,1708],{"class":205},[100,116349,95],{"class":209},[100,116351,1714],{"class":205},[100,116353,116354],{"class":102,"line":142},[100,116355,139],{"emptyLinePlaceholder":138},[100,116357,116358,116361,116363,116365,116367,116370,116372,116374],{"class":102,"line":152},[100,116359,116360],{"class":106},"position ",[100,116362,111],{"class":110},[100,116364,4350],{"class":106},[100,116366,290],{"class":118},[100,116368,116369],{"class":178},"find",[100,116371,170],{"class":118},[100,116373,115975],{"class":178},[100,116375,215],{"class":118},[100,116377,116378],{"class":102,"line":164},[100,116379,139],{"emptyLinePlaceholder":138},[100,116381,116382,116384,116387,116390,116392,116394],{"class":102,"line":185},[100,116383,2736],{"class":145},[100,116385,116386],{"class":106}," position ",[100,116388,116389],{"class":110},"!=",[100,116391,1906],{"class":110},[100,116393,123],{"class":122},[100,116395,149],{"class":118},[100,116397,116398,116400,116402,116404],{"class":102,"line":197},[100,116399,200],{"class":114},[100,116401,170],{"class":118},[100,116403,48175],{"class":178},[100,116405,215],{"class":118},[100,116407,116408,116410],{"class":102,"line":771},[100,116409,4944],{"class":145},[100,116411,149],{"class":118},[100,116413,116414,116416,116418,116420,116422,116424],{"class":102,"line":787},[100,116415,200],{"class":114},[100,116417,170],{"class":118},[100,116419,206],{"class":205},[100,116421,116005],{"class":209},[100,116423,206],{"class":205},[100,116425,215],{"class":118},[14,116427,218],{},[91,116429,116430],{"className":93,"code":116014,"language":95,"meta":96,"style":96},[17,116431,116432],{"__ignoreMap":96},[100,116433,116434,116436,116438],{"class":102,"line":103},[100,116435,116021],{"class":106},[100,116437,1059],{"class":110},[100,116439,43722],{"class":106},[14,116441,49650],{},[40,116443,116444,116447,116450],{},[43,116445,116446],{},"you expect some searches to fail",[43,116448,116449],{},"you do not want an exception",[43,116451,116452],{},"you want to handle the result yourself",[14,116454,116455],{},"Difference:",[40,116457,116458,116465],{},[43,116459,116460,116462,116463],{},[17,116461,109477],{}," → raises ",[17,116464,3483],{},[43,116466,116467,116469,116470],{},[17,116468,116038],{}," → returns ",[17,116471,35345],{},[77,116473,116475],{"id":116474},"fix-3-normalize-the-text-before-searching","Fix 3: Normalize the text before searching",[14,116477,116478],{},"Sometimes the substring is there logically, but the text does not match exactly.",[14,116480,116481],{},"Common problems include:",[40,116483,116484,116487,116489,116492],{},[43,116485,116486],{},"uppercase vs lowercase letters",[43,116488,44962],{},[43,116490,116491],{},"newline characters",[43,116493,116494],{},"hidden characters from file or user input",[675,116496,116498],{"id":116497},"use-lower-for-case-insensitive-matching","Use lower() for case-insensitive matching",[91,116500,116502],{"className":93,"code":116501,"language":95,"meta":96,"style":96},"text = \"Hello World\"\nsub = \"hello\"\n\nif sub.lower() in text.lower():\n    print(\"Found\")\nelse:\n    print(\"Not found\")\n",[17,116503,116504,116517,116529,116533,116556,116571,116577],{"__ignoreMap":96},[100,116505,116506,116508,116510,116512,116515],{"class":102,"line":103},[100,116507,4331],{"class":106},[100,116509,111],{"class":110},[100,116511,1708],{"class":205},[100,116513,116514],{"class":209},"Hello World",[100,116516,1714],{"class":205},[100,116518,116519,116521,116523,116525,116527],{"class":102,"line":135},[100,116520,115932],{"class":106},[100,116522,111],{"class":110},[100,116524,1708],{"class":205},[100,116526,3506],{"class":209},[100,116528,1714],{"class":205},[100,116530,116531],{"class":102,"line":142},[100,116532,139],{"emptyLinePlaceholder":138},[100,116534,116535,116537,116540,116542,116544,116546,116548,116550,116552,116554],{"class":102,"line":152},[100,116536,2736],{"class":145},[100,116538,116539],{"class":106}," sub",[100,116541,290],{"class":118},[100,116543,5802],{"class":178},[100,116545,7562],{"class":118},[100,116547,1971],{"class":110},[100,116549,4350],{"class":106},[100,116551,290],{"class":118},[100,116553,5802],{"class":178},[100,116555,1085],{"class":118},[100,116557,116558,116560,116562,116564,116567,116569],{"class":102,"line":164},[100,116559,200],{"class":114},[100,116561,170],{"class":118},[100,116563,206],{"class":205},[100,116565,116566],{"class":209},"Found",[100,116568,206],{"class":205},[100,116570,215],{"class":118},[100,116572,116573,116575],{"class":102,"line":185},[100,116574,4944],{"class":145},[100,116576,149],{"class":118},[100,116578,116579,116581,116583,116585,116588,116590],{"class":102,"line":197},[100,116580,200],{"class":114},[100,116582,170],{"class":118},[100,116584,206],{"class":205},[100,116586,116587],{"class":209},"Not found",[100,116589,206],{"class":205},[100,116591,215],{"class":118},[14,116593,218],{},[91,116595,116597],{"className":93,"code":116596,"language":95,"meta":96,"style":96},"Found\n",[17,116598,116599],{"__ignoreMap":96},[100,116600,116601],{"class":102,"line":103},[100,116602,116596],{"class":106},[14,116604,116605,116606,290],{},"If needed, learn more about ",[295,116607,116609],{"href":116608},"\u002Freference\u002Fpython-string-lower-method\u002F",[17,116610,7394],{},[675,116612,116614],{"id":116613},"use-strip-to-remove-extra-spaces","Use strip() to remove extra spaces",[91,116616,116618],{"className":93,"code":116617,"language":95,"meta":96,"style":96},"text = \"  apple  \"\nsub = \"apple\"\n\nif sub == text.strip():\n    print(\"Match\")\nelse:\n    print(\"No match\")\n",[17,116619,116620,116633,116645,116649,116665,116680,116686],{"__ignoreMap":96},[100,116621,116622,116624,116626,116628,116631],{"class":102,"line":103},[100,116623,4331],{"class":106},[100,116625,111],{"class":110},[100,116627,1708],{"class":205},[100,116629,116630],{"class":209},"  apple  ",[100,116632,1714],{"class":205},[100,116634,116635,116637,116639,116641,116643],{"class":102,"line":135},[100,116636,115932],{"class":106},[100,116638,111],{"class":110},[100,116640,1708],{"class":205},[100,116642,4148],{"class":209},[100,116644,1714],{"class":205},[100,116646,116647],{"class":102,"line":142},[100,116648,139],{"emptyLinePlaceholder":138},[100,116650,116651,116653,116655,116657,116659,116661,116663],{"class":102,"line":152},[100,116652,2736],{"class":145},[100,116654,115951],{"class":106},[100,116656,32169],{"class":110},[100,116658,4350],{"class":106},[100,116660,290],{"class":118},[100,116662,6235],{"class":178},[100,116664,1085],{"class":118},[100,116666,116667,116669,116671,116673,116676,116678],{"class":102,"line":164},[100,116668,200],{"class":114},[100,116670,170],{"class":118},[100,116672,206],{"class":205},[100,116674,116675],{"class":209},"Match",[100,116677,206],{"class":205},[100,116679,215],{"class":118},[100,116681,116682,116684],{"class":102,"line":185},[100,116683,4944],{"class":145},[100,116685,149],{"class":118},[100,116687,116688,116690,116692,116694,116697,116699],{"class":102,"line":197},[100,116689,200],{"class":114},[100,116691,170],{"class":118},[100,116693,206],{"class":205},[100,116695,116696],{"class":209},"No match",[100,116698,206],{"class":205},[100,116700,215],{"class":118},[14,116702,218],{},[91,116704,116706],{"className":93,"code":116705,"language":95,"meta":96,"style":96},"Match\n",[17,116707,116708],{"__ignoreMap":96},[100,116709,116710],{"class":102,"line":103},[100,116711,116705],{"class":106},[14,116713,116714,116716,116717,28239,116721,290],{},[17,116715,8739],{}," removes whitespace from the start and end of a string. See also ",[295,116718,116720],{"href":116719},"\u002Fhow-to\u002Fhow-to-remove-whitespace-from-a-string-in-python\u002F","how to remove whitespace from a string in Python",[295,116722,116724,116726],{"href":116723},"\u002Freference\u002Fpython-string-strip-method\u002F",[17,116725,8739],{}," method reference",[675,116728,116730],{"id":116729},"use-repr-to-spot-hidden-characters","Use repr() to spot hidden characters",[14,116732,116733,116735,116736,116738],{},[17,116734,13235],{}," can hide useful details. ",[17,116737,99698],{}," shows them more clearly.",[91,116740,116742],{"className":93,"code":116741,"language":95,"meta":96,"style":96},"text = \"apple\\n\"\nsub = \"apple\"\n\nprint(repr(text))\nprint(repr(sub))\nprint(sub in text)\n",[17,116743,116744,116758,116770,116774,116788,116802],{"__ignoreMap":96},[100,116745,116746,116748,116750,116752,116754,116756],{"class":102,"line":103},[100,116747,4331],{"class":106},[100,116749,111],{"class":110},[100,116751,1708],{"class":205},[100,116753,4148],{"class":209},[100,116755,20187],{"class":527},[100,116757,1714],{"class":205},[100,116759,116760,116762,116764,116766,116768],{"class":102,"line":135},[100,116761,115932],{"class":106},[100,116763,111],{"class":110},[100,116765,1708],{"class":205},[100,116767,4148],{"class":209},[100,116769,1714],{"class":205},[100,116771,116772],{"class":102,"line":142},[100,116773,139],{"emptyLinePlaceholder":138},[100,116775,116776,116778,116780,116782,116784,116786],{"class":102,"line":152},[100,116777,372],{"class":114},[100,116779,170],{"class":118},[100,116781,12228],{"class":114},[100,116783,170],{"class":118},[100,116785,4468],{"class":178},[100,116787,182],{"class":118},[100,116789,116790,116792,116794,116796,116798,116800],{"class":102,"line":164},[100,116791,372],{"class":114},[100,116793,170],{"class":118},[100,116795,12228],{"class":114},[100,116797,170],{"class":118},[100,116799,115975],{"class":178},[100,116801,182],{"class":118},[100,116803,116804,116806,116808,116810,116812,116814],{"class":102,"line":185},[100,116805,372],{"class":114},[100,116807,170],{"class":118},[100,116809,115932],{"class":178},[100,116811,617],{"class":145},[100,116813,4350],{"class":178},[100,116815,215],{"class":118},[14,116817,218],{},[91,116819,116821],{"className":93,"code":116820,"language":95,"meta":96,"style":96},"'apple\\n'\n'apple'\nTrue\n",[17,116822,116823,116833,116841],{"__ignoreMap":96},[100,116824,116825,116827,116829,116831],{"class":102,"line":103},[100,116826,1280],{"class":3553},[100,116828,4148],{"class":3556},[100,116830,20187],{"class":107694},[100,116832,3925],{"class":3553},[100,116834,116835,116837,116839],{"class":102,"line":135},[100,116836,1280],{"class":3553},[100,116838,4148],{"class":3556},[100,116840,3925],{"class":3553},[100,116842,116843],{"class":102,"line":142},[100,116844,3431],{"class":158},[14,116846,116847],{},"This is helpful when debugging file input or copied text.",[77,116849,3274],{"id":3273},[14,116851,45117],{},[3282,116853,116854,116857,116860,116863],{},[43,116855,116856],{},"Print the full string you are searching in.",[43,116858,116859],{},"Print the exact substring you are searching for.",[43,116861,116862],{},"Check capitalization, spaces, punctuation, and line breaks.",[43,116864,116865,116866,290],{},"Test whether the substring actually exists with ",[17,116867,116868],{},"substring in text",[14,116870,5295],{},[91,116872,116874],{"className":93,"code":116873,"language":95,"meta":96,"style":96},"print(text)\nprint(substring)\nprint(repr(text))\nprint(repr(substring))\nprint(substring in text)\nprint(text.find(substring))\n",[17,116875,116876,116886,116896,116910,116924,116939],{"__ignoreMap":96},[100,116877,116878,116880,116882,116884],{"class":102,"line":103},[100,116879,372],{"class":114},[100,116881,170],{"class":118},[100,116883,4468],{"class":178},[100,116885,215],{"class":118},[100,116887,116888,116890,116892,116894],{"class":102,"line":135},[100,116889,372],{"class":114},[100,116891,170],{"class":118},[100,116893,116052],{"class":178},[100,116895,215],{"class":118},[100,116897,116898,116900,116902,116904,116906,116908],{"class":102,"line":142},[100,116899,372],{"class":114},[100,116901,170],{"class":118},[100,116903,12228],{"class":114},[100,116905,170],{"class":118},[100,116907,4468],{"class":178},[100,116909,182],{"class":118},[100,116911,116912,116914,116916,116918,116920,116922],{"class":102,"line":152},[100,116913,372],{"class":114},[100,116915,170],{"class":118},[100,116917,12228],{"class":114},[100,116919,170],{"class":118},[100,116921,116052],{"class":178},[100,116923,182],{"class":118},[100,116925,116926,116928,116930,116933,116935,116937],{"class":102,"line":164},[100,116927,372],{"class":114},[100,116929,170],{"class":118},[100,116931,116932],{"class":178},"substring ",[100,116934,617],{"class":145},[100,116936,4350],{"class":178},[100,116938,215],{"class":118},[100,116940,116941,116943,116945,116947,116949,116951,116953,116955],{"class":102,"line":185},[100,116942,372],{"class":114},[100,116944,170],{"class":118},[100,116946,4468],{"class":178},[100,116948,290],{"class":118},[100,116950,116369],{"class":178},[100,116952,170],{"class":118},[100,116954,116052],{"class":178},[100,116956,182],{"class":118},[14,116958,116959],{},"These checks often reveal the real problem quickly.",[77,116961,116963],{"id":116962},"related-methods-to-understand","Related methods to understand",[14,116965,116966],{},"These string methods are closely related:",[40,116968,116969,116978,116986,116995],{},[43,116970,116971,20442,116975,116977],{},[295,116972,116973],{"href":116059},[17,116974,109449],{},[17,116976,3483],{}," if the substring is not found",[43,116979,116980,551,116984,116977],{},[295,116981,116982],{"href":116035},[17,116983,116315],{},[17,116985,35345],{},[43,116987,116988,116991,116992,116994],{},[17,116989,116990],{},"str.rindex()"," searches from the right side and raises ",[17,116993,3483],{}," if not found",[43,116996,116997,117000,117001,116994],{},[17,116998,116999],{},"str.rfind()"," searches from the right side and returns ",[17,117002,35345],{},[14,117004,117005],{},"Choose the method based on how you want your program to behave when there is no match.",[77,117007,7117],{"id":7116},[14,117009,117010],{},"These are common reasons for this error:",[40,117012,117013,117018,117021,117024,117027],{},[43,117014,1357,117015,117017],{},[17,117016,109449],{}," for text that may not exist",[43,117019,117020],{},"Case mismatch between the string and substring",[43,117022,117023],{},"Leading or trailing spaces in either value",[43,117025,117026],{},"Unexpected newline characters from file input",[43,117028,117029],{},"Assuming user input always matches expected text",[14,117031,117032,117033,117035,117036,290],{},"If you are seeing many different ",[17,117034,3483],{}," problems, it may also help to read ",[295,117037,3265],{"href":3264},[77,117039,1514],{"id":1513},[675,117041,117043],{"id":117042},"what-causes-valueerror-substring-not-found-in-python","What causes \"ValueError: substring not found\" in Python?",[14,117045,117046,117047,117049],{},"It usually happens when you use ",[17,117048,109449],{}," and the substring does not exist in the string.",[675,117051,117053],{"id":117052},"how-do-i-avoid-this-error","How do I avoid this error?",[14,117055,42784,117056,117058,117059,117061],{},[17,117057,617],{}," first, or use ",[17,117060,116038],{}," if you want a safe return value instead of an exception.",[675,117063,117065],{"id":117064},"what-is-the-difference-between-index-and-find","What is the difference between index() and find()?",[14,117067,117068,117070,117071,551,117073,290],{},[17,117069,109477],{}," raises an error if not found. ",[17,117072,116038],{},[17,117074,35345],{},[675,117076,117078],{"id":117077},"can-spaces-or-capitalization-cause-this-error","Can spaces or capitalization cause this error?",[14,117080,117081],{},"Yes. String searches must match exactly unless you clean or normalize the text first.",[77,117083,1554],{"id":1553},[40,117085,117086,117092,117098,117105,117112,117116,117121],{},[43,117087,117088],{},[295,117089,117090,116726],{"href":116059},[17,117091,109449],{},[43,117093,117094],{},[295,117095,117096,116726],{"href":116035},[17,117097,116315],{},[43,117099,117100],{},[295,117101,117102,116726],{"href":116608},[17,117103,117104],{},"str.lower()",[43,117106,117107],{},[295,117108,117109,116726],{"href":116723},[17,117110,117111],{},"str.strip()",[43,117113,117114],{},[295,117115,109196],{"href":109195},[43,117117,117118],{},[295,117119,117120],{"href":116719},"How to remove whitespace from a string in Python",[43,117122,117123],{},[295,117124,3265],{"href":3264},[1589,117126,117127],{},"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 .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 .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 pre.shiki code .sptTA, html code.shiki .sptTA{--shiki-light:#6182B8;--shiki-default:#005CC5;--shiki-dark:#79B8FF}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 .s_hVV, html code.shiki .s_hVV{--shiki-light:#90A4AE;--shiki-default:#005CC5;--shiki-dark:#79B8FF}html pre.shiki code .s2W-s, html code.shiki .s2W-s{--shiki-light:#39ADB5;--shiki-light-font-style:italic;--shiki-default:#032F62;--shiki-default-font-style:inherit;--shiki-dark:#9ECBFF;--shiki-dark-font-style:inherit}html pre.shiki code .sithA, html code.shiki .sithA{--shiki-light:#90A4AE;--shiki-light-font-style:italic;--shiki-default:#032F62;--shiki-default-font-style:inherit;--shiki-dark:#9ECBFF;--shiki-dark-font-style:inherit}html pre.shiki code .sfi6f, html code.shiki .sfi6f{--shiki-light:#90A4AE;--shiki-light-font-style:italic;--shiki-default:#005CC5;--shiki-default-font-style:inherit;--shiki-dark:#79B8FF;--shiki-dark-font-style:inherit}html pre.shiki code .s39Yj, html code.shiki .s39Yj{--shiki-light:#39ADB5;--shiki-default:#005CC5;--shiki-dark:#79B8FF}",{"title":96,"searchDepth":135,"depth":135,"links":117129},[117130,117131,117132,117133,117134,117135,117136,117141,117142,117143,117144,117150],{"id":79,"depth":135,"text":80},{"id":5880,"depth":135,"text":5881},{"id":26288,"depth":135,"text":26289},{"id":11030,"depth":135,"text":11031},{"id":116164,"depth":135,"text":116165},{"id":116306,"depth":135,"text":116307},{"id":116474,"depth":135,"text":116475,"children":117137},[117138,117139,117140],{"id":116497,"depth":142,"text":116498},{"id":116613,"depth":142,"text":116614},{"id":116729,"depth":142,"text":116730},{"id":3273,"depth":135,"text":3274},{"id":116962,"depth":135,"text":116963},{"id":7116,"depth":135,"text":7117},{"id":1513,"depth":135,"text":1514,"children":117145},[117146,117147,117148,117149],{"id":117042,"depth":142,"text":117043},{"id":117052,"depth":142,"text":117053},{"id":117064,"depth":142,"text":117065},{"id":117077,"depth":142,"text":117078},{"id":1553,"depth":135,"text":1554},"Master valueerror substring not found fix in our comprehensive Python beginner guide.",{},"\u002Ferrors\u002Fvalueerror-substring-not-found-fix",{"title":109201,"description":117151},"errors\u002Fvalueerror-substring-not-found-fix","1vW3IuK1GDhkEp3YFDvBmRT4Njfh_4H8SYZ_YCinh7g",{"id":117158,"title":79458,"body":117159,"description":119038,"extension":1623,"meta":119039,"navigation":138,"path":119040,"seo":119041,"stem":119042,"__hash__":119043},"content\u002Ferrors\u002Fvalueerror-too-many-values-to-unpack-fix.md",{"type":7,"value":117160,"toc":119006},[117161,117164,117169,117172,117190,117193,117195,117310,117313,117315,117318,117320,117365,117367,117380,117383,117390,117393,117395,117432,117434,117456,117458,117474,117477,117540,117542,117555,117557,117560,117562,117576,117581,117585,117588,117592,117668,117670,117688,117692,117770,117772,117786,117789,117800,117804,117807,117812,117904,117906,117936,117942,117945,117956,117959,118022,118024,118045,118051,118055,118058,118131,118134,118137,118218,118220,118241,118245,118248,118317,118320,118325,118331,118334,118379,118385,118417,118421,118506,118510,118516,118593,118595,118614,118621,118623,118626,118628,118671,118674,118709,118712,118739,118743,118759,118761,118838,118840,118885,118888,118892,118895,118916,118918,118921,118941,118943,118947,118950,118957,118960,118964,118970,118974,118977,118979,119003],[10,117162,79458],{"id":117163},"valueerror-too-many-values-to-unpack-fix",[14,117165,117166,117168],{},[17,117167,79329],{}," happens when Python tries to assign more values than your variables can hold.",[14,117170,117171],{},"This usually appears when:",[40,117173,117174,117177,117182,117185],{},[43,117175,117176],{},"you unpack a list or tuple into too few variables",[43,117178,82313,117179,117181],{},[17,117180,71],{}," loop expects the wrong number of values",[43,117183,117184],{},"a function returns more values than expected",[43,117186,117187,117189],{},[17,117188,8090],{}," gives you more parts than you planned for",[14,117191,117192],{},"The fix is usually simple: make the number of variables match the number of values, or use starred unpacking to collect extras.",[77,117194,80],{"id":79},[91,117196,117198],{"className":93,"code":117197,"language":95,"meta":96,"style":96},"values = [1, 2, 3]\na, b, c = values  # number of variables matches number of items\n\npairs = [(\"a\", 1), (\"b\", 2)]\nfor key, value in pairs:\n    print(key, value)\n",[17,117199,117200,117220,117240,117244,117279,117296],{"__ignoreMap":96},[100,117201,117202,117204,117206,117208,117210,117212,117214,117216,117218],{"class":102,"line":103},[100,117203,4010],{"class":106},[100,117205,111],{"class":110},[100,117207,594],{"class":118},[100,117209,123],{"class":122},[100,117211,126],{"class":118},[100,117213,129],{"class":122},[100,117215,126],{"class":118},[100,117217,1405],{"class":122},[100,117219,603],{"class":118},[100,117221,117222,117224,117226,117228,117230,117232,117234,117237],{"class":102,"line":135},[100,117223,295],{"class":106},[100,117225,126],{"class":118},[100,117227,15869],{"class":106},[100,117229,126],{"class":118},[100,117231,100263],{"class":106},[100,117233,111],{"class":110},[100,117235,117236],{"class":106}," values  ",[100,117238,117239],{"class":414},"# number of variables matches number of items\n",[100,117241,117242],{"class":102,"line":142},[100,117243,139],{"emptyLinePlaceholder":138},[100,117245,117246,117249,117251,117253,117255,117257,117259,117261,117263,117265,117267,117269,117271,117273,117275,117277],{"class":102,"line":152},[100,117247,117248],{"class":106},"pairs ",[100,117250,111],{"class":110},[100,117252,78799],{"class":118},[100,117254,206],{"class":205},[100,117256,295],{"class":209},[100,117258,206],{"class":205},[100,117260,126],{"class":118},[100,117262,6915],{"class":122},[100,117264,2874],{"class":118},[100,117266,16805],{"class":118},[100,117268,206],{"class":205},[100,117270,4027],{"class":209},[100,117272,206],{"class":205},[100,117274,126],{"class":118},[100,117276,129],{"class":122},[100,117278,78829],{"class":118},[100,117280,117281,117283,117285,117287,117289,117291,117294],{"class":102,"line":164},[100,117282,71],{"class":145},[100,117284,67508],{"class":106},[100,117286,126],{"class":118},[100,117288,989],{"class":106},[100,117290,617],{"class":145},[100,117292,117293],{"class":106}," pairs",[100,117295,149],{"class":118},[100,117297,117298,117300,117302,117304,117306,117308],{"class":102,"line":185},[100,117299,200],{"class":114},[100,117301,170],{"class":118},[100,117303,43824],{"class":178},[100,117305,126],{"class":118},[100,117307,6747],{"class":178},[100,117309,215],{"class":118},[14,117311,117312],{},"Match the number of variables to the number of values. In loops, also check the shape of each item you are unpacking.",[77,117314,5881],{"id":5880},[14,117316,117317],{},"Python lets you unpack values into separate variables.",[14,117319,580],{},[91,117321,117323],{"className":93,"code":117322,"language":95,"meta":96,"style":96},"x, y = (10, 20)\nprint(x)\nprint(y)\n",[17,117324,117325,117345,117355],{"__ignoreMap":96},[100,117326,117327,117329,117331,117333,117335,117337,117339,117341,117343],{"class":102,"line":103},[100,117328,39024],{"class":106},[100,117330,126],{"class":118},[100,117332,39029],{"class":106},[100,117334,111],{"class":110},[100,117336,16805],{"class":118},[100,117338,356],{"class":122},[100,117340,126],{"class":118},[100,117342,361],{"class":122},[100,117344,215],{"class":118},[100,117346,117347,117349,117351,117353],{"class":102,"line":135},[100,117348,372],{"class":114},[100,117350,170],{"class":118},[100,117352,39024],{"class":178},[100,117354,215],{"class":118},[100,117356,117357,117359,117361,117363],{"class":102,"line":142},[100,117358,372],{"class":114},[100,117360,170],{"class":118},[100,117362,39057],{"class":178},[100,117364,215],{"class":118},[14,117366,218],{},[91,117368,117370],{"className":93,"code":117369,"language":95,"meta":96,"style":96},"10\n20\n",[17,117371,117372,117376],{"__ignoreMap":96},[100,117373,117374],{"class":102,"line":103},[100,117375,3410],{"class":122},[100,117377,117378],{"class":102,"line":135},[100,117379,79076],{"class":122},[14,117381,117382],{},"Here, the tuple has 2 items, and there are 2 variables.",[14,117384,117385,117386,117389],{},"The error happens when the value on the right side has ",[22,117387,117388],{},"more items"," than the variables on the left side.",[14,117391,117392],{},"A common example is trying to unpack 3 items into 2 variables.",[77,117394,91663],{"id":91662},[91,117396,117398],{"className":93,"code":117397,"language":95,"meta":96,"style":96},"values = [1, 2, 3]\na, b = values\n",[17,117399,117400,117420],{"__ignoreMap":96},[100,117401,117402,117404,117406,117408,117410,117412,117414,117416,117418],{"class":102,"line":103},[100,117403,4010],{"class":106},[100,117405,111],{"class":110},[100,117407,594],{"class":118},[100,117409,123],{"class":122},[100,117411,126],{"class":118},[100,117413,129],{"class":122},[100,117415,126],{"class":118},[100,117417,1405],{"class":122},[100,117419,603],{"class":118},[100,117421,117422,117424,117426,117428,117430],{"class":102,"line":135},[100,117423,295],{"class":106},[100,117425,126],{"class":118},[100,117427,25360],{"class":106},[100,117429,111],{"class":110},[100,117431,113793],{"class":106},[14,117433,7710],{},[91,117435,117436],{"className":93,"code":99219,"language":95,"meta":96,"style":96},[17,117437,117438],{"__ignoreMap":96},[100,117439,117440,117442,117444,117446,117448,117450,117452,117454],{"class":102,"line":103},[100,117441,3483],{"class":191},[100,117443,89],{"class":118},[100,117445,99230],{"class":106},[100,117447,99233],{"class":178},[100,117449,170],{"class":118},[100,117451,99238],{"class":178},[100,117453,559],{"class":122},[100,117455,215],{"class":118},[14,117457,32043],{},[40,117459,117460,117465,117471],{},[43,117461,117462,117464],{},[17,117463,4049],{}," contains 3 items",[43,117466,117467,117470],{},[17,117468,117469],{},"a, b"," only provides 2 variables",[43,117472,117473],{},"Python does not know what to do with the extra item",[14,117475,117476],{},"To fix it, either use 3 variables:",[91,117478,117480],{"className":93,"code":117479,"language":95,"meta":96,"style":96},"values = [1, 2, 3]\na, b, c = values\n\nprint(a, b, c)\n",[17,117481,117482,117502,117518,117522],{"__ignoreMap":96},[100,117483,117484,117486,117488,117490,117492,117494,117496,117498,117500],{"class":102,"line":103},[100,117485,4010],{"class":106},[100,117487,111],{"class":110},[100,117489,594],{"class":118},[100,117491,123],{"class":122},[100,117493,126],{"class":118},[100,117495,129],{"class":122},[100,117497,126],{"class":118},[100,117499,1405],{"class":122},[100,117501,603],{"class":118},[100,117503,117504,117506,117508,117510,117512,117514,117516],{"class":102,"line":135},[100,117505,295],{"class":106},[100,117507,126],{"class":118},[100,117509,15869],{"class":106},[100,117511,126],{"class":118},[100,117513,100263],{"class":106},[100,117515,111],{"class":110},[100,117517,113793],{"class":106},[100,117519,117520],{"class":102,"line":142},[100,117521,139],{"emptyLinePlaceholder":138},[100,117523,117524,117526,117528,117530,117532,117534,117536,117538],{"class":102,"line":152},[100,117525,372],{"class":114},[100,117527,170],{"class":118},[100,117529,295],{"class":178},[100,117531,126],{"class":118},[100,117533,15869],{"class":178},[100,117535,126],{"class":118},[100,117537,100285],{"class":178},[100,117539,215],{"class":118},[14,117541,218],{},[91,117543,117545],{"className":93,"code":117544,"language":95,"meta":96,"style":96},"1 2 3\n",[17,117546,117547],{"__ignoreMap":96},[100,117548,117549,117551,117553],{"class":102,"line":103},[100,117550,123],{"class":122},[100,117552,129],{"class":122},[100,117554,31170],{"class":122},[77,117556,25091],{"id":25090},[14,117558,117559],{},"This error usually means your code expected a different shape of data.",[14,117561,3971],{},[40,117563,117564,117567,117570,117573],{},[43,117565,117566],{},"The value on the right side has more items than expected.",[43,117568,117569],{},"A loop is unpacking each item incorrectly.",[43,117571,117572],{},"A function returns more values than your code expects.",[43,117574,117575],{},"A string split produces more parts than your variables can hold.",[14,117577,117578,117579,290],{},"For example, if you are learning how functions return multiple values, see ",[295,117580,29749],{"href":29189},[77,117582,117584],{"id":117583},"fix-1-use-the-correct-number-of-variables","Fix 1: Use the correct number of variables",[14,117586,117587],{},"If you know exactly how many values you will get, use the same number of variables.",[675,117589,117591],{"id":117590},"example-with-a-tuple","Example with a tuple",[91,117593,117595],{"className":93,"code":117594,"language":95,"meta":96,"style":96},"point = (5, 10, 15)\nx, y, z = point\n\nprint(x)\nprint(y)\nprint(z)\n",[17,117596,117597,117617,117634,117638,117648,117658],{"__ignoreMap":96},[100,117598,117599,117601,117603,117605,117607,117609,117611,117613,117615],{"class":102,"line":103},[100,117600,39003],{"class":106},[100,117602,111],{"class":110},[100,117604,16805],{"class":118},[100,117606,3557],{"class":122},[100,117608,126],{"class":118},[100,117610,6550],{"class":122},[100,117612,126],{"class":118},[100,117614,81952],{"class":122},[100,117616,215],{"class":118},[100,117618,117619,117621,117623,117625,117627,117630,117632],{"class":102,"line":135},[100,117620,39024],{"class":106},[100,117622,126],{"class":118},[100,117624,78293],{"class":106},[100,117626,126],{"class":118},[100,117628,117629],{"class":106}," z ",[100,117631,111],{"class":110},[100,117633,39034],{"class":106},[100,117635,117636],{"class":102,"line":142},[100,117637,139],{"emptyLinePlaceholder":138},[100,117639,117640,117642,117644,117646],{"class":102,"line":152},[100,117641,372],{"class":114},[100,117643,170],{"class":118},[100,117645,39024],{"class":178},[100,117647,215],{"class":118},[100,117649,117650,117652,117654,117656],{"class":102,"line":164},[100,117651,372],{"class":114},[100,117653,170],{"class":118},[100,117655,39057],{"class":178},[100,117657,215],{"class":118},[100,117659,117660,117662,117664,117666],{"class":102,"line":185},[100,117661,372],{"class":114},[100,117663,170],{"class":118},[100,117665,116128],{"class":178},[100,117667,215],{"class":118},[14,117669,218],{},[91,117671,117673],{"className":93,"code":117672,"language":95,"meta":96,"style":96},"5\n10\n15\n",[17,117674,117675,117679,117683],{"__ignoreMap":96},[100,117676,117677],{"class":102,"line":103},[100,117678,1909],{"class":122},[100,117680,117681],{"class":102,"line":135},[100,117682,3410],{"class":122},[100,117684,117685],{"class":102,"line":142},[100,117686,117687],{"class":122},"15\n",[675,117689,117691],{"id":117690},"example-with-a-function-return-value","Example with a function return value",[91,117693,117695],{"className":93,"code":117694,"language":95,"meta":96,"style":96},"def get_user():\n    return \"Sam\", 25, \"admin\"\n\nname, age, role = get_user()\nprint(name, age, role)\n",[17,117696,117697,117705,117728,117732,117751],{"__ignoreMap":96},[100,117698,117699,117701,117703],{"class":102,"line":103},[100,117700,1078],{"class":1077},[100,117702,114083],{"class":1081},[100,117704,1085],{"class":118},[100,117706,117707,117709,117711,117713,117715,117717,117719,117721,117723,117726],{"class":102,"line":135},[100,117708,2552],{"class":145},[100,117710,1708],{"class":205},[100,117712,29329],{"class":209},[100,117714,206],{"class":205},[100,117716,126],{"class":118},[100,117718,16782],{"class":122},[100,117720,126],{"class":118},[100,117722,1708],{"class":205},[100,117724,117725],{"class":209},"admin",[100,117727,1714],{"class":205},[100,117729,117730],{"class":102,"line":142},[100,117731,139],{"emptyLinePlaceholder":138},[100,117733,117734,117736,117738,117740,117742,117745,117747,117749],{"class":102,"line":152},[100,117735,2853],{"class":106},[100,117737,126],{"class":118},[100,117739,31058],{"class":106},[100,117741,126],{"class":118},[100,117743,117744],{"class":106}," role ",[100,117746,111],{"class":110},[100,117748,114083],{"class":178},[100,117750,4734],{"class":118},[100,117752,117753,117755,117757,117759,117761,117763,117765,117768],{"class":102,"line":164},[100,117754,372],{"class":114},[100,117756,170],{"class":118},[100,117758,2853],{"class":178},[100,117760,126],{"class":118},[100,117762,31058],{"class":178},[100,117764,126],{"class":118},[100,117766,117767],{"class":178}," role",[100,117769,215],{"class":118},[14,117771,218],{},[91,117773,117775],{"className":93,"code":117774,"language":95,"meta":96,"style":96},"Sam 25 admin\n",[17,117776,117777],{"__ignoreMap":96},[100,117778,117779,117781,117783],{"class":102,"line":103},[100,117780,77121],{"class":106},[100,117782,27135],{"class":122},[100,117784,117785],{"class":106}," admin\n",[14,117787,117788],{},"This is the best fix when:",[40,117790,117791,117794,117797],{},[43,117792,117793],{},"the number of values is fixed",[43,117795,117796],{},"you know exactly what each value means",[43,117798,117799],{},"you want clear variable names",[77,117801,117803],{"id":117802},"fix-2-use-starred-unpacking","Fix 2: Use starred unpacking",[14,117805,117806],{},"Sometimes you only care about the first or last values, and the number of extra items may change.",[14,117808,117809,117810,290],{},"In that case, use starred unpacking with ",[17,117811,3252],{},[91,117813,117815],{"className":93,"code":117814,"language":95,"meta":96,"style":96},"data = [10, 20, 30, 40, 50]\n\nfirst, *middle, last = data\n\nprint(first)\nprint(middle)\nprint(last)\n",[17,117816,117817,117845,117849,117869,117873,117883,117893],{"__ignoreMap":96},[100,117818,117819,117821,117823,117825,117827,117829,117831,117833,117835,117837,117839,117841,117843],{"class":102,"line":103},[100,117820,7909],{"class":106},[100,117822,111],{"class":110},[100,117824,594],{"class":118},[100,117826,356],{"class":122},[100,117828,126],{"class":118},[100,117830,361],{"class":122},[100,117832,126],{"class":118},[100,117834,16342],{"class":122},[100,117836,126],{"class":118},[100,117838,114421],{"class":122},[100,117840,126],{"class":118},[100,117842,101094],{"class":122},[100,117844,603],{"class":118},[100,117846,117847],{"class":102,"line":135},[100,117848,139],{"emptyLinePlaceholder":138},[100,117850,117851,117853,117855,117857,117860,117862,117865,117867],{"class":102,"line":142},[100,117852,113780],{"class":106},[100,117854,126],{"class":118},[100,117856,113785],{"class":110},[100,117858,117859],{"class":106},"middle",[100,117861,126],{"class":118},[100,117863,117864],{"class":106}," last ",[100,117866,111],{"class":110},[100,117868,51622],{"class":106},[100,117870,117871],{"class":102,"line":152},[100,117872,139],{"emptyLinePlaceholder":138},[100,117874,117875,117877,117879,117881],{"class":102,"line":164},[100,117876,372],{"class":114},[100,117878,170],{"class":118},[100,117880,113780],{"class":178},[100,117882,215],{"class":118},[100,117884,117885,117887,117889,117891],{"class":102,"line":185},[100,117886,372],{"class":114},[100,117888,170],{"class":118},[100,117890,117859],{"class":178},[100,117892,215],{"class":118},[100,117894,117895,117897,117899,117902],{"class":102,"line":197},[100,117896,372],{"class":114},[100,117898,170],{"class":118},[100,117900,117901],{"class":178},"last",[100,117903,215],{"class":118},[14,117905,218],{},[91,117907,117909],{"className":93,"code":117908,"language":95,"meta":96,"style":96},"10\n[20, 30, 40]\n50\n",[17,117910,117911,117915,117931],{"__ignoreMap":96},[100,117912,117913],{"class":102,"line":103},[100,117914,3410],{"class":122},[100,117916,117917,117919,117921,117923,117925,117927,117929],{"class":102,"line":135},[100,117918,3166],{"class":118},[100,117920,6152],{"class":122},[100,117922,126],{"class":118},[100,117924,16342],{"class":122},[100,117926,126],{"class":118},[100,117928,114421],{"class":122},[100,117930,603],{"class":118},[100,117932,117933],{"class":102,"line":142},[100,117934,117935],{"class":122},"50\n",[14,117937,43223,117938,117941],{},[17,117939,117940],{},"*middle"," part collects all extra values into a list.",[14,117943,117944],{},"This is helpful when:",[40,117946,117947,117950,117953],{},[43,117948,117949],{},"the number of extra items is not fixed",[43,117951,117952],{},"you want to ignore or collect the remaining values",[43,117954,117955],{},"you only need part of the sequence",[14,117957,117958],{},"Another simple example:",[91,117960,117962],{"className":93,"code":117961,"language":95,"meta":96,"style":96},"values = [1, 2, 3]\na, *rest = values\n\nprint(a)\nprint(rest)\n",[17,117963,117964,117984,117998,118002,118012],{"__ignoreMap":96},[100,117965,117966,117968,117970,117972,117974,117976,117978,117980,117982],{"class":102,"line":103},[100,117967,4010],{"class":106},[100,117969,111],{"class":110},[100,117971,594],{"class":118},[100,117973,123],{"class":122},[100,117975,126],{"class":118},[100,117977,129],{"class":122},[100,117979,126],{"class":118},[100,117981,1405],{"class":122},[100,117983,603],{"class":118},[100,117985,117986,117988,117990,117992,117994,117996],{"class":102,"line":135},[100,117987,295],{"class":106},[100,117989,126],{"class":118},[100,117991,113785],{"class":110},[100,117993,113788],{"class":106},[100,117995,111],{"class":110},[100,117997,113793],{"class":106},[100,117999,118000],{"class":102,"line":142},[100,118001,139],{"emptyLinePlaceholder":138},[100,118003,118004,118006,118008,118010],{"class":102,"line":152},[100,118005,372],{"class":114},[100,118007,170],{"class":118},[100,118009,295],{"class":178},[100,118011,215],{"class":118},[100,118013,118014,118016,118018,118020],{"class":102,"line":164},[100,118015,372],{"class":114},[100,118017,170],{"class":118},[100,118019,114464],{"class":178},[100,118021,215],{"class":118},[14,118023,218],{},[91,118025,118027],{"className":93,"code":118026,"language":95,"meta":96,"style":96},"1\n[2, 3]\n",[17,118028,118029,118033],{"__ignoreMap":96},[100,118030,118031],{"class":102,"line":103},[100,118032,228],{"class":122},[100,118034,118035,118037,118039,118041,118043],{"class":102,"line":135},[100,118036,3166],{"class":118},[100,118038,559],{"class":122},[100,118040,126],{"class":118},[100,118042,1405],{"class":122},[100,118044,603],{"class":118},[14,118046,118047,118048,290],{},"If you want to understand unpacking more generally, it can help to review ",[295,118049,118050],{"href":115825},"Python statements and expressions",[77,118052,118054],{"id":118053},"fix-3-check-loops-that-unpack-items","Fix 3: Check loops that unpack items",[14,118056,118057],{},"A very common cause is a loop like this:",[91,118059,118061],{"className":93,"code":118060,"language":95,"meta":96,"style":96},"data = [(1, 2, 3), (4, 5, 6)]\n\nfor a, b in data:\n    print(a, b)\n",[17,118062,118063,118097,118101,118117],{"__ignoreMap":96},[100,118064,118065,118067,118069,118071,118073,118075,118077,118079,118081,118083,118085,118087,118089,118091,118093,118095],{"class":102,"line":103},[100,118066,7909],{"class":106},[100,118068,111],{"class":110},[100,118070,78799],{"class":118},[100,118072,123],{"class":122},[100,118074,126],{"class":118},[100,118076,129],{"class":122},[100,118078,126],{"class":118},[100,118080,1405],{"class":122},[100,118082,2874],{"class":118},[100,118084,16805],{"class":118},[100,118086,8322],{"class":122},[100,118088,126],{"class":118},[100,118090,31164],{"class":122},[100,118092,126],{"class":118},[100,118094,78826],{"class":122},[100,118096,78829],{"class":118},[100,118098,118099],{"class":102,"line":135},[100,118100,139],{"emptyLinePlaceholder":138},[100,118102,118103,118105,118107,118109,118111,118113,118115],{"class":102,"line":142},[100,118104,71],{"class":145},[100,118106,78678],{"class":106},[100,118108,126],{"class":118},[100,118110,25360],{"class":106},[100,118112,617],{"class":145},[100,118114,12102],{"class":106},[100,118116,149],{"class":118},[100,118118,118119,118121,118123,118125,118127,118129],{"class":102,"line":152},[100,118120,200],{"class":114},[100,118122,170],{"class":118},[100,118124,295],{"class":178},[100,118126,126],{"class":118},[100,118128,15869],{"class":178},[100,118130,215],{"class":118},[14,118132,118133],{},"This fails because each item has 3 values, but the loop expects 2.",[14,118135,118136],{},"You can fix it by matching the shape of each item:",[91,118138,118140],{"className":93,"code":118139,"language":95,"meta":96,"style":96},"data = [(1, 2, 3), (4, 5, 6)]\n\nfor a, b, c in data:\n    print(a, b, c)\n",[17,118141,118142,118176,118180,118200],{"__ignoreMap":96},[100,118143,118144,118146,118148,118150,118152,118154,118156,118158,118160,118162,118164,118166,118168,118170,118172,118174],{"class":102,"line":103},[100,118145,7909],{"class":106},[100,118147,111],{"class":110},[100,118149,78799],{"class":118},[100,118151,123],{"class":122},[100,118153,126],{"class":118},[100,118155,129],{"class":122},[100,118157,126],{"class":118},[100,118159,1405],{"class":122},[100,118161,2874],{"class":118},[100,118163,16805],{"class":118},[100,118165,8322],{"class":122},[100,118167,126],{"class":118},[100,118169,31164],{"class":122},[100,118171,126],{"class":118},[100,118173,78826],{"class":122},[100,118175,78829],{"class":118},[100,118177,118178],{"class":102,"line":135},[100,118179,139],{"emptyLinePlaceholder":138},[100,118181,118182,118184,118186,118188,118190,118192,118194,118196,118198],{"class":102,"line":142},[100,118183,71],{"class":145},[100,118185,78678],{"class":106},[100,118187,126],{"class":118},[100,118189,15869],{"class":106},[100,118191,126],{"class":118},[100,118193,100263],{"class":106},[100,118195,617],{"class":145},[100,118197,12102],{"class":106},[100,118199,149],{"class":118},[100,118201,118202,118204,118206,118208,118210,118212,118214,118216],{"class":102,"line":152},[100,118203,200],{"class":114},[100,118205,170],{"class":118},[100,118207,295],{"class":178},[100,118209,126],{"class":118},[100,118211,15869],{"class":178},[100,118213,126],{"class":118},[100,118215,100285],{"class":178},[100,118217,215],{"class":118},[14,118219,218],{},[91,118221,118223],{"className":93,"code":118222,"language":95,"meta":96,"style":96},"1 2 3\n4 5 6\n",[17,118224,118225,118233],{"__ignoreMap":96},[100,118226,118227,118229,118231],{"class":102,"line":103},[100,118228,123],{"class":122},[100,118230,129],{"class":122},[100,118232,31170],{"class":122},[100,118234,118235,118237,118239],{"class":102,"line":135},[100,118236,8322],{"class":122},[100,118238,31164],{"class":122},[100,118240,78890],{"class":122},[675,118242,118244],{"id":118243},"watch-out-for-mixed-data","Watch out for mixed data",[14,118246,118247],{},"If items have different lengths, unpacking can fail partway through the loop.",[91,118249,118251],{"className":93,"code":118250,"language":95,"meta":96,"style":96},"data = [(1, 2), (3, 4, 5)]\n\nfor a, b in data:\n    print(a, b)\n",[17,118252,118253,118283,118287,118303],{"__ignoreMap":96},[100,118254,118255,118257,118259,118261,118263,118265,118267,118269,118271,118273,118275,118277,118279,118281],{"class":102,"line":103},[100,118256,7909],{"class":106},[100,118258,111],{"class":110},[100,118260,78799],{"class":118},[100,118262,123],{"class":122},[100,118264,126],{"class":118},[100,118266,129],{"class":122},[100,118268,2874],{"class":118},[100,118270,16805],{"class":118},[100,118272,11541],{"class":122},[100,118274,126],{"class":118},[100,118276,2584],{"class":122},[100,118278,126],{"class":118},[100,118280,31164],{"class":122},[100,118282,78829],{"class":118},[100,118284,118285],{"class":102,"line":135},[100,118286,139],{"emptyLinePlaceholder":138},[100,118288,118289,118291,118293,118295,118297,118299,118301],{"class":102,"line":142},[100,118290,71],{"class":145},[100,118292,78678],{"class":106},[100,118294,126],{"class":118},[100,118296,25360],{"class":106},[100,118298,617],{"class":145},[100,118300,12102],{"class":106},[100,118302,149],{"class":118},[100,118304,118305,118307,118309,118311,118313,118315],{"class":102,"line":152},[100,118306,200],{"class":114},[100,118308,170],{"class":118},[100,118310,295],{"class":178},[100,118312,126],{"class":118},[100,118314,15869],{"class":178},[100,118316,215],{"class":118},[14,118318,118319],{},"The first item works, but the second item has 3 values, so Python raises the error there.",[14,118321,118322,118323,290],{},"This also happens often when looping through dictionaries incorrectly. If you need help with that, see ",[295,118324,79083],{"href":79082},[77,118326,118328,118329,36421],{"id":118327},"fix-4-check-split-results","Fix 4: Check ",[17,118330,8090],{},[14,118332,118333],{},"String splitting is another common source of this error.",[91,118335,118337],{"className":93,"code":118336,"language":95,"meta":96,"style":96},"text = \"apple,banana,cherry\"\nfirst, second = text.split(\",\")\n",[17,118338,118339,118352],{"__ignoreMap":96},[100,118340,118341,118343,118345,118347,118350],{"class":102,"line":103},[100,118342,4331],{"class":106},[100,118344,111],{"class":110},[100,118346,1708],{"class":205},[100,118348,118349],{"class":209},"apple,banana,cherry",[100,118351,1714],{"class":205},[100,118353,118354,118356,118358,118361,118363,118365,118367,118369,118371,118373,118375,118377],{"class":102,"line":135},[100,118355,113780],{"class":106},[100,118357,126],{"class":118},[100,118359,118360],{"class":106}," second ",[100,118362,111],{"class":110},[100,118364,4350],{"class":106},[100,118366,290],{"class":118},[100,118368,3922],{"class":178},[100,118370,170],{"class":118},[100,118372,206],{"class":205},[100,118374,126],{"class":209},[100,118376,206],{"class":205},[100,118378,215],{"class":118},[14,118380,118381,118382,118384],{},"This raises the error because ",[17,118383,108483],{}," returns 3 parts:",[91,118386,118387],{"className":93,"code":8922,"language":95,"meta":96,"style":96},[17,118388,118389],{"__ignoreMap":96},[100,118390,118391,118393,118395,118397,118399,118401,118403,118405,118407,118409,118411,118413,118415],{"class":102,"line":103},[100,118392,3166],{"class":118},[100,118394,1280],{"class":205},[100,118396,4148],{"class":209},[100,118398,1280],{"class":205},[100,118400,126],{"class":118},[100,118402,1274],{"class":205},[100,118404,4157],{"class":209},[100,118406,1280],{"class":205},[100,118408,126],{"class":118},[100,118410,1274],{"class":205},[100,118412,8949],{"class":209},[100,118414,1280],{"class":205},[100,118416,603],{"class":118},[675,118418,118420],{"id":118419},"fix-by-using-enough-variables","Fix by using enough variables",[91,118422,118424],{"className":93,"code":118423,"language":95,"meta":96,"style":96},"text = \"apple,banana,cherry\"\nfirst, second, third = text.split(\",\")\n\nprint(first)\nprint(second)\nprint(third)\n",[17,118425,118426,118438,118470,118474,118484,118495],{"__ignoreMap":96},[100,118427,118428,118430,118432,118434,118436],{"class":102,"line":103},[100,118429,4331],{"class":106},[100,118431,111],{"class":110},[100,118433,1708],{"class":205},[100,118435,118349],{"class":209},[100,118437,1714],{"class":205},[100,118439,118440,118442,118444,118447,118449,118452,118454,118456,118458,118460,118462,118464,118466,118468],{"class":102,"line":135},[100,118441,113780],{"class":106},[100,118443,126],{"class":118},[100,118445,118446],{"class":106}," second",[100,118448,126],{"class":118},[100,118450,118451],{"class":106}," third ",[100,118453,111],{"class":110},[100,118455,4350],{"class":106},[100,118457,290],{"class":118},[100,118459,3922],{"class":178},[100,118461,170],{"class":118},[100,118463,206],{"class":205},[100,118465,126],{"class":209},[100,118467,206],{"class":205},[100,118469,215],{"class":118},[100,118471,118472],{"class":102,"line":142},[100,118473,139],{"emptyLinePlaceholder":138},[100,118475,118476,118478,118480,118482],{"class":102,"line":152},[100,118477,372],{"class":114},[100,118479,170],{"class":118},[100,118481,113780],{"class":178},[100,118483,215],{"class":118},[100,118485,118486,118488,118490,118493],{"class":102,"line":164},[100,118487,372],{"class":114},[100,118489,170],{"class":118},[100,118491,118492],{"class":178},"second",[100,118494,215],{"class":118},[100,118496,118497,118499,118501,118504],{"class":102,"line":185},[100,118498,372],{"class":114},[100,118500,170],{"class":118},[100,118502,118503],{"class":178},"third",[100,118505,215],{"class":118},[675,118507,118509],{"id":118508},"fix-by-limiting-the-split","Fix by limiting the split",[14,118511,118512,118513,89],{},"If you only want 2 parts, use ",[17,118514,118515],{},"maxsplit",[91,118517,118519],{"className":93,"code":118518,"language":95,"meta":96,"style":96},"text = \"apple,banana,cherry\"\nfirst, rest = text.split(\",\", maxsplit=1)\n\nprint(first)\nprint(rest)\n",[17,118520,118521,118533,118569,118573,118583],{"__ignoreMap":96},[100,118522,118523,118525,118527,118529,118531],{"class":102,"line":103},[100,118524,4331],{"class":106},[100,118526,111],{"class":110},[100,118528,1708],{"class":205},[100,118530,118349],{"class":209},[100,118532,1714],{"class":205},[100,118534,118535,118537,118539,118542,118544,118546,118548,118550,118552,118554,118556,118558,118560,118563,118565,118567],{"class":102,"line":135},[100,118536,113780],{"class":106},[100,118538,126],{"class":118},[100,118540,118541],{"class":106}," rest ",[100,118543,111],{"class":110},[100,118545,4350],{"class":106},[100,118547,290],{"class":118},[100,118549,3922],{"class":178},[100,118551,170],{"class":118},[100,118553,206],{"class":205},[100,118555,126],{"class":209},[100,118557,206],{"class":205},[100,118559,126],{"class":118},[100,118561,118562],{"class":18141}," maxsplit",[100,118564,111],{"class":110},[100,118566,123],{"class":122},[100,118568,215],{"class":118},[100,118570,118571],{"class":102,"line":142},[100,118572,139],{"emptyLinePlaceholder":138},[100,118574,118575,118577,118579,118581],{"class":102,"line":152},[100,118576,372],{"class":114},[100,118578,170],{"class":118},[100,118580,113780],{"class":178},[100,118582,215],{"class":118},[100,118584,118585,118587,118589,118591],{"class":102,"line":164},[100,118586,372],{"class":114},[100,118588,170],{"class":118},[100,118590,114464],{"class":178},[100,118592,215],{"class":118},[14,118594,218],{},[91,118596,118598],{"className":93,"code":118597,"language":95,"meta":96,"style":96},"apple\nbanana,cherry\n",[17,118599,118600,118605],{"__ignoreMap":96},[100,118601,118602],{"class":102,"line":103},[100,118603,118604],{"class":106},"apple\n",[100,118606,118607,118609,118611],{"class":102,"line":135},[100,118608,4157],{"class":106},[100,118610,126],{"class":118},[100,118612,118613],{"class":106},"cherry\n",[14,118615,118616,118617,290],{},"If you want more detail on how this method works, see the ",[295,118618,1572,118619,4112],{"href":108048},[17,118620,115252],{},[77,118622,3274],{"id":3273},[14,118624,118625],{},"When you see this error, the fastest way to fix it is to inspect the value before unpacking.",[14,118627,26604],{},[91,118629,118631],{"className":93,"code":118630,"language":95,"meta":96,"style":96},"print(value)\nprint(type(value))\nprint(len(value))\n",[17,118632,118633,118643,118657],{"__ignoreMap":96},[100,118634,118635,118637,118639,118641],{"class":102,"line":103},[100,118636,372],{"class":114},[100,118638,170],{"class":118},[100,118640,757],{"class":178},[100,118642,215],{"class":118},[100,118644,118645,118647,118649,118651,118653,118655],{"class":102,"line":135},[100,118646,372],{"class":114},[100,118648,170],{"class":118},[100,118650,1250],{"class":191},[100,118652,170],{"class":118},[100,118654,757],{"class":178},[100,118656,182],{"class":118},[100,118658,118659,118661,118663,118665,118667,118669],{"class":102,"line":142},[100,118660,372],{"class":114},[100,118662,170],{"class":118},[100,118664,17779],{"class":114},[100,118666,170],{"class":118},[100,118668,757],{"class":178},[100,118670,182],{"class":118},[14,118672,118673],{},"If the problem happens in a loop:",[91,118675,118677],{"className":93,"code":118676,"language":95,"meta":96,"style":96},"for item in data:\n    print(item, len(item))\n",[17,118678,118679,118691],{"__ignoreMap":96},[100,118680,118681,118683,118685,118687,118689],{"class":102,"line":103},[100,118682,71],{"class":145},[100,118684,7568],{"class":106},[100,118686,617],{"class":145},[100,118688,12102],{"class":106},[100,118690,149],{"class":118},[100,118692,118693,118695,118697,118699,118701,118703,118705,118707],{"class":102,"line":135},[100,118694,200],{"class":114},[100,118696,170],{"class":118},[100,118698,7555],{"class":178},[100,118700,126],{"class":118},[100,118702,3141],{"class":114},[100,118704,170],{"class":118},[100,118706,7555],{"class":178},[100,118708,182],{"class":118},[14,118710,118711],{},"If the problem involves string splitting:",[91,118713,118715],{"className":93,"code":118714,"language":95,"meta":96,"style":96},"print(text.split(','))\n",[17,118716,118717],{"__ignoreMap":96},[100,118718,118719,118721,118723,118725,118727,118729,118731,118733,118735,118737],{"class":102,"line":103},[100,118720,372],{"class":114},[100,118722,170],{"class":118},[100,118724,4468],{"class":178},[100,118726,290],{"class":118},[100,118728,3922],{"class":178},[100,118730,170],{"class":118},[100,118732,1280],{"class":205},[100,118734,126],{"class":209},[100,118736,1280],{"class":205},[100,118738,182],{"class":118},[675,118740,118742],{"id":118741},"what-to-look-for","What to look for",[40,118744,118745,118748,118753,118756],{},[43,118746,118747],{},"Print the value before unpacking it.",[43,118749,241,118750,118752],{},[17,118751,32803],{}," when the value is a list, tuple, or other sequence.",[43,118754,118755],{},"Check what each loop item looks like.",[43,118757,118758],{},"Read the full traceback and find the exact line where unpacking happens.",[14,118760,580],{},[91,118762,118764],{"className":93,"code":118763,"language":95,"meta":96,"style":96},"data = [(\"a\", 1), (\"b\", 2, 3)]\n\nfor item in data:\n    print(item, len(item))\n",[17,118765,118766,118804,118808,118820],{"__ignoreMap":96},[100,118767,118768,118770,118772,118774,118776,118778,118780,118782,118784,118786,118788,118790,118792,118794,118796,118798,118800,118802],{"class":102,"line":103},[100,118769,7909],{"class":106},[100,118771,111],{"class":110},[100,118773,78799],{"class":118},[100,118775,206],{"class":205},[100,118777,295],{"class":209},[100,118779,206],{"class":205},[100,118781,126],{"class":118},[100,118783,6915],{"class":122},[100,118785,2874],{"class":118},[100,118787,16805],{"class":118},[100,118789,206],{"class":205},[100,118791,4027],{"class":209},[100,118793,206],{"class":205},[100,118795,126],{"class":118},[100,118797,129],{"class":122},[100,118799,126],{"class":118},[100,118801,1405],{"class":122},[100,118803,78829],{"class":118},[100,118805,118806],{"class":102,"line":135},[100,118807,139],{"emptyLinePlaceholder":138},[100,118809,118810,118812,118814,118816,118818],{"class":102,"line":142},[100,118811,71],{"class":145},[100,118813,7568],{"class":106},[100,118815,617],{"class":145},[100,118817,12102],{"class":106},[100,118819,149],{"class":118},[100,118821,118822,118824,118826,118828,118830,118832,118834,118836],{"class":102,"line":152},[100,118823,200],{"class":114},[100,118825,170],{"class":118},[100,118827,7555],{"class":178},[100,118829,126],{"class":118},[100,118831,3141],{"class":114},[100,118833,170],{"class":118},[100,118835,7555],{"class":178},[100,118837,182],{"class":118},[14,118839,218],{},[91,118841,118843],{"className":93,"code":118842,"language":95,"meta":96,"style":96},"('a', 1) 2\n('b', 2, 3) 3\n",[17,118844,118845,118863],{"__ignoreMap":96},[100,118846,118847,118849,118851,118853,118855,118857,118859,118861],{"class":102,"line":103},[100,118848,170],{"class":118},[100,118850,1280],{"class":205},[100,118852,295],{"class":209},[100,118854,1280],{"class":205},[100,118856,126],{"class":118},[100,118858,6915],{"class":122},[100,118860,6155],{"class":118},[100,118862,3564],{"class":122},[100,118864,118865,118867,118869,118871,118873,118875,118877,118879,118881,118883],{"class":102,"line":135},[100,118866,170],{"class":118},[100,118868,1280],{"class":205},[100,118870,4027],{"class":209},[100,118872,1280],{"class":205},[100,118874,126],{"class":118},[100,118876,129],{"class":122},[100,118878,126],{"class":118},[100,118880,1405],{"class":122},[100,118882,6155],{"class":118},[100,118884,31170],{"class":122},[14,118886,118887],{},"Now the problem is clear: not every item has the same number of values.",[77,118889,118891],{"id":118890},"related-errors-and-similar-cases","Related errors and similar cases",[14,118893,118894],{},"Do not confuse this error with these similar ones:",[40,118896,118897,118906],{},[43,118898,118899,118903,118905],{},[295,118900,118901],{"href":115867},[17,118902,79323],{},[9927,118904],{},"\nThis is the opposite problem. You have fewer values than variables.",[43,118907,118908,118913,118915],{},[295,118909,118910],{"href":79501},[17,118911,118912],{},"TypeError: cannot unpack non-iterable object",[9927,118914],{},"\nThis happens when the value cannot be unpacked at all.",[77,118917,1344],{"id":1343},[14,118919,118920],{},"Here are the most common situations that trigger this error:",[40,118922,118923,118926,118932,118935,118938],{},[43,118924,118925],{},"Unpacking a tuple or list into too few variables",[43,118927,1357,118928,118931],{},[17,118929,118930],{},"for a, b in ..."," when items contain 3 or more values",[43,118933,118934],{},"Splitting a string into more parts than expected",[43,118936,118937],{},"Expecting a function to return 2 values when it returns 3",[43,118939,118940],{},"Unpacking dictionary iteration incorrectly",[77,118942,1514],{"id":1513},[675,118944,118946],{"id":118945},"what-does-unpack-mean-in-python","What does \"unpack\" mean in Python?",[14,118948,118949],{},"It means assigning items from a sequence like a list or tuple into separate variables.",[675,118951,118953,118954,118956],{"id":118952},"why-do-i-get-this-error-in-a-for-loop","Why do I get this error in a ",[17,118955,71],{}," loop?",[14,118958,118959],{},"Each item in the loop likely has more values than the variables in the loop header.",[675,118961,118963],{"id":118962},"how-do-i-ignore-extra-values","How do I ignore extra values?",[14,118965,118966,118967,290],{},"Use starred unpacking such as ",[17,118968,118969],{},"first, *rest = values",[675,118971,118973],{"id":118972},"is-this-the-same-as-not-enough-values-to-unpack","Is this the same as \"not enough values to unpack\"?",[14,118975,118976],{},"No. That error means you have fewer values than variables. This one means you have more values than variables.",[77,118978,1554],{"id":1553},[40,118980,118981,118985,118989,118995,118999],{},[43,118982,118983],{},[295,118984,79323],{"href":115867},[43,118986,118987],{},[295,118988,29749],{"href":29189},[43,118990,118991],{},[295,118992,1572,118993,4112],{"href":108048},[17,118994,115252],{},[43,118996,118997],{},[295,118998,79448],{"href":79082},[43,119000,119001],{},[295,119002,118912],{"href":79501},[1589,119004,119005],{},"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 .sP7_E, html code.shiki .sP7_E{--shiki-light:#39ADB5;--shiki-default:#24292E;--shiki-dark:#E1E4E8}html pre.shiki code .srdBf, html code.shiki .srdBf{--shiki-light:#F76D47;--shiki-default:#005CC5;--shiki-dark:#79B8FF}html pre.shiki code .sutJx, html code.shiki .sutJx{--shiki-light:#90A4AE;--shiki-light-font-style:italic;--shiki-default:#6A737D;--shiki-default-font-style:inherit;--shiki-dark:#6A737D;--shiki-dark-font-style:inherit}html pre.shiki code .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 .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 .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 .s99_P, html code.shiki .s99_P{--shiki-light:#90A4AE;--shiki-light-font-style:italic;--shiki-default:#E36209;--shiki-default-font-style:inherit;--shiki-dark:#FFAB70;--shiki-dark-font-style:inherit}",{"title":96,"searchDepth":135,"depth":135,"links":119007},[119008,119009,119010,119011,119012,119016,119017,119020,119025,119028,119029,119030,119037],{"id":79,"depth":135,"text":80},{"id":5880,"depth":135,"text":5881},{"id":91662,"depth":135,"text":91663},{"id":25090,"depth":135,"text":25091},{"id":117583,"depth":135,"text":117584,"children":119013},[119014,119015],{"id":117590,"depth":142,"text":117591},{"id":117690,"depth":142,"text":117691},{"id":117802,"depth":135,"text":117803},{"id":118053,"depth":135,"text":118054,"children":119018},[119019],{"id":118243,"depth":142,"text":118244},{"id":118327,"depth":135,"text":119021,"children":119022},"Fix 4: Check split() results",[119023,119024],{"id":118419,"depth":142,"text":118420},{"id":118508,"depth":142,"text":118509},{"id":3273,"depth":135,"text":3274,"children":119026},[119027],{"id":118741,"depth":142,"text":118742},{"id":118890,"depth":135,"text":118891},{"id":1343,"depth":135,"text":1344},{"id":1513,"depth":135,"text":1514,"children":119031},[119032,119033,119035,119036],{"id":118945,"depth":142,"text":118946},{"id":118952,"depth":142,"text":119034},"Why do I get this error in a for loop?",{"id":118962,"depth":142,"text":118963},{"id":118972,"depth":142,"text":118973},{"id":1553,"depth":135,"text":1554},"Master valueerror too many values to unpack fix in our comprehensive Python beginner guide.",{},"\u002Ferrors\u002Fvalueerror-too-many-values-to-unpack-fix",{"title":79458,"description":119038},"errors\u002Fvalueerror-too-many-values-to-unpack-fix","DgHjjz7ywpagYYKfycxQz0ZbCoCOFftDYOOMTMaIt9Q",{"id":119045,"title":119046,"body":119047,"description":120940,"extension":1623,"meta":120941,"navigation":138,"path":120942,"seo":120943,"stem":120944,"__hash__":120945},"content\u002Ferrors\u002Fvalueerror-when-parsing-input-in-python-fix.md","ValueError when parsing input in Python (Fix)",{"type":7,"value":119048,"toc":120911},[119049,119052,119060,119068,119070,119082,119084,119178,119190,119192,119197,119200,119218,119220,119242,119246,119248,119251,119301,119304,119309,119336,119339,119347,119350,119382,119393,119397,119400,119406,119494,119496,119515,119520,119609,119614,119618,119623,119630,119727,119730,119745,119751,119767,119774,119778,119781,119786,119870,119873,119886,119889,120007,120009,120018,120021,120025,120028,120137,120140,120151,120159,120163,120169,120175,120197,120204,120209,120232,120237,120256,120261,120263,120271,120277,120323,120326,120328,120348,120354,120366,120370,120373,120375,120441,120446,120457,120459,120506,120512,120558,120561,120565,120568,120573,120587,120592,120607,120609,120677,120683,120685,120776,120782,120784,120787,120812,120814,120821,120836,120842,120848,120855,120863,120869,120874,120876,120908],[10,119050,119046],{"id":119051},"valueerror-when-parsing-input-in-python-fix",[14,119053,119054,119056,119057,119059],{},[17,119055,3483],{}," often happens when your program reads text with ",[17,119058,14906],{}," and then tries to convert that text into a number.",[14,119061,119062,119063,3178,119065,119067],{},"This usually affects code that uses ",[17,119064,6119],{},[17,119066,59778],{},". For example, the user might type letters instead of digits, leave the input blank, or enter a decimal when your code expects a whole number.",[14,119069,110518],{},[40,119071,119072,119074,119077,119079],{},[43,119073,85186],{},[43,119075,119076],{},"how to reproduce it",[43,119078,115902],{},[43,119080,119081],{},"how to prevent it in beginner-friendly code",[77,119083,80],{"id":79},[91,119085,119087],{"className":93,"code":119086,"language":95,"meta":96,"style":96},"user_text = input(\"Enter a number: \").strip()\n\ntry:\n    number = int(user_text)\n    print(\"You entered:\", number)\nexcept ValueError:\n    print(\"Please enter a whole number only.\")\n",[17,119088,119089,119112,119116,119122,119137,119155,119163],{"__ignoreMap":96},[100,119090,119091,119094,119096,119098,119100,119102,119104,119106,119108,119110],{"class":102,"line":103},[100,119092,119093],{"class":106},"user_text ",[100,119095,111],{"class":110},[100,119097,7221],{"class":114},[100,119099,170],{"class":118},[100,119101,206],{"class":205},[100,119103,6217],{"class":209},[100,119105,206],{"class":205},[100,119107,5799],{"class":118},[100,119109,6235],{"class":178},[100,119111,4734],{"class":118},[100,119113,119114],{"class":102,"line":135},[100,119115,139],{"emptyLinePlaceholder":138},[100,119117,119118,119120],{"class":102,"line":142},[100,119119,146],{"class":145},[100,119121,149],{"class":118},[100,119123,119124,119126,119128,119130,119132,119135],{"class":102,"line":152},[100,119125,98700],{"class":106},[100,119127,111],{"class":110},[100,119129,3389],{"class":191},[100,119131,170],{"class":118},[100,119133,119134],{"class":178},"user_text",[100,119136,215],{"class":118},[100,119138,119139,119141,119143,119145,119147,119149,119151,119153],{"class":102,"line":164},[100,119140,200],{"class":114},[100,119142,170],{"class":118},[100,119144,206],{"class":205},[100,119146,107320],{"class":209},[100,119148,206],{"class":205},[100,119150,126],{"class":118},[100,119152,84021],{"class":178},[100,119154,215],{"class":118},[100,119156,119157,119159,119161],{"class":102,"line":185},[100,119158,188],{"class":145},[100,119160,2794],{"class":191},[100,119162,149],{"class":118},[100,119164,119165,119167,119169,119171,119174,119176],{"class":102,"line":197},[100,119166,200],{"class":114},[100,119168,170],{"class":118},[100,119170,206],{"class":205},[100,119172,119173],{"class":209},"Please enter a whole number only.",[100,119175,206],{"class":205},[100,119177,215],{"class":118},[14,119179,241,119180,119182,119183,3178,119185,93668,119187,119189],{},[17,119181,244],{}," when converting input with ",[17,119184,6119],{},[17,119186,59778],{},[17,119188,8739],{}," also helps remove accidental spaces.",[77,119191,5881],{"id":5880},[14,119193,119194,119196],{},[17,119195,3483],{}," means Python received a value of the correct general type, but the content is not valid for the operation.",[14,119198,119199],{},"When parsing input, this often happens when you try to:",[40,119201,119202,119207,119212],{},[43,119203,119204,119205],{},"convert text to an integer with ",[17,119206,6119],{},[43,119208,119209,119210],{},"convert text to a decimal number with ",[17,119211,59778],{},[43,119213,119214,119215,119217],{},"unpack values from ",[17,119216,8090],{}," and the number of parts is wrong",[14,119219,13223],{},[40,119221,119222,119226,119231,119236],{},[43,119223,119224],{},[17,119225,99083],{},[43,119227,119228],{},[17,119229,119230],{},"int(\"\")",[43,119232,119233],{},[17,119234,119235],{},"float(\"\")",[43,119237,119238,119241],{},[17,119239,119240],{},"a, b = input().split()"," when the input does not contain exactly two parts",[14,119243,41228,119244,290],{},[295,119245,1587],{"href":1749},[77,119247,24958],{"id":24957},[14,119249,119250],{},"A very common beginner pattern is this:",[91,119252,119254],{"className":93,"code":119253,"language":95,"meta":96,"style":96},"age = int(input(\"Enter your age: \"))\nprint(\"Next year you will be\", age + 1)\n",[17,119255,119256,119278],{"__ignoreMap":96},[100,119257,119258,119260,119262,119264,119266,119268,119270,119272,119274,119276],{"class":102,"line":103},[100,119259,1664],{"class":106},[100,119261,111],{"class":110},[100,119263,3389],{"class":191},[100,119265,170],{"class":118},[100,119267,6210],{"class":114},[100,119269,170],{"class":118},[100,119271,206],{"class":205},[100,119273,7173],{"class":209},[100,119275,206],{"class":205},[100,119277,182],{"class":118},[100,119279,119280,119282,119284,119286,119289,119291,119293,119295,119297,119299],{"class":102,"line":135},[100,119281,372],{"class":114},[100,119283,170],{"class":118},[100,119285,206],{"class":205},[100,119287,119288],{"class":209},"Next year you will be",[100,119290,206],{"class":205},[100,119292,126],{"class":118},[100,119294,1676],{"class":178},[100,119296,6547],{"class":110},[100,119298,6915],{"class":122},[100,119300,215],{"class":118},[14,119302,119303],{},"This works only if the user enters a valid whole number.",[14,119305,14954,119306,119308],{},[17,119307,3506],{},", Python raises an error:",[91,119310,119312],{"className":93,"code":119311,"language":95,"meta":96,"style":96},"age = int(input(\"Enter your age: \"))\n",[17,119313,119314],{"__ignoreMap":96},[100,119315,119316,119318,119320,119322,119324,119326,119328,119330,119332,119334],{"class":102,"line":103},[100,119317,1664],{"class":106},[100,119319,111],{"class":110},[100,119321,3389],{"class":191},[100,119323,170],{"class":118},[100,119325,6210],{"class":114},[100,119327,170],{"class":118},[100,119329,206],{"class":205},[100,119331,7173],{"class":209},[100,119333,206],{"class":205},[100,119335,182],{"class":118},[14,119337,119338],{},"Example input:",[91,119340,119341],{"className":93,"code":6651,"language":95,"meta":96,"style":96},[17,119342,119343],{"__ignoreMap":96},[100,119344,119345],{"class":102,"line":103},[100,119346,6651],{"class":106},[14,119348,119349],{},"Typical error message:",[91,119351,119352],{"className":93,"code":16972,"language":95,"meta":96,"style":96},[17,119353,119354],{"__ignoreMap":96},[100,119355,119356,119358,119360,119362,119364,119366,119368,119370,119372,119374,119376,119378,119380],{"class":102,"line":103},[100,119357,3483],{"class":191},[100,119359,89],{"class":118},[100,119361,16983],{"class":106},[100,119363,71],{"class":145},[100,119365,3389],{"class":191},[100,119367,7562],{"class":118},[100,119369,16992],{"class":145},[100,119371,16995],{"class":106},[100,119373,356],{"class":122},[100,119375,89],{"class":118},[100,119377,1274],{"class":205},[100,119379,3506],{"class":209},[100,119381,3925],{"class":205},[14,119383,119384,119385,119387,119388,3266,119391,290],{},"That happens because ",[17,119386,14906],{}," always returns a string. If you want to learn more about that, see ",[295,119389,119390],{"href":85586},"Python input() function explained",[295,119392,81195],{"href":81194},[77,119394,119396],{"id":119395},"fix-1-use-try-except-around-the-conversion","Fix 1: Use try-except around the conversion",[14,119398,119399],{},"This is the best fix when input comes from a real user.",[14,119401,119402,119403,119405],{},"Wrap the conversion in a ",[17,119404,244],{}," block so your program does not crash.",[91,119407,119409],{"className":93,"code":119408,"language":95,"meta":96,"style":96},"user_text = input(\"Enter a whole number: \")\n\ntry:\n    number = int(user_text)\n    print(\"Valid number:\", number)\nexcept ValueError:\n    print(\"That was not a valid whole number.\")\n",[17,119410,119411,119429,119433,119439,119453,119471,119479],{"__ignoreMap":96},[100,119412,119413,119415,119417,119419,119421,119423,119425,119427],{"class":102,"line":103},[100,119414,119093],{"class":106},[100,119416,111],{"class":110},[100,119418,7221],{"class":114},[100,119420,170],{"class":118},[100,119422,206],{"class":205},[100,119424,110254],{"class":209},[100,119426,206],{"class":205},[100,119428,215],{"class":118},[100,119430,119431],{"class":102,"line":135},[100,119432,139],{"emptyLinePlaceholder":138},[100,119434,119435,119437],{"class":102,"line":142},[100,119436,146],{"class":145},[100,119438,149],{"class":118},[100,119440,119441,119443,119445,119447,119449,119451],{"class":102,"line":152},[100,119442,98700],{"class":106},[100,119444,111],{"class":110},[100,119446,3389],{"class":191},[100,119448,170],{"class":118},[100,119450,119134],{"class":178},[100,119452,215],{"class":118},[100,119454,119455,119457,119459,119461,119463,119465,119467,119469],{"class":102,"line":164},[100,119456,200],{"class":114},[100,119458,170],{"class":118},[100,119460,206],{"class":205},[100,119462,111972],{"class":209},[100,119464,206],{"class":205},[100,119466,126],{"class":118},[100,119468,84021],{"class":178},[100,119470,215],{"class":118},[100,119472,119473,119475,119477],{"class":102,"line":185},[100,119474,188],{"class":145},[100,119476,2794],{"class":191},[100,119478,149],{"class":118},[100,119480,119481,119483,119485,119487,119490,119492],{"class":102,"line":197},[100,119482,200],{"class":114},[100,119484,170],{"class":118},[100,119486,206],{"class":205},[100,119488,119489],{"class":209},"That was not a valid whole number.",[100,119491,206],{"class":205},[100,119493,215],{"class":118},[14,119495,26197],{},[40,119497,119498,119503,119512],{},[43,119499,119500,119502],{},[17,119501,73351],{}," runs the risky code",[43,119504,119505,119506,119509,119510],{},"if ",[17,119507,119508],{},"int(user_text)"," fails, Python jumps to ",[17,119511,73356],{},[43,119513,119514],{},"your program prints a helpful message instead of stopping",[14,119516,119517,119518,89],{},"You can do the same with ",[17,119519,59778],{},[91,119521,119523],{"className":93,"code":119522,"language":95,"meta":96,"style":96},"user_text = input(\"Enter a decimal number: \")\n\ntry:\n    number = float(user_text)\n    print(\"Valid number:\", number)\nexcept ValueError:\n    print(\"Please enter a valid number.\")\n",[17,119524,119525,119544,119548,119554,119568,119586,119594],{"__ignoreMap":96},[100,119526,119527,119529,119531,119533,119535,119537,119540,119542],{"class":102,"line":103},[100,119528,119093],{"class":106},[100,119530,111],{"class":110},[100,119532,7221],{"class":114},[100,119534,170],{"class":118},[100,119536,206],{"class":205},[100,119538,119539],{"class":209},"Enter a decimal number: ",[100,119541,206],{"class":205},[100,119543,215],{"class":118},[100,119545,119546],{"class":102,"line":135},[100,119547,139],{"emptyLinePlaceholder":138},[100,119549,119550,119552],{"class":102,"line":142},[100,119551,146],{"class":145},[100,119553,149],{"class":118},[100,119555,119556,119558,119560,119562,119564,119566],{"class":102,"line":152},[100,119557,98700],{"class":106},[100,119559,111],{"class":110},[100,119561,106783],{"class":191},[100,119563,170],{"class":118},[100,119565,119134],{"class":178},[100,119567,215],{"class":118},[100,119569,119570,119572,119574,119576,119578,119580,119582,119584],{"class":102,"line":164},[100,119571,200],{"class":114},[100,119573,170],{"class":118},[100,119575,206],{"class":205},[100,119577,111972],{"class":209},[100,119579,206],{"class":205},[100,119581,126],{"class":118},[100,119583,84021],{"class":178},[100,119585,215],{"class":118},[100,119587,119588,119590,119592],{"class":102,"line":185},[100,119589,188],{"class":145},[100,119591,2794],{"class":191},[100,119593,149],{"class":118},[100,119595,119596,119598,119600,119602,119605,119607],{"class":102,"line":197},[100,119597,200],{"class":114},[100,119599,170],{"class":118},[100,119601,206],{"class":205},[100,119603,119604],{"class":209},"Please enter a valid number.",[100,119606,206],{"class":205},[100,119608,215],{"class":118},[14,119610,119611,119612,290],{},"If you want more practice with this pattern, see ",[295,119613,66365],{"href":66364},[77,119615,119617],{"id":119616},"fix-2-validate-input-before-converting","Fix 2: Validate input before converting",[14,119619,119620,119621,290],{},"Sometimes you want to reject bad input before calling ",[17,119622,6119],{},[14,119624,119625,119626,119629],{},"For simple positive integers, ",[17,119627,119628],{},"isdigit()"," can help:",[91,119631,119633],{"className":93,"code":119632,"language":95,"meta":96,"style":96},"user_text = input(\"Enter a whole number: \").strip()\n\nif user_text.isdigit():\n    number = int(user_text)\n    print(\"Valid number:\", number)\nelse:\n    print(\"Please enter digits only.\")\n",[17,119634,119635,119657,119661,119674,119688,119706,119712],{"__ignoreMap":96},[100,119636,119637,119639,119641,119643,119645,119647,119649,119651,119653,119655],{"class":102,"line":103},[100,119638,119093],{"class":106},[100,119640,111],{"class":110},[100,119642,7221],{"class":114},[100,119644,170],{"class":118},[100,119646,206],{"class":205},[100,119648,110254],{"class":209},[100,119650,206],{"class":205},[100,119652,5799],{"class":118},[100,119654,6235],{"class":178},[100,119656,4734],{"class":118},[100,119658,119659],{"class":102,"line":135},[100,119660,139],{"emptyLinePlaceholder":138},[100,119662,119663,119665,119668,119670,119672],{"class":102,"line":142},[100,119664,2736],{"class":145},[100,119666,119667],{"class":106}," user_text",[100,119669,290],{"class":118},[100,119671,3367],{"class":178},[100,119673,1085],{"class":118},[100,119675,119676,119678,119680,119682,119684,119686],{"class":102,"line":152},[100,119677,98700],{"class":106},[100,119679,111],{"class":110},[100,119681,3389],{"class":191},[100,119683,170],{"class":118},[100,119685,119134],{"class":178},[100,119687,215],{"class":118},[100,119689,119690,119692,119694,119696,119698,119700,119702,119704],{"class":102,"line":164},[100,119691,200],{"class":114},[100,119693,170],{"class":118},[100,119695,206],{"class":205},[100,119697,111972],{"class":209},[100,119699,206],{"class":205},[100,119701,126],{"class":118},[100,119703,84021],{"class":178},[100,119705,215],{"class":118},[100,119707,119708,119710],{"class":102,"line":185},[100,119709,4944],{"class":145},[100,119711,149],{"class":118},[100,119713,119714,119716,119718,119720,119723,119725],{"class":102,"line":197},[100,119715,200],{"class":114},[100,119717,170],{"class":118},[100,119719,206],{"class":205},[100,119721,119722],{"class":209},"Please enter digits only.",[100,119724,206],{"class":205},[100,119726,215],{"class":118},[14,119728,119729],{},"This works for values like:",[40,119731,119732,119736,119741],{},[43,119733,119734],{},[17,119735,96033],{},[43,119737,119738],{},[17,119739,119740],{},"\"42\"",[43,119742,119743],{},[17,119744,84581],{},[14,119746,119747,119748,119750],{},"But it does ",[22,119749,1059],{}," work for:",[40,119752,119753,119758,119763],{},[43,119754,110655,119755],{},[17,119756,119757],{},"\"-5\"",[43,119759,119760,119761],{},"decimals like ",[17,119762,110670],{},[43,119764,112092,119765],{},[17,119766,11875],{},[14,119768,35458,119769,119771,119772,290],{},[17,119770,119628],{}," is useful for simple checks, but it is not a complete replacement for ",[17,119773,244],{},[77,119775,119777],{"id":119776},"fix-3-clean-the-input-first","Fix 3: Clean the input first",[14,119779,119780],{},"Sometimes the text is almost correct, but it contains spaces or formatting that causes problems.",[14,119782,241,119783,119785],{},[17,119784,8739],{}," to remove spaces at the start and end:",[91,119787,119789],{"className":93,"code":119788,"language":95,"meta":96,"style":96},"user_text = input(\"Enter a number: \").strip()\n\ntry:\n    number = int(user_text)\n    print(number)\nexcept ValueError:\n    print(\"Invalid input\")\n",[17,119790,119791,119813,119817,119823,119837,119847,119855],{"__ignoreMap":96},[100,119792,119793,119795,119797,119799,119801,119803,119805,119807,119809,119811],{"class":102,"line":103},[100,119794,119093],{"class":106},[100,119796,111],{"class":110},[100,119798,7221],{"class":114},[100,119800,170],{"class":118},[100,119802,206],{"class":205},[100,119804,6217],{"class":209},[100,119806,206],{"class":205},[100,119808,5799],{"class":118},[100,119810,6235],{"class":178},[100,119812,4734],{"class":118},[100,119814,119815],{"class":102,"line":135},[100,119816,139],{"emptyLinePlaceholder":138},[100,119818,119819,119821],{"class":102,"line":142},[100,119820,146],{"class":145},[100,119822,149],{"class":118},[100,119824,119825,119827,119829,119831,119833,119835],{"class":102,"line":152},[100,119826,98700],{"class":106},[100,119828,111],{"class":110},[100,119830,3389],{"class":191},[100,119832,170],{"class":118},[100,119834,119134],{"class":178},[100,119836,215],{"class":118},[100,119838,119839,119841,119843,119845],{"class":102,"line":164},[100,119840,200],{"class":114},[100,119842,170],{"class":118},[100,119844,631],{"class":178},[100,119846,215],{"class":118},[100,119848,119849,119851,119853],{"class":102,"line":185},[100,119850,188],{"class":145},[100,119852,2794],{"class":191},[100,119854,149],{"class":118},[100,119856,119857,119859,119861,119863,119866,119868],{"class":102,"line":197},[100,119858,200],{"class":114},[100,119860,170],{"class":118},[100,119862,206],{"class":205},[100,119864,119865],{"class":209},"Invalid input",[100,119867,206],{"class":205},[100,119869,215],{"class":118},[14,119871,119872],{},"This helps with input like:",[91,119874,119876],{"className":93,"code":119875,"language":95,"meta":96,"style":96},"\"  25  \"\n",[17,119877,119878],{"__ignoreMap":96},[100,119879,119880,119882,119884],{"class":102,"line":103},[100,119881,206],{"class":3553},[100,119883,111200],{"class":3556},[100,119885,1714],{"class":3553},[14,119887,119888],{},"You can also clean other formatting if needed. For example, some users may paste numbers with commas:",[91,119890,119892],{"className":93,"code":119891,"language":95,"meta":96,"style":96},"user_text = input(\"Enter a number: \").strip()\nuser_text = user_text.replace(\",\", \"\")\n\ntry:\n    number = int(user_text)\n    print(\"Parsed number:\", number)\nexcept ValueError:\n    print(\"Please enter a valid whole number.\")\n",[17,119893,119894,119916,119942,119946,119952,119966,119985,119993],{"__ignoreMap":96},[100,119895,119896,119898,119900,119902,119904,119906,119908,119910,119912,119914],{"class":102,"line":103},[100,119897,119093],{"class":106},[100,119899,111],{"class":110},[100,119901,7221],{"class":114},[100,119903,170],{"class":118},[100,119905,206],{"class":205},[100,119907,6217],{"class":209},[100,119909,206],{"class":205},[100,119911,5799],{"class":118},[100,119913,6235],{"class":178},[100,119915,4734],{"class":118},[100,119917,119918,119920,119922,119924,119926,119928,119930,119932,119934,119936,119938,119940],{"class":102,"line":135},[100,119919,119093],{"class":106},[100,119921,111],{"class":110},[100,119923,119667],{"class":106},[100,119925,290],{"class":118},[100,119927,104438],{"class":178},[100,119929,170],{"class":118},[100,119931,206],{"class":205},[100,119933,126],{"class":209},[100,119935,206],{"class":205},[100,119937,126],{"class":118},[100,119939,11901],{"class":205},[100,119941,215],{"class":118},[100,119943,119944],{"class":102,"line":142},[100,119945,139],{"emptyLinePlaceholder":138},[100,119947,119948,119950],{"class":102,"line":152},[100,119949,146],{"class":145},[100,119951,149],{"class":118},[100,119953,119954,119956,119958,119960,119962,119964],{"class":102,"line":164},[100,119955,98700],{"class":106},[100,119957,111],{"class":110},[100,119959,3389],{"class":191},[100,119961,170],{"class":118},[100,119963,119134],{"class":178},[100,119965,215],{"class":118},[100,119967,119968,119970,119972,119974,119977,119979,119981,119983],{"class":102,"line":185},[100,119969,200],{"class":114},[100,119971,170],{"class":118},[100,119973,206],{"class":205},[100,119975,119976],{"class":209},"Parsed number:",[100,119978,206],{"class":205},[100,119980,126],{"class":118},[100,119982,84021],{"class":178},[100,119984,215],{"class":118},[100,119986,119987,119989,119991],{"class":102,"line":197},[100,119988,188],{"class":145},[100,119990,2794],{"class":191},[100,119992,149],{"class":118},[100,119994,119995,119997,119999,120001,120003,120005],{"class":102,"line":771},[100,119996,200],{"class":114},[100,119998,170],{"class":118},[100,120000,206],{"class":205},[100,120002,110637],{"class":209},[100,120004,206],{"class":205},[100,120006,215],{"class":118},[14,120008,1844],{},[40,120010,120011],{},[43,120012,120013,34119,120015],{},[17,120014,112089],{},[17,120016,120017],{},"\"1000\"",[14,120019,120020],{},"Be careful with replacements. Only clean the formats you actually expect.",[77,120022,120024],{"id":120023},"fix-4-ask-again-until-the-input-is-valid","Fix 4: Ask again until the input is valid",[14,120026,120027],{},"For many beginner programs, the best user experience is to keep asking until the user enters valid input.",[91,120029,120031],{"className":93,"code":120030,"language":95,"meta":96,"style":96},"while True:\n    user_text = input(\"Enter a whole number: \").strip()\n\n    try:\n        number = int(user_text)\n        break\n    except ValueError:\n        print(\"That was not a valid whole number. Try again.\")\n\nprint(\"You entered:\", number)\n",[17,120032,120033,120041,120064,120068,120074,120088,120092,120100,120115,120119],{"__ignoreMap":96},[100,120034,120035,120037,120039],{"class":102,"line":103},[100,120036,721],{"class":145},[100,120038,159],{"class":158},[100,120040,149],{"class":118},[100,120042,120043,120046,120048,120050,120052,120054,120056,120058,120060,120062],{"class":102,"line":135},[100,120044,120045],{"class":106},"    user_text ",[100,120047,111],{"class":110},[100,120049,7221],{"class":114},[100,120051,170],{"class":118},[100,120053,206],{"class":205},[100,120055,110254],{"class":209},[100,120057,206],{"class":205},[100,120059,5799],{"class":118},[100,120061,6235],{"class":178},[100,120063,4734],{"class":118},[100,120065,120066],{"class":102,"line":142},[100,120067,139],{"emptyLinePlaceholder":138},[100,120069,120070,120072],{"class":102,"line":152},[100,120071,730],{"class":145},[100,120073,149],{"class":118},[100,120075,120076,120078,120080,120082,120084,120086],{"class":102,"line":164},[100,120077,101750],{"class":106},[100,120079,111],{"class":110},[100,120081,3389],{"class":191},[100,120083,170],{"class":118},[100,120085,119134],{"class":178},[100,120087,215],{"class":118},[100,120089,120090],{"class":102,"line":185},[100,120091,790],{"class":145},[100,120093,120094,120096,120098],{"class":102,"line":197},[100,120095,764],{"class":145},[100,120097,2794],{"class":191},[100,120099,149],{"class":118},[100,120101,120102,120104,120106,120108,120111,120113],{"class":102,"line":771},[100,120103,167],{"class":114},[100,120105,170],{"class":118},[100,120107,206],{"class":205},[100,120109,120110],{"class":209},"That was not a valid whole number. Try again.",[100,120112,206],{"class":205},[100,120114,215],{"class":118},[100,120116,120117],{"class":102,"line":787},[100,120118,139],{"emptyLinePlaceholder":138},[100,120120,120121,120123,120125,120127,120129,120131,120133,120135],{"class":102,"line":5816},[100,120122,372],{"class":114},[100,120124,170],{"class":118},[100,120126,206],{"class":205},[100,120128,107320],{"class":209},[100,120130,206],{"class":205},[100,120132,126],{"class":118},[100,120134,84021],{"class":178},[100,120136,215],{"class":118},[14,120138,120139],{},"Why this is useful:",[40,120141,120142,120145,120148],{},[43,120143,120144],{},"the program does not crash",[43,120146,120147],{},"the user gets another chance",[43,120149,120150],{},"your code stays simple and focused",[14,120152,120153,120154,120156,120157,290],{},"You can use the same pattern with ",[17,120155,59778],{}," if you expect decimal input. For a full walkthrough, see ",[295,120158,85785],{"href":85784},[77,120160,120162],{"id":120161},"special-cases-beginners-hit","Special cases beginners hit",[14,120164,120165,120166,120168],{},"Some ",[17,120167,3483],{}," cases are confusing at first. Here are a few common ones.",[675,120170,120172],{"id":120171},"int35",[17,120173,120174],{},"int('3.5')",[91,120176,120177],{"className":93,"code":99445,"language":95,"meta":96,"style":96},[17,120178,120179],{"__ignoreMap":96},[100,120180,120181,120183,120185,120187,120189,120191,120193,120195],{"class":102,"line":103},[100,120182,372],{"class":114},[100,120184,170],{"class":118},[100,120186,3499],{"class":191},[100,120188,170],{"class":118},[100,120190,206],{"class":205},[100,120192,80892],{"class":209},[100,120194,206],{"class":205},[100,120196,182],{"class":118},[14,120198,3513,120199,3516,120201,120203],{},[17,120200,3483],{},[17,120202,96443],{}," is a decimal string, not a whole number string.",[14,120205,120206,120207,79831],{},"If you expect decimal input, use ",[17,120208,59778],{},[91,120210,120212],{"className":93,"code":120211,"language":95,"meta":96,"style":96},"print(float(\"3.5\"))\n",[17,120213,120214],{"__ignoreMap":96},[100,120215,120216,120218,120220,120222,120224,120226,120228,120230],{"class":102,"line":103},[100,120217,372],{"class":114},[100,120219,170],{"class":118},[100,120221,60476],{"class":191},[100,120223,170],{"class":118},[100,120225,206],{"class":205},[100,120227,80892],{"class":209},[100,120229,206],{"class":205},[100,120231,182],{"class":118},[675,120233,120234],{"id":60476},[17,120235,120236],{},"float('')",[91,120238,120240],{"className":93,"code":120239,"language":95,"meta":96,"style":96},"print(float(\"\"))\n",[17,120241,120242],{"__ignoreMap":96},[100,120243,120244,120246,120248,120250,120252,120254],{"class":102,"line":103},[100,120245,372],{"class":114},[100,120247,170],{"class":118},[100,120249,60476],{"class":191},[100,120251,170],{"class":118},[100,120253,11875],{"class":205},[100,120255,182],{"class":118},[14,120257,3513,120258,120260],{},[17,120259,3483],{}," because an empty string is not a number.",[14,120262,26590],{},[40,120264,120265,120268],{},[43,120266,120267],{},"the user just presses Enter",[43,120269,120270],{},"you forgot to check for empty input",[675,120272,120274,120275],{"id":120273},"unpacking-from-split","Unpacking from ",[17,120276,8090],{},[91,120278,120280],{"className":93,"code":120279,"language":95,"meta":96,"style":96},"a, b = input(\"Enter two values: \").split()\nprint(a, b)\n",[17,120281,120282,120309],{"__ignoreMap":96},[100,120283,120284,120286,120288,120290,120292,120294,120296,120298,120301,120303,120305,120307],{"class":102,"line":103},[100,120285,295],{"class":106},[100,120287,126],{"class":118},[100,120289,25360],{"class":106},[100,120291,111],{"class":110},[100,120293,7221],{"class":114},[100,120295,170],{"class":118},[100,120297,206],{"class":205},[100,120299,120300],{"class":209},"Enter two values: ",[100,120302,206],{"class":205},[100,120304,5799],{"class":118},[100,120306,3922],{"class":178},[100,120308,4734],{"class":118},[100,120310,120311,120313,120315,120317,120319,120321],{"class":102,"line":135},[100,120312,372],{"class":114},[100,120314,170],{"class":118},[100,120316,295],{"class":178},[100,120318,126],{"class":118},[100,120320,15869],{"class":178},[100,120322,215],{"class":118},[14,120324,120325],{},"This works only if the input has exactly two parts.",[14,120327,580],{},[40,120329,120330,120337,120342],{},[43,120331,120332,120333,120336],{},"input: ",[17,120334,120335],{},"\"10 20\""," → works",[43,120338,120332,120339,120341],{},[17,120340,110652],{}," → not enough values",[43,120343,120332,120344,120347],{},[17,120345,120346],{},"\"10 20 30\""," → too many values",[14,120349,120350,120351,120353],{},"These cases can raise different ",[17,120352,3483],{}," messages. See:",[40,120355,120356,120361],{},[43,120357,120358],{},[295,120359,120360],{"href":79322},"ValueError: not enough values to unpack (fix)",[43,120362,120363],{},[295,120364,120365],{"href":79328},"ValueError: too many values to unpack (fix)",[77,120367,120369],{"id":120368},"how-to-debug-the-input-value","How to debug the input value",[14,120371,120372],{},"If the error does not make sense, inspect the actual input.",[14,120374,1206],{},[91,120376,120378],{"className":93,"code":120377,"language":95,"meta":96,"style":96},"user_input = input(\"Enter something: \")\n\nprint(user_input)\nprint(repr(user_input))\nprint(type(user_input))\n",[17,120379,120380,120399,120403,120413,120427],{"__ignoreMap":96},[100,120381,120382,120384,120386,120388,120390,120392,120395,120397],{"class":102,"line":103},[100,120383,6201],{"class":106},[100,120385,111],{"class":110},[100,120387,7221],{"class":114},[100,120389,170],{"class":118},[100,120391,206],{"class":205},[100,120393,120394],{"class":209},"Enter something: ",[100,120396,206],{"class":205},[100,120398,215],{"class":118},[100,120400,120401],{"class":102,"line":135},[100,120402,139],{"emptyLinePlaceholder":138},[100,120404,120405,120407,120409,120411],{"class":102,"line":142},[100,120406,372],{"class":114},[100,120408,170],{"class":118},[100,120410,6230],{"class":178},[100,120412,215],{"class":118},[100,120414,120415,120417,120419,120421,120423,120425],{"class":102,"line":152},[100,120416,372],{"class":114},[100,120418,170],{"class":118},[100,120420,12228],{"class":114},[100,120422,170],{"class":118},[100,120424,6230],{"class":178},[100,120426,182],{"class":118},[100,120428,120429,120431,120433,120435,120437,120439],{"class":102,"line":164},[100,120430,372],{"class":114},[100,120432,170],{"class":118},[100,120434,1250],{"class":191},[100,120436,170],{"class":118},[100,120438,6230],{"class":178},[100,120440,182],{"class":118},[14,120442,37364,120443,120445],{},[17,120444,99698],{}," helps:",[40,120447,120448,120451,120454],{},[43,120449,120450],{},"it shows hidden spaces",[43,120452,120453],{},"it makes empty strings easier to see",[43,120455,120456],{},"it can reveal unexpected characters",[14,120458,1844],{},[91,120460,120462],{"className":93,"code":120461,"language":95,"meta":96,"style":96},"user_input = \" 42 \"\nprint(user_input)        # looks like:  42\nprint(repr(user_input))  # shows: ' 42 '\n",[17,120463,120464,120476,120489],{"__ignoreMap":96},[100,120465,120466,120468,120470,120472,120474],{"class":102,"line":103},[100,120467,6201],{"class":106},[100,120469,111],{"class":110},[100,120471,1708],{"class":205},[100,120473,100147],{"class":209},[100,120475,1714],{"class":205},[100,120477,120478,120480,120482,120484,120486],{"class":102,"line":135},[100,120479,372],{"class":114},[100,120481,170],{"class":118},[100,120483,6230],{"class":178},[100,120485,6155],{"class":118},[100,120487,120488],{"class":414},"        # looks like:  42\n",[100,120490,120491,120493,120495,120497,120499,120501,120503],{"class":102,"line":142},[100,120492,372],{"class":114},[100,120494,170],{"class":118},[100,120496,12228],{"class":114},[100,120498,170],{"class":118},[100,120500,6230],{"class":178},[100,120502,411],{"class":118},[100,120504,120505],{"class":414},"  # shows: ' 42 '\n",[14,120507,120508,120509,120511],{},"For ",[17,120510,8090],{}," problems, inspect the list of parts:",[91,120513,120515],{"className":93,"code":120514,"language":95,"meta":96,"style":96},"parts = input(\"Enter values: \").split()\nprint(parts, len(parts))\n",[17,120516,120517,120540],{"__ignoreMap":96},[100,120518,120519,120521,120523,120525,120527,120529,120532,120534,120536,120538],{"class":102,"line":103},[100,120520,4345],{"class":106},[100,120522,111],{"class":110},[100,120524,7221],{"class":114},[100,120526,170],{"class":118},[100,120528,206],{"class":205},[100,120530,120531],{"class":209},"Enter values: ",[100,120533,206],{"class":205},[100,120535,5799],{"class":118},[100,120537,3922],{"class":178},[100,120539,4734],{"class":118},[100,120541,120542,120544,120546,120548,120550,120552,120554,120556],{"class":102,"line":135},[100,120543,372],{"class":114},[100,120545,170],{"class":118},[100,120547,4373],{"class":178},[100,120549,126],{"class":118},[100,120551,3141],{"class":114},[100,120553,170],{"class":118},[100,120555,4373],{"class":178},[100,120557,182],{"class":118},[14,120559,120560],{},"This helps you see whether you got the number of values you expected.",[77,120562,120564],{"id":120563},"when-to-use-int-vs-float","When to use int() vs float()",[14,120566,120567],{},"Choose the conversion based on the kind of number you expect.",[14,120569,241,120570,120572],{},[17,120571,6119],{}," for whole numbers:",[40,120574,120575,120579,120583],{},[43,120576,120577],{},[17,120578,3557],{},[43,120580,120581],{},[17,120582,95521],{},[43,120584,120585],{},[17,120586,2579],{},[14,120588,241,120589,120591],{},[17,120590,59778],{}," for decimal numbers:",[40,120593,120594,120598,120603],{},[43,120595,120596],{},[17,120597,110922],{},[43,120599,120600],{},[17,120601,120602],{},"2.0",[43,120604,120605],{},[17,120606,81803],{},[14,120608,1844],{},[91,120610,120612],{"className":93,"code":120611,"language":95,"meta":96,"style":96},"whole = int(\"5\")\ndecimal = float(\"3.14\")\n\nprint(whole)\nprint(decimal)\n",[17,120613,120614,120633,120652,120656,120667],{"__ignoreMap":96},[100,120615,120616,120619,120621,120623,120625,120627,120629,120631],{"class":102,"line":103},[100,120617,120618],{"class":106},"whole ",[100,120620,111],{"class":110},[100,120622,3389],{"class":191},[100,120624,170],{"class":118},[100,120626,206],{"class":205},[100,120628,3557],{"class":209},[100,120630,206],{"class":205},[100,120632,215],{"class":118},[100,120634,120635,120638,120640,120642,120644,120646,120648,120650],{"class":102,"line":135},[100,120636,120637],{"class":106},"decimal ",[100,120639,111],{"class":110},[100,120641,106783],{"class":191},[100,120643,170],{"class":118},[100,120645,206],{"class":205},[100,120647,110922],{"class":209},[100,120649,206],{"class":205},[100,120651,215],{"class":118},[100,120653,120654],{"class":102,"line":142},[100,120655,139],{"emptyLinePlaceholder":138},[100,120657,120658,120660,120662,120665],{"class":102,"line":152},[100,120659,372],{"class":114},[100,120661,170],{"class":118},[100,120663,120664],{"class":178},"whole",[100,120666,215],{"class":118},[100,120668,120669,120671,120673,120675],{"class":102,"line":164},[100,120670,372],{"class":114},[100,120672,170],{"class":118},[100,120674,60218],{"class":178},[100,120676,215],{"class":118},[14,120678,120679,120680,120682],{},"If your program may receive either whole numbers or decimals, ",[17,120681,59778],{}," is often the better choice.",[14,120684,580],{},[91,120686,120688],{"className":93,"code":120687,"language":95,"meta":96,"style":96},"user_text = input(\"Enter a number: \").strip()\n\ntry:\n    number = float(user_text)\n    print(\"Valid number:\", number)\nexcept ValueError:\n    print(\"Please enter a valid number.\")\n",[17,120689,120690,120712,120716,120722,120736,120754,120762],{"__ignoreMap":96},[100,120691,120692,120694,120696,120698,120700,120702,120704,120706,120708,120710],{"class":102,"line":103},[100,120693,119093],{"class":106},[100,120695,111],{"class":110},[100,120697,7221],{"class":114},[100,120699,170],{"class":118},[100,120701,206],{"class":205},[100,120703,6217],{"class":209},[100,120705,206],{"class":205},[100,120707,5799],{"class":118},[100,120709,6235],{"class":178},[100,120711,4734],{"class":118},[100,120713,120714],{"class":102,"line":135},[100,120715,139],{"emptyLinePlaceholder":138},[100,120717,120718,120720],{"class":102,"line":142},[100,120719,146],{"class":145},[100,120721,149],{"class":118},[100,120723,120724,120726,120728,120730,120732,120734],{"class":102,"line":152},[100,120725,98700],{"class":106},[100,120727,111],{"class":110},[100,120729,106783],{"class":191},[100,120731,170],{"class":118},[100,120733,119134],{"class":178},[100,120735,215],{"class":118},[100,120737,120738,120740,120742,120744,120746,120748,120750,120752],{"class":102,"line":164},[100,120739,200],{"class":114},[100,120741,170],{"class":118},[100,120743,206],{"class":205},[100,120745,111972],{"class":209},[100,120747,206],{"class":205},[100,120749,126],{"class":118},[100,120751,84021],{"class":178},[100,120753,215],{"class":118},[100,120755,120756,120758,120760],{"class":102,"line":185},[100,120757,188],{"class":145},[100,120759,2794],{"class":191},[100,120761,149],{"class":118},[100,120763,120764,120766,120768,120770,120772,120774],{"class":102,"line":197},[100,120765,200],{"class":114},[100,120767,170],{"class":118},[100,120769,206],{"class":205},[100,120771,119604],{"class":209},[100,120773,206],{"class":205},[100,120775,215],{"class":118},[14,120777,120778,120779,120781],{},"If needed, you can later convert a valid float to an integer, but only if that makes sense for your program. See ",[295,120780,60492],{"href":60491}," for more details.",[77,120783,1344],{"id":1343},[14,120785,120786],{},"This error often happens because:",[40,120788,120789,120792,120798,120801,120804,120809],{},[43,120790,120791],{},"user entered letters instead of digits",[43,120793,120794,120795,120797],{},"user entered a decimal when ",[17,120796,6119],{}," was used",[43,120799,120800],{},"input was empty",[43,120802,120803],{},"input had extra spaces or formatting",[43,120805,120806,120807],{},"code unpacked the wrong number of values from ",[17,120808,8090],{},[43,120810,120811],{},"program assumed input was always valid",[77,120813,1514],{"id":1513},[675,120815,9137,120817,120820],{"id":120816},"why-does-int30-raise-valueerror",[17,120818,120819],{},"int('3.0')"," raise ValueError?",[14,120822,14478,120823,120825,120826,120828,120829,120832,120833,120835],{},[17,120824,6119],{}," expects a whole number string like ",[17,120827,98944],{},", not a decimal string like ",[17,120830,120831],{},"\"3.0\"",". Convert with ",[17,120834,59778],{}," first if needed.",[675,120837,20428,120839,120841],{"id":120838},"does-strip-fix-valueerror-by-itself",[17,120840,8739],{}," fix ValueError by itself?",[14,120843,120844,120845,120847],{},"Only sometimes. It removes spaces at the start and end, but it does not turn invalid text like ",[17,120846,99181],{}," into a number.",[675,120849,39391,120851,15957,120853,11353],{"id":120850},"should-i-use-isdigit-instead-of-try-except",[17,120852,119628],{},[17,120854,244],{},[14,120856,120857,120859,120860,120862],{},[17,120858,119628],{}," is useful for simple cases, but ",[17,120861,244],{}," is more flexible and handles more real input situations.",[675,120864,50625,120866,120868],{"id":120865},"can-input-return-an-int-automatically",[17,120867,14906],{}," return an int automatically?",[14,120870,14506,120871,120873],{},[17,120872,14906],{}," always returns a string, so you must convert it yourself.",[77,120875,1554],{"id":1553},[40,120877,120878,120882,120886,120890,120894,120898,120903],{},[43,120879,120880],{},[295,120881,119390],{"href":85586},[43,120883,120884],{},[295,120885,81195],{"href":81194},[43,120887,120888],{},[295,120889,60492],{"href":60491},[43,120891,120892],{},[295,120893,86861],{"href":85784},[43,120895,120896],{},[295,120897,67731],{"href":66364},[43,120899,120900],{},[295,120901,120902],{"href":17023},"ValueError: invalid literal for int() with base 10 (fix)",[43,120904,120905],{},[295,120906,120907],{"href":109684},"ValueError: could not convert string to float (fix)",[1589,120909,120910],{},"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 .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 .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 .slqww, html code.shiki .slqww{--shiki-light:#6182B8;--shiki-default:#24292E;--shiki-dark:#E1E4E8}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 .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 .srdBf, html code.shiki .srdBf{--shiki-light:#F76D47;--shiki-default:#005CC5;--shiki-dark:#79B8FF}html pre.shiki code .s2W-s, html code.shiki .s2W-s{--shiki-light:#39ADB5;--shiki-light-font-style:italic;--shiki-default:#032F62;--shiki-default-font-style:inherit;--shiki-dark:#9ECBFF;--shiki-dark-font-style:inherit}html pre.shiki code .sithA, html code.shiki .sithA{--shiki-light:#90A4AE;--shiki-light-font-style:italic;--shiki-default:#032F62;--shiki-default-font-style:inherit;--shiki-dark:#9ECBFF;--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 .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}",{"title":96,"searchDepth":135,"depth":135,"links":120912},[120913,120914,120915,120916,120917,120918,120919,120920,120926,120927,120928,120929,120939],{"id":79,"depth":135,"text":80},{"id":5880,"depth":135,"text":5881},{"id":24957,"depth":135,"text":24958},{"id":119395,"depth":135,"text":119396},{"id":119616,"depth":135,"text":119617},{"id":119776,"depth":135,"text":119777},{"id":120023,"depth":135,"text":120024},{"id":120161,"depth":135,"text":120162,"children":120921},[120922,120923,120924],{"id":120171,"depth":142,"text":120174},{"id":60476,"depth":142,"text":120236},{"id":120273,"depth":142,"text":120925},"Unpacking from split()",{"id":120368,"depth":135,"text":120369},{"id":120563,"depth":135,"text":120564},{"id":1343,"depth":135,"text":1344},{"id":1513,"depth":135,"text":1514,"children":120930},[120931,120933,120935,120937],{"id":120816,"depth":142,"text":120932},"Why does int('3.0') raise ValueError?",{"id":120838,"depth":142,"text":120934},"Does strip() fix ValueError by itself?",{"id":120850,"depth":142,"text":120936},"Should I use isdigit() instead of try-except?",{"id":120865,"depth":142,"text":120938},"Can input() return an int automatically?",{"id":1553,"depth":135,"text":1554},"Master valueerror when parsing input in python fix in our comprehensive Python beginner guide.",{},"\u002Ferrors\u002Fvalueerror-when-parsing-input-in-python-fix",{"title":119046,"description":120940},"errors\u002Fvalueerror-when-parsing-input-in-python-fix","OL-ETwUlqtel3W-vZKKvFk_MvBkSBHY3BYGRkvyAtOE",{"id":120947,"title":120948,"body":120949,"description":122298,"extension":1623,"meta":122299,"navigation":138,"path":122300,"seo":122301,"stem":122302,"__hash__":122303},"content\u002Ferrors\u002Fzerodivisionerror-division-by-zero-fix.md","ZeroDivisionError: division by zero (Fix)",{"type":7,"value":120950,"toc":122272},[120951,120954,120962,120965,120967,120970,121045,121051,121053,121060,121063,121080,121090,121092,121095,121113,121115,121178,121188,121194,121196,121202,121204,121232,121234,121269,121280,121284,121287,121294,121365,121367,121375,121378,121448,121450,121459,121462,121466,121471,121539,121541,121549,121552,121564,121566,121631,121633,121641,121649,121653,121656,121662,121757,121760,121781,121784,121791,121795,121803,121809,121827,121832,121852,121857,121877,121882,121884,121887,121889,121957,121959,122041,122043,122062,122070,122073,122089,122091,122095,122124,122127,122181,122188,122190,122194,122201,122205,122214,122218,122226,122230,122238,122240,122269],[10,120952,120948],{"id":120953},"zerodivisionerror-division-by-zero-fix",[14,120955,120956,120959,120960,290],{},[17,120957,120958],{},"ZeroDivisionError: division by zero"," happens when Python tries to divide by ",[17,120961,2279],{},[14,120963,120964],{},"This page shows what the error means, why it happens, and the simplest ways to prevent or handle it.",[77,120966,80],{"id":79},[14,120968,120969],{},"Check the value before dividing. If zero is possible, handle that case first.",[91,120971,120973],{"className":93,"code":120972,"language":95,"meta":96,"style":96},"denominator = 0\n\nif denominator != 0:\n    result = 10 \u002F denominator\n    print(result)\nelse:\n    print(\"Cannot divide by zero\")\n",[17,120974,120975,120984,120988,121001,121014,121024,121030],{"__ignoreMap":96},[100,120976,120977,120980,120982],{"class":102,"line":103},[100,120978,120979],{"class":106},"denominator ",[100,120981,111],{"class":110},[100,120983,2069],{"class":122},[100,120985,120986],{"class":102,"line":135},[100,120987,139],{"emptyLinePlaceholder":138},[100,120989,120990,120992,120995,120997,120999],{"class":102,"line":142},[100,120991,2736],{"class":145},[100,120993,120994],{"class":106}," denominator ",[100,120996,116389],{"class":110},[100,120998,1921],{"class":122},[100,121000,149],{"class":118},[100,121002,121003,121005,121007,121009,121011],{"class":102,"line":152},[100,121004,59692],{"class":106},[100,121006,111],{"class":110},[100,121008,6550],{"class":122},[100,121010,17686],{"class":110},[100,121012,121013],{"class":106}," denominator\n",[100,121015,121016,121018,121020,121022],{"class":102,"line":164},[100,121017,200],{"class":114},[100,121019,170],{"class":118},[100,121021,11580],{"class":178},[100,121023,215],{"class":118},[100,121025,121026,121028],{"class":102,"line":185},[100,121027,4944],{"class":145},[100,121029,149],{"class":118},[100,121031,121032,121034,121036,121038,121041,121043],{"class":102,"line":197},[100,121033,200],{"class":114},[100,121035,170],{"class":118},[100,121037,206],{"class":205},[100,121039,121040],{"class":209},"Cannot divide by zero",[100,121042,206],{"class":205},[100,121044,215],{"class":118},[14,121046,121047,121048,121050],{},"If the denominator is ",[17,121049,2279],{},", the program avoids the division and prints a clear message instead.",[77,121052,5881],{"id":5880},[14,121054,3860,121055,121057,121058,290],{},[17,121056,17653],{}," when you try to divide a number by ",[17,121059,2279],{},[14,121061,121062],{},"This can happen with:",[40,121064,121065,121070,121075],{},[43,121066,121067,121069],{},[17,121068,2558],{}," for normal division",[43,121071,121072,121074],{},[17,121073,81351],{}," for floor division",[43,121076,121077,121079],{},[17,121078,92827],{}," for modulo",[14,121081,121082,121083,3266,121085,121087,121088,290],{},"When this error happens, Python stops at that line unless you handle the error with ",[17,121084,146],{},[17,121086,188],{},". If you are new to exceptions, see ",[295,121089,1587],{"href":1749},[77,121091,11031],{"id":11030},[14,121093,121094],{},"Here is the smallest possible example:",[91,121096,121097],{"className":93,"code":17673,"language":95,"meta":96,"style":96},[17,121098,121099],{"__ignoreMap":96},[100,121100,121101,121103,121105,121107,121109,121111],{"class":102,"line":103},[100,121102,372],{"class":114},[100,121104,170],{"class":118},[100,121106,356],{"class":122},[100,121108,17686],{"class":110},[100,121110,1921],{"class":122},[100,121112,215],{"class":118},[14,121114,218],{},[91,121116,121118],{"className":93,"code":121117,"language":95,"meta":96,"style":96},"Traceback (most recent call last):\n  File \"example.py\", line 1, in \u003Cmodule>\n    print(10 \u002F 0)\nZeroDivisionError: division by zero\n",[17,121119,121120,121130,121156,121170],{"__ignoreMap":96},[100,121121,121122,121124,121126,121128],{"class":102,"line":103},[100,121123,514],{"class":178},[100,121125,170],{"class":118},[100,121127,519],{"class":178},[100,121129,522],{"class":118},[100,121131,121132,121134,121136,121138,121140,121142,121144,121146,121148,121150,121152,121154],{"class":102,"line":135},[100,121133,1952],{"class":106},[100,121135,206],{"class":205},[100,121137,1957],{"class":209},[100,121139,206],{"class":205},[100,121141,126],{"class":118},[100,121143,1964],{"class":106},[100,121145,123],{"class":122},[100,121147,126],{"class":118},[100,121149,1971],{"class":110},[100,121151,1974],{"class":110},[100,121153,1977],{"class":106},[100,121155,1980],{"class":110},[100,121157,121158,121160,121162,121164,121166,121168],{"class":102,"line":142},[100,121159,200],{"class":114},[100,121161,170],{"class":118},[100,121163,356],{"class":122},[100,121165,17686],{"class":110},[100,121167,1921],{"class":122},[100,121169,215],{"class":118},[100,121171,121172,121174,121176],{"class":102,"line":152},[100,121173,17653],{"class":191},[100,121175,89],{"class":118},[100,121177,17706],{"class":106},[14,121179,121180,121181,121184,121185,290],{},"In a division like ",[17,121182,121183],{},"10 \u002F 0",", the value on the right side is called the ",[22,121186,121187],{},"denominator",[14,121189,121190,121191,121193],{},"Because the denominator is ",[17,121192,2279],{},", Python raises the error.",[77,121195,25091],{"id":25090},[14,121197,121198,121199,121201],{},"This error usually means the denominator became ",[17,121200,2279],{}," somewhere earlier in your program.",[14,121203,14889],{},[40,121205,121206,121211,121217,121223],{},[43,121207,3959,121208,121210],{},[17,121209,2279],{}," when you expected another number",[43,121212,121213,121214,121216],{},"User input becomes ",[17,121215,2279],{}," after conversion",[43,121218,121219,121220,121222],{},"A calculation returns ",[17,121221,2279],{}," and you divide by that result",[43,121224,121225,121226,3178,121228,42154,121230],{},"You use ",[17,121227,81351],{},[17,121229,92827],{},[17,121231,2279],{},[14,121233,108977],{},[91,121235,121237],{"className":93,"code":121236,"language":95,"meta":96,"style":96},"denominator = 0\nresult = 10 \u002F denominator\nprint(result)\n",[17,121238,121239,121247,121259],{"__ignoreMap":96},[100,121240,121241,121243,121245],{"class":102,"line":103},[100,121242,120979],{"class":106},[100,121244,111],{"class":110},[100,121246,2069],{"class":122},[100,121248,121249,121251,121253,121255,121257],{"class":102,"line":135},[100,121250,11556],{"class":106},[100,121252,111],{"class":110},[100,121254,6550],{"class":122},[100,121256,17686],{"class":110},[100,121258,121013],{"class":106},[100,121260,121261,121263,121265,121267],{"class":102,"line":142},[100,121262,372],{"class":114},[100,121264,170],{"class":118},[100,121266,11580],{"class":178},[100,121268,215],{"class":118},[14,121270,121271,121272,121274,121275,121277,121278,290],{},"Even though you are not writing ",[17,121273,121183],{}," directly, the result is the same because ",[17,121276,121187],{}," stores ",[17,121279,2279],{},[77,121281,121283],{"id":121282},"fix-1-check-before-dividing","Fix 1: Check before dividing",[14,121285,121286],{},"This is the clearest fix for beginners.",[14,121288,2746,121289,121291,121292,290],{},[17,121290,2736],{}," statement before the division. Only divide when the denominator is not ",[17,121293,2279],{},[91,121295,121297],{"className":93,"code":121296,"language":95,"meta":96,"style":96},"denominator = 2\n\nif denominator != 0:\n    result = 10 \u002F denominator\n    print(result)\nelse:\n    print(\"Cannot divide by zero\")\n",[17,121298,121299,121307,121311,121323,121335,121345,121351],{"__ignoreMap":96},[100,121300,121301,121303,121305],{"class":102,"line":103},[100,121302,120979],{"class":106},[100,121304,111],{"class":110},[100,121306,3564],{"class":122},[100,121308,121309],{"class":102,"line":135},[100,121310,139],{"emptyLinePlaceholder":138},[100,121312,121313,121315,121317,121319,121321],{"class":102,"line":142},[100,121314,2736],{"class":145},[100,121316,120994],{"class":106},[100,121318,116389],{"class":110},[100,121320,1921],{"class":122},[100,121322,149],{"class":118},[100,121324,121325,121327,121329,121331,121333],{"class":102,"line":152},[100,121326,59692],{"class":106},[100,121328,111],{"class":110},[100,121330,6550],{"class":122},[100,121332,17686],{"class":110},[100,121334,121013],{"class":106},[100,121336,121337,121339,121341,121343],{"class":102,"line":164},[100,121338,200],{"class":114},[100,121340,170],{"class":118},[100,121342,11580],{"class":178},[100,121344,215],{"class":118},[100,121346,121347,121349],{"class":102,"line":185},[100,121348,4944],{"class":145},[100,121350,149],{"class":118},[100,121352,121353,121355,121357,121359,121361,121363],{"class":102,"line":197},[100,121354,200],{"class":114},[100,121356,170],{"class":118},[100,121358,206],{"class":205},[100,121360,121040],{"class":209},[100,121362,206],{"class":205},[100,121364,215],{"class":118},[14,121366,218],{},[91,121368,121369],{"className":93,"code":55055,"language":95,"meta":96,"style":96},[17,121370,121371],{"__ignoreMap":96},[100,121372,121373],{"class":102,"line":103},[100,121374,55055],{"class":122},[14,121376,121377],{},"Now with a zero value:",[91,121379,121380],{"className":93,"code":120972,"language":95,"meta":96,"style":96},[17,121381,121382,121390,121394,121406,121418,121428,121434],{"__ignoreMap":96},[100,121383,121384,121386,121388],{"class":102,"line":103},[100,121385,120979],{"class":106},[100,121387,111],{"class":110},[100,121389,2069],{"class":122},[100,121391,121392],{"class":102,"line":135},[100,121393,139],{"emptyLinePlaceholder":138},[100,121395,121396,121398,121400,121402,121404],{"class":102,"line":142},[100,121397,2736],{"class":145},[100,121399,120994],{"class":106},[100,121401,116389],{"class":110},[100,121403,1921],{"class":122},[100,121405,149],{"class":118},[100,121407,121408,121410,121412,121414,121416],{"class":102,"line":152},[100,121409,59692],{"class":106},[100,121411,111],{"class":110},[100,121413,6550],{"class":122},[100,121415,17686],{"class":110},[100,121417,121013],{"class":106},[100,121419,121420,121422,121424,121426],{"class":102,"line":164},[100,121421,200],{"class":114},[100,121423,170],{"class":118},[100,121425,11580],{"class":178},[100,121427,215],{"class":118},[100,121429,121430,121432],{"class":102,"line":185},[100,121431,4944],{"class":145},[100,121433,149],{"class":118},[100,121435,121436,121438,121440,121442,121444,121446],{"class":102,"line":197},[100,121437,200],{"class":114},[100,121439,170],{"class":118},[100,121441,206],{"class":205},[100,121443,121040],{"class":209},[100,121445,206],{"class":205},[100,121447,215],{"class":118},[14,121449,218],{},[91,121451,121453],{"className":93,"code":121452,"language":95,"meta":96,"style":96},"Cannot divide by zero\n",[17,121454,121455],{"__ignoreMap":96},[100,121456,121457],{"class":102,"line":103},[100,121458,121452],{"class":106},[14,121460,121461],{},"This approach is simple and easy to read. In many programs, it is better than catching the error after it happens.",[77,121463,121465],{"id":121464},"fix-2-handle-the-error-with-try-except","Fix 2: Handle the error with try-except",[14,121467,241,121468,121470],{},[17,121469,244],{}," when a zero value is possible and hard to predict.",[91,121472,121474],{"className":93,"code":121473,"language":95,"meta":96,"style":96},"denominator = 0\n\ntry:\n    result = 10 \u002F denominator\n    print(result)\nexcept ZeroDivisionError:\n    print(\"Cannot divide by zero\")\n",[17,121475,121476,121484,121488,121494,121506,121516,121525],{"__ignoreMap":96},[100,121477,121478,121480,121482],{"class":102,"line":103},[100,121479,120979],{"class":106},[100,121481,111],{"class":110},[100,121483,2069],{"class":122},[100,121485,121486],{"class":102,"line":135},[100,121487,139],{"emptyLinePlaceholder":138},[100,121489,121490,121492],{"class":102,"line":142},[100,121491,146],{"class":145},[100,121493,149],{"class":118},[100,121495,121496,121498,121500,121502,121504],{"class":102,"line":152},[100,121497,59692],{"class":106},[100,121499,111],{"class":110},[100,121501,6550],{"class":122},[100,121503,17686],{"class":110},[100,121505,121013],{"class":106},[100,121507,121508,121510,121512,121514],{"class":102,"line":164},[100,121509,200],{"class":114},[100,121511,170],{"class":118},[100,121513,11580],{"class":178},[100,121515,215],{"class":118},[100,121517,121518,121520,121523],{"class":102,"line":185},[100,121519,188],{"class":145},[100,121521,121522],{"class":191}," ZeroDivisionError",[100,121524,149],{"class":118},[100,121526,121527,121529,121531,121533,121535,121537],{"class":102,"line":197},[100,121528,200],{"class":114},[100,121530,170],{"class":118},[100,121532,206],{"class":205},[100,121534,121040],{"class":209},[100,121536,206],{"class":205},[100,121538,215],{"class":118},[14,121540,218],{},[91,121542,121543],{"className":93,"code":121452,"language":95,"meta":96,"style":96},[17,121544,121545],{"__ignoreMap":96},[100,121546,121547],{"class":102,"line":103},[100,121548,121452],{"class":106},[14,121550,121551],{},"This tells Python:",[40,121553,121554,121557],{},[43,121555,121556],{},"Try to run the division",[43,121558,11734,121559,121561,121562,45100],{},[17,121560,17653],{}," happens, run the ",[17,121563,188],{},[14,121565,49669],{},[91,121567,121569],{"className":93,"code":121568,"language":95,"meta":96,"style":96},"denominator = 0\n\ntry:\n    result = 10 \u002F denominator\nexcept ZeroDivisionError:\n    result = None\n\nprint(result)\n",[17,121570,121571,121579,121583,121589,121601,121609,121617,121621],{"__ignoreMap":96},[100,121572,121573,121575,121577],{"class":102,"line":103},[100,121574,120979],{"class":106},[100,121576,111],{"class":110},[100,121578,2069],{"class":122},[100,121580,121581],{"class":102,"line":135},[100,121582,139],{"emptyLinePlaceholder":138},[100,121584,121585,121587],{"class":102,"line":142},[100,121586,146],{"class":145},[100,121588,149],{"class":118},[100,121590,121591,121593,121595,121597,121599],{"class":102,"line":152},[100,121592,59692],{"class":106},[100,121594,111],{"class":110},[100,121596,6550],{"class":122},[100,121598,17686],{"class":110},[100,121600,121013],{"class":106},[100,121602,121603,121605,121607],{"class":102,"line":164},[100,121604,188],{"class":145},[100,121606,121522],{"class":191},[100,121608,149],{"class":118},[100,121610,121611,121613,121615],{"class":102,"line":185},[100,121612,59692],{"class":106},[100,121614,111],{"class":110},[100,121616,4904],{"class":158},[100,121618,121619],{"class":102,"line":197},[100,121620,139],{"emptyLinePlaceholder":138},[100,121622,121623,121625,121627,121629],{"class":102,"line":771},[100,121624,372],{"class":114},[100,121626,170],{"class":118},[100,121628,11580],{"class":178},[100,121630,215],{"class":118},[14,121632,218],{},[91,121634,121635],{"className":93,"code":924,"language":95,"meta":96,"style":96},[17,121636,121637],{"__ignoreMap":96},[100,121638,121639],{"class":102,"line":103},[100,121640,924],{"class":158},[14,121642,121643,121644,3178,121647,290],{},"If you want to learn this pattern in more detail, read ",[295,121645,121646],{"href":12761},"using try-except, else, and finally in Python",[295,121648,1029],{"href":2824},[77,121650,121652],{"id":121651},"fix-3-validate-user-input","Fix 3: Validate user input",[14,121654,121655],{},"A very common cause is dividing by a number entered by the user.",[14,121657,121658,121659,121661],{},"After converting the input, check whether the value is ",[17,121660,2279],{},". Do not divide until the value is valid.",[91,121663,121665],{"className":93,"code":121664,"language":95,"meta":96,"style":96},"number = int(input(\"Enter a number to divide by: \"))\n\nif number == 0:\n    print(\"You cannot divide by zero.\")\nelse:\n    result = 10 \u002F number\n    print(\"Result:\", result)\n",[17,121666,121667,121690,121694,121706,121721,121727,121739],{"__ignoreMap":96},[100,121668,121669,121671,121673,121675,121677,121679,121681,121683,121686,121688],{"class":102,"line":103},[100,121670,1901],{"class":106},[100,121672,111],{"class":110},[100,121674,3389],{"class":191},[100,121676,170],{"class":118},[100,121678,6210],{"class":114},[100,121680,170],{"class":118},[100,121682,206],{"class":205},[100,121684,121685],{"class":209},"Enter a number to divide by: ",[100,121687,206],{"class":205},[100,121689,182],{"class":118},[100,121691,121692],{"class":102,"line":135},[100,121693,139],{"emptyLinePlaceholder":138},[100,121695,121696,121698,121700,121702,121704],{"class":102,"line":142},[100,121697,2736],{"class":145},[100,121699,614],{"class":106},[100,121701,32169],{"class":110},[100,121703,1921],{"class":122},[100,121705,149],{"class":118},[100,121707,121708,121710,121712,121714,121717,121719],{"class":102,"line":152},[100,121709,200],{"class":114},[100,121711,170],{"class":118},[100,121713,206],{"class":205},[100,121715,121716],{"class":209},"You cannot divide by zero.",[100,121718,206],{"class":205},[100,121720,215],{"class":118},[100,121722,121723,121725],{"class":102,"line":164},[100,121724,4944],{"class":145},[100,121726,149],{"class":118},[100,121728,121729,121731,121733,121735,121737],{"class":102,"line":185},[100,121730,59692],{"class":106},[100,121732,111],{"class":110},[100,121734,6550],{"class":122},[100,121736,17686],{"class":110},[100,121738,101794],{"class":106},[100,121740,121741,121743,121745,121747,121749,121751,121753,121755],{"class":102,"line":197},[100,121742,200],{"class":114},[100,121744,170],{"class":118},[100,121746,206],{"class":205},[100,121748,80989],{"class":209},[100,121750,206],{"class":205},[100,121752,126],{"class":118},[100,121754,75984],{"class":178},[100,121756,215],{"class":118},[14,121758,121759],{},"Example run:",[91,121761,121763],{"className":93,"code":121762,"language":95,"meta":96,"style":96},"Enter a number to divide by: 0\nYou cannot divide by zero.\n",[17,121764,121765,121774],{"__ignoreMap":96},[100,121766,121767,121770,121772],{"class":102,"line":103},[100,121768,121769],{"class":106},"Enter a number to divide by",[100,121771,89],{"class":118},[100,121773,2069],{"class":122},[100,121775,121776,121779],{"class":102,"line":135},[100,121777,121778],{"class":106},"You cannot divide by zero",[100,121780,75606],{"class":118},[14,121782,121783],{},"This is safer than dividing immediately.",[14,121785,121786,121787,3266,121789,290],{},"If you need help with conversion, see ",[295,121788,85785],{"href":85784},[295,121790,81195],{"href":81194},[77,121792,121794],{"id":121793},"division-operations-that-can-fail","Division operations that can fail",[14,121796,121797,121798,121800,121801,89],{},"These operations all raise ",[17,121799,17653],{}," when the denominator is ",[17,121802,2279],{},[675,121804,121806,121808],{"id":121805},"with-zero",[17,121807,2558],{}," with zero",[91,121810,121811],{"className":93,"code":17673,"language":95,"meta":96,"style":96},[17,121812,121813],{"__ignoreMap":96},[100,121814,121815,121817,121819,121821,121823,121825],{"class":102,"line":103},[100,121816,372],{"class":114},[100,121818,170],{"class":118},[100,121820,356],{"class":122},[100,121822,17686],{"class":110},[100,121824,1921],{"class":122},[100,121826,215],{"class":118},[675,121828,121830,121808],{"id":121829},"with-zero-1",[17,121831,81351],{},[91,121833,121835],{"className":93,"code":121834,"language":95,"meta":96,"style":96},"print(10 \u002F\u002F 0)\n",[17,121836,121837],{"__ignoreMap":96},[100,121838,121839,121841,121843,121845,121848,121850],{"class":102,"line":103},[100,121840,372],{"class":114},[100,121842,170],{"class":118},[100,121844,356],{"class":122},[100,121846,121847],{"class":110}," \u002F\u002F",[100,121849,1921],{"class":122},[100,121851,215],{"class":118},[675,121853,121855,121808],{"id":121854},"with-zero-2",[17,121856,92827],{},[91,121858,121860],{"className":93,"code":121859,"language":95,"meta":96,"style":96},"print(10 % 0)\n",[17,121861,121862],{"__ignoreMap":96},[100,121863,121864,121866,121868,121870,121873,121875],{"class":102,"line":103},[100,121865,372],{"class":114},[100,121867,170],{"class":118},[100,121869,356],{"class":122},[100,121871,121872],{"class":110}," %",[100,121874,1921],{"class":122},[100,121876,215],{"class":118},[14,121878,121879,121880,290],{},"All three fail for the same reason: the right side is ",[17,121881,2279],{},[77,121883,3274],{"id":3273},[14,121885,121886],{},"If you are not sure where the zero came from, check the denominator before the failing line.",[14,121888,5295],{},[91,121890,121892],{"className":93,"code":121891,"language":95,"meta":96,"style":96},"print(denominator)\nprint(type(denominator))\nprint('a =', a, 'b =', b)\nbreakpoint()\n",[17,121893,121894,121904,121918,121950],{"__ignoreMap":96},[100,121895,121896,121898,121900,121902],{"class":102,"line":103},[100,121897,372],{"class":114},[100,121899,170],{"class":118},[100,121901,121187],{"class":178},[100,121903,215],{"class":118},[100,121905,121906,121908,121910,121912,121914,121916],{"class":102,"line":135},[100,121907,372],{"class":114},[100,121909,170],{"class":118},[100,121911,1250],{"class":191},[100,121913,170],{"class":118},[100,121915,121187],{"class":178},[100,121917,182],{"class":118},[100,121919,121920,121922,121924,121926,121929,121931,121933,121935,121937,121939,121942,121944,121946,121948],{"class":102,"line":142},[100,121921,372],{"class":114},[100,121923,170],{"class":118},[100,121925,1280],{"class":205},[100,121927,121928],{"class":209},"a =",[100,121930,1280],{"class":205},[100,121932,126],{"class":118},[100,121934,78678],{"class":178},[100,121936,126],{"class":118},[100,121938,1274],{"class":205},[100,121940,121941],{"class":209},"b =",[100,121943,1280],{"class":205},[100,121945,126],{"class":118},[100,121947,15869],{"class":178},[100,121949,215],{"class":118},[100,121951,121952,121955],{"class":102,"line":152},[100,121953,121954],{"class":114},"breakpoint",[100,121956,4734],{"class":118},[14,121958,1891],{},[91,121960,121962],{"className":93,"code":121961,"language":95,"meta":96,"style":96},"a = 10\nb = 5 - 5\n\nprint('a =', a, 'b =', b)\nresult = a \u002F b\nprint(result)\n",[17,121963,121964,121972,121985,121989,122019,122031],{"__ignoreMap":96},[100,121965,121966,121968,121970],{"class":102,"line":103},[100,121967,82313],{"class":106},[100,121969,111],{"class":110},[100,121971,2181],{"class":122},[100,121973,121974,121977,121979,121981,121983],{"class":102,"line":135},[100,121975,121976],{"class":106},"b ",[100,121978,111],{"class":110},[100,121980,31164],{"class":122},[100,121982,1906],{"class":110},[100,121984,37580],{"class":122},[100,121986,121987],{"class":102,"line":142},[100,121988,139],{"emptyLinePlaceholder":138},[100,121990,121991,121993,121995,121997,121999,122001,122003,122005,122007,122009,122011,122013,122015,122017],{"class":102,"line":152},[100,121992,372],{"class":114},[100,121994,170],{"class":118},[100,121996,1280],{"class":205},[100,121998,121928],{"class":209},[100,122000,1280],{"class":205},[100,122002,126],{"class":118},[100,122004,78678],{"class":178},[100,122006,126],{"class":118},[100,122008,1274],{"class":205},[100,122010,121941],{"class":209},[100,122012,1280],{"class":205},[100,122014,126],{"class":118},[100,122016,15869],{"class":178},[100,122018,215],{"class":118},[100,122020,122021,122023,122025,122027,122029],{"class":102,"line":164},[100,122022,11556],{"class":106},[100,122024,111],{"class":110},[100,122026,25407],{"class":106},[100,122028,2558],{"class":110},[100,122030,33812],{"class":106},[100,122032,122033,122035,122037,122039],{"class":102,"line":185},[100,122034,372],{"class":114},[100,122036,170],{"class":118},[100,122038,11580],{"class":178},[100,122040,215],{"class":118},[14,122042,38743],{},[91,122044,122046],{"className":93,"code":122045,"language":95,"meta":96,"style":96},"a = 10 b = 0\n",[17,122047,122048],{"__ignoreMap":96},[100,122049,122050,122052,122054,122056,122058,122060],{"class":102,"line":103},[100,122051,82313],{"class":106},[100,122053,111],{"class":110},[100,122055,6550],{"class":122},[100,122057,25360],{"class":106},[100,122059,111],{"class":110},[100,122061,2069],{"class":122},[14,122063,122064,122065,122067,122068,290],{},"This tells you that ",[17,122066,4027],{}," became ",[17,122069,2279],{},[14,122071,122072],{},"When debugging this error:",[40,122074,122075,122078,122081,122086],{},[43,122076,122077],{},"Print the denominator value before the failing line",[43,122079,122080],{},"Trace where that value comes from",[43,122082,122083,122084],{},"Check calculations that may return ",[17,122085,2279],{},[43,122087,122088],{},"Check converted input values",[77,122090,7117],{"id":7116},[14,122092,106453,122093,89],{},[17,122094,17653],{},[40,122096,122097,122103,122106,122111,122119],{},[43,122098,122099,122100,122102],{},"Using a variable that is ",[17,122101,2279],{}," in a division expression",[43,122104,122105],{},"Dividing by user input without checking it first",[43,122107,122108,122109],{},"Using the result of a subtraction or count that became ",[17,122110,2279],{},[43,122112,1357,122113,3178,122115,42154,122117],{},[17,122114,81351],{},[17,122116,92827],{},[17,122118,2279],{},[43,122120,122121,122122],{},"Expecting a function to return a non-zero value when it returns ",[17,122123,2279],{},[14,122125,122126],{},"Example of a calculation that becomes zero:",[91,122128,122130],{"className":93,"code":122129,"language":95,"meta":96,"style":96},"total = 8\ncount = 8 - 8\n\naverage = total \u002F count\nprint(average)\n",[17,122131,122132,122141,122153,122157,122170],{"__ignoreMap":96},[100,122133,122134,122136,122138],{"class":102,"line":103},[100,122135,53376],{"class":106},[100,122137,111],{"class":110},[100,122139,122140],{"class":122}," 8\n",[100,122142,122143,122145,122147,122149,122151],{"class":102,"line":135},[100,122144,2176],{"class":106},[100,122146,111],{"class":110},[100,122148,71812],{"class":122},[100,122150,1906],{"class":110},[100,122152,122140],{"class":122},[100,122154,122155],{"class":102,"line":142},[100,122156,139],{"emptyLinePlaceholder":138},[100,122158,122159,122162,122164,122166,122168],{"class":102,"line":152},[100,122160,122161],{"class":106},"average ",[100,122163,111],{"class":110},[100,122165,2555],{"class":106},[100,122167,2558],{"class":110},[100,122169,101495],{"class":106},[100,122171,122172,122174,122176,122179],{"class":102,"line":164},[100,122173,372],{"class":114},[100,122175,170],{"class":118},[100,122177,122178],{"class":178},"average",[100,122180,215],{"class":118},[14,122182,122183,34119,122185,122187],{},[17,122184,51844],{},[17,122186,2279],{},", so the division fails.",[77,122189,1514],{"id":1513},[675,122191,122193],{"id":122192},"does-python-allow-dividing-by-zero","Does Python allow dividing by zero?",[14,122195,122196,122197,121800,122199,290],{},"No. Python raises ",[17,122198,17653],{},[17,122200,2279],{},[675,122202,122204],{"id":122203},"can-this-happen-with-modulo","Can this happen with modulo?",[14,122206,122207,122208,122211,122212,290],{},"Yes. Using ",[17,122209,122210],{},"% 0"," also raises ",[17,122213,17653],{},[675,122215,122217],{"id":122216},"should-i-use-if-or-try-except","Should I use if or try-except?",[14,122219,241,122220,122222,122223,122225],{},[17,122221,2736],{}," when you can easily check the value first. Use ",[17,122224,244],{}," when the zero value is less predictable.",[675,122227,122229],{"id":122228},"can-float-division-by-0-also-fail","Can float division by 0 also fail?",[14,122231,122232,122233,122235,122236,290],{},"Yes. In normal Python code, dividing a float by ",[17,122234,2279],{}," still raises ",[17,122237,17653],{},[77,122239,1554],{"id":1553},[40,122241,122242,122246,122251,122255,122259,122263],{},[43,122243,122244],{},[295,122245,1587],{"href":1749},[43,122247,122248],{},[295,122249,122250],{"href":12761},"Using try-except, else, and finally in Python",[43,122252,122253],{},[295,122254,1581],{"href":2824},[43,122256,122257],{},[295,122258,86861],{"href":85784},[43,122260,122261],{},[295,122262,81195],{"href":81194},[43,122264,122265],{},[295,122266,122268],{"href":122267},"\u002Ferrors\u002Fzerodivisionerror-in-python-causes-and-fixes\u002F","ZeroDivisionError in Python: causes and fixes",[1589,122270,122271],{},"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 .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 .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 .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 .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 .s39Yj, html code.shiki .s39Yj{--shiki-light:#39ADB5;--shiki-default:#005CC5;--shiki-dark:#79B8FF}",{"title":96,"searchDepth":135,"depth":135,"links":122273},[122274,122275,122276,122277,122278,122279,122280,122281,122289,122290,122291,122297],{"id":79,"depth":135,"text":80},{"id":5880,"depth":135,"text":5881},{"id":11030,"depth":135,"text":11031},{"id":25090,"depth":135,"text":25091},{"id":121282,"depth":135,"text":121283},{"id":121464,"depth":135,"text":121465},{"id":121651,"depth":135,"text":121652},{"id":121793,"depth":135,"text":121794,"children":122282},[122283,122285,122287],{"id":121805,"depth":142,"text":122284},"\u002F with zero",{"id":121829,"depth":142,"text":122286},"\u002F\u002F with zero",{"id":121854,"depth":142,"text":122288},"% with zero",{"id":3273,"depth":135,"text":3274},{"id":7116,"depth":135,"text":7117},{"id":1513,"depth":135,"text":1514,"children":122292},[122293,122294,122295,122296],{"id":122192,"depth":142,"text":122193},{"id":122203,"depth":142,"text":122204},{"id":122216,"depth":142,"text":122217},{"id":122228,"depth":142,"text":122229},{"id":1553,"depth":135,"text":1554},"Master zerodivisionerror division by zero fix in our comprehensive Python beginner guide.",{},"\u002Ferrors\u002Fzerodivisionerror-division-by-zero-fix",{"title":120948,"description":122298},"errors\u002Fzerodivisionerror-division-by-zero-fix","_jIjq-HpdRStU7r-XP0gMIOUx8f-WQOltHjVaizwo3Q",{"id":122305,"title":122306,"body":122307,"description":123799,"extension":1623,"meta":123800,"navigation":138,"path":123801,"seo":123802,"stem":123803,"__hash__":123804},"content\u002Ferrors\u002Fzerodivisionerror-in-python-causes-and-fixes.md","ZeroDivisionError in Python: Causes and Fixes",{"type":7,"value":122308,"toc":123767},[122309,122312,122319,122329,122332,122349,122351,122357,122428,122430,122438,122443,122447,122452,122455,122470,122476,122484,122486,122488,122506,122508,122520,122531,122534,122575,122579,122586,122588,122608,122611,122648,122650,122653,122679,122684,122689,122693,122696,122699,122780,122783,122794,122797,122874,122878,122881,122884,122951,122953,122961,122964,122970,122977,122981,122984,122989,123078,123088,123091,123101,123103,123106,123110,123113,123116,123130,123134,123137,123139,123182,123190,123194,123197,123299,123301,123364,123368,123371,123389,123392,123452,123458,123462,123470,123476,123494,123496,123508,123514,123532,123534,123553,123559,123577,123579,123593,123597,123608,123632,123635,123638,123641,123655,123657,123661,123670,123690,123694,123702,123722,123726,123729,123733,123736,123738,123764],[10,122310,122306],{"id":122311},"zerodivisionerror-in-python-causes-and-fixes",[14,122313,122314,122316,122317,290],{},[17,122315,17653],{}," happens when Python tries to divide a number by ",[17,122318,2279],{},[14,122320,122321,122322,5870,122324,5894,122326,122328],{},"This is a common beginner error. It usually means the value on the right side of ",[17,122323,2558],{},[17,122325,81351],{},[17,122327,92827],{}," became zero before the calculation ran.",[14,122330,122331],{},"In this guide, you will learn:",[40,122333,122334,122340,122343,122346],{},[43,122335,122336,122337,122339],{},"what ",[17,122338,17653],{}," means",[43,122341,122342],{},"the common reasons it happens",[43,122344,122345],{},"how to prevent it",[43,122347,122348],{},"how to debug the value that became zero",[77,122350,80],{"id":79},[14,122352,122353,122354,122356],{},"If the divisor might be ",[17,122355,2279],{},", check it before dividing:",[91,122358,122360],{"className":93,"code":122359,"language":95,"meta":96,"style":96},"number = 10\nvalue = 0\n\nif value != 0:\n    print(number \u002F value)\nelse:\n    print(\"Cannot divide by zero\")\n",[17,122361,122362,122370,122378,122382,122394,122408,122414],{"__ignoreMap":96},[100,122363,122364,122366,122368],{"class":102,"line":103},[100,122365,1901],{"class":106},[100,122367,111],{"class":110},[100,122369,2181],{"class":122},[100,122371,122372,122374,122376],{"class":102,"line":135},[100,122373,3318],{"class":106},[100,122375,111],{"class":110},[100,122377,2069],{"class":122},[100,122379,122380],{"class":102,"line":142},[100,122381,139],{"emptyLinePlaceholder":138},[100,122383,122384,122386,122388,122390,122392],{"class":102,"line":152},[100,122385,2736],{"class":145},[100,122387,989],{"class":106},[100,122389,116389],{"class":110},[100,122391,1921],{"class":122},[100,122393,149],{"class":118},[100,122395,122396,122398,122400,122402,122404,122406],{"class":102,"line":164},[100,122397,200],{"class":114},[100,122399,170],{"class":118},[100,122401,1901],{"class":178},[100,122403,2558],{"class":110},[100,122405,6747],{"class":178},[100,122407,215],{"class":118},[100,122409,122410,122412],{"class":102,"line":185},[100,122411,4944],{"class":145},[100,122413,149],{"class":118},[100,122415,122416,122418,122420,122422,122424,122426],{"class":102,"line":197},[100,122417,200],{"class":114},[100,122419,170],{"class":118},[100,122421,206],{"class":205},[100,122423,121040],{"class":209},[100,122425,206],{"class":205},[100,122427,215],{"class":118},[14,122429,218],{},[91,122431,122432],{"className":93,"code":121452,"language":95,"meta":96,"style":96},[17,122433,122434],{"__ignoreMap":96},[100,122435,122436],{"class":102,"line":103},[100,122437,121452],{"class":106},[14,122439,122440,122441,290],{},"Use a condition before dividing when the divisor might be ",[17,122442,2279],{},[77,122444,122446],{"id":122445},"what-zerodivisionerror-means","What ZeroDivisionError means",[14,122448,122449,122450,290],{},"This error happens when Python tries to divide by ",[17,122451,2279],{},[14,122453,122454],{},"It can happen with:",[40,122456,122457,122462,122466],{},[43,122458,122459,122461],{},[17,122460,2558],{}," for regular division",[43,122463,122464,121074],{},[17,122465,81351],{},[43,122467,122468,121079],{},[17,122469,92827],{},[14,122471,122472,122473,290],{},"The number on the right side of the operator is called the ",[22,122474,122475],{},"divisor",[14,122477,122478,122479,122481,122482,290],{},"If the divisor is ",[17,122480,2279],{},", Python stops and raises ",[17,122483,17653],{},[77,122485,11031],{"id":11030},[14,122487,1891],{},[91,122489,122490],{"className":93,"code":17673,"language":95,"meta":96,"style":96},[17,122491,122492],{"__ignoreMap":96},[100,122493,122494,122496,122498,122500,122502,122504],{"class":102,"line":103},[100,122495,372],{"class":114},[100,122497,170],{"class":118},[100,122499,356],{"class":122},[100,122501,17686],{"class":110},[100,122503,1921],{"class":122},[100,122505,215],{"class":118},[14,122507,33127],{},[91,122509,122510],{"className":93,"code":17695,"language":95,"meta":96,"style":96},[17,122511,122512],{"__ignoreMap":96},[100,122513,122514,122516,122518],{"class":102,"line":103},[100,122515,17653],{"class":191},[100,122517,89],{"class":118},[100,122519,17706],{"class":106},[14,122521,122522,122523,290,122525,122527,122528,122530],{},"The problem is the value on the right side of ",[17,122524,2558],{},[9927,122526],{},"\nIt is ",[17,122529,2279],{},", so Python cannot complete the calculation.",[14,122532,122533],{},"The same problem happens with a variable:",[91,122535,122537],{"className":93,"code":122536,"language":95,"meta":96,"style":96},"number = 10\ndivisor = 0\n\nprint(number \u002F divisor)\n",[17,122538,122539,122547,122556,122560],{"__ignoreMap":96},[100,122540,122541,122543,122545],{"class":102,"line":103},[100,122542,1901],{"class":106},[100,122544,111],{"class":110},[100,122546,2181],{"class":122},[100,122548,122549,122552,122554],{"class":102,"line":135},[100,122550,122551],{"class":106},"divisor ",[100,122553,111],{"class":110},[100,122555,2069],{"class":122},[100,122557,122558],{"class":102,"line":142},[100,122559,139],{"emptyLinePlaceholder":138},[100,122561,122562,122564,122566,122568,122570,122573],{"class":102,"line":152},[100,122563,372],{"class":114},[100,122565,170],{"class":118},[100,122567,1901],{"class":178},[100,122569,2558],{"class":110},[100,122571,122572],{"class":178}," divisor",[100,122574,215],{"class":118},[77,122576,122578],{"id":122577},"common-reasons-this-happens","Common reasons this happens",[14,122580,122581,122583,122584,92017],{},[17,122582,17653],{}," often appears because a value became ",[17,122585,2279],{},[14,122587,3971],{},[40,122589,122590,122595,122600,122605],{},[43,122591,122592,122593],{},"A variable unexpectedly contains ",[17,122594,2279],{},[43,122596,122597,122598],{},"User input is converted to a number and becomes ",[17,122599,2279],{},[43,122601,121219,122602,122604],{},[17,122603,2279],{}," and that result is reused as a divisor",[43,122606,122607],{},"A loop or function changes a value before division",[14,122609,122610],{},"Examples of common causes:",[40,122612,122613,122618,122625,122633,122639],{},[43,122614,122615,122616],{},"Dividing by a variable that has the value ",[17,122617,2279],{},[43,122619,1357,122620,122622,122623],{},[17,122621,7143],{}," and the user enters ",[17,122624,2279],{},[43,122626,11006,122627,122630,122631],{},[17,122628,122629],{},"len(...) - len(...)"," or another calculation that becomes ",[17,122632,2279],{},[43,122634,122635,122636,122638],{},"A function returns ",[17,122637,2279],{}," and that result is used as the divisor",[43,122640,1357,122641,3178,122643,42154,122645,122647],{},[17,122642,81351],{},[17,122644,92827],{},[17,122646,2279],{}," as the second value",[77,122649,6244],{"id":6243},[14,122651,122652],{},"There are a few good ways to fix this error.",[40,122654,122655,122661,122666,122669,122674],{},[43,122656,122657,122658,122660],{},"Check that the divisor is not ",[17,122659,2279],{}," before dividing",[43,122662,241,122663,122665],{},[17,122664,2736],{}," statements to handle the zero case",[43,122667,122668],{},"Validate user input before using it in math",[43,122670,6342,122671,122673],{},[17,122672,13235],{}," statements to inspect the divisor value",[43,122675,241,122676,122678],{},[17,122677,244],{}," only when you need error-handling logic",[14,122680,122681,122682,290],{},"In most beginner programs, the best fix is to prevent the division from happening when the divisor is ",[17,122683,2279],{},[14,122685,122686,122687,290],{},"If you are still learning how exceptions work, see ",[295,122688,1587],{"href":1749},[77,122690,122692],{"id":122691},"fix-method-1-check-before-dividing","Fix method 1: check before dividing",[14,122694,122695],{},"This is usually the best option when zero is expected sometimes.",[14,122697,122698],{},"It keeps the code clear and easy to read. It also lets you show a custom message or choose another result.",[91,122700,122702],{"className":93,"code":122701,"language":95,"meta":96,"style":96},"total = 50\ncount = 0\n\nif count != 0:\n    average = total \u002F count\n    print(average)\nelse:\n    print(\"Cannot calculate average because count is 0\")\n",[17,122703,122704,122712,122720,122724,122736,122749,122759,122765],{"__ignoreMap":96},[100,122705,122706,122708,122710],{"class":102,"line":103},[100,122707,53376],{"class":106},[100,122709,111],{"class":110},[100,122711,76942],{"class":122},[100,122713,122714,122716,122718],{"class":102,"line":135},[100,122715,2176],{"class":106},[100,122717,111],{"class":110},[100,122719,2069],{"class":122},[100,122721,122722],{"class":102,"line":142},[100,122723,139],{"emptyLinePlaceholder":138},[100,122725,122726,122728,122730,122732,122734],{"class":102,"line":152},[100,122727,2736],{"class":145},[100,122729,2190],{"class":106},[100,122731,116389],{"class":110},[100,122733,1921],{"class":122},[100,122735,149],{"class":118},[100,122737,122738,122741,122743,122745,122747],{"class":102,"line":164},[100,122739,122740],{"class":106},"    average ",[100,122742,111],{"class":110},[100,122744,2555],{"class":106},[100,122746,2558],{"class":110},[100,122748,101495],{"class":106},[100,122750,122751,122753,122755,122757],{"class":102,"line":185},[100,122752,200],{"class":114},[100,122754,170],{"class":118},[100,122756,122178],{"class":178},[100,122758,215],{"class":118},[100,122760,122761,122763],{"class":102,"line":197},[100,122762,4944],{"class":145},[100,122764,149],{"class":118},[100,122766,122767,122769,122771,122773,122776,122778],{"class":102,"line":771},[100,122768,200],{"class":114},[100,122770,170],{"class":118},[100,122772,206],{"class":205},[100,122774,122775],{"class":209},"Cannot calculate average because count is 0",[100,122777,206],{"class":205},[100,122779,215],{"class":118},[14,122781,122782],{},"This approach is useful because:",[40,122784,122785,122788,122791],{},[43,122786,122787],{},"it prevents the error before it happens",[43,122789,122790],{},"it makes your logic easy to understand",[43,122792,122793],{},"it lets you decide what should happen when zero is not allowed",[14,122795,122796],{},"You can also return a different value:",[91,122798,122800],{"className":93,"code":122799,"language":95,"meta":96,"style":96},"total = 50\ncount = 0\n\nif count != 0:\n    average = total \u002F count\nelse:\n    average = 0\n\nprint(average)\n",[17,122801,122802,122810,122818,122822,122834,122846,122852,122860,122864],{"__ignoreMap":96},[100,122803,122804,122806,122808],{"class":102,"line":103},[100,122805,53376],{"class":106},[100,122807,111],{"class":110},[100,122809,76942],{"class":122},[100,122811,122812,122814,122816],{"class":102,"line":135},[100,122813,2176],{"class":106},[100,122815,111],{"class":110},[100,122817,2069],{"class":122},[100,122819,122820],{"class":102,"line":142},[100,122821,139],{"emptyLinePlaceholder":138},[100,122823,122824,122826,122828,122830,122832],{"class":102,"line":152},[100,122825,2736],{"class":145},[100,122827,2190],{"class":106},[100,122829,116389],{"class":110},[100,122831,1921],{"class":122},[100,122833,149],{"class":118},[100,122835,122836,122838,122840,122842,122844],{"class":102,"line":164},[100,122837,122740],{"class":106},[100,122839,111],{"class":110},[100,122841,2555],{"class":106},[100,122843,2558],{"class":110},[100,122845,101495],{"class":106},[100,122847,122848,122850],{"class":102,"line":185},[100,122849,4944],{"class":145},[100,122851,149],{"class":118},[100,122853,122854,122856,122858],{"class":102,"line":197},[100,122855,122740],{"class":106},[100,122857,111],{"class":110},[100,122859,2069],{"class":122},[100,122861,122862],{"class":102,"line":771},[100,122863,139],{"emptyLinePlaceholder":138},[100,122865,122866,122868,122870,122872],{"class":102,"line":787},[100,122867,372],{"class":114},[100,122869,170],{"class":118},[100,122871,122178],{"class":178},[100,122873,215],{"class":118},[77,122875,122877],{"id":122876},"fix-method-2-handle-the-error-with-try-except","Fix method 2: handle the error with try-except",[14,122879,122880],{},"This method is useful when the divisor comes from uncertain input or another function.",[14,122882,122883],{},"You can catch the error and respond safely:",[91,122885,122887],{"className":93,"code":122886,"language":95,"meta":96,"style":96},"number = 10\ndivisor = 0\n\ntry:\n    print(number \u002F divisor)\nexcept ZeroDivisionError:\n    print(\"Cannot divide by zero\")\n",[17,122888,122889,122897,122905,122909,122915,122929,122937],{"__ignoreMap":96},[100,122890,122891,122893,122895],{"class":102,"line":103},[100,122892,1901],{"class":106},[100,122894,111],{"class":110},[100,122896,2181],{"class":122},[100,122898,122899,122901,122903],{"class":102,"line":135},[100,122900,122551],{"class":106},[100,122902,111],{"class":110},[100,122904,2069],{"class":122},[100,122906,122907],{"class":102,"line":142},[100,122908,139],{"emptyLinePlaceholder":138},[100,122910,122911,122913],{"class":102,"line":152},[100,122912,146],{"class":145},[100,122914,149],{"class":118},[100,122916,122917,122919,122921,122923,122925,122927],{"class":102,"line":164},[100,122918,200],{"class":114},[100,122920,170],{"class":118},[100,122922,1901],{"class":178},[100,122924,2558],{"class":110},[100,122926,122572],{"class":178},[100,122928,215],{"class":118},[100,122930,122931,122933,122935],{"class":102,"line":185},[100,122932,188],{"class":145},[100,122934,121522],{"class":191},[100,122936,149],{"class":118},[100,122938,122939,122941,122943,122945,122947,122949],{"class":102,"line":197},[100,122940,200],{"class":114},[100,122942,170],{"class":118},[100,122944,206],{"class":205},[100,122946,121040],{"class":209},[100,122948,206],{"class":205},[100,122950,215],{"class":118},[14,122952,218],{},[91,122954,122955],{"className":93,"code":121452,"language":95,"meta":96,"style":96},[17,122956,122957],{"__ignoreMap":96},[100,122958,122959],{"class":102,"line":103},[100,122960,121452],{"class":106},[14,122962,122963],{},"Use this when you need error-handling behavior.",[14,122965,122966,122967,122969],{},"Do not use it to hide logic mistakes. If your program should never use ",[17,122968,2279],{}," as a divisor, it is usually better to find out why that happened and fix the real cause.",[14,122971,122972,122973,3178,122975,290],{},"If you want more practice with this pattern, read ",[295,122974,66365],{"href":66364},[295,122976,1029],{"href":2824},[77,122978,122980],{"id":122979},"fix-method-3-validate-user-input","Fix method 3: validate user input",[14,122982,122983],{},"User input is a common cause of this error.",[14,122985,85669,122986,122988],{},[17,122987,2279],{},", you can ask again or stop the program before dividing.",[91,122990,122992],{"className":93,"code":122991,"language":95,"meta":96,"style":96},"numerator = 100\ndivisor = int(input(\"Enter a divisor: \"))\n\nif divisor == 0:\n    print(\"You must enter a number other than 0\")\nelse:\n    print(numerator \u002F divisor)\n",[17,122993,122994,123003,123026,123030,123043,123058,123064],{"__ignoreMap":96},[100,122995,122996,122999,123001],{"class":102,"line":103},[100,122997,122998],{"class":106},"numerator ",[100,123000,111],{"class":110},[100,123002,5377],{"class":122},[100,123004,123005,123007,123009,123011,123013,123015,123017,123019,123022,123024],{"class":102,"line":135},[100,123006,122551],{"class":106},[100,123008,111],{"class":110},[100,123010,3389],{"class":191},[100,123012,170],{"class":118},[100,123014,6210],{"class":114},[100,123016,170],{"class":118},[100,123018,206],{"class":205},[100,123020,123021],{"class":209},"Enter a divisor: ",[100,123023,206],{"class":205},[100,123025,182],{"class":118},[100,123027,123028],{"class":102,"line":142},[100,123029,139],{"emptyLinePlaceholder":138},[100,123031,123032,123034,123037,123039,123041],{"class":102,"line":152},[100,123033,2736],{"class":145},[100,123035,123036],{"class":106}," divisor ",[100,123038,32169],{"class":110},[100,123040,1921],{"class":122},[100,123042,149],{"class":118},[100,123044,123045,123047,123049,123051,123054,123056],{"class":102,"line":164},[100,123046,200],{"class":114},[100,123048,170],{"class":118},[100,123050,206],{"class":205},[100,123052,123053],{"class":209},"You must enter a number other than 0",[100,123055,206],{"class":205},[100,123057,215],{"class":118},[100,123059,123060,123062],{"class":102,"line":185},[100,123061,4944],{"class":145},[100,123063,149],{"class":118},[100,123065,123066,123068,123070,123072,123074,123076],{"class":102,"line":197},[100,123067,200],{"class":114},[100,123069,170],{"class":118},[100,123071,122998],{"class":178},[100,123073,2558],{"class":110},[100,123075,122572],{"class":178},[100,123077,215],{"class":118},[14,123079,123080,123081,123083,123084,290],{},"If the user types something that is not a valid integer, ",[17,123082,6119],{}," can raise a different error. You can learn more about that in ",[295,123085,123087],{"href":123086},"\u002Ferrors\u002Fvalueerror-when-parsing-input-in-python-fix\u002F","ValueError when parsing input in Python",[14,123089,123090],{},"You may also find these helpful:",[40,123092,123093,123097],{},[43,123094,123095],{},[295,123096,119390],{"href":85586},[43,123098,123099],{},[295,123100,81195],{"href":81194},[77,123102,3274],{"id":3273},[14,123104,123105],{},"If you are not sure where the zero came from, follow these steps.",[675,123107,123109],{"id":123108},"_1-find-the-exact-division-line","1. Find the exact division line",[14,123111,123112],{},"Read the traceback and locate the line that failed.",[14,123114,123115],{},"Look for the line using:",[40,123117,123118,123122,123126],{},[43,123119,123120],{},[17,123121,2558],{},[43,123123,123124],{},[17,123125,81351],{},[43,123127,123128],{},[17,123129,92827],{},[675,123131,123133],{"id":123132},"_2-check-the-value-on-the-right-side","2. Check the value on the right side",[14,123135,123136],{},"The right-side value is the divisor. That is the value you need to inspect.",[14,123138,580],{},[91,123140,123142],{"className":93,"code":123141,"language":95,"meta":96,"style":96},"numerator = 25\ndivisor = 5 - 5\n\nprint(numerator \u002F divisor)\n",[17,123143,123144,123152,123164,123168],{"__ignoreMap":96},[100,123145,123146,123148,123150],{"class":102,"line":103},[100,123147,122998],{"class":106},[100,123149,111],{"class":110},[100,123151,16667],{"class":122},[100,123153,123154,123156,123158,123160,123162],{"class":102,"line":135},[100,123155,122551],{"class":106},[100,123157,111],{"class":110},[100,123159,31164],{"class":122},[100,123161,1906],{"class":110},[100,123163,37580],{"class":122},[100,123165,123166],{"class":102,"line":142},[100,123167,139],{"emptyLinePlaceholder":138},[100,123169,123170,123172,123174,123176,123178,123180],{"class":102,"line":152},[100,123171,372],{"class":114},[100,123173,170],{"class":118},[100,123175,122998],{"class":178},[100,123177,2558],{"class":110},[100,123179,122572],{"class":178},[100,123181,215],{"class":118},[14,123183,123184,123185,123187,123188,290],{},"Here, the divisor is not written as ",[17,123186,2279],{},", but the calculation produces ",[17,123189,2279],{},[675,123191,123193],{"id":123192},"_3-print-values-before-the-failing-line","3. Print values before the failing line",[14,123195,123196],{},"Add debug prints before the division:",[91,123198,123200],{"className":93,"code":123199,"language":95,"meta":96,"style":96},"numerator = 25\ndivisor = 5 - 5\n\nprint(divisor)\nprint(type(divisor))\nprint(f\"numerator={numerator}, divisor={divisor}\")\n\nprint(numerator \u002F divisor)\n",[17,123201,123202,123210,123222,123226,123236,123250,123281,123285],{"__ignoreMap":96},[100,123203,123204,123206,123208],{"class":102,"line":103},[100,123205,122998],{"class":106},[100,123207,111],{"class":110},[100,123209,16667],{"class":122},[100,123211,123212,123214,123216,123218,123220],{"class":102,"line":135},[100,123213,122551],{"class":106},[100,123215,111],{"class":110},[100,123217,31164],{"class":122},[100,123219,1906],{"class":110},[100,123221,37580],{"class":122},[100,123223,123224],{"class":102,"line":142},[100,123225,139],{"emptyLinePlaceholder":138},[100,123227,123228,123230,123232,123234],{"class":102,"line":152},[100,123229,372],{"class":114},[100,123231,170],{"class":118},[100,123233,122475],{"class":178},[100,123235,215],{"class":118},[100,123237,123238,123240,123242,123244,123246,123248],{"class":102,"line":164},[100,123239,372],{"class":114},[100,123241,170],{"class":118},[100,123243,1250],{"class":191},[100,123245,170],{"class":118},[100,123247,122475],{"class":178},[100,123249,182],{"class":118},[100,123251,123252,123254,123256,123258,123261,123263,123266,123268,123271,123273,123275,123277,123279],{"class":102,"line":185},[100,123253,372],{"class":114},[100,123255,170],{"class":118},[100,123257,18199],{"class":1077},[100,123259,123260],{"class":209},"\"numerator=",[100,123262,16490],{"class":122},[100,123264,123265],{"class":178},"numerator",[100,123267,18216],{"class":122},[100,123269,123270],{"class":209},", divisor=",[100,123272,16490],{"class":122},[100,123274,122475],{"class":178},[100,123276,18216],{"class":122},[100,123278,206],{"class":209},[100,123280,215],{"class":118},[100,123282,123283],{"class":102,"line":197},[100,123284,139],{"emptyLinePlaceholder":138},[100,123286,123287,123289,123291,123293,123295,123297],{"class":102,"line":771},[100,123288,372],{"class":114},[100,123290,170],{"class":118},[100,123292,122998],{"class":178},[100,123294,2558],{"class":110},[100,123296,122572],{"class":178},[100,123298,215],{"class":118},[14,123300,5295],{},[91,123302,123304],{"className":93,"code":123303,"language":95,"meta":96,"style":96},"print(divisor)\nprint(type(divisor))\nprint(f\"numerator={numerator}, divisor={divisor}\")\nbreakpoint()\n",[17,123305,123306,123316,123330,123358],{"__ignoreMap":96},[100,123307,123308,123310,123312,123314],{"class":102,"line":103},[100,123309,372],{"class":114},[100,123311,170],{"class":118},[100,123313,122475],{"class":178},[100,123315,215],{"class":118},[100,123317,123318,123320,123322,123324,123326,123328],{"class":102,"line":135},[100,123319,372],{"class":114},[100,123321,170],{"class":118},[100,123323,1250],{"class":191},[100,123325,170],{"class":118},[100,123327,122475],{"class":178},[100,123329,182],{"class":118},[100,123331,123332,123334,123336,123338,123340,123342,123344,123346,123348,123350,123352,123354,123356],{"class":102,"line":142},[100,123333,372],{"class":114},[100,123335,170],{"class":118},[100,123337,18199],{"class":1077},[100,123339,123260],{"class":209},[100,123341,16490],{"class":122},[100,123343,123265],{"class":178},[100,123345,18216],{"class":122},[100,123347,123270],{"class":209},[100,123349,16490],{"class":122},[100,123351,122475],{"class":178},[100,123353,18216],{"class":122},[100,123355,206],{"class":209},[100,123357,215],{"class":118},[100,123359,123360,123362],{"class":102,"line":152},[100,123361,121954],{"class":114},[100,123363,4734],{"class":118},[675,123365,123367],{"id":123366},"_4-trace-where-the-divisor-was-assigned","4. Trace where the divisor was assigned",[14,123369,123370],{},"If the divisor comes from another part of the program, go back and check:",[40,123372,123373,123376,123381,123384],{},[43,123374,123375],{},"where the variable was assigned",[43,123377,123378,123379],{},"whether a function returned ",[17,123380,2279],{},[43,123382,123383],{},"whether a loop changed the value",[43,123385,123386,123387],{},"whether user input was converted into ",[17,123388,2279],{},[14,123390,123391],{},"Example with a function:",[91,123393,123395],{"className":93,"code":123394,"language":95,"meta":96,"style":96},"def get_divisor():\n    return 0\n\nnumber = 10\ndivisor = get_divisor()\n\nprint(number \u002F divisor)\n",[17,123396,123397,123406,123412,123416,123424,123434,123438],{"__ignoreMap":96},[100,123398,123399,123401,123404],{"class":102,"line":103},[100,123400,1078],{"class":1077},[100,123402,123403],{"class":1081}," get_divisor",[100,123405,1085],{"class":118},[100,123407,123408,123410],{"class":102,"line":135},[100,123409,2552],{"class":145},[100,123411,2069],{"class":122},[100,123413,123414],{"class":102,"line":142},[100,123415,139],{"emptyLinePlaceholder":138},[100,123417,123418,123420,123422],{"class":102,"line":152},[100,123419,1901],{"class":106},[100,123421,111],{"class":110},[100,123423,2181],{"class":122},[100,123425,123426,123428,123430,123432],{"class":102,"line":164},[100,123427,122551],{"class":106},[100,123429,111],{"class":110},[100,123431,123403],{"class":178},[100,123433,4734],{"class":118},[100,123435,123436],{"class":102,"line":185},[100,123437,139],{"emptyLinePlaceholder":138},[100,123439,123440,123442,123444,123446,123448,123450],{"class":102,"line":197},[100,123441,372],{"class":114},[100,123443,170],{"class":118},[100,123445,1901],{"class":178},[100,123447,2558],{"class":110},[100,123449,122572],{"class":178},[100,123451,215],{"class":118},[14,123453,123454,123455,290],{},"In this case, the real issue is inside ",[17,123456,123457],{},"get_divisor()",[77,123459,123461],{"id":123460},"operators-that-can-raise-zerodivisionerror","Operators that can raise ZeroDivisionError",[14,123463,123464,123465,123467,123468,290],{},"These operators can all raise ",[17,123466,17653],{}," when the second value is ",[17,123469,2279],{},[675,123471,123473,123475],{"id":123472},"division",[17,123474,2558],{}," division",[91,123477,123478],{"className":93,"code":17673,"language":95,"meta":96,"style":96},[17,123479,123480],{"__ignoreMap":96},[100,123481,123482,123484,123486,123488,123490,123492],{"class":102,"line":103},[100,123483,372],{"class":114},[100,123485,170],{"class":118},[100,123487,356],{"class":122},[100,123489,17686],{"class":110},[100,123491,1921],{"class":122},[100,123493,215],{"class":118},[14,123495,28421],{},[91,123497,123498],{"className":93,"code":17695,"language":95,"meta":96,"style":96},[17,123499,123500],{"__ignoreMap":96},[100,123501,123502,123504,123506],{"class":102,"line":103},[100,123503,17653],{"class":191},[100,123505,89],{"class":118},[100,123507,17706],{"class":106},[675,123509,123511,123513],{"id":123510},"floor-division",[17,123512,81351],{}," floor division",[91,123515,123516],{"className":93,"code":121834,"language":95,"meta":96,"style":96},[17,123517,123518],{"__ignoreMap":96},[100,123519,123520,123522,123524,123526,123528,123530],{"class":102,"line":103},[100,123521,372],{"class":114},[100,123523,170],{"class":118},[100,123525,356],{"class":122},[100,123527,121847],{"class":110},[100,123529,1921],{"class":122},[100,123531,215],{"class":118},[14,123533,28421],{},[91,123535,123537],{"className":93,"code":123536,"language":95,"meta":96,"style":96},"ZeroDivisionError: integer division or modulo by zero\n",[17,123538,123539],{"__ignoreMap":96},[100,123540,123541,123543,123545,123548,123550],{"class":102,"line":103},[100,123542,17653],{"class":191},[100,123544,89],{"class":118},[100,123546,123547],{"class":106}," integer division ",[100,123549,11841],{"class":110},[100,123551,123552],{"class":106}," modulo by zero\n",[675,123554,123556,123558],{"id":123555},"modulo",[17,123557,92827],{}," modulo",[91,123560,123561],{"className":93,"code":121859,"language":95,"meta":96,"style":96},[17,123562,123563],{"__ignoreMap":96},[100,123564,123565,123567,123569,123571,123573,123575],{"class":102,"line":103},[100,123566,372],{"class":114},[100,123568,170],{"class":118},[100,123570,356],{"class":122},[100,123572,121872],{"class":110},[100,123574,1921],{"class":122},[100,123576,215],{"class":118},[14,123578,28421],{},[91,123580,123582],{"className":93,"code":123581,"language":95,"meta":96,"style":96},"ZeroDivisionError: integer modulo by zero\n",[17,123583,123584],{"__ignoreMap":96},[100,123585,123586,123588,123590],{"class":102,"line":103},[100,123587,17653],{"class":191},[100,123589,89],{"class":118},[100,123591,123592],{"class":106}," integer modulo by zero\n",[77,123594,123596],{"id":123595},"related-beginner-questions","Related beginner questions",[14,123598,123599,123600,123603,123604,123607],{},"A common question is: what is the difference between ",[22,123601,123602],{},"preventing"," the error and ",[22,123605,123606],{},"catching"," the error?",[40,123609,123610,123619],{},[43,123611,123612,123615,123616],{},[22,123613,123614],{},"Preventing the error"," means checking the divisor first with ",[17,123617,123618],{},"if divisor != 0",[43,123620,123621,123624,123625,123627,123628,681,123630],{},[22,123622,123623],{},"Catching the error"," means letting the division happen inside ",[17,123626,146],{},", then handling ",[17,123629,17653],{},[17,123631,188],{},[14,123633,123634],{},"For most simple programs, preventing the error is clearer.",[14,123636,123637],{},"Another common question is why Python raises an error instead of returning infinity. Python does this because division by zero is usually a mistake in program logic, and raising an error helps you find and fix it.",[14,123639,123640],{},"You may also need to decide what your program should return when zero is not allowed. That depends on the task. Common choices are:",[40,123642,123643,123646,123649,123652],{},[43,123644,123645],{},"show an error message",[43,123647,123648],{},"ask the user again",[43,123650,123651],{},"return a default value",[43,123653,123654],{},"stop the current calculation",[77,123656,1514],{"id":1513},[675,123658,123660],{"id":123659},"can-zerodivisionerror-happen-with-float-values","Can ZeroDivisionError happen with float values?",[14,123662,123663,123664,123667,123668,290],{},"Yes. If the divisor is ",[17,123665,123666],{},"0.0",", Python still raises ",[17,123669,17653],{},[91,123671,123673],{"className":93,"code":123672,"language":95,"meta":96,"style":96},"print(5 \u002F 0.0)\n",[17,123674,123675],{"__ignoreMap":96},[100,123676,123677,123679,123681,123683,123685,123688],{"class":102,"line":103},[100,123678,372],{"class":114},[100,123680,170],{"class":118},[100,123682,3557],{"class":122},[100,123684,17686],{"class":110},[100,123686,123687],{"class":122}," 0.0",[100,123689,215],{"class":118},[675,123691,123693],{"id":123692},"does-modulo-by-zero-also-raise-this-error","Does modulo by zero also raise this error?",[14,123695,122207,123696,42154,123698,20442,123700,290],{},[17,123697,92827],{},[17,123699,2279],{},[17,123701,17653],{},[91,123703,123705],{"className":93,"code":123704,"language":95,"meta":96,"style":96},"print(7 % 0)\n",[17,123706,123707],{"__ignoreMap":96},[100,123708,123709,123711,123713,123716,123718,123720],{"class":102,"line":103},[100,123710,372],{"class":114},[100,123712,170],{"class":118},[100,123714,123715],{"class":122},"7",[100,123717,121872],{"class":110},[100,123719,1921],{"class":122},[100,123721,215],{"class":118},[675,123723,123725],{"id":123724},"should-i-always-use-try-except-for-this","Should I always use try-except for this?",[14,123727,123728],{},"No. If you can check the divisor first, that is usually simpler and clearer.",[675,123730,123732],{"id":123731},"how-do-i-find-where-the-zero-came-from","How do I find where the zero came from?",[14,123734,123735],{},"Read the traceback, inspect the divisor variable, and trace where it was assigned or returned.",[77,123737,1554],{"id":1553},[40,123739,123740,123744,123748,123752,123756,123760],{},[43,123741,123742],{},[295,123743,1581],{"href":2824},[43,123745,123746],{},[295,123747,67731],{"href":66364},[43,123749,123750],{},[295,123751,1587],{"href":1749},[43,123753,123754],{},[295,123755,119390],{"href":85586},[43,123757,123758],{},[295,123759,81195],{"href":81194},[43,123761,123762],{},[295,123763,123087],{"href":123086},[1589,123765,123766],{},"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 .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 .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 .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 .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 .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}",{"title":96,"searchDepth":135,"depth":135,"links":123768},[123769,123770,123771,123772,123773,123774,123775,123776,123777,123783,123791,123792,123798],{"id":79,"depth":135,"text":80},{"id":122445,"depth":135,"text":122446},{"id":11030,"depth":135,"text":11031},{"id":122577,"depth":135,"text":122578},{"id":6243,"depth":135,"text":6244},{"id":122691,"depth":135,"text":122692},{"id":122876,"depth":135,"text":122877},{"id":122979,"depth":135,"text":122980},{"id":3273,"depth":135,"text":3274,"children":123778},[123779,123780,123781,123782],{"id":123108,"depth":142,"text":123109},{"id":123132,"depth":142,"text":123133},{"id":123192,"depth":142,"text":123193},{"id":123366,"depth":142,"text":123367},{"id":123460,"depth":135,"text":123461,"children":123784},[123785,123787,123789],{"id":123472,"depth":142,"text":123786},"\u002F division",{"id":123510,"depth":142,"text":123788},"\u002F\u002F floor division",{"id":123555,"depth":142,"text":123790},"% modulo",{"id":123595,"depth":135,"text":123596},{"id":1513,"depth":135,"text":1514,"children":123793},[123794,123795,123796,123797],{"id":123659,"depth":142,"text":123660},{"id":123692,"depth":142,"text":123693},{"id":123724,"depth":142,"text":123725},{"id":123731,"depth":142,"text":123732},{"id":1553,"depth":135,"text":1554},"Master zerodivisionerror in python causes and fixes in our comprehensive Python beginner guide.",{},"\u002Ferrors\u002Fzerodivisionerror-in-python-causes-and-fixes",{"title":122306,"description":123799},"errors\u002Fzerodivisionerror-in-python-causes-and-fixes","iohpEufyF8UDR5WUc0LcvF73jxRWST6gvYH0CZ8onJQ",1776944935659]