Help: Que Manager

# Manage the queue without mistakes.
# Useful string constants.
PHRASE_IN = " in"
PHRASE_OUT = " out"
IDLE = "idle"
# The list of workers.
workers = hero.findByType("peasant", hero.findFriends())
# Use this array to control the worker queue.
queue = []
workerIndex = 0
while True:
    # Iterate through all the workers:
    while workerIndex < len(workers):
        worker = workers[workerIndex]
        # If the worker's "status" property is "idle":
        if worker.status is "idle":
            # Say the worker's name and the string " in":
            hero.say(worker + " in")
            # Add the worker to the end of the queue:
            workerIndex += 1
        item = hero.findNearestItem()
        # If the item exists and the queue isn't empty:
    if item:
        # Say the name of the first worker in the queue
        # with the string " out":
        hero.say(worker + " out")
        #  Remove (shift) the first worker from the queue:
        workerIndex -= 1

Yep, that’s my code.

1 Like

Probably the last line. Plz explain…

You have to check if there is an item and if the length of the queue is greater than zero, so that you know that there is an item, and the queue is not empty.


if item and workerIndex > 0


You have to check the length of the queue. Check it with:

len(queue) >0

You need to append the worker into the queue. And later, you need to remove the worker from the queue.

@Dragonlouis, the reason you get an infinite loop is because your code is not correct.