Question: In Python: Goals At the completion of this lab, you should gain experience working with and processing files gain experience working with strings, slices and
In Python:
Goals
At the completion of this lab, you should
- gain experience working with and processing files
- gain experience working with strings, slices and string operators and function
- gain additional experience working with in and not in operators
- gain additional experience working with loops
- gain additional experience working with the counter pattern
- gain additional experience working with the guardian pattern
- gain additional experience performing incremental development
- gain additional experience working with IDLE to create a Python application
Getting Started
For this lab you will write two programs to search a word list file words_alpha.txt
(courtesy of dwyl (Links to an external site.)).
>>> Download the word list file and save the file as words_alpha.txt in the same folder you will save your Python program files.
The first program will be a word checking program (useful for Scrabble). The second program returns information about the list of words.
Word Check Program
This first program will prompt the user for input, exit if nothing entered, or search through the list of words to determine if the word (case insensitive) is found.
>>> Create a file called cis122-lab07-word-check.py.
Remember the following when working with the words from the file:
- Remove any white space or line ending characters from the word list
- Remove any white space from the user entered word
- Perform a case insensitive comparison
- Use a file object method to start at the beginning of the file
- Open and close between searches, or reset the file pointer
We will work through this first program in a series of steps. You will have to solve the second program on your own.
Tip: The word list file is very long, so you should avoid printing each line, or any type of output that will produce output based on the number of lines in the file. As an alternative, you can create a much smaller file with only a few lines for testing during development.
Step 1: Input
>>> Outline the steps for input, implement the steps, and test to ensure your code works.

Below are suggested steps:
- Loop until nothing entered
- Prompt for input
- Remove leading/trailing spaces
- Exit if nothing entered
- Perform search
STOP! Feel free to create your own steps, and implement the steps above or your own steps before reviewing the solution below.
Step 1 Solution
# Loop until nothing entered while True: # Prompt for input word = input("Enter a search word (blank to exit): ") word = word.strip() # Remove leading/trailing if len(word) == 0: # Exit if nothing entered break else: # Perform search print(word)Step 2: Open, Read and Close File
The next step is to open the file, loop through the file contents, and close the file. We need to open and close the file with each word search to ensure we start over at the beginning of the file. We do not want to list out every word in the file, so instead we'll add test code (later removed) to count the number of lines in the file.
Note: We are not yet attempting to find the word.
>>> Outline the steps for file handling, implement the steps, and test to ensure your code works.

Below are suggest steps:
- Open file
- Search for word by looping over file
- Close file
- Print results
STOP! Feel free to create your own steps, and implement the steps above or your own steps before reviewing the solution below.
Step 2 Solution
while True ... if len(word) == 0: ... else: # Perform search # Open file fin = open("words_alpha.txt") # Open file # Search for word by looping over file count = 0 for line in fin: count = count + 1 # Close file fin.close() # Print results print(count)Step 3: Word Search and Results
The last step is to add code to perform the word search and output the results.
>>> Outline the steps for performing the word search and results, implement the steps, and test to ensure your code works.

Below are suggest steps:
- Initialize default search result to False
- Remove file line leading/trailing white space and line endings
- Test for match changing to same case
- If found set result status and exit loop
- Print results (updated)
STOP! Feel free to create your own steps, and implement the steps above or your own steps before reviewing the solution below.
Step 3 Solution
Below is the final solution that includes the solution to the final step.
# Loop until nothing entered while True: # Prompt for input word = input("Enter a search word (blank to exit): ") # Remove leading/trailing word = word.strip() # Remove leading/trailing if len(word) == 0: # Exit if nothing entered break else: # Perform search # Open file fin = open("words_alpha.txt") # Open file # Search for word by looping over file # Initialize default search result to False word_found = False for line in fin: # Remove file line leading/trailing white space and line endings line = line.strip() # Test for match changing to same case if word.upper() == line.upper(): # Set result status and exit loop word_found = True break # Close file fin.close() # Print results if word_found: print("Word " + word + " found") else: print("Word " + word + " not found")File Information
The second program will loop over the input file and update information about the file. Use what you learned in the first program to create this second program.
>>> Create a file called cis122-lab07-word-stats.py.
You will need to use the round() function to round percentages to nearest hundredths value.
Functions are optional, so you may create your own functions to make the final solution easier.
Below is the sample output from this program.

Transcribed image text
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
