From c114e1a3c42eba5eaba8b4c43f598bc93f86b857 Mon Sep 17 00:00:00 2001 From: Jessica Budwilowitz Date: Fri, 20 Jun 2025 21:14:17 +0200 Subject: [PATCH 1/2] Solved Lab --- lab-python-error-handling.ipynb | 174 +++++++++++++++++++++++++++++++- 1 file changed, 171 insertions(+), 3 deletions(-) diff --git a/lab-python-error-handling.ipynb b/lab-python-error-handling.ipynb index f4c6ef6..5e8390a 100644 --- a/lab-python-error-handling.ipynb +++ b/lab-python-error-handling.ipynb @@ -72,13 +72,181 @@ "\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": 4, + "id": "e4b708ac-1936-4982-818b-6e7e5a5cda8d", + "metadata": {}, + "outputs": [ + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Please enter stock for article t-shirt 1\n", + "Please enter stock for article mug 2\n", + "Please enter stock for article hat 3\n", + "Please enter stock for article book 4\n", + "Please enter stock for article keychain 5\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "{'t-shirt': 1, 'mug': 2, 'hat': 3, 'book': 4, 'keychain': 5}\n" + ] + }, + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Please enter the number of products you would like to order. 1\n", + "Please enter 0 for t-shirt, 1 for mug, 2 for hat, 3 for book or 4 for keychain 0\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "{'t-shirt'}\n", + "Order Statistics:\n", + " Total Products Ordered: 1\n", + " Percentage of Products Ordered: 20.0 %\n", + "['The stock for mug is 2', 'The stock for hat is 3', 'The stock for book is 4', 'The stock for keychain is 5']\n" + ] + }, + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Please enter the price of product t-shirt: 5\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "The total price of your order is: 5.0 EUR.\n" + ] + } + ], + "source": [ + "def initialize_inventory(products):\n", + " \"\"\"Initializing a dictionary for inventory input\"\"\"\n", + " inventory = {}\n", + " for product in products:\n", + " valid_input = False\n", + " while not valid_input:\n", + " try:\n", + " stock = int(input(f\"Please enter stock for article {product}\"))\n", + " if stock >= 0:\n", + " inventory[product] = stock\n", + " valid_input = True\n", + " else:\n", + " print(\"Invalid input. Please enter a valid quantity.\")\n", + " except:\n", + " print(\"Invalid input. Please enter a valid quantity.\")\n", + " return inventory\n", + "\n", + "def get_customer_orders():\n", + " \"\"\"Creates Set for customer order input\"\"\"\n", + " valid_input = False\n", + " while not valid_input:\n", + " try:\n", + " amount_products = int(input(\"Please enter the number of products you would like to order.\"))\n", + " if amount_products >= 0 and amount_products <=5:\n", + " valid_input = True\n", + " else:\n", + " print(\"Invalid input. Please enter a number between 1 and 5.\")\n", + " except:\n", + " print(\"Invalid input. Please enter a positive number.\")\n", + " customer_orders = set()\n", + " for order in range(amount_products):\n", + " valid_input = False\n", + " while not valid_input:\n", + " try:\n", + " number_articles = int(input(\"Please enter 0 for t-shirt, 1 for mug, 2 for hat, 3 for book or 4 for keychain\"))\n", + " if number_articles >= 0 and number_articles <= 4:\n", + " customer_orders.add(products[number_articles])\n", + " valid_input = True\n", + " else:\n", + " print(\"Please enter a number between 1 and 4.\")\n", + " except:\n", + " print(\"Invalid input. Please enter a number between 1 and 4.\")\n", + " return customer_orders\n", + "\n", + "def update_inventory(customer_orders, inventory):\n", + " inventory = {product: (stock - 1 if product in customer_orders else inventory[product]) for product, stock in inventory.items()}\n", + " inventory = {product: stock for product, stock in inventory.items() if stock > 0}\n", + " return inventory\n", + "\n", + "def calculate_order_statistics(customer_orders, products):\n", + " total_products_ordered = len(customer_orders)\n", + " percentage_ordered = (len(customer_orders)/len(products))*100\n", + " return(total_products_ordered, percentage_ordered)\n", + "\n", + "def print_order_statistics(order_statistics):\n", + " print(f\"\"\"Order Statistics:\n", + " Total Products Ordered: {order_statistics[0]}\n", + " Percentage of Products Ordered: {order_statistics[1]} %\"\"\")\n", + "\n", + "def print_updated_inventory(inventory):\n", + " updated_inventory = [f\"The stock for {product} is {inventory[product]}\" for product in inventory]\n", + " print(updated_inventory)\n", + "\n", + "def calculate_total_price(customer_orders):\n", + " \"\"\"Calculates total price of customer orders\"\"\"\n", + " total_price = []\n", + " for product in customer_orders:\n", + " valid_input = False\n", + " while not valid_input:\n", + " try:\n", + " price = float(input(f\"Please enter the price of product {product}: \"))\n", + " if price >= 0:\n", + " total_price.append(price)\n", + " valid_input = True\n", + " else:\n", + " print(\"Please enter a valid price.\")\n", + " except ValueError:\n", + " print(\"Please enter a valid price.\")\n", + " total_price = sum(total_price)\n", + " return total_price\n", + "\n", + "products = [\"t-shirt\", \"mug\", \"hat\", \"book\", \"keychain\"]\n", + "\n", + "inventory = initialize_inventory(products)\n", + "print(inventory)\n", + "\n", + "customer_orders = get_customer_orders()\n", + "print(customer_orders)\n", + "\n", + "order_statistics = calculate_order_statistics(customer_orders, products)\n", + "\n", + "print_order_statistics(order_statistics)\n", + "\n", + "inventory = update_inventory(customer_orders, inventory)\n", + "\n", + "print_updated_inventory(inventory)\n", + "\n", + "total_price = calculate_total_price(customer_orders)\n", + "print(f\"The total price of your order is: {total_price} EUR.\")\n" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "95aac34b-3feb-4d08-80e7-eaeaddcb43d7", + "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": { @@ -90,7 +258,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.9.13" + "version": "3.12.7" } }, "nbformat": 4, From 79bfffed3b8e112fd0bdeeafba912fe9ad69b182 Mon Sep 17 00:00:00 2001 From: Jessica Budwilowitz Date: Sun, 22 Jun 2025 21:00:17 +0200 Subject: [PATCH 2/2] Solved Lab --- lab-python-error-handling.ipynb | 52 +++++++++++++++++++++------------ 1 file changed, 34 insertions(+), 18 deletions(-) diff --git a/lab-python-error-handling.ipynb b/lab-python-error-handling.ipynb index 5e8390a..d6e9ed7 100644 --- a/lab-python-error-handling.ipynb +++ b/lab-python-error-handling.ipynb @@ -75,7 +75,7 @@ }, { "cell_type": "code", - "execution_count": 4, + "execution_count": 1, "id": "e4b708ac-1936-4982-818b-6e7e5a5cda8d", "metadata": {}, "outputs": [ @@ -101,33 +101,49 @@ "name": "stdin", "output_type": "stream", "text": [ - "Please enter the number of products you would like to order. 1\n", - "Please enter 0 for t-shirt, 1 for mug, 2 for hat, 3 for book or 4 for keychain 0\n" + "Please enter the number of products you would like to order. 2\n", + "Please enter the name of the product you would like to order. k\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ - "{'t-shirt'}\n", + "Invalid input. Please enter a valid product name.\n" + ] + }, + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Please enter the name of the product you would like to order. T-Shirt\n", + "Please enter the name of the product you would like to order. book\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "{'book', 't-shirt'}\n", "Order Statistics:\n", - " Total Products Ordered: 1\n", - " Percentage of Products Ordered: 20.0 %\n", - "['The stock for mug is 2', 'The stock for hat is 3', 'The stock for book is 4', 'The stock for keychain is 5']\n" + " Total Products Ordered: 2\n", + " Percentage of Products Ordered: 40.0 %\n", + "['The stock for mug is 2', 'The stock for hat is 3', 'The stock for book is 3', 'The stock for keychain is 5']\n" ] }, { "name": "stdin", "output_type": "stream", "text": [ - "Please enter the price of product t-shirt: 5\n" + "Please enter the price of product book: 10\n", + "Please enter the price of product t-shirt: 10\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ - "The total price of your order is: 5.0 EUR.\n" + "The total price of your order is: 20.0 EUR.\n" ] } ], @@ -149,7 +165,7 @@ " print(\"Invalid input. Please enter a valid quantity.\")\n", " return inventory\n", "\n", - "def get_customer_orders():\n", + "def get_customer_orders(inventory):\n", " \"\"\"Creates Set for customer order input\"\"\"\n", " valid_input = False\n", " while not valid_input:\n", @@ -162,18 +178,18 @@ " except:\n", " print(\"Invalid input. Please enter a positive number.\")\n", " customer_orders = set()\n", - " for order in range(amount_products):\n", + " for amount in range(amount_products):\n", " valid_input = False\n", " while not valid_input:\n", " try:\n", - " number_articles = int(input(\"Please enter 0 for t-shirt, 1 for mug, 2 for hat, 3 for book or 4 for keychain\"))\n", - " if number_articles >= 0 and number_articles <= 4:\n", - " customer_orders.add(products[number_articles])\n", + " articles_ordered = input(\"Please enter the name of the product you would like to order.\").lower()\n", + " if articles_ordered in inventory and inventory[articles_ordered] > 0:\n", + " customer_orders.add(articles_ordered)\n", " valid_input = True\n", " else:\n", - " print(\"Please enter a number between 1 and 4.\")\n", - " except:\n", - " print(\"Invalid input. Please enter a number between 1 and 4.\")\n", + " print(\"Invalid input. Please enter a valid product name.\")\n", + " except: \n", + " print(\"Invalid input. Please enter a valid product name.\")\n", " return customer_orders\n", "\n", "def update_inventory(customer_orders, inventory):\n", @@ -218,7 +234,7 @@ "inventory = initialize_inventory(products)\n", "print(inventory)\n", "\n", - "customer_orders = get_customer_orders()\n", + "customer_orders = get_customer_orders(inventory)\n", "print(customer_orders)\n", "\n", "order_statistics = calculate_order_statistics(customer_orders, products)\n",