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
241 changes: 239 additions & 2 deletions lab-python-functions.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,248 @@
"\n",
"\n"
]
},
{
"cell_type": "code",
"execution_count": 40,
"id": "a2157d27",
"metadata": {},
"outputs": [],
"source": [
"# 1\n",
"def initialize_inventory(products):\n",
" inventory = {}\n",
" for product in products:\n",
" quantity = int(input(\"how many \" + product + \" are there?\"))\n",
" inventory[product] = quantity\n",
" return inventory\n",
" "
]
},
{
"cell_type": "code",
"execution_count": 41,
"id": "b93bc9bb",
"metadata": {},
"outputs": [],
"source": [
"products = [\"t-shirt\", \"mug\", \"hat\", \"book\", \"keychain\"]"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "ec624d05",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"{'t-shirt': 6, 'mug': 5, 'hat': 7, 'book': 6, 'keychain': 4}"
]
},
"execution_count": 42,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"#testing initialize_inventory \n",
"inventory = initialize_inventory(products)\n",
"inventory"
]
},
{
"cell_type": "code",
"execution_count": 43,
"id": "2e26d15c",
"metadata": {},
"outputs": [],
"source": [
"#2\n",
"def get_customer_orders():\n",
" customer_order = set()\n",
" question = input(\"Would you like to purchase an item (Y/N)?\")\n",
" while question.upper() == 'Y':\n",
" item = input(\"Which item would you like to order? (t-shirt, mug, hat, book, keychain) \")\n",
" if item in inventory:\n",
" customer_order.add(item.lower())\n",
" question = input(\"Would you like to purchase another item? \")\n",
" return customer_order"
]
},
{
"cell_type": "code",
"execution_count": 44,
"id": "6a3d8ae8",
"metadata": {},
"outputs": [],
"source": [
"#testing get_customer_orders and saving it\n",
"customer_order = get_customer_orders()"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "e73b0e09",
"metadata": {},
"outputs": [],
"source": [
"# 3\n",
"def update_inventory(customer_order_arg,inventory_arg): \n",
" for item in customer_order_arg:\n",
" inventory_arg[item] = inventory_arg[item] -1\n",
" return inventory\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "07c2fbba",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"{'t-shirt': 6, 'mug': 4, 'hat': 7, 'book': 5, 'keychain': 3}"
]
},
"execution_count": 46,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"#testing update_inventory and updating inventory\n",
"inventory_updated = update_inventory(customer_order, inventory)\n",
"inventory_updated"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "bc85db6e",
"metadata": {},
"outputs": [],
"source": [
"#4\n",
"def calculate_order_statistics(customer_order_arg, products_arg):\n",
" num_products = len(products_arg)\n",
" num_products_ordered = len(customer_order_arg)\n",
" pct_ordered_products = num_products_ordered / num_products * 100\n",
" return num_products_ordered, pct_ordered_products\n"
]
},
{
"cell_type": "code",
"execution_count": 48,
"id": "2b3d2079",
"metadata": {},
"outputs": [],
"source": [
"order_statistics = calculate_order_statistics(customer_order, products)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "ca3ecbd8",
"metadata": {},
"outputs": [],
"source": [
"#5\n",
"def print_order_statistics(order_statistics):\n",
" num_products_ordered, pct_ordered_products = order_statistics\n",
" print(f\"The total ordered items are {num_products_ordered} and the percentage of them is {pct_ordered_products}%\")"
]
},
{
"cell_type": "code",
"execution_count": 50,
"id": "c11c605b",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"The total ordered items are 3 and the percentage of them is 60.0%\n"
]
}
],
"source": [
"print_order_statistics(order_statistics)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "e13b76c5",
"metadata": {},
"outputs": [],
"source": [
"#6\n",
"def print_updated_inventory(updated_inventory):\n",
" print(updated_inventory)"
]
},
{
"cell_type": "code",
"execution_count": 52,
"id": "c185809b",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"{'t-shirt': 6, 'mug': 4, 'hat': 7, 'book': 5, 'keychain': 3}\n"
]
}
],
"source": [
"new_inventory = print_updated_inventory(inventory_updated)\n",
"new_inventory"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "5a77fc2f",
"metadata": {},
"outputs": [],
"source": [
"#7\n",
"products = [\"t-shirt\", \"mug\", \"hat\", \"book\", \"keychain\"]\n",
"inventory = initialize_inventory(products)\n",
"customer_order = get_customer_orders()\n",
"inventory = update_inventory(inventory, customer_order)\n",
"order_statistics = calculate_order_statistics(customer_order, products)\n",
"print_order_statistics(order_statistics)\n",
"print_updated_inventory(inventory)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "99379a13",
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"id": "64660e8f",
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
Expand All @@ -61,7 +298,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.9.13"
"version": "3.13.7"
}
},
"nbformat": 4,
Expand Down