There is one deliverable for this assignment
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:
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
.
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.
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.
Open an a text editor and create the file hw3.py.
You can use the editor built into IDLE or a program like Sublime.
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
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.
pass
. pass
statement in open_file_read
with the body of the code
from your hw2.py script. pass
statement in element_dictionary_create
with a statement that creates the empty dictionary elements. for
loop that prints each line in the file. print
statement in with an assignment statement
that uses split to give values to the
variables symbol, name,
number and weight. print
statement. print
statement that prints the key and the value.
Run the script. H ['Hydrogen', '1', '1.008'] He ['Helium', '2', '4.0026'] Li ['Lithium', '3', '6.94'] ...Fix any errors you find.
print
statement from element_dictionary_create. for
loop return the dictionary elements. pass
statement from print_elements. print
statement.for
loop over the dictionary dict
using symbol as the loop variable.for
loop print the value of symbol. print
statement. for
loop get this value from the dictionary
and assign it to the variable element_data. print
. H Hydrogen 1 1.008 He Helium 2 4.0026 Li Lithium 3 6.94 ...Fix any errors you find.
$ ./hw03.py
File name: elements.txt
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
The text in blue must be entered at the command line.
cd 117
cd hw
cd hw3
chmod 755 hw3.py
Copyright © 2021 Glenn Hoffman. All rights reserved. May not be reproduced without permission.