You are the owner of a hardware store and need to keep an inventory that can tell you what different tools you have, how many of each you have on hand and the cost of each one. Write a program that initializes the shelve file "hardware.dat", lets you input the data concerning each tool and enables you to list all your tools. The tool identification number should be the record number. Use the

following information to start your file:

![15073|477x140](upload://pVMVoh5yzyMZ2zWoIMOKJxi2WSP.png)
What will be an ideal response?

```
# Hardware inventory.

import sys
import shelve

# prompt for an input menu choice
def enterChoice():

print "\nEnter your choice"
print "1 - add a new tool"
print "2 - list tools"
print "3 - end program"

while 1:
menuChoice = int( raw_input( "? " ) )

if not 1 <= menuChoice <= 3:
print >> sys.stderr, "Incorrect choice"

else:
break
return menuChoice

# add item to shelve file
def addItem( inventory ):

print "Enter tool identification number."
toolID = int( raw_input( "? " ) )

if 0 < toolID <= 100:
print "Enter: tool quantity cost"
userInput = raw_input( "? " )
inventory[ str( toolID ) ] = userInput.split()
else:
print "Invalid record number."

# list tools
def listTools( inventory ):

print "ToolID".ljust( 10 ),
print "Tools".ljust( 10 ),
print "Quantity".ljust( 10 ),
print "Price".ljust( 10 )

for key in inventory.keys():
print key.ljust( 10 ),
print inventory[ key ][ 0 ].ljust( 10 ),
print inventory[ key ][ 1 ].ljust( 10 ),
print inventory[ key ][ 2 ].ljust( 10 )

options = [ addItem, listTools ]

# open shelve file
try:
inventory = shelve.open( "hardware.dat" )
except IOError:
print >> sys.stderr, "File could not be opened"
sys.exit( 1 )

# process user commands
while 1:

choice = enterChoice()

if choice == 3:
break

options[ choice - 1 ]( inventory )

inventory.close()
```
Enter your choice
1 - add a new tool
2 - list tools
3 - end program
? 1
Enter tool identification number.
? 88
Enter: tool quantity cost
? saw 34 18.50
Enter your choice
1 - add a new tool
2 - list tools
3 - end program
? 1
Enter tool identification number.
? 21
Enter: tool quantity cost
? wrench 45 8.00
Enter your choice
1 - add a new tool
2 - list tools
3 - end program
? 2
ToolID Tools Quantity Price
88 saw 34 18.50
21 wrench 45 8.00
Enter your choice
1 - add a new tool
2 - list tools
3 - end program
? 3

Computer Science & Information Technology

You might also like to view...

In a database, a group of related records is a(n) ________

Fill in the blank(s) with correct word

Computer Science & Information Technology

The Macro Recorder uses absolute references by default

Indicate whether the statement is true or false

Computer Science & Information Technology