diff --git a/lab-python-flow-control.ipynb b/lab-python-flow-control.ipynb index f4c7391..1c7f7f8 100644 --- a/lab-python-flow-control.ipynb +++ b/lab-python-flow-control.ipynb @@ -37,11 +37,97 @@ "\n", "3. Instead of updating the inventory by subtracting 1 from the quantity of each product, only do it for the products that were ordered (those in \"customer_orders\")." ] + }, + { + "cell_type": "code", + "execution_count": 1, + "id": "738c9e69", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "\n", + "Please enter the quantity of the following products:\n", + "Customer orders: set()\n", + "\n", + "Order Statistics:\n", + "Percentage of Products ordered: 0.00%\n", + "\n", + "Updated inventory:\n", + "t-shirt: 2\n", + "mug: 3\n", + "hat: 2\n", + "book: 10\n", + "keychain: 2\n" + ] + } + ], + "source": [ + "product = [\"t-shirt\", \"mug\", \"hat\", \"book\", \"keychain\"]\n", + "inventory = {}\n", + "\n", + "print(\"\\nPlease enter the quantity of the following products:\")\n", + "\n", + "for item in product:\n", + " while True: # Loop until valid input is provided\n", + " quantity_str = input(f\"{item}: \")\n", + " try:\n", + " quantity = int(quantity_str)\n", + " if quantity >= 0: # Ensure the quantity is a non-negative number\n", + " inventory[item] = quantity\n", + " break # Exit the inner loop if input is valid\n", + " else:\n", + " print(\"Please enter a non-negative number for the quantity.\")\n", + " except ValueError:\n", + " print(\"Invalid input. Please enter a valid integer.\")\n", + "\n", + "# Customer order processing\n", + "customer_orders = set()\n", + "while True:\n", + " order = input(\"Enter a product to order (or 'no' to finish): \").lower()\n", + " if order == 'no':\n", + " break\n", + " if order in product:\n", + " customer_orders.add(order)\n", + " else:\n", + " print(\"This item is not available.\")\n", + "\n", + "print(f\"Customer orders: {customer_orders}\")\n", + "\n", + "# Order Statistics\n", + "ordered_list = len(customer_orders)\n", + "available_product = len(product)\n", + "percentage_ordered = (ordered_list / available_product) * 100\n", + "\n", + "print(\"\\nOrder Statistics:\")\n", + "print(f\"Percentage of Products ordered: {percentage_ordered:.2f}%\")\n", + "\n", + "# Updating the inventory (only for ordered items)\n", + "for ordered_item in customer_orders:\n", + " if ordered_item in inventory and inventory[ordered_item] > 0:\n", + " inventory[ordered_item] -= 1\n", + " elif ordered_item in inventory: # just checking whether the item is in the inventory\n", + " print(f\"{ordered_item} is out of stock.\")\n", + "\n", + "print(\"\\nUpdated inventory:\")\n", + "for item, quantity in inventory.items():\n", + " print(f\"{item}: {quantity}\")\n" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "eb6bfc9d", + "metadata": {}, + "outputs": [], + "source": [] } ], "metadata": { "kernelspec": { - "display_name": "Python 3 (ipykernel)", + "display_name": "Python 3", "language": "python", "name": "python3" }, @@ -55,7 +141,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.9.13" + "version": "3.12.9" } }, "nbformat": 4,