简体   繁体   中英

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.

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.

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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