ZeroDivisionError unknown cause

95 views Asked by At

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.

0

There are 0 answers