02 Teach : Team Activity
Overview
Write a Python 3 program to read the contents of a file and count the occurrences of a particular word.
Instructions
Special Instructions for Online Students
There is a team activity every week that will require you to meet synchronously with a small group of other students.
For this week's activity, while teams are still being finalized, you are not expected to meet with anyone for this activity. Instead, feel free to collaborate with others via the course discussion.
Despite the fact that you will not meet with a team, you are still expected to complete the activity, including submitting the quiz, so that you will understand what is expected for future weeks. When completing the quiz for this week, please go ahead and mark "yes" to the questions about meeting with your team, being on-time, etc. (For future weeks, you will answer these questions according to what you actually do.)
Write a program that prompts the user for a file, reads it line by line, splits it into words, and then counts the number of times the word "pride" occurs.
Use the following two files for testing:
/home/cs241/ta02/burton.txt- Very small. Useful for first pass testing./home/cs241/ta02/benson.txt- Much larger...useful for pondering ;-).
To accomplish this task, complete the following:
Core Requirements
Create a function
prompt_filenamethat prompts the user for a filename and returns it. Create amainfunction that calls theprompt_filenamefunction, and displays the text,Opening file '...'replacing the "..." with the actual filename. Then, use the__name__syntax to call the initial main function. Run your program and ensure that it words correctly.Create another function,
parse_filethat should receive a filename frommain. It should then open the file and read through it line by line and word by word. For testing purposes, at this point print out each word in the file at this point.Change your
parse_filefunction so that it does not print anything out, but instead counts the number of times the word "pride" occurs. Have the function return this number and then change main to display this to the screen (e.g., "The word pride occurs xx times in this file").
Stretch Challenges
After finishing the core requirements, help everyone on your larger team complete them. When everyone has completed the core requirements, please move on to these stretch challenges.
Change your program so that it is case-insensitive. In other words, both "Pride" and "pride" should be counted.
Change your program so that it can count any word, not just "pride". Add a function to prompt the user for the word of their choice, then pass that word to the
parse_filefunction and use it when displaying your results.Change your program so that it counts any words that contain the user's word as well. For example, if the user enters "pride" the words "pride" and "prideful" would both be counted.
Instructor's Solution
As a part of this team activity, you are expected to look over a solution from the instructor, to compare your approach to that one. One of the questions on the I-Learn submission will ask you to provide insights from this comparison.
Please DO NOT open the solution until you have worked through this activity as a team for the one hour period. At the end of the hour, if you are still struggling with some of the core requirements, you are welcome to view the instructor's solution and use it to help you complete your own code. Even if you use the instructor's code to help you, you are welcome to report that you finished the core requirements, if you code them up yourself.
After working with your team for the one hour activity, click here for the instructor's solution.
Submission
When you have finished this activity, please fill out the assessment in I-Learn. You are welcome to complete any additional parts of this activity by yourself or with others after class before submitting the assessment.