From c76f899c77bf65d9c9fdf87a8103a61ad04fd1e3 Mon Sep 17 00:00:00 2001 From: Amanda Joseph Date: Sat, 19 Sep 2026 15:20:50 +0200 Subject: [PATCH] Lab 1, attempt 2 --- lab-python-data-structures.ipynb | 183 ++++++++++++++++++++++++++++++- 1 file changed, 180 insertions(+), 3 deletions(-) diff --git a/lab-python-data-structures.ipynb b/lab-python-data-structures.ipynb index 5b3ce9e0..4ab4c5d6 100644 --- a/lab-python-data-structures.ipynb +++ b/lab-python-data-structures.ipynb @@ -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": { @@ -68,7 +245,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.9.13" + "version": "3.13.9" } }, "nbformat": 4,