Ryan
Ryan

Reputation: 10179

Zero Division error,float division by zero

This is My code

for i in range(len(speed)):
for j in range(len(time_two)):
    new.append((speed[i] - speed[i-1])/(time_two[j] - time_two[j-1]))

I get this error:

ZeroDivisionError                         Traceback (most recent call  
last)
    <ipython-input-7-9cdf386300d7> in <module>()
      4 for i in range(len(speed)):
      5     for j in range(len(time_two)):
    ----> 6         new.append((speed[i] - speed[i-1])/(time_two[j] - 
    time_two[j-1]))

ZeroDivisionError: float division by zero

Here speed and time_two are a list of floats, There are no 0's in the lists.

Any suggestions as to what i may change? Any help is appreciated ! Thank you in advance

EDIT: speed = [14.13102608620676, 6.111463527087486, 5.593147106275493, 4.854037993898749,...] time_two = [2.0, 14.0, 15.0, 17.0, 15.0, 15.0...]

Upvotes: 0

Views: 2927

Answers (1)

Tim Pietzcker
Tim Pietzcker

Reputation: 336498

time_two = [2.0, 14.0, 15.0, 17.0, 15.0, 15.0...]

has two identical values following each other, so if you do

time_two[j] - time_two[j-1]

you get zero, so

(speed[i] - speed[i-1])/(time_two[j] - time_two[j-1])

will trigger a ZeroDivisionError.

Upvotes: 3

Related Questions