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
254 changes: 251 additions & 3 deletions lab-python-data-structures.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -50,13 +50,261 @@
"\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": 42,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"5\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",
"l = len(products)\n",
"print(l)\n"
]
},
{
"cell_type": "code",
"execution_count": 43,
"metadata": {},
"outputs": [],
"source": [
"#2 Create an empty dictionary called inventory\n",
"inventory = {}"
]
},
{
"cell_type": "code",
"execution_count": 100,
"metadata": {},
"outputs": [
{
"name": "stdin",
"output_type": "stream",
"text": [
"Please enter the quantity of t-shirt 8\n",
"Please enter the quantity of mug 7\n",
"Please enter the quantity of hat 6\n",
"Please enter the quantity of book 5\n",
"Please enter the quantity of keychain 4\n"
]
}
],
"source": [
"#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",
"products = [\"t-shirt\", \"mug\", \"hat\", \"book\", \"keychain\"]\n",
"inventory = {}\n",
"for product in products:\n",
" quantity = int(input(f\"Please enter the quantity of {product}\"))\n",
" inventory[product] = quantity"
]
},
{
"cell_type": "code",
"execution_count": 101,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"{'t-shirt': 8, 'mug': 7, 'hat': 6, 'book': 5, 'keychain': 4}"
]
},
"execution_count": 101,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# checking if inventory was updated correctly\n",
"inventory"
]
},
{
"cell_type": "code",
"execution_count": 102,
"metadata": {},
"outputs": [],
"source": [
"#4 Create an empty set called customer_orders\n",
"customer_order = set()"
]
},
{
"cell_type": "code",
"execution_count": 103,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Dear customer, please type below the 3 products you wish to order\n"
]
},
{
"name": "stdin",
"output_type": "stream",
"text": [
"Please specify product 1 you wish to order: mug\n",
"Please specify product 2 you wish to order: book\n",
"Please specify product 3 you wish to order: hat\n"
]
}
],
"source": [
"#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",
"print(\"Dear customer, please type below the 3 products you wish to order\")\n",
"for i in range(3):\n",
" product_name = input(f\"Please specify product {i+1} you wish to order: \")\n",
" if product_name in products:\n",
" customer_order.add(product_name) \n",
" else:\n",
" print(f\"{product_name} not available, please enter a different product name: \")\n",
" product_name = input(\"Please tell us a product from our inventory you wish to order: \")"
]
},
{
"cell_type": "code",
"execution_count": 104,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"{'book', 'hat', 'mug'}"
]
},
"execution_count": 104,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"#6 Print the products in the customer_orders set.\n",
"customer_order"
]
},
{
"cell_type": "code",
"execution_count": 105,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"30\n",
"60.0%\n"
]
}
],
"source": [
"#7 Calculate the following order statistics:\n",
"\n",
"# Total Products Ordered: The total number of products in the customer_orders set.\n",
"total_inventory = sum(inventory.values())\n",
"print(total_inventory)\n",
"\n",
"# Percentage of Products Ordered: The percentage of products ordered compared to the total available products.\n",
"total_ofproducstordered = 0\n",
"for product in products:\n",
" if product in customer_order: \n",
" total_ofproducstordered += inventory[product]\n",
"\n",
"percentage_ofproductsordered = (total_ofproducstordered / total_inventory)*100\n",
"print(str(percentage_ofproductsordered) + \"%\")\n",
"\n",
"#Store these statistics in a tuple called order_status"
]
},
{
"cell_type": "code",
"execution_count": 106,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Order Statistic: \n",
"Total Products Ordered: 18\n",
"Percentage of Products Ordered: 60.0\n"
]
}
],
"source": [
"#8 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>% \n",
"\n",
"total = total_ofproducstordered\n",
"\n",
"print(\"Order Statistic: \")\n",
"print(f\"Total Products Ordered: {total}\")\n",
"print(f\"Percentage of Products Ordered: {percentage_ofproductsordered}\")"
]
},
{
"cell_type": "code",
"execution_count": 112,
"metadata": {},
"outputs": [],
"source": [
"#9 Update the inventory by subtracting 1 from the quantity of each product. Modify the inventory dictionary accordingly.\n",
"\n",
"inventory = {'t-shirt': 8, 'mug': 7, 'hat': 6, 'book': 5, 'keychain': 4} \n",
"#defining this inside the cell to reset the dictionary, otherwise, the more I repeatedly run the cell, the more it subtracts\n",
"\n",
"for product in inventory:\n",
" inventory[product] -= 1\n"
]
},
{
"cell_type": "code",
"execution_count": 108,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"The inventory quanity of t-shirt is: 7\n",
"The inventory quanity of mug is: 6\n",
"The inventory quanity of hat is: 5\n",
"The inventory quanity of book is: 4\n",
"The inventory quanity of keychain is: 3\n"
]
}
],
"source": [
"#10 Print the updated inventory, displaying the quantity of each product on separate lines\n",
"for product in inventory:\n",
" print(f\"The inventory quanity of {product} is: {inventory[product]}\")"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"display_name": "Python [conda env:base] *",
"language": "python",
"name": "python3"
"name": "conda-base-py"
},
"language_info": {
"codemirror_mode": {
Expand All @@ -68,7 +316,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.9.13"
"version": "3.13.5"
}
},
"nbformat": 4,
Expand Down