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