liveBook · Manning

Allowed answers list

As explained in A Pythonic Adventure: From Python basics to a working web app

An allowed answers list is a collection of input strings accepted as valid responses for a menu.

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

How A Pythonic Adventure: From Python basics to a working web app covers it

How deep it goes
Explains construction with lists, len(), range(), string conversion, membership testing, and cancellation values.
Examples
Demonstrates drinks, flavors, toppings, numbered choices, and X or x cancellation.
What it assumes
Assumes basic Python conditionals and lists while teaching dynamic numbering from zero through len() and range().
What you can do after
Build it from scratch: create menu-specific valid answers and use them to reject invalid selections safely.
How it connects
Connects Python lists, input validation, len(), range(), loops, and menus.

This explainer was generated by Manning's knowledge graph from the text of A Pythonic Adventure: From Python basics to a working web app. The section links point to the parts of the book it draws on.