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

Dani AI

Generated

— the immediate cause of the AttributeError is that your loop tries to inspect every runway’s top unconditionally. As hinted, the loop condition should let the body run while at least one runway still has planes (use or or any()), but as pointed out you must never call top(...) (or otherwise dereference) on an empty stack. The safe pattern is: 1) make a list of runways, 2) inside the loop only look at runways that are non-empty, 3) pick the non-empty runway whose top has the largest passenger count, and 4) pop that runway.

A concise, scalable example (assumes Python lists where the end is the top):

stacks = [runway1, runway2, runway3]
out = []

while any(stacks):                         # run while at least one non-empty
    candidates = [(i, s[-1]) for i,s in enumerate(stacks) if s]
    i_best, best_plane = max(candidates, key=lambda p: p[1].getNumOfPassengers())
    out.append(stacks[i_best].pop())

Notes and edge cases: Python’s max chooses the first highest key when there’s a tie — that gives a predictable tie-break (lower index wins). If you want round-robin tie-breaking, or random choice on ties, change the tie policy explicitly. If your stack API is a custom object (not plain lists), replace s[-1] and s.pop() with your safe accessors — but always guard them with if s: (or if not s.is_empty():) so you never call getNumOfPassengers() on None.

Quick debugging checklist: print the candidates each iteration to verify which runways are considered; add assertions that best_plane is not None; and write a small test with one empty runway to reproduce the AttributeError (confirm the fix). This approach scales cleanly to N runways and avoids the “loop dies when one runway empties” problem.

Recommended Answers

All 3 Replies

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.

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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.