英文:
I want to create a running subtotal based on the quantity added of different food items. What am I missing?
问题
print("欢迎!这是今天的菜单。")
print("")
print("- 咖啡($2)")
print("- 汉堡($4)")
print("- 果汁($3)")
print("- 松饼($2)")
print("- 卷饼($5)")
print("")
def student_order():
order = input("您今天想点什么?:")
print("")
if (order == '咖啡'
or order == '汉堡'
or order == '果汁'
or order == '松饼'
or order == '卷饼'):
order_quantity = input("数量:")
print("")
print("感谢您的订购!您订购了" + order_quantity + "份" + order + "。")
coffee_count = 0
burger_count = 0
juice_count = 0
muffin_count = 0
burrito_count = 0
order_subtotal = 0
order_more = input("还需要点其他菜吗?(是或否):")
while (order_more != '否' or order_more != 'no' or order_more != 'No'):
if (order_more == '是' or order_more == 'yes' or order_more == 'YES'):
student_order()
if order == ('咖啡'):
coffee_count += int(order_quantity)
elif order == ('汉堡'):
burger_count += int(order_quantity)
elif order == ('果汁'):
juice_count += int(order_quantity)
elif order == ('松饼'):
muffin_count += int(order_quantity)
elif order == ('卷饼'):
burrito_count += int(order_quantity)
else:
order_subtotal = (coffee_count * 2.00) + (burger_count * 4.00) + (juice_count * 3.00) + (muffin_count * 2.00) + (burrito_count * 5.00)
print("")
print("感谢您的订购!")
print("")
print("您的订单小计为:$", order_subtotal)
print("")
order_tax = order_subtotal * 0.13
print("您的税费为:$", order_tax)
print("")
print("您的总计为:$", (order_subtotal + order_tax))
print("")
print("请到柜台取餐。祝您有愉快的一天!")
break
else:
print("抱歉," + order + "不在今天的菜单上。请重新选择其他项目。")
student_order()
student_order()
尝试将食物项目的计数初始化为零,但似乎无法正确累加。我该如何修复这个问题?例如,我先点了1份果汁,然后再点了2份松饼。小计应该是$7。现在,无论您输入什么数量,它都只显示$0。也许代码的顺序有问题,但这只是我的猜测。
英文:
print("Welcome! Here is today's menu.")
print("")
print("- Coffee ($2)")
print("- Burger ($4)")
print("- Juice ($3)")
print("- Muffin ($2)")
print("- Burrito ($5)")
print("")
def student_order() :
order = input("What would you like to order today?: ")
print("")
if (order == 'Coffee'
or order == 'Burger'
or order == 'Juice'
or order == 'Muffin'
or order == 'Burrito'):
order_quantity = input("Quantity: ")
print("")
print("Thank you for your order! You ordered " + order_quantity + " " + order + ".")
coffee_count = 0
burger_count = 0
juice_count = 0
muffin_count = 0
burrito_count = 0
order_subtotal = 0
order_more = input("Anything else? (Yes or No): ")
while (order_more != 'NO' or order_more != 'no' or order_more != 'No'):
if (order_more == 'Yes' or order_more == 'yes' or order_more == 'YES'):
student_order()
if order == ('Coffee'):
coffee_count += order_quantity
elif order == ('Burger'):
burger_count += order_quantity
elif order == ('Juice'):
juice_count += order_quantity
elif order == ('Muffin'):
muffin_count += order_quantity
elif order == ('Burrito'):
burrito_count += order_quantity
else:
order_subtotal = (coffee_count * 2.00) + (burger_count * 4.00) + (juice_count * 3.00) + (muffin_count * 2.00) + (burrito_count * 5.00)
print("")
print("Thank you for your order!")
print("")
print("Your order subtotal is: $", order_subtotal)
print("")
order_tax = order_subtotal * 0.13
print("Your tax is: $", order_tax)
print("")
print("Your total is: $", (order_subtotal + order_tax))
print("")
print("Please pick up your order at the counter. Have a good day!")
break
else:
print("Sorry, " + order + " is not on today's menu. Please choose another item.")
student_order()
student_order()
Tried initializing food item counts to zero, but doesn't seem to add up correctly. How do I fix this? For example, I add 1 Juice, then add 2 Muffins. The Subtotal should be $7. Right now, it only shows $0 regardless of what quantity you put in. Maybe the order of the code is wrong, but that is just my guess.
答案1
得分: 1
根据评论所述,在这里使用循环进行连续订购比尝试在使用递归时维护订单状态更好。我已经写了一个经过整理的代码版本,并添加了注释,指出了一些对于学习有用的Python/编程概念。特别是,使用字典可以节省一些代码行数。
def student_order():
# 使用字典存储价格和数量
menu = {
"coffee": [2, 0],
"burger": [4, 0],
"juice": [3, 0],
"muffin": [2, 0],
"burrito": [5, 0],
}
print("欢迎光临!这是今天的菜单。\n")
# 使用字符串格式化或 f-strings 进行更简单的字符串拼接
for item in menu:
print(f"- {item} (${menu[item][0]})")
order_more = "yes"
# 使用 string.lower() 或 string.upper() 来不用担心大小写问题
while order_more.lower() != "no":
order = input("今天您想点什么?:")
print("")
if order.lower() in menu:
order_quantity = input("数量:")
print(f"\n感谢您的订购!您订购了 {order_quantity} {order}")
menu[order][1] += int(order_quantity)
order_more = input("还需要其他的吗?(是或否):")
else:
print(f"抱歉,{order} 不在今天的菜单上。请重新选择其他项目。")
# 使用列表推导式获取明细账单
subtotals = [menu[item][0] * menu[item][1] for item in menu]
# 对列表求和以获取小计
order_subtotal = sum(subtotals)
order_tax = 0.13 * order_subtotal
print("\n感谢您的订购!")
print(f"\n您的订单小计为:${order_subtotal}")
print(f"\n您的税费为:${order_tax}")
print(f"\n您的总计为:${order_subtotal + order_tax}")
print("\n请到柜台取走您的订单。祝您有美好的一天!")
英文:
As stated by the comments, using a loop here for continual ordering is better than trying to maintain the order state while using recursion. I've written a cleaned version of your code with comments to point to some python/coding concepts that are useful to pickup. Particularly, using a dictionary will save you some lines.
def student_order():
# Use dictionary to store prices and quantities
menu = {
"coffee": [2, 0],
"burger": [4, 0],
"juice": [3, 0],
"muffin": [2, 0],
"burrito": [5, 0],
}
print("Welcome! Here is today's menu.\n")
# Use string formatting or f-strings for easier string concatenation
for item in menu:
print(f"- {item} (${menu[item][0]})")
order_more = "yes"
# Use string.lower() or string.upper() to not worry about cases
while order_more.lower() != "no":
order = input("What would you like to order today?: ")
print("")
if order.lower() in menu:
order_quantity = input("Quantity: ")
print(f"\nThank you for your order! You ordered {order_quantity} {order}")
menu[order][1] += int(order_quantity)
order_more = input("Anything else? (Yes or No): ")
else:
print(f"Sorry, {order} is not on today's menu. Please choose another item.")
# List comprehension to get an itemized bill
subtotals = [menu[item][0] * menu[item][1] for item in menu]
# Sum list to get subtotal
order_subtotal = sum(subtotals)
order_tax = 0.13 * order_subtotal
print("\nThank you for your order!")
print(f"\nYour order subtotal is: ${order_subtotal}")
print(f"\nYour tax is: ${order_tax}")
print(f"\nYour total is: ${order_subtotal + order_tax}")
print("\nPlease pick up your order at the counter. Have a good day!")
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论