0

I want to categorize the food by category, set the category, and choose the food. I want the total price to rise after choosing food. The order coffee part is a problem for me. Coffee menus overlap with other categories. Also, if I press the order completed button, I want a receipt with a thank you message. What should I do?

My error message is

The menu you entered does not exist.
Exception in Tkinter callback
Traceback (most recent call last):
  File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/tkinter/__init__.py", line 1883, in __call__
    return self.func(*args)
  File "/Users/mason/PycharmProjects/APCSP/APCSP.py", line 174, in <lambda>
    btn_drink_1 = tk.Button(frame3, text="Iced Coffee\n($2.65)", padx="10", pady="10", width="10", command=lambda: drink_add('Iced Coffee'))
  File "/Users/mason/PycharmProjects/APCSP/APCSP.py", line 60, in drink_add
    total_price += this_price
TypeError: unsupported operand type(s) for +=: 'float' and 'NoneType'

Here is my code

price_meal = {"Donut": 1.25, "Filled Donut": 1.50, "Apple Fritter": 2.95, "Cinnamon roll": 2.95, "Muffin": 2.95, "Scone": 2.95}
price_drink = {"Hot Tea": 1.95, "Hot Chocolate": 1.75, "Milk": 1.50, "Chocolate Milk": 1.95, "Soda": 2.25}
price_coffees = {"Iced Coffee": 2.65, "Cold Brew": 3.95, "Iced Americano" :2.45, "Mochatella":4.25, "Iced Mochatella": 4.75, "Cafe Caramel/Mocha": 4.70}

order_meal = {}
order_drink = {}
order_coffees = {}

total_price = 0


def show_meal():
    btn_meal.configure(bg="yellow")
    btn_drink.configure(bg="white")
    frame4.pack_forget()
    frame3.pack_forget()
    frame2.pack(fill="both", expand=True)
    frame4.pack(fill="both", expand=True)

def show_drink():
    btn_meal.configure(bg="white")
    btn_drink.configure(bg="yellow")
    frame4.pack_forget()
    frame2.pack_forget()
    frame3.pack(fill="both", expand=True)
    frame4.pack(fill="both", expand=True)

def show_coffees():
    btn_meal.configure(bg="white")
    btn_drink.configure(bg="yellow")
    frame4.pack_forget()
    frame2.pack_forget()
    frame3.pack(fill="both", expand=True)
    frame4.pack(fill="both", expand=True)


def meal_add(m):
    global price_meal, order_meal, total_price
    if m not in price_meal:
        print("The menu you entered does not exist.")
    this_price = price_meal.get(m)
    total_price += this_price

    if m in order_meal:
        order_meal[m] = order_meal.get(m) + 1
    else:
        order_meal[m] = 1
    print_order()
    print_price()


def drink_add(m):
    global price_drink, order_drink, total_price
    if m not in price_drink:
        print("The menu you entered does not exist.")
    this_price = price_drink.get(m)
    total_price += this_price

    if m in order_drink:
        order_drink[m] = order_drink.get(m) + 1
    else:
        order_drink[m] = 1
    print_order()
    print_price()


def coffees_add(m):
    global price_coffees, order_coffees, total_price
    if m not in price_coffees:
        print("The menu you entered does not exist.")
    this_price = price_coffees.get(m)
    total_price += this_price

    if m in order_coffees:
        order_drink[m] = order_coffees.get(m) + 1
    else:
        order_coffees[m] = 1
    print_order()
    print_price()


def print_order():
    global order_meal, order_drink, order_coffees

    tmp = ""
    price_tmp = 0
    for i in order_meal:
        price_tmp = price_meal[i] * order_meal.get(i)
        tmp = tmp + i + " X " + str(order_meal.get(i)) +  " = " + str(price_tmp)+"\n"
    for i in order_drink:
        price_tmp = price_drink[i] * order_drink.get(i)
        tmp = tmp + i + " X " + str(order_drink.get(i)) +  " = " + str(price_tmp)+"\n"
    for i in order_coffees:
        price_tmp = price_coffees[i] * order_coffees.get(i)
        tmp = tmp + i + " X " + str(order_coffees.get(i)) +  " = " + str(price_tmp)+"\n"

    text_1.delete('1.0', tk.END)
    text_1.insert(tk.INSERT, tmp)


