A dictionary keeps related bits of information together by pairing each key with a value. Think of one coffee order: the keys can be name, drink, flavor, and topping, while the values hold the customer’s choices. Use a key to assign a value and quickly find it again. section 6.1 section 7.0
Start with an empty dictionary using curly braces: order = {}. Then fill it one small piece at a time, such as order['name'] = input("What's your name: "). Create the dictionary before adding to it; otherwise Python reports that order is not defined. Return the completed dictionary from the function so the rest of the program can use the order. section 7.0
Build one order
An order dictionary can grow as the customer makes choices. Each readable key labels what its value means, making the printed result easy to check before saving or serving the order. section 6.0 section 7.0
In [4]: order['drink'] = 'decaf'
In [5]: order['flavor'] = 'vanilla'
In [6]: order['topping'] = 'chocolate'
In [7]: print(order)
{'name': 'Erik', 'drink': 'decaf', 'flavor': 'vanilla',
'topping': 'chocolate'}
In [8]:
Inspect a live dictionary as name, drink, flavor, and topping are added one key-value pair at a time. section 6.0
Dictionaries also help collect web-form choices. A submitted form provides values through request.form using names such as "drink" as keys. Those values can be gathered into one new_order dictionary, added to an orders list, saved, and read in a template with expressions like new_order["drink"]. section 11.0 section 11.0
For a tidy order, use keys that describe each choice:
- "name" for the customer’s entered name. section 7.0
- "drink", "flavor", and "topping" for menu selections. section 6.0
- The same keys when printing the order, so every value has a clear label. section 11.0









































