Home > Blockchain >  How to match array and object iterators?
How to match array and object iterators?

Time:10-07

I have a pizzaPriceCalculator function that takes in 3 arguments: sauce (String), toppings (initially String) and loyalCustomer (boolean).

I have 2 dictionaries: sauceMenu and toppingsMenu like this:

  sauceMenu = {
    "regular": 4.35,
    "alfredo": 4.6,
    "marinara": 5.85,
    "mix": 5.47
  }
  toppingsMenu = {
    "cheese": 0.5,
    "pepperoni": 0.75,
    "salami": 0.65,
    "olives": 1.0,
    "mushroom": 0.35
  }

Function is called this way:

pizzaPriceCalculator("marinara", "pepperoni", True)

I just iterate through both dictionaries, return costs if there is a match and sum sauce and topping costs to print the final price.

Here is the full code:

def pizzaPriceCalculator (sauce, toppings, loyalCustomer):
  sauceMenu = {
    "regular": 4.35,
    "alfredo": 4.6,
    "marinara": 5.85,
    "mix": 5.47
  }

  toppingsMenu = {
    "cheese": 0.5,
    "pepperoni": 0.75,
    "salami": 0.65,
    "olives": 1.0,
    "mushroom": 0.35
  }

  for i in sauceMenu:
    if sauce is i:
      print(sauceMenu[i])
      baseTeaAmount = sauceMenu[i]

      for j in toppingsMenu:
        if toppings is j:
          print(toppingsMenu[j])
          toppingsAmount = toppingsMenu[j]

          priceBeforeDiscount = baseTeaAmount   toppingsAmount
          print(priceBeforeDiscount)

          if loyalCustomer:
            priceAfterDiscount = priceBeforeDiscount - 1
            print(priceAfterDiscount)
            # return priceAfterDiscount;


pizzaPriceCalculator("marinara", "pepperoni", True)

The problem is that I was told to refactor the code and make 2nd argument (toppings) a list of strings so you can pick many toppings.

 pizzaPriceCalculator("marinara", ["pepperoni", "cheese", "olives"], True)

How do I match an array iterator and object iterator so it sums sauce and all toppings?

I've tried to iterate through the toppings list (from input) and check if it matches our toppingsMenu dictionary iterator but it doesn't work:

for j in toppingsMenu:
    for k in toppings:    
        if toppings[k] is j:
            print(toppingsMenu[j])
            toppingsAmount = toppingsMenu[j]

Also how do I add all picked topping prices to each other and finally to sauce price to get the final cost?

Any advice is appreciated.

CodePudding user response:

Follow the below code. Assuming, for sauce the input is just one item in string & toppings will be a list of many items...

def pizzaPriceCalculator (sauce, toppings, loyalCustomer): 
    sauceMenu = {"regular": 4.35, "alfredo": 4.6, "marinara": 5.85, "mix": 5.47} 
    toppingsMenu = {"cheese": 0.5, "pepperoni": 0.75, "salami": 0.65, "olives": 1.0, "mushroom": 0.35}
        
    baseTeaAmount = sauceMenu[sauce]
    
    toppingsAmount = 0
    for item in toppings:
        toppingsAmount = toppingsAmount   toppingsMenu[item]
        
    priceBeforeDiscount = baseTeaAmount   toppingsAmount

    if loyalCustomer:
        priceAfterDiscount = priceBeforeDiscount - 1
        print(priceAfterDiscount)
    else:
        print(priceBeforeDiscount)


           
pizzaPriceCalculator("marinara", ["pepperoni", "cheese", "olives"], True)

# 7.1 (output)
  • Related