在 python 购物程序中将产品价格添加到结帐列表时遇到问题

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

这是一个购物车程序,用户可以在其中指定要放入购物车的商品以及数量,它们是 select 列表中的商品。例如,他们可以将电影《冰雪奇缘》添加到购物车中 3 个单位。每个项目都有一个与之关联的价格,该价格在名为 ItemPriceInv 的字典中指定。我必须将所有这些商品价格放在一个列表中,以计算商品在购物车中出现的次数,以便计算总和。这是我到目前为止所拥有的,它只会为购物车中的每个独特商品添加一次价格,而我需要它列出商品在购物车中出现的次数的价格:

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')
···
elif command == 5:
            ItemPriceList = []
            for item in cart:
                ItemPrice = ItemPriceInv[item][0]
                ItemNum = cart[item]
                ItemPriceList.extend([ItemPrice]*ItemNum)
            print(ItemPriceList)

我要说的是,请遵循良好的代码风格。 未使用的代码 # 只是做你想做的。