Skip to content
Open

lab2 #615

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
83 changes: 81 additions & 2 deletions lab-python-flow-control.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,90 @@
"\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": "8bd74d7f",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"{'t-shirt': 5, 'mug': 4, 'hat': 3, 'book': 2, 'keychain': 1}\n"
]
}
],
"source": [
"products = [\"t-shirt\", \"mug\", \"hat\", \"book\", \"keychain\"]\n",
"inventory = {}\n",
"\n",
"for product in products:\n",
" quantity = int(input(\"Please enter quantity for product:\"))\n",
" inventory[product] = quantity\n",
"\n",
"print(inventory)\n"
]
},
{
"cell_type": "code",
"execution_count": 9,
"id": "5ebe58cd",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Customer orders: {'hat', 'mug'}\n"
]
}
],
"source": [
"customer_orders = set()\n",
"\n",
"while True:\n",
" product = input(\"Enter product name: \")\n",
" customer_orders.add(product)\n",
" \n",
" choice = input(\"Do you want to add another product? (yes/no): \")\n",
" if choice == \"no\":\n",
" break\n",
"\n",
"print(\"Customer orders:\", customer_orders)\n"
]
},
{
"cell_type": "code",
"execution_count": 7,
"id": "f0ff5ddf",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"{'t-shirt': 5, 'mug': 3, 'hat': 2, 'book': 2, 'keychain': 1}"
]
},
"execution_count": 7,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"inventory={'t-shirt': 5, 'mug': 4, 'hat': 3, 'book': 2, 'keychain': 1}\n",
"customer_orders={'hat':2, 'mug':3}\n",
"for product in customer_orders:\n",
" if product in inventory:\n",
" inventory[product] = inventory[product] - 1\n",
"inventory"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"display_name": "base",
"language": "python",
"name": "python3"
},
Expand All @@ -55,7 +134,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.9.13"
"version": "3.13.9"
}
},
"nbformat": 4,
Expand Down