Search code examples
pythonshopping-cart

How to create simple simulaton code using classes


I want to create simple simulation code using classes like:

product_list = []
price = []
barcode = ["banana": 123 , "apple": 234 , "orange": 345 , "pear": 456 ] 

I want to add product in cart by using their representative number and sum up added product price.

Example: If the user enters 123 then banana need to add in list and price of banana should add in price list.


Solution

  • Looks like you need a python dictionary.

    Ex:

    barcode = {"banana": 123 , "apple": 234 , "orange": 345 , "pear": 456 }
    barcode = dict((v,k) for k,v in barcode.items())    #-->Reverse Key -Value
    v = 123
    
    product_list = []
    price = []    
    
    product_list.append(barcode[123])
    price.append(123)
    
    print(product_list)
    print(price)