简体   繁体   English

Python3-将用户响应添加到列表,而不是覆盖现有数据

[英]Python3 - Adding User Response to a List Instead of Over-writing Existing Data

I am trying to write a basic store-front script that loops until the customer says no to the question. 我正在尝试编写一个基本的店面脚本,该脚本将循环运行,直到客户对问题说不。 Each time there's input of an Item Number, I'm trying to store it and then eventually be able to match those numbers up with the Item Name and the Price (not quite yet, though)... 每次输入商品编号时,我都会尝试存储它,然后最终能够将这些编号与商品名称和价格进行匹配(不过还不完全如此)...

I am just, now, trying to get it to add to the empty list "item_nums" instead of adding the last entry and over-writing the previous numbers. 现在,我只是试图将其添加到空列表“ item_nums”中,而不是添加最后一个条目并覆盖之前的数字。

STOREKEEPER 店主

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])

I'm not sure why you're appending to SKU , you need a new list to track order numbers. 我不确定为什么要附加到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)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM