Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Python

Using while loop in a function and then the same function in the while loop [Python]

I wrote this code to create a function that removes items from list

ToDo = ["Python", "Java", "Electronics"]
def remove(list):
print("Currently you have following items in: ")
display(list)
answer= input ("Do you want to continue yes/no ") 
while answer.lower() == "yes":
    list.remove(input("Enter the Value you want to remove"))
    print("List Updated")
    remove(list)

  def display(list):
  for item in list:
    print(item)

While calling remove() fucntion in python interactive mode

treehouse:~/workspace$ python -i work.py

remove(ToDo)
Currently you have following items in:
Python
Java
Electronics
Do you want to continue yes/no yes Enter the Value you want to remove Electronics
List Updated
Currently you have following items in:
Python
Java
Do you want to continue yes/no no
Enter the Value you want to remove _

Why it's asking again to remove the value even if I wrote no in last second line. What's wrong with my code?

1 Answer

turaboy holmirzaev
turaboy holmirzaev
19,198 Points

Hi, -- program starts -- answer gets 'yes' # you set answer yes -- while loop runs -- remove the item -- (second) program starts again # but this time the program is running inside while loop -- inside program prompts for answer -- if you set answer 'no' -- (second) while loop inside the second(program) does not run -- jumps back to the beginning of the first while loop In other words, it will run infinitely until you run out of list items