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
196 changes: 195 additions & 1 deletion lab-python-data-structures.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,200 @@
"\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": 1,
"metadata": {},
"outputs": [],
"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"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [],
"source": [
"# 2 Create an empty dictionary called inventory.\n",
"inventory={}"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [
{
"name": "stdin",
"output_type": "stream",
"text": [
"how many t-shirt do you want to order: 4\n",
"how many mug do you want to order: 8\n",
"how many hat do you want to order: 5\n",
"how many book do you want to order: f\n",
"how many keychain do you want to order: 12\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"{'t-shirt': 4, 'mug': 8, 'hat': 5, 'book': 0, 'keychain': 12}\n"
]
}
],
"source": [
"# 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",
"for product in products:\n",
" order_input = input(f\"how many {product} do you want to order: \")\n",
" if order_input.isdigit():\n",
" Order = int(order_input)\n",
" inventory[product] = Order\n",
" else:\n",
" inventory[product] = 0\n",
"print(inventory)"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [],
"source": [
"#\"4. Create an empty set called customer_orders.\"¶\n",
"#Sets: Unordered, mutable collections of unique elements with set operations like union and intersection. { } \n",
"customer_orders = set()"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [
{
"name": "stdin",
"output_type": "stream",
"text": [
"What is your {product} in the {products} list: mug\n",
"What is your {product} in the {products} list: hat\n",
"What is your {product} in the {products} list: dfghj\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"Product not found. Please try again.\n"
]
},
{
"name": "stdin",
"output_type": "stream",
"text": [
"What is your {product} in the {products} list: book\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"Your order is: {'book', 'hat', 'mug'}\n"
]
}
],
"source": [
"# Ask the user to input the name of three products that a customer wants to order (from those in the products list, \n",
"# meaning three products out of \"t-shirt\", \"mug\", \"hat\", \"book\" or \"keychain\". \n",
"# Add each product name to the customer_orders set.\n",
"\n",
"##créer un compteur\n",
"count = 0\n",
"while count <= 2:\n",
" select = input(f\"What is your {{product}} in the {{products}} list: \")\n",
" if select in products:\n",
" customer_orders.add(select)\n",
" count += 1\n",
" else:\n",
" print(\"Product not found. Please try again.\")\n",
" \n",
"# 6 Print the products in the customer_orders set.\n",
"print(\"Your order is: \", customer_orders)"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Total product ordered = 3\n",
"Percentage of Products Ordered: 60.0 %\n",
"statistics of these order: (3, 60.0)\n"
]
}
],
"source": [
"# 7. Calculate the following order statistics:\n",
" # - Total Products Ordered: The total number of products in the `customer_orders` set.\n",
"Total_product_ordered = len(customer_orders)\n",
"print(\"Total product ordered = \", Total_product_ordered)\n",
" # - Percentage of Products Ordered: The percentage of products ordered compared to the total available products.\n",
"Percentage = 100 * (Total_product_ordered / len(inventory))\n",
"print (\"Percentage of Products Ordered: \", Percentage, \"%\")\n",
" \n",
" # Store these statistics in a tuple called `order_status`.\n",
"order_status = (Total_product_ordered, Percentage)\n",
"print(\"statistics of these order: \", order_status)\n",
"#Print the order statistics using the following format:\n",
"#Order Statistics:\n",
"#Total Products Ordered: <total_products_ordered>\n",
"#Percentage of Products Ordered: <percentage_ordered>% "
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"{'t-shirt': 3, 'mug': 7, 'hat': 4, 'book': 0, 'keychain': 11}\n"
]
}
],
"source": [
"#Update the inventory by subtracting 1 from the quantity of each product. Modify the inventory dictionary accordingly.\n",
"#Print the updated inventory, displaying the quantity of each product on separate lines.\n",
"#Solve the exercise by implementing the steps using the Python concepts of lists, dictionaries, sets, and basic input/output operations.\n",
"\n",
"for item in inventory: \n",
" if inventory[item] > 0:\n",
" inventory[item] -= 1\n",
" \n",
" else:\n",
" inventory[item] = 0\n",
"\n",
"print (inventory)\n",
" \n",
" \n",
"\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
Expand All @@ -68,7 +262,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.9.13"
"version": "3.13.5"
}
},
"nbformat": 4,
Expand Down