0

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')
chrk
  • 4,037
  • 2
  • 39
  • 47
Kaede
  • 3
  • 1

1 Answers1

0

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)
That1Guy
  • 7,075
  • 4
  • 47
  • 59