def order_end():
    global total_price, order_meal, order_drink, order_coffees
    total_price = 0
    del order_meal
    del order_drink
    del order_coffees

    order_meal = {}
    order_drink = {}
    order_coffees = {}
    print_price()
    print_order()
    show_meal()


def print_price():
    global total_price
    label_price.configure(text=str(total_price))


window = tk.Tk()
window.title("Brunch Cafe")
window.geometry("800x400+500+300")
window.resizable(False, False)

frame1 = tk.Frame(window, width="800", height="10")
frame1.pack(fill="both")

frame2 = tk.Frame(window, width="800")
frame2.pack(fill="both", expand=True)

frame3 = tk.Frame(window, width="800")
frame3.pack(fill="both", expand=True)

frame4 = tk.Frame(window, width="800")
# frame4.pack(fill="both", expand=True)

frame5 = tk.Frame(window, width="800", height="10")
frame5.pack(fill="both", expand=True)

btn_meal = tk.Button(frame1, text="meal", padx="10", pady="10", bg="yellow", command=show_meal)
btn_meal.grid(row=0, column=0, padx=10, pady=10)

btn_drink = tk.Button(frame1, text="drinks", padx="10", pady="10", bg="white", command=show_drink)
btn_drink.grid(row=0, column=1, padx=10, pady=10)

btn_end = tk.Button(frame1, text="Order Completed", padx="10", pady="10", command=order_end)
btn_end.grid(row=0, column=2, padx=10, pady=10)

label_price = tk.Label(frame1, text="0 dollars", width="20", padx=10, pady="10", fg="blue", font='Arial 15')
label_price.grid(row=0, column="3", padx="10", pady="10")

# Meal
btn_meal_1 = tk.Button(frame2, text="Donut\n($1.25)", padx="10", pady="10", width="10", command=lambda: meal_add('Donut'))
btn_meal_1.grid(row=0, column=0, padx=10, pady=10)

btn_meal_2 = tk.Button(frame2, text="Filled Donut\n($1.50)", padx="10", pady="10", width="10", command=lambda: meal_add('Filled Donut'))
btn_meal_2.grid(row=0, column=1, padx=10, pady=10)

btn_meal_3 = tk.Button(frame2, text="Cinnamon roll\n($2.95)", padx="10", pady="10", width="10", command=lambda: meal_add('Cinnamon roll'))
btn_meal_3.grid(row=0, column=2, padx=10, pady=10)

btn_meal_4 = tk.Button(frame2, text="Muffin\n($2.95)", padx="10", pady="10", width="10", command=lambda: meal_add('Muffin'))
btn_meal_4.grid(row=0, column=3, padx=10, pady=10)

btn_meal_5 = tk.Button(frame2, text="Scone\n($2.95)", padx="10", pady="10", width="10", command=lambda: meal_add('Scone'))
btn_meal_5.grid(row=0, column=4, padx=10, pady=10)


# Coffees
btn_drink_1 = tk.Button(frame3, text="Iced Coffee\n($2.65)", padx="10", pady="10", width="10", command=lambda: drink_add('Iced Coffee'))
btn_drink_1.grid(row=0, column=0, padx=10, pady=10)

btn_drink_2 = tk.Button(frame3, text="Cold Brew\n($3.95)", padx="10", pady="10", width="10", command=lambda: drink_add('Cold Brew'))
btn_drink_2.grid(row=0, column=1, padx=10, pady=10)

btn_drink_3 = tk.Button(frame3, text="Iced Americano\n($2.45)", padx="10", pady="10", width="10", command=lambda: drink_add('Iced Americano'))
btn_drink_3.grid(row=0, column=2, padx=10, pady=10)

btn_drink_4 = tk.Button(frame3, text="Mochatella\n($4.25)", padx="10", pady="10", width="10", command=lambda: drink_add('Mochatella'))
btn_drink_4.grid(row=0, column=3, padx=10, pady=10)

