The question asks to write a function creating a dictionary with the count of each word in the string and only remove the punctuation if it is the last character in the word. I've been trying to solve the punctuation part of the problem. For the assignment I need to identify the punctuation using isalpha
, but
- I'm not sure if using the
word[-1]
is helping to identify if the last character is punctuation and - I don't know how to write the else statement to get the full dictionary to produce.
def word_distribution(s):
s = s.lower()
s = s.split()
wordfreq = {}
for word in s:
if word[-1].isalpha():
if word in wordfreq:
wordfreq[word] += 1
else:
wordfreq[word] = 1
return wordfreq
Example of what my code is producing...
word_distribution("Why's it always the nice guys?")
Out : {'always': 1, 'it': 1, 'nice': 1, 'the': 1}
Example of what it should be producing.....
Out : {'always': 1, 'it': 1, 'nice': 1, 'the': 1, 'why's': 1, 'guys': 1}