Using a list as an index for another list python

Viewed 64

I am trying to create an online shopping cart system in python to understand list better but have come across some struggles.

I am having difficulties with displaying information using a list. I have already created a list where the person writes in the product the code into an empty list as shown below.

else:
    code_input.append(i)
    quan_input.append(s)
code_inputed.append(code_input)
quan_inputed.append(quan_input)

I want to use the list with the product codes to find the correlating name and price by using trying to use the code_input list as an index to find the items in the other list.

I have written the simple code to try to find if it works but it comes up with TypeError: list indices must be integers or slices, not list

The code

def display():
    index = code_inputed
    for i in code_inputed: # and index in range(len(productList)):
        print(index)
        print(productList[index], quan_inputed[index])

Any help would be greatly appreciated and I'm sorry if none of this makes any sense I am only new.

Thank you

2 Answers

It is a bit unclear what you want, but do you perhaps mean:

def display():
index = code_inputed
for i in code_inputed: # and index in range(len(productList)):
    print(i) #CHANGED TO i!
    print(productList[i], quan_inputed[i])

Incorporate this code based on your needs. But you get the idea on how to do that. I made list of dicts using 3 lists you provided (productList, priceList, code_of_product) And everything else is just straight forward :)

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_of_product = [0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,
       38,39]

# Generate a list of dicts from 3 lists productList, priceList, code_of_product
products = [ {'product_name': productList[i], 'price': priceList[i], 'code': code_of_product[i] } for i in range(len(productList)) ]


def catalogue():
    global productList, priceList, code_of_product
    print('{:<12}\t{:<40}{:<20}'.format("code", "product", "Price $"))
    print('{:<12}\t{:<40}{:<20}'.format("----", "------------------------------------", "-------"))


    for prod in range(len(products)):
        print('{:<12}\t{:<40}{:<20}'.format(products[prod]["code"], products[prod]["product_name"], products[prod]["price"]))

catalogue()
        

            
code = int(input("Please input code?"))
quantity = int(input("Please input quntity?"))



def showRecord(cd, qty):
price = products[cd]["price"]

total = float(price)*float(qty)
print(f"Product name: {products[cd]['product_name']}")
print(f"Price: ${price}")
print(f"Total: {total}")

showRecord(code, quantity)
Related