btn_drink_6 = tk.Button(frame3, text="Iced Mochatella\n($4.75)", padx="10", pady="10", width="10", command=lambda: drink_add('Iced Mochatella'))
btn_drink_6.grid(row=0, column=4, padx=10, pady=10)

btn_drink_7 = tk.Button(frame3, text="Cafe Caramel/Mocha\n($4.70)", padx="10", pady="10", width="10", command=lambda: drink_add('Cafe Caramel/Mocha'))
btn_drink_7.grid(row=0, column=4, padx=10, pady=10)


# Drinks
btn_drink_1 = tk.Button(frame4, text="Hot Tea\n($1.95)", padx="10", pady="10", width="10", command=lambda: drink_add('Hot Tea'))
btn_drink_1.grid(row=0, column=0, padx=10, pady=10)

btn_drink_2 = tk.Button(frame4, text="Hot Chocolate\n($1.75)", padx="10", pady="10", width="10", command=lambda: drink_add('Hot Chocolate'))
btn_drink_2.grid(row=0, column=1, padx=10, pady=10)

btn_drink_3 = tk.Button(frame4, text="Milk\n($1.50)", padx="10", pady="10", width="10", command=lambda: drink_add('Milk'))
btn_drink_3.grid(row=0, column=2, padx=10, pady=10)

btn_drink_4 = tk.Button(frame4, text="Soda\n($2.25)", padx="10", pady="10", width="10", command=lambda: drink_add('Soda'))
btn_drink_4.grid(row=0, column=3, padx=10, pady=10)


# Order list
text_1 = tk.Text(frame5, height="10")
text_1.pack()

window.mainloop()
10 Rep
  • 2,217
  • 7
  • 19
  • 33

2 Answers2

1

The error is telling you that this_price is a None. Why would that be so?

Well, if we check out the documentation for the .get method:

None if the key is not found and value is not specified.

In your code, you are not passing a value to the .get() method. The parameters are [key, value], and you did not pass value.

So, switch out the this_price = price_meal.get(m) with this_price = price_meal[str(m)], and you will get the value, which is exactly what you want.

Instead of using .get, you would be using slice notation. Python will look through the dictionary for m, and when it finds m, it will return the value for m in the dictionary, which is the price.

Your entire code would look something like this:

price_meal = {"Donut": 1.25, "Filled Donut": 1.50, "Apple Fritter": 2.95, "Cinnamon roll": 2.95, "Muffin": 2.95, "Scone": 2.95}
price_drink = {"Hot Tea": 1.95, "Hot Chocolate": 1.75, "Milk": 1.50, "Chocolate Milk": 1.95, "Soda": 2.25}
price_coffees = {"Iced Coffee": 2.65, "Cold Brew": 3.95, "Iced Americano" :2.45, "Mochatella":4.25, "Iced Mochatella": 4.75, "Cafe Caramel/Mocha": 4.70}

order_meal = {}
order_drink = {}
order_coffees = {}

total_price = 0


def show_meal():
    btn_meal.configure(bg="yellow")
    btn_drink.configure(bg="white")
    frame4.pack_forget()
    frame3.pack_forget()
    frame2.pack(fill="both", expand=True)
    frame4.pack(fill="both", expand=True)

def show_drink():
    btn_meal.configure(bg="white")
    btn_drink.configure(bg="yellow")
    frame4.pack_forget()
    frame2.pack_forget()
    frame3.pack(fill="both", expand=True)
    frame4.pack(fill="both", expand=True)

def show_coffees():
    btn_meal.configure(bg="white")
    btn_drink.configure(bg="yellow")
    frame4.pack_forget()
    frame2.pack_forget()
    frame3.pack(fill="both", expand=True)
    frame4.pack(fill="both", expand=True)


def meal_add(m):
    global price_meal, order_meal, total_price
    if m not in price_meal:
        print("The menu you entered does not exist.")
    this_price = price_meal[m]
    total_price += this_price

    if m in order_meal:
        order_meal[m] = order_meal[m] + 1
    else:
        order_meal[m] = 1
    print_order()
    print_price()


