Write a function named repeatCount().
The function repeatCount() takes two string parameters:
--> the name of an input file and the name of an output file.
The function repeatCount() should read each line of the input file, identify the number of words on the line that occur more than once, and write that number to a line in the output file.
A word should be considered to be repeated if occurs with different capitalization.
That is, 'To' and 'to' are the same word.
You may assume that the file contains only upper and lower case letters and white space; it does not contain any punctuation marks.

For example, suppose the input file contains the following four lines:
Woke up this morning with an ache in my head
I splashed on my clothes as I spilled out of bed
I opened the window to listen to the news
But all I heard was the Establishment Blues

Then the output file should contain the following four lines:

Note: * 1: because I is the only letter that repeats in 2nd line and 2: because to , the are being repeated
0 indicates no words are being repeated*

Here is my code

def repeatCount(filename1, filename2):
    inf = open(filename1, 'r')
    outf = open(filename2, 'w')
    aDict = {}
    text = inf.read()
    text = text.lower().split()
    aDict = {}
    for word in text:
        aDict[word] = text.count(word)
    for key in aDict:
        outf.write(key + int(aDict[key]) + '\n')

repeatCount("fourlines.txt", "outputLines.txt")

-- I am getting error --
outf.write(key + int(aDict[key]) + '\n')
TypeError: Can't convert 'int' object to str implicitly

Can someone help me with this code!!
Thank you.

Recommended Answers

All 2 Replies

This is a tough piece of homework!
As a hint, I used something like this ...

    for ix, line in enumerate(textList):
        # change to lower case and strip newline char
        # then create a list of words for each line
        wordList = line.lower().strip().split()
        print(wordList)  # test
        for word in wordList:
            cnt = wordList.count(word)
            # key is a (linenumber, word) tuple
            # value is a list of counts for that key
            aDict.setdefault((ix, word), []).append(cnt)

vegaseat has it right. I used the same kind of menthod in a program for counting the number of words in a file. I just didn't use the ".lower()" part.

Be a part of the DaniWeb community

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