0

I have a piece of code from a larger script, and for the life of me I can't figure out what's causing the error.

It looks like this:

counter = 1
for i in range(len(binCounts)):
    thisRatio = float(binCounts[i]) / (float(counter) / float(len(bins)))
    OUTFILE.write("\t".join(bins[i][0:3]))
    OUTFILE.write("\t")
    OUTFILE.write(str(binCounts[i]))
    OUTFILE.write("\t")
    OUTFILE.write(str(thisRatio))
    OUTFILE.write("\n") 

where binCounts is a chronological list [1, 2, 3]

and bins is another list that contains slightly more info:

[['chrY', '28626328', '3064930174', '28718777', '92449', '49911'], ['chrY', '28718777', '3065022623', '28797881', '79104', '49911'], ['chrY', '28797881', '3065101727', '59373566', '30575685', '49912']]

It should be, for each variable in binCounts, taking the calculated thisRatio, the first 3 rows in bins, and the output of binCounts, and putting them together in a new file (OUTFILE).

But it's not doing this. It's giving me an error:

thisRatio = float(binCounts[i]) / (float(counter) / float(len(bins)))
ZeroDivisionError: float division by zero

When I run the line:

thisRatio = float(binCounts[i]) / (float(counter) / float(len(bins)))

interactively, it works fine.

When I break it into pieces, this is what I get:

A = float(binCounts[i])
print (A)
49999.0

B = (float(counter))
print (B)
1.0

C = float(len(bins))
print (C)
50000.0

thisRatio
2499950000.0

And then I reran the whole piece interactively (which I hadn't done before - just the single thisRatio line) and got this error...

Traceback (most recent call last):
  File "<stdin>", line 3, in <module>
IndexError: list index out of range

So it seems when run as a .py script the error is a ZeroDivisionError, and when run interactively the error is an IndexError.

mfk534
  • 719
  • 1
  • 9
  • 21
  • 1
    Have you tried printing each of those values before performing the division? Because one of them is zero. – TigerhawkT3 Jul 27 '15 at 19:09
  • Disassemble the statement into three variable assignments (each float operand), and use either `print` or `pdb` to examine the contents of each. – Two-Bit Alchemist Jul 27 '15 at 19:09
  • You may be getting that error because the length of bins is for some reason zero. By the way, shouldn't you have range(len(binCounts)-1) so that you don't run into a list index out of range error? – 74U n3U7r1no Jul 27 '15 at 19:20
  • OK, so that's maybe their values on the first loop iteration. What about the rest? Are you printing these from the same test where you raise the `ZeroDivisionError`, rather than interactively where you avoid that result? The plain fact here is that unless there is some really obscure bug in Python none of us know about (unlikely), at some point one of those three things is zero. – Two-Bit Alchemist Jul 27 '15 at 19:22
  • So you are getting a list index out of range because your are starting your loop from zero to len(binCounts) which means that you are exceeding the length of binCounts by 1. It should be range(len(binCounts)-1) – 74U n3U7r1no Jul 27 '15 at 19:24
  • Are you sure you are running the same code? – Padraic Cunningham Jul 27 '15 at 19:28
  • Or just replace the whole [`range(len(a))` anti-pattern](https://stackoverflow.com/questions/19184335/is-there-a-need-for-rangelena) with a regular loop so you don't have to write all that silliness out. – Two-Bit Alchemist Jul 27 '15 at 19:28
  • The `IndexError` is probably because you're using `i` to index into `bins` as well as `binCounts`. The `ZeroDivisionError` is because `len(bins)` at some point becomes zero... – thebjorn Jul 27 '15 at 19:32
  • Your index `i` is probably not reset to 0, so you went out of array length. Then the other error occurs when divisor is 0. – Maelstrom Jul 27 '15 at 20:00
  • THANK YOU ALL - this combination of comments was on-point. All of you helped me realize how a part of the loop ultimately resulted in a zero, allowing me to trace the error & fix my problem. What a community :'] – mfk534 Jul 27 '15 at 20:18

0 Answers0