Python 如何使列表和字典的函数成为摘要?
我有数据样本:Python 如何使列表和字典的函数成为摘要?,python,list,function,dictionary,transactions,Python,List,Function,Dictionary,Transactions,我有数据样本: 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: m
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
您的代码似乎甚至没有尝试处理折扣价格。要开始清理此代码片段,请执行以下操作:1
BUNDED
不应同时是循环变量和函数输入,这至少会让人困惑。2. <代码>键和值
未在任何位置定义。3. <代码>“键”不是价格中的键。4.一旦进入for循环,buy
就是一个字符串,可能不应该被索引/不能被另一个字符串索引。5. <代码>总计
未定义。您能指导我一步一步地解决它吗?
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