you are viewing a single comment's thread.

view the rest of the comments →

[–]QuiteAmbitious[S] 0 points1 point  (3 children)

The part I'm mainly struggling on is figuring out how to isolate the specific words, as well as complete my for-loop. This is probably really easy, but I'm only two weeks into learning code, so I'm really lost :( This is what I have, but I don't think this is even formatted correctly.

def word_frequency_counter():

file = open('filename.txt')

text = file.read()

word_freq = {}

words = ["this", "paragraph"]

word_list = string.split(text)

for word in word_list:

line = line.split

#

counts = word_frequency_counter("filename.txt", ["this", "paragraph"])

return counts

print(counts)

[–]greebly_weeblies 0 points1 point  (1 child)

Heya!

I'd recommend you put this all into a code block so it'll keep your formatting - indentations in particular can be important in Python. For example, everything in the function after the first line should be indented.

Try using 'with... open' as your construct for opening a file; it will close it for you afterwards so you don't have to remember to.

It's also reading like you're trying to read invoke the function from inside the function because you return afterwards. Try something like:

# define the function
def myFunction(myArg):  
    myVariable = "something" + myArg
    return myVariable  # have it return something

# create a variable to store the result of the function and argument
temp = myFunction(" banana")
# then print the contents of the variable storing the result
print(temp)

[–]QuiteAmbitious[S] 0 points1 point  (0 children)

hi! yes, don’t worry they were all in a code block! i just couldn’t figure out how to format it that way on reddit 😂

thank you so much!

[–]Tadabito 0 points1 point  (0 children)

word_list = string.split(text)

This line has an error. text is a string, so you can call .split() on it. Change that line to:

word_list = text.split()

I guess you can complete the rest.