Python 自动售货机错误(未定义订购咖啡)
Python vending machine error (order coffees is not defined)
我想对食物进行分类,设置类别,选择食物。我希望选择食物后总价上涨。订购咖啡部分对我来说是个问题。咖啡菜单与其他类别重叠。另外,如果我按下订单完成按钮,我想要一张带有感谢信息的收据。我该怎么办?
我的错误信息是
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(.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(.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(.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(.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(.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(.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(.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(.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(.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(.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(.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(.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(.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(.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(.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(.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()
错误告诉您 this_price
是 None
。为什么会这样?
好吧,如果我们查看 the documentation for the .get
方法:
None
if the key is not found and value is not specified.
在您的代码中,您没有将值传递给 .get()
方法。参数是[key, value]
,你没有传值
所以,把this_price = price_meal.get(m)
换成this_price = price_meal[str(m)]
,你就会得到你想要的值。
您将使用 slice notation
而不是 .get
。 Python 会在字典中查找 m
,当它找到 m
时,它会 return 字典中 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(.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(.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(.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(.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(.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(.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(.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(.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(.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(.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(.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(.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(.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(.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(.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.get(m)
的每个实例都替换为 dictionary[m]
。
这是因为您对所有咖啡项目都使用了drink_add()
。使用 coffees_add()
代替:
# Coffees
btn_drink_1 = tk.Button(frame3, text="Iced Coffee\n(.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(.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(.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(.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(.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(.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)
请注意,对于 btn_drink_7
,我也将 column=4
更改为 column=5
。您还为 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
我想对食物进行分类,设置类别,选择食物。我希望选择食物后总价上涨。订购咖啡部分对我来说是个问题。咖啡菜单与其他类别重叠。另外,如果我按下订单完成按钮,我想要一张带有感谢信息的收据。我该怎么办?
我的错误信息是
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(.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(.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(.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(.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(.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(.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(.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(.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(.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(.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(.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(.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(.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(.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(.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(.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()
错误告诉您 this_price
是 None
。为什么会这样?
好吧,如果我们查看 the documentation for the .get
方法:
None
if the key is not found and value is not specified.
在您的代码中,您没有将值传递给 .get()
方法。参数是[key, value]
,你没有传值
所以,把this_price = price_meal.get(m)
换成this_price = price_meal[str(m)]
,你就会得到你想要的值。
您将使用 slice notation
而不是 .get
。 Python 会在字典中查找 m
,当它找到 m
时,它会 return 字典中 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(.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(.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(.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(.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(.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(.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(.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(.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(.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(.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(.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(.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(.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(.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(.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.get(m)
的每个实例都替换为 dictionary[m]
。
这是因为您对所有咖啡项目都使用了drink_add()
。使用 coffees_add()
代替:
# Coffees
btn_drink_1 = tk.Button(frame3, text="Iced Coffee\n(.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(.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(.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(.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(.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(.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)
请注意,对于 btn_drink_7
,我也将 column=4
更改为 column=5
。您还为 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