2

So to begin I am new to programming in general (3 months or so in) and although learning through books is good I do like to try and apply my knowledge and learn through experience.

At my work our warehouse staff often pick orders wrong so I am trying to develop something that will pull an order list from a .txt file and check it against the picked item. I feel this is a task I can use to consolidate some of my current knowledge whilst also learning new things.

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()

So the general idea is to creata a .txt file with a list of item serial codes that need pulling and then get that in python within the order function that I created. I then use the picklist function to split the items that are stored in the .txt file into a list so that I can get the user to scan one item at a time to verify it is the correct one.

This is where I'm trying to call on what is currently called the test function. I want this function to take each item within the list and if it is equal to the item scanned to print that is is the correct item. This works kind of fine, well perfectly fine for the first item.

The problem is getting it to iterate onto the next item in the list. So if item one is 2155 and item 2155 is scanned it will say correct item. The issue is it will then say "Wrong item! Please scan again:" because I assume pythong has now moved onto item 2 in the list. But, if I then input the code for 2 it will say wrong item! Please scan again.

I've tried using list indexing to no avial - maybe I should be doing this in a single function and not splitting it as I am.

I am certainly not looking for anyone to finish the code for me but really point me in the right direction of what I need to learn. The final goal of the project is for it also to hold information regarding the warehouse location for each item, the amount of each item needed, and the ability to pull the picklist off of our internal order system. However, they're things I want to integrate bit by bit as I learn.

I understand this probably is not the slickest, most pythonic code ever but really im after something easy to read, understand and edit in the future.

For now I just need to understand what I need to learn / how I need to think about this issue so that I can check each item in the .txt file provided matches each item scanned by the user.

Thanks in advance.

1 Answers1

0

For each item in the list you want to scan a picked item and compare them; if the comparison fails you want to continually scan picked items until they match.

You are really close - in the else suite the comparison with the new scanned item needs to be in the loop, the indentation is wrong; you also need to assign input's return value to pickeditem,

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

Then at the bottom of the for loop, you need to scan the next item picked to compare it to the next item in the list.

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")

The else suite could be simplified a bit - the while statement makes the comparison for you so you don't need to check it again with the if statement:

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")

You should always try to simplify the logic to minimize potential for problems/errors and it can make the code easier to read. The if/else statement is not really needed because the comparison is being made with the while statement.

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")

When iterating with a for loop you don't need to use indices, you can iterate over the sequence items directly. You shouldn't use python keywords, function or class names for your variable names, i changed the list name to 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")

If you need to keep track of the item indices while iterating, use enumerate.


You might be interested in the SO answer for: Asking a user for input till they give a valid response.

wwii
  • 23,232
  • 7
  • 37
  • 77
  • Hello, wwii thank you for such a detailed response. I will be adding these amendments to my programme and though process moving forward. I see now you mention it that the while statment already processes the comparison so I'm writing too much with the next if statement. I tried for ages without using indices and its just the only way I could get it to work so will have to look back at that. – William Cullum Nov 26 '18 at 18:45