python在执行平均值时更改值

python在执行平均值时更改值,python,Python,我现在得到了一个平均值。我是平均项目数,但如果我看到某个产品名称,我需要将该值计算为3。我做不到。下面是我的代码和XML(缩写)文件。基本上,如果循环看到Tofus,数量应该被3而不是9覆盖。然后得到平均值 import xml.etree.ElementTree as ET root = ET.ElementTree(file="nwind_medium.xml") orders = root.findall("./orders") quantityTotal = 0 for order i

我现在得到了一个平均值。我是平均项目数,但如果我看到某个产品名称,我需要将该值计算为3。我做不到。下面是我的代码和XML(缩写)文件。基本上,如果循环看到Tofus,数量应该被3而不是9覆盖。然后得到平均值

import xml.etree.ElementTree as ET
root = ET.ElementTree(file="nwind_medium.xml")

orders = root.findall("./orders")
quantityTotal = 0
for order in orders:
    orderdetails = order.findall("./orderdetails")
    for detail in orderdetails:
        productName = detail.findall("./products/productname")
        quantityNew = detail.findall("./quantity")
        if productName[0].text == "chang" or productName[0].text == "chai" or productName[0].text == "Tofus":
            quantityNew = 3;
        else: 
            quantityTotal += float(quantityNew[0].text)
numberQuantity = len(orders)
打印“订单的平均项数”,四舍五入((quantityTotal/numberQuantity),1)


-酒和酒骑士葡萄园——72支吉奥瓦尼马苏里拉酒34.8 5-14支福提尼酒s.r.l.——11支卡布拉尔酒14 12-5支卡布拉尔酒——42支新加坡福建炒面酒9.8 10-20支利卡酒交易——汤姆酒——14支豆腐18.6 9-6支马尤米酒——51支曼吉木普干苹果42.4 40-24克/天,伙计-哈纳里·卡恩斯·哈纳-65路易斯安那火辣辣椒酱16.8 15-2新奥尔良卡琼美食-41杰克新英格兰蛤蜊浓汤7.7 10-19新英格兰海鲜罐头厂-51曼吉姆普干苹果42.4 35-24克/天,伙计

您正在设置
quantityNew=3
但随后求和
quantityNew[0]。text
-这是否有效?如果productName[0],我会这样做
。text==“chang”或…:_数量=3;否则:_quantity=float(quantityNew[0].text);quantityTotal+=\u quantity
@AdamSmith该值似乎已更改为零,而不是3。我得到了42.3的输出。应该是43.3。Thanks@user_new_in_python我100%确定您发布的代码不会运行或永远不会进入
if
块。如果您的代码不同,我无法发表评论,但您在此处发布的代码将抛出一个
类型错误:“int”对象不可订阅
如果有
productName[0]。text
匹配
“chang”、“chai”或“Tofus”
@AdamSmith对此深表歉意。。。是的,我知道我发布的那个会显示一个错误,因为我不确定如何继续执行else语句。这就是为什么我把它留为空白,以便得到关于该放什么的建议。我的帖子不清楚,谢谢。在循环中添加变量_quantity后,它会起作用。请更新发布的答案,然后我会接受它作为解决方案。再次感谢:)@user\u new\u在_python中我不懂——我的代码写得很好correct@user_new_in_python我从不定义数量。你把我的代码复制错了吗?
<?xml version="1.0"?>
-<nwind> -<orders another="Friday" orderid="10248"> -<customers> <companyname>Vins et alcools Chevalier</companyname> <customerid>VINET</customerid> </customers> -<orderdetails> -<products> <productid>72</productid> <productname>Mozzarella di Giovanni</productname> </products> <unitprice>34.8</unitprice> <quantity>5</quantity> -<suppliers> <supplierid>14</supplierid> <companyname>Formaggi Fortini s.r.l.</companyname> </suppliers> </orderdetails> -<orderdetails> -<products> <productid>11</productid> <productname>Queso Cabrales</productname> </products> <unitprice>14</unitprice> <quantity>12</quantity> -<suppliers> <supplierid>5</supplierid> <companyname>Cooperativa de Quesos 'Las Cabras'</companyname> </suppliers> </orderdetails> -<orderdetails> -<products> <productid>42</productid> <productname>Singaporean Hokkien Fried Mee</productname> </products> <unitprice>9.8</unitprice> <quantity>10</quantity> -<suppliers> <supplierid>20</supplierid> <companyname>Leka Trading</companyname> </suppliers> </orderdetails> </orders> -<orders orderid="10249"> -<customers> <companyname>Toms Spezialitaten</companyname> <customerid>TOMSP</customerid> </customers> -<orderdetails> -<products> <productid>14</productid> <productname>Tofus</productname> </products> <unitprice>18.6</unitprice> <quantity>9</quantity> -<suppliers> <supplierid>6</supplierid> <companyname>Mayumi's</companyname> </suppliers> </orderdetails> -<orderdetails> -<products> <productid>51</productid> <productname>Manjimup Dried Apples</productname> </products> <unitprice>42.4</unitprice> <quantity>40</quantity> -<suppliers> <supplierid>24</supplierid> <companyname>G'day, Mate</companyname> </suppliers> </orderdetails> </orders> -<orders orderid="10250"> -<customers> <companyname>Hanari Carnes</companyname> <customerid>HANAR</customerid> </customers> -<orderdetails> -<products> <productid>65</productid> <productname>Louisiana Fiery Hot Pepper Sauce</productname> </products> <unitprice>16.8</unitprice> <quantity>15</quantity> -<suppliers> <supplierid>2</supplierid> <companyname>New Orleans Cajun Delights</companyname> </suppliers> </orderdetails> -<orderdetails> -<products> <productid>41</productid> <productname>Jack's New England Clam Chowder</productname> </products> <unitprice>7.7</unitprice> <quantity>10</quantity> -<suppliers> <supplierid>19</supplierid> <companyname>New England Seafood Cannery</companyname> </suppliers> </orderdetails> -<orderdetails> -<products> <productid>51</productid> <productname>Manjimup Dried Apples</productname> </products> <unitprice>42.4</unitprice> <quantity>35</quantity> -<suppliers> <supplierid>24</supplierid> <companyname>G'day, Mate</companyname> </suppliers> </orderdetails> </orders> </nwind>
import xml.etree.ElementTree as ET
root = ET.ElementTree(file="nwind_medium.xml")

orders = root.findall("./orders")
quantityTotal = 0
for order in orders:
    orderdetails = order.findall("./orderdetails")
    for detail in orderdetails:
        productName = detail.findall("./products/productname")
        quantityNew = detail.findall("./quantity")
        if productName[0].text == "chang" or productName[0].text == "chai" or productName[0].text == "Tofus":
            _quantity = 3
        else:
            _quantity = float(quantityNew[0].text)
        quantityTotal += _quantity
numberQuantity = len(orders)