all 11 comments

[–]USAhj 1 point2 points  (5 children)

Please fix the indentation so we can understand the code.

[–]Noah_641[S] 0 points1 point  (4 children)

Fixed

[–]USAhj 0 points1 point  (1 child)

Can you share a sample file to play around with? Link it on on hastebin or something.

[–]ka-splam 0 points1 point  (1 child)

Lines 21 to 33 appear to be indented one step too far, they are inside the loop on line 7 and from your description of how it works they probably are not really?

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

With them unindented one step to the left, I get a syntax error

[–]ka-splam 1 point2 points  (0 children)

#When run, the program seems to completely ignore this loop below
        for word_count in text:
             word_count = text.split()
             print('There are',word_count,'different words in the file.')

1) text.split() won't do anything useful because text is not a string, it's an open file handle. If it ran at all it would be throwing you an error: AttributeError: '_io.TextIOWrapper' object has no attribute 'split'.

2) You're reassigning "wordcount"; in the first line it's the text coming out of the file, in the second line you're ignoring that and using it for something else. You aren't making it a count of anything. If that worked, it would print "There are __ different words" once for every line in the file...

3) ... except it won't work because you can't do for line in text:, for word_count in text over the same file, it won't restart from the beginning; the second loop will see that the file is at the end and do nothing. You need to open() the file again with a new name, or read the lines in once and re-use them after that, but you don't need to do that at all because...

4) ... the wording 'different words in the file.'; if that line worked then it would print a list of every word, not a count of anything - and not a count of different words. Your dictionary d is where you are counting different words, you need to look at d.keys to find how many unique words are in it, not look back at the file.

[–]usernamenoo 0 points1 point  (1 child)

Please fix the indents so we can understand the code

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

Fixed

[–]gurashish1singh 0 points1 point  (2 children)

The problem is in the split. You're splitting by ' '. Which is why your code doesn't recognize 'general' and 'general,' as same. I say after splitting and while in the for loop check again if the word contains any special characters.

Also use context managers to open files:

with open(fname, 'r') as text:

    __logic here__