首页 > 解决方案 > 为什么即使满足 if 语句条件,我的 else 语句也会执行?

问题描述

我正在使用 for 循环在文本中搜索 for 循环内的值 usinf if else 语句。即使满足了我的搜索条件,我的 else 块也正在执行中。

这是我用来搜索值的文本:

SKU 产品描述 包装/尺寸 数量 数量 价格扩展 1 WL140.111 Clam Tuatua Medium NZ / 20-34 pcs / kg / 30.00 KG HK$109.25 HK$3,277.50 地点: KIT - Butchery (8%) 30.00 Edit Line Edit Alloc

这是我的代码:

whole_details = re.compile(r'Item([\$\w\s\.\/\-\,:()%]+)(?:Sub Total)')
wd = whole_details.search(text)
wd_text = wd.group(1)

products = ["Yoghurt Passionfruit Organic", "Yoghurt Plain Organic Vegan", "Clam Tuatua Medium 20-", "Clam Tuatua Medium NZ /", "Oyster Pacific NZ /"]

for product in products:
    if wd_text.find(product) != -1:
        re_qty = re.compile(rf'{product}\s([\d.]+)')
        qty_search = re_qty.search(wd_text)
        qty = qty_search.group(1)
        print("Product Description : " + product)
        print("Quantity : " + qty)
else:
    print("No product")

这是我现在得到的输出:

Product Description : Clam Tuatua Medium NZ /
Quantity : 20
No products

标签: pythonfor-loopif-statement

解决方案


推荐阅读