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
86 changes: 84 additions & 2 deletions lab-python-flow-control.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,93 @@
"\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": "cdbd88da",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"{'t-shirt': 76, 'mug': 22, 'hat': 81, 'book': 60, 'keychain': 3}\n"
]
}
],
"source": [
"#Task 1\n",
"products = [\"t-shirt\", \"mug\", \"hat\", \"book\", \"keychain\"]\n",
"inventory = {}\n",
"\n",
"for product in products:\n",
" quantity = int(input(f\"Enter the number of {product} available in the inventory\"))\n",
" inventory[product] = quantity #name_of_the_dictionary(key) = output variable (or) value for the key\n",
"print(inventory)"
]
},
{
"cell_type": "code",
"execution_count": 13,
"id": "07aad4e5",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Your order is complete!\n",
"You ordered these products: {'hat', 'keychain', 'mug'}\n"
]
}
],
"source": [
"#Task 2\n",
"customer_orders = set()\n",
"def order():\n",
" order_item = input(\"Please enter the name of the product you want to order\")\n",
" return order_item\n",
"\n",
"another_order = \"YES\" #set a true value for while loop, so it automatically ends when this is false\n",
"\n",
"while another_order == \"YES\":\n",
" product = order() #this variable is for storing what the ordered item is\n",
" customer_orders.add(product)\n",
" another_order = input(\"Do you want to add another order? Please type 'YES' or 'NO': \")\n",
" \n",
"print(\"Your order is complete!\")\n",
"print(\"You ordered these products:\", customer_orders)"
]
},
{
"cell_type": "code",
"execution_count": 15,
"id": "fc9dcee9",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"{'t-shirt': 76, 'mug': 21, 'hat': 80, 'book': 60, 'keychain': 2}\n"
]
}
],
"source": [
"#Task 3\n",
"inventory = {'t-shirt': 76, 'mug': 22, 'hat': 81, 'book': 60, 'keychain': 3}\n",
"\n",
"for product, quantity in inventory.items():\n",
" if product in customer_orders:\n",
" inventory[product] = quantity -1 #dictionary_name[key] = value -1 (this will assign the value to the respective keys)\n",
"print(inventory)"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"display_name": "base",
"language": "python",
"name": "python3"
},
Expand All @@ -55,7 +137,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.9.13"
"version": "3.13.5"
}
},
"nbformat": 4,
Expand Down