Why is my counter not updated by "count == count + 1"?

Question:

 answers = []

   for syn in all_words :
      count = 0
      result = [syn, count]
      for Document in Corpus:
         for Word in Document:
            if syn == Word :
               count == count + 1
               
      answers.append(tuple(result))

i’m trying to count the number of occurrences of a given word from all_words in each document in a corpus. For some reason, the count is always 0.

Asked By: user_1738

||

Answers:

count == count + 1

You’re using the wrong operator.

The double equal sign == is a comparison operator. It tests if the two arguments are equal.

The single equal sign = is an assignment operator.

Use = instead of ==.

Answered By: John Gordon
  • You are using a comparison operator (==).

  • What you want instead is an assignment operator (=).

  • To make life a little easier, you can use the increment shortcut of +=.

  • Example:

...
if syn == Word :
  count = count + 1
...

# Is Equivalent To:
...
if syn == Word :
  count += 1
...
Answered By: BitWrecker
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.