Python 如何管理库存:添加、删除、修改/更新、搜索和打印
对不起,如果这不是很好地介绍。这是我第一次使用StackOverflow。我正试图创建这个来跟踪库存。我不知道如何利用特例。此外,我不知道还有什么地方出了问题。谢谢你的帮助 数据处理程序:Python 如何管理库存:添加、删除、修改/更新、搜索和打印,python,python-3.x,try-except,inventory-management,Python,Python 3.x,Try Except,Inventory Management,对不起,如果这不是很好地介绍。这是我第一次使用StackOverflow。我正试图创建这个来跟踪库存。我不知道如何利用特例。此外,我不知道还有什么地方出了问题。谢谢你的帮助 数据处理程序: import os class DataHandler(): def __init__(self, filename, datadict = {}): self.filename = filename self.datadict = datadict de
import os
class DataHandler():
def __init__(self, filename, datadict = {}):
self.filename = filename
self.datadict = datadict
def readData(self): # returns dictionary
try:
fileobj = open(self.filename,'r')
datastr = fileobj.read()
fileobj.close()
# return dictionary read from file or null dictionary
if datastr != '':
return eval(datastr)
else:
return self.datadict
except FileNotFoundError:
return self.datadict # null dictionary
def writeData(self, datadict): # must pass dictionary
fileobj = open(self.filename,'w')
fileobj.write(str(datadict))
fileobj.close()
单独文件:
import DataHandler as DH
def menuDisplay():
print("="*40)
print("1 - Add inventory item")
print("2 - Modify inventory item")
print("3 - Delete inventory item")
print("4 - Print inventory list")
print("5 - Exit program")
print("="*40)
selection = input("\nPlease enter menu choice: ")
if selection < "1" and selection > "5":
input("Invalid selection, please enter 1 - 5")
menuDisplay()
else:
return int(selection)
def addItem(invDict,dh):
itemdesc = input("Item Description: ")
itemqty = int(input("Qty: "))
if len(invDict) > 0:
nextKey = sorted(invDict.keys())[-1]+1
else:
nextKey = 1
invDict[nextKey]=[itemdesc,itemqty]
dh.writeData(invDict)
def printInv(invDict):
print("{:<10}{:20}{:6}".format("Item #", "Description", "Qty",))
for dkey, lvalue in invDict.items():
print("{:<10}{:20}{:6d}".format(str(dkey), lvalue[0],lvalue[1], lvalue[2]))
def delItem(invDict,dh):
itemnum = input("Please enter item to delete: ")
del invDict[int(itemnum)]
dh.writeData(invDict)
# complete error checking
def modItem(invDict,dh):
itemnum = input("Please enter item to modify: ")
newqty = input("Please enter the new quantity: ")
invDict[int(itemnum)][1] = int(newqty)
dh = DH.DataHandler("inventory.txt")
invDict = dh.readData()
selection = 0
while selection != 5:
selection = menuDisplay()
if selection == 1:
addItem(invDict,dh)
elif selection == 2:
modItem(invDict,dh)
elif selection == 3:
delItem(invDict,dh)
elif selection == 4:
printInv(invDict)
将数据处理程序导入为DH
def menuDisplay():
打印(“=”*40)
打印(“1-添加库存项目”)
打印(“2-修改库存项目”)
打印(“3-删除库存项目”)
打印(“4-打印库存清单”)
打印(“5-退出程序”)
打印(“=”*40)
选择=输入(“\n请输入菜单选项:”)
如果选择<“1”和选择>“5”:
输入(“无效选择,请输入1-5”)
menuDisplay()
其他:
返回整数(选择)
def附加项(INVDECT,dh):
itemdesc=输入(“项目说明:”)
itemqty=int(输入(“数量:”)
如果len(invDict)>0:
nextKey=sorted(invDict.keys())[-1]+1
其他:
nextKey=1
invDict[nextKey]=[itemdesc,itemqty]
dh.书面数据(invDict)
def printInv(INVDECT):
打印(“{:你能补充一些你需要帮助的细节吗?函数和变量名应该遵循带有下划线的小写形式。使用上下文管理器来处理文件。使用字典作为默认值可能是一个糟糕的主意。你使用
比较包含数字的字符串,这是不会按您希望的方式运行。例如,您知道吗?'10'<'5'
?您的帖子中的代码格式已关闭,这使得这一点有点困难。第一:代码有错误的缩进-更正它。第二:始终将完整的错误消息(从单词“Traceback”开始)作为文本(而不是屏幕截图)进行讨论(而不是注释)。还有其他有用的信息。您可以使用模块json
而不是eval()
。或者请参阅模块pickle
。