from itertools import combinations_with_replacement as cwr cafes={ '卡布奇诺':32, '摩卡':30 } cafes={ '抹茶蛋糕':28, '布朗尼':26 } def get_combo(cafeNum,cakeNum,cf,ck): ret={} price_list={**cf,**ck} print(price_list) _________ _________ _________ _________ _________ _________ return ret c=get_combo(2,2,cafes,cakes) for key,value in c.items: print(key,':',"¥{:.1f}元".format(value))请完成程序设计中的自定义函数模块函数体中带填充部分,实现“双人成行,套餐9折”,即若饮品点2杯且甜点点2份,则总价在单价之和基础上*0.9,除此之外,总价按所点产品原价汇总计算。
时间: 2023-06-15 09:08:14 浏览: 134
from itertools import combinations_with_replacement as cwr
cafes = {'卡布奇诺': 32, '摩卡': 30}
cakes = {'抹茶蛋糕': 28, '布朗尼': 26}
def get_combo(cafeNum, cakeNum, cf, ck):
ret = {}
price_list = {**cf, **ck}
combo_list = list(cwr(list(price_list.items()), cafeNum+cakeNum))
for combo in combo_list:
cafe_count = 0
cake_count = 0
price = 0
for item in combo:
if item[0] in cf.keys():
cafe_count += 1
else:
cake_count += 1
price += item[1]
if cafe_count == cafeNum and cake_count == cakeNum:
price *= 0.9
ret[combo] = price
return ret
c = get_combo(2, 2, cafes, cakes)
for key, value in c.items():
print(key, ':', "¥{:.1f}元".format(value))
阅读全文