From 191c8ceff488ac65557d073063eb593e67a71b0e Mon Sep 17 00:00:00 2001 From: Sardine Date: Mon, 24 Nov 2025 01:07:01 +0100 Subject: [PATCH 1/4] lab completed --- lab-python-error-handling.ipynb | 252 +++++++++++++++++++++++++++++++- 1 file changed, 251 insertions(+), 1 deletion(-) diff --git a/lab-python-error-handling.ipynb b/lab-python-error-handling.ipynb index f4c6ef6..71cee0e 100644 --- a/lab-python-error-handling.ipynb +++ b/lab-python-error-handling.ipynb @@ -72,6 +72,256 @@ "\n", "4. Test your code by running the program and deliberately entering invalid quantities and product names. Make sure the error handling mechanism works as expected.\n" ] + }, + { + "cell_type": "code", + "execution_count": 1, + "id": "8465b3be-559e-4ee1-9322-6c550dd66de8", + "metadata": {}, + "outputs": [], + "source": [ + "#1\n", + "products=[\"t-shirt\", \"mug\", \"hat\", \"book\", \"keychain\"]\n", + "def initialize_inventory(products):\n", + " inventory={product: int(input(f\"quantity of {product}:\")) for product in products}\n", + " return inventory\n", + "#initialize_inventory(products)" + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "id": "c63957b1-80e5-4d2b-ad48-609ae4e67b89", + "metadata": {}, + "outputs": [], + "source": [ + "#2\n", + "def get_customer_orders():\n", + " customer_orders=set()\n", + " NumberOrders = int(input(f\"number of orders:\"))\n", + " while NumberOrders <= 0:\n", + " print( f\"enter a correct order number\")\n", + " NumberOrders = int(input(f\"number of orders:\"))\n", + " while NumberOrders>0:\n", + " customer_product=input(f\"add a product from the list {products}:\") \n", + " customer_orders.update({customer_product} if customer_product in products else {})\n", + " NumberOrders -= 1 if customer_product in products else 0\n", + " return customer_orders\n", + "#get_customer_orders()" + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "id": "0813dc4f-ebc6-48ff-b5c6-79bfdb6154cc", + "metadata": {}, + "outputs": [ + { + "name": "stdin", + "output_type": "stream", + "text": [ + "quantity of t-shirt: 5\n", + "quantity of mug: 5\n", + "quantity of hat: 5\n", + "quantity of book: 5\n", + "quantity of keychain: 5\n", + "number of orders: 2\n", + "add a product from the list ['t-shirt', 'mug', 'hat', 'book', 'keychain']: mug\n", + "add a product from the list ['t-shirt', 'mug', 'hat', 'book', 'keychain']: hat\n", + "enter a price for hat: -1\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "invalid input\n" + ] + }, + { + "name": "stdin", + "output_type": "stream", + "text": [ + "enter a price for hat: 1\n", + "enter a price for mug: a\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "invalid input\n" + ] + }, + { + "name": "stdin", + "output_type": "stream", + "text": [ + "enter a price for mug: -6\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "invalid input\n" + ] + }, + { + "name": "stdin", + "output_type": "stream", + "text": [ + "enter a price for mug: 2\n" + ] + }, + { + "data": { + "text/plain": [ + "'total price is 3'" + ] + }, + "execution_count": 3, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "#3\n", + "inventory=initialize_inventory(products)\n", + "customer_orders=get_customer_orders()\n", + "def get_customer_orders_price(customer_orders):\n", + " CustomerPrices=[]\n", + " for product in customer_orders:\n", + " while True: \n", + " try:\n", + " price_str = input(f\"enter a price for {product}:\")\n", + " price= int(price_str)\n", + " if price <= 0:\n", + " raise ValueError (\"number must be positive\")\n", + " CustomerPrices.append(price)\n", + " break\n", + " except:\n", + " print(\"invalid input\")\n", + " return f\"total price is {sum(CustomerPrices)}\"\n", + "get_customer_orders_price(customer_orders)" + ] + }, + { + "cell_type": "markdown", + "id": "4d291cba-f559-440c-a6c6-22735fc3bcfe", + "metadata": {}, + "source": [ + "##### " + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "1183fe39-74bf-4714-90d4-fc9c1ebc41b2", + "metadata": {}, + "outputs": [ + { + "name": "stdin", + "output_type": "stream", + "text": [ + "quantity of t-shirt: 5\n", + "quantity of mug: 5\n", + "quantity of hat: 5\n", + "quantity of book: 5\n", + "quantity of keychain: 5\n", + "number of orders: 2\n" + ] + } + ], + "source": [ + "#4\n", + "inventory=initialize_inventory(products)\n", + "customer_orders=get_customer_orders()\n", + "def update_inventory(customer_orders, inventory):\n", + " CustOrderList=list(customer_orders)\n", + " inventory = {order:inventory[order]-CustOrderList.count(order) for order in inventory if inventory[order]-CustOrderList.count(order)!=0}\n", + " return print(f\"updated inventory: {inventory}\")\n", + " \n", + "#update_inventory(customer_orders, inventory)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "e32ee759-ef3c-4488-b7c2-8d636ab7b8db", + "metadata": {}, + "outputs": [], + "source": [ + "#5\n", + "def calculate_order_statistics(customer_orders, products):\n", + " number_orders=len(customer_orders)\n", + " ordered_products=f\"{int((number_orders/len(products))*100)}%\"\n", + " return number_orders, ordered_products\n", + "\n", + "#calculate_order_statistics(customer_orders, products)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "bc27ddd3-91b9-4059-90ce-064cb13415ab", + "metadata": {}, + "outputs": [], + "source": [ + "#6\n", + "order_statistics = calculate_order_statistics(customer_orders, products)\n", + "def print_order_statistics(order_statistics):\n", + " print(f\"number of products:\", order_statistics[0], \"products %:\", order_statistics[1])\n", + "\n", + "#print_order_statistics(order_statistics)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "16d68e8f-8776-4714-b15a-d18a9ae0560d", + "metadata": {}, + "outputs": [], + "source": [ + "#7\n", + "def print_updated_inventory(inventory):\n", + " return print(inventory)\n", + "#print_updated_inventory(inventory)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "aee5f95e-9f3d-473a-965f-cac55411c971", + "metadata": {}, + "outputs": [], + "source": [ + "#8\n", + "products=[\"t-shirt\", \"mug\", \"hat\", \"book\", \"keychain\"]\n", + "inventory=initialize_inventory(products)\n", + "customer_orders=get_customer_orders()\n", + "inventory=update_inventory(customer_orders, inventory)\n", + "order_statistics=calculate_order_statistics(customer_orders, products)\n", + "print_order_statistics(order_statistics)\n", + "print_updated_inventory(inventory)\n", + "get_customer_orders_price(customer_orders)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "b56143ef-d926-437e-9f2e-ae08b8cc6b33", + "metadata": {}, + "outputs": [], + "source": [] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "d31a79c6-efcf-49b9-8966-103126be583a", + "metadata": {}, + "outputs": [], + "source": [] } ], "metadata": { @@ -90,7 +340,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.9.13" + "version": "3.12.4" } }, "nbformat": 4, From 5e38cbc5993da156e8220d3b466c1028bd3e5e8c Mon Sep 17 00:00:00 2001 From: Sardine Date: Mon, 24 Nov 2025 01:14:49 +0100 Subject: [PATCH 2/4] commit test 2 --- lab-python-error-handling.ipynb | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/lab-python-error-handling.ipynb b/lab-python-error-handling.ipynb index 71cee0e..2252815 100644 --- a/lab-python-error-handling.ipynb +++ b/lab-python-error-handling.ipynb @@ -110,6 +110,16 @@ "#get_customer_orders()" ] }, + { + "cell_type": "code", + "execution_count": null, + "id": "de663730-e768-42af-87d7-9487ff84386b", + "metadata": {}, + "outputs": [], + "source": [ + "#d" + ] + }, { "cell_type": "code", "execution_count": 3, From bdbf6e9bbce513b2412e1ced2c3d0e3662dcde62 Mon Sep 17 00:00:00 2001 From: Sardine Date: Mon, 24 Nov 2025 09:19:41 +0100 Subject: [PATCH 3/4] a-gal lab error handling completed2 --- lab-python-error-handling.ipynb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lab-python-error-handling.ipynb b/lab-python-error-handling.ipynb index 2252815..38654a3 100644 --- a/lab-python-error-handling.ipynb +++ b/lab-python-error-handling.ipynb @@ -207,7 +207,7 @@ " price_str = input(f\"enter a price for {product}:\")\n", " price= int(price_str)\n", " if price <= 0:\n", - " raise ValueError (\"number must be positive\")\n", + " raise ValueError (\"number must be positive \")\n", " CustomerPrices.append(price)\n", " break\n", " except:\n", From 25fc56a133fcd18c3eee6ef70f9a228c47dfb5b4 Mon Sep 17 00:00:00 2001 From: Sardine Date: Mon, 24 Nov 2025 15:56:33 +0100 Subject: [PATCH 4/4] Laboratory with step 1 added --- lab-python-error-handling.ipynb | 65 ++++++++++++++++++++------------- 1 file changed, 39 insertions(+), 26 deletions(-) diff --git a/lab-python-error-handling.ipynb b/lab-python-error-handling.ipynb index 38654a3..2907498 100644 --- a/lab-python-error-handling.ipynb +++ b/lab-python-error-handling.ipynb @@ -73,6 +73,21 @@ "4. Test your code by running the program and deliberately entering invalid quantities and product names. Make sure the error handling mechanism works as expected.\n" ] }, + { + "cell_type": "code", + "execution_count": null, + "id": "da597fad-a0d3-49b5-9e55-951efa7b25ed", + "metadata": {}, + "outputs": [], + "source": [ + "######\n", + "products=[\"t-shirt\", \"mug\", \"hat\", \"book\", \"keychain\"]\n", + "def initialize_inventory(products):\n", + " inventory={product: int(input(f\"quantity of {product}:\")) for product in products}\n", + " return inventory\n", + "#initialize_inventory(products)" + ] + }, { "cell_type": "code", "execution_count": 1, @@ -83,7 +98,19 @@ "#1\n", "products=[\"t-shirt\", \"mug\", \"hat\", \"book\", \"keychain\"]\n", "def initialize_inventory(products):\n", - " inventory={product: int(input(f\"quantity of {product}:\")) for product in products}\n", + " inventory = {}\n", + " for product in products:\n", + " valid_input = False\n", + " while not valid_input:\n", + " try:\n", + " quantity = int(input(f\"Enter the quantity of {product}s available: \"))\n", + " if quantity >= 0:\n", + " inventory[product] = quantity\n", + " valid_input = True\n", + " else:\n", + " print(\"Quantity cannot be negative. Please enter a valid quantity.\")\n", + " except ValueError:\n", + " print(\"Invalid input. Please enter a valid quantity.\")\n", " return inventory\n", "#initialize_inventory(products)" ] @@ -130,30 +157,16 @@ "name": "stdin", "output_type": "stream", "text": [ - "quantity of t-shirt: 5\n", - "quantity of mug: 5\n", - "quantity of hat: 5\n", - "quantity of book: 5\n", - "quantity of keychain: 5\n", + "Enter the quantity of t-shirts available: 5\n", + "Enter the quantity of mugs available: 5\n", + "Enter the quantity of hats available: 5\n", + "Enter the quantity of books available: 5\n", + "Enter the quantity of keychains available: 5\n", "number of orders: 2\n", - "add a product from the list ['t-shirt', 'mug', 'hat', 'book', 'keychain']: mug\n", + "add a product from the list ['t-shirt', 'mug', 'hat', 'book', 'keychain']: book\n", "add a product from the list ['t-shirt', 'mug', 'hat', 'book', 'keychain']: hat\n", - "enter a price for hat: -1\n" - ] - }, - { - "name": "stdout", - "output_type": "stream", - "text": [ - "invalid input\n" - ] - }, - { - "name": "stdin", - "output_type": "stream", - "text": [ - "enter a price for hat: 1\n", - "enter a price for mug: a\n" + "enter a price for book: 5\n", + "enter a price for hat: '2\n" ] }, { @@ -167,7 +180,7 @@ "name": "stdin", "output_type": "stream", "text": [ - "enter a price for mug: -6\n" + "enter a price for hat: '1\n" ] }, { @@ -181,13 +194,13 @@ "name": "stdin", "output_type": "stream", "text": [ - "enter a price for mug: 2\n" + "enter a price for hat: 1\n" ] }, { "data": { "text/plain": [ - "'total price is 3'" + "'total price is 6'" ] }, "execution_count": 3,