Assignment: Count Dictionary Words
Server Academy Members Only
Sorry, this lesson is only available to Server Academy members. Create a free account now to get instant access to this and more free courses. Click the Sign Up Free button below to get access to our free courses now, or sign in if you have an account.
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:
- Start by defining the
count_words
function 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 statement to check the length of the current word and increment the appropriate count in the dictionary.
- 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..
Saving Progress...
Python 3 for Beginners
Installing Python on Windows • 1hr 17min
0 / 4 lessons complete
Section Overview
Free Preview Lesson
Text | 1 min
Downloading and Installing Python on Windows
Free Preview Lesson
Text | 8 min
Installing and configuring VS Code for Python
Free Preview Lesson
Text | 8 min
Lab: Installing Python
Devops Lab | 60
Python Basics • 28min
0 / 7 lessons complete
Section Overview
Text | 2 min
Executing Python Code
Free Preview Lesson
Text | 3 min
Python 3 Syntax
Free Preview Lesson
Text | 5 min
Help! Python Keywords
Free Preview Lesson
Text | 4 min
Printing to the console!
Text | 5 min
Python Operators
Text | 4 min
Section Review
Quiz | 5 min
Python Variables • 41min
0 / 8 lessons complete
Section Overview
Text | 4 min
Casting
Text | 4 min
Strings
Text | 5 min
Booleans
Text | 8 min
User Input
Text | 2 min
Numbers
Text | 7 min
NoneType
Free Preview Lesson
Text | 5 min
Assignment: Write a Mad Libs Script
Text | 6 min
Even more Python Variables! • 41min
0 / 6 lessons complete
Python Lists
Text | 8 min
Python Tuples
Text | 7 min
Python Sets
Text | 7 min
Frozensets
Text | 6 min
Dictionaries
Text | 8 min
Iterator and Iterable
Text | 5 min
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
Section Overview
Text | 3 min
Installing Python PIP
Text | 4 min
Installing Modules with PIP
Text | 5 min
Importing Modules
Text | 6 min
RegEx • 26min
0 / 4 lessons complete
Section Overview
Text | 4 min
Regex 101
Text | 10 min
Importing Regex and manipulating strings
Text | 7 min
Regex Challenge!
Text | 5 min
Working with APIs • 12min
0 / 3 lessons complete
Making HTTP Requests
Text | 3 min
Working with JSON
Text | 5 min
Get your weather with the OpenWeatherMap
Text | 4 min
Course Conclusion • 2min
0 / 1 lessons complete