我制作了一个程序,可以将快餐菜单的订单加起来。下订单后,我需要添加一个正在运行的小计。由于我是python新手,因此我不确定该怎么做。我还需要确保订单字典已修改,但是不确定如何修改。
我曾考虑过使用范围来循环以保持总数,但我不希望出现范围,因为我希望程序能够接受尽可能多的订单。
# menu and order options
menu = {"burger":5.00, "fries":3.50, "drink":1.00}
order = {"burger":0, "fries":0, "drink":0}
bcount = 0
fcount = 0
dcount = 0
while True:
print("Please make a selection:")
print("1. Burger = $5.00")
print("2. Fries = $3.50")
print("3. Drink = $1.00")
print("4. Quit")
choice = int(input('Please order: '))
if choice == 1:
amount = int(input("Enter number of Burgers: "))
bcount += amount
elif choice == 2:
amount = int(input("Enter number of Fries: "))
fcount += amount
elif choice == 3:
amount = int(input("Enter number of Drinks: "))
dcount += amount
elif choice == 4:
sub = (bcount * 5.00) + (fcount * 3.50) + (dcount * 1.00)
tax = sub * 0.075
total = sub + tax
print('Number of Burgers: {0}'.format(bcount))
print('Number of Fries: {0}'.format(fcount))
print('Number of Drinks: {0}'.format(dcount))
print('Subtotal: {:0.2f}'.format(sub))
print('Tax: {:0.2f}'.format(tax))
print('Total: {:0.2f}'.format(total))
break
预期结果是每次下单后,程序都会给我一个小计。
示例:输入汉堡的订单后,看起来像: 您的小计是:$ 5.00
那么下一个订单是一份薯条和一杯饮料 您的小计为:$ 9.50(从先前的订单中添加汉堡包)
答案 0 :(得分:1)
我认为,对于选项1-3,您需要显示小计,对于选项4,您需要显示完整的报告。
我将代码更新如下:
calculate_sub_total
方法。if-elif-else
分为两个分区。menu
词典在总计计算和显示菜单中获取商品价格。order
字典来跟踪预期的项目数。已删除bcount
,dcount
,fcount
变量,因为它们不再需要了。更新的代码:
# menu and order options
menu = {"burger":5.00, "fries":3.50, "drink":1.00}
order = {"burger":0, "fries":0, "drink":0}
def calculate_sub_total():
return (order["burger"] * menu["burger"]) + \
(order["fries"] * menu["fries"]) + \
(order["drink"] * menu["drink"])
while True:
print("Please make a selection:")
print("1. Burger = ${}".format(menu["burger"]))
print("2. Fries = ${}".format(menu["fries"]))
print("3. Drink = ${}".format(menu["drink"]))
print("4. Quit")
choice = int(input('Please order: '))
show_subtotal = False
if choice == 1:
amount = int(input("Enter number of Burgers: "))
order["burger"] += amount
show_subtotal = True
elif choice == 2:
amount = int(input("Enter number of Fries: "))
order["fries"] += amount
show_subtotal = True
elif choice == 3:
amount = int(input("Enter number of Drinks: "))
order["drink"] += amount
show_subtotal = True
sub = calculate_sub_total()
if show_subtotal:
print('Subtotal: {:0.2f}'.format(sub))
if choice == 4:
tax = sub * 0.075
total = sub + tax
print('Number of Burgers: {0}'.format(order["burger"]))
print('Number of Fries: {0}'.format(order["fries"]))
print('Number of Drinks: {0}'.format(order["drink"]))
print('Subtotal: {:0.2f}'.format(sub))
print('Tax: {:0.2f}'.format(tax))
print('Total: {:0.2f}'.format(total))
break
输出:
答案 1 :(得分:0)
执行此操作的一种方法是在开头称为 subtotal 的变量中添加一个名为subtotal的变量。随附汉堡的一个示例,可以将其应用于其余示例。我同意以前的评论者所说的关于变量重置而不是 break 的问题。
subtotal=0
bcount = 0
fcount = 0
dcount = 0
if choice == 1:
amount = int(input("Enter number of Burgers: "))
bcount += amount
subtotal+=(amount*5)
print("Your total is: ",subtotal)