This is my solution resulting in an error. Returns 0
PS: I\'d still love a fix to my code :)
from collections import Counter
import
MattBryant's answer is a good one, but if you want to exclude more types of letters than just spaces, it will get clunky. Here's a variation on your current code using Counter
that will work:
from collections import Counter
import string
def count_letters(word, valid_letters=string.ascii_letters):
count = Counter(word) # this counts all the letters, including invalid ones
return sum(count[letter] for letter in valid_letters) # add up valid letters
Example output:
>>> count_letters("The grey old fox is an idiot.") # the period will be ignored
22