diff --git a/.ipynb_checkpoints/lab-python-flow-control-checkpoint.ipynb b/.ipynb_checkpoints/lab-python-flow-control-checkpoint.ipynb new file mode 100644 index 0000000..156076a --- /dev/null +++ b/.ipynb_checkpoints/lab-python-flow-control-checkpoint.ipynb @@ -0,0 +1,507 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "id": "d3bfc191-8885-42ee-b0a0-bbab867c6f9f", + "metadata": { + "tags": [] + }, + "source": [ + "# Lab | Flow Control" + ] + }, + { + "cell_type": "markdown", + "id": "3851fcd1-cf98-4653-9c89-e003b7ec9400", + "metadata": {}, + "source": [ + "## Exercise: Managing Customer Orders Optimized\n", + "\n", + "In the last lab, you were starting an online store that sells various products. To ensure smooth operations, you developed a program that manages customer orders and inventory.\n", + "\n", + "You did so without using flow control. Let's go a step further and improve this code.\n", + "\n", + "Follow the steps below to complete the exercise:\n", + "\n", + "1. Look at your code from the lab data structures, and improve repeated code with loops.\n", + "\n", + "2. Instead of asking the user to input the name of three products that a customer wants to order, do the following:\n", + " \n", + " a. Prompt the user to enter the name of a product that a customer wants to order.\n", + " \n", + " b. Add the product name to the \"customer_orders\" set.\n", + " \n", + " c. Ask the user if they want to add another product (yes/no).\n", + " \n", + " d. Continue the loop until the user does not want to add another product.\n", + "\n", + "3. Instead of updating the inventory by subtracting 1 from the quantity of each product, only do it for the products that were ordered (those in \"customer_orders\")." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "f822ed3b-b18c-4a14-a47a-f79d5f898c80", + "metadata": {}, + "outputs": [], + "source": [ + "# 1 # list\n", + "#products = [\"t-shirt\",\"mug\",\"hat\",\"book\",\"keychain\"]\n", + "#products" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "1eca6713-b92a-4951-ac7a-1c9ddb527fe0", + "metadata": {}, + "outputs": [], + "source": [] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "ed07d074-e4e2-45e3-972b-a4d877074cee", + "metadata": {}, + "outputs": [], + "source": [ + "# 2 # Dictionary\n", + "#inventory = {}\n", + "#inventory" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "88967a0e-0961-4c41-89df-b44a7890e7e5", + "metadata": { + "scrolled": true + }, + "outputs": [], + "source": [ + "# 3 # Ask the user to input the quantity of each product available in the inventory. Use the product names from the products list as keys in the inventory dictionary and assign the respective quantities as values.\n", + "q_tshirt = int(input(\"Enter the quantity of available T-shirts: \"))\n", + "q_mug = int(input(\"Enter the quantity of available mugs: \"))\n", + "q_hat = int(input(\"Enter the quantity of available hats: \"))\n", + "q_book = int(input(\"Enter the quantity of available books: \"))\n", + "q_keychain = int(input(\"Enter the quantity of available keychains: \"))" + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "id": "277d17d1-d47a-45dd-b2a6-78d004e9ab51", + "metadata": {}, + "outputs": [ + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Enter the available quantity of the product: 1\n", + "Enter the available quantity of the product: 2\n", + "Enter the available quantity of the product: 3\n", + "Enter the available quantity of the product: 4\n", + "Enter the available quantity of the product: 5\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "{'t-shirt': 1, 'mug': 2, 'hat': 3, 'book': 4, 'keychain': 5}\n" + ] + } + ], + "source": [ + "## New code ## Lab Python flow control\n", + "## 1 ##\n", + "products = [\"t-shirt\",\"mug\",\"hat\",\"book\",\"keychain\"]\n", + "inventory = {}\n", + "for i in products:\n", + " inventory[i] = int(input(\"Enter the available quantity of the product: \"))\n", + "\n", + "print(inventory)\n", + "\n" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "a2c29abd-78f7-4fd2-aa63-d124bd510f3a", + "metadata": {}, + "outputs": [], + "source": [] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "4cd654c7-6bf9-4521-9dda-981ddde4a34a", + "metadata": {}, + "outputs": [], + "source": [ + "#inventory = {products[0]:q_tshirt, products[1]:q_mug, products[2]:q_hat, products[3]:q_book, products[4]:q_keychain }" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "87a8eb43-88ea-43ec-9aad-b313513bdf91", + "metadata": {}, + "outputs": [], + "source": [ + "print(inventory)\n", + "type(inventory)" + ] + }, + { + "cell_type": "code", + "execution_count": 4, + "id": "d58546ea-2a44-4264-a8e2-db6fcf564436", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "set" + ] + }, + "execution_count": 4, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "# 4 # Create an empty set called customer_orders\n", + "customer_orders = set()\n", + "type(customer_orders)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "776d46f7-b401-426a-a357-dde5b29b3e54", + "metadata": {}, + "outputs": [], + "source": [ + "# 5 # Ask the user to input the name of three products that a customer wants to order (from those in the products list, meaning three products out of \"t-shirt\", \"mug\", \"hat\", \"book\" or \"keychain\". Add each product name to the `customer_orders` set.\n", + "#ordered_product_1 = input(\"Enter the name of the first product of the order: \")\n", + "#ordered_product_2 = input(\"Enter the name of the second product of the order: \")\n", + "#ordered_product_3 = input(\"Enter the name of the third product of the order: \")" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "3fc1e92b-6e54-4ebf-9527-e6fd3cc74074", + "metadata": {}, + "outputs": [], + "source": [ + "customer_orders" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "14486de4-fa76-462b-9729-c067a23d7da3", + "metadata": {}, + "outputs": [], + "source": [ + "#customer_orders.add(ordered_product_1)\n", + "#customer_orders.add(ordered_product_2)\n", + "#customer_orders.add(ordered_product_3)\n", + "#customer_orders" + ] + }, + { + "cell_type": "code", + "execution_count": 8, + "id": "499653e7-02b3-4d5d-aaae-6539494e8ac8", + "metadata": {}, + "outputs": [ + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Enter the name of the first ordered product: hat\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "{'hat'}\n" + ] + }, + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Do you want to add another product? Answer Yes/No: Yes\n", + "Enter the name of the next ordered product: mug\n", + "Do you want to add another product? Answer Yes/No: Yes\n", + "Enter the name of the next ordered product: hat\n", + "Do you want to add another product? Answer Yes/No: No\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "{'mug', 'hat'}\n" + ] + } + ], + "source": [ + "### New code #### Lab Python flow control\n", + "## 2 ###\n", + "\n", + "first_order = input(\"Enter the name of the first ordered product: \")\n", + "customer_orders.add(first_order)\n", + "print(customer_orders)\n", + "answer=input(\"Do you want to add another product? Answer Yes/No: \")\n", + "while answer =='Yes':\n", + " order = input(\"Enter the name of the next ordered product: \")\n", + " customer_orders.add(order)\n", + " answer=input(\"Do you want to add another product? Answer Yes/No: \")\n", + " \n", + "print(customer_orders) \n", + " " + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "0f3cea4c-b0d8-4ceb-8a9c-e482ff828dd6", + "metadata": {}, + "outputs": [], + "source": [] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "0145c2b6-ba63-4093-8841-0ac58ba88efa", + "metadata": {}, + "outputs": [], + "source": [] + }, + { + "cell_type": "code", + "execution_count": 10, + "id": "7c6e436c-476f-4e4f-ba77-760b0383814e", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "{'mug', 'hat'}\n" + ] + } + ], + "source": [ + "# 6 #\n", + "print(customer_orders)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "b3002afe-0caa-4af7-ac0c-5ce9cfe802a0", + "metadata": {}, + "outputs": [], + "source": [ + "# 7 #\n", + "\"\"\" Calculate the following order statistics:\n", + " - Total Products Ordered: The total number of products in the `customer_orders` set.\n", + " - Percentage of Products Ordered: The percentage of products ordered compared to the total available products.\n", + " \n", + " Store these statistics in a tuple called `order_status`.\n", + "\"\"\"\n", + "total_products_ordered = len(customer_orders)\n", + "print(total_products_ordered)\n", + "pct_products_ordered = 100*total_products_ordered/len(products)\n", + "pct_products_ordered" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "c4509feb-bb95-41d7-bd8d-6c604b2330be", + "metadata": {}, + "outputs": [], + "source": [ + "order_status = (total_products_ordered, pct_products_ordered)\n", + "order_status" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "069d867d-f276-4e88-8427-6703c345d78c", + "metadata": {}, + "outputs": [], + "source": [ + "# 8 # Print the order statistics using the following format:\n", + "'''\n", + " Order Statistics:\n", + " Total Products Ordered: \n", + " Percentage of Products Ordered: % \n", + " '''\n", + "# print(\"Your age after 10 years:\",age_after_10y)\n", + "# message = f\"Your age after 10 years is {age_after_10y}\"\n", + "\n", + "print(f\"Total Products Ordered: {total_products_ordered}\")\n", + "print(f\"Percentage of Products Ordered: {pct_products_ordered}%\")" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "fe3d9293-1905-4a79-848b-c389185f63bb", + "metadata": {}, + "outputs": [], + "source": [ + "# 9 # Update the inventory by subtracting 1 from the quantity of each product. Modify the `inventory` dictionary accordingly.\n", + "# inventory['t-shirt'] = q_tshirt - 1" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "d2104764-ccb9-46f0-8f9b-1bdb2fb2d9eb", + "metadata": {}, + "outputs": [], + "source": [ + "# inventory['mug'] = q_mug - 1\n", + "#inventory['hat'] = q_hat - 1\n", + "#inventory['book'] = q_book - 1\n", + "#inventory['keychain'] = q_keychain - 1" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "3e81472d-b298-4345-8a26-e1cdb61876c4", + "metadata": {}, + "outputs": [], + "source": [ + "# 10. # Print the updated inventory, displaying the quantity of each product on separate lines.\n", + "print(f\"Products in inventory: {inventory.keys()}\")\n", + "print(f\"Quantities in inventory: {inventory.values()}\")" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "92d8b680-44d6-445a-9d92-8ebd209fcfd3", + "metadata": {}, + "outputs": [], + "source": [ + "inventory" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "9d119865-5997-44b1-ad3d-76573e668474", + "metadata": {}, + "outputs": [], + "source": [ + "inventory.keys()" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "aa7015d8-e2d6-4621-9c4a-2031b46bb38b", + "metadata": {}, + "outputs": [], + "source": [ + "inventory.values()" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "1208e177-7199-4fd4-abb5-a1eda601cb85", + "metadata": {}, + "outputs": [], + "source": [ + "inventory['mug']\n" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "85b47107-799d-4eaf-bfb7-545ac980d7f3", + "metadata": {}, + "outputs": [], + "source": [ + "print(inventory)" + ] + }, + { + "cell_type": "code", + "execution_count": 11, + "id": "4a2acf6f-ee90-4bc5-bff6-b4c729eb4c28", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "{'t-shirt': 1, 'mug': 0, 'hat': 1, 'book': 4, 'keychain': 5}\n" + ] + } + ], + "source": [ + "## New code ## Lab Python flow control\n", + "# 3 # Instead of updating the inventory by subtracting 1 from the quantity of each product, only do it for the products that were ordered (those in \"customer_orders\").\n", + "for product in customer_orders:\n", + " #print(product)\n", + " inventory[product] -= 1\n", + " #print(inventory[product])\n", + "print(inventory)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "743cd24f-9e69-4cba-8102-43166afa9d27", + "metadata": {}, + "outputs": [], + "source": [ + "inventory" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "b09ccff1-34c5-4acf-8a2f-3cd2c89dec14", + "metadata": {}, + "outputs": [], + "source": [] + } + ], + "metadata": { + "kernelspec": { + "display_name": "Python [conda env:base] *", + "language": "python", + "name": "conda-base-py" + }, + "language_info": { + "codemirror_mode": { + "name": "ipython", + "version": 3 + }, + "file_extension": ".py", + "mimetype": "text/x-python", + "name": "python", + "nbconvert_exporter": "python", + "pygments_lexer": "ipython3", + "version": "3.13.9" + } + }, + "nbformat": 4, + "nbformat_minor": 5 +} diff --git a/lab-python-flow-control.ipynb b/lab-python-flow-control.ipynb index f4c7391..156076a 100644 --- a/lab-python-flow-control.ipynb +++ b/lab-python-flow-control.ipynb @@ -37,13 +37,457 @@ "\n", "3. Instead of updating the inventory by subtracting 1 from the quantity of each product, only do it for the products that were ordered (those in \"customer_orders\")." ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "f822ed3b-b18c-4a14-a47a-f79d5f898c80", + "metadata": {}, + "outputs": [], + "source": [ + "# 1 # list\n", + "#products = [\"t-shirt\",\"mug\",\"hat\",\"book\",\"keychain\"]\n", + "#products" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "1eca6713-b92a-4951-ac7a-1c9ddb527fe0", + "metadata": {}, + "outputs": [], + "source": [] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "ed07d074-e4e2-45e3-972b-a4d877074cee", + "metadata": {}, + "outputs": [], + "source": [ + "# 2 # Dictionary\n", + "#inventory = {}\n", + "#inventory" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "88967a0e-0961-4c41-89df-b44a7890e7e5", + "metadata": { + "scrolled": true + }, + "outputs": [], + "source": [ + "# 3 # Ask the user to input the quantity of each product available in the inventory. Use the product names from the products list as keys in the inventory dictionary and assign the respective quantities as values.\n", + "q_tshirt = int(input(\"Enter the quantity of available T-shirts: \"))\n", + "q_mug = int(input(\"Enter the quantity of available mugs: \"))\n", + "q_hat = int(input(\"Enter the quantity of available hats: \"))\n", + "q_book = int(input(\"Enter the quantity of available books: \"))\n", + "q_keychain = int(input(\"Enter the quantity of available keychains: \"))" + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "id": "277d17d1-d47a-45dd-b2a6-78d004e9ab51", + "metadata": {}, + "outputs": [ + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Enter the available quantity of the product: 1\n", + "Enter the available quantity of the product: 2\n", + "Enter the available quantity of the product: 3\n", + "Enter the available quantity of the product: 4\n", + "Enter the available quantity of the product: 5\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "{'t-shirt': 1, 'mug': 2, 'hat': 3, 'book': 4, 'keychain': 5}\n" + ] + } + ], + "source": [ + "## New code ## Lab Python flow control\n", + "## 1 ##\n", + "products = [\"t-shirt\",\"mug\",\"hat\",\"book\",\"keychain\"]\n", + "inventory = {}\n", + "for i in products:\n", + " inventory[i] = int(input(\"Enter the available quantity of the product: \"))\n", + "\n", + "print(inventory)\n", + "\n" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "a2c29abd-78f7-4fd2-aa63-d124bd510f3a", + "metadata": {}, + "outputs": [], + "source": [] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "4cd654c7-6bf9-4521-9dda-981ddde4a34a", + "metadata": {}, + "outputs": [], + "source": [ + "#inventory = {products[0]:q_tshirt, products[1]:q_mug, products[2]:q_hat, products[3]:q_book, products[4]:q_keychain }" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "87a8eb43-88ea-43ec-9aad-b313513bdf91", + "metadata": {}, + "outputs": [], + "source": [ + "print(inventory)\n", + "type(inventory)" + ] + }, + { + "cell_type": "code", + "execution_count": 4, + "id": "d58546ea-2a44-4264-a8e2-db6fcf564436", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "set" + ] + }, + "execution_count": 4, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "# 4 # Create an empty set called customer_orders\n", + "customer_orders = set()\n", + "type(customer_orders)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "776d46f7-b401-426a-a357-dde5b29b3e54", + "metadata": {}, + "outputs": [], + "source": [ + "# 5 # Ask the user to input the name of three products that a customer wants to order (from those in the products list, meaning three products out of \"t-shirt\", \"mug\", \"hat\", \"book\" or \"keychain\". Add each product name to the `customer_orders` set.\n", + "#ordered_product_1 = input(\"Enter the name of the first product of the order: \")\n", + "#ordered_product_2 = input(\"Enter the name of the second product of the order: \")\n", + "#ordered_product_3 = input(\"Enter the name of the third product of the order: \")" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "3fc1e92b-6e54-4ebf-9527-e6fd3cc74074", + "metadata": {}, + "outputs": [], + "source": [ + "customer_orders" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "14486de4-fa76-462b-9729-c067a23d7da3", + "metadata": {}, + "outputs": [], + "source": [ + "#customer_orders.add(ordered_product_1)\n", + "#customer_orders.add(ordered_product_2)\n", + "#customer_orders.add(ordered_product_3)\n", + "#customer_orders" + ] + }, + { + "cell_type": "code", + "execution_count": 8, + "id": "499653e7-02b3-4d5d-aaae-6539494e8ac8", + "metadata": {}, + "outputs": [ + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Enter the name of the first ordered product: hat\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "{'hat'}\n" + ] + }, + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Do you want to add another product? Answer Yes/No: Yes\n", + "Enter the name of the next ordered product: mug\n", + "Do you want to add another product? Answer Yes/No: Yes\n", + "Enter the name of the next ordered product: hat\n", + "Do you want to add another product? Answer Yes/No: No\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "{'mug', 'hat'}\n" + ] + } + ], + "source": [ + "### New code #### Lab Python flow control\n", + "## 2 ###\n", + "\n", + "first_order = input(\"Enter the name of the first ordered product: \")\n", + "customer_orders.add(first_order)\n", + "print(customer_orders)\n", + "answer=input(\"Do you want to add another product? Answer Yes/No: \")\n", + "while answer =='Yes':\n", + " order = input(\"Enter the name of the next ordered product: \")\n", + " customer_orders.add(order)\n", + " answer=input(\"Do you want to add another product? Answer Yes/No: \")\n", + " \n", + "print(customer_orders) \n", + " " + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "0f3cea4c-b0d8-4ceb-8a9c-e482ff828dd6", + "metadata": {}, + "outputs": [], + "source": [] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "0145c2b6-ba63-4093-8841-0ac58ba88efa", + "metadata": {}, + "outputs": [], + "source": [] + }, + { + "cell_type": "code", + "execution_count": 10, + "id": "7c6e436c-476f-4e4f-ba77-760b0383814e", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "{'mug', 'hat'}\n" + ] + } + ], + "source": [ + "# 6 #\n", + "print(customer_orders)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "b3002afe-0caa-4af7-ac0c-5ce9cfe802a0", + "metadata": {}, + "outputs": [], + "source": [ + "# 7 #\n", + "\"\"\" Calculate the following order statistics:\n", + " - Total Products Ordered: The total number of products in the `customer_orders` set.\n", + " - Percentage of Products Ordered: The percentage of products ordered compared to the total available products.\n", + " \n", + " Store these statistics in a tuple called `order_status`.\n", + "\"\"\"\n", + "total_products_ordered = len(customer_orders)\n", + "print(total_products_ordered)\n", + "pct_products_ordered = 100*total_products_ordered/len(products)\n", + "pct_products_ordered" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "c4509feb-bb95-41d7-bd8d-6c604b2330be", + "metadata": {}, + "outputs": [], + "source": [ + "order_status = (total_products_ordered, pct_products_ordered)\n", + "order_status" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "069d867d-f276-4e88-8427-6703c345d78c", + "metadata": {}, + "outputs": [], + "source": [ + "# 8 # Print the order statistics using the following format:\n", + "'''\n", + " Order Statistics:\n", + " Total Products Ordered: \n", + " Percentage of Products Ordered: % \n", + " '''\n", + "# print(\"Your age after 10 years:\",age_after_10y)\n", + "# message = f\"Your age after 10 years is {age_after_10y}\"\n", + "\n", + "print(f\"Total Products Ordered: {total_products_ordered}\")\n", + "print(f\"Percentage of Products Ordered: {pct_products_ordered}%\")" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "fe3d9293-1905-4a79-848b-c389185f63bb", + "metadata": {}, + "outputs": [], + "source": [ + "# 9 # Update the inventory by subtracting 1 from the quantity of each product. Modify the `inventory` dictionary accordingly.\n", + "# inventory['t-shirt'] = q_tshirt - 1" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "d2104764-ccb9-46f0-8f9b-1bdb2fb2d9eb", + "metadata": {}, + "outputs": [], + "source": [ + "# inventory['mug'] = q_mug - 1\n", + "#inventory['hat'] = q_hat - 1\n", + "#inventory['book'] = q_book - 1\n", + "#inventory['keychain'] = q_keychain - 1" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "3e81472d-b298-4345-8a26-e1cdb61876c4", + "metadata": {}, + "outputs": [], + "source": [ + "# 10. # Print the updated inventory, displaying the quantity of each product on separate lines.\n", + "print(f\"Products in inventory: {inventory.keys()}\")\n", + "print(f\"Quantities in inventory: {inventory.values()}\")" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "92d8b680-44d6-445a-9d92-8ebd209fcfd3", + "metadata": {}, + "outputs": [], + "source": [ + "inventory" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "9d119865-5997-44b1-ad3d-76573e668474", + "metadata": {}, + "outputs": [], + "source": [ + "inventory.keys()" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "aa7015d8-e2d6-4621-9c4a-2031b46bb38b", + "metadata": {}, + "outputs": [], + "source": [ + "inventory.values()" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "1208e177-7199-4fd4-abb5-a1eda601cb85", + "metadata": {}, + "outputs": [], + "source": [ + "inventory['mug']\n" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "85b47107-799d-4eaf-bfb7-545ac980d7f3", + "metadata": {}, + "outputs": [], + "source": [ + "print(inventory)" + ] + }, + { + "cell_type": "code", + "execution_count": 11, + "id": "4a2acf6f-ee90-4bc5-bff6-b4c729eb4c28", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "{'t-shirt': 1, 'mug': 0, 'hat': 1, 'book': 4, 'keychain': 5}\n" + ] + } + ], + "source": [ + "## New code ## Lab Python flow control\n", + "# 3 # Instead of updating the inventory by subtracting 1 from the quantity of each product, only do it for the products that were ordered (those in \"customer_orders\").\n", + "for product in customer_orders:\n", + " #print(product)\n", + " inventory[product] -= 1\n", + " #print(inventory[product])\n", + "print(inventory)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "743cd24f-9e69-4cba-8102-43166afa9d27", + "metadata": {}, + "outputs": [], + "source": [ + "inventory" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "b09ccff1-34c5-4acf-8a2f-3cd2c89dec14", + "metadata": {}, + "outputs": [], + "source": [] } ], "metadata": { "kernelspec": { - "display_name": "Python 3 (ipykernel)", + "display_name": "Python [conda env:base] *", "language": "python", - "name": "python3" + "name": "conda-base-py" }, "language_info": { "codemirror_mode": { @@ -55,7 +499,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.9.13" + "version": "3.13.9" } }, "nbformat": 4,