Search code examples
pythonpython-3.xlistappendshopping-cart

trouble adding product prices to a list for checkout in python shopping program


This is a shopping cart program where the user can specify which items to put in the cart and how many, they select items from a list. For example, they can add the movie Frozen to the cart for 3 units. Each item has a price associated with it which is specified in a dictionary called ItemPriceInv. I have to put all of these item prices in a list for the number of times the item appears in cart in order to calculate the sum. This is what i have so far, it will only add one occurence of price for each unique item in cart while i need it to list the prices of items for as many times as they appear in the cart:

class Cart():
    #prints options to screen
    print(''' 
        1: Add an Item
        2: Remove an Item
        3: View Cart
        4: View List
        5: Checkout
        6: Exit
        ''')
    #dictionary of item prices and inventory
    ItemPriceInv = {'The Bourne Identity':  [9.99, 200], 'Harry Potter': [15.99,1000], 'The Holy Grail': [4.75, 800],
                'Arrival': [24.99, 900], 'Hidden Figures': [29.98, 3], 'Fantastic Beastes': [11.99, 2000],
                'Frozen':   [19.99, 77], 'The Godfather':[10.99 ,55], 'Analyze This': [8.99 ,20],
                'American Splendor':[3.99,  50], 'Lego Movie':  [19.99, 233], 'Transformers': [24.99, 500],
                'Limitless': [29.99, 2], 'The Matrix': [10.99,  278]}



    cart= {}
    cost = []
    command = int(input("Enter the option you would like to execute: "))

    while command != 6:
        if command == 1:  #code for 'add item' option
            product = input("Enter the product you would like to add: ")
            if product == 'cart':
               print(cart)
            if product in ItemPriceInv:
                NumofProducts = int(input('Enter the number of this product you would like to add to cart: '))
                if NumofProducts > ItemPriceInv[product][1]:
                    print("We do not carry that quantity of", product)
                    print(cart)
                else:
                    cart[product] = NumofProducts
                    ItemPriceInv[product][1]= ItemPriceInv[product][1]- NumofProducts
            if product not in ItemPriceInv:
                print('We do not carry this product :( ')
        elif command == 2: #code for option 2
            product = input('Enter an item to remove: ')
            NumofProducts = int(input('How many of this item do you want to remove? '))
            cart[product]-= NumofProducts
            print(cart)
        elif command == 3:
            print(cart)
        elif command == 4:
            print(ItemPriceInv)
        elif command == 5:
            ItemPriceList = []
            for item in cart:
                ItemPrice = ItemPriceInv[item][0]
                ItemPriceList.append(ItemPrice)
            print(ItemPriceList)





                #ItemPriceList.append(ItemPrice) #prices of items in cart
                #L = list(itertools.repeat([ItemPrice, NumofProducts]))
            #for ItemPrice in ItemPriceList:
                #ItemPriceList = [ItemPrice] * NumofProducts


        elif command != 6:
            print('please enter a valid option')
        command = int(input('Enter the option you would like to execute: '))

    else:
        print('Cart closed')

Solution

  • ···
    elif command == 5:
                ItemPriceList = []
                for item in cart:
                    ItemPrice = ItemPriceInv[item][0]
                    ItemNum = cart[item]
                    ItemPriceList.extend([ItemPrice]*ItemNum)
                print(ItemPriceList)
    

    what I have to say is that follow a good code style please. And the unused code that with # just do what you want.