我怎样才能使列表和字典的功能成为摘要?
how can i make function with list and dictionary become summary?
我有数据样本:
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
我有数据样本:
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