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
94 changes: 92 additions & 2 deletions lab-python-data-structures.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -50,11 +50,101 @@
"\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": 12,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"{'t-shirt': 1, 'mug': 2, 'hat': 3, 'book': 4, 'keychain': 0}\n",
"Customer orders: {'mug', 'hat', 'book'}\n",
"Total Products Ordered: 3\n",
"Percentage of Products Ordered: 60.0 %\n",
"\n",
" Order Statistics:\n",
" Total Products Ordered: 3\n",
" Percentage of Products Ordered: 60.0%\n",
"\n",
"updated inventory: \n",
"t-shirt: 0\n",
"mug: 1\n",
"hat: 2\n",
"book: 3\n",
"keychain: 0\n"
]
}
],
"source": [
"#1. Define a list called `products` that contains the following items: \"t-shirt\", \"mug\", \"hat\", \"book\", \"keychain\".\n",
"products = [\"t-shirt\", \"mug\", \"hat\", \"book\", \"keychain\"]\n",
"\n",
"#2. Create an empty dictionary called `inventory`.\n",
"inventory = {}\n",
"\n",
"#3. Ask the user to input the quantity of each product available in the inventory. \n",
"#Use the product names from the `products` list as keys in the `inventory` dictionary and assign the respective quantities as values.\n",
"\n",
"for product in products:\n",
" inventory[product] = int(input(f\"Please enter the quantity of {product} available in the inventory: \"))\n",
"\n",
"print(inventory)\n",
" \n",
"#4. Create an empty set called `customer_orders`.\n",
"customer_orders = set()\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). Add each product name to the `customer_orders` set.\n",
"for x in range(3):\n",
" input_product = input(\"Please enter the name of a product the customer wants to order (t-shirt, mug, hat, book, keychain): \")\n",
" if input_product in products:\n",
" customer_orders.add(input_product)\n",
" else:\n",
" print(\"Sorry, that product is not available.\")\n",
"\n",
"#6. Print the products in the `customer_orders` set.\n",
"print(\"Customer orders:\", customer_orders)\n",
"\n",
"#7. Calculate the following order statistics:\n",
" #- Total Products Ordered: The total number of products in the `customer_orders` set.\n",
"print(\"Total Products Ordered:\", len(customer_orders))\n",
"\n",
" #- Percentage of Products Ordered: The percentage of products ordered compared to the total available products.\n",
"print(\"Percentage of Products Ordered:\", (len(customer_orders) / len(products)) * 100, \"%\")\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",
"print(f\"\"\"\n",
" Order Statistics:\n",
" Total Products Ordered: {len(customer_orders)}\n",
" Percentage of Products Ordered: {(len(customer_orders) / len(products)) * 100}%\n",
"\"\"\")\n",
"\n",
"#9. Update the inventory by subtracting 1 from the quantity of each product. Modify the `inventory` dictionary accordingly.\n",
"\n",
"for product in products:\n",
" inventory[product] -= 1\n",
" if inventory[product] < 0:\n",
" inventory[product] = 0\n",
"\n",
"#10. Print the updated inventory, displaying the quantity of each product on separate lines.\n",
"print(\"updated inventory: \")\n",
"\n",
"for item in inventory:\n",
" print(f\"{item}: {inventory[item]}\")\n"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
Expand All @@ -68,7 +158,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.9.13"
"version": "3.12.9"
}
},
"nbformat": 4,
Expand Down