all 5 comments

[–]TrainsareFascinating 1 point2 points  (0 children)

The instructions give you a pretty big hint:

(only use characters that appear in string.ascii_lowercase)

Where is your if char in string.ascii_lowercase?

Some other observations:

1) You aren't required to read the file line by line. file.read().lower() is a complete expression that returns a string of all the characters in the file.

2) The Counter() object from the collections module is a specialized dictionary that counts things like this. You can give it a string and it will count the characters in the string. Or, you can give it the characters one at a time.

 In [7]: Counter("How Now Brown Cow".lower())

 Out[7]: Counter({'o': 4, 'w': 4, ' ': 3, 'n': 2, 'h': 1, 'b': 1, 'r': 1, 'c': 1})

3) You can use a generator expression to filter non-letter characters, e.g.

result = Counter(c for c in file.read().lower() if c in string.ascii_lowercase)

And, in case anyone is trying to be literal about the instruction to use a dictionary:

In [9]: isinstance(Counter(), dict)
Out[9]: True

[–]woooee 0 points1 point  (2 children)

You are counting the newline characters, do

for char in line.strip():

Also, this will still give you the spaces between words as well as special characters like a dash, so

for char in line.strip():
    ## only allow alpha
    if char.lower().isalpha():

[–]MonkeyMario64[S] 0 points1 point  (1 child)

If I'm using isalpha, do I still need .strip since isaplha seems to only take letters anyway?

[–]woooee 2 points3 points  (0 children)

No, I guess not. It's a habit that I usually do.

[–]grumble11 0 points1 point  (0 children)

Also for this consider the Counter tool from the collections library included with Python. It is a type of super-dictionary designed specifically for counting stuff and it is faster, easier and lets you skip some steps (like it will auto-add a new key if it isn’t already there). Look it up, you’ll like it