Python Forum
Help with while loop creating an infinite loop. - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: Homework (https://python-forum.io/forum-9.html)
+--- Thread: Help with while loop creating an infinite loop. (/thread-15765.html)



Help with while loop creating an infinite loop. - FWendeburg - Jan-30-2019

Hi, i have a problem with this python code that should not create an infinite loop. This code should take one item from one list, print a message using that item and then add it to another list.At the end of the program all the items in the second list should be printed.

sandwich_orders = ['halumi', 'tuna', 'ham and cheese', 'salami', ]

finished_sanwiches = []

while sandwich_orders:
    sandwich_ordered = sandwich_orders.pop()
    print("\nMaking your " + sandwich_ordered.title() + " sandwich.")
    sandwich_orders.append(sandwich_ordered)

print("\n===This sandwiches have been finished making.===")
for sandwich in finished_sanwiches:
    print(sandwich.title())
What the program does:
Error:
Making your Salami sandwich. Making your Salami sandwich. Making your Salami sandwich. Making your Salami sandwich. Making your Salami sandwich. Making your Salami sandwich. Making your Salami sandwich. -this continues to infinity-



RE: Help with while loop creating an infinite loop. - woooee - Jan-30-2019

Use a for instead. Note that you append the "pop" in the last statement.
for sandwich_ordered in sandwich_orders:
    print("\nMaking your " + sandwich_ordered.title() + " sandwich.") 



RE: Help with while loop creating an infinite loop. - nilamo - Jan-30-2019

(Jan-30-2019, 04:13 PM)FWendeburg Wrote:
while sandwich_orders:
    sandwich_ordered = sandwich_orders.pop()
    sandwich_orders.append(sandwich_ordered)

I think you meant to append that to the finished_sanwiches list.


RE: Help with while loop creating an infinite loop. - FWendeburg - Jan-30-2019

(Jan-30-2019, 04:57 PM)woooee Wrote: Use a for instead. Note that you append the "pop" in the last statement.
for sandwich_ordered in sandwich_orders:
    print("\nMaking your " + sandwich_ordered.title() + " sandwich.") 

Thanks for your help :D.

(Jan-30-2019, 05:16 PM)nilamo Wrote:
(Jan-30-2019, 04:13 PM)FWendeburg Wrote:
while sandwich_orders:
    sandwich_ordered = sandwich_orders.pop()
    sandwich_orders.append(sandwich_ordered)

I think you meant to append that to the finished_sanwiches list.

Thank you for your help :D.