This code aims to check whether inputs are in a pre-determined list and, if not, force the user to start over again.
I understand I can use a FOR or WHILE loop and have the IF and ELSE lines nested in the loops. However, I've realised this gets rather messy and tedious on a large scale.
Is there a way to send read previous lines of code after a loop has been exited? The line that concerns us is the one with the "#problem area"; here is my code:
# Variables:
healthy = ["potato", "tomato"]
counter_ = 0
limit_ = 3
while counter_ < limit_:
shopping_list = []
wanted_item = input("What to get? \n").lower()
if wanted_item not in healthy:
counter_ += 1
print("Try again")
continue
if wanted_item in healthy:
shopping_list.append(wanted_item)
break
else:
break
if counter_ >= limit_:
print("Start over")
#problem area
else:
print("That's a good list")
print(shopping_list)