1

I'm writing python scripts to count how many times certain strings occur but it seems like the else is not working correctly.

Here is my codes:

import os

user_input = "#"
directory = os.listdir(user_input)

searchstring1 = 'type="entity"'

searchstring2 = 'type="field"'

searchstring3 = 'type="other"'

searchstring4 = "type="

count_entity = 0

count_field = 0

count_other = 0

count_none = 0

counttotal = 0

for fname in directory:
    if os.path.isfile(user_input + os.sep + fname):
        f = open(user_input + os.sep + fname, 'r', encoding="utf-8")
        for line in f:
            if "<noun" in line:
                counttotal += 1
                if searchstring1 in line:
                    count_entity += 1
                if searchstring2 in line:
                    count_field += 1
                if searchstring3 in line:
                    count_other += 1
                else:
                    count_none += 1

        f.close()

print("Entity Number" + str(count_entity))
print("Field Number" + str(count_field))
print("Other Number" + str(count_other))
print("None Number" + str(count_none))

If it worked correctly, the count_none should equal to total-entity-field-other. But I don't know why it turns out count_none = counttotal so obvious else is not working correctly.

Can anyone tell me why this happened? Thanks for your help!!

ShadowRanger
  • 143,180
  • 12
  • 188
  • 271
Cissy Liu
  • 21
  • 3

1 Answers1

1

Your else only applies to the preceding if (and any elifs attached to it). By doing:

            if searchstring1 in line:
                count_entity += 1
            if searchstring2 in line:
                count_field += 1
            if searchstring3 in line:
                count_other += 1
            else:
                count_none += 1

you increment count_none every time searchstring3 isn't in line, even if searchstring1 or searchstring2 was in the line (so count_other + count_none will always sum to count_total).

To fix, use elif instead of if for the in between if statements, so the else case only executes if none of the search strings were found:

            if searchstring1 in line:
                count_entity += 1
            elif searchstring2 in line:  # Changed to elif
                count_field += 1
            elif searchstring3 in line:  # Changed to elif
                count_other += 1
            else:
                count_none += 1

This will prevent you from checking for searchstring2 and searchstring3 if searchstring1 is found (and similarly, if searchstring2 is found, you won't check for or increment based on searchstring3). If you need to search all three, but only increment count_none if none of them hit, you'll need to get a little more complicated:

            foundany = False
            if searchstring1 in line:
                foundany = True
                count_entity += 1
            if searchstring2 in line:
                foundany = True
                count_field += 1
            if searchstring3 in line:
                foundany = True
                count_other += 1
            if not foundany:
                count_none += 1
ShadowRanger
  • 143,180
  • 12
  • 188
  • 271