Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
111 changes: 109 additions & 2 deletions lab-python-flow-control.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,118 @@
"\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": null,
"id": "e5c00077",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"book is out of stock.\n",
"hat is out of stock.\n",
"book is out of stock.\n",
"hat is out of stock.\n",
"Customer orders: ['book', 'hat', 'keychain', 'mug', 't-shirt']\n",
"Order Statistics:\n",
"Total Products Ordered: 5\n",
"Percentage of Products Ordered: 7.936507936507936%\n",
"t-shirt: 55\n",
"mug: 2\n",
"hat: 0\n",
"book: 0\n",
"keychain: 1\n"
]
}
],
"source": [
"# I had probably misunderstood the previous task and already used the flow control there, \n",
"#but let's improve the code to make it reasonable for the actual shop environment.\n",
"\n",
"#Define the list of products\n",
"products = [\"t-shirt\", \"mug\", \"hat\", \"book\", \"keychain\"]\n",
"\n",
"# Create an inventory.\n",
"inventory = {}\n",
"\n",
"# Filling up investory with data on number of items\n",
"for product in products:\n",
" quantity = int(input(f\"Enter the quantity of {product}: \"))\n",
" inventory[product] = quantity\n",
"\n",
"# I will change the set to list for known reasons.\n",
"# If we use set, the customer can only order each product once.\n",
"customer_orders = list()\n",
"\n",
"#5. Now let's allow the customer to place orders for max three products.\n",
"y=10000000\n",
"n = 0\n",
"\n",
"# I will use a placeholder inventory to track the available quantities during the\n",
"# ordering process\n",
"inventory_workingvalue = inventory.copy()\n",
"\n",
"while n < y:\n",
" order = input(f\"Enter the name of a product you would like to order {n+1}: \") \n",
" # Check if the order is valid and available in the inventory:\n",
" if order in products and inventory_workingvalue[order] > 0:\n",
" customer_orders.append(order)\n",
" inventory_workingvalue[order] -= 1\n",
" n+=1\n",
" elif order in products and inventory_workingvalue[order] == 0:\n",
" print(f\"{order} is out of stock.\") \n",
" else:\n",
" print(f\"{order} is not available in the products list.\\nWe only sell t-shirts, mugs, hats, books, keychains\")\n",
" # This part will allow the user to order less than the y products:\n",
" order_more = input(\"Would you like to order more products? \\n type Y for \\\"yes\\\", N for \\\"no\\\": \").strip().lower()\n",
" while order_more not in [\"y\", \"n\"]:\n",
" print(\"Invalid input. Please type Y for \\\"yes\\\" or N for \\\"no\\\".\")\n",
" order_more = input(\"Would you like to order more products? \\n type Y for \\\"yes\\\", N for \\\"no\\\": \").strip().lower()\n",
" continue\n",
" if order_more == \"Y\" or order_more == \"y\":\n",
" continue\n",
" else:\n",
" break\n",
"\n",
"# I will keep the code as it was before, since I was already only subtructing the sold items \n",
"# from the inventory. But now they can order several items of the same category.\n",
"\n",
"print(\"Customer orders:\", customer_orders)\n",
"\n",
"# The statistic we needed: \n",
"\n",
"total_products_ordered = len(customer_orders)\n",
"percentage_products_ordered = (total_products_ordered / sum(inventory.values())) * 100\n",
"order_status = (total_products_ordered, percentage_products_ordered)\n",
"\n",
"print(\"Order Statistics:\")\n",
"print(f\"Total Products Ordered: {total_products_ordered}\")\n",
"print(f\"Percentage of Products Ordered: {percentage_products_ordered}%\")\n",
"\n",
"# I configured disctionary to updat in the function. But I am not sure how to call the \n",
"# function in the previous loop. But we had a workaround for that :)\n",
"\n",
"def inventory_new(inventory):\n",
" for product in customer_orders:\n",
" if product in inventory and inventory[product] > 0:\n",
" inventory[product] -= 1\n",
" return inventory\n",
"\n",
"inventory = inventory_new(inventory) \n",
"\n",
"# Displaying the updated inventory.\n",
"for product, quantity in inventory.items():\n",
" print(f\"{product}: {quantity}\")\n",
"\n"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
Expand All @@ -55,7 +162,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.9.13"
"version": "3.14.7"
}
},
"nbformat": 4,
Expand Down