I need to build a function that takes as input a string and returns a dictionary.
The keys are numbers and the values are lists that contain the unique words that have a number of letters equal to the keys.
For example, if the input function is as follows:
n_letter_dictionary("The way you see people is the way you treat them and the Way you treat them is what they become")
The function should return:
{2: ['is'], 3: ['and', 'see', 'the', 'way', 'you'], 4: ['them', 'they', 'what'], 5: ['treat'], 6: ['become', 'people']}
The code that I have written is as follows:
def n_letter_dictionary(my_string):
my_string=my_string.lower().split()
sample_dictionary={}
for word in my_string:
words=len(word)
sample_dictionary[words]=word
print(sample_dictionary)
return sample_dictionary
The function is returning a dictionary as follows:
{2: 'is', 3: 'you', 4: 'they', 5: 'treat', 6: 'become'}
The dictionary does not contain all the words with the same number of letters but is returning only the last one in the string.