diff --git a/lab-python-error-handling.ipynb b/lab-python-error-handling.ipynb index f4c6ef6..ec0f236 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,386 @@ "\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": 88, + "id": "448f3d0b-f40d-410e-a828-e622cab628d7", + "metadata": {}, + "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", + " # 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 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": 96, + "id": "268e9b30-64fd-4190-b071-d30ea28ae10e", + "metadata": {}, + "outputs": [ + { + "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: 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": [ + "{'keychain'}" + ] + }, + "execution_count": 96, + "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", + " attempts=0\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 inventory[choice]!=0:\n", + " customer_orders.add(choice)\n", + " validity=True\n", + " else:\n", + " print('This product is no longer in stock. Please choose another product')\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)" + ] + }, + { + "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": 70, + "id": "04ceab96-7fe4-49c2-b9ed-4d5c28c6af5d", + "metadata": {}, + "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 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", + " 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": 79, + "id": "305efbe0-84d5-42eb-9b48-ba8d620557d1", + "metadata": {}, + "outputs": [ + { + "name": "stdin", + "output_type": "stream", + "text": [ + "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": [ + "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: mug\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Order Statistics\n", + "Total Products Ordered: 1\n", + "Percentage of Unique Products Ordered: 20.0%\n", + "Updated Inventory\n", + "mug: 11\n", + "hat: 2\n", + "book: 1\n", + "keychain: 2\n" + ] + }, + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Enter the price of mug: 34\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Total Price: 34\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=calculate_unit_price(customer_orders)\n", + "print(f\"Total Price: {total_price}\")" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "be9941b5-a375-4392-a550-718ab7d062a7", + "metadata": {}, + "outputs": [], + "source": [] } ], "metadata": { @@ -90,7 +470,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.9.13" + "version": "3.13.9" } }, "nbformat": 4,