首页 > 解决方案 > 遍历列表以创建库存检查器

问题描述

所以开始我一般是新手(3个月左右),虽然通过书本学习很好,但我确实喜欢尝试应用我的知识并通过经验学习。

在我的工作中,我们的仓库工作人员经常选择错误的订单,所以我正在尝试开发一些东西,可以从 .txt 文件中提取订单列表,并根据选择的项目进行检查。我觉得这是一项可以用来巩固我现有知识的任务,同时也可以学习新事物。

from tkinter.filedialog import askopenfilename
#This splits the order .txt file into a list
def picklist(ordernum):
    with open(ordernum, "r") as f:
        mylist = [line.strip() for line in f]

return mylist

def test(list):
    pickeditem = input("Please scan the first item")
    for i in range(len(list)):
        if list[i] == pickeditem:
            print("Correct item.")
        else:
            while list[i] != pickeditem:
                input("Wrong item! Please scan again:")
            if list[i] == pickeditem:
                print("Correct item.")

def order():
    print("Please pick the order you want to complete")
    order = askopenfilename() #gets order .txt from user
    pcklist = picklist(order)
    print("You pick list is: ",pcklist)
    test(pcklist)

order()

所以一般的想法是创建一个 .txt 文件,其中包含需要提取的项目序列代码列表,然后在我创建的 order 函数中的 python 中获取它。然后,我使用 picklist 函数将存储在 .txt 文件中的项目拆分为一个列表,以便我可以让用户一次扫描一个项目以验证它是正确的。

这是我试图调用当前称为测试功能的地方。我希望此函数获取列表中的每个项目,如果它等于扫描要打印的项目,那就是正确的项目。这很好用,对于第一项来说非常好。

问题是让它迭代到列表中的下一个项目。因此,如果项目一是 2155 并且项目 2155 被扫描,它将显示正确的项目。问题是它会说“错误的项目!请再次扫描:”因为我假设 pythong 现在已经移动到列表中的第 2 项。但是,如果我然后输入 2 的代码,它会说错误的项目!请重新扫描。

我尝试过使用列表索引而不是 avial - 也许我应该在单个函数中执行此操作,而不是像我一样拆分它。

我当然不是在寻找任何人来为我完成代码,而是真正为我指明了我需要学习的正确方向。该项目的最终目标是保存有关每个项目的仓库位置、所需每个项目的数量以及从我们的内部订单系统中提取选择列表的能力的信息。但是,它们是我想在学习的过程中一点一点整合的东西。

我知道这可能不是有史以来最流畅、最 Python 的代码,但我真的很想在将来容易阅读、理解和编辑。

现在我只需要了解我需要学习什么/我需要如何考虑这个问题,以便我可以检查 .txt 文件中提供的每个项目是否与用户扫描的每个项目匹配。

提前致谢。

标签: pythonlistfunction

解决方案


对于列表中的每个项目,您要扫描挑选的项目并进行比较;如果比较失败,您希望不断扫描挑选的项目,直到它们匹配。

你真的很接近 - 在else套件中,与新扫描项目的比较需要循环中,缩进是错误的;您还需要将input的返回值分配给pickeditem,

            while list[i] != pickeditem:
                pickeditem = input("Wrong item! Please scan again:")
                if list[i] == pickeditem:
                    print("Correct item.")

然后在 for 循环的底部,您需要扫描选择的下一个项目以将其与列表中的下一个项目进行比较。

def test(list):
    pickeditem = input("Please scan the first item")
    for i in range(len(list)):
        if list[i] == pickeditem:
            print("Correct item.")
        else:
            while list[i] != pickeditem:
                pickeditem = input("Wrong item! Please scan again:")
                if list[i] == pickeditem:
                    print("Correct item.")
        pickeditem = input("Please scan the next item")

else套件可以简化一点 - while 语句为您进行比较,因此您无需使用该if语句再次检查它:

def test(list):
    pickeditem = input("Please scan the first item")
    for i in range(len(list)):
        if list[i] == pickeditem:
            print("Correct item.")
        else:
            while list[i] != pickeditem:
                pickeditem = input("Wrong item! Please scan again:")
            print("Correct item.")
        pickeditem = input("Please scan the next item")

您应该始终尝试简化逻辑以最大程度地减少问题/错误的可能性,并且它可以使代码更易于阅读。该if/else语句并不是真正需要的,因为正在使用 while 语句进行比较。

def test(list):
    pickeditem = input("Please scan the first item")
    for i in range(len(list)):
        while list[i] != pickeditem:
            pickeditem = input("Wrong item! Please scan again:")
        print("Correct item.")
        pickeditem = input("Please scan the next item")

使用 for 循环进行迭代时,您不需要使用索引,您可以直接迭代序列项。您不应该使用 python 关键字、函数或类名作为变量名,我将列表名称更改为a_list.

def test(a_list):
    pickeditem = input("Please scan the first item")
    for item in a_list:
        while item != pickeditem:
            pickeditem = input("Wrong item! Please scan again:")
        print("Correct item.")
        pickeditem = input("Please scan the next item")

如果您需要在迭代时跟踪项目索引,请使用enumerate.


您可能对 SO 的答案感兴趣:Asking a user for input until they give a valid response


推荐阅读