Python Forum
Help with while loop creating an infinite loop.
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Help with while loop creating an infinite loop.
#1
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-
Reply
#2
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.") 
Reply
#3
(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.
Reply
#4
(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.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Loop to find the best combination/score KoinKoin 21 26,515 Jan-05-2023, 10:31 AM
Last Post: KoinKoin
  Many iterations for loop question adesimone 9 1,818 Nov-12-2022, 07:08 PM
Last Post: deanhystad
  Please check whether the code about the for loop question is correct. (SyntaxError) lilliancsk01 10 2,554 Nov-08-2022, 01:25 PM
Last Post: deanhystad
  while loop idddj 8 1,672 Oct-03-2022, 05:03 PM
Last Post: jefsummers
  Beginner Python Question: FIzz Buzz using while loop camoyn13 2 1,780 Sep-20-2022, 09:00 AM
Last Post: deanhystad
  Function combining file manipulation and loop Leyo 5 1,744 Mar-23-2022, 09:47 AM
Last Post: Leyo
  Using If Statements Instead of While Loop in Simple Game Program new_coder_231013 5 3,128 Dec-14-2021, 12:23 AM
Last Post: supuflounder
Big Grin for loop nadun 3 1,852 Nov-22-2021, 03:36 PM
Last Post: deanhystad
  How to compile following python loop program reinispl 3 1,936 Oct-27-2021, 01:57 PM
Last Post: DeaD_EyE
  Printing During a Loop apeltes 16 5,161 Oct-21-2021, 12:19 AM
Last Post: apeltes

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020