【问题标题】:Python vending machine error (order coffees is not defined) [closed]Python自动售货机错误(未定义订购咖啡)[关闭]
【发布时间】:2021-05-18 03:35:53
【问题描述】:

我想按类别对食物进行分类,设置类别,然后选择食物。我希望选择食物后总价格上涨。订购咖啡部分对我来说是个问题。咖啡菜单与其他类别重叠。另外,如果我按下订单完成按钮,我想要一张带有感谢信息的收据。我该怎么办?

我的错误信息是

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'

这是我的代码

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()

【问题讨论】:

  • 你让我们猜测错误发生在哪里。请更新问题以包含完整的错误回溯消息。
  • 您已为所有咖啡致电drink_add()。应该改用coffees_add()
  • 这告诉你this_priceNone

标签: python list tkinter tk


【解决方案1】:

错误告诉您this_priceNone。为什么会这样?

好吧,如果我们检查 the documentation.get 方法:

None 如果没有找到键并且没有指定值。

在您的代码中,您没有将值传递给.get() 方法。参数是[key, value],你没有传值。

所以,把this_price = price_meal.get(m) 换成this_price = price_meal[str(m)],你就会得到这个值,这正是你想要的。

您将使用slice notation,而不是使用.get。 Python会在字典中查找m,当找到m时,会在字典中返回m的值,即价格。

您的整个代码将如下所示:

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()

请注意,我基本上用dictionary[m] 替换了dictionary.get(m) 的每个实例。

【讨论】:

  • 您输入的菜单不存在。 Tkinter 回调 Traceback 中的异常(最近一次调用最后一次):文件“/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/tkinter/__init__.py”,第 1883 行,在 call return self.func(*args) 文件“/Use/PycharmProjects/APCSP/APCSP.py”,第 178 行,在 btn_drink_3 = tk.Button(frame3, text="Iced Americano\n($2.45)" , padx="10", pady="10", width="10", command=lambda:drink_add('Iced Americano')) 文件“/Users/PycharmProjects/APCSP/APCSP.py”,第 57 行,在 drink_add this_price = price_drink[m] KeyError: 'Iced Americano'
  • 非常感谢。我做了你告诉我的,这些错误出现了。我该怎么办?
  • @MasonMount app[earing 的原因是因为“Iced Americano”不在您的字典中。您可能想要添加它。您可能想输入“Iced American”。
  • @MasonMount 你不接受这个答案有什么原因吗?
【解决方案2】:

这是因为您对所有咖啡项目都使用了drink_add()。请改用coffees_add()

# 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)

请注意,我还将column=4 更改为column=5btn_drink_7。您还为coffeesdrinks 使用了相同的变量名称集。


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

【讨论】:

  • 非常感谢!如何更改咖啡和饮料的变量?
  • 您可以将咖啡项目的 btn_drink_x 更改为 btn_coffee_x
  • 这似乎不是问题。问题似乎出在.get() 函数上,而不是btn 变量。
  • @10Rep 你真的看过我的描述吗?原因是使用drink_add() 而不是coffees_add() 用于咖啡项目,因此在price_drink 字典中找不到所选的咖啡项目。
  • 是的,但这似乎并不是真正导致错误的原因。该错误是由对字典使用 .get() 方法引起的。如果您不将值作为参数,它将返回 None 值,这就是 OP 所做的。我在这里错过了什么吗?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2019-03-25
  • 2017-03-24
  • 2014-06-17
  • 1970-01-01
  • 1970-01-01
  • 2017-05-01
  • 1970-01-01
相关资源
最近更新 更多