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
92 changes: 90 additions & 2 deletions lab-python-functions.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,99 @@
"\n",
"\n"
]
},
{
"cell_type": "code",
"execution_count": 1,
"id": "559c9293",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Total unique products ordered: 1\n",
"Percentage of products ordered: 20.0 %\n",
"t-shirt 12\n",
"mug 11\n",
"hat 12\n",
"book 12\n",
"keychain 12\n"
]
}
],
"source": [
"products = [\"t-shirt\", \"mug\", \"hat\", \"book\", \"keychain\"]\n",
"\n",
"def initialize_inventory (products):\n",
"#I had the empty dictionary as a global variable, but it is not the best way to do it,\n",
"#it should be inside the function, locally!\n",
" inventory = {}\n",
" for product in products:\n",
" quantity = int(input(\"Insert Quantity of: \" + product + \"s\"))\n",
" inventory[product] = quantity\n",
"#I had the return inside the loop. Return immediately stops the function, so that means that \n",
"#if it is inside the loop, as soon as the user gives the first input, the code stops!\n",
" return inventory\n",
"\n",
"inventory = initialize_inventory(products)\n",
"\n",
"def get_customer_orders():\n",
"#Once again, I had the set outside of the function\n",
" customer_orders = set()\n",
" ask_customer = \"yes\"\n",
" \n",
" while ask_customer == \"yes\":\n",
" order = input(\"Select a product from the products list: \")\n",
" if order in products:\n",
" customer_orders.add(order)\n",
" else:\n",
" print(\"Invalid product. Please choose one from the list.\")\n",
" ask_customer = input(\"Do you want to order another product? (yes/no): \").lower()\n",
" \n",
" return customer_orders\n",
"\n",
"customer_orders = get_customer_orders()\n",
"\n",
"#two parameters but only return one value because \n",
"#customer_orders = instructions.\n",
"#inventory = the thing being updated.\n",
"#You return only the thing that changed, which is inventory.\n",
"def update_inventory(customer_orders, inventory):\n",
" for item in customer_orders:\n",
" inventory[item] -= 1\n",
"\n",
" return inventory\n",
"\n",
"inventory = update_inventory(customer_orders, inventory)\n",
"\n",
"def calculate_order_statistics(customer_orders, products):\n",
" total_products_ordered = len(customer_orders)\n",
" total_available_products = len(products)\n",
" percentage_ordered = (total_products_ordered / total_available_products) * 100\n",
"\n",
" return total_products_ordered, percentage_ordered\n",
"\n",
"order_statistics = calculate_order_statistics(customer_orders, products)\n",
"\n",
"def print_order_statistics(order_statistics):\n",
" total_products_ordered, percentage_ordered = order_statistics\n",
" print(\"Total unique products ordered:\", total_products_ordered)\n",
" print(\"Percentage of products ordered:\", percentage_ordered, \"%\")\n",
"\n",
"print_order_statistics(order_statistics)\n",
"\n",
"def print_updated_inventory(inventory):\n",
" for product, quantity in inventory.items():\n",
" print(product, quantity)\n",
"\n",
"print_updated_inventory(inventory)"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"display_name": "base",
"language": "python",
"name": "python3"
},
Expand All @@ -61,7 +149,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.9.13"
"version": "3.13.5"
}
},
"nbformat": 4,
Expand Down