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:
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".
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?