0

OK, how do I make this program allow the user to modify the price of items and save and load the modified catalog? i've spent ages trying and failing. pls help!

'''maintain a catalog'''

def main():
    global catalog

    loadCatalog()
    while True:
        pick = showMenu()
        if pick == 0:
            break
        elif pick == 1:
            sellItem()
        elif pick == 2:
            buyItem()
        else:
            print "Invalid selection"
    saveCatalog()

def showMenu():
    global catalog

    while True:
        print "Catalog:\n\t%6s %12s %8s %8s" % ( "ItemID", "Item", "Price", "Stock" )
        for k in catalog.keys():
            print "\t%6d %12s %8s %8d" % (k,catalog[k][0], str("$%0.2f" % catalog[k][1]), catalog[k][2])

        try:
            pick = int(raw_input('''
Enter selection:
0 - Quit
1 - sell Item
2 - buy Item

Selection : ''' ))
        except:
            print "Pick must be an integer\n"
            continue
        return pick

def sellItem():
    global catalog
    print "sellItem"

    try:
        itemID = int(raw_input("Which item? "))
        if itemID in catalog.keys():
            howMany = int(raw_input("How many? "))
            if 0 < howMany <= catalog[itemID][2]:
                catalog[itemID][2] -= howMany
                print "Price for %d %s is $%0.2f" % ( howMany, catalog[itemID][0],catalog[itemID][1]*howMany )
            else:
                print "Can't sell %d %s" % ( howMany, catalog[itemID][0] ) 
        else:
            print "Invalid itemID"
    except:
        print "Bad value entered"
    print

def buyItem():
    global catalog
    print "buyItem"
    try:
        itemID = int(raw_input("Which item? "))
        if itemID in catalog.keys():
            howMany = int(raw_input("How many? "))
            if 0 < howMany:
                catalog[itemID][2] += howMany
            else:
                print "Can't buy %d %s" % ( howMany, catalog[itemID][0] ) 
        else:
            print "Invalid itemID"
    except:
        print "Bad value entered"
    print

def loadCatalog():
    global catalog
    catalog = { 1:["Bread", 1.50, 10 ], 2:["Cheese", 5.00, 5], 3:["Apples", 2.50,12] }

def saveCatalog():
    global catalog
    pass

if __name__ == "__main__":
    main()
3
Contributors
3
Replies
4
Views
7 Years
Discussion Span
Last Post by Lizzard013
0

You don't need that many global statements.
Actually, the use of global is discouraged.
Since catalog variable is constant, just declare it in the beginning of your file.

0

this is the program that i was given to work with, i tried using a def modPrice() but couldn't get it to work :(

0

try this:

def modPrice():
    global catalog
    print "modifyPrice"
    try:
        itemID = int(raw_input("Which item? "))
        if itemID in catalog.keys():
            price = float(raw_input("How many? "))
            if 0 < price:
                catalog[itemID][1] = price
                print "Price has been changed"
            else:
                print "Price needs to be greater than 0"
         else:
            print "Invalid itemID"
    except:
        print "Bad value entered"
    print
This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.