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
152 changes: 150 additions & 2 deletions lab-python-functions.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,159 @@
"\n",
"\n"
]
},
{
"cell_type": "code",
"execution_count": 35,
"id": "6df02b54",
"metadata": {},
"outputs": [],
"source": [
"products = [\"t-shirt\", \"mug\", \"hat\", \"book\", \"keychain\"]\n",
"\n",
"# Exercise number 1\n",
"def initialize_inventory(products):\n",
" inventory = {}\n",
"\n",
" for product in products:\n",
" quantity = int(input(f\"Enter quantity for {product}: \"))\n",
" inventory[product] = quantity\n",
"\n",
" return inventory\n"
]
},
{
"cell_type": "code",
"execution_count": 36,
"id": "6d493778",
"metadata": {},
"outputs": [],
"source": [
"# Exercise number 2\n",
"def get_customer_orders():\n",
" customer_orders = set()\n",
"\n",
" add_another = \"yes\"\n",
"\n",
" while add_another == \"yes\":\n",
" order = input(\"Please input the name of the product: \").lower().strip()\n",
" customer_orders.add(order)\n",
"\n",
" add_another = input(\"Do you want another product? (yes/no): \").lower().strip()\n",
"\n",
" return customer_orders"
]
},
{
"cell_type": "code",
"execution_count": 37,
"id": "2e61470b",
"metadata": {},
"outputs": [],
"source": [
"# Exercise number 3\n",
"def update_inventory(customer_orders, inventory):\n",
" for product in customer_orders:\n",
" if product in inventory:\n",
" inventory[product] -= 1\n",
" \n",
" return inventory"
]
},
{
"cell_type": "code",
"execution_count": 38,
"id": "45690d3b",
"metadata": {},
"outputs": [],
"source": [
"# Exercise number 4\n",
"def calculate_order_statistics(customer_orders, products):\n",
" total_order = len(customer_orders)\n",
" rate = (total_order / len(products)) * 100\n",
"\n",
" return total_order, rate"
]
},
{
"cell_type": "code",
"execution_count": 39,
"id": "e446f5a9",
"metadata": {},
"outputs": [],
"source": [
"# Exercise number 5\n",
"def print_order_statistics(order_statistics):\n",
" total_order, rate = order_statistics\n",
" \n",
" print(f\"Total Ordered: {total_order}\")\n",
" print(f\"Percentagem of products ordered: {rate}\")"
]
},
{
"cell_type": "code",
"execution_count": 40,
"id": "42a641d9",
"metadata": {},
"outputs": [],
"source": [
"# Exercise number 6\n",
"def print_updated_inventory(inventory):\n",
" print(\"\\nUpdated inventory: \")\n",
" for product, quantity in inventory.items():\n",
" print(f\"{product}: {quantity}\")"
]
},
{
"cell_type": "code",
"execution_count": 41,
"id": "9932bcc3",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Total Ordered: 1\n",
"Percentagem of products ordered: 20.0\n",
"\n",
"Updated inventory: \n",
"t-shirt: 10\n",
"mug: 10\n",
"hat: 10\n",
"book: 9\n",
"keychain: 10\n"
]
}
],
"source": [
"# Exercise number 7\n",
"\n",
"inventory = initialize_inventory(products)\n",
"\n",
"customer_orders = get_customer_orders()\n",
"\n",
"inventory = update_inventory(customer_orders, inventory)\n",
"\n",
"order_statistics = calculate_order_statistics(customer_orders, products)\n",
"\n",
"print_order_statistics(order_statistics)\n",
"\n",
"print_updated_inventory(inventory)\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "01493c6b",
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
Expand All @@ -61,7 +209,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.9.13"
"version": "3.14.5"
}
},
"nbformat": 4,
Expand Down