Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Zero Division Error
#1
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
Reply


Messages In This Thread
Zero Division Error - by moga2003 - Mar-04-2019, 12:49 AM
RE: Zero Division Error - by micseydel - Mar-04-2019, 12:56 AM
RE: Zero Division Error - by moga2003 - Mar-04-2019, 01:32 AM
RE: Zero Division Error - by ichabod801 - Mar-04-2019, 01:55 AM
RE: Zero Division Error - by moga2003 - Mar-07-2019, 02:15 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Zero Division Error Leo 2 1,965 Mar-25-2022, 05:56 AM
Last Post: Leo
  Finding square roots using long division. jahuja73 10 7,824 Feb-24-2021, 01:25 PM
Last Post: jahuja73
  Division problem Eric7Giants 1 2,168 Nov-16-2019, 05:50 AM
Last Post: ndc85430
  Count how many carpets you need to fill room floor without multiplication/division Ech0ke 1 2,838 Apr-20-2019, 07:50 PM
Last Post: ichabod801

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020