Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Given Code: Specifically, modify the program in the following steps: Step 1: Write a read_inventory function that reads and returns the inventory as a list

image text in transcribedGiven Code:

image text in transcribedimage text in transcribedimage text in transcribedimage text in transcribed

Specifically, modify the program in the following steps: Step 1: Write a read_inventory function that reads and returns the inventory as a list of lists from the given csv file (wizard-items.csv). Step 2: Write a write_inventory function that takes an argument which is a list of lists and writes the contents to the same csv file (wizard-items.csv). Step 3: Modify the rest of the code to initialize the inventory by reading it from the wizard-items.csv file and writing to that csv file every time any changes are done to the inventory by the user. So that when user makes changes to the inventory, exits the program and runs it again, the changes are seen in the newly read inventory. Step 4: Add exception handling to the delete_item and edit_item methods so that a. if user enters a non-integer value for the index, program doesn't crash instead prints specific feedback: ******** Please enter a valid integer** b. if user enters an out of range index value, program doesn't crash instead prints ******** Please enter an index between 1 and n ********" (where n is the number of items in the inventory). **" See the sample run 2 below. Step 5: Add exception handling to the read and write functions so that if there is any error in opening, reading from or writing to the file, program doesn't crash instead prints specific feedback (e.g. ******** Error reading from wizard-items.csv ******** Error writing to wizard-items.csv********"). See the sample run 3 below. ***" or ############################################################# ############# ##Code to display and manage a list of Wizard Inventory Items ## where items are stored a list of lists with each item having a name, a color ## and a weight value. ############################################################################ def list_items (inventory): "" Function to display the list of items from the invetory!!! for i in range(len(inventory)): item = inventory[i] print("{i+1}. {item[1]} {item[@].title()} ({item[2]})") print() def add_item(inventory): " Function to add a new item to the invetory' name = input("Name: ") color = input("Color: ") weight = int(input("Weight: ")) item = [] item.append(name) item.append(color) item.append(weight) inventory.append(item) print(name + " was added. ") def delete_item(inventory): "Function to delete an item from the invetory' index = int(input("Number: ")) item = inventory.pop(index - 1) print(item[@] + " was deleted. In") def edit_item_name(inventory): !!! Function to delete an item from the invetory!!! index int(inp ("Number: ")) newName = input("Enter updated name: ") inventory[index - 1][0] = newName print("Item number " + str(index) + " name was updated. ") def display_menu(): print("The Wizard Inventory List program") print() print("COMMAND MENU") print("list - List all inventory items") print("add - Add an item") print("del - Delete an item") print("edit - Edit name of an item") print("exit - Exit program") print() def main(): display_menu() inventory = [["wooden staff", "Brown", 30.0), ["wizard hat", "Black", 1.5), ["cloth shoes", "Blue" 5.311 while True: command = input ("Command: ") if command.lower() == "list": list_items(inventory) elif command.lower() == "add": add_item( inventory) elif command.lower() == "del": delete_item(inventory) elif command.lower() == "edit": edit_item_name (inventory) elif command lower() == "exit": break else: print("Not a valid command. Please try again. ") print("Bye!") if name main() == "__main__": Sample Run 1: Edit name, exit program and run again, edits show in list L *Python 3.6.3 Shell* File Edit Shell Debug Options Window Help A aga - Add an item del - Delete an item edit - Edit name of an item exit - Exit program Command: list 1. Brown Wooden Staff (3.1) 2. Black Wizard Hat (1.5) 3. Black Cloak Of Invisibility (10.2) 4. Blue Scroll Of Uncursing (2.7) Command: edit Number: 2 Enter updated name: Wizard Cap Item number 2 name was updated. Command: list 1. Brown Wooden Staff (3.1) 2. Black Wizard Cap (1.5) 3. Black Cloak Of Invisibility (10.2) 4. Blue Scroll Of Uncursing (2.7) Command: exit Bye! >>> main() The Wizard Inventory List program COMMAND MENU list - List all inventory items add - Add an item del - Delete an item edit - Edit name of an item exit - Exit program Command: list 1. Brown Wooden Staff (3.1) 2. Black Wizard Cap (1.5) 3. Black Cloak Of Invisibility (10.2) 4. Blue Scroll Of Uncursing (2.7) Ln: 227 Col: 9 Sample Run 2: User enters invalid input for del and edit commands *Python 3.6.3 Shell File Edit Shell Debug Options Window Help ADJIMA. C./ Couc, Fy no riayyLuumu-12UMOST YiuucUILS/ wicalu_1UVCLLULY-11101.PY The Wizard Inventory List program COMMAND MENU list - List all inventory items add - Add an item del - Delete an item edit - Edit name of an item exit - Exit program Command: list 1. Brown Wooden Staff (3.1) 2. Black Wizard Hat (1.5) 3. Black Cloak Of Invisibility (10.2) 4. Blue Scroll Of Uncursing (2.7) Command: del Number: eight ********Please enter a valid integer. ******** Command: del Number: 8 ********Please enter an integer between 1 and 4 ******** Command: edit Number: eight ********Please enter a valid integer. ******** Command: edit Number: -8 Enter updated name: New Name ********Please enter an integer between 1 and 4 ******** Command: Ln: 149 Col: 9 Sample Run 3: After renaming file wizard-items.csv to wizard-items1.csv, and running the program, program shows Error reading ... message. Shows empty list after reading error. *Python 3.6.3 Shell File Edit Shell Debug Options Window Help L I LILLEPY WIJI. Vi LuuLLI LIIULITIIUyyULILE ILUSS yum LamiLULU_111YLLILULI The Wizard Inventory List program COMMAND MENU list - List all inventory items add - Add an item del Delete an item edit - Edit name of an item exit - Exit program ********Error reading inventory from wizard_items.csv ** Command: list Command: Ln: 51 Col: 9

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Sams Teach Yourself Beginning Databases In 24 Hours

Authors: Ryan Stephens, Ron Plew

1st Edition

067232492X, 978-0672324925

More Books

Students also viewed these Databases questions

Question

What is the basis for Security Concerns in Cloud Computing?

Answered: 1 week ago

Question

Describe the three main Cloud Computing Environments.

Answered: 1 week ago