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
115 changes: 114 additions & 1 deletion lab-python-flow-control.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,119 @@
"\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": 5,
"id": "972043e4-48cf-44f7-90a8-450238286efb",
"metadata": {},
"outputs": [
{
"name": "stdin",
"output_type": "stream",
"text": [
"Please give me the stock of t-shirt 5\n",
"Please give me the stock of mug 5\n",
"Please give me the stock of hat 5\n",
"Please give me the stock of book 5\n",
"Please give me the stock of keychain 5\n"
]
}
],
"source": [
"#1\n",
"from functools import reduce\n",
"products =[\"t-shirt\", \"mug\", \"hat\", \"book\", \"keychain\"]\n",
"inventory = {}\n",
"for product in products:\n",
" stock= input(f\"Please give me the stock of {product}\") \n",
" while not stock.isdigit():\n",
" stock= input(f\"Please give me the stock of {product}\")\n",
" inventory[product] = int(stock) \n",
"customer_orders = set()\n"
]
},
{
"cell_type": "code",
"execution_count": 6,
"id": "471d2af4-90ec-495f-90f0-5b5bf37eeb43",
"metadata": {},
"outputs": [
{
"name": "stdin",
"output_type": "stream",
"text": [
"choose the product you want to buy : t-shirt, mug, hat, book, keychain mug\n",
"Do you need more article : yes/no yes\n",
"choose the product you want to buy : t-shirt, mug, hat, book, keychain book\n",
"Do you need more article : yes/no no\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"{'mug', 'book'}\n"
]
}
],
"source": [
"#2\n",
"need_more_item= True\n",
"user_product_choice = input(\"choose the product you want to buy : t-shirt, mug, hat, book, keychain \")\n",
"user_whishlist = customer_orders.add(user_product_choice)\n",
"try:\n",
"\n",
" while need_more_item:\n",
" \n",
" more_choice = input(\"Do you need more article : yes/no\")\n",
" if bool(more_choice.strip().lower() == \"yes\"):\n",
" user_product_choice = input(\"choose the product you want to buy : t-shirt, mug, hat, book, keychain \")\n",
" user_whishlist = customer_orders.add(user_product_choice) \n",
" else:\n",
" need_more_item = False\n",
" \n",
"except:\n",
" input(\"you didn't choose the right products types/number\")\n",
"print(customer_orders) \n",
" "
]
},
{
"cell_type": "code",
"execution_count": 7,
"id": "2fa8fba4-f54b-46ec-b9f9-66293484adf5",
"metadata": {},
"outputs": [
{
"ename": "SyntaxError",
"evalue": "closing parenthesis '}' does not match opening parenthesis '(' (788843126.py, line 7)",
"output_type": "error",
"traceback": [
"\u001b[1;36m Cell \u001b[1;32mIn[7], line 7\u001b[1;36m\u001b[0m\n\u001b[1;33m print(f\"The product {order_item} doesn't exist\"}\u001b[0m\n\u001b[1;37m ^\u001b[0m\n\u001b[1;31mSyntaxError\u001b[0m\u001b[1;31m:\u001b[0m closing parenthesis '}' does not match opening parenthesis '('\n"
]
}
],
"source": [
"#3\n",
"\n",
"for order_item in customer_orders:\n",
" try:\n",
" inventory[order_item] -= 1\n",
" except:\n",
" print(f\"The product {order_item} doesn't exist\"}\n",
"\n",
"for key,value in inventory.items():\n",
" print(f\"The updated stock for {key} is {value}\")"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "cc2ca850-73de-4c3b-aedb-4cbde6482295",
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
Expand All @@ -55,7 +168,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.9.13"
"version": "3.13.5"
}
},
"nbformat": 4,
Expand Down