-1

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

  • Welcome to Stack Overflow! Please take the [tour] and read [ask], which has tips like how to write a good title. Personally, I'd write something more like "Why is only the first argument being used?" It's also important to note that SO is meant to be like a reference, not a forum, so chit-chat like "I'm new here" and such should be kept to a minimum. – wjandrea Jul 08 '22 at 20:29
  • BTW, instead of `self.menu = None`, it'd probably be better to pass `menu` into `__init__`, that way you won't forget to set it. You could also add type hinting instead of that comment. So, something like: `def __init__(self, name: str, menu: dict): ...`. – wjandrea Jul 08 '22 at 20:41

2 Answers2

3

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.

vogelstein
  • 394
  • 1
  • 10
0

The problem is that both if and else end with a return, so the loop will exit after the first time.

Here's a suggestion to fix that.

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():
               print(f"{food} is already in the menu")

           else:
               #self.menu[food] = price
               self.menu.setdefault(food,price)
               print(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}
McDonalds.add_food(Big_Mac = 6, Spicy_chicken_burger=4)
print(McDonalds.__dict__)
Duarte P
  • 154
  • 12