def drink_add(m):
    global price_drink, order_drink, total_price
    if m not in price_drink:
        print("The menu you entered does not exist.")
    this_price = price_drink[m]
    total_price += this_price

    if m in order_drink:
        order_drink[m] = order_drink[m] + 1
    else:
        order_drink[m] = 1
    print_order()
    print_price()


def coffees_add(m):
    global price_coffees, order_coffees, total_price
    if m not in price_coffees:
        print("The menu you entered does not exist.")
    this_price = price_coffees[m]
    total_price += this_price

    if m in order_coffees:
        order_drink[m] = order_coffees[m] + 1
    else:
        order_coffees[m] = 1
    print_order()
    print_price()


def print_order():
    global order_meal, order_drink, order_coffees

    tmp = ""
    price_tmp = 0
    for i in order_meal:
        price_tmp = price_meal[i] * order_meal[i]
        tmp = tmp + i + " X " + str(order_meal[i]) +  " = " + str(price_tmp)+"\n"
    for i in order_drink:
        price_tmp = price_drink[i] * order_drink[i]
        tmp = tmp + i + " X " + str(order_drink[i]) +  " = " + str(price_tmp)+"\n"
    for i in order_coffees:
        price_tmp = price_coffees[i] * order_coffees[i]
        tmp = tmp + i + " X " + str(order_coffees[i]) +  " = " + str(price_tmp)+"\n"

    text_1.delete('1.0', tk.END)
    text_1.insert(tk.INSERT, tmp)


def order_end():
    global total_price, order_meal, order_drink, order_coffees
    total_price = 0
    del order_meal
    del order_drink
    del order_coffees

    order_meal = {}
    order_drink = {}
    order_coffees = {}
    print_price()
    print_order()
    show_meal()


def print_price():
    global total_price
    label_price.configure(text=str(total_price))

import tkinter as tk
window = tk.Tk()
window.title("Brunch Cafe")
window.geometry("800x400+500+300")
window.resizable(False, False)

frame1 = tk.Frame(window, width="800", height="10")
frame1.pack(fill="both")

frame2 = tk.Frame(window, width="800")
frame2.pack(fill="both", expand=True)

frame3 = tk.Frame(window, width="800")
frame3.pack(fill="both", expand=True)

frame4 = tk.Frame(window, width="800")
# frame4.pack(fill="both", expand=True)

frame5 = tk.Frame(window, width="800", height="10")
frame5.pack(fill="both", expand=True)

btn_meal = tk.Button(frame1, text="meal", padx="10", pady="10", bg="yellow", command=show_meal)
btn_meal.grid(row=0, column=0, padx=10, pady=10)

btn_drink = tk.Button(frame1, text="drinks", padx="10", pady="10", bg="white", command=show_drink)
btn_drink.grid(row=0, column=1, padx=10, pady=10)

btn_end = tk.Button(frame1, text="Order Completed", padx="10", pady="10", command=order_end)
btn_end.grid(row=0, column=2, padx=10, pady=10)

label_price = tk.Label(frame1, text="0 dollars", width="20", padx=10, pady="10", fg="blue", font='Arial 15')
label_price.grid(row=0, column="3", padx="10", pady="10")

# Meal
btn_meal_1 = tk.Button(frame2, text="Donut\n($1.25)", padx="10", pady="10", width="10", command=lambda: meal_add('Donut'))
btn_meal_1.grid(row=0, column=0, padx=10, pady=10)

btn_meal_2 = tk.Button(frame2, text="Filled Donut\n($1.50)", padx="10", pady="10", width="10", command=lambda: meal_add('Filled Donut'))
btn_meal_2.grid(row=0, column=1, padx=10, pady=10)

btn_meal_3 = tk.Button(frame2, text="Cinnamon roll\n($2.95)", padx="10", pady="10", width="10", command=lambda: meal_add('Cinnamon roll'))
btn_meal_3.grid(row=0, column=2, padx=10, pady=10)

btn_meal_4 = tk.Button(frame2, text="Muffin\n($2.95)", padx="10", pady="10", width="10", command=lambda: meal_add('Muffin'))
btn_meal_4.grid(row=0, column=3, padx=10, pady=10)

