-1
def click(self,floor):
        self.order.append(floor)
        self.label.config(text="Order list: {}".format(self.order))
        # door process.
        self.elevator.openDoor()
        time.sleep(1)
        if self.order:
            self.draw_floor(FRAME_LIST[0])
        self.elevator.closeDoor()
        
        while self.order:
            self.elevator.gotoFloor(self.order[0])
            while len(FRAME_LIST) > 1:
                time.sleep(0.5)
                self.draw_floor(FRAME_LIST[0])
                FRAME_LIST.pop(0)
            time.sleep(0.5)
            self.draw_floor(FRAME_LIST[0])
            # door process.
            self.elevator.openDoor()
            self.draw_floor(FRAME_LIST[0])
            time.sleep(1)
            self.elevator.closeDoor()
            self.draw_floor(FRAME_LIST[0])
            self.order.pop(0)
            self.label.config(text="Order list: {}".format(self.order))
        if FRAME_LIST:
            self.draw_floor(FRAME_LIST[0])

"self.order" is a list. this method is triggered by a few tkinter buttons. However, if I rapidly press these buttons, I got the error message "pop from empty list" , followed by my test print output "Before pop(): []". How can the list be empty inside this while loop with condition "list > []" or "list != []" ? Now, even I changed the condition to "while self.order", the error still here.

General Grievance
  • 4,555
  • 31
  • 31
  • 45
JadeChest
  • 1
  • 1
  • 1
  • 2
    `self.order > []` clearly does not do what you think it does. `[1, 1] > []` is `True`. – DeepSpace Jul 21 '21 at 14:14
  • Instead, use `while self.order:` – DeepSpace Jul 21 '21 at 14:16
  • @DeepSpace an empty list is not greater than itself, so it's weird that OP gets this error. `[1, 1] > []` is `True`, but `[1, 1].pop(0)` wouldn't throw that error – Pranav Hosangadi Jul 21 '21 at 14:27
  • OP, can you [edit] your question to include the _exact_ output you get? Condense your code down to a [mre]: remove irrelevant parts of your code like the references to objects and functions that aren't included here. I suspect one of your functions modifies `self.order` _after_ it's in the loop, so `self.order.pop(0)` is no longer possible. I'm not able to replicate your issue with a simple `l = [1, 2, 3, 4, 5]; while l > []: print("Before: ", l, end=""); l.pop(0); print("; After: ", l)` – Pranav Hosangadi Jul 21 '21 at 14:29
  • We _don't_ want your whole code. Please do some [debugging](//ericlippert.com/2014/03/05/how-to-debug-small-programs/) and condense your code down to a [__minimal__ reproducible example](https://stackoverflow.com/help/minimal-reproducible-example). – Pranav Hosangadi Jul 22 '21 at 13:03
  • @PranavHosangadi I think my problem is after I rapidly click the buttons, the button will trigger the command function for additional times without clicking. I'm still confused, I got the lambda after the command tag. btw, the whole code is actually very minimal – JadeChest Jul 22 '21 at 14:08

1 Answers1

0

If you want to have the condition to test that self.order should not be empty list. then use condition as len(self.order)>0 this should be the appropriate instead the one you have.

dinesh kumar
  • 95
  • 1
  • 4