Hi, I have a program that deals with stacks and queues in python. Part of the program compares items in 3 stacks that contain objects. The loop will run and compare all items in the top of the stacks and pop the item that has the highest value and appends to a queue.

The problem I am haveing right now is the best way to set up my conditional while loop to have the block of code keep running untill all 3 stacks are empty of objects. Currently the loop ends as soon as one of the stacks become empty of objects.

Whats the best way to do a multi way decision to make sure my loop continues to run untill ALL stacks are empty.
Here is a snippit of the code thats giving me issues.

Runway1-3 are my stacks, and they are being added to runway4 which is my queue depending on which airplane object has the highest number of passengers. Currently the comparison part of my code seems to be working correctly but the top while loop that runs the whole block of code untill the stacks are empty is whats giving me issues. Its ending as soon as 1 stack becomes empty, not all three.

    def makeRunwayQueue(runway1, runway2, runway3):
        runway4 = makeQueue()

        while len(runway1) > 0 and len(runway2) > 0 and len(runway3) > 0:
            run1Plane = top(runway1)
            topPassenger1 = run1Plane.getNumOfPassengers()

            run2Plane = top(runway2)
            topPassenger2 = run2Plane.getNumOfPassengers()

            run3Plane = top(runway3)
            topPassenger3 = run3Plane.getNumOfPassengers()

            if topPassenger1 > topPassenger2 and topPassenger1 > topPassenger3:
                popPlane = pop(runway1)
                runway4.append(popPlane)
            elif topPassenger2 > topPassenger1 and topPassenger2 > topPassenger3:
                popPlane = pop(runway2)
                runway4.append(popPlane)
            else:
                popPlane = pop(runway3)
                runway4.append(popPlane)
        return runway4

while len(runway1) > 0 and len(runway2) > 0 and len(runway3) > 0

This should be or not and

run1Plane = top(runway1)
topPassenger1 = run1Plane.getNumOfPassengers()

This would need to go inside a

if runway1:

Then you would keep looking which of stacks has the value to pop, but you must update that info to variables inside these if statements and only execute the pop after. Best would probably be to have the stacks in list, so you can use simple indexing instead of multiple variable names.

Edited 4 Years Ago by pyTony

Could you explain what you mean by the second half of your reply with the if statement? I'm not quite sure how it fits into my code and what it will change.

Also, changing and to or gives me an error

line 54, in makeRunwayQueue
topPassenger3 = run3Plane.getNumOfPassengers()
AttributeError: 'NoneType' object has no attribute 'getNumOfPassengers'

Perhaps runway3 is empty at that point? Like pyTony said, you should first check each runway if it is empty or not before trying to get the top. Because if a runway is empty, the top of it is a nonetype which will give you that error.

Edited 4 Years Ago by scudzilla

This article has been dead for over six months. Start a new discussion instead.