Assignment: Count Dictionary Words
For this challenge, you will be creating a Python function that uses the concepts of if statements, try, except, dictionaries, and iterators/iterables to process a list of words. The function should take in a list of words as an input and return a dictionary containing the following information:
- A count of the total number of words in the list
- A count of the number of words that are 5 characters or less
- A count of the number of words that are more than 5 characters but less than or equal to 10 characters
- A count of the number of words that are more than 10 characters
To complete this challenge, you will need to use if statements to check the length of each word and increment the appropriate count in the dictionary. You will also need to use a try, except block to handle any words that are not strings. Additionally, you will need to use an iterator or iterable to loop through the list of words and process each word.
Here is an example of how the function should behave:
The output should be the following:
>> {'total': 7, '5_or_less': 3, '5-10': 3, 'more_than_10': 0}
Note: In the example above, the non-string value 123 is ignored and not included in the counts.
Try implementing this function on your own and see how it works! Once you have written the function, you can test it using the words list provided above. Good luck!
Hints
Here are some hints that may help you solve this exercise:
- Start by defining the
count_wordsfunction and creating an empty dictionary to store the counts. - Next, use a for loop to iterate through the list of words and process each word.
- Inside the for loop, use a try, except block to handle any non-string values in the list. This will allow you to continue processing the remaining words without stopping the program.
- Use an if statemen…
No comments yet. Add the first comment to start the discussion.