Question
Deliverables There is one deliverable for this assignment hw0.py Make sure the script obeys all the rules in Homework Script Rules Specification This script must
Deliverables
There is one deliverable for this assignment
- hw0.py
Make sure the script obeys all the rules in Homework Script Rules
Specification
This script must read in a text file containing a list of students and their scores on quizzes.
The file has entries like the following
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 ...
The first value on each line is a student ID and the rest are quiz scores.
The script should create two dictionaries and print them.
The keys of both dictionaries are student_id.
The values of the first dictionary must be an list of integer scores for each student.
The values of the second dictionary must be the rounded average of quiz scores for each student.
The script must have 4 functions:
- open_file_read
- quiz_dictionary_create
- average_dictionary_create
- print_dictionary
open_file_read
This function must have the following header:
def open_file_read(filename):
It must try to create a file object for reading on the file whose name is given by the parameter filename.
If it is succesful in creating the file object it should return the object.
If it is cannot create the object it should print an error message and return None.
quiz_dictionary_create
This function must have the following header:
def quiz_dictionary_create(file):
The function must create a dictionary where the id is the key and a list of grades is the value.
The grades in the list must be stored as integers.
The function must return this dictionary.
average_dictionary_create
This function must have the following header:
def average_dictionary_create(dict):
The function must create a dictionary where the id is the key and the average score is the value.
The average score must be an integer.
The function must return this dictionary.
print_dictionary
This function must have the following header:
def print_dictionary(dict):
The function must print the key and value for each entry in the dictionary sorted by key.
Script for this assignment
Open an a text editor and create the file hw4.py.
You can use the editor built into IDLE or a program like Sublime.
Test Code
Your hw0.py file must contain the following test code at the bottom of the file:
file = open_file_read("quiz_scores.txt") scores = quiz_dictionary_create(file) print_dictionary(scores) print() averages = average_dictionary_create(scores) print_dictionary(averages)
For this test code to work, you must copy quiz_scores.txt to your machine.
To do this use FileZilla to copy the file quiz_scores.txt from /home/ghoffman/course_files/it117_files into the directory that holds your hw4.py script.
Run the script entering quiz_scores.txt when prompted.
Suggestions
Write this program in a step-by-step fashion using the technique of incremental development.
In other words, write a bit of code, test it, make whatever changes you need to get it working, and go on to the next step.
- 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.
- Replace the pass statement in open_file_read with the body of the code from your hw3.py script. Copy the first line of the test code into the bottom of 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.
- 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 object file. Inside the for loop write a print statement to print the line. Copy the second line of the test code into the bottom of the file. Run the script. Fix any errors you find.
- Remove the print statement. Run split on the line and store it in the variable fields. Print fields. Run the script. Fix any errors you find.
- Remove the print statement. Set the variable id to the first element in fields. Print id. Run the script. Fix any errors you find.
- Remove the print statement. Create the empty list scores. Write a for loop that loops over the all but the first element of fields. In other words the loop must skip the first element which is an id, not a grade. You can do with the range function using indexing. Or you could use a slice. Inside the for loop print each element. Run the script. Fix any errors you find.
- 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. Fix any errors you find.
- Replace the print statement with a line that adds an entry to the dictionary quiz_scores. id will be the key and grades will be the value. After the outer for loop print quiz_scores. Run the script. Fix any errors you find.
- 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. Add the third line of the test code the bottom of the script. Run the script. Fix any errors you find.
- Remove the pass statement from average_dictionary_create. Replace it with a statement that creates an empty dictionary. Write a for loop that loops through the dictionary parameter dict. Use id as the loop variable. Inside the loop create the variable grades and set it equal to the value associated with id. Print grades. Add the next two lines from the test code to the bottom of the script. Run the script. Fix any errors you find.
- Remove the print statement. Create the variable total and set it to 0. Use a for loop to iterate through the elements in grades. Inside the loop add each element to total. After the loop print total. Run the script. Fix any errors you find.
- Remove the print statement. Calculate the average grade and set it equal to the variable average. The value of average must be an integer. 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. Fix any errors you find.
- Remove the print statement. Replace it with a statement that returns the dictionary. Add the last line of the test code to the bottom of the script. Run the script. Fix any errors you find.
Testing on Your Machine
- Open IDLE
- Use the Open command in IDLE to open hw4.py
- Under the Run menu, select Run Module
- Your output should look something like this
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] 01583835 70 03201438 74 03981869 68 04352948 72 07787151 76
quiz_scores.txt file content
95 Alan Ball 98 Eddie Bauer 80 Dan Franks 90 Anthony Charles 90 Pat Dempsey 90 Octavia Drale 93 Jack Framls 85 Boris Hubble 90 Jenny Hanleu 50 Jean Marks 73 Sean Simons 95 Nancy Dooley 95 Karem Simms 78 Justin Barney 100 Joseph Smathers 63 John Henry 60 Nick Reading 95 Victoria Jewel 45 Joseph Sanders 97 Irene Mitchel 88 Charles Levy 92 Anthony Sampson
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started