Help, ZeroDivisionError, I checked there is no 0 in my code

PS: This is not in any of the courses, I was just making this code for fun

I was making a code filled mostly with variable so that I can input the value for later purposes, I was doing fine, but then when I tried running the code, it showed the “ZeroDivisionError: integer division or modulo by zero”, I checked, all of the inputs in my variables are > 0, and that there are no subtractions that will make them 0. I checked harder and found out the problem (or is it just me), you can’t divide a number by a fraction that is less than 0. like:
a = 1/3
b = 1/a
print a
it shows the error… so, how do I fix this?

And if you want to see the code, here is my code:

a = 1

def c (a):

    if a == 1:

        return 1


        return 2

def stack (a):

  count = 0

  a = 1

  b = 1

  CD = a * b

  CDA = (b / CD) * a

  CDB = ((b+c(a)) / CD)


  while a/b < 2:

    if a/b < 2:

      x = (a/b + 1/(b+c(a))) * (b+c(a))

      y = (1 / ((a/b) + (1/(b+c(a))))) * CDC

      a = x

      b = y

      count = count + 1




  return count

print stack(a)

#End of Code

I am still new to python coding, and I really appreciate changes and extra information, thanks

Hi there, welcome to the forums!

The fact that you are writing statements like print a leads me to believe that you are using a variant of Python 2.

In Python 2, the division operator (/) performs integer division when both operands are integers. As a result, your division gets the following result:

>>> 1/3

If you change either of the operands to a float, the result is different:

>>> 1.0/3

In your code, if you change the value of b to 1.0, you should be OK. (I have done this, and run your code, and it completes without a ZeroDivisionError.)

I should note, also, that Python 3 does not have this behaviour. In Python 3, 1/3 correctly evaluates to 0.3333333333333333 without the need to cast an operand to a float.

Hope that helps.


It works, thank you very much, now I have to make it do what I want it to do. Thanks again

Sorry if I broke the Does your reply improve the conversation in some way? rule


Haha, don’t worry about that. It’s more of a guideline, meant to discourage people from posting simply because they can. Coming back to let us know you’ve solved your problem is certainly a good reason to reply to the topic. :smiley:

No worries, glad to be of some help. :slight_smile:

Pop back and let us know if you need a hand with anything else. :slight_smile:

1 Like

Hello again, (this may sound stupid) is there a way to like save and/or read the input as a fraction, because the code that I am trying to make is fraction based. It is pretty hard debugging when I try to do decimal to fraction and vice versa manually

1 Like

You might want to look at the fractions module which sounds like a good fit to your work-
Edit: If you’re working with Python2 you’d want this link to the docs instead-

Python3.8 verison-

If it’s no good you may have to search around for some third-party options to find an optimal solution.

1 Like

okay, thank you for the information, I’ll try my best! Thank you