Python 产品工艺订单

Python 产品工艺订单,python,python-3.x,Python,Python 3.x,我想制作一个简单的流程订单,如: 你订了一份抗酸药,总共5.33美元 你填了一份3份酸咬的订单,总共6.99美元 我的代码是: total = 0 def process_order(x_list): for x in len(x_list): print(f"You filled an order for{x[1]} {x[0]} for a total of {x[1]* x[2]}") x = [("oranges", 4, 3.22),("gummy bear

我想制作一个简单的流程订单,如: 你订了一份抗酸药,总共5.33美元 你填了一份3份酸咬的订单,总共6.99美元

我的代码是:

total = 0

def process_order(x_list):

    for x in len(x_list):
        print(f"You filled an order for{x[1]} {x[0]} for a total of {x[1]* x[2]}")
x = [("oranges", 4, 3.22),("gummy bears",1,1.99),("sour bites", 3, 2.33), ("antacid", 1, 5.33)]

while(len(x)>0):
    process_order(x)

print("Total price: ${:.2f}".format(total))
但我犯了个错误


TypeError:“int”对象不可编辑

据我所知,这似乎是您想要的代码:

total = 0

def process_order(x_list):
    global total
    for x in x_list:
        print(f"You filled an order for {x[1]} {x[0]} for a total of {x[1]* x[2]}")
        total = total + x[1] * x[2]

    return

x = [("oranges", 4, 3.22),("gummy bears",1,1.99),("sour bites", 3, 2.33), ("antacid", 1, 5.33)]

process_order(x)
print("Total price: ${:.2f}".format(total))
输出

You filled an order for 4 oranges for a total of 12.88
You filled an order for 1 gummy bears for a total of 1.99
You filled an order for 3 sour bites for a total of 6.99
You filled an order for 1 antacid for a total of 5.33
Total price: $27.19

您需要将
total
定义为一个全局变量,以获得价格的总金额。此外,您还可以在python中迭代列表,如列表中列表元素的
。如果你想做一个函数,它需要
return
语句结尾。在这种情况下,您需要做的是将每个价格添加到
全局变量中,并打印出每个项目的流程,无需返回任何内容。

您的代码可以使用相同的概念简化,而无需使用定义的函数。首先要解决的是您的
,而len(x)>0
需要一些东西来减少
len(x)
以结束循环。为此,我们可以使用
.pop()
。这将删除索引处的项,我们将使用
0
,并将其分配给变量
z
。从这里我们可以使用您的打印语句不变,我使用其他格式作为首选。然后我们可以将每个人的总数添加到我们的跑步记录中

x = [("oranges", 4, 3.22),("gummy bears",1,1.99),("sour bites", 3, 2.33), ("antacid", 1, 5.33)]
total = 0

while len(x) > 0:
    z = x.pop(0)
    print('You filled and order for {} {} for a total of {}'.format(z[1], z[0], z[1] * z[2]))
    total += z[1] * z[2]

print("Total price: {:.2f}".format(total))

函数名process\u order给出了一个概念,即只处理了一个订单。因此,在那里同时处理许多订单不是一个好主意。此外,如果您从这个函数返回一个订单总数,您可以在之后对其求和,得到total:total=sum(map(process\u order,orders))。这将为您节省大量时间来改进您的功能,同时与计数总数保持一致

下面是示例代码:

def处理顺序(顺序):
“”“处理订单。
订单是一个元组,包含3个字段:名称、数量和价格。
"""
名称、数量、价格=订单
总价=价格*数量
打印(f)“您填写了{]}{}的订单,总共{.2f}”。格式(
数量、名称、总价)
返回总价格
订单=[
(“橘子”,4,3.22),(“粘熊”,1,1.99),
(“酸咬”,3,2.33),(“抗酸剂”,1,5.33)]
total=sum(映射(流程顺序,顺序))#可以用for循环重写
打印(“总价:${.2f}”。格式(总价))

使用pythonic方式:
用于items中的item
,它将解决您的问题。在您的情况下,您可以使用
来表示项目、数量、价格:print(如果您填写了{quantity}{item}的订单,表示总{quantity*price}')
使用
来表示x_列表中的x
而不是
表示len(x)中的x(x)
是无限的,
len x(x)
永远不要更改
过程中的循环顺序
方法:
对于len中的x(x\u列表):
循环
x
为0到3之间的整数。当您执行
x[1]
(或任何其他下标)时,您需要在
int
下标。函数名
process\u order
给出了一个想法,即只处理了一个订单。因此,在那里同时处理许多订单不是一个好主意。此外,如果您从这个函数返回一个订单总计,您可以在之后对其求和,得到总计:
total=sum(map(process\u order,orders))
。这将为您节省大量时间来改进您的函数,而计数总数仍然保持不变。格式:请在
process\u order
@EirNym-Oops的正文中添加更多空格,我已经修复了它!谢谢
You filled and order for 4 oranges for a total of 12.88
You filled and order for 1 gummy bears for a total of 1.99
You filled and order for 3 sour bites for a total of 6.99
You filled and order for 1 antacid for a total of 5.33
Total price: 27.19