heya again guys - sorry for asking for help once again, but i think once i have my head round this, i'll sort myself out pretty well *python noob* :?:
the prac is to add an option to change the price of a listed item in a catalog - this ive completed with no probs at all.
But we also have to read & write to an external txt file - for whatever reason, i cant get this to work. It keeps wiping out my txt file (obviously im screwing up the write code somewhere????) anyway - the code with the correct 'change $' def is printed, to show where ive gone right
then the changes ive made to try & open a txt file is posted - to show where im going wrong.
After i get the txt read/write sorted, i also need to adjest it to let me add items/delete items etc - but ill be doing that myself - i just need some help as to where im going wrong at the stage im at. - im guessing i need to provide field headings (eg ItemID, name, price, stock) and the program uses the txt file to provide/save data.
correct code up to the 'change item $' point
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
3 - Check Price
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 checkPrice():
global catalog
print "checkPrice"
try:
itemID = int(raw_input("Which item? "))
if itemID in catalog.keys():
newPrice = float(raw_input("Enter new Price? "))
if newPrice <0:
print "%d is a negative amount. Please enter a correct Price" % ( newPrice )
else:
catalog[itemID][1]= newPrice
print "You changed the price to %d for the %s " % (newPrice, catalog[itemID][0] )
else:
print "Invalid itemID"
except:
print "Bad value entered"
saveCatalog()
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()
code changed to read/write to txt file - where am i screwing up?? - I keep getting the error: for k in catalog.keys():
AttributeError: 'file' object has no attribute 'keys'
def main():
c=open("catalog.txt", "r")
for line in c:
fields = line.split(',')
itemID = fields[0]
name = fields[1]
price = fields[2]
stock = fields[3]
d=open("catalog.txt","w")
for item in catalog.values():
count -= 1
outputString = "%d %s %0.2f %d" % (itemID[0], name[1], price[2], stock[3])
d.write(outputString)
if count > 0:
d.write("\n")
d.close()
catalog[itemID] = [name,price,stock]
loadCatalog()
while True:
pick = showMenu()
if pick == 0:
break
elif pick == 1:
sellItem()
elif pick == 2:
buyItem()
elif pick == 3:
checkPrice()
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
3 - Check Price
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"
saveCatalog()
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
print "You added %d %s to the Stock" % ( howMany, catalog[itemID][0] )
else:
print "Can't buy %d %s" % ( howMany, catalog[itemID][0] )
else:
print "Invalid itemID"
except:
print "Bad value entered"
saveCatalog()
print
def checkPrice():
global catalog
print "checkPrice"
try:
itemID = int(raw_input("Which item? "))
if itemID in catalog.keys():
newPrice = float(raw_input("Enter new Price? "))
if newPrice <0:
print "%d is a negative amount. Please enter a correct Price" % ( newPrice )
else:
catalog[itemID][1]= newPrice
print "You changed the price to %d for the %s " % (newPrice, catalog[itemID][0] )
else:
print "Invalid itemID"
except:
print "Bad value entered"
saveCatalog()
print
def loadCatalog():
global catalog
def saveCatalog():
print "All changes have been saved"
global catalog
pass
if __name__ == "__main__":
main()
raw_input("\n\npress ENTER to continue")
thanks v much for any hints as to where i need to change things