Finish the last if statement in python Description: Your job is to implement chef. Chef takes two arguments, a dictionary mapping customer coroutines to dish
Finish the last if statement in python
Description:
Your job is to implement chef. Chef takes two arguments, a dictionary mapping customer coroutines to dish names, and a dictionary mapping dish names to ingredient lists. The chef coroutine should:
Receive ingredients from its supplier, using a yield expression.
After receiving each ingredient, check to see if enough ingredients have been received to serve a customer:
A customer may be served if all of the ingredients in their desired dish have been received.
Ingredients never run out, so the same ingredient can be used to serve multiple customers.
Customers should be served only once.
If the chef coroutine is closed, it should print 'Chef went home.' before exiting.
If the chef coroutine receives ingredients after all customers have been served, it should raise StopIteration('No one left to serve!') to indicate that its work is complete.
Code:
def supplier(ingredients, chef): for ingredient in ingredients: try: chef.send(ingredient) except StopIteration as e: print(e) break chef.close()
def customer(): served = False while True: try: dish = yield print('Yum! Customer got a {}!'.format(dish)) served = True except GeneratorExit: if not served: print('Customer never got served.') raise
def chef(customers, dishes):
remaining_customers = dict(customers) ingredients = set() while True: try: ingredient = yield except GeneratorExit: print('Chef went home.') for customer in customers: customer.close() raise
ingredients.add(ingredient)
if not remaining_customers: raise StopIteration('No one left to serve!')
for customer, dish_name in dict(remaining_customers).items(): # include an if-statement to check if all ingredients for dish are available to chef so that the customer is ready to be served
##### INSET CODE HERE #####
See here for better formatting:
def supplier(ingredients, chef): for ingredient in ingredients: try: chef.send (ingredient) except StopIteration as e print(e) break chef.close() def customer(): served-False while True: try: dish -yield print( 'Yum! Customer got a (!".format (dish) served = True except GeneratorExit: if not served: print( 'Customer never got served.' raise def supplier(ingredients, chef): for ingredient in ingredients: try: chef.send (ingredient) except StopIteration as e print(e) break chef.close() def customer(): served-False while True: try: dish -yield print( 'Yum! Customer got a (!".format (dish) served = True except GeneratorExit: if not served: print( 'Customer never got served.' raiseStep by Step Solution
There are 3 Steps involved in it
Step: 1
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started