user13949302
user13949302

Reputation:

I am trying to make a list in a while loop but my loop will not restart

inp = input("Would you want to (A)dd or (D)elete something from the list? (E to exit) ")
mylist = []
while inp != "E":
    if inp == "A":
        add = input("Enter an item to add: ")
        mylist.append(add)
    elif inp == "D":
        delete = input("Enter an item to delete: ")
        mylist.remove(delete)` 

When I do this, my program continues to ask for an item to add. How do I get my loop to start over?

Upvotes: 0

Views: 50

Answers (2)

ckunder
ckunder

Reputation: 2300

Move the first input() within the while loop:

  while inp != "E":
      inp = input("Would you want to (A)dd or (D)elete something from the list? (E to exit) ") 

Upvotes: 0

Aleksander Ikleiw
Aleksander Ikleiw

Reputation: 2685

You have to call the inp variable which is input under the while loop.

inp = ""

mylist = []
while inp != "E":
    inp = input("Would you want to (A)dd or (D)elete something from the list? (E to exit) ")
    if inp == "A":
        add = input("Enter an item to add: ")
        mylist.append(add)
    elif inp == "D":
        delete = input("Enter an item to delete: ")
        mylist.remove(delete)

Upvotes: 1

Related Questions