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
289 changes: 287 additions & 2 deletions lab-python-functions.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
},
{
"cell_type": "markdown",
"id": "0c581062-8967-4d93-b06e-62833222f930",
"id": "340baa2d-38bd-4df9-a193-9c6a4d51dbd7",
"metadata": {
"tags": []
},
Expand Down Expand Up @@ -43,6 +43,291 @@
"\n",
"\n"
]
},
{
"cell_type": "code",
"execution_count": 2,
"id": "96af86ed-cc29-4320-b538-26a3f4899e33",
"metadata": {},
"outputs": [],
"source": [
"products=[\"t-shirt\", \"mug\", \"hat\", \"book\", \"keychain\"]"
]
},
{
"cell_type": "code",
"execution_count": 201,
"id": "3db15d02-d7e4-4790-b7ab-c9ee5908cacf",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"{'t-shirt': 2, 'mug': 2, 'hat': 2, 'book': 2, 'keychain': 2}"
]
},
"execution_count": 201,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"inventory_1"
]
},
{
"cell_type": "code",
"execution_count": 202,
"id": "c610d678-0ed4-4d05-8aea-c12cecb86426",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"{'book', 'mug'}"
]
},
"execution_count": 202,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"customer_order_1"
]
},
{
"cell_type": "code",
"execution_count": 21,
"id": "5190fc17-c203-4077-ba1f-93f05d929bf4",
"metadata": {},
"outputs": [
{
"name": "stdin",
"output_type": "stream",
"text": [
"Enter quantity: 22\n",
"Enter quantity: 2\n",
"Enter quantity: 2\n",
"Enter quantity: 2\n",
"Enter quantity: 2\n"
]
}
],
"source": [
"#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",
"def initialize_inventory(products):\n",
" inventory=dict()\n",
" for i in products:\n",
" quantity=int(input(\"Enter quantity: \"))\n",
" inventory[i]=quantity\n",
" return inventory\n",
"inventory= initialize_inventory(products)"
]
},
{
"cell_type": "code",
"execution_count": 22,
"id": "026f7d14-348f-4d23-895b-0c9859c6606e",
"metadata": {},
"outputs": [
{
"name": "stdin",
"output_type": "stream",
"text": [
"Enter name of product: mug\n",
"Enter yes or no to add another product: yes\n",
"Enter name of product: book\n",
"Enter yes or no to add another product: no\n"
]
}
],
"source": [
"def get_customer_orders():\n",
" x=len(products)\n",
" customer_orders=set()\n",
" count=0\n",
"\n",
" for count in range(x):\n",
" name=input(\"Enter name of product: \")\n",
" if name in products:\n",
" customer_orders.add(name)\n",
" ask=input(\"Enter yes or no to add another product: \")\n",
" if ask==\"yes\":\n",
" count+=1\n",
" elif ask==\"no\":\n",
" break\n",
" else:\n",
" print(\"please add available\")\n",
" \n",
" return customer_orders\n",
" \n",
"customer_orders=get_customer_orders() "
]
},
{
"cell_type": "code",
"execution_count": 25,
"id": "28030f42-4f98-4936-8a7d-6c3d0bee2f47",
"metadata": {},
"outputs": [],
"source": [
"def update_inventory(customer_orders, inventory):\n",
" for i in customer_orders:\n",
" if i in inventory:\n",
" inventory[i]-=1 \n",
" return inventory \n",
"inventory= update_inventory(customer_orders, inventory)"
]
},
{
"cell_type": "code",
"execution_count": 27,
"id": "5b1b4e48-8663-4638-b293-71d297f0f2f3",
"metadata": {},
"outputs": [],
"source": [
"def calculate_order_statistics(customer_orders, products):\n",
" total_products_ordered=len(customer_orders)\n",
" total_products= len(products)\n",
" percentage_unique_ordered=round((total_products_ordered/total_products)*100)\n",
" return total_products_ordered, percentage_unique_ordered\n",
"\n",
"order_statistics=calculate_order_statistics(customer_orders, products)\n",
" \n",
" \n",
" "
]
},
{
"cell_type": "code",
"execution_count": 28,
"id": "5556235c-f778-427c-8dda-addd40370279",
"metadata": {},
"outputs": [],
"source": [
"def print_order_statistics(order_statistics):\n",
" print(f\"Order Statistics= Total products ordered:Percentage of products ordered are {order_statistics}\")\n",
" "
]
},
{
"cell_type": "code",
"execution_count": 32,
"id": "974e016c-6f4c-4dc9-aa40-a70f715aaba3",
"metadata": {},
"outputs": [],
"source": [
"def print_updated_inventory(inventory):\n",
" print(f\"The updated inventory is {inventory}\")"
]
},
{
"cell_type": "code",
"execution_count": 26,
"id": "fa25e788-70a3-435a-8a62-8e5414756a91",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"{'t-shirt': 22, 'mug': -1, 'hat': 2, 'book': -1, 'keychain': 2}"
]
},
"execution_count": 26,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"update_inventory(customer_orders, inventory)"
]
},
{
"cell_type": "code",
"execution_count": 30,
"id": "714357e3-0b7f-4835-9472-b46344cb118d",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"(2, 40)"
]
},
"execution_count": 30,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"calculate_order_statistics(customer_orders, products)"
]
},
{
"cell_type": "code",
"execution_count": 29,
"id": "150884de-e376-4aba-a26c-7d442d364595",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Order Statistics= Total products ordered:Percentage of products ordered are (2, 40)\n"
]
}
],
"source": [
"print_order_statistics(order_statistics)"
]
},
{
"cell_type": "code",
"execution_count": 33,
"id": "9785603b-f206-4f27-ad53-3b9fc1c3e6ec",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"The updated inventory is {'t-shirt': 22, 'mug': -1, 'hat': 2, 'book': -1, 'keychain': 2}\n"
]
}
],
"source": [
"print_updated_inventory(inventory)"
]
},
{
"cell_type": "code",
"execution_count": 248,
"id": "c13f8ae4-487e-485d-9cb6-f8337d9cb2e4",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"{'t-shirt': 2, 'mug': 1, 'hat': 2, 'book': 1, 'keychain': 2}"
]
},
"execution_count": 248,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"inventory_1"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "ae9e26c5-ce93-4498-bfa5-35a3b627375e",
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
Expand All @@ -61,7 +346,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.9.13"
"version": "3.12.7"
}
},
"nbformat": 4,
Expand Down