IT 117: Introduction to Scripting
Homework 3
Due
Sunday, February 12th at 11:59 PM
What You Need to Do
- Create the script hw3.py
- Make sure it obeys the rules in
Rules for Homework Scripts
- Make sure the script has a hashbang line and is executable
- Move it to an an hw3
directory on pe15.cs.umb.edu
Setup On Your Machine
- Open a text editor.
I would suggest the text editor built into the program IDLE
.
- Save the file as hw3.py
- Copy the file full_student_data.txt from
/home/ghoffman/course_files/it117_files.
Use FileZilla to do this.
Specification
- This script reads in a file containing data for a
class
- It then creates a dictionary to hold this data
- The script will then print this dictionary
- The script must contain 3 functions
- open_file_read
- student_dictionary_create
- student_dictionary_print
Functions
open_file_read
student_dictionary_create
student_dictionary_print
Test Code
- Your script must contain the following statements
- They should appear at the bottom of your script
filename = input("File name: ")
file = open_file_read(filename)
if file:
students = student_dictionary_create(file)
if students:
student_dictionary_print(students)
- This code will only work if you have copied
full_student_data.txt into the same directory
as your script
- You will find this file in
/home/ghoffman/course_files/it117_files
Output
Suggestions
- Write this script in stages
- Test your script at each step
- Print the steps below
- And check them off as you finish each one
-
Create the file hw3.py.
Enter the headers for open_file_read,
student_dictionary_create and
student_dictionary_print.
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 hw2.py.
Copy the test code into the bottom of the file.
Comment out all but the first two lines.
Run the scrip entering both a real filename and the name of a file
that does not exists.
Fix any errors you find.
-
Remove the
pass
statement in
student_dictionary_create.
Replace it with a for
loop on file using
line as the loop variable.
Inside the loop, print line.
Uncomment the next two lines in the test code..
Run the script entering full_student_data.txt
when prompted.
Fix any errors you find.
-
Remove the
print
statement.
Create the list variable fields by calling
the split method on
line.
If you are not sure what I mean, see
Class Exercise 2.
Print the value of fields.
Run the script.
Fix any errors you find.
-
Remove the
print
statement.
Create the variables id,
first, last,
user and email
from fields using indexing.
Again, see
Class Exercise 2
for examples.
Print these new variables.
Run the script.
Fix any errors you find.
-
Remove the
print
statement.
Create the tuple data containing
the values of first, last,
user and email.
Print this tuple.
Run the script.
Fix any errors you find.
-
Remove the
print
statement.
Above the for
loop, create the empty dictionary
students.
As the last statement in the for
loop, create an entry in
students with id as the
key and data as the value,
Outside the for
loop print
students.
Run the script.
Fix any errors you find.
-
Remove the
print
statement.
Replace it with a return
statement
that returns students.
In student_dictionary_print replace the
pass
statement that prints the parameter
students.
Uncomment the last two lines in the test code.
Run the script.
Fix any errors you find.
-
Remove the print statement.
Replace it with a for
loop over
students using
id as the loop variable.
The loop should be sorted by id.
Inside the for
loop print id.
Run the script.
Fix any errors you find.
-
Remove the
print
statement.
Create the tuple variable data
from the value associated with id
in the students dictionary.
Create the variables first,
last, user and
email from data
using indexing.
Print id, first,
last, user and
email on a single line.
Run the script.
Fix any errors you find.
Testing on Your Machine
Copy the Script to Unix
- Open FileZilla and connect to
pe15.cs.umb.edu
- Go to your it117 directory
- Go to your hw directory
- Right-click in the whitespace inside the
hw directory
- Enter hw3 in the dialog box
- Click and drag your script from the bottom left panel
to the bottom right panel
- Right-click on the file and select "Permissions" from
the menu
- Enter 755 in the box provided
- This will make the script executable
- Click and drag
full_student_data.txt from the bottom left
panel to the bottom right panel
Testing the Script on Unix (Optional)
Copyright © 2020 Glenn Hoffman. All rights reserved. May not be reproduced without permission.