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
231 changes: 229 additions & 2 deletions lab-python-functions.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,238 @@
"\n",
"\n"
]
},
{
"cell_type": "code",
"execution_count": 73,
"id": "89e02ac6",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"['t-shirt', 'mug', 'hat', 'book', 'keychain']"
]
},
"execution_count": 73,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"products = [\"t-shirt\", \"mug\", \"hat\", \"book\", \"keychain\"]\n",
"products"
]
},
{
"cell_type": "code",
"execution_count": 109,
"id": "4e2766eb",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"{'t-shirt': 5, 'mug': 5, 'hat': 5, 'book': 5, 'keychain': 5}"
]
},
"execution_count": 109,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"def initialize_inventory(products):\n",
" inventory = {}\n",
"\n",
" for product in products:\n",
" quantity = int(input(f\"Enter the quantity of {product}\"))\n",
" inventory[product] = quantity\n",
" return inventory\n",
"\n",
"inventory = initialize_inventory(products)\n",
"\n",
"initialize_inventory(products)"
]
},
{
"cell_type": "code",
"execution_count": 110,
"id": "49ebc54c",
"metadata": {},
"outputs": [],
"source": [
"\n",
"customer_orders = set()\n",
"\n",
"def get_customer_orders():\n",
" customer_orders = set()\n",
"\n",
" while True:\n",
"\n",
" while True:\n",
" order = input(\"Enter product name\")\n",
"\n",
" if order in products:\n",
" customer_orders.add(order)\n",
" break\n",
" else:\n",
" print(\"invalid value\")\n",
"\n",
" while True:\n",
" another_order = input(\"Would you like to add another product (yes/no)?\")\n",
" if another_order == \"no\" or another_order == \"yes\":\n",
" break\n",
" else:\n",
" print('invalid answer. please enter \"yes\" or \"no\"')\n",
"\n",
" if another_order == \"no\":\n",
" break\n",
"\n",
" return customer_orders\n",
"\n",
"customer_orders = get_customer_orders()"
]
},
{
"cell_type": "code",
"execution_count": 84,
"id": "82b877af",
"metadata": {},
"outputs": [],
"source": [
"def update_inventory(customer_orders, inventory):\n",
" for product in customer_orders:\n",
" inventory[product] -=1\n",
"\n",
" return inventory\n",
"\n",
"inventory = update_inventory(customer_orders, inventory)"
]
},
{
"cell_type": "code",
"execution_count": 111,
"id": "a2aeb0cf",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"(1, 20.0)\n"
]
}
],
"source": [
"def calculate_order_statistics(customer_orders, products):\n",
" total_products_ordered = len(customer_orders)\n",
" procentage_ordered = len(customer_orders) / len (products) * 100\n",
"\n",
" return total_products_ordered, procentage_ordered\n",
"\n",
"order_statistics = calculate_order_statistics(customer_orders, products)\n",
"\n",
"print(order_statistics)\n"
]
},
{
"cell_type": "code",
"execution_count": 112,
"id": "6c7fa6e8",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Order Statistics:\n",
"Total Products Ordered: 1\n",
"Percentage of Products Ordered: 20.0%\n"
]
}
],
"source": [
"def print_order_statistics(order_statistics):\n",
" total_products_ordered, percentage_ordered = order_statistics\n",
"\n",
" print(f\"\"\"Order Statistics:\n",
"Total Products Ordered: {total_products_ordered}\n",
"Percentage of Products Ordered: {percentage_ordered}%\"\"\")\n",
"\n",
"print_order_statistics(order_statistics)"
]
},
{
"cell_type": "code",
"execution_count": 113,
"id": "a121489f",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Updated inventory:\n",
"t-shirt 5\n",
"mug 5\n",
"hat 5\n",
"book 5\n",
"keychain 5\n"
]
}
],
"source": [
"def print_updated_inventory(inventory):\n",
" print(\"Updated inventory:\")\n",
" for product in inventory:\n",
" print(product, inventory[product])\n",
"\n",
"print_updated_inventory(inventory)"
]
},
{
"cell_type": "code",
"execution_count": 107,
"id": "1ed465e7",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Order Statistics:\n",
"Total Products Ordered: 2\n",
"Percentage of Products Ordered: 40.0%\n",
"Updated inventory:\n",
"t-shirt 5\n",
"mug 4\n",
"hat 4\n",
"book 5\n",
"keychain 5\n"
]
}
],
"source": [
"products = [\"t-shirt\", \"mug\", \"hat\", \"book\", \"keychain\"]\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)"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"display_name": "base",
"language": "python",
"name": "python3"
},
Expand All @@ -61,7 +288,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.9.13"
"version": "3.14.6"
}
},
"nbformat": 4,
Expand Down