btn_meal_5 = tk.Button(frame2, text="Scone\n($2.95)", padx="10", pady="10", width="10", command=lambda: meal_add('Scone'))
btn_meal_5.grid(row=0, column=4, padx=10, pady=10)


# Coffees
btn_drink_1 = tk.Button(frame3, text="Iced Coffee\n($2.65)", padx="10", pady="10", width="10", command=lambda: drink_add('Iced Coffee'))
btn_drink_1.grid(row=0, column=0, padx=10, pady=10)

btn_drink_2 = tk.Button(frame3, text="Cold Brew\n($3.95)", padx="10", pady="10", width="10", command=lambda: drink_add('Cold Brew'))
btn_drink_2.grid(row=0, column=1, padx=10, pady=10)

btn_drink_3 = tk.Button(frame3, text="Iced Americano\n($2.45)", padx="10", pady="10", width="10", command=lambda: drink_add('Iced Americano'))
btn_drink_3.grid(row=0, column=2, padx=10, pady=10)

btn_drink_4 = tk.Button(frame3, text="Mochatella\n($4.25)", padx="10", pady="10", width="10", command=lambda: drink_add('Mochatella'))
btn_drink_4.grid(row=0, column=3, padx=10, pady=10)

btn_drink_6 = tk.Button(frame3, text="Iced Mochatella\n($4.75)", padx="10", pady="10", width="10", command=lambda: drink_add('Iced Mochatella'))
btn_drink_6.grid(row=0, column=4, padx=10, pady=10)

btn_drink_7 = tk.Button(frame3, text="Cafe Caramel/Mocha\n($4.70)", padx="10", pady="10", width="10", command=lambda: drink_add('Cafe Caramel/Mocha'))
btn_drink_7.grid(row=0, column=4, padx=10, pady=10)


# Drinks
btn_drink_1 = tk.Button(frame4, text="Hot Tea\n($1.95)", padx="10", pady="10", width="10", command=lambda: drink_add('Hot Tea'))
btn_drink_1.grid(row=0, column=0, padx=10, pady=10)

btn_drink_2 = tk.Button(frame4, text="Hot Chocolate\n($1.75)", padx="10", pady="10", width="10", command=lambda: drink_add('Hot Chocolate'))
btn_drink_2.grid(row=0, column=1, padx=10, pady=10)

btn_drink_3 = tk.Button(frame4, text="Milk\n($1.50)", padx="10", pady="10", width="10", command=lambda: drink_add('Milk'))
btn_drink_3.grid(row=0, column=2, padx=10, pady=10)

btn_drink_4 = tk.Button(frame4, text="Soda\n($2.25)", padx="10", pady="10", width="10", command=lambda: drink_add('Soda'))
btn_drink_4.grid(row=0, column=3, padx=10, pady=10)


# Order list
text_1 = tk.Text(frame5, height="10")
text_1.pack()

window.mainloop()

Note that I essentially replaced every instance of dictionary.get(m) with dictionary[m].

