Home > other >  Python: Having a list reference another list to create a list of options
Python: Having a list reference another list to create a list of options

Time:06-15

I am trying to create a simple random meal picker. I want it to allow the user to select from multiple check boxes of ingredients then take that new list it made and have it index the other lists for the meals and then use the random.choice option to have it only suggest meals that have all the ingredients(List items) checked off by the user.

So the idea is that it will create a list from the selected check boxes called cb_vars. that will be a list of ingredients, I then want it to scan each list to see if any of the lists have all their ingredients represented in cb_vars, then it would take all the lists that match that criteria and make a list of those recipes(list names) and randomly select one.

I've tried a few different ways and can't seem to figure it out. I am new and trying to learn on my own.

import random
from tkinter import *


root = Tk()

category_data = ['Ground Beef', 'Cheese', 'Buns', 'Bacon', 'Eggs', 'Mushrooms', 'Egg Noodles', 'Gravy']
meals = ['burgers', 'omelettes']
burgers = ['Ground Beef', 'Cheese', 'Buns']
omelettes = ['Eggs', 'Cheese', 'Bacon']
selected_meals = []

cb_vars = {}  # dict to store the BooleanVar
for category in category_data:
    var = BooleanVar()
    l = Checkbutton(root, text=category, variable=var, onvalue=True, offvalue=False)
    l.pack(anchor=W)
    cb_vars[category] = var  # store the BooleanVar

check = any(item in cb_vars for item in burgers or omelettes)

def select_meals():
    check
    if check is True:
        print(random.choice(check))
    else:
        print("You need to go shopping.")

Button(root, text="Randomize Meal", command=select_meals).pack()

root.mainloop()

CodePudding user response:

There are multiple problems in your coding attempt which need to be addressed in order to make the code deliver expected results. With learning by doing you have now the chance to understand how it comes that following code provides the requested result. This will enable you to extend or modify the code to adapt it better to your needs:

import random
from   tkinter import Tk, Checkbutton, BooleanVar, Button, W

root = Tk()

category_data  = ['Ground Beef', 'Cheese', 'Buns', 'Bacon', 'Eggs', 'Mushrooms', 'Egg Noodles', 'Gravy']
meals          = ['burgers', 'omelettes']
burgers        = ['Ground Beef', 'Cheese', 'Buns']
omelettes      = ['Eggs', 'Cheese', 'Bacon']

cb_vars = [] # list to store the BooleanVar
indx    = 0  
for category in category_data:
    cb_vars.append(BooleanVar())
    l = Checkbutton(root, text=category, variable=cb_vars[indx], onvalue=True, offvalue=False)
    indx = indx   1  # store the BooleanVar list index == index to category_data
    l.pack(anchor=W)

def select_meals():
    availableMeals = []
    categoryChoice = []
    for indx, cb_var in enumerate(cb_vars):
       if cb_var.get() is True:
           categoryChoice.append(category_data[indx])
    print(categoryChoice)
    for meal in meals:
        mealFits = True
        for choice in categoryChoice: 
           if choice in eval(meal): # eval() returns a list with ingredients
               pass # meal OK
           else:
               mealFits = False
        if mealFits:
            availableMeals.append(meal)
    if len(availableMeals) == 0:                 
        print("You need to go shopping.")
    else: 
        print('Available:', availableMeals)
        print('Choice:   ', random.choice(availableMeals))

Button(root, text="Randomize Meal", command=select_meals).pack()

root.mainloop()
  • Related