title: I can't seem to figure out how to fix this 'ZeroDivisionError: division by zero'
body: <p>I keep this is an error with my code and I don't know how to fix it and when I try to fix it, it ends up messing up the entire program how can I fix it.</p>
  <pre><code>def p_appears_in_spam(word):
      count = 0
      total_spams = 0
      for t in train_set:
          text = t[0]
          if t[1] == 1:
              total_spams += 1
              if word in text:
                  count += 1
      return count/total_spams
  def p_appears_in_ham(word):
      count = 0
      total_hams = 0
      for t in train_set:
          text = t[0]
          if t[1] == 0:
              total_hams += 1
              if word in text:
                  count += 1
      return count/total_hams
  def total_spams_and_hams(tset):
      spams = 0
      hams = 0
      for t in tset:
          spams += 1 if t[1] == 1 else 0
          hams += 1 if t[1] == 0 else 0
      return spams, hams
  
  p_spam = total_spams_and_hams(train_set)[0]/len(train_set) # Probability that a message is spam
  p_ham = total_spams_and_hams(train_set)[1]/len(train_set) # Probability that a message is ham
  
  def p_is_spam_given_word(word):
      return (p_appears_in_spam(word)*p_spam)/((p_appears_in_spam(word)*p_spam + p_appears_in_ham(word)*p_ham))
  
  word = 'free'
  print('Probability that a message is spam given the word &quot;{}&quot; is: {}'.format(word, p_is_spam_given_word(word)))
  </code></pre>
  <p>this is the error I am getting</p>
  <pre><code>---------------------------------------------------------------------------
  ZeroDivisionError                         Traceback (most recent call last)
  &lt;ipython-input-102-eaaafc1fa61f&gt; in &lt;module&gt;
       31     return (p_appears_in_spam(word)*p_spam)/((p_appears_in_spam(word)*p_spam + p_appears_in_ham(word)*p_ham))
       32 word = 'free'
  ---&gt; 33 print('Probability that a message is spam given the word &quot;{}&quot; is: {}'.format(word, p_is_spam_given_word(word)))
  
  &lt;ipython-input-102-eaaafc1fa61f&gt; in p_is_spam_given_word(word)
       29 p_ham = total_spams_and_hams(train_set)[1]/len(train_set) # Probability that a message is ham
       30 def p_is_spam_given_word(word):
  ---&gt; 31     return (p_appears_in_spam(word)*p_spam)/((p_appears_in_spam(word)*p_spam + p_appears_in_ham(word)*p_ham))
       32 word = 'free'
       33 print('Probability that a message is spam given the word &quot;{}&quot; is: {}'.format(word, p_is_spam_given_word(word)))
  
  &lt;ipython-input-102-eaaafc1fa61f&gt; in p_appears_in_spam(word)
        8             if word in text:
        9                 count += 1
  ---&gt; 10     return count/total_spams
       11 def p_appears_in_ham(word):
       12     count = 0
  
  ZeroDivisionError: division by zero
  </code></pre>
  <p>I am getting zero division error for the count in both the spams and hams functions that I wrote for this.</p>