-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAssignment 7.2.py
64 lines (61 loc) · 2.16 KB
/
Assignment 7.2.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
#-------------------------------------------------#
# Title: Python Pickling Example
# Dev: LDenney
# Date: February 26, 2018
# ChangeLog: (Who, When, What)
# Laura Denney, 2/26/18, Created File
#-------------------------------------------------#
#---Data------------------------------------------------------
FILENAME = "Pickle.dat" #Constant for file name
userOption = None #user menu option choice
lstTableData = [] #table of product lists
lst = [] #stores a products ID, name, price
#---Processing------------------------------------------------------
def ShowOptions():
print("""
Menu:
1 - Add Product Data to list
2 - Save list to File
3 - Load list from File
4 - Exit
""")
def getUserOption():
option = input("Please pick a number based on what you'd like to do: ")
return option
def addData():
productId = int(input("What is the product ID: "))
productName = input("What is the product Name: ")
productPrice = input("What is the product Price: ")
lstData = [productId, productName, productPrice]
return lstData
def OpenReadData(File):
objFile = open(File, "rb")
Datalst = pickle.load(objFile)
objFile.close()
return Datalst
def SaveData(File, Data):
objFile = open(File, "wb")
pickle.dump(Data, objFile)
objFile.close()
#---Presentation-----------------------------------------------------
import pickle
print("This program makes a list of product IDs, product names and product prices.\n\
It can also store this list for future use. ")
while(True):
ShowOptions()
userOption = getUserOption()
if userOption == "1": #add items to list
lst = addData()
lstTableData.append(lst)
print(lst, "added.")
elif userOption == "2": #save list to file
SaveData(FILENAME, lstTableData)
print(lstTableData, "saved to file.")
elif userOption == "3": #load list from file
lstTableData = OpenReadData(FILENAME)
print("Here is the current list:", lstTableData)
elif userOption == "4": #exit
break
else:
print("Please choose a valid option.")
print("Thank you. Goodbye.")