user3519870
user3519870

Reputation: 81

ZeroDivisionError Python

def do_timelapse(self, cmd):
    self.final_fps = input("What will your final edit be at(FPS)?\n")
    self.frame_interval = input("What is your Camera's Frame interval(Sec)")
    self.original_frame_fps = 1/self.frame_interval
    self.small_original = self.original_frame_fps/100
    self.percentage = self.final_fps/self.small_original
    print self.percentage

How do i resolve this error:

self.percentage = self.final_fps/self.small_original
ZeroDivisionError: integer division or modulo by zero ?

Upvotes: 0

Views: 576

Answers (2)

Jean-François Fabre
Jean-François Fabre

Reputation: 140178

self.frame_interval = input("What is your Camera's Frame interval(Sec)")

it returns an integer, means that you're using python 2. So the next line

self.original_frame_fps = 1/self.frame_interval

probably issues 0 if self.frame_interval > 1

self.small_original = self.original_frame_fps/100

so self.small_original is zero

self.percentage = self.final_fps/self.small_original

so it crashes.

Fix: work with floats:

self.original_frame_fps = 1.0/self.frame_interval

and

self.small_original = self.original_frame_fps/100.0

(of course check that self.frame_interval isn't 0 either)

Alternatives:

  1. use python 3, with just a small fix (and still the check for zero):

    self.final_fps = int(input("What will your final edit be at(FPS)?\n")) self.frame_interval = int(input("What is your Camera's Frame interval(Sec)"))

since input returns strings now (like raw_input does in python 3)

  1. use python 3 division in python 2: add this at the start of your file:

    from __future__ import division

Upvotes: 3

Moinuddin Quadri
Moinuddin Quadri

Reputation: 48077

My best guess is you are using Python 2.7 which returns an integer value on division of two int. For example:

>>> 5 / 100
0

That is the reason the value of self.small_original is getting set as 0. In order to fix it, you need to type-cast any of the numerator or denominator to float as:

>>> float(5) / 100
0.05  # returns float value

Hence, you need to update the line in your code with:

self.original_frame_fps = 1.0/self.frame_interval
#               float here ^

and

self.small_original = self.original_frame_fps/100.0
#                                  float value  ^

Upvotes: 1

Related Questions