Python Forum

Full Version: Help with while loop creating an infinite loop.
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
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-
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.") 
(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.
(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.