【问题标题】:Iterating Through a List to create a Stock checker遍历列表以创建库存检查器
【发布时间】:2019-04-27 09:36:57
【问题描述】:

所以开始时,我一般是编程新手(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 文件中提供的每个项目是否与用户扫描的每个项目匹配。

提前致谢。

【问题讨论】:

    标签: python list function


    【解决方案1】:

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

    你真的很接近-在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")
    

    iterating with a for loop 不需要使用索引时,可以直接遍历序列项。您不应该使用 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


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

    【讨论】:

    • 你好,wwii 谢谢你这么详细的回复。我将把这些修改添加到我的程序中,并继续前进。我现在看到您提到 while 语句已经处理了比较,所以我用下一个 if 语句写了太多。我在不使用索引的情况下尝试了很长时间,这只是我可以让它工作的唯一方法,所以必须回顾一下。
    猜你喜欢
    • 1970-01-01
    • 2017-05-07
    • 2020-07-18
    • 2020-11-25
    • 2021-03-06
    • 2023-01-25
    • 1970-01-01
    • 2018-04-12
    • 2013-08-11
    相关资源
    最近更新 更多