Skip to content
Open

Title #621

Show file tree
Hide file tree
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
Binary file added .DS_Store
Binary file not shown.
292 changes: 292 additions & 0 deletions .ipynb_checkpoints/lab-python-data-structures-checkpoint.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,292 @@
{
"cells": [
{
"cell_type": "markdown",
"metadata": {
"tags": []
},
"source": [
"# Lab | Data Structures "
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Exercise: Managing Customer Orders\n",
"\n",
"As part of a business venture, you are starting an online store that sells various products. To ensure smooth operations, you need to develop a program that manages customer orders and inventory.\n",
"\n",
"Follow the steps below to complete the exercise:\n",
"\n",
"1. Define a list called `products` that contains the following items: \"t-shirt\", \"mug\", \"hat\", \"book\", \"keychain\".\n",
"\n",
"2. Create an empty dictionary called `inventory`.\n",
"\n",
"3. Ask the user to input the quantity of each product available in the inventory. Use the product names from the `products` list as keys in the `inventory` dictionary and assign the respective quantities as values.\n",
"\n",
"4. Create an empty set called `customer_orders`.\n",
"\n",
"5. Ask the user to input the name of three products that a customer wants to order (from those in the products list, meaning three products out of \"t-shirt\", \"mug\", \"hat\", \"book\" or \"keychain\". Add each product name to the `customer_orders` set.\n",
"\n",
"6. Print the products in the `customer_orders` set.\n",
"\n",
"7. Calculate the following order statistics:\n",
" - Total Products Ordered: The total number of products in the `customer_orders` set.\n",
" - Percentage of Products Ordered: The percentage of products ordered compared to the total available products.\n",
" \n",
" Store these statistics in a tuple called `order_status`.\n",
"\n",
"8. Print the order statistics using the following format:\n",
" ```\n",
" Order Statistics:\n",
" Total Products Ordered: <total_products_ordered>\n",
" Percentage of Products Ordered: <percentage_ordered>% \n",
" ```\n",
"\n",
"9. Update the inventory by subtracting 1 from the quantity of each product. Modify the `inventory` dictionary accordingly.\n",
"\n",
"10. Print the updated inventory, displaying the quantity of each product on separate lines.\n",
"\n",
"Solve the exercise by implementing the steps using the Python concepts of lists, dictionaries, sets, and basic input/output operations. "
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"1:"
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
"products = [\"t-shirt\", \"mug\", \"hat\", \"book\", \"keychain\"]"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"2:"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [],
"source": [
"inventory = {}"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"3:"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [],
"source": [
"for product in products:\n",
" quantity = input(f\"Enter the quantity of '{product}' in stock:\")\n",
" inventory[product] = int(quantity)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"4:"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [],
"source": [
"customer_order = set()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"5:"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [],
"source": [
"while len(customer_order) < 3:\n",
" item = input(\"Enter the name of a product to order(t-shirt, mug, hat, book, keychain): \").strip()\n",
" if item in products:\n",
" customer_order.add(item)\n",
" else:\n",
" print(\"Sorry, we don't have that product. Please choose from the available products.(t-shirt, mug, hat, book, keychain)\")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"6:"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Your order contains: {'mug', 'hat', 'book'}\n"
]
}
],
"source": [
"print(f\"Your order contains: {customer_order}\")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"7:"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {},
"outputs": [],
"source": [
"total_products_ordered = len(customer_order)\n",
"total_inventory = len(inventory)\n",
"percentage_order = (total_products_ordered / total_inventory) * 100\n",
"order_status = (total_products_ordered, percentage_order)\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"8:"
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Order stattistics:\n",
"Total Products Ordered: 3\n",
"Percentage of Products Ordered: 60.0%\n"
]
}
],
"source": [
"print(f\"Order statistics:\\nTotal Products Ordered: {total_products_ordered}\\nPercentage of Products Ordered: {percentage_order}%\")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"9:"
]
},
{
"cell_type": "code",
"execution_count": 9,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"'mug' has been shipped. Remaining stock: 1\n",
"'hat' has been shipped. Remaining stock: 1\n",
"'book' has been shipped. Remaining stock: 1\n"
]
}
],
"source": [
"for product in customer_order:\n",
" if inventory[product] > 0:\n",
" inventory[product] -= 1\n",
" print(f\"'{product}' has been shipped. Remaining stock: {inventory[product]}\")\n",
" else:\n",
" print(f\"Sorry, '{product}' is out of stock and cannot be shipped.\")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"10:\n"
]
},
{
"cell_type": "code",
"execution_count": 10,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"t-shirt: 2\n",
"mug: 1\n",
"hat: 1\n",
"book: 1\n",
"keychain: 2\n"
]
}
],
"source": [
"for product in products:\n",
" print(f\"{product}: {inventory[product]}\")"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.11.4"
}
},
"nbformat": 4,
"nbformat_minor": 4
}
Loading