Search code examples
pythonloopsinfinite-loopexit

Trying to exit the program, get endless loop (Python)


I am a beginner, trying to exit one of my first programs in Python, but just get endless loop. Can't understand what's going wrong.

question = input("Please enter your 'to do' list: ")
some_list = []

while True:
    if question not in some_list:
        some_list.append(question)
        question = input("Please enter your 'to do' list: ")
    else:
        print("\n\nPress enter to exit")
        print(some_list)

Solution

  • Welcome! If you want to break the loop like you wrote in one of your prints: "\n\nPress enter to exit", you can use the following solution:

    while True:
      x = input()
      if len(x) == 0:
        break
    

    The full code will be:

    question = input("Please enter your 'to do' list: ")
    list = []
    
    while True:
        question = input("Please enter your 'to do' list: ")
        if len(question) == 0:
            break
        if question not in list:
            list.append(question)
    
    print(list)