英文:
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'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.
</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']
所以,为了使“鸡肉和米饭”成为可行的餐点,你需要的配料包括chicken
、and
、rice
;但是你没有叫做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 = ['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
.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论