Ye, but its required by the exercise call the function words(with the parameter filename).

Well, i just taught of doing this. The code it was developed here works fine, but it has one catch. It counts numbers. Where can i insert word.isalpha()?

from string import punctuation

def words(filename):
    count = {}
    for aline in filename:
        palavras = ''.join(c for c in aline.lower() if c not in punctuation)
        palavras = palavras.split()
        for word in palavras:
            if word in count:
                count[word]  +=1
            else:
                count[word] = 1
    return count



filename=open("texto.txt")
print(words(filename))

change line 6 condition.

I tried and i think its good now.

from string import punctuation

def words(filename):
    count = {}
    for aline in filename:
        palavras = ''.join(c for c in aline.lower() if c not in punctuation if c.isalpha())
        palavras = palavras.split()
        for word in palavras:
            if word in count:
                count[word]  +=1
            else:
                count[word] = 1
    return count



filename=open("texto.txt")
print(words(filename))

No it is not: you should be able to call the function like

print(words('texto.txt'))

(and you left in the old condition which was less strict)

By fixing the parameter to be filename like specified.

Well can you give me an example how to do it? I now what you mean, i can't put it on Python language... Besides, realised that using

from string import punctuation
 
def words(filename):
    count = {}
    for aline in filename:
        palavras = ''.join(c for c in aline.lower() if c not in punctuation if c.isalpha())
        palavras = palavras.split()
        for word in palavras:
            if word in count:
                count[word]  +=1
            else:
                count[word] = 1
    return count
 
 
 
filename=open("texto.txt")
print(words(filename))

That doesn't count numbers has words anymore, BUT it returns a unsplitted string... What is wrong?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.