Skip to content
Open
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
146 changes: 146 additions & 0 deletions lab-data-structures_elbgr.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,146 @@
{
"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": 8,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"{'t-shirt': 5.0}\n",
"{'t-shirt': 5.0, 'mug': 5.0}\n",
"{'t-shirt': 5.0, 'mug': 5.0, 'hat': 5.0}\n",
"{'t-shirt': 5.0, 'mug': 5.0, 'hat': 5.0, 'book': 5.0}\n",
"{'t-shirt': 5.0, 'mug': 5.0, 'hat': 5.0, 'book': 5.0, 'keychain': 5.0}\n",
"This product is not available\n",
"This product is not available\n",
"You already selected this product\n",
"You already selected this product\n",
"{'mug', 'keychain', 'hat'}\n",
"3\n",
"60.0\n",
"{'t-shirt': 4.0, 'mug': 4.0, 'hat': 4.0, 'book': 4.0, 'keychain': 4.0}\n"
]
}
],
"source": [
"products=[\"t-shirt\",\"mug\",\"hat\",\"book\",\"keychain\"]\n",
"inventory={}\n",
"for product in products:\n",
" quantity=float(input(f\"Please enter the quantity for {product}\"))\n",
" #here we are defining that product is the key on the dictionary and quantity is the value\n",
" inventory[product]=quantity\n",
" print(inventory)\n",
"\n",
"customers_orders=set()\n",
"while len(customers_orders)<3:\n",
" order_product=input(\"Please choose an item from the product list\")\n",
" #Previusly you tried to write \"if is not in\" as you can see the if conditional goes without \"is\"\n",
" if order_product not in products:\n",
" print(\"This product is not available\")\n",
" continue\n",
" if order_product in customers_orders:\n",
" print(\"You already selected this product\")\n",
" continue\n",
" \n",
" customers_orders.add(order_product)\n",
" \n",
"\n",
"print(customers_orders)\n",
"\n",
"#Order statistics:\n",
"total_products_ordered=len(customers_orders)\n",
"print(total_products_ordered)\n",
"percentage_products_ordered=(len(customers_orders)/len(products))*100\n",
"print(percentage_products_ordered)\n",
"order_status=(total_products_ordered,percentage_products_ordered)\n",
"\n",
"#Substracting one from the quantity\n",
"\n",
"for product in inventory:\n",
" inventory[product]=float(inventory[product])-1\n",
"\n",
"\n",
"print(inventory) \n",
"\n",
"\n",
"\n",
"\n"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"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.13.6"
}
},
"nbformat": 4,
"nbformat_minor": 4
}
40 changes: 38 additions & 2 deletions lab-python-data-structures.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -50,11 +50,47 @@
"\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": null,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"{'t-shirt': 6.0}\n",
"{'t-shirt': 6.0, 'mug': 6.0}\n",
"{'t-shirt': 6.0, 'mug': 6.0, 'hat': 6.0}\n",
"{'t-shirt': 6.0, 'mug': 6.0, 'hat': 6.0, 'book': 6.0}\n",
"{'t-shirt': 6.0, 'mug': 6.0, 'hat': 6.0, 'book': 6.0, 'keychain': 6.0}\n"
]
}
],
"source": [
"products=[\"t-shirt\",\"mug\",\"hat\",\"book\",\"keychain\"]\n",
"inventory={}\n",
"for product in products:\n",
" quantity=float(input(f\"Please enter the quantity for {product}\"))\n",
" inventory[product]=quantity\n",
" print(inventory)\n",
"\n",
"customers_orders=set()\n",
"while len(customers_orders)<4:\n",
" order_product=input(\"Please choose an item from the product list\")\n",
" customers_orders.add(order_product)\n",
"\n",
"print(customers_orders)\n",
"\n",
"\n",
"\n"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
Expand All @@ -68,7 +104,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.9.13"
"version": "3.13.6"
}
},
"nbformat": 4,
Expand Down