Question: Please help me to fix the code shown below. These are the requirements: Design a syntactically correct Python program to solve the following problem: Design
Please help me to fix the code shown below. These are the requirements:
Design a syntactically correct Python program to solve the following problem:
Design a program for the local coffee shop owner who wants to be able to control his inventory. The program must be written in accordance with the following specs:
1. Write the following data to an external file, name the file coffeeInventory.txt
Description Pounds Blonde Roast 15 Medium Roast 21 Flavored Roast 10 Dark Roast 12 Costa Rica Tarrazu 18
2. You do not need to write the table, just the data
3. Read in the records you just wrote to coffeeInventory.txt and display them on the screen and sum the total pounds of coffee
4. Append these records to the file
Guatemala Antigua 22 House Blend 25 Decaf House Blend 16
5. Modify the file by allowing the owner to remove data from the file:
a. Ask the owner to enter a description to remove
b. If the description exists, remove the coffee name and the quantity
c. If the description is not found, display the message:That item was not found in the file.
6. Modify the file by allowing the owner to delete data from the file:
a. Ask the owner to enter a description to delete
b. If the description exists, delete the coffee name and the quantity
c. Replace the name and quantity of the coffee removed in step b by asking the user to enter a new coffee name and quantity
d. If the description is not found, display the message:That item was not found in the file.
I have received this error running the code. Can you help me to fix it. I am stuck here.
RESTART: C:/Users/Gsuar/AppData/Local/Programs/Python/Python37-32/Gretchen_Suarez_Lab6c.py
Description;Pounds
Blonde Roast;15
Medium Roast;21
Flavored Roast;10
Dark Roast;12
Costa Rica Tarrazu;18
Traceback (most recent call last):
File "C:/Users/Gsuar/AppData/Local/Programs/Python/Python37-32/Gretchen_Suarez_Lab6c.py", line 8, in
numPounds+=(int((f1[i].split(' ')[0]).split(';')[1]))
IndexError: list index out of range
>>>
Code
#reading Coffe shop Inventory file
f = open('coffeeInventory.txt','r+')#opening file in reading mode
f1 = f.readlines()
numPounds = 0
for i in range(len(f1)):
print(f1[i].split(' ')[0])
if i!=0:
numPounds+=(int((f1[i].split(' ')[0]).split(';')[1]))
#Total pounds of coffee
print("Total pounds of Coffee:",numPounds)
f.close()#closing file
#4
f = open('coffeeInventory.txt','a+')#opening file in appending mode
#appending records
f.write(' Guatemala Antigua;22')
f.write(' House Blend;25')
f.write(' Decaf House Blend;16')
f.close()#closing file
#5
#removing the record as per owner's description
Description = input("enter Description to remove:")
f = open('coffeeInventory.txt','r')#opening files in reading mode
f1 = f.readlines()
found = -1
for i in range(len(f1)):
line = f1[i].split(' ')[0]
des = line.split(';')[0]
if des==Description:
found = i
#storing the previous data
f2= open('coffeeInventory_pre.txt','w+')
f2.writelines(f1)
f2.close()
#removing the data if found
if found==-1:
print("The item was not found in the file")
else:
f1.remove(f1[found])
data_list = f1
f.close()
#modifying file after removing
modify_file = open('coffeeInventory.txt','w+')
for x in data_list:
modify_file.write(x)
modify_file.close()
f.close()
#6--------------
#deleting the record as per owner's description
Description = input("enter Description to delete:")
f = open('coffeeInventory.txt','r')#opening files in reading mode
f1 = f.readlines()
found = -1
for i in range(len(f1)):
line = f1[i].split(' ')[0]
des = line.split(';')[0]
if des==Description:
found = i
if i==-1:
print("The item was not found in the file")
else:
f1.remove(f1[found])
data_list = f1
f.close()
# deleting in the pre_Coffee_inventory file
#finding record
f2 = open('coffeeInventory_pre.txt','r')
f3 =f2.readlines()
found = -1
for i in range(len(f3)):
line = f3[i].split(' ')[0]
des = line.split(';')[0]
if des==Description:
found = i
if i==-1:
print("The item was not found in the file")
else:
f3.remove(f3[found])#deleting record
data_list1 = f3
f2.close()
#modifying pre file
f2 = open('coffeeInventory_pre.txt','w+')
f2.writelines(data_list1)
f2.close()
#opening file in writing mode
modify_file = open('coffeeInventory.txt','w+')
modify_file.writelines(data_list)
modify_file.close()
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