10 Rep
  • 2,217
  • 7
  • 19
  • 33
  • The menu you entered does not exist. Exception in Tkinter callback Traceback (most recent call last): File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/tkinter/__init__.py", line 1883, in __call__ return self.func(*args) File "/Use/PycharmProjects/APCSP/APCSP.py", line 178, in btn_drink_3 = tk.Button(frame3, text="Iced Americano\n($2.45)", padx="10", pady="10", width="10", command=lambda: drink_add('Iced Americano')) File "/Users/PycharmProjects/APCSP/APCSP.py", line 57, in drink_add this_price = price_drink[m] KeyError: 'Iced Americano' – Mason Mount May 18 '21 at 04:30
  • Thank you so much. I did what you told me and these errors came up. What should I do? – Mason Mount May 18 '21 at 04:31
  • @MasonMount the reason that is app[earing is because "Iced Americano" Is not in your dictionary. You may want to add it. You may have meant to type "Iced American". – 10 Rep May 18 '21 at 05:20
  • @MasonMount is there a reason you unaccepted this answer? – 10 Rep May 20 '21 at 18:38
0

It is because you have used drink_add() for all coffee items. Use coffees_add() instead:

# Coffees
btn_drink_1 = tk.Button(frame3, text="Iced Coffee\n($2.65)", padx="10", pady="10", width="10", command=lambda: coffees_add('Iced Coffee'))
btn_drink_1.grid(row=0, column=0, padx=10, pady=10)

btn_drink_2 = tk.Button(frame3, text="Cold Brew\n($3.95)", padx="10", pady="10", width="10", command=lambda: coffees_add('Cold Brew'))
btn_drink_2.grid(row=0, column=1, padx=10, pady=10)

btn_drink_3 = tk.Button(frame3, text="Iced Americano\n($2.45)", padx="10", pady="10", width="10", command=lambda: coffees_add('Iced Americano'))
btn_drink_3.grid(row=0, column=2, padx=10, pady=10)

btn_drink_4 = tk.Button(frame3, text="Mochatella\n($4.25)", padx="10", pady="10", width="10", command=lambda: coffees_add('Mochatella'))
btn_drink_4.grid(row=0, column=3, padx=10, pady=10)

btn_drink_6 = tk.Button(frame3, text="Iced Mochatella\n($4.75)", padx="10", pady="10", width="10", command=lambda: coffees_add('Iced Mochatella'))
btn_drink_6.grid(row=0, column=4, padx=10, pady=10)

btn_drink_7 = tk.Button(frame3, text="Cafe Caramel\n/Mocha\n($4.70)", padx="10", pady="10", width="10", command=lambda: coffees_add('Cafe Caramel/Mocha'))
btn_drink_7.grid(row=0, column=5, padx=10, pady=10)

Note that I have also changed column=4 to column=5 for btn_drink_7. Also you have used same set of variable names for coffees and drinks.


There is typo in the following block inside coffees_add():

if m in order_coffees:
    order_drink[m] = order_coffees.get(m) + 1   # <- order_coffees[m] should be used instead
else:
    order_coffees[m] = 1
acw1668
  • 40,144
  • 5
  • 22
  • 34
  • Thank you so much! How can I change the variable of coffee and drinks? – Mason Mount May 18 '21 at 04:42
  • You can change those `btn_drink_x` for coffee items to `btn_coffee_x`. – acw1668 May 18 '21 at 04:45
  • That doesn't seem to be the problem. The problem seems to be with the `.get()` function, not with the `btn` variables at all. – 10 Rep May 18 '21 at 05:21
  • @10Rep Did you really read my description? The cause is using `drink_add()` instead of `coffees_add()` for coffee items so the selected coffee item cannot be found in `price_drink` dictionary. – acw1668 May 18 '21 at 05:25
  • Yes, but that doesn't seem to be what's actually causing the error. The error is caused by using the .get() method for dictionaries. It returns a None value if you don't put a value as a parameter, which is what the OP did. Am I missing something here? – 10 Rep May 18 '21 at 05:28
  • @10Rep Please read carefully on OP code. Which line in OP code has called `get()` without argument? – acw1668 May 18 '21 at 05:30
  • `get()` required two arguments, the `key`, and the `value`. So if you do `dictionary.get(m)`, you will get None because it requires two parameters. The OP's code has this line: `this_price = price_coffees.get(m)`. That is the culprit. – 10 Rep May 18 '21 at 05:32
  • @10Rep Where do you learn that `.get()` on dictionary requires exactly two argument? The second argument is optional. – acw1668 May 18 '21 at 05:33
  • https://www.programiz.com/python-programming/methods/dictionary/get says that `.get()` will return None if the key is not found, or if the value is not specified. – 10 Rep May 18 '21 at 05:34
  • @10Rep As I said, the second argument is *optional*. It is returned if the key cannot be found in the dictionary. Also I have said that OP used `drink_add()` for coffee items, so it cannot find the coffee item in `price_drink`. If OP changes to `coffees_add()`, then such error is fixed. – acw1668 May 18 '21 at 05:38
  • @MasonMount As I notice that you have used my suggestion in your new [question](https://stackoverflow.com/questions/67595285/python-vending-machine-making-new-page), so accept my answer if it solves this question. – acw1668 May 19 '21 at 01:10