이 프로그램에서는 인벤토리 프로그램을 만들려고합니다. "재고 업데이트"라는 옵션 (3)에서 목록에 대한 항목을 입력하면 목록 name
의 기존 수량에 추가 qty
하거나 빼라 는 메시지가 표시됩니다 .
예를 들어에 5 개의 항목이 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
는 아마도 가장 좋은 방법 일 것입니다. 특히 인벤토리 시스템으로 작업 할 때-인벤토리에서 모든 항목은 고유 한 속성을 갖습니다. 딕셔너리는 키 / 값 쌍을 저장하므로 이와 같은 작업에 적합합니다. 두 개의 목록을 사용하면 해로울 수 있습니다. 하나의 작은 버그가 전체 시스템을 탈선시킬 수 있습니다. 다음은 여러 목록에서 사전을 사용하는 단순성을 보여줍니다.
inventory = {'apples':0, 'bananas':0, 'oranges':0}
item = input("Enter the item to update: ")
qty = int(input("Enter the updated quantity. Enter 5 for additional or -5 for less: "))
inventory[item] += qty
그러나 두 개의 목록을 사용하도록 설정 한 경우 다음과 같은 목적을 달성 할 수 있습니다.
name = ['apples', 'bananas', 'oranges']
qty = [0,0,0]
item = input("Enter the item to update: ")
update = int(input("Enter the updated quantity. Enter 5 for additional or -5 for less: "))
qty[name.index(item)] += update
목록이 서로 100 % 연관되어 있다고 가정하면 위의 항목은 항목의 색인 위치를 사용하고 다른 목록에서 해당 수량을 업데이트합니다.
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다