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