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
147 changes: 146 additions & 1 deletion lab-python-flow-control.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,151 @@
"\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": 29,
"id": "1199bdb3-b8d3-43ac-b260-79934749edad",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Please, enter the avaliable quantity of each product\n"
]
},
{
"name": "stdin",
"output_type": "stream",
"text": [
"t-shirt: 0\n",
"mug: 0\n",
"hat: 0\n",
"book: 3\n",
"keychain: 3\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"The following products are availiable in the stock: {'t-shirt': 0, 'mug': 0, 'hat': 0, 'book': 3, 'keychain': 3}\n"
]
},
{
"name": "stdin",
"output_type": "stream",
"text": [
"Do you want to add product name to the order? If 'yes', please, enter the product name from the above list. If 'no', please, enter 'no' HAT\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"Sorry, this product is out of stock\n"
]
},
{
"name": "stdin",
"output_type": "stream",
"text": [
"Do you want to add product name to the order? If 'yes', please, enter the product name from the above list. If 'no', please, enter 'no' Book\n",
"Do you want to add product name to the order? If 'yes', please, enter the product name from the above list. If 'no', please, enter 'no' keychain\n",
"Do you want to add product name to the order? If 'yes', please, enter the product name from the above list. If 'no', please, enter 'no' no\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"Your order contains this products: {'book', 'keychain'}\n"
]
}
],
"source": [
"products = [\"t-shirt\", \"mug\", \"hat\", \"book\", \"keychain\"]\n",
"inventory = {}\n",
"\n",
"print(\"Please, enter the avaliable quantity of each product\")\n",
"\n",
"for i in products:\n",
" inp = int(input(\"{}: \".format(i)))\n",
" inventory[i] = inp\n",
"print(\"The following products are availiable in the stock:\", inventory)\n",
"\n",
"customer_orders = set() # Func. set() helps to identify Set instead of Dictionary -> Dictionary has no .add() method\n",
"\n",
"\n",
"while True:\n",
" question = input(\"Do you want to add product name to the order? If 'yes', please, enter the product name from the above list. If 'no', please, enter 'no'\").lower()\n",
" if question == \"no\":\n",
" break\n",
" if question in products:\n",
" if inventory[question] > 0:\n",
" customer_orders.add(question) \n",
" else:\n",
" print(\"Sorry, this product is out of stock\")\n",
" else:\n",
" print(\"Please, enter the name from the list\", products)\n",
"\n",
"print(\"Your order contains this products:\", customer_orders)"
]
},
{
"cell_type": "code",
"execution_count": 27,
"id": "b7891098-fab5-49c7-80eb-33ecf24c1bce",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Total Products Ordered: 2\n",
"Percentage of Products Ordered: 40%\n"
]
}
],
"source": [
"tpo = len(customer_orders)\n",
"ppo = int(len(customer_orders)*100/len(products))\n",
"\n",
"order_status = (tpo, ppo)\n",
" \n",
"print(\"Total Products Ordered: {}\".format(order_status[0]))\n",
"print(\"Percentage of Products Ordered: {}%\".format(order_status[1]))"
]
},
{
"cell_type": "code",
"execution_count": 28,
"id": "0b6526be-51b2-499b-89fc-937628a3724d",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"The following products are availiable in the stock: {'t-shirt': 3, 'mug': 3, 'hat': 2, 'book': 2, 'keychain': 3}\n"
]
}
],
"source": [
"for i in customer_orders:\n",
" inventory[i] = int(inventory[i]) - 1\n",
" \n",
"print(\"The following products are availiable in the stock:\", inventory) "
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "25677007-f22f-4d62-964d-3398135a2279",
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
Expand All @@ -55,7 +200,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.9.13"
"version": "3.14.6"
}
},
"nbformat": 4,
Expand Down