'Cannot get out of infinite loop

The problem I am trying to solve is go through a list. In this list, if there is the letter "a", give me all those words. Once I have done that, pose the same question again so I go choose the letter "m". Once I type "quit" give me the final list.

Here is the code I have so far, but I get stuck in an infinite loop. I am missing something small that prevents me from asking the same question. When I type the below code, I get an infinite loop.

list = ["aardvark", "baboon", "camel", "elephant", "python", "giraffe", "tiger", "gorilla"]
     
new_list = []

y = input("Pick an orange letter that you want to check against.  Type quit to exit  ") 


while y != "quit":
    for x in list:
        if y in x:
            new_list.append(x)
    print(new_list)


Solution 1:[1]

You are not asking for new input in your loop. Therefore, you are not updating your value of y necessary to exit the loop.

Something like the below should work.

list = ["aardvark", "baboon", "camel", "elephant", "python", "giraffe", "tiger", "gorilla"]
 
new_list = []

y = input("Pick an orange letter that you want to check against.  Type quit to exit  ") 


while y!= "quit":
 for x in list:
     if y in x:
      new_list.append(x)
 print(new_list)
 y = input("Pick an orange letter that you want to check against.  Type quit to exit  ") 

Solution 2:[2]

You answered you own question: you did not let the user type the question again! Correct the code like this:

list = ["aardvark", "baboon", "camel", "elephant", "python", "giraffe", "tiger", "gorilla"]

new_list = []

y = input("Pick an orange letter that you want to check against.  Type quit to exit  ") 


while y!= "quit":
 for x in list:
  if y in x:
   new_list.append(x)
 y = input("Pick an orange letter that you want to check against.  Type quit to exit  ") 
 print(new_list)

Solution 3:[3]

You are going to have some repeated words unless you add line 4 below:

while y!= "quit":
 for x in list:
     if y in x:
         if x not in new_list:
            new_list.append(x)

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 lmonninger
Solution 2 richardec
Solution 3 gerald