Generation

generate functionSun, 11 Dec 2022

Write a function that multiplies each dictionary value by portions portions = 5 grams = 0 key = 'salad' cook_book = {'salad': [{'ingridient_name': 'cheese', 'quantity': 50, 'measure': 'gr'}, {'ingridient_name': 'tomatoes', 'quantity': 2, 'measure': 'pct'}, {'ingridient_name': 'pepper', 'quantity': 20, 'measure': 'гр'}]} if key == 'salad': grams = 'quantity' * portions print(grams) The result should be: Cheese: 210 g Tomatoes: 6 pcs. Cucumbers: 60g Olives: 30 g Olive oil: 60 ml Lettuce: 30 gr Pepper: 60 gr

def multiply_cook_book(portions): new_book = {} for key, value in cook_book.items(): new_book[key] = [] for d in value: new_book[key].append({'ingridient_name': d['ingridient_name'], 'quantity': d['quantity'] * portions, 'measure': d['measure']}) return new_book print(multiply_cook_book(2))

Questions about programming?Chat with your personal AI assistant