Shopping basket

Try me

Open In ColabBinder

Let´s build a bot to collect the shopping list of a user in a dictionary. Use the input function to get the name of the user, and the names and number of items that the user wants to add to the shopping list. The expected result is a dictionary like this:

shopping_list = {
    'user_name': 'Mark Grayson',
    'shopping_list': [
        {'product_name': 'Tomatoes', 'items': 3},
        {'product_name': 'Chicken', 'items': 4},
        {'product_name': 'Rice', 'items': 2}
    ]}

That is, we store the name of the user in a string field, and shopping list in a list of dictionaries where each dictionary has a ‘product_name’ key with the value of the name of the product and an ‘items’ key with the number of items to purchase. Once you have collected this information, can you count the total number of items to purchase?

Solution

Ok, let´s build a shopping assistant Our assistant uses a while loop that breaks when the user enters a blank product name:

[2]:
# Let´s build a shopping assistant
my_shopping_list = {} #Empty dictionary
user_name = input("Tell me your name")
my_shopping_list["user_name"] = user_name
my_shopping_list["shopping_list"] = []
while True:
  product_name = input("Tell me the name of the product (or press enter to stop): ")
  if product_name == "":
    break
  no_items = int(input("Tell me the number of items:"))
  new_product_info = {
      "product_name":product_name,
      "items":no_items
      }
  my_shopping_list["shopping_list"].append(new_product_info)


print(my_shopping_list)
{'user_name': 'Mark Grayson', 'shopping_list': [{'product_name': 'Kanslok', 'items': 25}, {'product_name': 'Fried Sequids', 'items': 10}, {'product_name': 'Talescrian Lobster', 'items': 13}]}
[4]:
# To get the total number of items, just traverse the product list:
total_items = 0
for product in my_shopping_list["shopping_list"]:
    total_items += product["items"]

print(f"Hello, {my_shopping_list['user_name']} you have added a total of {total_items} items to your shopping list")
Hello, Mark Grayson you have added a total of 48 items to your shopping list

Analysis questions

  1. Error Handling: What happens if the user enters a string instead of a number when asked for the number of items? And what happens if the user enters a negative number? Can you modify the code to handle this error? What happens if the user enters a blank product name? Can you modify the code to handle this error?

  2. Data structure: Can you think of a better data structure to store the shopping list? What if we want to add the price of each item? What if we want to add the total price of the shopping list? What if we want to add the shop where we can buy each item?

  3. Data Persistence: Can you think of a way to store the shopping list in a file so that we can retrieve it later?

  4. Personalization: How does the chatbot personalize the interaction with the user? How does it remember the user’s name throughout the conversation?

  5. Nested Structures: Describe how the dictionary is structured. Why might nested dictionaries be useful in this scenario?