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
69 changes: 0 additions & 69 deletions lab-python-functions.ipynb

This file was deleted.

166 changes: 166 additions & 0 deletions lab_function.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,166 @@
{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"id": "ac0e563a",
"metadata": {},
"outputs": [],
"source": [
"# Define a function named initialize_inventory that takes products as a parameter. Inside the function, \n",
"# 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(\"Enter the quantity for \" + product + \": \"))\n",
" inventory[product] = quantity\n",
" return inventory "
]
},
{
"cell_type": "code",
"execution_count": 2,
"id": "a4794377",
"metadata": {},
"outputs": [],
"source": [
"# Define a function named get_customer_orders that takes no parameters. Inside the function, \n",
"# 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",
"def get_customer_orders():\n",
" customer_orders = set()\n",
" add_more = \"yes\"\n",
" while add_more == \"yes\":\n",
" order = input(\"Enter the name of a product to order: \")\n",
" customer_orders.add(order)\n",
" add_more = input(\"Do you want to add another product? (yes/no): \")\n",
" return customer_orders \n"
]
},
{
"cell_type": "code",
"execution_count": 3,
"id": "fbf7229c",
"metadata": {},
"outputs": [],
"source": [
"# 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 order in customer_orders:\n",
" if order in inventory:\n",
" inventory[order] = inventory[order] - 1"
]
},
{
"cell_type": "code",
"execution_count": 4,
"id": "790e59d6",
"metadata": {},
"outputs": [],
"source": [
"# 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 = len(customer_orders)\n",
" percentage = (total / len(products)) * 100\n",
" return total,percentage\n"
]
},
{
"cell_type": "code",
"execution_count": 5,
"id": "94029a1c",
"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",
" print(\"Total products ordered:\", order_statistics[0])\n",
" print(\"Percentage ordered:\", order_statistics[1],\"%\")"
]
},
{
"cell_type": "code",
"execution_count": 6,
"id": "9a4bca63",
"metadata": {},
"outputs": [],
"source": [
"# 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(\"updated inventory:\")\n",
" for product in inventory:\n",
" print(product, \":\", inventory[product])\n"
]
},
{
"cell_type": "code",
"execution_count": 7,
"id": "9feb93a7",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Total products ordered: 1\n",
"Percentage ordered: 20.0 %\n",
"updated inventory:\n",
"t-shirt : 50\n",
"mug : 10\n",
"hat : 60\n",
"book : 22\n",
"keychain : 11\n"
]
}
],
"source": [
"# Call the functions in the appropriate sequence to execute the program and manage customer orders.\n",
"\n",
"products = [\"t-shirt\", \"mug\", \"hat\", \"book\", \"keychain\"]\n",
"\n",
"inventory = initialize_inventory(products)\n",
"\n",
"orders = get_customer_orders()\n",
"\n",
"update_inventory(orders, inventory)\n",
"\n",
"statistics = calculate_order_statistics(orders, products)\n",
"\n",
"print_order_statistics(statistics)\n",
"\n",
"print_updated_inventory(inventory)\n"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "base",
"language": "python",
"name": "python3"
},
"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.9"
}
},
"nbformat": 4,
"nbformat_minor": 5
}