Repository navigation
Expand file tree
/
Copy pathCDInventory.py
More file actions
94 lines (82 loc) · 3.04 KB
/
Copy pathCDInventory.py
File metadata and controls
94 lines (82 loc) · 3.04 KB
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
#------------------------------------------#
# Title: CDInventory.py
# Desc: Starter Script for Assignment 05
# Change Log: (Who, When, What)
# DBiesinger, 2030-Jan-01, Created File
# AnhVu, 2021-Aug-06, Added Code
#------------------------------------------#
# Declare variables
strChoice = '' # User input
lstdict = [] # list of lists to hold data
dict3 = {} # list of data row
strFileName = 'CDInventory.txt' # data storage file
objFile = None # file object
# Get user Input
print('The Magic CD Inventory\n')
while True:
# 1. Display menu allowing the user to choose:
print ('[l] Load Inventory from file')
print ('[a] Add CD\n[i] Display Current Inventory')
print ('[d] Delete CD from Inventory')
print ('[s] Save Inventory to file')
print ('[x] Exit')
strChoice = input('l, a, i, d, s or x: ').lower() # convert choice to lower case at time of input
# 1. Load inventory if the user chooses so
if strChoice == 'l':
f = open ('CDInventory.txt', 'r')
i = 0
l = f.readlines ()
while i < len(l) - 1:
ID = l[i]
ID = ID.replace('\n', '')
Title = l[i+1]
Title = Title.replace('\n', '')
Artist= l[i+2]
Artist= Artist.replace('\n', '')
dict = {'ID':ID, 'Artist':Artist, 'Title': Title}
lstdict.append (dict)
i = i + 3
f.close ()
pass
# 2. Add CD if the user chooses so
elif strChoice == 'a':
strID = input('Enter an ID: ')
strTitle = input('Enter the CD\'s Title: ')
strArtist = input('Enter the Artist\'s Name: ')
intID = int(strID)
dict3 = {'ID': strID, 'Title': strTitle, 'Artist': strArtist}
lstdict.append(dict3)
pass
# 3. Display the current data to the user each time the user wants to display the data
elif strChoice == 'i':
print('ID, CD Title, Artist')
for row in (lstdict):
print(row)
pass
# 4. Deleting an entry if user chooses to do so
elif strChoice == 'd':
removeTitle = input ('Enter Title to be removed: ')
for row in lstdict:
Title1 =row ['Title']
if Title1 == removeTitle:
lstdict.remove (row)
print ('New inventory is: ','\n',lstdict)
pass
# 5. Save the data to a text file CDInventory.txt if the user chooses so
elif strChoice == 's':
for row in lstdict:
print (row)
ID1 = row ['ID']
Artist1 = row ['Artist']
Title1 = row ['Title']
objFile = open(strFileName, 'a')
objFile.write(ID1 + '\n')
objFile.write (Artist1 + '\n')
objFile.write (Title1 + '\n')
objFile.close()
pass
# 6. Exit the program if the user cxhooses so
elif strChoice == 'x':
break
else:
print('Please choose either l, a, i, d, s or x!')