From 85fab45277b234e3e84d0de493a7b01858302731 Mon Sep 17 00:00:00 2001 From: Claire Leyden Date: Thu, 7 May 2026 16:12:39 +0200 Subject: [PATCH 1/4] Solved lab --- lab-python-error-handling.ipynb | 475 +++++++++++++++++++++++++++++++- 1 file changed, 473 insertions(+), 2 deletions(-) diff --git a/lab-python-error-handling.ipynb b/lab-python-error-handling.ipynb index f4c6ef6..4b791f9 100644 --- a/lab-python-error-handling.ipynb +++ b/lab-python-error-handling.ipynb @@ -10,7 +10,7 @@ }, { "cell_type": "markdown", - "id": "bc99b386-7508-47a0-bcdb-d969deaf6c8b", + "id": "5292f7fe-c103-4852-b5e5-238fa3ee7f52", "metadata": {}, "source": [ "## Exercise: Error Handling for Managing Customer Orders\n", @@ -72,6 +72,477 @@ "\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": 36, + "id": "448f3d0b-f40d-410e-a828-e622cab628d7", + "metadata": {}, + "outputs": [ + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Enter the quantity of t-shirts available: 5\n", + "Enter the quantity of mugs available: shoe\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Invalid input, please try again\n" + ] + }, + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Enter the quantity of mugs available: 2\n", + "Enter the quantity of hats available: -1\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Invalid input, please try again\n" + ] + }, + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Enter the quantity of hats available: 12\n", + "Enter the quantity of books available: 4\n", + "Enter the quantity of keychains available: 23\n" + ] + } + ], + "source": [ + "def initialize_inventory(products):\n", + " # inventory = {product: int(input(f\"Enter the quantity of {product}s available: \")) for product in products}\n", + " # return inventory\n", + " for product in products:\n", + " valid=False\n", + " while not valid:\n", + " try:\n", + " quantity = input(f\"Enter the quantity of {product}s available:\")\n", + " if quantity.isdigit()==1 and int(quantity)>0:\n", + " inventory[product] = int(quantity)\n", + " valid=True\n", + " else:\n", + " print('Invalid input, please try again') \n", + " except:\n", + " ValueError as error:\n", + " print(f\"Error: {error}\")\n", + " return inventory\n", + "# products=[\"t-shirt\", \"mug\", \"hat\", \"book\", \"keychain\"]\n", + "# inventory=initialize_inventory(products)" + ] + }, + { + "cell_type": "code", + "execution_count": 66, + "id": "268e9b30-64fd-4190-b071-d30ea28ae10e", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "['t-shirt', 'hat', 'book', 'keychain', 'mug']\n" + ] + }, + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Enter the number of customer orders: 2\n", + "Enter the name of a product that a customer wants to order: 2\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Try again. Please choose from these options: ['t-shirt', 'hat', 'book', 'keychain', 'mug']\n" + ] + }, + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Enter the name of a product that a customer wants to order: hat a \n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Try again. Please choose from these options: ['t-shirt', 'hat', 'book', 'keychain', 'mug']\n" + ] + }, + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Enter the name of a product that a customer wants to order: hat\n", + "Enter the name of a product that a customer wants to order: hat\n" + ] + }, + { + "data": { + "text/plain": [ + "{'hat'}" + ] + }, + "execution_count": 66, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "#If the user enters an invalid product name (e.g., a product name that is not in the inventory), or that doesn't have stock available, \n", + "#display an error message and ask them to re-enter the product name. Hint: you will need to pass inventory as a parameter\n", + "#Use a try-except block to handle the error and continue prompting the user until a valid product name is entered.\n", + "\n", + "def get_customer_orders(inventory):\n", + " #options=[\"t-shirt\", \"mug\", \"hat\", \"book\", \"keychain\"]\n", + " options=list(inventory.keys())\n", + " itsanumber=False\n", + " while itsanumber==False:\n", + " num_orders=input('Enter the number of customer orders:')\n", + " if not num_orders.isdigit() or int(num_orders)<1:\n", + " print('Please enter a valid number of orders')\n", + " else:\n", + " itsanumber=True\n", + " num_orders=int(num_orders)\n", + " customer_orders=set()\n", + " for i in range(num_orders):\n", + " validity=False\n", + " while not validity:\n", + " choice= input('Enter the name of a product that a customer wants to order: ')\n", + " try:\n", + " if choice in options and inventory[choice]>0:\n", + " customer_orders.add(choice)\n", + " #inventory=update_inventory(choice,inventory)\n", + " validity=True\n", + " elif choice in options and inventory[choice]<1:\n", + " print('This product is no longer in stock. Please choose another product')\n", + " else:\n", + " print('Try again. Please choose from these options: ',options)\n", + " except ValueError as error:\n", + " print(f\"Error: {error}\") \n", + "#customer_orders={input('Enter the name of a product that a customer wants to order: ') for value in range(num_orders)}\n", + " return customer_orders\n", + "#get_customer_orders(inventory)" + ] + }, + { + "cell_type": "code", + "execution_count": 29, + "id": "d57a502c-396d-4708-a916-063103998114", + "metadata": {}, + "outputs": [], + "source": [ + "def update_inventory(customer_orders, inventory):\n", + " #inventory_decreased={key:(value-1 if key in customer_orders and value!=0 else value) for key,value in inventory.items() }\n", + " inventory_decreased={key:(value-1 if key in customer_orders else value) for key,value in inventory.items() }\n", + " inventory_clean={key:value for key,value in inventory_decreased.items() if value!=0}\n", + " # for i in customer_orders:\n", + " # if inventory[i]!=0 and i in inventory:\n", + " # inventory[i]-=1\n", + " return inventory_clean, inventory\n", + "#x=update_inventory(['mug', 'mug'], {'t-shirt': 1, 'mug': 2, 'hat': 1, 'book': 1, 'keychain': 1})" + ] + }, + { + "cell_type": "code", + "execution_count": 22, + "id": "5f8c8a4b-7a84-41c1-bf39-bf614ae70bae", + "metadata": {}, + "outputs": [], + "source": [ + "# Define a function named calculate_order_statistics that takes customer_orders and products as parameters. Inside the function, \n", + "# implement the code for calculating the order statistics (total products ordered, and percentage of unique products ordered). \n", + "# The function should return these values.\n", + "def calculate_order_statistics(customer_orders,products):\n", + " total_number_product_cats=len(products)\n", + " percentage_ordered=round(len(set(customer_orders))/total_number_product_cats*100,1)\n", + " #order_status=('Total Products Ordered:',total_number_products,'Percentage of Products Ordered:',percentage_ordered)\n", + " #print('Order Statistics')\n", + " #print(f\"Total Products Ordered: {total_number_products}\")\n", + " #print(f\"Percentage of Products Ordered: {percentage_ordered}%\" )\n", + " order_statistics= [len(customer_orders), percentage_ordered]\n", + " return order_statistics\n", + "#order_statistics=calculate_order_statistics(customer_orders,products)\n", + "# order_statistics\n" + ] + }, + { + "cell_type": "code", + "execution_count": 23, + "id": "10534142-9365-4a3c-a9fa-9af1e99b0ac9", + "metadata": {}, + "outputs": [], + "source": [ + "# Define a function named print_order_statistics that takes order_statistics as a parameter. Inside the function, implement the \n", + "# code for printing the order statistics.\n", + "def print_order_statistics(order_statistics):\n", + " #print(order_statistics[0])\n", + " total_number_product_cats=order_statistics[0]\n", + " percentage_ordered=order_statistics[1]\n", + " print('Order Statistics')\n", + " print(f\"Total Products Ordered: {total_number_product_cats}\")\n", + " print(f\"Percentage of Unique Products Ordered: {percentage_ordered}%\" )\n", + "#print_order_statistics(order_statistics)" + ] + }, + { + "cell_type": "code", + "execution_count": 25, + "id": "90d569d4-ded0-4c8c-9e80-12aaa2ce18dd", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Updated Inventory\n", + "t-shirt: 2\n", + "book: 1\n", + "keychain: 1\n" + ] + } + ], + "source": [ + "# Define a function named print_updated_inventory that takes inventory as a parameter. Inside the function, \n", + "# implement the code for printing the updated inventory.\n", + "def print_updated_inventory(inventory):\n", + " print(\"Updated Inventory\")\n", + " # for key,value in inventory.items():\n", + " # print(f\"{key}: {value}\")\n", + " [print(f\"{key}: {value}\") for key,value in inventory.items()]\n", + "#print_updated_inventory(inventory)" + ] + }, + { + "cell_type": "code", + "execution_count": 42, + "id": "04ceab96-7fe4-49c2-b9ed-4d5c28c6af5d", + "metadata": {}, + "outputs": [ + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Enter the price of mug: ha\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Invalid input, try again\n" + ] + }, + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Enter the price of mug: -1\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Invalid input, try again\n" + ] + }, + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Enter the price of mug: 0\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Invalid input, try again\n" + ] + }, + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Enter the price of mug: 8\n" + ] + }, + { + "data": { + "text/plain": [ + "8" + ] + }, + "execution_count": 42, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "# Add a new function to calculate the total price of the customer order. For each product in customer_orders, prompt the user to enter \n", + "# the price of that product. Use comprehension to calculate the total price. Note: assume that the user can only have 1 unit of each product.\n", + "def unit_price(customer_order):\n", + " #prices={item:input(f\"What is the price of {item}\") for item in customer_order}\n", + " #customer_order=set(customer_order)\n", + " for item in customer_order:\n", + " prices=[]\n", + " validity=False\n", + " while not validity:\n", + " try:\n", + " quantity=input(f\"Enter the price of {item}: \")\n", + " if quantity.isdigit()==1 and int(quantity)>0:\n", + " prices.append(int(quantity))\n", + " validity=True\n", + " else:\n", + " print('Invalid input, try again')\n", + " except ValueError as error:\n", + " print(f\"Error: {error}\")\n", + " price_sum=sum(prices) \n", + " #price_sum=float(sum([int(input(f\"Enter the price of {item}: \")) for item in customer_order]))\n", + " return price_sum\n", + " #print(f\"Total Price: {price_b}\")\n", + "#unit_price(customer_orders)" + ] + }, + { + "cell_type": "code", + "execution_count": 68, + "id": "305efbe0-84d5-42eb-9b48-ba8d620557d1", + "metadata": {}, + "outputs": [ + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Enter the quantity of t-shirts available: 2\n", + "Enter the quantity of mugs available: 3\n", + "Enter the quantity of hats available: 1\n", + "Enter the quantity of books available: d\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Invalid input, please try again\n" + ] + }, + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Enter the quantity of books available: 423\n", + "Enter the quantity of keychains available: 3\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "['t-shirt', 'hat', 'book', 'keychain', 'mug']\n" + ] + }, + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Enter the number of customer orders: 1\n", + "Enter the name of a product that a customer wants to order: mug\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "{'t-shirt': 2, 'hat': 1, 'book': 423, 'keychain': 3, 'mug': 2}\n", + "Order Statistics\n", + "Total Products Ordered: 1\n", + "Percentage of Unique Products Ordered: 20.0%\n", + "Updated Inventory\n", + "t-shirt: 2\n", + "hat: 1\n", + "book: 423\n", + "keychain: 3\n", + "mug: 2\n" + ] + }, + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Enter the price of mug: h\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Invalid input, try again\n" + ] + }, + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Enter the price of mug: -1\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Invalid input, try again\n" + ] + }, + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Enter the price of mug: 3\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Total Price: 3\n" + ] + } + ], + "source": [ + "products=[\"t-shirt\", \"mug\", \"hat\", \"book\", \"keychain\"]\n", + "inventory=initialize_inventory(products)\n", + "customer_orders=get_customer_orders(inventory)\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", + "total_price=unit_price(customer_orders)\n", + "print(f\"Total Price: {total_price}\")" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "8a0b5dae-afb1-484b-a6fe-f92af151aade", + "metadata": {}, + "outputs": [], + "source": [] } ], "metadata": { @@ -90,7 +561,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.9.13" + "version": "3.13.9" } }, "nbformat": 4, From 6afaf94f65d9a04efd14254c16887eb129fbf606 Mon Sep 17 00:00:00 2001 From: Claire Leyden Date: Thu, 7 May 2026 16:29:09 +0200 Subject: [PATCH 2/4] Solved lab --- lab-python-error-handling.ipynb | 238 ++++---------------------------- 1 file changed, 29 insertions(+), 209 deletions(-) diff --git a/lab-python-error-handling.ipynb b/lab-python-error-handling.ipynb index 4b791f9..1e6be1a 100644 --- a/lab-python-error-handling.ipynb +++ b/lab-python-error-handling.ipynb @@ -75,50 +75,10 @@ }, { "cell_type": "code", - "execution_count": 36, + "execution_count": 73, "id": "448f3d0b-f40d-410e-a828-e622cab628d7", "metadata": {}, - "outputs": [ - { - "name": "stdin", - "output_type": "stream", - "text": [ - "Enter the quantity of t-shirts available: 5\n", - "Enter the quantity of mugs available: shoe\n" - ] - }, - { - "name": "stdout", - "output_type": "stream", - "text": [ - "Invalid input, please try again\n" - ] - }, - { - "name": "stdin", - "output_type": "stream", - "text": [ - "Enter the quantity of mugs available: 2\n", - "Enter the quantity of hats available: -1\n" - ] - }, - { - "name": "stdout", - "output_type": "stream", - "text": [ - "Invalid input, please try again\n" - ] - }, - { - "name": "stdin", - "output_type": "stream", - "text": [ - "Enter the quantity of hats available: 12\n", - "Enter the quantity of books available: 4\n", - "Enter the quantity of keychains available: 23\n" - ] - } - ], + "outputs": [], "source": [ "def initialize_inventory(products):\n", " # inventory = {product: int(input(f\"Enter the quantity of {product}s available: \")) for product in products}\n", @@ -128,13 +88,12 @@ " while not valid:\n", " try:\n", " quantity = input(f\"Enter the quantity of {product}s available:\")\n", - " if quantity.isdigit()==1 and int(quantity)>0:\n", + " if quantity.isdigit()==1 and int(quantity)>=0:\n", " inventory[product] = int(quantity)\n", " valid=True\n", " else:\n", " print('Invalid input, please try again') \n", - " except:\n", - " ValueError as error:\n", + " except ValueError as error:\n", " print(f\"Error: {error}\")\n", " return inventory\n", "# products=[\"t-shirt\", \"mug\", \"hat\", \"book\", \"keychain\"]\n", @@ -143,61 +102,25 @@ }, { "cell_type": "code", - "execution_count": 66, + "execution_count": 75, "id": "268e9b30-64fd-4190-b071-d30ea28ae10e", "metadata": {}, "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "['t-shirt', 'hat', 'book', 'keychain', 'mug']\n" - ] - }, - { - "name": "stdin", - "output_type": "stream", - "text": [ - "Enter the number of customer orders: 2\n", - "Enter the name of a product that a customer wants to order: 2\n" - ] - }, - { - "name": "stdout", - "output_type": "stream", - "text": [ - "Try again. Please choose from these options: ['t-shirt', 'hat', 'book', 'keychain', 'mug']\n" - ] - }, - { - "name": "stdin", - "output_type": "stream", - "text": [ - "Enter the name of a product that a customer wants to order: hat a \n" - ] - }, - { - "name": "stdout", - "output_type": "stream", - "text": [ - "Try again. Please choose from these options: ['t-shirt', 'hat', 'book', 'keychain', 'mug']\n" - ] - }, { "name": "stdin", "output_type": "stream", "text": [ - "Enter the name of a product that a customer wants to order: hat\n", - "Enter the name of a product that a customer wants to order: hat\n" + "Enter the number of customer orders: 1\n", + "Enter the name of a product that a customer wants to order: mug\n" ] }, { "data": { "text/plain": [ - "{'hat'}" + "{'mug'}" ] }, - "execution_count": 66, + "execution_count": 75, "metadata": {}, "output_type": "execute_result" } @@ -329,74 +252,14 @@ }, { "cell_type": "code", - "execution_count": 42, + "execution_count": 70, "id": "04ceab96-7fe4-49c2-b9ed-4d5c28c6af5d", "metadata": {}, - "outputs": [ - { - "name": "stdin", - "output_type": "stream", - "text": [ - "Enter the price of mug: ha\n" - ] - }, - { - "name": "stdout", - "output_type": "stream", - "text": [ - "Invalid input, try again\n" - ] - }, - { - "name": "stdin", - "output_type": "stream", - "text": [ - "Enter the price of mug: -1\n" - ] - }, - { - "name": "stdout", - "output_type": "stream", - "text": [ - "Invalid input, try again\n" - ] - }, - { - "name": "stdin", - "output_type": "stream", - "text": [ - "Enter the price of mug: 0\n" - ] - }, - { - "name": "stdout", - "output_type": "stream", - "text": [ - "Invalid input, try again\n" - ] - }, - { - "name": "stdin", - "output_type": "stream", - "text": [ - "Enter the price of mug: 8\n" - ] - }, - { - "data": { - "text/plain": [ - "8" - ] - }, - "execution_count": 42, - "metadata": {}, - "output_type": "execute_result" - } - ], + "outputs": [], "source": [ "# Add a new function to calculate the total price of the customer order. For each product in customer_orders, prompt the user to enter \n", "# the price of that product. Use comprehension to calculate the total price. Note: assume that the user can only have 1 unit of each product.\n", - "def unit_price(customer_order):\n", + "def calculate_unit_price(customer_order):\n", " #prices={item:input(f\"What is the price of {item}\") for item in customer_order}\n", " #customer_order=set(customer_order)\n", " for item in customer_order:\n", @@ -421,7 +284,7 @@ }, { "cell_type": "code", - "execution_count": 68, + "execution_count": 79, "id": "305efbe0-84d5-42eb-9b48-ba8d620557d1", "metadata": {}, "outputs": [ @@ -429,39 +292,26 @@ "name": "stdin", "output_type": "stream", "text": [ - "Enter the quantity of t-shirts available: 2\n", - "Enter the quantity of mugs available: 3\n", - "Enter the quantity of hats available: 1\n", - "Enter the quantity of books available: d\n" - ] - }, - { - "name": "stdout", - "output_type": "stream", - "text": [ - "Invalid input, please try again\n" - ] - }, - { - "name": "stdin", - "output_type": "stream", - "text": [ - "Enter the quantity of books available: 423\n", - "Enter the quantity of keychains available: 3\n" + "Enter the quantity of t-shirts available: 0\n", + "Enter the quantity of mugs available: 12\n", + "Enter the quantity of hats available: 2\n", + "Enter the quantity of books available: 1\n", + "Enter the quantity of keychains available: 2\n", + "Enter the number of customer orders: 1\n", + "Enter the name of a product that a customer wants to order: t-shirt\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ - "['t-shirt', 'hat', 'book', 'keychain', 'mug']\n" + "This product is no longer in stock. Please choose another product\n" ] }, { "name": "stdin", "output_type": "stream", "text": [ - "Enter the number of customer orders: 1\n", "Enter the name of a product that a customer wants to order: mug\n" ] }, @@ -469,58 +319,28 @@ "name": "stdout", "output_type": "stream", "text": [ - "{'t-shirt': 2, 'hat': 1, 'book': 423, 'keychain': 3, 'mug': 2}\n", "Order Statistics\n", "Total Products Ordered: 1\n", "Percentage of Unique Products Ordered: 20.0%\n", "Updated Inventory\n", - "t-shirt: 2\n", - "hat: 1\n", - "book: 423\n", - "keychain: 3\n", - "mug: 2\n" - ] - }, - { - "name": "stdin", - "output_type": "stream", - "text": [ - "Enter the price of mug: h\n" - ] - }, - { - "name": "stdout", - "output_type": "stream", - "text": [ - "Invalid input, try again\n" - ] - }, - { - "name": "stdin", - "output_type": "stream", - "text": [ - "Enter the price of mug: -1\n" - ] - }, - { - "name": "stdout", - "output_type": "stream", - "text": [ - "Invalid input, try again\n" + "mug: 11\n", + "hat: 2\n", + "book: 1\n", + "keychain: 2\n" ] }, { "name": "stdin", "output_type": "stream", "text": [ - "Enter the price of mug: 3\n" + "Enter the price of mug: 34\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ - "Total Price: 3\n" + "Total Price: 34\n" ] } ], @@ -532,14 +352,14 @@ "order_statistics=calculate_order_statistics(customer_orders,products)\n", "print_order_statistics(order_statistics)\n", "print_updated_inventory(inventory)\n", - "total_price=unit_price(customer_orders)\n", + "total_price=calculate_unit_price(customer_orders)\n", "print(f\"Total Price: {total_price}\")" ] }, { "cell_type": "code", "execution_count": null, - "id": "8a0b5dae-afb1-484b-a6fe-f92af151aade", + "id": "be9941b5-a375-4392-a550-718ab7d062a7", "metadata": {}, "outputs": [], "source": [] From f92d8b687447d4039e5605c00e272865cbda7120 Mon Sep 17 00:00:00 2001 From: Claire Leyden Date: Thu, 7 May 2026 17:03:41 +0200 Subject: [PATCH 3/4] Solved lab --- lab-python-error-handling.ipynb | 132 ++++++++++++++++++++++++++++---- 1 file changed, 117 insertions(+), 15 deletions(-) diff --git a/lab-python-error-handling.ipynb b/lab-python-error-handling.ipynb index 1e6be1a..c2e64f9 100644 --- a/lab-python-error-handling.ipynb +++ b/lab-python-error-handling.ipynb @@ -75,10 +75,22 @@ }, { "cell_type": "code", - "execution_count": 73, + "execution_count": 88, "id": "448f3d0b-f40d-410e-a828-e622cab628d7", "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Enter the quantity of t-shirts available: 0\n", + "Enter the quantity of mugs available: 0\n", + "Enter the quantity of hats available: 0\n", + "Enter the quantity of books available: 0\n", + "Enter the quantity of keychains available: 1\n" + ] + } + ], "source": [ "def initialize_inventory(products):\n", " # inventory = {product: int(input(f\"Enter the quantity of {product}s available: \")) for product in products}\n", @@ -97,12 +109,12 @@ " print(f\"Error: {error}\")\n", " return inventory\n", "# products=[\"t-shirt\", \"mug\", \"hat\", \"book\", \"keychain\"]\n", - "# inventory=initialize_inventory(products)" + "inventory=initialize_inventory(products)" ] }, { "cell_type": "code", - "execution_count": 75, + "execution_count": 96, "id": "268e9b30-64fd-4190-b071-d30ea28ae10e", "metadata": {}, "outputs": [ @@ -111,16 +123,93 @@ "output_type": "stream", "text": [ "Enter the number of customer orders: 1\n", - "Enter the name of a product that a customer wants to order: mug\n" + "Enter the name of a product that a customer wants to order: hak\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Try again. Please choose from these options: ['mug', 'hat', 'book', 'keychain', 't-shirt']\n" + ] + }, + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Enter the name of a product that a customer wants to order: kasf\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Try again. Please choose from these options: ['mug', 'hat', 'book', 'keychain', 't-shirt']\n" + ] + }, + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Enter the name of a product that a customer wants to order: hat\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "This product is no longer in stock. Please choose another product\n" + ] + }, + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Enter the name of a product that a customer wants to order: hat\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "This product is no longer in stock. Please choose another product\n" + ] + }, + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Enter the name of a product that a customer wants to order: hat\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "This product is no longer in stock. Please choose another product\n" + ] + }, + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Enter the name of a product that a customer wants to order: keychain\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "1\n" ] }, { "data": { "text/plain": [ - "{'mug'}" + "{'keychain'}" ] }, - "execution_count": 75, + "execution_count": 96, "metadata": {}, "output_type": "execute_result" } @@ -134,12 +223,17 @@ " #options=[\"t-shirt\", \"mug\", \"hat\", \"book\", \"keychain\"]\n", " options=list(inventory.keys())\n", " itsanumber=False\n", - " while itsanumber==False:\n", + " attempts=0\n", + " while itsanumber==False and attempts<5:\n", " num_orders=input('Enter the number of customer orders:')\n", " if not num_orders.isdigit() or int(num_orders)<1:\n", " print('Please enter a valid number of orders')\n", + " attempts+=1\n", + " #print(attempts)\n", " else:\n", " itsanumber=True\n", + " if attempts==5:\n", + " raise ValueError(\"You inputted too many incorrect values\")\n", " num_orders=int(num_orders)\n", " customer_orders=set()\n", " for i in range(num_orders):\n", @@ -147,18 +241,26 @@ " while not validity:\n", " choice= input('Enter the name of a product that a customer wants to order: ')\n", " try:\n", - " if choice in options and inventory[choice]>0:\n", + " if inventory[choice]!=0:\n", " customer_orders.add(choice)\n", - " #inventory=update_inventory(choice,inventory)\n", " validity=True\n", - " elif choice in options and inventory[choice]<1:\n", - " print('This product is no longer in stock. Please choose another product')\n", + " #print(inventory[choice])\n", " else:\n", - " print('Try again. Please choose from these options: ',options)\n", - " except ValueError as error:\n", - " print(f\"Error: {error}\") \n", + " print('This product is no longer in stock. Please choose another product')\n", + " # if choice in options and inventory[choice]>0:\n", + " # customer_orders.add(choice)\n", + " # #inventory=update_inventory(choice,inventory)\n", + " # validity=True\n", + " # elif choice in options and inventory[choice]<1:\n", + " # print('This product is no longer in stock. Please choose another product')\n", + " # else:\n", + " # print('Try again. Please choose from these options: ',options)\n", + " except KeyError as error:\n", + " print('Try again. Please choose from these options: ',options)\n", + " #print(f\"Error: {error}\") \n", "#customer_orders={input('Enter the name of a product that a customer wants to order: ') for value in range(num_orders)}\n", " return customer_orders\n", + "# print(inventory)\n", "#get_customer_orders(inventory)" ] }, From 5d4a8515b54f5cd162f743eeffbd485eca9d8eba Mon Sep 17 00:00:00 2001 From: Claire Leyden Date: Thu, 7 May 2026 17:26:19 +0200 Subject: [PATCH 4/4] Solved lab --- lab-python-error-handling.ipynb | 15 +-------------- 1 file changed, 1 insertion(+), 14 deletions(-) diff --git a/lab-python-error-handling.ipynb b/lab-python-error-handling.ipynb index c2e64f9..ec0f236 100644 --- a/lab-python-error-handling.ipynb +++ b/lab-python-error-handling.ipynb @@ -224,16 +224,12 @@ " options=list(inventory.keys())\n", " itsanumber=False\n", " attempts=0\n", - " while itsanumber==False and attempts<5:\n", + " while itsanumber==False:\n", " num_orders=input('Enter the number of customer orders:')\n", " if not num_orders.isdigit() or int(num_orders)<1:\n", " print('Please enter a valid number of orders')\n", - " attempts+=1\n", - " #print(attempts)\n", " else:\n", " itsanumber=True\n", - " if attempts==5:\n", - " raise ValueError(\"You inputted too many incorrect values\")\n", " num_orders=int(num_orders)\n", " customer_orders=set()\n", " for i in range(num_orders):\n", @@ -244,17 +240,8 @@ " if inventory[choice]!=0:\n", " customer_orders.add(choice)\n", " validity=True\n", - " #print(inventory[choice])\n", " else:\n", " print('This product is no longer in stock. Please choose another product')\n", - " # if choice in options and inventory[choice]>0:\n", - " # customer_orders.add(choice)\n", - " # #inventory=update_inventory(choice,inventory)\n", - " # validity=True\n", - " # elif choice in options and inventory[choice]<1:\n", - " # print('This product is no longer in stock. Please choose another product')\n", - " # else:\n", - " # print('Try again. Please choose from these options: ',options)\n", " except KeyError as error:\n", " print('Try again. Please choose from these options: ',options)\n", " #print(f\"Error: {error}\") \n",