Zerodivisionerror: integer Division or modulo by zero

Asked

Viewed 1,047 times

1

The code:

janela=[]
    for i in range(100):
        if(len(leitura)%i == 0):
            janela.append(i)

The mistake I get is this:

Zerodivisionerror: integer Division or modulo by zero

  • The value of i starts at zero because of range And you’re trying to calculate the rest of the division of something by zero, which doesn’t make sense. It is important to always take care of the indentation, because in Python this makes total difference; in your code the second line should not be indented in relation to the first. To actually fix the code you will need to explain what it should do and what the expected result is. You probably shouldn’t even be using Python 2 either, see to migrate to 3.

  • That’s right. The issue of indentation was when I passed the code here, but it’s indented correctly. Thank you!

1 answer

4


How the name says the function range() generates numbers within a defined range. If you put only one number this will be the final limit, and the beginning will be automatically 0, then you make a division by that first number that it generates and mathematics prohibits it because it has no way of defining what that result would be. Starting at 1 solves the problem:

janela=[]
leitura = ""
for i in range(1, 100):
    if len(leitura) % i == 0:
        janela.append(i)

Behold working in the ideone. And in the repl it.. Also put on the Github for future reference.

  • Got it, pure lack of attention from me, thank you!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.