[FIXED] cannot iterate a kwargs input

Issue

Heey everyone! It is my first time to post a question as I am practicing Python by myself. I am not sure if similar question has been asked, but I need some tips with using **kwargs and I look forward for a feedback from a smart, enthusisastic professional coder ;)

I created a class FastFoodChain and a method add_food that if you pass in multiple food names and prices as kwargs, they shall be added to the menu:

class FastFoodChain():

   def __init__(self, name):
       self.name = name
       self.menu = None #type is a dictionary
       print(f"{name} established! Congrats!")
    
   def add_food(self, **foods): # Add a few foods from the product
    
       for food,price in foods.items():

           if food in self.menu.keys():
               return f"{food} is already in the menu"

           else:
               #self.menu[food] = price
               self.menu.setdefault(food,price)
               return f"{food} added to the menu"

McDonalds = FastFoodChain("McDonalds")
McDonalds.menu = {'fries': 3, 'chicken_nuggets': 4, 'cheeseburger': 4.5, 'cola': 2.5, 'chicken_burger': 5}

However, this function is not iterating **foods but only Big_mac, the first argument is updated to the menu:

McDonalds.add_food(Big_Mac = 6, Spicy_chicken_burger=4)

Any suggestions could be helpful. :)) Thanks in advance. Leo


Solution

The problem is that you are using the return statement, which automatically ends the function. You could simply replace the two return statements with the print function.



Answered By - vogelstein
Answer Checked By - Cary Denson (Python.Easybugfix Admin)

Related Posts

Post a Comment