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:

words = ['apple', 'banana', 'carrot', 123, 'dog', 'elephant', 'fish']
count_words(words)

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:

  1. Start by defining the count_words function and creating an empty dictionary to store the counts.
  2. Next, use a for loop to iterate through the list of words and process each word.
  3. 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.
  4. Use an if statement to check the length of the current word and increment the appropriate count in the dictionary.
  5. After processing all of the words, return the counts dictionary.

Remember, you can use the len() function to determine the length of a string. You can also use the continue keyword inside a try, except block to move on to the next iteration of the loop without executing any more code in the current iteration.

Try implementing these steps on your own and see how it works! If you get stuck, you can refer to the solution provided below for guidance. Good luck!

Solution

Solution

Server Academy Members Only

Want to access this lesson? Just sign up for a free Server Academy account and you'll be on your way. Already have an account? Click the Sign Up Free button to get started..

0 0 votes
Lesson Rating
Subscribe
Notify of
profile avatar
0 Comments
Inline Feedbacks
View all comments

Saving Progress...

Sign up for free!

Sign up for free and get instant access to this course!.

Python 3 for Beginners

0%

0/1 Lessons

Installing Python on Windows

• 1hr 17min

0 / 4 lessons complete

Python Basics

• 28min

0 / 7 lessons complete

Python Variables

• 41min

0 / 8 lessons complete

Even more Python Variables!

• 41min

0 / 6 lessons complete

Conditional Statements

• 15min

0 / 3 lessons complete

Writing Functions

• 30min

0 / 5 lessons complete

Python Loops

• 23min

0 / 5 lessons complete

Python PIP and Modules

• 18min

0 / 4 lessons complete

RegEx

• 26min

0 / 4 lessons complete

Working with APIs

• 12min

0 / 3 lessons complete

Course Conclusion

• 2min

0 / 1 lessons complete