我如何使具有列表和字典的功能成为摘要?

问题描述 投票:0回答:1

我有数据样本:

bought = ['Banana','Banana','Pineapple','Pineapple']
price_of = {'Apple': 6, 'Avocado': 5, 'Banana': 3, 'Blackberries': 10, 'Blueberries':12, 'Cherries': 7, 'Pineapple': 7}
def summary(bought, price_of):
    for bought,n in price_of:
       multi = bought[keys] * price_of['keys']
       print(fruit_price[values],':', values)
       if total >= 10:
          print('discount', total * 5/10)

我仍然感到困惑,如果要购买的最低总金额是10,我想折价5我想这样输出:

2 Banana : 6
2 pineapple : 14
total : 20
discount price : #showing discount price
python list function dictionary transactions
1个回答
1
投票
bought = ['Banana','Banana','Pineapple','Pineapple']
price_of = {'Apple': 6, 'Avocado': 5, 'Banana': 3, 'Blackberries': 10, 'Blueberries':12, 'Cherries': 7, 'Pineapple': 7}

def get_summary(items):
    result = {}
    for item in items:
        if not item in result:
            result[item] = {
                'count': 0,
                'price': 0
            }

        result[item]['price'] += price_of[item]
        result[item]['count'] += 1
    return result


def print_summary(items):
    total = 0
    for key in items:
        total += items[key]['price']
        print('{} {} {}'.format(items[key]['count'], key, items[key]['price']))
    print('Total: {}'.format(total))
    if total >= 10:
        print('Discount: {}'.format(total * 0.5))


print_summary(get_summary(bought))

# 2 Banana 6
# 2 Pineapple 14
# Total: 20
# Discount: 10.0
© www.soinside.com 2019 - 2024. All rights reserved.