2017-11-20 8 views
0

고객이 질문에 no라고 대답 할 때까지 반복되는 기본 store-front 스크립트를 작성하려고합니다. 매번 항목 번호가 입력 될 때마다 항목을 저장하려고 시도하고 항목 번호와 가격을 일치시킬 수 있습니다. (0120) 이제는 마지막 항목을 추가하고 이전 번호를 덮어 쓰지 않고 "item_nums"빈 목록에 추가하려고합니다.Python3 - 기존 데이터 덮어 쓰지 않고 목록에 사용자 응답 추가

점주

products = ['Notebook', 'Atari', 'TrapperKeeper', 'Jeans', 'Insects', 
'Harbormaster', 'Lobotomy', 'PunkRock', 'HorseFeathers', 'Pants', 
'Plants', 'Salami'] 
prices = ['$4.99', '$99.99', '$89.99', '$3.99', '$2.99', '$299.99', 
'$19.99', '$3.99', '$4.99', '$2.99', '$119.99', '$1.99'] 
SKUs = [1, 2, 3, 4, 5, 6, 7, 8 ,9, 10, 11, 12] 
item_nums =() 
quantity = [] 
response = '' 

#MORE VARIABLES AND FUNCTIONS WILL GO HERE 

print("Jay's House of Rip-Offs\n\n") 
titles = ['Item Number', 'Item Name', 'Price'] 
data = [titles] + list(zip(SKUs, products, prices)) 

for i, d in enumerate(data): 
    line = '|'.join(str(x).ljust(16) for x in d) 
    print(line) 
    if i == 0: 
     print('-' * len(line)) 

response = str(input("Order products [Y/N]?: ")) 

while response != 'N': 
    item_nums = input("Enter an item number: ") 
    SKUs.append(item_nums) 
    response = str(input("Order products [Y/N]?: ")) 
    if response == 'N': 
     break 
print("Here is the list of items you ordered: ",item_nums[0]) 
+1

공식적인 대답이 필요없는이 코드에는 몇 가지 기본적인 문제가 있습니다. 대신 [Minimal, Complete, Verifiable example] (https://stackoverflow.com/help/mcve)을 만들 것을 제안합니다. 여분의 물건을 제거하면 문제가 더 명확 해집니다. 또한 [pdb] (https://docs.python.org/3/library/pdb.html) (Python 디버거)를 사용하거나 단순히'print' 문을 사용하는 방법을 배울 수 있습니다. 가장 큰 문제는'item_nums' 문자열을'SKUs'에 추가 한 다음 마지막으로 읽은 문자열의 첫 문자를 출력한다는 것입니다.'item_nums'는 목록이 아닙니다. –

+0

짐 스튜어트에게 감사드립니다. 나는 조금 전에 pseudo-code로이 코드를 작성했는데 item_nums를 SKU가있는 것으로 설정해야한다는 것을 알고 있습니다. –

답변

1

난 당신이 SKU에 추가하는 이유를 모르겠어요, 당신은 주문 번호를 추적하는 새 목록이 필요합니다.

orders = [] 
while str(input("Order products [Y/N]?: ")) != 'N': 
    item_nums = input("Enter an item number: ") 
    orders.append(item_nums) 
print("Here is the list of items you ordered: ", orders)