Home > Software design >  Why is this simple histogram dictionary code saying that words is not defined?
Why is this simple histogram dictionary code saying that words is not defined?

Time:01-23

This is the code:

name = input("Enter file: ")
handle = open(name)

counts = dict()
filetext = handle.read()
for line in handle:
    words = line.split()
    for word in words:
        counts[word] = counts.get(word, 0)   1
print(words)
print(counts)
bigcount = None
bigword = None
for word,count in counts.items():
    if bigcount == None or count > bigcount:
        bigword = word
        bigcount = count

print(filetext)
print("Most common word: ", bigword, bigcount)
print(counts.items())

this is the output:
Enter file: pls.txt
Traceback (most recent call last):
  File "D:\Tools\Coding\PyCharm Community Edition 
2021.2.3\bin\pythonProject2\Mostcommonword.py", line 10, in <module>
    print(words)
NameError: name 'words' is not defined

Process finished with exit code 1

When running the program, instead of returning the most common number, it returned None. I managed to find out that the reason for that is that the "words" list is completely empty, for some reason. The good thing about simple problems is that I know what's going on. The bad thing is that there are not many ways to fix it at all.

CodePudding user response:

After you do handle.read(), the file is positioned at end-of-file. There's nothing left for the for line in handle: to read. You either need to rewind in between (handle.seek(0)), or just skip the first read altogether.

CodePudding user response:

You could consider building a list of lines as you iterate over the file handle rather than doing a read/seek. Also, you can work out the most frequently occurring word as you work through the file rather than doing a second pass of your dictionary. Something like this:

D = dict()
M = 0
B = None
C = list()
with open('<Your filename goes here>') as txt:
    for line in txt:
        C.append(line)
        for word in line.strip().split():
            D[word] = D.get(word, 0)   1
            if D[word] > M:
                B = word
                M = D[word]

print(''.join(C))
print(f'Most common word with {M} occurrences is {B}')
  •  Tags:  
  • Related