Search code examples
pythonpython-3.xkivykivy-language

Meal not showing up when right ingredients are present


So, i'm making a recipe app. When I put in the right ingredients in my ingredients_list to make a meal, and click on a "pot" button, it says "No meals can be made" even tho the right ingredients are present. Heres the code:

from kivy.app import App
from kivy.uix.button import Button
from kivy.uix.floatlayout import FloatLayout
from kivy.uix.image import Image
from kivy.uix.popup import Popup
from kivy.uix.label import Label
from kivy.uix.textinput import TextInput
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.gridlayout import GridLayout
from kivy.uix.dropdown import DropDown

class IngredientPopup(Popup):
    def __init__(self, **kwargs):
        super(IngredientPopup, self).__init__(**kwargs)

    def load_ingredients_list(self):
        # Ingredients list
        ingredients_list = ['salt', 'pepper', 'sugar', 'flour', 'rice', 'chicken', 'carrots', 'onion']

        # Update the label to display the current ingredients list
        self.current_ingredients_label.text = 'Current Ingredients:\n' + '\n'.join(ingredients_list)

    def update_ingredient_list(self, instance):
        # Update the list of ingredients displayed in the pop-up window
        self.load_ingredients_list()


class RemoveIngredientPopup(Popup):
    def __init__(self, **kwargs):
        super(RemoveIngredientPopup, self).__init__(**kwargs)

    def load_ingredients_list(self):
        # Ingredients
        ingredients_list = ['salt', 'pepper', 'sugar', 'flour', 'rice', 'chicken', 'carrots', 'onion']

        # Add the ingredients to the dropdown menu
        for ingredient in ingredients_list:
            self.ingredient_dropdown.add_widget(Button(text=ingredient, on_release=self.remove_ingredient, size_hint_y=None, height=44))

class Pot(Button):
    def __init__(self, **kwargs):
        super().__init__(**kwargs)
        self.opacity = 1
        self.size_hint = (None, None)
        self.size = (0, 0)
        
    def on_press(self):
        ingredients = ['rice', 'chicken', 'carrots', 'onion']
        meals = ['chicken and rice', 'chicken stew', 'chicken curry']
        possible_meals = [meal for meal in meals if set(ingredients).issuperset(set(meal.split()))]
        content = Label(text='\n'.join(possible_meals) if possible_meals else 'No meals can be made')
        popup = Popup(title='Possible Meals', content=content, size_hint=(0.8, 0.8))
        popup.open()

class MyApp(App):
    def build(self):
        # Create the main layout
        layout = FloatLayout()

        # Create the background image
        background = Image(source='C:/Users/Nikola/Desktop/KuhinjaV2.jpg')

        # Create the buttons
        button4 = Pot(text='pot')

        # Position the buttons on top of the background image
        button4.pos_hint = {'x': 0.094, 'y': 0.38}
        button4.size_hint = (0.07, 0.07)

        # Add the background and buttons to the layout
        layout.add_widget(background)
        layout.add_widget(button4)
    
        return layout

if __name__ == '__main__':
    MyApp().run()

I'm a beginner, so some explaining would be really appreciated!

I tried adding the ingredients to ingredients_list, but nothing seemed to work. Even tried removing the other ingredients I didn't need to make that meal, didn't work.


Solution

  • What you've shown is still a lot of code. When debugging you really want to try to make a hypothesis about what you'll see at each point in your code and then test that hypothesis. You could test by running your code in a debugger and watching it happen or you could just put in print statements here and then to make sure you're seeing what you expect.

    I noticed this code:

    ingredients = ['rice', 'chicken', 'carrots', 'onion']
    meals = ['chicken and rice', 'chicken stew', 'chicken curry']
    possible_meals = [meal for meal in meals if
       set(ingredients).issuperset(set(meal.split()))]
    

    If you put a print('Possible meals:', possible_meals) after that you'll discover that your possible meals is empty. I'm pretty sure that's not what you expected. So to figure out why, let's make things smaller yet and investigate:

    In [3]: for meal in meals:
       ...:     print('Split is:', meal.split())
       ...: 
    Split is: ['chicken', 'and', 'rice']
    Split is: ['chicken', 'stew']
    Split is: ['chicken', 'curry']
    

    So for you to have a viable meal for "chicken and rice" you're requiring your ingredients to include chicken, and, rice; but you don't have an ingredient called and. Similarly for "chicken stew" you need an ingredient called stew.