我有数据样本:
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
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