0

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) 
  • You are trying to update a global, immutable integer inside a function – roganjosh Sep 25 '21 at 20:10
  • Most visibly, trying to increase `wordcount` by 1 in a function, and a space between `open` and `(` Please provide the error you are getting, as well as a sample from the file for better help. – Y.R. Sep 25 '21 at 20:12
  • It's not clear to me _why_ you would package the particular code you did into a function. Either remove the function definition, or make sure that you give it everything it needs to act in a repeatable way. You can already see that your counter is outside the function, yet the whole point of the function is to do the counting of occurrences – roganjosh Sep 25 '21 at 20:12
  • This is my first week of python, so I am just trying and failing. Would really appriciate some solution proposals – python noob Sep 25 '21 at 20:15
  • That's fine, I'm just trying to make you think about this. `wordcount` is integral to what the function does, no? In which case, it should be initialised in the function, and you want to `return` something (functions with no `return` just return `None` to the "caller" - `count_words(data)` – roganjosh Sep 25 '21 at 20:20
  • wordcount is supposed to save the number of times that the script finds a word that ends with 'er' and then add 1 for every word that it finds. How would you suppose that i do it without a function? I am kinda stuck rn. – python noob Sep 25 '21 at 20:29
  • Here is a sample of the file: File name: in1140-tekst.txt Sample : «A Monster Calls»: Et indre monster A Monster Calls er en tungt prisbelønnet, britisk barnebok fra 2011, skrevet av Patrick Ness, etter en idé av Siobhan Dowd som døde av kreft før hun selv rakk å skrive historien. Jeg kjenner ikke til boken, men merket at den spesielle tilblivelseshistorien ga en viss tyngde til filmen til og med før jeg så den. Man møter gjerne filmer som dette med et eget sett med forventninger, ofte preget av ærbødighet og respekt, på samme måte som man forholder seg – python noob Sep 25 '21 at 20:31
  • ```wordcount = 0 for word in data.split(): if word.endswith("er") == True: wordcount+=1 print(wordcount) ``` – python noob Sep 25 '21 at 20:35

1 Answers1

1

You want to define the variable wordcount inside the function... Also, be sure to return it so you can print it later, outside the function. Look up 'scope' of variables in Python. The wordcount variable is only defined inside the scope of the function count_words below. To access it from outside, you need to return it.

Likewise, if wordcount is defined outside the function, you can access the value, but not edit it in the function scope, unless you add the line 'global wordcount' to include it.

file = open ("in1140-tekst.txt")
data = file.read()
def count_words(string):
    wordcount = 0
    for word in string.split():
        if word.endswith("er") == True:
            wordcount += 1
    return wordcount

wordcount = count_words(data)
print(wordcount) 

Example using globally defined wordcount

file = open ("in1140-tekst.txt")
data = file.read()
wordcount = 0
def count_words(string):
    global wordcount
    for word in string.split():
        if word.endswith("er") == True:
            wordcount += 1

count_words(data)
print(wordcount)
seadniksic
  • 31
  • 3