Skip to content

Solved functions lab #437

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
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
280 changes: 279 additions & 1 deletion lab-python-functions.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,284 @@
"\n",
"\n"
]
},
{
"cell_type": "code",
"execution_count": 156,
"id": "09b021ef-204c-4b65-9b5f-663addd99563",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"['t-shirt', 'mug', 'hat', 'book', 'keychain']"
]
},
"execution_count": 156,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"products = [\"t-shirt\", \"mug\", \"hat\", \"book\", \"keychain\"]\n",
"\n",
"products"
]
},
{
"cell_type": "code",
"execution_count": 157,
"id": "9ea48860-ccc6-4e87-8e6a-3f13f7ba6b09",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Insert the quantity of product: \n"
]
},
{
"name": "stdin",
"output_type": "stream",
"text": [
"t-shirt: 100\n",
"mug: 100\n",
"hat: 100\n",
"book: 100\n",
"keychain: 100\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"{'t-shirt': 100, 'mug': 100, 'hat': 100, 'book': 100, 'keychain': 100}\n"
]
}
],
"source": [
"def inizialize_inventory(products):\n",
" inventory = {}\n",
" print(f\"Insert the quantity of product: \")\n",
" for product in products:\n",
" quantity = int(input(f\"{product}: \"))\n",
" inventory[product] = quantity \n",
" return inventory\n",
"\n",
"inventory = inizialize_inventory(products)\n",
"print(inventory)"
]
},
{
"cell_type": "code",
"execution_count": 121,
"id": "b7e3db56-f8a0-4aad-97d2-5e22a5d0c899",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Insert the name of the product to order: \n"
]
},
{
"name": "stdin",
"output_type": "stream",
"text": [
"Product: mug\n",
"Quantity of mug: 10\n",
"Do you want to add a new product? Yes/No : Yes\n",
"Product: hat\n",
"Quantity of hat: 10\n",
"Do you want to add a new product? Yes/No : NO\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"You have finished your order\n"
]
}
],
"source": [
"def get_customer_orders():\n",
" customer_orders = set()\n",
" print(\"Insert the name of the product to order: \")\n",
" while True:\n",
" order = input(f\"Product: \")\n",
" \n",
" if order in products:\n",
" quantity_order = int(input(f\"Quantity of {order}: \"))\n",
" customer_orders.add((order,quantity_order))\n",
" \n",
" yes_no = input(\"Do you want to add a new product? Yes/No : \")\n",
" if yes_no != \"Yes\":\n",
" print(\"You have finished your order\")\n",
" break \n",
" else:\n",
" print(\"Invalid product.\") \n",
" return customer_orders\n",
"\n",
"customer_orders = get_customer_orders()"
]
},
{
"cell_type": "code",
"execution_count": 158,
"id": "74815837-d669-47ab-b59f-cda0899c47f1",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"{('hat', 10), ('mug', 10)}"
]
},
"execution_count": 158,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"customer_orders"
]
},
{
"cell_type": "code",
"execution_count": 271,
"id": "bd94b0fd-c95b-4549-97c7-fbc4c459f065",
"metadata": {},
"outputs": [],
"source": [
"def calculate_order_statistics(customer_orders, products):\n",
" sum_order_prod = sum(qty for _, qty in customer_orders)\n",
" sum_invent_prod = sum(inventory.values())\n",
" print(f\"Total of products ordered: {sum_order_prod}\")\n",
" percentage = (sum_order_prod / sum_invent_prod) * 100\n",
" print(f\"Percentage of products ordered: {percentage:.2f}%\")\n",
" return (sum_order_prod, percentage)"
]
},
{
"cell_type": "code",
"execution_count": 276,
"id": "cd029576-1b06-44b4-9158-806bb8ee08d7",
"metadata": {},
"outputs": [],
"source": [
"#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",
"def print_order_statistics(order_statistics):\n",
" sum_order_prod, percentage = order_statistics\n",
" print({sum_order_prod} , {percentage})"
]
},
{
"cell_type": "code",
"execution_count": 277,
"id": "2e2ae312-e184-41a1-935f-77ef404a2fbf",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"{20} {4.0}\n"
]
}
],
"source": [
"print_order_statistics(order_statistics)"
]
},
{
"cell_type": "code",
"execution_count": 225,
"id": "7323aef3-95bc-4b6c-ac61-2db908855d74",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"{'t-shirt': 100, 'mug': 30, 'hat': 30, 'book': 100, 'keychain': 100}\n"
]
}
],
"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 product, quantity_order in customer_orders:\n",
" if product in inventory:\n",
" inventory[product] -= quantity_order\n",
" if inventory[product] < 0:\n",
" inventory[product] = 0\n",
"\n",
" return inventory\n",
"\n",
"inventory = update_inventory(customer_orders, inventory)\n",
"print(inventory)"
]
},
{
"cell_type": "code",
"execution_count": 226,
"id": "7af207ba-df96-4314-aa12-ef86f2d78bc7",
"metadata": {},
"outputs": [],
"source": [
"#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",
"def print_updated_inventory(inventory):\n",
" print(\"Update_inventory: \")\n",
" for product, quantity in inventory.items():\n",
" print(f\"{product}: {quantity} \")\n",
" "
]
},
{
"cell_type": "code",
"execution_count": 229,
"id": "1f0478c1-0cfa-4407-8dca-54472c0f1113",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Update_inventory: \n",
"t-shirt: 100 \n",
"mug: 30 \n",
"hat: 30 \n",
"book: 100 \n",
"keychain: 100 \n"
]
}
],
"source": [
"print_updated_inventory(inventory)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "bc366a61-91c8-4723-9b00-cb541c17c986",
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"id": "30b94608-0db2-444e-8448-2c0f85a26771",
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
Expand All @@ -61,7 +339,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.9.13"
"version": "3.12.7"
}
},
"nbformat": 4,
Expand Down