An allowed answers list stores the input strings a menu accepts as valid choices. It replaces a long chain of comparisons and keeps invalid input from reaching the menu lookup. section 4.0
Build the list from the menu itself:
- Start with an empty list. section 4.0
- Use len(choices) and range(1, len(choices)+1) to produce every menu number. section 4.0
- Convert each number to a string because input returns strings, then test choice in allowed_answers. section 4.0
- Add special strings such as X and x when the menu needs a cancellation option. section 4.0
A menu that grows with its choices
A three-item drinks menu accepts 1, 2, and 3; a five-flavor menu automatically accepts 1 through 5. The final menu also accepts X or x to leave a selection blank. section 4.0
