Python Forum

Full Version: Zero Division Error
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Good evening,
For my project, I keep getting a Zero Division error when trying to calculate the score below. I can't seem to figure out exactly what I am doing incorrectly. The code snippet is below. Any help would be greatly appreciated.

Thank you.

 
@property
    def positivity(self):
        """"A positivity score calculated as follows:
            Create local tally variable with initial value of 0.
            Increment tally by 1 for every word in self.words found in positive.txt (in same directory)
            Decrement tally by 1 for every word in self.words found in negative.txt (in same directory)
            Calculate score as follows: 
            round( tally / self.word_count * 1000)"""
        tally = 0
        words = self._words()
        neg_word_list = re.findall(r'^\w+', 'negative.txt')
        pos_word_list = re.findall(r'^\w+', 'positive.txt')
        for item in words:
            if item in pos_word_list: tally = tally + 1
            if item in neg_word_list: tally = tally -1
        return (round(tally / self.word_count * 1000)) 
Error:
ZeroDivisionError Traceback (most recent call last) <ipython-input-52-5bbe8bf0ab1a> in <module>() 226 print("distinct_word_count", ta.distinct_word_count) 227 #print("avg_word_length", ta.avg_word_length) --> 228 print("positivity", ta.positivity) 229 print(ta.common_words(minlen=5, maxlen=10)) 230 print("plot", ta.plot_common_words(minlen=5, maxlen=10)) <ipython-input-52-5bbe8bf0ab1a> in positivity(self) 198 if item in pos_word_list: tally = tally + 1 199 if item in neg_word_list: tally = tally -1 --> 200 return (round(tally / self.word_count * 1000)) 201 202 ZeroDivisionError: division by zero
It seems pretty evident from the error that you need to not divide by zero. Your code doesn't handle that case, and you need to change is to that it does. You can either check for zero before dividing by it or you can catch the exception - I would suggest the first option, as I try to keep exceptions to exceptional circumstances.
I had thought of that, however I have to use this equation: round( tally / self.word_count * 1000). Since it is supposed to return a value due to the multiple text strings being used, I do not understand why it would return a zero value. Can someone explain?
Since I am new to Python, maybe am I not writing it correctly for it to turn the text into integers properly? I'm still learning.
Your regexes start with '^', so they only match the beginning of the string. So even though you are using findall, they can only ever match once, because their is only one start of the string. Second, you are searching on the string 'negative.txt', not the contents of the file negative.txt. So the only work in neg_word_list is 'negative'. That's limiting your matches. You need to read the contents of the file with something like negative_text = open('negative.txt').read().
That did fix the error!

Thank you so much!!!