Python否则无法正常工作

Python否则无法正常工作,python,python-3.x,if-statement,Python,Python 3.x,If Statement,我正在编写python脚本来计算某些字符串出现的次数,但似乎else无法正常工作 这是我的密码: import os user_input = "#" directory = os.listdir(user_input) searchstring1 = 'type="entity"' searchstring2 = 'type="field"' searchstring3 = 'type="other"' searchstring4 = "type=" count_entity =

我正在编写python脚本来计算某些字符串出现的次数,但似乎else无法正常工作

这是我的密码:

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))
导入操作系统
用户输入=“#”
directory=os.listdir(用户输入)
searchstring1='type=“entity”'
searchstring2='type=“field”'
searchstring3='type=“其他”'
searchstring4=“类型=”
计数单位=0
计数\u字段=0
count_other=0
计数\u无=0
counttotal=0
对于目录中的fname:
如果os.path.isfile(用户输入+os.sep+fname):
f=打开(用户输入+os.sep+fname,'r',encoding=“utf-8”)
对于f中的行:

如果“您的
else
仅适用于前面的
if
(以及附加到它的任何
elif
)。执行以下操作:

            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
每次
searchstring3
不在
时,即使
searchstring1
searchstring2
在行中,您都会增加
count\u none
(因此
count\u other+count\u none
的总和总是
count\u total

要修复此问题,请对中间的
if
语句使用
elif
而不是
if
,因此
else
案例仅在未找到任何搜索字符串时执行:

            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
如果找到
searchstring1
,这将阻止您检查
searchstring2
searchstring3
(同样,如果找到
searchstring2
,您将不会检查或基于
searchstring3
)。如果您需要搜索所有三个,但如果它们都未命中,则只需递增
count\u none
,您需要稍微复杂一点:

            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

除了第一个
if
外,请使用
elif
进行所有操作。非常感谢您的完美答案!!