IT 117: Introduction to Scripting
Homework 3

Due

Sunday, February 11th at 11:59 PM

Deliverables

There is one deliverable for this assignment

Make sure the script obeys all the rules in Homework Script Rules

Specification

This script must read in a text file containing data from the periodic table of elements.

The file has entries like the following

H Hydrogen 1 1.008
He Helium 2 4.0026
Li Lithium 3 6.94
...

The script should create a dictionary where the keys are the element symbols and the values are lists containing the name, the atomic number and the atomic weight.

It should then print the dictionary.

The script must have 3 functions:

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.

element_dictionary_create

This function must have the following header:

def element_dictionary_create(file):

The function must read in a file using the file object file.

It must loop through the file and create dictionary where the symbols are the keys and the values a list consisting of the name, the atomic number and the atomic weight.

The function must return the dictionary it creates.

print_elements

This function must have the following header:

def print_elements(dict):

The function must print the symbol for each element followed by a string consisting of the name, he atomic number and the atomic weight.

It should NOT print the list associated with each symbol.

Script for this assignment

Open an a text editor and create the file hw3.py.

You can use the editor built into IDLE or a program like Sublime.

Test Code

Your hw3.py file must contain the following test code at the bottom of the file:

filename = input("File name: ")
file = open_file_read(filename)
if file:
    elements = element_dictionary_create(file)
    if elements:
        print_elements(elements)

For this test code to work, you must copy elements.txt to your machine.

To do this use FileZilla to copy the file elements.txt from /home/ghoffman/course_files/it117_files into the directory that holds your hw3.py script.

Run the script entering elements.txt when prompted.

You should see

$ ./hw3.py 
H Hydrogen 1 1.008
He Helium 2 4.0026
Li Lithium 3 6.94
...
Es Einsteinium 99 252.08 
Fm Fermium 100 257.10 
Md Mendelevium 101 258.10

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.

  1. Create the file hw3.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 hw2.py script.
    Copy 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.
  3. Replace the pass statement in element_dictionary_create with a statement that creates the empty dictionary elements.
    Now write a for loop that prints each line in the file.
    Run the script entering elements.txt when prompted.
    Fix any errors you find.
  4. Replace the print statement in with an assignment statement that uses split to give values to the variables symbol, name, number and weight.
    For an example of what I mean, see Class Exercise 3.
    Print the values of these variables.
    Run the script.
    Fix any errors you find.
  5. Remove the print statement.
    Now you need to make an entry in the elements dictionary.
    The key will be symbol and the value will be a list containing name, number and weight.
    Write a print statement that prints the key and the value. Run the script.
    You should see
    H ['Hydrogen', '1', '1.008']
    He ['Helium', '2', '4.0026']
    Li ['Lithium', '3', '6.94']
    ...
    Fix any errors you find.
  6. Remove the print statement from element_dictionary_create.
    Outside the for loop return the dictionary elements.
    Remove the pass statement from print_elements.
    Replace it with a line that prints the value of the parameter, dict.
    Run the script.
    Fix any errors you find.
  7. Remove the print statement.
    Replace it with a for loop over the dictionary dict using symbol as the loop variable.
    Inside the for loop print the value of symbol.
    Run the script.
    Fix any errors you find.
  8. Remove the print statement.
    The value associated with each entry in the dictionary is a list consisting of the element name, the atomic number and the atomic weight.
    Inside the for loop get this value from the dictionary and assign it to the variable element_data.
    Then print the values of symbol and element_data.
    Run the script.
    Fix any errors you find.
  9. Now we need to change the call to print.
    Keep the first argument symbol, but delete the 2nd argument element_data.
    Replace it with the following text "element_data[0], element_data[1], element_data[2]".
    Run the script.
    Your output should look like this
    H Hydrogen 1 1.008 
    He Helium 2 4.0026 
    Li Lithium 3 6.94 
    ...
    Fix any errors you find.

Testing on Your Machine

Copy the file to Unix

Make the File Executable

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