Skip to content

lab python functions complete #425

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
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: 69 additions & 0 deletions .ipynb_checkpoints/lab-python-functions-checkpoint.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
{
"cells": [
{
"cell_type": "markdown",
"id": "25d7736c-ba17-4aff-b6bb-66eba20fbf4e",
"metadata": {},
"source": [
"# Lab | Functions"
]
},
{
"cell_type": "markdown",
"id": "0c581062-8967-4d93-b06e-62833222f930",
"metadata": {
"tags": []
},
"source": [
"## Exercise: Managing Customer Orders with Functions\n",
"\n",
"In the previous exercise, you improved the code for managing customer orders by using loops and flow control. Now, let's take it a step further and refactor the code by introducing functions.\n",
"\n",
"Follow the steps below to complete the exercise:\n",
"\n",
"1. 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",
"\n",
"2. Define a function named `get_customer_orders` that takes no parameters. Inside the function, 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",
"3. Define a function named `update_inventory` that takes `customer_orders` and `inventory` as parameters. Inside the function, implement the code for updating the inventory dictionary based on the customer orders.\n",
"\n",
"4. Define a function named `calculate_order_statistics` that takes `customer_orders` and `products` as parameters. Inside the function, implement the code for calculating the order statistics (total products ordered, and percentage of unique products ordered). The function should return these values.\n",
"\n",
"5. Define a function named `print_order_statistics` that takes `order_statistics` as a parameter. Inside the function, implement the code for printing the order statistics.\n",
"\n",
"6. Define a function named `print_updated_inventory` that takes `inventory` as a parameter. Inside the function, implement the code for printing the updated inventory.\n",
"\n",
"7. Call the functions in the appropriate sequence to execute the program and manage customer orders.\n",
"\n",
"Hints for functions:\n",
"\n",
"- Consider the input parameters required for each function and their return values.\n",
"- Utilize function parameters and return values to transfer data between functions.\n",
"- Test your functions individually to ensure they work correctly.\n",
"\n",
"\n"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"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.9.13"
}
},
"nbformat": 4,
"nbformat_minor": 5
}
115 changes: 115 additions & 0 deletions .ipynb_checkpoints/lab-python-functions-max-pisolkar-checkpoint.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
{
"cells": [
{
"cell_type": "code",
"execution_count": 36,
"id": "083ecb10-b75c-425c-b02f-7ca2521478d7",
"metadata": {},
"outputs": [],
"source": [
"products = [\"t-shirt\", \"mug\", \"hat\", \"book\", \"keychain\"]\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "b2b8513a-0b04-451d-bdde-9853c06e38de",
"metadata": {},
"outputs": [],
"source": [
"# Global list of products\n",
"products = [\"apple\", \"banana\", \"orange\", \"bread\", \"milk\"]\n",
"\n",
"def initialize_inventory(products):\n",
" inventory = {}\n",
" for product in products:\n",
" quantity = int(input(f\"Enter quantity for {product}: \"))\n",
" inventory[product] = quantity\n",
" return(inventory)\n",
"\n",
"def get_customer_orders():\n",
" customer_orders=set()\n",
" answer = input(f\"Would you like to add an item? (Yes or No)\").strip().lower()\n",
" while answer == \"yes\":\n",
" name = str(input(f\"Enter name of item:\")).strip().lower()\n",
" if name in products:\n",
" customer_orders.add(name)\n",
" else:\n",
" print(\"Item not in stock, please choose another.\")\n",
" answer = input(\"Would you like to add another item? (Yes or No): \").strip().lower()\n",
" else:print(\"Thank you for visiting the shop :)\")\n",
" print(\"The items you want to order are: \" + \", \".join(customer_orders))\n",
" return customer_orders\n",
"\n",
"def update_inventory(customer_orders, inventory):\n",
" for item in customer_orders:\n",
" if item in inventory:\n",
" inventory[item] -= 1\n",
" print(f\"{item} inventory updated. Remaining: {inventory[item]}\")\n",
" else:\n",
" print(f\"{item} is not found in the inventory.\")\n",
" return inventory\n",
"\n",
"def calculate_order_statistics (customer_orders, products):\n",
" total_number_products= len(customer_orders)\n",
" percentage_ordered = (total_number_products / len(products)) * 100\n",
" return total_number_products, percentage_ordered\n",
"\n",
"def print_order_statistics(order_stats):\n",
" print(f\"Total number of products ordered: {order_stats[0]}\")\n",
" print(f\"Total number of unique products ordered: {order_stats[0]}\")\n",
"\n",
"def print_updated_inventory(inventory):\n",
" print(\"Updated Inventory:\")\n",
" for item, qty in inventory.items():\n",
" print(f\"{item}: {qty}\")\n",
"\n",
"inventory = initialize_inventory(products)\n",
"customer_orders = get_customer_orders()\n",
"updated_inventory = update_inventory(customer_orders, inventory)\n",
"order_stats = calculate_order_statistics(customer_orders, products)\n",
" \n",
"print_order_statistics(order_stats)\n",
"print_updated_inventory(updated_inventory)\n",
" "
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "25e9bd35-a51a-4090-be8b-cb09a62a46dd",
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"id": "1528a688-452b-4cc2-afd7-b5042b8dfda7",
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python [conda env:base] *",
"language": "python",
"name": "conda-base-py"
},
"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.12.7"
}
},
"nbformat": 4,
"nbformat_minor": 5
}
115 changes: 115 additions & 0 deletions lab-python-functions-max-pisolkar.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
{
"cells": [
{
"cell_type": "code",
"execution_count": 36,
"id": "083ecb10-b75c-425c-b02f-7ca2521478d7",
"metadata": {},
"outputs": [],
"source": [
"products = [\"t-shirt\", \"mug\", \"hat\", \"book\", \"keychain\"]\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "b2b8513a-0b04-451d-bdde-9853c06e38de",
"metadata": {},
"outputs": [],
"source": [
"# Global list of products\n",
"products = [\"apple\", \"banana\", \"orange\", \"bread\", \"milk\"]\n",
"\n",
"def initialize_inventory(products):\n",
" inventory = {}\n",
" for product in products:\n",
" quantity = int(input(f\"Enter quantity for {product}: \"))\n",
" inventory[product] = quantity\n",
" return(inventory)\n",
"\n",
"def get_customer_orders():\n",
" customer_orders=set()\n",
" answer = input(f\"Would you like to add an item? (Yes or No)\").strip().lower()\n",
" while answer == \"yes\":\n",
" name = str(input(f\"Enter name of item:\")).strip().lower()\n",
" if name in products:\n",
" customer_orders.add(name)\n",
" else:\n",
" print(\"Item not in stock, please choose another.\")\n",
" answer = input(\"Would you like to add another item? (Yes or No): \").strip().lower()\n",
" else:print(\"Thank you for visiting the shop :)\")\n",
" print(\"The items you want to order are: \" + \", \".join(customer_orders))\n",
" return customer_orders\n",
"\n",
"def update_inventory(customer_orders, inventory):\n",
" for item in customer_orders:\n",
" if item in inventory:\n",
" inventory[item] -= 1\n",
" print(f\"{item} inventory updated. Remaining: {inventory[item]}\")\n",
" else:\n",
" print(f\"{item} is not found in the inventory.\")\n",
" return inventory\n",
"\n",
"def calculate_order_statistics (customer_orders, products):\n",
" total_number_products= len(customer_orders)\n",
" percentage_ordered = (total_number_products / len(products)) * 100\n",
" return total_number_products, percentage_ordered\n",
"\n",
"def print_order_statistics(order_stats):\n",
" print(f\"Total number of products ordered: {order_stats[0]}\")\n",
" print(f\"Total number of unique products ordered: {order_stats[0]}\")\n",
"\n",
"def print_updated_inventory(inventory):\n",
" print(\"Updated Inventory:\")\n",
" for item, qty in inventory.items():\n",
" print(f\"{item}: {qty}\")\n",
"\n",
"inventory = initialize_inventory(products)\n",
"customer_orders = get_customer_orders()\n",
"updated_inventory = update_inventory(customer_orders, inventory)\n",
"order_stats = calculate_order_statistics(customer_orders, products)\n",
" \n",
"print_order_statistics(order_stats)\n",
"print_updated_inventory(updated_inventory)\n",
" "
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "25e9bd35-a51a-4090-be8b-cb09a62a46dd",
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"id": "1528a688-452b-4cc2-afd7-b5042b8dfda7",
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python [conda env:base] *",
"language": "python",
"name": "conda-base-py"
},
"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.12.7"
}
},
"nbformat": 4,
"nbformat_minor": 5
}