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
183 changes: 180 additions & 3 deletions lab-python-data-structures.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -50,13 +50,190 @@
"\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": "stdin",
"output_type": "stream",
"text": [
"How many t-shirts do you have? 1\n",
"How many mugs do you have? 2\n",
"How many hats do you have? 3\n",
"How many books do you have? 4\n",
"How many keychains do you have? 5\n"
]
}
],
"source": [
"# 1. Define a list call products\n",
"products = [\"t-shirt\", \"mug\", \"hat\", \"book\", \"keychain\"]\n",
"# 2. Create an empty dictionary called inventory\n",
"inventory = {}\n",
"\n",
"# 3. For each product available in inventory, ask user to input quantity of product\n",
"for product in products:\n",
" quantity = int(input(f\"How many {product}s do you have? \"))\n",
" # Add each product as a key and each corresponding quantity as a value to the inventory dictionary\n",
" inventory[product] = quantity"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"{'t-shirt': 1, 'mug': 2, 'hat': 3, 'book': 4, 'keychain': 5}\n"
]
}
],
"source": [
"print(inventory)"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Available products: ['t-shirt', 'mug', 'hat', 'book', 'keychain']\n"
]
},
{
"name": "stdin",
"output_type": "stream",
"text": [
"Enter a product to order: pen\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"That product is not available. Please choose from the list.\n"
]
},
{
"name": "stdin",
"output_type": "stream",
"text": [
"Enter a product to order: book\n",
"Enter a product to order: mug\n",
"Enter a product to order: t-shirt\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"\n",
"Products in your order: {'book', 't-shirt', 'mug'}\n"
]
}
],
"source": [
"# 4. Create an empty set called customer_orders\n",
"customer_orders = set()\n",
"\n",
"# 5. Ask user for three products to add to customer_orders\n",
"print(f\"Available products: {products}\")\n",
"while len(customer_orders) < 3:\n",
" product_ordered = input(\"Enter a product to order: \").lower().strip()\n",
" if product_ordered in products:\n",
" customer_orders.add(product_ordered)\n",
" else:\n",
" print(\"That product is not available. Please choose from the list.\")\n",
"\n",
"# 6. Print the products in the customer_orders set\n",
"print(\"\\nProducts in your order:\", customer_orders)\n",
"\n",
"# 7. Calculate total products ordered\n",
"total_products_ordered = len(customer_orders)"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Order Statistics:\n",
"Total Products Ordered: 3\n",
"Percentage of Products Ordered: 60.0%\n"
]
}
],
"source": [
"# 8. Calculate order statistics\n",
"percentage_ordered = (total_products_ordered / len(products)) * 100\n",
"\n",
"# Store statistics in a tuple\n",
"order_status = (total_products_ordered, percentage_ordered)\n",
"\n",
"# Print order statistics using the specified format\n",
"print(\"Order Statistics:\")\n",
"print(f\"Total Products Ordered: {order_status[0]}\")\n",
"print(f\"Percentage of Products Ordered: {order_status[1]:.1f}%\")"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"\n",
"Updated Inventory:\n",
"t-shirt: 0\n",
"mug: 1\n",
"hat: 3\n",
"book: 3\n",
"keychain: 5\n"
]
}
],
"source": [
"# 9. Update the inventory by subtracting 1 for each ordered product\n",
"for product in customer_orders:\n",
" if product in inventory:\n",
" inventory[product] -= 1\n",
"\n",
"# 10. Print the updated inventory on separate lines\n",
"print(\"\\nUpdated Inventory:\")\n",
"for product, quantity in inventory.items():\n",
" print(f\"{product}: {quantity}\")"
]
},
{
"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 +245,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.9.13"
"version": "3.13.9"
}
},
"nbformat": 4,
Expand Down