So for my assignment i have to scan a .txt file with almost 300k words and count the number of occurences where the word ends with 'er'.
Here is the assignment:
How many words in the text end in "er"? (NB! Here we are looking for the suffix "er" and not just an instance anywhere in a word (ie we will count "danser" as an instance but not “indere”). In this exercise you do not have to deal with punctuation; just count the words as ends in "er" and overlook those that end in "er," or "er.", ie instances of are followed of period, comma or other punctuation. Tips & Warnings You can use the split methods and endswith
I have tried different solutions, but i am not getting anywhere.
What am i doing wrong?
file = open ("in1140-tekst.txt")
data = file.read()
wordcount = 0
def count_words(string):
for word in string.split():
if word.endswith("er") == True:
wordcount += 1
count_words(data)
print(wordcount)