Finding Item In 3D List

Viewed 68

I am doing a school project where a customer orders one item of food. They will then click Submit Order, it will then run a function which finds what food they ordered in a list, take the value next to it in the sub-list, which is a price, then display it in a textbox to confirm the order, which will then add the order to a database. This is all displayed using tkinter.

The user picks what food they want using a radiobutton, which is the stored as self.r:

self.tab1_rbut1 = ttk.Radiobutton(self.tab1, variable=self.r, value=self.food_list[0][0][1])

Currently, the program can't find self.r inside the list (which gets the its string from the same list), and I haven't been able to figure out the problem.

The list:

self.food_list = [[[0, 'Caesar', 6], 
                   [1, 'Greek', 6],
                   [2, 'Tex Mex', 6]],
                  [[3, 'BLT', 3],
                   [4, 'Veggie', 4.5],
                   [5, 'Ham & Cheese', 3.5]],
                  [[6, 'Baconator Panini', 4.8],
                   [7, 'Breakfast Burrito', 4.8],
                   [8, 'Chicken Burger', 4.8]]]

The function:

def confirm_order(self):
    self.first_list = []
    self.second_list = []
    self.first_list.append(self.r.get())
    print(self.first_list)

    if self.r.get() in self.food_list:
        print(self.food_list)

To put it simply, is there a way to find the item next to another item in 3D list?

If it helps, here is the entire code:

from tkinter import *
from tkinter import ttk
from tkinter import messagebox
import mysql.connector

mydb = mysql.connector.connect(
    host="localhost",
    user=[name],
    password="Likllikllikl1",
    database="orderdb"
)

