'I just started programming and am having problem with both of these while loops

I've just starting a programming course and im making an adventure game. I've gotten up to this "path" where the user must choose between the market or the blacksmith.

If they choose either they have two choices and if they dont say either choice I want to re-ask and go through the loop again. currently if I type everything in first try it works but if I fail to provide a valid answer it prompts to re-enter then doesn't go through the loop again.

How do I write this to take my:

else:
    print ("Choice must be (Sword / Platemail)...")
    answer = input ("").lower().strip()

and

else:
    print ("Choice must be (Bread / Cabbage)...")
    answer = input("").lower().strip()

return to the top of the loop with the "answer" in order to have the if and elif statments do what they need to do?

Any help recommendations would be amazing please keep in mind im brand new to programming.

Picture of code in question



Solution 1:[1]

The pattern is like this:

while True:
    answer = input("Choice?").lower().strip()
    if answer in ('sword','platemail'):
        break
    print("Choice must be Sword or Platemail")

For cleaner code, you might want to put this into a function, where you pass the possible answers and return the validated one.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Tim Roberts