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
250 changes: 249 additions & 1 deletion lab-python-functions.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,254 @@
"\n",
"\n"
]
},
{
"cell_type": "code",
"execution_count": 19,
"id": "ab63b15e-b917-4ac1-bcac-debf233ee5ba",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"['t-shirt', 'mug', 'hat', 'book', 'keychain']"
]
},
"execution_count": 19,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"products = [\"t-shirt\", \"mug\", \"hat\", \"book\", \"keychain\"]\n",
"products"
]
},
{
"cell_type": "code",
"execution_count": 20,
"id": "72cd0621-a051-47a1-ad73-01924a7ce63f",
"metadata": {},
"outputs": [],
"source": [
"# 1.\n",
"def initialize_inventory(products):\n",
" inventory = {}\n",
" for product in products:\n",
" value = int(input(f\"Number of {product}:\"))\n",
" inventory[product] = value\n",
" return inventory\n",
" "
]
},
{
"cell_type": "code",
"execution_count": 21,
"id": "4f678249-6797-4c16-a3b2-5d71279746a0",
"metadata": {},
"outputs": [],
"source": [
"# 2.\n",
"def get_customer_orders():\n",
" customer_orders = set()\n",
" continue_adding = \"yes\"\n",
"\n",
" while continue_adding == \"yes\":\n",
" product = input(\"Enter the name of a product: \")\n",
" product = product.lower()\n",
"\n",
" if product in products: \n",
" customer_orders.add(product) \n",
" else:\n",
" print(\"This product is not available\")\n",
" continue_adding = input(\"Do you want to continue adding products? yes/no: \").lower() \n",
" \n",
" return customer_orders"
]
},
{
"cell_type": "code",
"execution_count": 22,
"id": "978093db-0e85-4a90-aaa2-388eccd49635",
"metadata": {},
"outputs": [],
"source": [
"# 3.\n",
"def update_inventory(customer_orders,inventory):\n",
" for product in customer_orders: \n",
" inventory[product] = inventory[product]-1\n",
" return inventory "
]
},
{
"cell_type": "code",
"execution_count": 23,
"id": "3aa06beb-e5d3-4e5e-a79a-ff5a7d886ec4",
"metadata": {},
"outputs": [],
"source": [
"# 4.\n",
"def calculate_order_statistics(customer_orders, products):\n",
" total = (len(customer_orders))\n",
" percentage = len(customer_orders)/len(products)*100\n",
" order_status = (total, percentage)\n",
" return order_status\n",
" "
]
},
{
"cell_type": "code",
"execution_count": 24,
"id": "f1d59ae8-88ac-4bb9-a4c9-2968595dbae7",
"metadata": {},
"outputs": [],
"source": [
"# 5.\n",
"def print_order_statistics(order_statistics):\n",
" print(\"Order Statistics:\")\n",
" print(f\"Total products ordered: {order_statistics[0]}\")\n",
" print(f\"Percentage of products ordered: {order_statistics[1]}%\")\n"
]
},
{
"cell_type": "code",
"execution_count": 25,
"id": "f9fc6b12-680f-4ed2-85a5-7832feeb3586",
"metadata": {},
"outputs": [],
"source": [
"# 6.\n",
"def print_updated_inventory(inventory):\n",
" print(inventory)"
]
},
{
"cell_type": "code",
"execution_count": 26,
"id": "2034ce46-aa75-42cd-b78f-f0d8d798900f",
"metadata": {},
"outputs": [
{
"name": "stdin",
"output_type": "stream",
"text": [
"Number of t-shirt: 10\n",
"Number of mug: 10\n",
"Number of hat: 10\n",
"Number of book: 10\n",
"Number of keychain: 10\n"
]
}
],
"source": [
"inventory = initialize_inventory(products)"
]
},
{
"cell_type": "code",
"execution_count": 27,
"id": "3161c44f-fd7a-4a51-a855-99060f8f6220",
"metadata": {},
"outputs": [
{
"name": "stdin",
"output_type": "stream",
"text": [
"Enter the name of a product: Mug\n",
"Do you want to continue adding products? yes/no: yes\n",
"Enter the name of a product: banana\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"This product is not available\n"
]
},
{
"name": "stdin",
"output_type": "stream",
"text": [
"Do you want to continue adding products? yes/no: Yes\n",
"Enter the name of a product: Hat\n",
"Do you want to continue adding products? yes/no: yes\n",
"Enter the name of a product: mug\n",
"Do you want to continue adding products? yes/no: yes\n",
"Enter the name of a product: Book\n",
"Do you want to continue adding products? yes/no: No\n"
]
}
],
"source": [
"customer_orders = get_customer_orders()"
]
},
{
"cell_type": "code",
"execution_count": 31,
"id": "30874f15-ed48-499f-bc84-b4634333c749",
"metadata": {},
"outputs": [],
"source": [
"inventory = update_inventory(customer_orders, inventory)"
]
},
{
"cell_type": "code",
"execution_count": 32,
"id": "e07bd44d-2c32-4560-a4a9-233663865f57",
"metadata": {},
"outputs": [],
"source": [
"order_statistics = calculate_order_statistics(customer_orders, products)"
]
},
{
"cell_type": "code",
"execution_count": 33,
"id": "f5685b28-2cd8-4679-96cb-ae2b4d149c23",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Order Statistics:\n",
"Total products ordered: 3\n",
"Percentage of products ordered: 60.0%\n"
]
}
],
"source": [
"print_order_statistics(order_statistics)"
]
},
{
"cell_type": "code",
"execution_count": 34,
"id": "b6406f7a-c63d-4faa-b4e3-df4778b186f5",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"{'t-shirt': 10, 'mug': 8, 'hat': 8, 'book': 8, 'keychain': 10}\n"
]
}
],
"source": [
"print_updated_inventory(inventory)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "ac190a99-2b36-4acc-a1ec-eebb2268936d",
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
Expand All @@ -61,7 +309,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.9.13"
"version": "3.13.5"
}
},
"nbformat": 4,
Expand Down