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
82 changes: 80 additions & 2 deletions lab-python-data-structures.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -50,11 +50,89 @@
"\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": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Your order includes: {'keychain', 't-shirt', 'hat'}\n",
"Order Statistics:\n",
"Total Products Ordered: 3\n",
"Percentage of Products Ordered: 60.00%\n",
"Updated Inventory:\n",
"t-shirt: 9\n",
"mug: 10\n",
"hat: 9\n",
"book: 10\n",
"keychain: 9\n"
]
}
],
"source": [
"# We're starting by creating a list with our venture items\n",
"product = [\"t-shirt\", \"mug\", \"hat\", \"book\", \"keychain\"]\n",
"\n",
"# We're creating an empty dictionary to be our inventory\n",
"inventory = {}\n",
"\n",
"# We're now asking the user input for the stock of each product\n",
"for item in product:\n",
" stock = int(input(f\"Enter the stock for {item}: \"))\n",
" inventory[item] = stock # Adding the item and its stock to the inventory dictionary\n",
"\n",
"# Now, we're creating an empty set to serve as customers orders\n",
"customer_orders = set()\n",
"\n",
"# We're asking the user to input 3 items they want to order and adding them to the orders set\n",
"for i in range(3):\n",
" item = input(\"Enter a product from the catalog you want to order: \")\n",
" # Checking the input validity\n",
" while item not in product:\n",
" print(\"Invalid product. Please choose from the catalog.\")\n",
" item = input(\"Enter a product from the catalog you want to order: \")\n",
" item = item.lower() # Converting input to lowercase for consistency\n",
"\n",
" # Adding the valid item to the customer's orders\n",
" customer_orders.add(item)\n",
"\n",
"# We're printing the customer's orders\n",
"print(f\"Your order includes: {customer_orders}\")\n",
"\n",
"# Now, we need to perform some statistics calculations on the customer's orders based on the inventory\n",
"total_products_ordered = len(customer_orders) # Total number of products ordered in the customer's order\n",
"# total_stock_available = sum(inventory[item] for item in customer_orders) # Total stock available\n",
"\n",
"# Percentage of products ordered from the catalog\n",
"percentage_of_products_ordered = total_products_ordered / len(product) * 100\n",
"\n",
"# We're storing the statistics in a tuple\n",
"order_status = (total_products_ordered, percentage_of_products_ordered)\n",
"\n",
"# Printing the statistics\n",
"print(f\"\"\"Order Statistics:\n",
"Total Products Ordered: {order_status[0]}\n",
"Percentage of Products Ordered: {order_status[1]:.2f}%\"\"\")\n",
"\n",
"# Udating the inventory by reducing the stock of ordered items by 1\n",
"for item in customer_orders:\n",
" if inventory[item] > 0:\n",
" inventory[item] -= 1 \n",
"\n",
"# Printing the updated inventory\n",
"print(\"Updated Inventory:\")\n",
"for item, stock in inventory.items():\n",
" print(f\"{item}: {stock}\")\n"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
Expand All @@ -68,7 +146,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.9.13"
"version": "3.9.6"
}
},
"nbformat": 4,
Expand Down