[{"data":1,"prerenderedAt":1451},["ShallowReactive",2],{"doc-\u002Ferrors\u002Fvalueerror-in-python-causes-and-fixes":3},{"id":4,"title":5,"body":6,"description":1444,"extension":1445,"meta":1446,"navigation":77,"path":1447,"seo":1448,"stem":1449,"__hash__":1450},"content\u002Ferrors\u002Fvalueerror-in-python-causes-and-fixes.md","ValueError in Python: Causes and Fixes",{"type":7,"value":8,"toc":1416},"minimark",[9,13,21,24,34,39,142,151,155,161,171,174,195,201,220,226,238,249,253,259,294,297,322,326,329,337,381,384,427,430,445,452,495,497,528,530,544,551,555,561,564,587,591,643,645,654,659,663,803,806,817,821,827,849,852,920,928,934,942,944,993,996,1023,1026,1030,1033,1045,1047,1104,1107,1130,1137,1141,1146,1149,1163,1165,1257,1260,1263,1266,1320,1323,1326,1333,1337,1341,1349,1353,1359,1363,1366,1370,1376,1380,1412],[10,11,5],"h1",{"id":12},"valueerror-in-python-causes-and-fixes",[14,15,16,20],"p",{},[17,18,19],"code",{},"ValueError"," is a common Python error for beginners.",[14,22,23],{},"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,25,26,27,30,31,33],{},"For example, ",[17,28,29],{},"int()"," can convert strings to integers. But if the string does not contain a valid whole number, Python raises ",[17,32,19],{},".",[35,36,38],"h2",{"id":37},"quick-fix","Quick fix",[40,41,46],"pre",{"className":42,"code":43,"language":44,"meta":45,"style":45},"language-python shiki shiki-themes material-theme-lighter github-light github-dark","text = \"abc\"\n\ntry:\n    number = int(text)\nexcept ValueError:\n    print(\"Input must be a valid number\")\n","python","",[17,47,48,72,79,90,112,123],{"__ignoreMap":45},[49,50,53,57,61,65,69],"span",{"class":51,"line":52},"line",1,[49,54,56],{"class":55},"su5hD","text ",[49,58,60],{"class":59},"smGrS","=",[49,62,64],{"class":63},"sjJ54"," \"",[49,66,68],{"class":67},"s_sjI","abc",[49,70,71],{"class":63},"\"\n",[49,73,75],{"class":51,"line":74},2,[49,76,78],{"emptyLinePlaceholder":77},true,"\n",[49,80,82,86],{"class":51,"line":81},3,[49,83,85],{"class":84},"sVHd0","try",[49,87,89],{"class":88},"sP7_E",":\n",[49,91,93,96,98,102,105,109],{"class":51,"line":92},4,[49,94,95],{"class":55},"    number ",[49,97,60],{"class":59},[49,99,101],{"class":100},"sZMiF"," int",[49,103,104],{"class":88},"(",[49,106,108],{"class":107},"slqww","text",[49,110,111],{"class":88},")\n",[49,113,115,118,121],{"class":51,"line":114},5,[49,116,117],{"class":84},"except",[49,119,120],{"class":100}," ValueError",[49,122,89],{"class":88},[49,124,126,130,132,135,138,140],{"class":51,"line":125},6,[49,127,129],{"class":128},"sptTA","    print",[49,131,104],{"class":88},[49,133,134],{"class":63},"\"",[49,136,137],{"class":67},"Input must be a valid number",[49,139,134],{"class":63},[49,141,111],{"class":88},[14,143,144,145,148,149,33],{},"Use ",[17,146,147],{},"try-except"," when a value might have the right type but the wrong content, such as converting non-numeric text with ",[17,150,29],{},[35,152,154],{"id":153},"what-valueerror-means","What ValueError means",[14,156,157,158,160],{},"A ",[17,159,19],{}," happens when:",[162,163,164,168],"ul",{},[165,166,167],"li",{},"Python gets the kind of object it expects",[165,169,170],{},"But the value inside that object is not acceptable",[14,172,173],{},"Example:",[40,175,177],{"className":42,"code":176,"language":44,"meta":45,"style":45},"int(\"hello\")\n",[17,178,179],{"__ignoreMap":45},[49,180,181,184,186,188,191,193],{"class":51,"line":52},[49,182,183],{"class":100},"int",[49,185,104],{"class":88},[49,187,134],{"class":63},[49,189,190],{"class":67},"hello",[49,192,134],{"class":63},[49,194,111],{"class":88},[14,196,197,198,200],{},"This raises ",[17,199,19],{}," because:",[162,202,203,209,214],{},[165,204,205,208],{},[17,206,207],{},"\"hello\""," is a string",[165,210,211,213],{},[17,212,29],{}," accepts strings",[165,215,216,217,219],{},"But ",[17,218,207],{}," is not a valid integer",[14,221,222,223,33],{},"This is different from ",[17,224,225],{},"TypeError",[162,227,228,233],{},[165,229,230,232],{},[17,231,19],{},": right type, wrong value",[165,234,235,237],{},[17,236,225],{},": wrong type for the operation",[14,239,240,241,243,244,33],{},"If you are specifically seeing an ",[17,242,29],{}," conversion error, see ",[245,246,248],"a",{"href":247},"\u002Ferrors\u002Fvalueerror-invalid-literal-for-int-with-base-10-fix\u002F","ValueError: invalid literal for int() with base 10",[35,250,252],{"id":251},"common-places-beginners-see-valueerror","Common places beginners see ValueError",[14,254,255,256,258],{},"Beginners often run into ",[17,257,19],{}," in these situations:",[162,260,261,276,279,282,291],{},[165,262,263,264,269,270],{},"Converting text to numbers with ",[245,265,267],{"href":266},"\u002Freference\u002Fpython-int-function-explained\u002F",[17,268,29],{}," or ",[245,271,273],{"href":272},"\u002Freference\u002Fpython-float-function-explained\u002F",[17,274,275],{},"float()",[165,277,278],{},"Unpacking the wrong number of values",[165,280,281],{},"Using string methods with invalid input",[165,283,284,285,269,288],{},"Searching for missing items with methods like ",[17,286,287],{},"str.index()",[17,289,290],{},"tuple.index()",[165,292,293],{},"Passing invalid values to math-related functions",[14,295,296],{},"Common examples include:",[162,298,299,304,309,312,319],{},[165,300,301,302],{},"Trying to convert non-numeric text with ",[17,303,29],{},[165,305,306,307],{},"Trying to convert badly formatted text with ",[17,308,275],{},[165,310,311],{},"Unpacking too many or too few values",[165,313,314,315,318],{},"Using ",[17,316,317],{},"index()"," for a value that is not present",[165,320,321],{},"Passing invalid values to library functions",[35,323,325],{"id":324},"example-that-causes-valueerror","Example that causes ValueError",[14,327,328],{},"Here are two short examples.",[330,331,333,334],"h3",{"id":332},"example-1-intabc","Example 1: ",[17,335,336],{},"int(\"abc\")",[40,338,340],{"className":42,"code":339,"language":44,"meta":45,"style":45},"text = \"abc\"\nnumber = int(text)\nprint(number)\n",[17,341,342,354,369],{"__ignoreMap":45},[49,343,344,346,348,350,352],{"class":51,"line":52},[49,345,56],{"class":55},[49,347,60],{"class":59},[49,349,64],{"class":63},[49,351,68],{"class":67},[49,353,71],{"class":63},[49,355,356,359,361,363,365,367],{"class":51,"line":74},[49,357,358],{"class":55},"number ",[49,360,60],{"class":59},[49,362,101],{"class":100},[49,364,104],{"class":88},[49,366,108],{"class":107},[49,368,111],{"class":88},[49,370,371,374,376,379],{"class":51,"line":81},[49,372,373],{"class":128},"print",[49,375,104],{"class":88},[49,377,378],{"class":107},"number",[49,380,111],{"class":88},[14,382,383],{},"Output:",[40,385,387],{"className":42,"code":386,"language":44,"meta":45,"style":45},"ValueError: invalid literal for int() with base 10: 'abc'\n",[17,388,389],{"__ignoreMap":45},[49,390,391,393,396,399,402,404,407,410,413,417,419,422,424],{"class":51,"line":52},[49,392,19],{"class":100},[49,394,395],{"class":88},":",[49,397,398],{"class":55}," invalid literal ",[49,400,401],{"class":84},"for",[49,403,101],{"class":100},[49,405,406],{"class":88},"()",[49,408,409],{"class":84}," with",[49,411,412],{"class":55}," base ",[49,414,416],{"class":415},"srdBf","10",[49,418,395],{"class":88},[49,420,421],{"class":63}," '",[49,423,68],{"class":67},[49,425,426],{"class":63},"'\n",[14,428,429],{},"Why this fails:",[162,431,432,436,440],{},[165,433,434,208],{},[17,435,108],{},[165,437,438,213],{},[17,439,29],{},[165,441,216,442,219],{},[17,443,444],{},"\"abc\"",[330,446,448,449],{"id":447},"example-2-float125","Example 2: ",[17,450,451],{},"float(\"12,5\")",[40,453,455],{"className":42,"code":454,"language":44,"meta":45,"style":45},"text = \"12,5\"\nnumber = float(text)\nprint(number)\n",[17,456,457,470,485],{"__ignoreMap":45},[49,458,459,461,463,465,468],{"class":51,"line":52},[49,460,56],{"class":55},[49,462,60],{"class":59},[49,464,64],{"class":63},[49,466,467],{"class":67},"12,5",[49,469,71],{"class":63},[49,471,472,474,476,479,481,483],{"class":51,"line":74},[49,473,358],{"class":55},[49,475,60],{"class":59},[49,477,478],{"class":100}," float",[49,480,104],{"class":88},[49,482,108],{"class":107},[49,484,111],{"class":88},[49,486,487,489,491,493],{"class":51,"line":81},[49,488,373],{"class":128},[49,490,104],{"class":88},[49,492,378],{"class":107},[49,494,111],{"class":88},[14,496,383],{},[40,498,500],{"className":42,"code":499,"language":44,"meta":45,"style":45},"ValueError: could not convert string to float: '12,5'\n",[17,501,502],{"__ignoreMap":45},[49,503,504,506,508,511,514,517,520,522,524,526],{"class":51,"line":52},[49,505,19],{"class":100},[49,507,395],{"class":88},[49,509,510],{"class":55}," could ",[49,512,513],{"class":59},"not",[49,515,516],{"class":55}," convert string to ",[49,518,519],{"class":100},"float",[49,521,395],{"class":88},[49,523,421],{"class":63},[49,525,467],{"class":67},[49,527,426],{"class":63},[14,529,429],{},[162,531,532,538],{},[165,533,534,535],{},"Python expects decimal numbers like ",[17,536,537],{},"\"12.5\"",[165,539,540,543],{},[17,541,542],{},"\"12,5\""," uses a comma instead of a dot",[14,545,546,547,33],{},"If this is your exact problem, see ",[245,548,550],{"href":549},"\u002Ferrors\u002Fvalueerror-could-not-convert-string-to-float-fix\u002F","ValueError: could not convert string to float",[35,552,554],{"id":553},"how-to-fix-valueerror","How to fix ValueError",[14,556,557,558,560],{},"There is no single fix for every ",[17,559,19],{},". The correct fix depends on the value that caused the problem.",[14,562,563],{},"Common ways to fix it:",[162,565,566,569,576,579,584],{},[165,567,568],{},"Check the actual value before passing it to a function",[165,570,571,572,575],{},"Clean user input with ",[17,573,574],{},"strip()"," before conversion",[165,577,578],{},"Validate data before unpacking or parsing",[165,580,144,581,583],{},[17,582,147],{}," when bad input is possible",[165,585,586],{},"Read the full error message carefully",[330,588,590],{"id":589},"clean-input-before-converting","Clean input before converting",[40,592,594],{"className":42,"code":593,"language":44,"meta":45,"style":45},"text = \" 42 \\n\"\nnumber = int(text.strip())\nprint(number)\n",[17,595,596,613,633],{"__ignoreMap":45},[49,597,598,600,602,604,607,611],{"class":51,"line":52},[49,599,56],{"class":55},[49,601,60],{"class":59},[49,603,64],{"class":63},[49,605,606],{"class":67}," 42 ",[49,608,610],{"class":609},"s_hVV","\\n",[49,612,71],{"class":63},[49,614,615,617,619,621,623,625,627,630],{"class":51,"line":74},[49,616,358],{"class":55},[49,618,60],{"class":59},[49,620,101],{"class":100},[49,622,104],{"class":88},[49,624,108],{"class":107},[49,626,33],{"class":88},[49,628,629],{"class":107},"strip",[49,631,632],{"class":88},"())\n",[49,634,635,637,639,641],{"class":51,"line":81},[49,636,373],{"class":128},[49,638,104],{"class":88},[49,640,378],{"class":107},[49,642,111],{"class":88},[14,644,383],{},[40,646,648],{"className":42,"code":647,"language":44,"meta":45,"style":45},"42\n",[17,649,650],{"__ignoreMap":45},[49,651,652],{"class":51,"line":52},[49,653,647],{"class":415},[14,655,656,658],{},[17,657,574],{}," removes extra spaces and newline characters from the start and end of the string.",[330,660,662],{"id":661},"validate-before-unpacking","Validate before unpacking",[40,664,666],{"className":42,"code":665,"language":44,"meta":45,"style":45},"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,667,668,682,686,712,716,740,755,767,779,787],{"__ignoreMap":45},[49,669,670,673,675,677,680],{"class":51,"line":52},[49,671,672],{"class":55},"data ",[49,674,60],{"class":59},[49,676,64],{"class":63},[49,678,679],{"class":67},"Alice,25",[49,681,71],{"class":63},[49,683,684],{"class":51,"line":74},[49,685,78],{"emptyLinePlaceholder":77},[49,687,688,691,693,696,698,701,703,705,708,710],{"class":51,"line":81},[49,689,690],{"class":55},"parts ",[49,692,60],{"class":59},[49,694,695],{"class":55}," data",[49,697,33],{"class":88},[49,699,700],{"class":107},"split",[49,702,104],{"class":88},[49,704,134],{"class":63},[49,706,707],{"class":67},",",[49,709,134],{"class":63},[49,711,111],{"class":88},[49,713,714],{"class":51,"line":92},[49,715,78],{"emptyLinePlaceholder":77},[49,717,718,721,724,726,729,732,735,738],{"class":51,"line":114},[49,719,720],{"class":84},"if",[49,722,723],{"class":128}," len",[49,725,104],{"class":88},[49,727,728],{"class":107},"parts",[49,730,731],{"class":88},")",[49,733,734],{"class":59}," ==",[49,736,737],{"class":415}," 2",[49,739,89],{"class":88},[49,741,742,745,747,750,752],{"class":51,"line":125},[49,743,744],{"class":55},"    name",[49,746,707],{"class":88},[49,748,749],{"class":55}," age ",[49,751,60],{"class":59},[49,753,754],{"class":55}," parts\n",[49,756,758,760,762,765],{"class":51,"line":757},7,[49,759,129],{"class":128},[49,761,104],{"class":88},[49,763,764],{"class":107},"name",[49,766,111],{"class":88},[49,768,770,772,774,777],{"class":51,"line":769},8,[49,771,129],{"class":128},[49,773,104],{"class":88},[49,775,776],{"class":107},"age",[49,778,111],{"class":88},[49,780,782,785],{"class":51,"line":781},9,[49,783,784],{"class":84},"else",[49,786,89],{"class":88},[49,788,790,792,794,796,799,801],{"class":51,"line":789},10,[49,791,129],{"class":128},[49,793,104],{"class":88},[49,795,134],{"class":63},[49,797,798],{"class":67},"Input must contain exactly two values",[49,800,134],{"class":63},[49,802,111],{"class":88},[14,804,805],{},"This helps prevent unpacking errors when the number of values is wrong.",[14,807,808,809,269,813,33],{},"If you need more help with conversion, see ",[245,810,812],{"href":811},"\u002Fhow-to\u002Fhow-to-convert-string-to-int-in-python\u002F","how to convert a string to int in Python",[245,814,816],{"href":815},"\u002Fhow-to\u002Fhow-to-convert-string-to-float-in-python\u002F","how to convert a string to float in Python",[35,818,820],{"id":819},"step-by-step-debugging-checklist","Step-by-step debugging checklist",[14,822,823,824,826],{},"When you get a ",[17,825,19],{},", use this checklist:",[828,829,830,833,836,843,846],"ol",{},[165,831,832],{},"Find the exact line named in the traceback",[165,834,835],{},"Print the value that caused the error",[165,837,838,839,842],{},"Check what ",[17,840,841],{},"type(value)"," returns",[165,844,845],{},"Check whether the value format matches what the function expects",[165,847,848],{},"Add validation or exception handling if needed",[14,850,851],{},"Useful debugging commands:",[40,853,855],{"className":42,"code":854,"language":44,"meta":45,"style":45},"print(value)\nprint(type(value))\nprint(repr(value))\nhelp(int)\nhelp(float)\n",[17,856,857,868,884,899,910],{"__ignoreMap":45},[49,858,859,861,863,866],{"class":51,"line":52},[49,860,373],{"class":128},[49,862,104],{"class":88},[49,864,865],{"class":107},"value",[49,867,111],{"class":88},[49,869,870,872,874,877,879,881],{"class":51,"line":74},[49,871,373],{"class":128},[49,873,104],{"class":88},[49,875,876],{"class":100},"type",[49,878,104],{"class":88},[49,880,865],{"class":107},[49,882,883],{"class":88},"))\n",[49,885,886,888,890,893,895,897],{"class":51,"line":81},[49,887,373],{"class":128},[49,889,104],{"class":88},[49,891,892],{"class":128},"repr",[49,894,104],{"class":88},[49,896,865],{"class":107},[49,898,883],{"class":88},[49,900,901,904,906,908],{"class":51,"line":92},[49,902,903],{"class":128},"help",[49,905,104],{"class":88},[49,907,183],{"class":100},[49,909,111],{"class":88},[49,911,912,914,916,918],{"class":51,"line":114},[49,913,903],{"class":128},[49,915,104],{"class":88},[49,917,519],{"class":100},[49,919,111],{"class":88},[330,921,923,924,927],{"id":922},"why-repr-is-useful","Why ",[17,925,926],{},"repr()"," is useful",[14,929,930,933],{},[17,931,932],{},"print(value)"," shows the value in a normal way.",[14,935,936,939,940,33],{},[17,937,938],{},"print(repr(value))"," shows hidden characters too, such as spaces or ",[17,941,610],{},[14,943,173],{},[40,945,947],{"className":42,"code":946,"language":44,"meta":45,"style":45},"value = \"42\\n\"\n\nprint(value)\nprint(repr(value))\n",[17,948,949,965,969,979],{"__ignoreMap":45},[49,950,951,954,956,958,961,963],{"class":51,"line":52},[49,952,953],{"class":55},"value ",[49,955,60],{"class":59},[49,957,64],{"class":63},[49,959,960],{"class":67},"42",[49,962,610],{"class":609},[49,964,71],{"class":63},[49,966,967],{"class":51,"line":74},[49,968,78],{"emptyLinePlaceholder":77},[49,970,971,973,975,977],{"class":51,"line":81},[49,972,373],{"class":128},[49,974,104],{"class":88},[49,976,865],{"class":107},[49,978,111],{"class":88},[49,980,981,983,985,987,989,991],{"class":51,"line":92},[49,982,373],{"class":128},[49,984,104],{"class":88},[49,986,892],{"class":128},[49,988,104],{"class":88},[49,990,865],{"class":107},[49,992,883],{"class":88},[14,994,995],{},"Possible output:",[40,997,999],{"className":42,"code":998,"language":44,"meta":45,"style":45},"42\n\n'42\\n'\n",[17,1000,1001,1005,1009],{"__ignoreMap":45},[49,1002,1003],{"class":51,"line":52},[49,1004,647],{"class":415},[49,1006,1007],{"class":51,"line":74},[49,1008,78],{"emptyLinePlaceholder":77},[49,1010,1011,1015,1018,1021],{"class":51,"line":81},[49,1012,1014],{"class":1013},"s2W-s","'",[49,1016,960],{"class":1017},"sithA",[49,1019,610],{"class":1020},"sfi6f",[49,1022,426],{"class":1013},[14,1024,1025],{},"This helps you notice formatting problems that may be causing the error.",[35,1027,1029],{"id":1028},"valueerror-vs-typeerror","ValueError vs TypeError",[14,1031,1032],{},"The difference is simple:",[162,1034,1035,1040],{},[165,1036,1037,1039],{},[17,1038,19],{},": the type is acceptable, but the value is not",[165,1041,1042,1044],{},[17,1043,225],{},": the type itself is wrong",[14,1046,173],{},[40,1048,1050],{"className":42,"code":1049,"language":44,"meta":45,"style":45},"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,1051,1052,1076,1080,1085,1090,1094,1099],{"__ignoreMap":45},[49,1053,1054,1056,1058,1060,1062,1064,1067,1069,1072],{"class":51,"line":52},[49,1055,373],{"class":128},[49,1057,104],{"class":88},[49,1059,183],{"class":100},[49,1061,104],{"class":88},[49,1063,134],{"class":63},[49,1065,1066],{"class":67},"123",[49,1068,134],{"class":63},[49,1070,1071],{"class":88},"))",[49,1073,1075],{"class":1074},"sutJx","   # works\n",[49,1077,1078],{"class":51,"line":74},[49,1079,78],{"emptyLinePlaceholder":77},[49,1081,1082],{"class":51,"line":81},[49,1083,1084],{"class":1074},"# ValueError: string is allowed, but the content is invalid\n",[49,1086,1087],{"class":51,"line":92},[49,1088,1089],{"class":1074},"# int(\"abc\")\n",[49,1091,1092],{"class":51,"line":114},[49,1093,78],{"emptyLinePlaceholder":77},[49,1095,1096],{"class":51,"line":125},[49,1097,1098],{"class":1074},"# TypeError: None is not a valid type here\n",[49,1100,1101],{"class":51,"line":757},[49,1102,1103],{"class":1074},"# int(None)\n",[14,1105,1106],{},"Another way to think about it:",[162,1108,1109,1119],{},[165,1110,1111,1113,1114,1116,1117],{},[17,1112,444],{}," is still a string, and ",[17,1115,29],{}," accepts strings, so this becomes ",[17,1118,19],{},[165,1120,1121,1124,1125,1127,1128],{},[17,1122,1123],{},"None"," is not a string, number, or compatible value for ",[17,1126,29],{}," in this context, so this becomes ",[17,1129,225],{},[14,1131,1132,1133,33],{},"For a full comparison, see ",[245,1134,1136],{"href":1135},"\u002Ferrors\u002Ftypeerror-vs-valueerror-in-python-explained\u002F","TypeError vs ValueError in Python explained",[35,1138,1140],{"id":1139},"when-to-use-try-except","When to use try-except",[14,1142,144,1143,1145],{},[17,1144,147],{}," when invalid values are possible and normal in your program.",[14,1147,1148],{},"Good examples:",[162,1150,1151,1154,1157,1160],{},[165,1152,1153],{},"User input",[165,1155,1156],{},"File content",[165,1158,1159],{},"Data from APIs",[165,1161,1162],{},"Data from external sources",[14,1164,173],{},[40,1166,1168],{"className":42,"code":1167,"language":44,"meta":45,"style":45},"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,1169,1170,1190,1194,1200,1214,1234,1242],{"__ignoreMap":45},[49,1171,1172,1174,1176,1179,1181,1183,1186,1188],{"class":51,"line":52},[49,1173,56],{"class":55},[49,1175,60],{"class":59},[49,1177,1178],{"class":128}," input",[49,1180,104],{"class":88},[49,1182,134],{"class":63},[49,1184,1185],{"class":67},"Enter a whole number: ",[49,1187,134],{"class":63},[49,1189,111],{"class":88},[49,1191,1192],{"class":51,"line":74},[49,1193,78],{"emptyLinePlaceholder":77},[49,1195,1196,1198],{"class":51,"line":81},[49,1197,85],{"class":84},[49,1199,89],{"class":88},[49,1201,1202,1204,1206,1208,1210,1212],{"class":51,"line":92},[49,1203,95],{"class":55},[49,1205,60],{"class":59},[49,1207,101],{"class":100},[49,1209,104],{"class":88},[49,1211,108],{"class":107},[49,1213,111],{"class":88},[49,1215,1216,1218,1220,1222,1225,1227,1229,1232],{"class":51,"line":114},[49,1217,129],{"class":128},[49,1219,104],{"class":88},[49,1221,134],{"class":63},[49,1223,1224],{"class":67},"You entered:",[49,1226,134],{"class":63},[49,1228,707],{"class":88},[49,1230,1231],{"class":107}," number",[49,1233,111],{"class":88},[49,1235,1236,1238,1240],{"class":51,"line":125},[49,1237,117],{"class":84},[49,1239,120],{"class":100},[49,1241,89],{"class":88},[49,1243,1244,1246,1248,1250,1253,1255],{"class":51,"line":757},[49,1245,129],{"class":128},[49,1247,104],{"class":88},[49,1249,134],{"class":63},[49,1251,1252],{"class":67},"That is not a valid whole number",[49,1254,134],{"class":63},[49,1256,111],{"class":88},[14,1258,1259],{},"This is useful because users may enter unexpected text.",[14,1261,1262],{},"Do not hide the error silently.",[14,1264,1265],{},"Bad example:",[40,1267,1269],{"className":42,"code":1268,"language":44,"meta":45,"style":45},"text = \"abc\"\n\ntry:\n    number = int(text)\nexcept ValueError:\n    pass\n",[17,1270,1271,1283,1287,1293,1307,1315],{"__ignoreMap":45},[49,1272,1273,1275,1277,1279,1281],{"class":51,"line":52},[49,1274,56],{"class":55},[49,1276,60],{"class":59},[49,1278,64],{"class":63},[49,1280,68],{"class":67},[49,1282,71],{"class":63},[49,1284,1285],{"class":51,"line":74},[49,1286,78],{"emptyLinePlaceholder":77},[49,1288,1289,1291],{"class":51,"line":81},[49,1290,85],{"class":84},[49,1292,89],{"class":88},[49,1294,1295,1297,1299,1301,1303,1305],{"class":51,"line":92},[49,1296,95],{"class":55},[49,1298,60],{"class":59},[49,1300,101],{"class":100},[49,1302,104],{"class":88},[49,1304,108],{"class":107},[49,1306,111],{"class":88},[49,1308,1309,1311,1313],{"class":51,"line":114},[49,1310,117],{"class":84},[49,1312,120],{"class":100},[49,1314,89],{"class":88},[49,1316,1317],{"class":51,"line":125},[49,1318,1319],{"class":84},"    pass\n",[14,1321,1322],{},"This makes debugging harder because the program fails quietly.",[14,1324,1325],{},"Instead, show a helpful message or ask for new input.",[14,1327,1328,1329,33],{},"If you want to learn the pattern in more detail, see ",[245,1330,1332],{"href":1331},"\u002Fhow-to\u002Fhow-to-handle-exceptions-in-python\u002F","how to handle exceptions in Python",[35,1334,1336],{"id":1335},"faq","FAQ",[330,1338,1340],{"id":1339},"what-is-the-difference-between-valueerror-and-typeerror","What is the difference between ValueError and TypeError?",[14,1342,1343,1345,1346,1348],{},[17,1344,19],{}," means the type is accepted but the value is invalid. ",[17,1347,225],{}," means the type itself is wrong for that operation.",[330,1350,1352],{"id":1351},"can-i-prevent-valueerror-before-it-happens","Can I prevent ValueError before it happens?",[14,1354,1355,1356,1358],{},"Yes. Validate or clean the value first, or use ",[17,1357,147],{}," when invalid input is possible.",[330,1360,1362],{"id":1361},"why-does-int-raise-valueerror","Why does int() raise ValueError?",[14,1364,1365],{},"Because the text you passed does not represent a valid integer.",[330,1367,1369],{"id":1368},"should-i-always-use-try-except-for-valueerror","Should I always use try-except for ValueError?",[14,1371,1372,1373,1375],{},"Not always. If you can validate the value first, that is often clearer. Use ",[17,1374,147],{}," when failure is expected or input is unpredictable.",[35,1377,1379],{"id":1378},"see-also","See also",[162,1381,1382,1386,1390,1396,1402,1407],{},[165,1383,1384],{},[245,1385,248],{"href":247},[165,1387,1388],{},[245,1389,550],{"href":549},[165,1391,1392],{},[245,1393,1395],{"href":1394},"\u002Ferrors\u002Fvalueerror-too-many-values-to-unpack-fix\u002F","ValueError: too many values to unpack",[165,1397,1398],{},[245,1399,1401],{"href":1400},"\u002Ferrors\u002Fvalueerror-not-enough-values-to-unpack-fix\u002F","ValueError: not enough values to unpack",[165,1403,1404],{},[245,1405,1406],{"href":266},"Python int() function explained",[165,1408,1409],{},[245,1410,1411],{"href":272},"Python float() function explained",[1413,1414,1415],"style",{},"html pre.shiki code .su5hD, html code.shiki .su5hD{--shiki-light:#90A4AE;--shiki-default:#24292E;--shiki-dark:#E1E4E8}html pre.shiki code .smGrS, html code.shiki .smGrS{--shiki-light:#39ADB5;--shiki-default:#D73A49;--shiki-dark:#F97583}html pre.shiki code .sjJ54, html code.shiki .sjJ54{--shiki-light:#39ADB5;--shiki-default:#032F62;--shiki-dark:#9ECBFF}html pre.shiki code .s_sjI, html code.shiki .s_sjI{--shiki-light:#91B859;--shiki-default:#032F62;--shiki-dark:#9ECBFF}html pre.shiki code .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":45,"searchDepth":74,"depth":74,"links":1417},[1418,1419,1420,1421,1427,1431,1435,1436,1437,1443],{"id":37,"depth":74,"text":38},{"id":153,"depth":74,"text":154},{"id":251,"depth":74,"text":252},{"id":324,"depth":74,"text":325,"children":1422},[1423,1425],{"id":332,"depth":81,"text":1424},"Example 1: int(\"abc\")",{"id":447,"depth":81,"text":1426},"Example 2: float(\"12,5\")",{"id":553,"depth":74,"text":554,"children":1428},[1429,1430],{"id":589,"depth":81,"text":590},{"id":661,"depth":81,"text":662},{"id":819,"depth":74,"text":820,"children":1432},[1433],{"id":922,"depth":81,"text":1434},"Why repr() is useful",{"id":1028,"depth":74,"text":1029},{"id":1139,"depth":74,"text":1140},{"id":1335,"depth":74,"text":1336,"children":1438},[1439,1440,1441,1442],{"id":1339,"depth":81,"text":1340},{"id":1351,"depth":81,"text":1352},{"id":1361,"depth":81,"text":1362},{"id":1368,"depth":81,"text":1369},{"id":1378,"depth":74,"text":1379},"Master valueerror in python causes and fixes in our comprehensive Python beginner guide.","md",{},"\u002Ferrors\u002Fvalueerror-in-python-causes-and-fixes",{"title":5,"description":1444},"errors\u002Fvalueerror-in-python-causes-and-fixes","-o2I60oC8oNnBlMmKnrWTVJjj0f9YJQUwN198uQxh3Y",1777585495678]