简体   繁体   中英

Printing multiple list in a for loop

Hello I am trying to create the table below by using list. The product s and the prices are already in lists (priceList and productList). i have successfully created new more list where the person types in the product code (code_inputed) and quantity (quan_inputed).

##updated

def showRecord():
    for idx, kk in enumerate(code_inputed):   
        code = code_inputed[idx]
        quan = quan_inputed[idx]
        price = priceList[idx]
        product = productList[idx]
        print("Code", "Quanity", "Price", "Product")

在此处输入图像描述 print(code, quan, price, product)

在此处输入图像描述

I tried to create a basic for loop which goes down finds the code and acts as the index for product and price. I used the code below.

productList = ['Salad Server Set', 'Party Serviette Holder', 'Tea Set', 'Mixing Bowl Set', 'Knife Block Set',
           'Coffee Capsule Holder', 'Plastic Sensor Soap Pump', 'Storage Bucket', 'Oven Glove', 'Apron', 
           'Biscuit Barrel', 'Chopping Board', 'Carioca Cups', 'Soup Bowls', 'Elevate Wood Turner', 
           'Pasta Machine', 'Teapot', 'Cake Pop Scoop', 'Cookbook Stand', 'Chocolate Station', 'Coffee Maker', 
           'Pepper Mill', 'Salt Mill', 'Glass Storage Jar', 'Measuring jug', 'Kitchen Scale', 'Tenderiser', 
           'Pizza Docker', 'Knife Sharpener', 'Steel Cork Opener', 'Steel Garlic Press', 'Steel Can Opener', 
           'Stainless Steel Crank Flour Sifter', 'Mineral Stone Mortar and Pestle', 'Citrus Cather', 
           'Cherry & Olive Pitter', 'Multi Grater-Detachable', 'Stainless Steel Colander', 'Steel Pizza Pan', 
           'Pop Container'];

priceList = [18.70, 11.95, 39.95, 49.95, 99.95, 29.95, 79.95, 24.95, 9.95, 29.95, 39.95, 12.95, 54.95,
         43.00, 19.95, 144.95, 29.95, 9.95, 29.95, 34.95, 29.00, 84.94, 84.95, 4.95, 19.95, 39.95, 34.95, 
         19.95, 79.95, 36.95, 34.95, 36.95, 33.95, 74.95, 19.95, 27.95, 26.95, 44.95, 12.95, 22.95]; 

code_inputed = []
quan_inputed = []

 #types in valid code and quantity 
def add_item():
 #two other statements which are not important 
 #while quan and code are valid and "END" is not written into the item code, it 
#will continue to ask for item code and quan amount and add to 
 #the list and once an invalid code or END is written the showFunction() will execute
    else:
        code_inputed.append(i)
        quan_inputed.append(s)
    code_inputed.append(code_inputed)
    quan_inputed.append(quan_inputed)
         

def showRecord():
    for kk in code_inputed:
        code_1 = (code_inputed[kk])
        quan = quan_inputed
        price = priceList[kk]
        product = productList[kk]
#if in the code is inputed more than once add all of the qunaities of the same code together 
        if code_1  not in code_inputed > 1:
        #if the same product code is inputed twice add the qunaities together and 
        #post as one but i don't know how
#adds the qunaties together but I don't know how to 
            quan = quan[kk] + quan[kk]
#if multiple take 10% off the price
            price = price * quan - (0.1*price)
 #tempoarary headers just want the basic outputs working at the moment 
            print("Code", "Quanity", "Price", "Product")
            print(code_1, quan, price, product)
   #if the code does not appear in the inputed codes qunaity should equal 0
        elif code not in code_inputed:
           quan = 0
           price = 0
        else:
            print("Code", "Quanity", "Price", "Product")
            print(code_1, quan, price, product) 

but it outputs TypeError: list indices must be integers or slices, not list

Any help would be greatly appreciated as I don't know what to do. I have tried several methods already and this is the closest I've come to getting multiple inputs at once.

Thank you

在此处输入图像描述 Try This:

def add_item():
    code_inputed = []
    quan_inputed = []
    while True:
        i = input("enter code: ")
        if i != "END":
            q = input("enter quantity: ")
            code_inputed.append(int(i))
            quan_inputed.append(int(q))
        else:
            break
    return code_inputed,quan_inputed

def showRecord(code_inputed, quan_inputed):
    product_info = {}
    for kk in range(len(code_inputed)):
        quan = quan_inputed[kk]
        kk = code_inputed[kk]
        price = priceList[kk]
        product = productList[kk]
        if kk not in product_info:
            product_info[kk] = [kk, quan, price, product]
        else:
            product_info[kk][1] += quan
            product_info[kk][2] = product_info[kk][1] * price
    for x in ["Code", "Quanity", "Price", "Product"]:
        print(x, end="  ")
    print()
    for x in product_info:
        for info in product_info[x]:
            print(info, end="     ")
        print()

code_inputed, quan_inputed = add_item()
showRecord(code_inputed, quan_inputed)

Just to explain my comment about the other approach, I recommend storing the data in other way than just in multiple lists. For example like this:

items = [
    {'code': 2, 'price': 39.95, 'quantity': 11, 'product': "Tea Set\t\t"},
    {'code': 34, 'price': 19.95, 'quantity': 3, 'product': "Citrus Cather"}
]

print("Code\tProduct\t\t\tPrice $\tQuantinty\tCost $")
print("------------------------------------------------------")
for item in items:
    print(f"{item.get('code')}\t\t{item.get('product')}\t{item.get('price')}\t{item.get('quantity')}\t\t\t{item.get('price') * item.get('quantity')}")

Those tabs (\t) in product name in dictionary are just to make the table nice, you should come up with nicer way to print it...

Result:

在此处输入图像描述

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