Printing multiple list in a for loop

Viewed 75

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 and quantity (quan_inputed).

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

2 Answers

enter image description here 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:

enter image description here

Related