When a menu must keep asking until the user enters an acceptable choice, place the prompt inside a while True loop. A break statement ends the innermost loop immediately and continues with the code after it. section 4.0
A menu that stops at the right moment
while True: choice = input(prompt) if choice in allowed_answers: answer = choices[int(choice) - 1] break else: print("Enter a valid number") return answer Invalid input reaches the message and repeats the loop. A valid choice reaches break, so the function returns the selected answer instead of asking again. section 4.0
Keep these details in view:
- Indent every statement belonging to the loop. section 4.0
- Use break only after the desired condition succeeds. section 4.0
- Try changing the menu so X or x exits without selecting an item. section 4.0






