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