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
228 changes: 226 additions & 2 deletions lab-python-functions.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,235 @@
"\n",
"\n"
]
},
{
"cell_type": "code",
"execution_count": 2,
"id": "c57d7eca",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"<class 'builtin_function_or_method'> <built-in function print>\n"
]
}
],
"source": [
"\n",
"import builtins\n",
"builtins.print(type(print), print) "
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "fbd99e86",
"metadata": {},
"outputs": [],
"source": [
"products = [\"t-shirt\", \"mug\", \"hat\", \"book\", \"keychain\"]\n",
"def initialize_inventory(products):\n",
" inventory = {}\n",
" for product in products:\n",
" inventory[product] = int(input(f\"How many {product}s do you have in storage?\"))\n",
" return inventory\n",
"\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "d6131b0c",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"{'t-shirt': 23, 'mug': 56, 'hat': 49, 'book': 27, 'keychain': 75}\n"
]
}
],
"source": [
"inventory = initialize_inventory(products)\n",
"print(inventory)\n"
]
},
{
"cell_type": "code",
"execution_count": 36,
"id": "184c3b79",
"metadata": {},
"outputs": [],
"source": [
"products = [\"t-shirt\", \"mug\", \"hat\", \"book\", \"keychain\"]\n",
"def get_customer_orders():\n",
" products = []\n",
" still_buying = True\n",
" while still_buying:\n",
" product = input(\"What products would you like to buy?\")\n",
" customer_answer = input(\"Do you wish to buy another product? (Y/N)\")\n",
"\n",
" if customer_answer == \"N\":\n",
" still_buying = False\n",
"\n",
" products.append(product)\n",
" return products\n",
" "
]
},
{
"cell_type": "code",
"execution_count": 38,
"id": "7ab0275b",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"['t-shirt', 'mug', 'hat', 'book', 'keychain']\n"
]
}
],
"source": [
"customer_orders = get_customer_orders()\n",
"print(products)"
]
},
{
"cell_type": "code",
"execution_count": 3,
"id": "b756ee2c",
"metadata": {},
"outputs": [],
"source": [
"def update_inventory(customer_orders, inventory):\n",
" for product in customer_orders:\n",
" if product in inventory:\n",
" inventory[product] = 0\n",
" else:\n",
" print(f\"{product} is not in inventory!\")\n",
" \n",
" return inventory"
]
},
{
"cell_type": "code",
"execution_count": 7,
"id": "b80384cc",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"{'t-shirt': 0, 'mug': 56, 'hat': 49, 'book': 27, 'keychain': 75}"
]
},
"execution_count": 7,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"customer_orders = ['t-shirt', 'mug', 'hat', 'book', 'keychain']\n",
"inventory = {'t-shirt': 23, 'mug': 56, 'hat': 49, 'book': 27, 'keychain': 75}\n",
"update_inventory(\n",
" customer_orders= customer_orders,\n",
" inventory=inventory \n",
")"
]
},
{
"cell_type": "code",
"execution_count": 13,
"id": "4b9d9ba1",
"metadata": {},
"outputs": [],
"source": [
"\n",
"def calculate_order_statistics(customer_orders, products):\n",
" total_ordered = len(customer_orders)\n",
"\n",
" if len(products) > 0:\n",
" percentage_ordered = (len(customer_orders) / len(products)) * 100\n",
" else:\n",
" percentage_ordered = 0\n",
"\n",
" return {\n",
" \"total_ordered\": total_ordered,\n",
" \"percentage_ordered\": percentage_ordered\n",
" }\n"
]
},
{
"cell_type": "code",
"execution_count": 21,
"id": "7140be35",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Total Products Ordered: 5\n",
"Percentage of Products Ordered: 100.00%\n"
]
}
],
"source": [
"inventory={'t-shirt': 0, 'mug': 56, 'hat': 49, 'book': 27, 'keychain': 75}\n",
"products = [\"t-shirt\", \"mug\", \"hat\", \"book\", \"keychain\"]\n",
"customer_orders = {\"t-shirt\", \"mug\", \"hat\", \"book\", \"keychain\"}\n",
"order_statistics = calculate_order_statistics(customer_orders, products)\n",
"print_order_statistics(order_statistics)\n"
]
},
{
"cell_type": "code",
"execution_count": 23,
"id": "f06eb63c",
"metadata": {},
"outputs": [],
"source": [
"def print_order_statistics(order_statistics):\n",
" print(f\"Total Products Ordered: {order_statistics['total_ordered']}\")\n",
" print(f\"Percentage of Products Ordered: {order_statistics['percentage_ordered']:.2f}%\") \n",
"\n"
]
},
{
"cell_type": "code",
"execution_count": 22,
"id": "5c137855",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"{'t-shirt': 0, 'mug': 56, 'hat': 49, 'book': 27, 'keychain': 75}\n"
]
}
],
"source": [
"print_updated_inventory(inventory)\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "3bcad495",
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"display_name": "base",
"language": "python",
"name": "python3"
},
Expand All @@ -61,7 +285,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.9.13"
"version": "3.13.5"
}
},
"nbformat": 4,
Expand Down