Skip to content

Finished lab #466

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
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
83 changes: 81 additions & 2 deletions lab-python-functions.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,90 @@
"\n",
"\n"
]
},
{
"cell_type": "code",
"execution_count": 62,
"id": "983e99d7",
"metadata": {},
"outputs": [],
"source": [
"# Define a function named `initialize_inventory` that takes `products` as a parameter.\n",
"# Initializes the inventory dictionary using a loop and user input.\n",
"def initialize_inventory(products):\n",
" inventory = {}\n",
" for product in products:\n",
" value = int(input(f'How many {product}s are in the inventory? '))\n",
" inventory[product] = value\n",
" return inventory\n",
"# Define a function named `get_customer_orders` that takes no parameters. Inside the function, implement \n",
"# the code for prompting the user to enter the product names using a loop. The function should return the\n",
"# `customer_orders` set.\n",
"def get_customer_orders():\n",
" customer_orders = set()\n",
" answer = 'yes'\n",
" while answer == 'yes':\n",
" customer_orders.add(input('Please type a product you would like to purchase'))\n",
" answer = input('Would you like to add another product to your order? (yes/no)')\n",
" while answer != 'yes' and answer!= 'no':\n",
" print('That is an invalid input, please answer yes or no')\n",
" answer = input('Would you like to add another product to your order? (yes/no)')\n",
" return customer_orders\n",
"# 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",
"def update_inventory(customer_orders, inventory):\n",
" for item in customer_orders:\n",
" inventory[item] -= 1\n",
" return inventory\n",
"# Define a function named `calculate_order_statistics` that takes `customer_orders` and `products` \n",
"# as parameters. 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",
"def calculate_order_statistics(customer_orders, products):\n",
" total_products_ordered = len(customer_orders)\n",
" percent_unique = total_products_ordered/len(products)*100\n",
" order_statistics = [total_products_ordered, percent_unique]\n",
" return order_statistics\n",
"# Define a function named `print_order_statistics` that takes `order_statistics` as a parameter. Inside the \n",
"# function, implement the code for printing the order statistics.\n",
"def print_order_statistics(order_statistics):\n",
" print(f'The customer ordered {order_statistics[0]} products')\n",
" print(f'This represents the {order_statistics[1]}% of products in the inventory')\n",
"# Define a function named `print_updated_inventory` that takes `inventory` as a parameter. Inside the function, \n",
"# implement the code for printing the updated inventory.\n",
"def print_updated_inventory(inventory):\n",
" print(f'The updated inventory after customer order is {inventory}')\n"
]
},
{
"cell_type": "code",
"execution_count": 63,
"id": "0231d252",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"The customer ordered 1 products\n",
"This represents the 33.33333333333333% of products in the inventory\n",
"The updated inventory after customer order is {'shirt': 5, 'book': 4, 'bottle': 5}\n"
]
}
],
"source": [
"products = ['shirt', 'book', 'bottle']\n",
"inventory = initialize_inventory(products)\n",
"customer_orders = get_customer_orders()\n",
"inventory = update_inventory(customer_orders, inventory)\n",
"order_statistics = calculate_order_statistics(customer_orders,products)\n",
"print_order_statistics(order_statistics)\n",
"print_updated_inventory(inventory)\n"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"display_name": "base",
"language": "python",
"name": "python3"
},
Expand All @@ -61,7 +140,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.9.13"
"version": "3.13.5"
}
},
"nbformat": 4,
Expand Down