【问题标题】:Return to "for" after "if" is executed执行完“if”后返回“for”
【发布时间】:2021-06-15 19:43:58
【问题描述】:

我正在尝试创建一个为潜在客户生成估算值的程序。 这个想法是,您输入商品 ID 和所需商品的数量,然后程序会通过数据库查找和提取匹配值(如果有)。

在没有匹配的情况下,我希望能够手动添加明细(针对当前计算,不写入数据库)

一切正常,如果手动过程是在最后完成的。但是由于你事先不知道一个项目是否存在,所以很容易破坏程序。

在写这篇文章之前,我已经尝试过到处寻找解决方案,但一直没能做到。 我的猜测是我不完全理解事物的顺序和/或缺少一些使其工作的基本功能。

据我了解,我最接近的解释是,以某种方式为 if/else 语句创建单独的函数并在适当时单独调用它们,但我不知道如何在现有代码中实现它.

这是代码中的一个 sn-p:

    #TRY TO FIND ITEM IN FILE

while totalItems != 0:
    article = input("Search: ")
    totalItems = totalItems - 1

    for row in csv_file:
        if article == row[0]:
            items.append(row[0])
            desc.append(row[1])
            prices.append(float(row[2]))
            qt = input("Quantity: ")
            qt = int(qt)
            qts.append(qt)
            break

    if article != row[0]:
        items.append(article)
        qt = input("Quantity: ")
        qt = int(qt)
        qts.append(qt)
        inputItem = input("Item: ")
        desc.append(inputItem)
        inputPrice = input("Price: ")
        inputPrice = float(inputPrice)
        prices.append(inputPrice)

如果所有项目都存在于数据库/文件中,则示例 sn-p:

Search: 1821091
Quantity: 5
Search: 1835006
Quantity: 5
Search: 1835017
Quantity: 5
How many % profit?: 

如果最后一项不存在,则示例 sn-p:

Search: 1821091
Quantity: 5
Search: 1835006
Quantity: 5
Search: 0000000
Quantity: 5
Item: Bogus-Product
Price: 80.40
How many % profit?: 

如果在最后一个条目之前的任何位置添加了不存在的项目,则示例 sn-p:

Search: 0000000
Quantity: 5
Item: Bogus-Product
Price: 80.40
Search: 1821091
Quantity: 5
Item: < This should not ask me to add another item...

【问题讨论】:

  • 你不能只是跳来跳去。你的第二个 if 应该缩进,它应该在 for 循环内......
  • 我知道没有“返回”选项,但在我看来,当第二个 if 语句结束时,它应该返回到 for 循环的开头并尝试搜索下一个输入。相反,它再次继续执行第二个 if 语句。如果我缩进第二个“如果”,则一切正常。
  • 如果我理解正确,第二个 if 应该只在没有找到匹配项的情况下执行。因此,在第一个 if 块中,在中断之前添加一个标志 found = True,然后在第二个中检查它。
  • @Reti43 正确,它第一次工作。但不是打破第二个 if,而是回到 for 循环的开头并问我另一个产品(“搜索:” ),它继续要求我手动输入(“项目:”)。我试图实现标志,但无法使其正常工作。设置如下。在 if 语句之外找到 = True | if found: --> 运行第一个块 else: found = False |如果未找到:--> 运行第二个块集找到 = True

标签: python function csv if-statement while-loop


【解决方案1】:

我建议您在处理这些列表时使用键/值以获得更好的性能。此外,始终尽量减少代码创建函数的歧义,以重用相同的部分。

对于每个元素,您需要遍历所有行并检查它是否存在。对于您的初始代码,您需要每次都执行“for”和“break”,同时不要离开。这使您有责任处理列表中元素的查找和分配,如果您已经在字典中拥有所需的数据,则可以轻松查找并使用其值添加到其他列表中。

def add_item(item, description, price): # As you always request for the quantity before adding prices, you can ask it inside the method
            qt = input("Quantity: ")

            items.append(item)
            desc.append(description)
            prices.append(float(price))
            qts.append(int(qt))

...

    csv_list = {}

    for row in csv_reader:
        csv_list.update({row[1]: {"desc": row[0], "price": row[2]}}) # Add each article as being the key in a dictionary and its values with keys for the description and price

    while totalItems != 0:
        article = input("Search: ")
        totalItems = totalItems - 1

        if article in csv_list.keys(): # If the article is in the csv, you just get its values and add to your lists
            values = csv_list[article]
            add_item(article, values["desc"], values["price"])
        else: # If not, you retrieve the item and price to add in your list anyway
            inputItem = input("Item: ")
            inputPrice = input("Price: ")
            add_item(article, inputItem, inputPrice)


我希望这会有所帮助。

问候。

【讨论】:

  • 经过一些修补后,我使用您的方法让它工作了!非常感谢您提供全面、有启发性的帮助。
猜你喜欢
  • 1970-01-01
  • 2010-12-06
  • 2021-08-10
  • 1970-01-01
  • 2022-06-15
  • 1970-01-01
  • 2012-07-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多