Define a function called articleStats() that takes a string parameter named fileName representing the name of a file. The file contains lower case words separated by spaces. The function returns the total number of all articles. An article is one of the following words: a, the, an.
I know this is a pretty simple question but I'm just really confused on what it is that I am doing wrong. This is what I have so far, but I know it's wrong
def articleStats(filename):
filename.split()
for word in filename:
if 'a' or 'the' or 'an' in word:
print(len(filename))
articleStats('an apple a day keeps the doctor away')
The problem is if 'a' or 'the' or 'an' in word:
. In Python string literals are evaluated as True
so your condition is seen as: if True or True or 'an' in word
Change this to if 'a' in word or 'the' in word or 'an' in word
For a better understanding of what is happening, run the following code to see how Python might treat other conditions.
tests = [[], ['not empty list'], {}, {'not':'empty dict'}, 0, 1, '', 'not empty string']
for test in tests:
print test, bool(test)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.