Michael Murphy
IT FDN 100
Computer OS: Windows 10 Pro
Date: 5/22/2017
Due 5/22/2017
Prof. Randal Root
Assignment
#7 Code using Exceptions and Pickling
The code for this assignment is generally straightforward. I wanted to
make the code somewhat relevant as a useable program for information storage,
so I set it up as a storage program for personal data. I first wrote the code
to prompt the user for their name, year of birth, mother and then father’s
name. The program then saves this data to a list and pickles it to a .dat file.
The program saves this file, then prompts the user to press enter before
regurgitating the data once again by printing the list.
After creating this program, I wanted to make it a little more robust.
The year of birth really needs to be an integer, but technically the user could
enter a string, or even a float. Therefore, I convert the input into an int,
and then if the computer throws up a ValueError the user is given a verbal
reprimand, and re-prompted. This section of the code is in an infinite loop;
the only way to get out of the loop is to enter a valid integer value, at which
point the code contains a break function.
Code for Assignment Assignment07_Murphy.py
Date:
May 22nd 2017
Due:
May 22nd 2017
Professor:
Randall Root
Course:
IT FDN 100A
Assignment
Title: Example of Exception Handling and Pickling
References:
Exception Handling:
https://www.programiz.com/python-programming/exception-handling
Pickling:
https://wiki.python.org/moin/UsingPickle
'''
import
pickle
print("Greetings
Human, I am going to demonstrate the awesome power of exception handling and
Pickling")
print("I
will ask you for some personal data, and then pickle it into a data file. Then
I will unpickle it")
your_Name
= str(input("Enter your Full Name: ")) #Enter your name
while(True):
#Enter your age. Forces you to enter an integer year, otherwise it will repeat
the question if invalid
try:
your_Year = int(input("What year
were you born? "))
break
except ValueError as e:
print("Silly Human, that was not a
valid year, or in python lingo: " + str(e))
print("Try Again...")
your_Mother
= str(input("What is your mother's first name? ")) #Asks you for your
mother's name
your_Father
= str(input("What is your Father's first name?")) #Asks you for your
father's name
Info
= [your_Name, your_Year, your_Mother, your_Father] #Stores your data into a
list
saveFile
= open("C:\_PythonClass\Module7\PickleFile.dat", "wb")
#Opens .dat file (write mode)
pickle.dump(Info,
saveFile) #pickles and saves data to binary data file
saveFile.close()
print("Processing....please
wait")
print("Still
processing....")
print("OMG
STILL PROCESSING....done")
input("Data
Saved & Pickled. Press Enter to regurgitate Pickling.")
openTheFile
= open("C:\_PythonClass\Module7\PickleFile.dat", "rb") #Opens
.dat file in read only mode
Regurgitate
= pickle.load(openTheFile) #Load reads file and converts from binary to
readable text
openTheFile.close()
print(Regurgitate) #Prints the unpickled list
What this code looks like in the Command Prompt:
Works Cited
“Python
Programming, Third Edition for the absolute beginner” by Michael Dawson, Course
Technology, 2010
Randall
Root, Youtube Videos for Python Module 7
YouTube 2016