class FoodOrder:
    def __init__(self, parent):
        self.customer = StringVar()
        self.message_text = StringVar()


        #Create top frame
        self.top_frame = ttk.LabelFrame(root, text="Food Order")
        self.top_frame.grid(row=0, column=0, padx=10, pady=10, sticky="NSEW")

        #Create bottom frame
        self.bottom_frame = ttk.LabelFrame(root, text="Place Order")
        self.bottom_frame.grid(row=1, column=0, padx=10, pady=10, sticky="NSEW")

        #Create and set the message text variable
        self.message_text.set("Welcome! You can select and order your food from The Food Depot from here. You may only selet ONE item.")

        #Create and pack the message label
        self.message_label = ttk.Label(self.top_frame, textvariable=self.message_text, wraplength=250)
        self.message_label.grid(row=0, column=0, columnspan=2, padx=10, pady=10)

        #Create the PhotoIMage and label to hold it
        self.logo_image = PhotoImage(file="images/canteen_logo.png", height=100, width=100)
        self.logo_label = ttk.Label(self.top_frame, image=self.logo_image)
        self.logo_label.grid(row=1, column=0, columnspan=2, padx=10, pady=10)

        #Create label for customer name
        self.customer_label = ttk.Label(self.top_frame, text="Customer Name: ")
        self.customer_label.grid(row=3, column=0, padx=10, pady=3)

        self.customer_entry = ttk.Entry(self.top_frame)
        self.customer_entry.grid(row=3, column=1, padx=10, pady=3, sticky="WE")

        self.food_id_label = ttk.Label(self.top_frame, text="Food ID:")
        self.food_id_label.grid(row=4, column=0, padx=10, pady=3)

        self.food_id_entry = ttk.Entry(self.top_frame)
        self.food_id_entry.grid(row=4, column=1, padx=10, pady=3, sticky="WE")

        #Buttons
        self.submit_button = ttk.Button(self.bottom_frame, text="Submit Order", command=self.confirm_order)
        self.submit_button.grid(row=6, column=0, columnspan=2, padx=10, pady=10)

        self.exit_button = ttk.Button(self.bottom_frame, text="Exit", command=exit)
        self.exit_button.grid(row=11, column=0, columnspan=2, padx=10, pady=10)

        self.Tabs()

    def Tabs(self):

        self.food_cat = ["Salads", "Sandwiches", "Hot Food"]
        #ID, Name, Cost
        self.food_list = [[[0, "Caesar", 6],[1, "Greek", 6],[2, "Tex Mex", 6]],[[3, "BLT", 3],[4, "Veggie", 4.5],[5, "Ham & Cheese", 3.5]],[[6, "Baconator Panini", 4.8],[7, "Breakfast Burrito", 4.8],[8, "Chicken Burger", 4.8]]]

        self.image_list=[]
        self.image_list.append(PhotoImage(file=r"images/food/caesar_salad.png"))
        self.image_list.append(PhotoImage(file=r"images/food/blt.png"))
        self.image_list.append(PhotoImage(file=r"images/food/baconator.png"))


        self.tabControl = ttk.Notebook(root)
        self.r = StringVar()

        #Tab 1
        self.tab1 = ttk.Frame(self.tabControl)
        self.tabControl.add(self.tab1, text=self.food_cat[0])

        for i in range(len(self.food_list)):
            row = i

            self.tab1_image = ttk.Label(self.tab1, image=self.image_list[i])
            self.tab1_image.grid(row=row, column=0)

            self.tab1_item_label = ttk.Label(self.tab1, text=self.food_list[0][i][1])
            self.tab1_item_label.grid(row=row, column=1, padx=10, pady=10)

            self.tab1_price_label = ttk.Label(self.tab1, text="${:.2f}".format(self.food_list[0][i][2]))
            self.tab1_price_label.grid(row=row, column=2, padx=10, pady=10)


        self.tab1_rbut1 = ttk.Radiobutton(self.tab1, variable=self.r, value=self.food_list[0][0][1])
        self.tab1_rbut1.grid(row=0, column=3, padx=10, pady=10)

        self.tab1_rbut2 = ttk.Radiobutton(self.tab1, variable=self.r, value=self.food_list[0][1][1])
        self.tab1_rbut2.grid(row=1, column=3, padx=10, pady=10)

        self.tab1_rbut3 = ttk.Radiobutton(self.tab1, variable=self.r, value=self.food_list[0][2][1])
        self.tab1_rbut3.grid(row=2, column=3, padx=10, pady=10)


        #Tab 2
        self.tab2 = ttk.Frame(self.tabControl)
        self.tabControl.add(self.tab2, text=self.food_cat[1])

    for i in range(len(self.food_list)):
        row = i

        self.tab2_image = ttk.Label(self.tab2, image=self.image_list[i])
        self.tab2_image.grid(row=row, column=0)

        self.tab2_item_label = ttk.Label(self.tab2, text=self.food_list[1][i][1])
        self.tab2_item_label.grid(row=row, column=1, padx=10, pady=10)

        self.tab2_price_label = ttk.Label(self.tab2, text="${:.2f}".format(self.food_list[1][i][2]))
        self.tab2_price_label.grid(row=row, column=2, padx=10, pady=10)

        self.tab2_rbut1 = ttk.Radiobutton(self.tab2, variable=self.r, value=self.food_list[1][0][1])
        self.tab2_rbut1.grid(row=0, column=3, padx=10, pady=10)

        self.tab2_rbut2 = ttk.Radiobutton(self.tab2, variable=self.r, value=self.food_list[1][1][1])
        self.tab2_rbut2.grid(row=1, column=3, padx=10, pady=10)

        self.tab2_rbut3 = ttk.Radiobutton(self.tab2, variable=self.r, value=self.food_list[1][2][1])
        self.tab2_rbut3.grid(row=2, column=3, padx=10, pady=10)

        #Tab 3
        self.tab3 = ttk.Frame(self.tabControl)
        self.tabControl.add(self.tab3, text=self.food_cat[2])

        for i in range(len(self.food_list)):
            row = i

            self.tab3_image = ttk.Label(self.tab3, image=self.image_list[i])
            self.tab3_image.grid(row=row, column=0)

            self.tab3_item_label = ttk.Label(self.tab3, text=self.food_list[2][i][1])
            self.tab3_item_label.grid(row=row, column=1, padx=10, pady=10)

            self.tab3_price_label = ttk.Label(self.tab3, text="${:.2f}".format(self.food_list[2][i][2]))
            self.tab3_price_label.grid(row=row, column=2, padx=10, pady=10)

        self.tab3_rbut1 = ttk.Radiobutton(self.tab3, variable=self.r, value=self.food_list[2][0][1])
        self.tab3_rbut1.grid(row=0, column=3, padx=10, pady=10)

        self.tab3_rbut2 = ttk.Radiobutton(self.tab3, variable=self.r, value=self.food_list[2][1][1])
        self.tab3_rbut2.grid(row=1, column=3, padx=10, pady=10)

        self.tab3_rbut3 = ttk.Radiobutton(self.tab3, variable=self.r, value=self.food_list[2][2][1])
        self.tab3_rbut3.grid(row=2, column=3, padx=10, pady=10)

        #Tab placement in GUI
        for i in range(len(self.food_cat)):
            column = i
            self.tabControl.grid(row=0, column=column)

    def confirm_order(self):
        self.order_l = []
        self.order_l.append(self.r.get())
        print(self.order_l)

        if self.r.get() in self.food_list:
            print(self.order_l)

    def take_order(self):
        self.mycursor = mydb.cursor()
        self.sql = "INSERT INTO orders (name, foodid) VALUES (%s, %s)"
        self.val = (self.customer_entry.get(), self.r.get())
        self.mycursor.execute(self.sql, self.val)
        mydb.commit()
        messagebox.showinfo(title="Order Placed!", message="Your order has been placed!")

# main route
if __name__ == "__main__":
    root = Tk()
    root.title("Food Order App")
    food_order=FoodOrder(root)
    root.mainloop()
1 Answers

This should give you the basic idea of how to search through the 3d list:

def find(thing, list3d):
    for x in list3d:
        for y in x:
            if y[1] == thing:
                return True
    return False

def check(item, list3d):
    result = 'found' if find(item, list3d) else 'not found'
    print(f'{item} was {result}')


food_list = [[[0, 'Caesar', 6],
              [1, 'Greek', 6],
              [2, 'Tex Mex', 6]],
             [[3, 'BLT', 3],
              [4, 'Veggie', 4.5],
              [5, 'Ham & Cheese', 3.5]],
             [[6, 'Baconator Panini', 4.8],
              [7, 'Breakfast Burrito', 4.8],
              [8, 'Chicken Burger', 4.8]]]

check('BLT', food_list)
check('Steak', food_list)
Related