IT 117: Intermediate Scripting
Homework 4

Due

Sunday, February 23rd at 11:59 PM

What You Need to Do

Setup On Your Machine

Specification

Functions

open_file_read

word_frequencies_create

word_frequency_print

Test Code

Output

Suggestions


  1. Create a file with the hashbang line.
    Copy and paste the headers for each of the three functions into the scrpt.
    The code block for each of these functions should be the single Python statement pass.
    Pass does nothing but it will keep the code from giving syntax errors.
    Run the code.
    You should see nothing.
    If you have errors, fix them.
  2. Remove the pass statement in open_file_read.
    Create a try/except statement.
    In the try block, add a line that opens a file for reading using the filename parameter.
    Add a statement that returns a pointer to this object.
    In the except block, write code to print an error message if the file cannot be opened.
    Paste the test code in the bottom of the file.
    Comment out the last two lines of the test code by typing # at the beginning of each line.
    Run the code.
    You should see
    Cannot open xxxxxxx
    If you see something else, fix it.
  3. Remove the pass statement in word_frequencies_create.
    Create an empty dictionary called word_frequencies.
    Create a for loop that prints each line in the file.
    Run the code and fix any errors.
  4. You need to convert all words into lowercase.
    To do this insert an assignment statement before the print statement that will turn all the uppercase characters in the line to lowercase using the lower string method.
    Print the line.
    Run the code and fix any errors.
  5. Remove the print statement.
    Use the split string method to create a list of all the words in the file and assign this list to the variable words.
    Print this list.
    Run the code and fix any errors.
  6. Remove the previous print statement.
    Add a new for loop inside the old for loop using word as the loop variable.
    Inside the loop print each word.
    Run the code and fix any errors.
  7. Remove the previous print statement.
    Write an if statement that checks whether the word is NOT already in the dictionary word_frequencies.
    If the word is not already in the dictionary, create an entry in the dictionary with the word as the key and a value of 1.
    Add a line to print the dictionary.
    This line must be outside both for loops but still inside the body of the function.
    Make sure you get the indentation right.
    Run the code.
    You should see many words, but all the values should be 1.
    Fix any errors you find.
  8. Add an else clause to the if statement inside the second for loop.
    In the code block inside this else clause write an assignment statement that sets the variable count to the value associated with word inside word_frequencies.
    Now set the value associated with word to count + 1.
    Run the code.
    You should see words with many different values.
    Fix any errors you find.
  9. Remove the print statement from the end of the function.
    Replace it with a statement that returns the word_frequencies dictionary.
    Remove the pass statement from word_frequency_print code block.
    Replace it with a statement that prints the parameter frequencies.
    Run the code.
    The output should be similar to what you saw at the last step.
    Fix any errors you find.
  10. Replace the print statement with a for loop that prints the word and word count value for each word in the dictionary.
    Run the code and fix any errors.
  11. Change the for loop so it prints the words in alphabetical order.

Output

When you run the program the output should look something like this

Cannot open file xxxxxxx
a 7
above 1
add 1
advanced 1
ago 1
all 1
altogether 1
and 6
any 1
...
war 2
we 10
what 2
whether 1
which 2
who 3
will 1
work 1
world 1
years 1

Copy the file to Unix

Testing on Your Machine

Copy the Script to Unix

Testing the script on Unix (Optional)

Testing the Script on Unix (Optional)

Copyright © 2022 Glenn Hoffman. All rights reserved. May not be reproduced without permission.