提问人:Kevin L 提问时间:10/26/2022 更新时间:10/26/2022 访问量:441
我需要帮助修改订餐系统。使用列表获取具有数量的订单项键。这是针对 Python 的
I need help in modifying a food ordering system. Using a list to take order item key with quantity. This is for Python
问:
下面的代码用于生成显示排序系统的输出。我需要帮助创建一个列表来获取订单项目键和数量。 因此,所采取的订单的示例列表格式: [[1,2], [3,1]] 表示商品巨无霸 (id 1) 订购数量 2,商品素食汉堡 (id 3) 订购数量 1。 基本上,我需要在循环开始之前创建一个空列表,然后每次输入带有 id 和 quantity 的订单时通过附加列表来添加一个嵌套列表。但我不知道如何以及从哪里开始。谁能帮忙?
def processOrder(quantity, item_list):
global total
if quantity > item_list[2]:
print("There is not enough stock!")
pass
else:
total += item_list[1] * quantity
item_list[2] -= quantity
total = 0
A = ["Big Mac", float(2.50), 50], ["Large Fries", float(0.50), 200], ["Vegetarian Burger", float(1.00), 20]
print("Welcome to McDonald's")
print("[1]", A[0][0:2],
"\n[2]", A[1][0:2],
"\n[3]", A[2][0:2])
more_items = 'yes'
while more_items == 'yes':
choice, quantity = int((input("\nWhat would you like?\n"))), int(input("\nHow many would you like?\n"))
if 1 <= choice <= 3:
processOrder(quantity, A[choice-1])
more_items = (input("Do you want to order more items?")).lower()
print("Thank you for ordering!\nYour total cost is: $" + str(total))
我尝试创建一个新函数来获取数量如下的订单项目键:
# Function to get order item key with quantity
def get_order_item_with_quantity(items, quantity):
empty_list = []
for items in A:
empty_list.append([A])
但我真的不知道从这里开始该去哪里,或者这是否是正确的方法。
答:
0赞
Алексей Р
10/26/2022
#1
我认为在这种情况下没有必要编写函数,一个简单的就足够了。我在您的代码中添加了几行 - 请参阅注释append()
#added
def processOrder(quantity, item_list):
global total
if quantity > item_list[2]:
print("There is not enough stock!")
pass
else:
total += item_list[1] * quantity
item_list[2] -= quantity
total = 0
A = ["Big Mac", float(2.50), 50], ["Large Fries", float(0.50), 200], ["Vegetarian Burger", float(1.00), 20]
print("Welcome to McDonald's")
print("[1]", A[0][0:2],
"\n[2]", A[1][0:2],
"\n[3]", A[2][0:2])
order = [] # added - initail empty order
more_items = 'yes'
while more_items == 'yes':
choice, quantity = int((input("\nWhat would you like?\n"))), int(input("\nHow many would you like?\n"))
if 1 <= choice <= 3:
processOrder(quantity, A[choice - 1])
order.append([choice-1, quantity]) # added - append the order with the current position
more_items = (input("Do you want to order more items?")).lower()
print("Thank you for ordering!\nYour total cost is: $" + str(total))
#added - print out the order list
print('Order list:')
for el in order:
print(f'Position: {A[el[0]][0]}, quantity: {el[1]}, cost: {A[el[0]][1]*el[1]}')
Welcome to McDonald's
[1] ['Big Mac', 2.5]
[2] ['Large Fries', 0.5]
[3] ['Vegetarian Burger', 1.0]
What would you like?
1
How many would you like?
2
Do you want to order more items?yes
What would you like?
3
How many would you like?
1
Do you want to order more items?
Thank you for ordering!
Your total cost is: $6.0
Order list:
Position: Big Mac, quantity: 2, cost: 5.0
Position: Vegetarian Burger, quantity: 1, cost: 1.0
评论
0赞
Kevin L
10/27/2022
谢谢,我没想到会这么简单。感谢它!
评论