如何解决Python自动售货机错误订单咖啡未定义
我要按类别对食物进行分类,设置类别,然后选择食物。我希望选择食物后总价上涨。订购咖啡部分对我来说是个问题。咖啡菜单与其他类别重叠。另外,如果我按下订单完成按钮,我想要一张带有感谢信息的收据。我该怎么办?
我的错误信息是
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)
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)
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_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_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,height="10")
frame5.pack(fill="both",expand=True)
btn_meal = tk.Button(frame1,text="meal",bg="yellow",command=show_meal)
btn_meal.grid(row=0,column=0,padx=10,pady=10)
btn_drink = tk.Button(frame1,text="drinks",bg="white",command=show_drink)
btn_drink.grid(row=0,column=1,pady=10)
btn_end = tk.Button(frame1,text="Order Completed",command=order_end)
btn_end.grid(row=0,column=2,pady=10)
label_price = tk.Label(frame1,text="0 dollars",width="20",fg="blue",font='Arial 15')
label_price.grid(row=0,column="3",pady="10")
# Meal
btn_meal_1 = tk.Button(frame2,text="Donut\n($1.25)",command=lambda: meal_add('Donut'))
btn_meal_1.grid(row=0,pady=10)
btn_meal_2 = tk.Button(frame2,text="Filled Donut\n($1.50)",command=lambda: meal_add('Filled Donut'))
btn_meal_2.grid(row=0,pady=10)
btn_meal_3 = tk.Button(frame2,text="Cinnamon roll\n($2.95)",command=lambda: meal_add('Cinnamon roll'))
btn_meal_3.grid(row=0,pady=10)
btn_meal_4 = tk.Button(frame2,text="Muffin\n($2.95)",command=lambda: meal_add('Muffin'))
btn_meal_4.grid(row=0,column=3,pady=10)
btn_meal_5 = tk.Button(frame2,text="Scone\n($2.95)",command=lambda: meal_add('Scone'))
btn_meal_5.grid(row=0,column=4,pady=10)
# Coffees
btn_drink_1 = tk.Button(frame3,command=lambda: drink_add('Iced Coffee'))
btn_drink_1.grid(row=0,pady=10)
btn_drink_2 = tk.Button(frame3,text="Cold Brew\n($3.95)",command=lambda: drink_add('Cold Brew'))
btn_drink_2.grid(row=0,pady=10)
btn_drink_3 = tk.Button(frame3,text="Iced Americano\n($2.45)",command=lambda: drink_add('Iced Americano'))
btn_drink_3.grid(row=0,pady=10)
btn_drink_4 = tk.Button(frame3,text="Mochatella\n($4.25)",command=lambda: drink_add('Mochatella'))
btn_drink_4.grid(row=0,pady=10)
btn_drink_6 = tk.Button(frame3,text="Iced Mochatella\n($4.75)",command=lambda: drink_add('Iced Mochatella'))
btn_drink_6.grid(row=0,pady=10)
btn_drink_7 = tk.Button(frame3,text="Cafe Caramel/Mocha\n($4.70)",command=lambda: drink_add('Cafe Caramel/Mocha'))
btn_drink_7.grid(row=0,pady=10)
# Drinks
btn_drink_1 = tk.Button(frame4,text="Hot Tea\n($1.95)",command=lambda: drink_add('Hot Tea'))
btn_drink_1.grid(row=0,pady=10)
btn_drink_2 = tk.Button(frame4,text="Hot Chocolate\n($1.75)",command=lambda: drink_add('Hot Chocolate'))
btn_drink_2.grid(row=0,pady=10)
btn_drink_3 = tk.Button(frame4,text="Milk\n($1.50)",command=lambda: drink_add('Milk'))
btn_drink_3.grid(row=0,pady=10)
btn_drink_4 = tk.Button(frame4,text="Soda\n($2.25)",command=lambda: drink_add('Soda'))
btn_drink_4.grid(row=0,pady=10)
# Order list
text_1 = tk.Text(frame5,height="10")
text_1.pack()
window.mainloop()
解决方法
错误告诉您 this_price
是 None
。为什么会这样?
好吧,如果我们检查 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)
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)
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_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_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,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",bg="white",command=show_drink)
btn_drink.grid(row=0,column=1,pady=10)
btn_end = tk.Button(frame1,text="Order Completed",command=order_end)
btn_end.grid(row=0,column=2,pady=10)
label_price = tk.Label(frame1,text="0 dollars",width="20",fg="blue",font='Arial 15')
label_price.grid(row=0,column="3",pady="10")
# Meal
btn_meal_1 = tk.Button(frame2,text="Donut\n($1.25)",width="10",command=lambda: meal_add('Donut'))
btn_meal_1.grid(row=0,pady=10)
btn_meal_2 = tk.Button(frame2,text="Filled Donut\n($1.50)",command=lambda: meal_add('Filled Donut'))
btn_meal_2.grid(row=0,pady=10)
btn_meal_3 = tk.Button(frame2,text="Cinnamon roll\n($2.95)",command=lambda: meal_add('Cinnamon roll'))
btn_meal_3.grid(row=0,pady=10)
btn_meal_4 = tk.Button(frame2,text="Muffin\n($2.95)",command=lambda: meal_add('Muffin'))
btn_meal_4.grid(row=0,column=3,pady=10)
btn_meal_5 = tk.Button(frame2,text="Scone\n($2.95)",command=lambda: meal_add('Scone'))
btn_meal_5.grid(row=0,column=4,pady=10)
# Coffees
btn_drink_1 = tk.Button(frame3,text="Iced Coffee\n($2.65)",command=lambda: drink_add('Iced Coffee'))
btn_drink_1.grid(row=0,pady=10)
btn_drink_2 = tk.Button(frame3,text="Cold Brew\n($3.95)",command=lambda: drink_add('Cold Brew'))
btn_drink_2.grid(row=0,pady=10)
btn_drink_3 = tk.Button(frame3,text="Iced Americano\n($2.45)",command=lambda: drink_add('Iced Americano'))
btn_drink_3.grid(row=0,pady=10)
btn_drink_4 = tk.Button(frame3,text="Mochatella\n($4.25)",command=lambda: drink_add('Mochatella'))
btn_drink_4.grid(row=0,pady=10)
btn_drink_6 = tk.Button(frame3,text="Iced Mochatella\n($4.75)",command=lambda: drink_add('Iced Mochatella'))
btn_drink_6.grid(row=0,pady=10)
btn_drink_7 = tk.Button(frame3,text="Cafe Caramel/Mocha\n($4.70)",command=lambda: drink_add('Cafe Caramel/Mocha'))
btn_drink_7.grid(row=0,pady=10)
# Drinks
btn_drink_1 = tk.Button(frame4,text="Hot Tea\n($1.95)",command=lambda: drink_add('Hot Tea'))
btn_drink_1.grid(row=0,pady=10)
btn_drink_2 = tk.Button(frame4,text="Hot Chocolate\n($1.75)",command=lambda: drink_add('Hot Chocolate'))
btn_drink_2.grid(row=0,pady=10)
btn_drink_3 = tk.Button(frame4,text="Milk\n($1.50)",command=lambda: drink_add('Milk'))
btn_drink_3.grid(row=0,pady=10)
btn_drink_4 = tk.Button(frame4,text="Soda\n($2.25)",command=lambda: drink_add('Soda'))
btn_drink_4.grid(row=0,pady=10)
# Order list
text_1 = tk.Text(frame5,height="10")
text_1.pack()
window.mainloop()
请注意,我基本上用 dictionary.get(m)
替换了 dictionary[m]
的每个实例。
这是因为您已将 drink_add()
用于所有咖啡项目。改用 coffees_add()
:
# Coffees
btn_drink_1 = tk.Button(frame3,command=lambda: coffees_add('Iced Coffee'))
btn_drink_1.grid(row=0,command=lambda: coffees_add('Cold Brew'))
btn_drink_2.grid(row=0,command=lambda: coffees_add('Iced Americano'))
btn_drink_3.grid(row=0,command=lambda: coffees_add('Mochatella'))
btn_drink_4.grid(row=0,command=lambda: coffees_add('Iced Mochatella'))
btn_drink_6.grid(row=0,text="Cafe Caramel\n/Mocha\n($4.70)",command=lambda: coffees_add('Cafe Caramel/Mocha'))
btn_drink_7.grid(row=0,column=5,pady=10)
请注意,我还将 column=4
的 column=5
更改为 btn_drink_7
。您还为 coffees
和 drinks
使用了相同的变量名称集。
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
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。