Skip to main content
UW Python Class - Student Mario


"""
File names: tryexceptfinally.py and Pythonpickle.py
Author: Mario
Assignment 07
Class: IT FDN 100 A Sp 17
Date created: 5/22/2017
This code is to simply request an integer and when
an exception occurs, the normal flow of execution is interrupted.
 
"""
The first part of the assignment I researched how to do exception handling.  I found a resource at http://www.python-course.eu/python3_exception_handling.php.
This example walked through how to create code for user who does not input an integer and gets a message either indicating correct or not so post error (or non error).


#requests a statement to be true
while True:
    try:
#input request from user for an integer      
        n = input("Please enter an integer: ")
        n = int(n)
        break
#where user does not enter an integer except statement as error raised
    except ValueError:
#prints the statement to the user
        print("No valid integer! Please you must enter an integer ...")
#upon correct input is prints this message
print("Super, you successfully entered an integer!")



The second part of the assignment I researched how to do python pickling.  I sourced the code in research from this site: https://pythontips.com/2013/08/02/what-is-pickle-in-python/.  The steps are to first bring in the module by citing "import" and then specifically it is pickle.  I decided to specifically not use code from previous assignments and look at a new example so that I could get a different perspective with a fresh set of code to view.  It helped me get my head wrapped around the idea of serialization referencing this example of a sequential list of values that are reconstructed.  As recommended on this assignment, because I have been hasty in past ones, I thoroughly tested this time and found the example in the  python tips web site posted is actually wrong.  Side note: I've started using jupyter notebooks.  I have found it works well for me.  So the error was the "r" instead of "rb".  I actually figured this out thinking about the binary aspect of definition for the first "wb."




#bring in python module
import pickle

#Assigns values variable a
a = ['test value','test value 2','test value 3']
a
['test value','test value 2','test value 3']

file_Name = "testfile"
# open the file for writing
#wb is for write binary below
fileObject = open(file_Name,'wb')

# this writes the object a to the
# file named 'testfile'
pickle.dump(a,fileObject)

# here we close the fileObject
fileObject.close()
# we open the file for reading.  The 'r' indicates "read".
fileObject = open(file_Name,'rb') #this changed from tips site to rb from r
# load the object from the file into var b
b = pickle.load(fileObject)
b
['test value','test value 2','test value 3']
a==b
True
#require user to press enter to exit
print=input("Press enter to exit")

Comments