餐点没有显示,尽管有正确的食材。

huangapple go评论56阅读模式
英文:

Meal not showing up when right ingredients are present

问题

当我在ingredients_list中放入正确的配料以制作一餐,并点击“pot”按钮时,它显示“无法制作餐点”,即使正确的配料已经准备好。以下是代码:
class IngredientPopup(Popup):
    def __init__(self, **kwargs):
        super(IngredientPopup, self).__init__(**kwargs)

    def load_ingredients_list(self):
        # 配料列表
        ingredients_list = ['盐', '胡椒粉', '糖', '面粉', '大米', '鸡肉', '胡萝卜', '洋葱']

        # 更新标签以显示当前配料列表
        self.current_ingredients_label.text = '当前配料:\n' + '\n'.join(ingredients_list)

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 = ['大米', '鸡肉', '胡萝卜', '洋葱']
        meals = ['鸡肉炖饭', '鸡肉炖菜', '鸡肉咖喱饭']
        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 '无法制作餐点')
        popup = Popup(title='可能的餐点', content=content, size_hint=(0.8, 0.8))
        popup.open()

class MyApp(App):
    def build(self):
        # 创建主要布局
        layout = FloatLayout()

        # 创建背景图像
        background = Image(source='C:/Users/Nikola/Desktop/KuhinjaV2.jpg')

        # 创建按钮
        button4 = Pot(text='锅')

        # 将按钮定位在背景图像上方
        button4.pos_hint = {'x': 0.094, 'y': 0.38}
        button4.size_hint = (0.07, 0.07)

        # 将背景和按钮添加到布局中
        layout.add_widget(background)
        layout.add_widget(button4)
    
        return layout

我尝试将配料添加到ingredients_list,但似乎没有起作用。甚至尝试删除我不需要的其他配料,也没有成功。


<details>
<summary>英文:</summary>

So, i&#39;m making a recipe app. When I put in the right ingredients in my ingredients_list to make a meal, and click on a &quot;pot&quot; button, it says &quot;No meals can be made&quot; 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 = [&#39;salt&#39;, &#39;pepper&#39;, &#39;sugar&#39;, &#39;flour&#39;, &#39;rice&#39;, &#39;chicken&#39;, &#39;carrots&#39;, &#39;onion&#39;]

    # Update the label to display the current ingredients list
    self.current_ingredients_label.text = &#39;Current Ingredients:\n&#39; + &#39;\n&#39;.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 = [&#39;salt&#39;, &#39;pepper&#39;, &#39;sugar&#39;, &#39;flour&#39;, &#39;rice&#39;, &#39;chicken&#39;, &#39;carrots&#39;, &#39;onion&#39;]

    # 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 = [&#39;rice&#39;, &#39;chicken&#39;, &#39;carrots&#39;, &#39;onion&#39;]
    meals = [&#39;chicken and rice&#39;, &#39;chicken stew&#39;, &#39;chicken curry&#39;]
    possible_meals = [meal for meal in meals if set(ingredients).issuperset(set(meal.split()))]
    content = Label(text=&#39;\n&#39;.join(possible_meals) if possible_meals else &#39;No meals can be made&#39;)
    popup = Popup(title=&#39;Possible Meals&#39;, 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=&#39;C:/Users/Nikola/Desktop/KuhinjaV2.jpg&#39;)

    # Create the buttons
    button4 = Pot(text=&#39;pot&#39;)

    # Position the buttons on top of the background image
    button4.pos_hint = {&#39;x&#39;: 0.094, &#39;y&#39;: 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&#39;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&#39;t need to make that meal, didn&#39;t work. 

</details>


# 答案1
**得分**: 0

你展示的仍然是很多代码。在调试时,你确实想要尝试对代码中的每个点提出一个假设,然后测试该假设。你可以通过在调试器中运行代码并观察它发生,或者只需在这里添加`print`语句,确保你看到的是你所期望的。

我注意到这段代码:

```python
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()))]

如果在此之后加上print('Possible meals:', possible_meals),你会发现你的可能餐点是空的。我相当确定这不是你期望的结果。为了弄清楚原因,让我们将事情变得更小,并进行调查:

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

所以,为了使“鸡肉和米饭”成为可行的餐点,你需要的配料包括chickenandrice;但是你没有叫做and的配料。同样,对于“鸡肉炖菜”,你需要一个叫做stew的配料。

英文:

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 = [&#39;rice&#39;, &#39;chicken&#39;, &#39;carrots&#39;, &#39;onion&#39;]
meals = [&#39;chicken and rice&#39;, &#39;chicken stew&#39;, &#39;chicken curry&#39;]
possible_meals = [meal for meal in meals if
   set(ingredients).issuperset(set(meal.split()))]

If you put a print(&#39;Possible meals:&#39;, 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(&#39;Split is:&#39;, meal.split())
   ...: 
Split is: [&#39;chicken&#39;, &#39;and&#39;, &#39;rice&#39;]
Split is: [&#39;chicken&#39;, &#39;stew&#39;]
Split is: [&#39;chicken&#39;, &#39;curry&#39;]

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.

huangapple
  • 本文由 发表于 2023年4月20日 03:17:27
  • 转载请务必保留本文链接:https://go.coder-hub.com/76058100.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定