Skip to content
Open
Show file tree
Hide file tree
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
253 changes: 253 additions & 0 deletions .ipynb_checkpoints/lab-python-functions-checkpoint.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,253 @@
{
"cells": [
{
"cell_type": "markdown",
"id": "25d7736c-ba17-4aff-b6bb-66eba20fbf4e",
"metadata": {},
"source": [
"# Lab | Functions"
]
},
{
"cell_type": "markdown",
"id": "0c581062-8967-4d93-b06e-62833222f930",
"metadata": {
"tags": []
},
"source": [
"## Exercise: Managing Customer Orders with Functions\n",
"\n",
"In the previous exercise, you improved the code for managing customer orders by using loops and flow control. Now, let's take it a step further and refactor the code by introducing functions.\n",
"\n",
"Follow the steps below to complete the exercise:\n",
"\n",
"1. Define a function named `initialize_inventory` that takes `products` as a parameter. Inside the function, implement the code for initializing the inventory dictionary using a loop and user input.\n",
"\n",
"2. Define a function named `get_customer_orders` that takes no parameters. Inside the function, implement the code for prompting the user to enter the product names using a loop. The function should return the `customer_orders` set.\n",
"\n",
"3. Define a function named `update_inventory` that takes `customer_orders` and `inventory` as parameters. Inside the function, implement the code for updating the inventory dictionary based on the customer orders.\n",
"\n",
"4. Define a function named `calculate_order_statistics` that takes `customer_orders` and `products` as parameters. Inside the function, implement the code for calculating the order statistics (total products ordered, and percentage of unique products ordered). The function should return these values.\n",
"\n",
"5. Define a function named `print_order_statistics` that takes `order_statistics` as a parameter. Inside the function, implement the code for printing the order statistics.\n",
"\n",
"6. Define a function named `print_updated_inventory` that takes `inventory` as a parameter. Inside the function, implement the code for printing the updated inventory.\n",
"\n",
"7. Call the functions in the appropriate sequence to execute the program and manage customer orders.\n",
"\n",
"Hints for functions:\n",
"\n",
"- Consider the input parameters required for each function and their return values.\n",
"- Utilize function parameters and return values to transfer data between functions.\n",
"- Test your functions individually to ensure they work correctly.\n",
"\n",
"\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "40e1d6ad-fa61-43d2-84a5-b68844b7c778",
"metadata": {},
"outputs": [],
"source": [
"products = [\"t-shirt\", \"mug\", \"hat\", \"book\", \"keychain\"]"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "327bde1e-5ffe-4d17-93dc-207741327ea0",
"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",
"def initialize_inventory(products):\n",
" inventory = {}\n",
" \"\"\"Shows the number of each item\"\"\"\n",
" for item in products:\n",
" number_of_items = int(input(\"Number of \" + item + \": \")) \n",
" inventory[item] = number_of_items\n",
" print (inventory)\n",
" return inventory "
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "35135cb7-d43b-4537-b5ea-f0ba9d2a3bf6",
"metadata": {},
"outputs": [],
"source": [
"inventory = initialize_inventory(products)\n",
"inventory"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "7a2437ca-3681-49e0-8898-fe6f4067b1fd",
"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",
" answer = \"yes\"\n",
" while answer == \"yes\":\n",
" product = input(\"Choose a product: \")\n",
" customer_orders.add(product)\n",
" answer = input(\"Do you want something else? (yes/no): \").lower()\n",
" while answer not in [\"yes\", \"no\"]:\n",
" answer = input(\"Do you want something else? (yes/no): \").lower()\n",
" print (\"customer_ order\", customer_orders) \n",
" return customer_orders"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "d088eabd-adf4-4a04-a61e-fd3f4326ccd8",
"metadata": {},
"outputs": [],
"source": [
"customer_orders = get_customer_orders()"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "86be53b5-2c9a-4f21-bd05-cccb95e47b6a",
"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 updated_inventory (customer_orders, inventory):\n",
" for item in customer_orders:\n",
" if item in inventory and inventory[item] > 0:\n",
" inventory[item] -= 1\n",
" return inventory \n"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "2c940a7e-9af8-4aa5-a9d3-86739c75f715",
"metadata": {},
"outputs": [],
"source": [
"inventory = updated_inventory(customer_orders, inventory)\n",
"print(inventory)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "941ac3fb-0f28-480c-bf8b-795e30f987b6",
"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). \n",
"#The function should return these values.\n",
"\n",
"def calculate_order_statistics(customer_orders, products):\n",
" total = len(customer_orders)\n",
" percentage = len(customer_orders) * 100 / len(products)\n",
" return total, percentage"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "ad5111f1-f623-4dfb-ae9d-8015a30da88d",
"metadata": {},
"outputs": [],
"source": [
"order_statistics = calculate_order_statistics(customer_orders, products)\n",
"print(order_statistics)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "8caaeaef-81c9-4dc4-98a0-dba2558dd29f",
"metadata": {},
"outputs": [],
"source": [
"# 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, percentage = order_statistics\n",
" print(total)\n",
" print(percentage)\n",
"\n",
"stats = calculate_order_statistics(customer_orders, products)\n",
"print(f\"Total customer products: {stats[0]}\") # total\n",
"print(f\"Percentage: {stats[1]}\") # percentage \n",
" \n"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "f1d59425-db7c-4b75-bf22-ab8ee0403f06",
"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": null,
"id": "0156e6a4-0983-4837-a047-b7554944e5ef",
"metadata": {},
"outputs": [],
"source": [
"print_updated_inventory (inventory)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "0242022c-ead2-4c8c-94d5-ff986479ae10",
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python [conda env:base] *",
"language": "python",
"name": "conda-base-py"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.13.5"
}
},
"nbformat": 4,
"nbformat_minor": 5
}
Binary file added anaconda_projects/db/project_filebrowser.db
Binary file not shown.
Loading