我该如何修复我的程序?计算输入文件中的文章
How do I fix my program? Counting articles in an input file
我需要计算输入文件中的冠词 (a, an, and, the)。下面是我写的代码。它计算文章,但它也计算单词中的字母或字母串,例如单词"sand"或单词"thanks"将被算作一篇文章。如何调整我的代码?
def countArticles (line):
articleCount = 0
for art in line:
if art ==" a " or art ==" A " or art == " An " or art ==" an " or art ==" and " or art == "And " or art ==" the " or art ==" The ":
articleCount = articleCount + 1
return articleCount
我可能会这样尝试:
def countArticles (line):
articleCount = 0
for art in line.lower().split(' '): # Make the words all lowercase, split on spaces.
if art in ("a", "an", "and", "the"):
articleCount = articleCount + 1
return articleCount
我需要计算输入文件中的冠词 (a, an, and, the)。下面是我写的代码。它计算文章,但它也计算单词中的字母或字母串,例如单词"sand"或单词"thanks"将被算作一篇文章。如何调整我的代码?
def countArticles (line):
articleCount = 0
for art in line:
if art ==" a " or art ==" A " or art == " An " or art ==" an " or art ==" and " or art == "And " or art ==" the " or art ==" The ":
articleCount = articleCount + 1
return articleCount
我可能会这样尝试:
def countArticles (line):
articleCount = 0
for art in line.lower().split(' '): # Make the words all lowercase, split on spaces.
if art in ("a", "an", "and", "the"):
articleCount = articleCount + 1
return articleCount