IT 117: Intermediate to Scripting
Homework 4

Due

Sunday, February 22nd at 11:59 PM

What You Need to Do

Setup On Your Machine

Specification

Functions

open_file_read


quiz_dictionary_create

create an empty dictionary
for each line in the file
    run split on the line to create the list fields
    remove the first element of fields and save it as the student ID
    create an empty list scores
    for each element in fields
        convert the element into an integer
        add this integer to scores
    return the dictionary

average_dictionary_create

for each entry in the dictionary
    store the key
    get the list of scores
    set total to 0
    for each score in the list of scores
        add score to total
        get the average by dividing total by the number of scores
        create a dictionary entry with this ID and the average
    return the dictionary

print_dictionary

Test Code

Output

Suggestions

  1. Create the file hw4.py.
    Enter the headers for each of the required functions.
    Under each header write the Python statement pass.
    Run the script.
    Fix any errors you find.
  2. Replace the pass statement in open_file_read with the body of the code from your hw3.py script.
    Copy the test code to the bottom of the file.
    Comment out all but the first line in the file.
    Run the script entering both a real filename and the name of a file that does not exists.
    Fix any errors you find.
  3. Remove the pass statement from quiz_dictionary_create.
    Replace it with a statement that creates an empty dictionary named quiz_scores.
    Write a for loop that loops through the file using the file object file.
    Inside the for loop write a print statement to print the line.
    Uncomment the second line of the test code.
    Run the script.
    You should see something like this
    04352948 77 84 51 71 72 92 85 51 75 66 
    
    03981869 76 73 94 53 59 62 86 50 70 52 
    
    03201438 68 50 71 71 83 95 64 62 83 96 
    
    01583835 57 69 66 88 69 77 80 79 53 57 
    
    07787151 81 78 61 89 74 73 87 77 92 52 
    Fix any errors you find.
  4. Remove the print statement.
    Run split on the line and store it in the variable fields.
    Print fields.
    Run the script.
    You should see
    ['04352948', '77', '84', '51', '71', '72', '92', '85', '51', '75', '66']
    ['03981869', '76', '73', '94', '53', '59', '62', '86', '50', '70', '52']
    ['03201438', '68', '50', '71', '71', '83', '95', '64', '62', '83', '96']
    ['01583835', '57', '69', '66', '88', '69', '77', '80', '79', '53', '57']
    ['07787151', '81', '78', '61', '89', '74', '73', '87', '77', '92', '52']
    Fix any errors you find.
  5. Remove the print statement.
    Set the variable id to the first element fields.
    Do this by calling the list method pop with an argument of 0.
    This will also remove the first element from fields.
    Print id.
    Run the script.
    You should see
    04352948
    03981869
    03201438
    01583835
    07787151
    Fix any errors you find.
  6. Remove the print statement.
    Create the empty list scores.
    Write a for loop that loops over the remaining elements of fields.
    Inside the for loop print each score.
    You should see
    77
    84
    51
    71
    ...
    Run the script.
    Fix any errors you find.
  7. Remove the print statement.
    Replace it with a statement that adds the element to scores after converting it to an integer.
    Outside the inner for loop print scores.
    Run the script.
    You should see
    [77, 84, 51, 71, 72, 92, 85, 51, 75, 66]
    [76, 73, 94, 53, 59, 62, 86, 50, 70, 52]
    [68, 50, 71, 71, 83, 95, 64, 62, 83, 96]
    [57, 69, 66, 88, 69, 77, 80, 79, 53, 57]
    [81, 78, 61, 89, 74, 73, 87, 77, 92, 52]
    Fix any errors you find.
  8. Replace the print statement with a line that adds an entry to the dictionary quiz_scores.
    id will be the key and scores will be the value.
    Outside both for loops, print quiz_scores.
    Run the script.
    You should see something like this
    {'04352948': [77, 84, 51, 71, 72, 92, 85, 51, 75, 66], '03981869': [76, 73, 94, 53, 59, 62, 86, 50, 70, 52], ... 
    Fix any errors you find.
  9. Remove the print statement.
    Replace it with a statement that returns quiz_scores.
    Remove the pass statement from print_dictionary.
    Replace it with the body of the code from print_dictionary in hw3.py.
    Uncomment the third line of the test code.
    Run the script.
    You should see
    01583835 [57, 69, 66, 88, 69, 77, 80, 79, 53, 57]
    03201438 [68, 50, 71, 71, 83, 95, 64, 62, 83, 96]
    03981869 [76, 73, 94, 53, 59, 62, 86, 50, 70, 52]
    04352948 [77, 84, 51, 71, 72, 92, 85, 51, 75, 66]
    07787151 [81, 78, 61, 89, 74, 73, 87, 77, 92, 52]
    Fix any errors you find.
  10. Remove the pass statement from average_dictionary_create.
    Replace it with a statement that creates the empty dictionary quiz_averages.
    Write a for loop that loops through the dictionary dict.
    Use id as the loop variable.
    Inside the loop create the variable scores and set it equal to the value associated with id.
    Print scores.
    Uncomment the next two lines from the test code.
    Run the script.
    You should see
    01583835 [57, 69, 66, 88, 69, 77, 80, 79, 53, 57]
    03201438 [68, 50, 71, 71, 83, 95, 64, 62, 83, 96]
    03981869 [76, 73, 94, 53, 59, 62, 86, 50, 70, 52]
    04352948 [77, 84, 51, 71, 72, 92, 85, 51, 75, 66]
    07787151 [81, 78, 61, 89, 74, 73, 87, 77, 92, 52]
    
    [77, 84, 51, 71, 72, 92, 85, 51, 75, 66]
    [76, 73, 94, 53, 59, 62, 86, 50, 70, 52]
    [68, 50, 71, 71, 83, 95, 64, 62, 83, 96]
    [57, 69, 66, 88, 69, 77, 80, 79, 53, 57]
    [81, 78, 61, 89, 74, 73, 87, 77, 92, 52]
    Fix any errors you find.
  11. Remove the print statement.
    Create the variable total and set it to 0.
    Use a for loop to iterate through the values in scores.
    Inside the loop add each element to total.
    After the loop print total.
    Run the script.
    You should see
    01583835 [57, 69, 66, 88, 69, 77, 80, 79, 53, 57]
    03201438 [68, 50, 71, 71, 83, 95, 64, 62, 83, 96]
    03981869 [76, 73, 94, 53, 59, 62, 86, 50, 70, 52]
    04352948 [77, 84, 51, 71, 72, 92, 85, 51, 75, 66]
    07787151 [81, 78, 61, 89, 74, 73, 87, 77, 92, 52]
    
    724
    675
    743
    695
    764
    Fix any errors you find.
  12. Remove the print statement.
    Calculate the average grade and set it equal to the variable average.
    The average is total divided by the length of the list scores.
    Turn this value into an integer using round.
    Create an entry in the dictionary where id is the key and average is the value.
    After the outer loop, print the dictionary.
    Run the script.
    You should see
    01583835 [57, 69, 66, 88, 69, 77, 80, 79, 53, 57]
    03201438 [68, 50, 71, 71, 83, 95, 64, 62, 83, 96]
    03981869 [76, 73, 94, 53, 59, 62, 86, 50, 70, 52]
    04352948 [77, 84, 51, 71, 72, 92, 85, 51, 75, 66]
    07787151 [81, 78, 61, 89, 74, 73, 87, 77, 92, 52]
    
    {'04352948': 72, '03981869': 68, '03201438': 74, '01583835': 70, '07787151': 76}
    Fix any errors you find.
  13. Remove the print statement.
    Replace it with a statement that returns the dictionary.
    Uncomment the last line of the test code.
    Run the script.
    Fix any errors you find.

Testing on Your Machine

Copy the Script to Unix

Testing the Script on Unix (Optional)

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