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
212 changes: 211 additions & 1 deletion lab-python-flow-control.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,216 @@
"\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": 25,
"id": "a84764ee-e642-47e7-87a7-80d2d4232123",
"metadata": {},
"outputs": [],
"source": [
"products = [\"t-shirt\", \"mug\", \"hat\", \"book\", \"keychain\"]"
]
},
{
"cell_type": "code",
"execution_count": 4,
"id": "f47ea14b-5643-4ee3-a079-c4a17e47f1e9",
"metadata": {},
"outputs": [],
"source": [
"inventory = {}"
]
},
{
"cell_type": "code",
"execution_count": 34,
"id": "cf86bac3-f3ec-4fbc-a7c7-7ee05599ab85",
"metadata": {},
"outputs": [
{
"name": "stdin",
"output_type": "stream",
"text": [
"Please enter the number of t-shirt 1\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"The number of t-shirt is 1\n"
]
},
{
"name": "stdin",
"output_type": "stream",
"text": [
"Please enter the number of mug 2\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"The number of mug is 2\n"
]
},
{
"name": "stdin",
"output_type": "stream",
"text": [
"Please enter the number of hat 3\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"The number of hat is 3\n"
]
},
{
"name": "stdin",
"output_type": "stream",
"text": [
"Please enter the number of book 4\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"The number of book is 4\n"
]
},
{
"name": "stdin",
"output_type": "stream",
"text": [
"Please enter the number of keychain 5\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"The number of keychain is 5\n"
]
}
],
"source": [
"for product_type in products:\n",
" inventory[product_type] = int(input(f\"Please enter the number of {product_type}\"))\n",
" print(f\"The number of {product_type} is {inventory[product_type]}\")"
]
},
{
"cell_type": "code",
"execution_count": 29,
"id": "6b0c9453-b9bc-4980-b65a-dc32a0b7301c",
"metadata": {},
"outputs": [],
"source": [
"customer_orders = set({})"
]
},
{
"cell_type": "code",
"execution_count": 32,
"id": "5401d134-a033-4272-a546-23386f46f7e0",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"{'mug', 'book', 't-shirt'}\n"
]
}
],
"source": [
"print(customer_orders)"
]
},
{
"cell_type": "code",
"execution_count": 31,
"id": "0c164d1e-b070-4b1b-b084-3b318e0d2424",
"metadata": {},
"outputs": [
{
"name": "stdin",
"output_type": "stream",
"text": [
"Please enter the product ordered: mug\n",
"Do you want to add another product? Enter Yes or No Yes\n",
"Please enter the product ordered: t-shirt\n",
"Do you want to add another product? Enter Yes or No Yes\n",
"Please enter the product ordered: tt-shirt\n",
"Please enter the product ordered (among the existing products types): book\n",
"Do you want to add another product? Enter Yes or No No\n"
]
}
],
"source": [
"count = 0\n",
"customer_order_type = input('Please enter the product ordered:')\n",
"if customer_order_type in products:\n",
" customer_orders.add(customer_order_type)\n",
"elif customer_order_type not in products:\n",
" customer_order_type = input('Please enter the product ordered (among the existing products types):')\n",
"customer_order_type_question = input('Do you want to add another product? Enter Yes or No')\n",
"if customer_order_type_question != 'Yes' and customer_order_type_question != 'No':\n",
" customer_order_type_question = input('The input is wrong, please try again and answer only with Yes or No the following question: Do you want to add another product?')\n",
"while customer_order_type_question == 'Yes':\n",
" customer_order_type_another = input('Please enter the product ordered:')\n",
" if customer_order_type_another in products:\n",
" customer_orders.add(customer_order_type_another)\n",
" elif customer_order_type_another not in products:\n",
" customer_order_type_another = input('Please enter the product ordered (among the existing products types):')\n",
" customer_orders.add(customer_order_type_another)\n",
" customer_order_type_question = input('Do you want to add another product? Enter Yes or No')"
]
},
{
"cell_type": "code",
"execution_count": 35,
"id": "00ed9e07-ac40-4feb-991b-e60a18afd1c0",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"{'t-shirt': 1, 'mug': 2, 'hat': 3, 'book': 4, 'keychain': 5}\n"
]
}
],
"source": [
"print(inventory)"
]
},
{
"cell_type": "code",
"execution_count": 36,
"id": "4d0c771d-476d-46e8-a3b8-7b695971bdc2",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"{'t-shirt': 0, 'mug': 1, 'hat': 3, 'book': 3, 'keychain': 5}\n"
]
}
],
"source": [
"for customer_order in customer_orders:\n",
" if customer_order in products:\n",
" inventory[customer_order] -= 1\n",
"print(inventory)"
]
}
],
"metadata": {
Expand All @@ -55,7 +265,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.9.13"
"version": "3.13.5"
}
},
"nbformat": 4,
Expand Down