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
326 changes: 323 additions & 3 deletions lab-python-functions.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -43,13 +43,333 @@
"\n",
"\n"
]
},
{
"cell_type": "code",
"execution_count": 73,
"id": "1b3d2774-c203-4e7d-b642-bba2d9175291",
"metadata": {},
"outputs": [],
"source": [
"#1 Define a function named initialize_inventory that takes products as a parameter. \n",
"# Inside the function, implement the code for initializing the inventory dictionary using a loop and user input.\n",
"\n",
"def initialize_inventory(products):\n",
" inventory = {}\n",
" for product in products:\n",
" quantity = int(input(f\"Please enter the quantity of {product}\"))\n",
" inventory[product] = quantity\n",
" return inventory\n",
" "
]
},
{
"cell_type": "code",
"execution_count": 74,
"id": "84621429-0779-45b5-b604-141558348dfa",
"metadata": {},
"outputs": [],
"source": [
"#2 Define a function named get_customer_orders that takes no parameters. \n",
"# Inside the function, implement the code for prompting the user to enter the product names using a loop. \n",
"# The function should return the customer_orders set.\n",
"\n",
"def get_customer_orders():\n",
" customer_orders = set()\n",
" max_products = len(products)\n",
" count = 0\n",
" \n",
" # 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",
" while count < max_products: # adding this because it does not make sense to keep asking the customer to add more than the available products in the inventory, hence after 5 input, the loop should terminate\n",
" product_name = input(\"Please enter the name of a product you wish to order: \")\n",
" if product_name in products:\n",
" customer_orders.add(product_name)\n",
" count += 1\n",
" decision = input(\"Do you want to add another product to your order? yes or no: \")\n",
" while decision != \"yes\" and decision != \"no\":\n",
" print(\"Answer not accepted, question can only be answered with yes or no\")\n",
" decision = input(\"Do you want to add another product to your order? yes or no: \")\n",
" if decision != \"yes\":\n",
" break\n",
" else:\n",
" print(\"Please enter a product that is available in our shop\")\n",
"\n",
" return(customer_orders)"
]
},
{
"cell_type": "code",
"execution_count": 75,
"id": "f29c1b9c-4b3d-4a57-9738-e0642ac29be3",
"metadata": {},
"outputs": [],
"source": [
"#3 Define a function named update_inventory that takes customer_orders and inventory as parameters. \n",
"# Inside the function, implement the code for updating the inventory dictionary based on the customer orders.\n",
"\n",
"def update_inventory(customer_orders, inventory):\n",
" for product in inventory:\n",
" if product in customer_orders:\n",
" inventory[product] -= 1\n",
" return inventory\n",
" "
]
},
{
"cell_type": "code",
"execution_count": 114,
"id": "3b9662d0-763f-45f4-bc5b-b84c40ee93e2",
"metadata": {},
"outputs": [],
"source": [
"#4 Define a function named calculate_order_statistics that takes customer_orders and products as parameters. \n",
"# Inside the function, implement the code for calculating the order statistics, \n",
"# (total products ordered, and percentage of unique products ordered). The function should return these values.\n",
"\n",
"def calculate_order_statistics(customer_orders, products):\n",
" total_of_products_ordered = 0\n",
" for product in products:\n",
" if product in customer_orders: \n",
" total_of_products_ordered += inventory[product]\n",
"\n",
" # Percentage of Products Ordered: The percentage of products ordered compared to the total available products.\n",
" total_inventory = sum(inventory.values())\n",
" \n",
" percentage_of_products_ordered = (total_of_products_ordered / total_inventory)*100\n",
" return total_of_products_ordered, percentage_of_products_ordered\n",
" "
]
},
{
"cell_type": "code",
"execution_count": 124,
"id": "f5bee4a9-94d1-4e2d-a2ec-c020a126240e",
"metadata": {},
"outputs": [],
"source": [
"#5 Define a function named print_order_statistics that takes order_statistics as a parameter. \n",
"# Inside the function, implement the code for printing the order statistics.\n",
"\n",
"def print_order_statistics(order_statistics):\n",
" total_of_products_ordered, percentage_of_products_ordered = order_statistics\n",
" print(total_of_products_ordered)\n",
" print(f\"{percentage_of_products_ordered:.1f}%\")\n"
]
},
{
"cell_type": "code",
"execution_count": 125,
"id": "eee7b42f-4604-4e32-87f1-1949e97eb08f",
"metadata": {},
"outputs": [],
"source": [
"#6 Define a function named print_updated_inventory that takes inventory as a parameter. \n",
"# Inside the function, implement the code for printing the updated inventory.\n",
"\n",
"def print_updated_inventory(inventory):\n",
" print(inventory)"
]
},
{
"cell_type": "code",
"execution_count": 126,
"id": "18572ff7-b98b-4dd9-a406-e90e962cfbbc",
"metadata": {},
"outputs": [],
"source": [
"products = [\"t-shirt\", \"mug\", \"hat\", \"book\", \"keychain\"]"
]
},
{
"cell_type": "code",
"execution_count": 80,
"id": "e308eb5c-7bc3-466c-8f04-9f544a6eec04",
"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": [
"# calling the function initialize_inventory to define the inventory\n",
"inventory = initialize_inventory(products)"
]
},
{
"cell_type": "code",
"execution_count": 81,
"id": "f0d0d480-e240-4589-bc57-3b30d26b3bdf",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"{'t-shirt': 8, 'mug': 7, 'hat': 6, 'book': 5, 'keychain': 4}"
]
},
"execution_count": 81,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# quick check if inventory has filled in correctly\n",
"inventory"
]
},
{
"cell_type": "code",
"execution_count": 83,
"id": "bbb15bbc-8a56-4b53-b183-e9c250d6d947",
"metadata": {},
"outputs": [
{
"name": "stdin",
"output_type": "stream",
"text": [
"Please enter the name of a product you wish to order: mug\n",
"Do you want to add another product to your order? yes or no: book\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"Answer not accepted, question can only be answered with yes or no\n"
]
},
{
"name": "stdin",
"output_type": "stream",
"text": [
"Do you want to add another product to your order? yes or no: yes\n",
"Please enter the name of a product you wish to order: hat\n",
"Do you want to add another product to your order? yes or no: yes\n",
"Please enter the name of a product you wish to order: book\n",
"Do you want to add another product to your order? yes or no: no\n"
]
}
],
"source": [
"customer_orders = get_customer_orders()"
]
},
{
"cell_type": "code",
"execution_count": 85,
"id": "c40359cb-e7d5-461d-9fc5-3e254fe45c3b",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"{'book', 'hat', 'mug'}"
]
},
"execution_count": 85,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"customer_orders"
]
},
{
"cell_type": "code",
"execution_count": 86,
"id": "e07ec939-66f3-4a7f-bacb-552bf2fc5b8f",
"metadata": {},
"outputs": [],
"source": [
"inventory = update_inventory(customer_orders, inventory)"
]
},
{
"cell_type": "code",
"execution_count": 88,
"id": "d0141be1-329d-46c8-9fa5-e01bee0e94f8",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"{'t-shirt': 8, 'mug': 6, 'hat': 5, 'book': 4, 'keychain': 4}"
]
},
"execution_count": 88,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# quick check if inventory has been correctly updated\n",
"inventory"
]
},
{
"cell_type": "code",
"execution_count": 122,
"id": "492871f1-d50c-4b1e-9d90-d590f165dfdc",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"(15, 55.55555555555556)\n"
]
}
],
"source": [
"order_statistics = calculate_order_statistics(customer_orders, products)\n",
"stats = order_statistics\n",
"print(stats)"
]
},
{
"cell_type": "code",
"execution_count": 127,
"id": "68a899bb-5b8c-4ea0-a20f-673308a95913",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"15\n",
"55.6%\n",
"None\n"
]
}
],
"source": [
"cart_details = print_order_statistics(stats)\n",
"print(cart_details)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "78c780aa-5bd7-41b8-86a0-2ee01e1bceeb",
"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 @@ -61,7 +381,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.9.13"
"version": "3.13.5"
}
},
"nbformat": 4,
Expand Down