How to combine these two methods in Python?

Viewed 35

I would like to combine these two methods into one but I am running into an issue with how to accomplish this. I am reading in a list of groceries from a JSON file that will pass in 1 parameter (food_name). I want to call add_to_cart so that the method goes through each food item listed below and compares the grocery with the key of the grocery_cap dictionary. If an item contains that specific word then the method should change the "click" to the respective value of the key-value pair. But if it doesnt then just return a 1.


Example data: "H-E-B Texas Sized Hamburger Buns", "H-E-B Select Ingredients 100% Apple Juice", "Louisiana Fish Fry Products Seasoned Crispy Fish Fry", "Andy's Seasoning Mild Chicken Breading", "Progresso Garlic and Herb Bread Crumbs"


*GLOBAL VARIABLES*

cereal_cap, juice_cap, water_cap, salad_cap, milk_cap = 2, 2, 3, 5, 6

grocery_cap = {'Water': water_cap, 'Juice': juice_cap, 'Milk': milk_cap, 'Salad': salad_cap,
               'Cereal': cereal_cap}

cart_png = 'pictures/shopping/add_to_cart_btn.png'

############################################################################################

def add_to_cart():
    print('Found item')
    btn_x, btn_y = pyautogui.locateCenterOnScreen(cart_png, confidence=0.8)
    pyautogui.click(btn_x, btn_y)
    pyautogui.click(button='left', clicks=1, interval=2.0)

def add_to_cart_limit(grocery):
    btn_x, btn_y = pyautogui.locateCenterOnScreen(cart_png, confidence=0.8)
    for k, v in grocery_cap.items():
        if k in grocery:
            print(v)
            pyautogui.click(btn_x, btn_y)
            pyautogui.click(button='left', clicks=v, interval=2.0)

So based on the example data I would like to pass in the first string into the function "H-E-B Texas Sized Hamburger Buns" and see if it contains the words "Milk", "Juice", "Cereal", "Water", etc. in the grocery_cap dictionary (It needs to check every string). Since it doesnt contain the string the click will be 1 (pyautogui.click(button='left', clicks=1, interval=2.0).

Then I want it to go to the next item "H-E-B Select Ingredients 100% Apple Juice" and see if the string contains "Juice". Since it does then I need clicks to be equal to 2.

And continue to iterate through the rest of the list.

Is there any way to achieve this in one method? I have tried adding an else statement but that doesnt give me the desired outcome I need.

0 Answers
Related