1

I am working on a sequence that loops through a list and asks the user a question about each of the items in the list. Depending on the answer, the sequence should do 1 of 3 things:

1. Continue on to the next item
2. Delete the item at the present index
3. Display and error message, restart

So far, I've gotten the loop to work almost how I need it to, but it is still not functioning the way that I would like for it to and I am not sure where I am going wrong with it.

Here's what I've got:

fruit_list = ["Apples", "Pears", "Oranges", "Peaches"]
item = 1
while item < len(fruit_list):

    for val in fruit_list:

        user_resp = input("Do you like " + str(val) + "? ")
        print()
        if user_resp != "yes" and user_resp != "no":
            print("Please only answer with 'yes' or 'no'.")
            item+=1
            break
        elif user_resp == "yes":
        item+=1
            continue
        elif user_resp == "no":
            fruit_list.remove(val)
            item+=1
            continue

The issues/questions I have are:

  1. I used the variable "item" to try to limit the while-loop to loop until all items in the list have been cycled through, but if the user enters a wrong answer I want to be able to keep asking the same question until they answer "yes" or "no". What I have now just breaks out of the loop if the answer is not "yes" or "no".

  2. When the user enters "no" the sequence should remove the index/value for the item that had just been displayed to the user and then display the next indexed item's value but instead what I have does remove the item but it also displays the value after the next index (i.e. if you removed "Apples" from the list, instead of displaying "Pears" next, it displays "Oranges").

Any thoughts/suggestions on how to optimize this?

pault
  • 41,343
  • 15
  • 107
  • 149
McBraunie
  • 107
  • 1
  • 12
  • 1
    It's generally not a good idea to try to [modify a list while iterating over it](https://stackoverflow.com/questions/1637807/modifying-list-while-iterating). – pault May 16 '18 at 19:33
  • 2
    maybe use a while loop inside your for loop that only increments the for loop if the input is valid, and like @pault said instead of modefing the list while iterating it "mark" all the index that need to be deleted, maybe store in a list the index to be removed – MiguelD May 16 '18 at 19:40

3 Answers3

1

I think the issue is that you're trying to loop over a list using an index but once you delete one of the items in the list the index is no longer correct. It might work if you just remove the item +=1 if the user answers no. However if I was to do it I would do something like this:

fruit_list = ["Apples", "Pears", "Oranges", "Peaches"]
liked_fruits = []

for fruit in fruit_list:
    while True:
        user_resp = input("Do you like " + fruit + "?: ").lower()
        print()
        if user_resp != "yes" and user_resp != "no":
            print("Please only answer with 'yes' or 'no'.")
        elif user_resp == "yes":
            liked_fruits.append(fruit)
            break
        else:
            break
print(liked_fruits)
Jibbril
  • 36
  • 4
0

if you just create a copy of list you can avoid extra while loop. Please note that you need create the copy of list, list(fruits) if you will just do fruits_copy = fruits it's going to point to the same list and will create issue.

fruits = ["Apples", "Pears", "Oranges", "Peaches"]
fruits_copy = list(fruits)

for fruit in fruits:

    user_resp = raw_input("Do you like " + fruit + "? ")
    if user_resp.lower() == "yes":
        continue
    elif user_resp.lower() == "no":
        fruits_copy.remove(fruit)
    else:
        print("Please only answer with 'yes' or 'no'.")
        break

print fruits_copy
print fruits
Gaurang Shah
  • 11,764
  • 9
  • 74
  • 137
0

No need to copy or create another list, your problem was in remove function

fruit_list = ["Apples", "Pears", "Oranges", "Peaches"]                                                                                                                            

counter = 0

while True:
   if not len(fruit_list) or counter >= len(fruit_list):
       break

   for i, val in enumerate(fruit_list):
       user_resp = input("Do you like " + str(val) + "? ")
       if user_resp == "yes":
           counter += 1
           continue
       elif user_resp == 'no':
           fruit_list.pop(i)
           continue
       else:
           print("Please only answer with 'yes' or 'no'.")
           break

~

Druta Ruslan
  • 7,171
  • 2
  • 28
  • 38