Home > database >  Python While Loop W/ "Go Back" Option
Python While Loop W/ "Go Back" Option

Time:10-26

So I am trying to think of a way to have a go back option if the user scans the wrong barcode. Some insight, my company scans in a lot of equipment and sometimes there are barcodes very close to each other and it easy to scan the wrong one.

The problem I am currently having with my code is that if the user scans the wrong code, there is not way to simply go back. You either have to terminate the script and start over, or insert the data into the db and change it manually in access.

Below is my current code:

# get serial numbers for equipment
print("------------------------")
serial_numbers = []
print("Scan barcode, type 'q' when you are done.")
# loop for user to insert multiple serial numbers.
while True:
    s = input("Scan Code: ")
    if s == "q":
        break
    serial_numbers.append(s)
print("Here is the list of serial numbers:")
print(serial_numbers)

Currently, as stated above, this code is not very...robust, if you will. If you don't get it right on the first try... the user has to completely start over or fix if manually in the Access DB.

Edit:

I attempted to add the .pop() method into the loop, but now the user input 'e' I am using to initiate the pop is being added to the list.

# get serial numbers for equipment
print("------------------------")
serial_numbers = []
print("Scan barcode, Type 'e' to delete last scanned item. Type 'q' when you are done.")
# loop for user to insert multiple serial numbers.
while True:
    s = input("Scan Code: ")
    if s == "e":
        serial_numbers.pop()
        print(serial_numbers)
    if s == "q":
        break
    serial_numbers.append(s)
print("Here is the list of serial numbers:")
print(serial_numbers)

Output:

Scan barcode, Type 'e' to delete last scanned item. Type 'q' when you are done.
Scan Code: 31P11JJ0019US
Scan Code: 31P11JJ0019US
Scan Code: 31P11JJ0019US
Scan Code: 31P11JJ0019US
Scan Code: e
['31P11JJ0019US', '31P11JJ0019US', '31P11JJ0019US']
Scan Code: 31P11JJ0019US
Scan Code: e
['31P11JJ0019US', '31P11JJ0019US', '31P11JJ0019US', 'e']
Scan Code:

CodePudding user response:

What about introducing another command that just delete the last element of the list with the .pop() method?

Something like

if s=="e":
    serial_numbers.pop()

So if the user insert "e", he would be able to insert again the last code. I don't know if this is sufficient but may be a start...

CodePudding user response:

Following the advice from @Liutprand in their solution and adding "continue" into the if pop statement, I was able to get it to work as intended.

# get serial numbers for equipment
print("------------------------")
serial_numbers = []
print("Scan barcode, Type 'e' to delete last scanned item. Type 'q' when you are done.")
# loop for user to insert multiple serial numbers.
while True:
    s = input("Scan Code: ")
    if s == "e":
        serial_numbers.pop()
        print(serial_numbers)
        continue
    if s == "q":
        break
    serial_numbers.append(s)
print("Here is the list of serial numbers:")
print(serial_numbers)
  • Related