diff --git a/lab-python-error-handling.ipynb b/lab-python-error-handling.ipynb index f4c6ef6..d6e9ed7 100644 --- a/lab-python-error-handling.ipynb +++ b/lab-python-error-handling.ipynb @@ -72,13 +72,197 @@ "\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": "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. 2\n", + "Please enter the name of the product you would like to order. k\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "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: 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 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: 20.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(inventory):\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 amount in range(amount_products):\n", + " valid_input = False\n", + " while not valid_input:\n", + " try:\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(\"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", + " 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(inventory)\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 +274,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.9.13" + "version": "3.12.7" } }, "nbformat": 4,