Skip to content
Open

zahid #805

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
201 changes: 201 additions & 0 deletions .ipynb_checkpoints/lab-python-data-structures-checkpoint.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,201 @@
{
"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": "code",
"execution_count": 2,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"--- Inventory Setup ---\n"
]
},
{
"name": "stdin",
"output_type": "stream",
"text": [
"Enter quantity for t-shirt: 1\n",
"Enter quantity for mug: 1\n",
"Enter quantity for hat: 1\n",
"Enter quantity for book: 1\n",
"Enter quantity for keychain: 1\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"\n",
"--- Place Customer Order ---\n"
]
},
{
"name": "stdin",
"output_type": "stream",
"text": [
"Enter product #1 to order: hat\n",
"Enter product #2 to order: book\n",
"Enter product #3 to order: mug\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"\n",
"Products ordered:\n",
"{'hat', 'mug', 'book'}\n",
"\n",
"Order Statistics:\n"
]
},
{
"ename": "TypeError",
"evalue": "can only concatenate str (not \"int\") to str",
"output_type": "error",
"traceback": [
"\u001b[31m---------------------------------------------------------------------------\u001b[39m",
"\u001b[31mTypeError\u001b[39m Traceback (most recent call last)",
"\u001b[36mCell\u001b[39m\u001b[36m \u001b[39m\u001b[32mIn[2]\u001b[39m\u001b[32m, line 35\u001b[39m\n\u001b[32m 31\u001b[39m order_status = (total_products_ordered, percentage_ordered)\n\u001b[32m 32\u001b[39m \n\u001b[32m 33\u001b[39m \u001b[38;5;66;03m# Step 8: Print the order statistics using standard string concatenation\u001b[39;00m\n\u001b[32m 34\u001b[39m print(\u001b[33m\"\\nOrder Statistics:\"\u001b[39m)\n\u001b[32m---> \u001b[39m\u001b[32m35\u001b[39m print(\u001b[33m\"Total Products Ordered: \"\u001b[39m + order_status[\u001b[32m0\u001b[39m])\n\u001b[32m 36\u001b[39m print(\u001b[33m\"Percentage of Products Ordered: \"\u001b[39m + str(order_status[\u001b[32m1\u001b[39m]) + \u001b[33m\"%\"\u001b[39m)\n\u001b[32m 37\u001b[39m \n\u001b[32m 38\u001b[39m \u001b[38;5;66;03m# Step 9: Update the inventory by subtracting 1 from every item manually\u001b[39;00m\n",
"\u001b[31mTypeError\u001b[39m: can only concatenate str (not \"int\") to str"
]
}
],
"source": [
"\n",
"# Step 1: list of products \n",
"products = [\"t-shirt\", \"mug\", \"hat\", \"book\", \"keychain\"]\n",
"\n",
"# Step 2:create an empty dictionary \n",
"inventory = {}\n",
"\n",
"# Step 3: ask the user for each product quantity and add it to the dictionary\n",
"print(\"____Inventory Setup ____\")\n",
"inventory[products[0]] = int(input(\"Enter quantity for \" + products[0] + \": \"))\n",
"inventory[products[1]] = int(input(\"Enter quantity for \" + products[1] + \": \"))\n",
"inventory[products[2]] = int(input(\"Enter quantity for \" + products[2] + \": \"))\n",
"inventory[products[3]] = int(input(\"Enter quantity for \" + products[3] + \": \"))\n",
"inventory[products[4]] = int(input(\"Enter quantity for \" + products[4] + \": \"))\n",
"\n",
"# Step 4: Create an empty set for customer orders\n",
"customer_orders = set()\n",
"\n",
"# Step 5: Asks user to input three products and add them to the set\n",
"print(\"\\n___ Place Customer Order ___\")\n",
"customer_orders.add(input(\"Enter product #1 to order: \"))\n",
"customer_orders.add(input(\"Enter product #2 to order: \"))\n",
"customer_orders.add(input(\"Enter product #3 to order: \"))\n",
"\n",
"# Step 6: Print the products in the customer_orders set\n",
"print(\"\\nProducts ordered:\")\n",
"print(customer_orders)\n",
"\n",
"# Step 7: Calculate order statistics and store them in a tuple\n",
"total_products_ordered = len(customer_orders)\n",
"percentage_ordered = (total_products_ordered / len(products)) * 100\n",
"order_status = (total_products_ordered, percentage_ordered)\n",
"\n",
"# Step 8: Print the order statistics using standard string concatenation\n",
"print(\"\\nOrder Statistics:\")\n",
"print(\"Total Products Ordered: \" + str(order_status[0]))\n",
"print(\"Percentage of Products Ordered: \" + str(order_status[1]) + \"%\")\n",
"\n",
"# Step 9: Update the inventory by subtracting 1 from every item manually\n",
"inventory[products[0]] = inventory[products[0]] - 1\n",
"inventory[products[1]] = inventory[products[1]] - 1\n",
"inventory[products[2]] = inventory[products[2]] - 1\n",
"inventory[products[3]] = inventory[products[3]] - 1\n",
"inventory[products[4]] = inventory[products[4]] - 1\n",
"\n",
"# Step 10: Print the updated inventory on separate lines\n",
"print(\"\\nUpdated Inventory:\")\n",
"print(products[0] + \": \" + str(inventory[products[0]]))\n",
"print(products[1] + \": \" + str(inventory[products[1]]))\n",
"print(products[2] + \": \" + str(inventory[products[2]]))\n",
"print(products[3] + \": \" + str(inventory[products[3]]))\n",
"print(products[4] + \": \" + str(inventory[products[4]]))\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"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.14.6"
}
},
"nbformat": 4,
"nbformat_minor": 4
}
127 changes: 126 additions & 1 deletion lab-python-data-structures.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,131 @@
"\n",
"Solve the exercise by implementing the steps using the Python concepts of lists, dictionaries, sets, and basic input/output operations. "
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"--- Inventory Setup ---\n"
]
},
{
"name": "stdin",
"output_type": "stream",
"text": [
"Enter quantity for t-shirt: 1\n",
"Enter quantity for mug: 1\n",
"Enter quantity for hat: 1\n",
"Enter quantity for book: 1\n",
"Enter quantity for keychain: 1\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"\n",
"--- Place Customer Order ---\n"
]
},
{
"name": "stdin",
"output_type": "stream",
"text": [
"Enter product #1 to order: hat\n",
"Enter product #2 to order: book\n",
"Enter product #3 to order: mug\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"\n",
"Products ordered:\n",
"{'hat', 'mug', 'book'}\n",
"\n",
"Order Statistics:\n"
]
},
{
"ename": "TypeError",
"evalue": "can only concatenate str (not \"int\") to str",
"output_type": "error",
"traceback": [
"\u001b[31m---------------------------------------------------------------------------\u001b[39m",
"\u001b[31mTypeError\u001b[39m Traceback (most recent call last)",
"\u001b[36mCell\u001b[39m\u001b[36m \u001b[39m\u001b[32mIn[2]\u001b[39m\u001b[32m, line 35\u001b[39m\n\u001b[32m 31\u001b[39m order_status = (total_products_ordered, percentage_ordered)\n\u001b[32m 32\u001b[39m \n\u001b[32m 33\u001b[39m \u001b[38;5;66;03m# Step 8: Print the order statistics using standard string concatenation\u001b[39;00m\n\u001b[32m 34\u001b[39m print(\u001b[33m\"\\nOrder Statistics:\"\u001b[39m)\n\u001b[32m---> \u001b[39m\u001b[32m35\u001b[39m print(\u001b[33m\"Total Products Ordered: \"\u001b[39m + order_status[\u001b[32m0\u001b[39m])\n\u001b[32m 36\u001b[39m print(\u001b[33m\"Percentage of Products Ordered: \"\u001b[39m + str(order_status[\u001b[32m1\u001b[39m]) + \u001b[33m\"%\"\u001b[39m)\n\u001b[32m 37\u001b[39m \n\u001b[32m 38\u001b[39m \u001b[38;5;66;03m# Step 9: Update the inventory by subtracting 1 from every item manually\u001b[39;00m\n",
"\u001b[31mTypeError\u001b[39m: can only concatenate str (not \"int\") to str"
]
}
],
"source": [
"\n",
"# Step 1: list of products \n",
"products = [\"t-shirt\", \"mug\", \"hat\", \"book\", \"keychain\"]\n",
"\n",
"# Step 2:create an empty dictionary \n",
"inventory = {}\n",
"\n",
"# Step 3: ask the user for each product quantity and add it to the dictionary\n",
"print(\"____Inventory Setup ____\")\n",
"inventory[products[0]] = int(input(\"Enter quantity for \" + products[0] + \": \"))\n",
"inventory[products[1]] = int(input(\"Enter quantity for \" + products[1] + \": \"))\n",
"inventory[products[2]] = int(input(\"Enter quantity for \" + products[2] + \": \"))\n",
"inventory[products[3]] = int(input(\"Enter quantity for \" + products[3] + \": \"))\n",
"inventory[products[4]] = int(input(\"Enter quantity for \" + products[4] + \": \"))\n",
"\n",
"# Step 4: Create an empty set for customer orders\n",
"customer_orders = set()\n",
"\n",
"# Step 5: Asks user to input three products and add them to the set\n",
"print(\"\\n___ Place Customer Order ___\")\n",
"customer_orders.add(input(\"Enter product #1 to order: \"))\n",
"customer_orders.add(input(\"Enter product #2 to order: \"))\n",
"customer_orders.add(input(\"Enter product #3 to order: \"))\n",
"\n",
"# Step 6: Print the products in the customer_orders set\n",
"print(\"\\nProducts ordered:\")\n",
"print(customer_orders)\n",
"\n",
"# Step 7: Calculate order statistics and store them in a tuple\n",
"total_products_ordered = len(customer_orders)\n",
"percentage_ordered = (total_products_ordered / len(products)) * 100\n",
"order_status = (total_products_ordered, percentage_ordered)\n",
"\n",
"# Step 8: Print the order statistics using standard string concatenation\n",
"print(\"\\nOrder Statistics:\")\n",
"print(\"Total Products Ordered: \" + str(order_status[0]))\n",
"print(\"Percentage of Products Ordered: \" + str(order_status[1]) + \"%\")\n",
"\n",
"# Step 9: Update the inventory by subtracting 1 from every item manually\n",
"inventory[products[0]] = inventory[products[0]] - 1\n",
"inventory[products[1]] = inventory[products[1]] - 1\n",
"inventory[products[2]] = inventory[products[2]] - 1\n",
"inventory[products[3]] = inventory[products[3]] - 1\n",
"inventory[products[4]] = inventory[products[4]] - 1\n",
"\n",
"# Step 10: Print the updated inventory on separate lines\n",
"print(\"\\nUpdated Inventory:\")\n",
"print(products[0] + \": \" + str(inventory[products[0]]))\n",
"print(products[1] + \": \" + str(inventory[products[1]]))\n",
"print(products[2] + \": \" + str(inventory[products[2]]))\n",
"print(products[3] + \": \" + str(inventory[products[3]]))\n",
"print(products[4] + \": \" + str(inventory[products[4]]))\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
Expand All @@ -68,7 +193,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.9.13"
"version": "3.14.6"
}
},
"nbformat": 4,
Expand Down