在这个程序中,我试图制作一个清单程序。在标题为“更新库存”的选项(3)下,您为列表name
键入一个项目,然后系统会提示您向列表qty
中的现有数量添加或从中减去
例如,如果我在name
中有五项,在qty
中有相应的数量。如何查找第3项,并通过添加或减去当前金额来更新数量
完整程序代码(仅寻求有关如何编写选项3的帮助):
name = []
qty = []
class Foo():
def __init__(self, name, qty):
self.name = name
self.qty = qty
def menuDisplay():
print ('=============================')
print ('= Inventory Management Menu =')
print ('=============================')
print ('(1) Add New Item to Inventory')
print ('(2) Remove Item from Inventory')
print ('(3) Update Inventory')
print ('(4) Search Item in Inventory')
print ('(5) Print Inventory Report')
print ('(99) Quit')
CHOICE = int(input("Enter choice: "))
menuSelection(CHOICE)
def menuSelection(CHOICE):
if CHOICE == 1:
print('Adding Inventory')
print('================')
new_name = input('Enter the name of the item: ')
name.append(new_name)
new_qty = int(input("Enter the quantity of the item: "))
qty.append(new_qty)
CHOICE = int(input('Enter 98 to continue or 99 to exit: '))
if CHOICE == 98:
menuDisplay()
elif CHOICE == 99:
exit()
elif CHOICE == 2:
print('Removing Inventory')
print('==================')
removing = input('Enter the item name to remove from inventory: ')
indexdel = name.index(removing)
name.pop(indexdel)
qty.pop(indexdel)
CHOICE = int(input('Enter 98 to continue or 99 to exit: '))
if CHOICE == 98:
menuDisplay()
elif CHOICE == 99:
exit()
elif CHOICE == 3:
print('Updating Inventory')
print('==================')
item = input('Enter the item to update: ')
update = int(input("Enter the updated quantity. Enter 5 for additional or -5 for less: "))
if update >= 0:
print()
elif update <= -1:
print()
CHOICE = int(input('Enter 98 to continue or 99 to exit: '))
if CHOICE == 98:
menuDisplay()
elif CHOICE == 99:
exit()
我缩短了代码,因为我相信这是复制我的问题的最低代码
我不建议使用多个列表来存储相关项目。A
dictionary
可能是您最好的选择,尤其是在使用库存系统时-在库存中,每个项目都有一个唯一的属性。字典存储键/值对,非常适合这样的情况。使用两个列表可能是有害的;一个小错误可能会破坏整个系统。类似的内容将说明在多个列表上使用字典的简单性:但是,如果您打算使用两个列表,这将满足您的目的:
假设您的列表彼此100%关联,以上将使用项目的索引位置并更新其他列表中的数量
相关问题 更多 >
编程相关推荐