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
323 changes: 322 additions & 1 deletion lab-python-functions.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,327 @@
"\n",
"\n"
]
},
{
"cell_type": "code",
"execution_count": 99,
"id": "de2abae8-4e51-4daf-9f56-eabb7ff14178",
"metadata": {},
"outputs": [],
"source": [
"products = [\"t-shirt\", \"mug\", \"hat\", \"book\", \"keychain\"]"
]
},
{
"cell_type": "code",
"execution_count": 101,
"id": "7b1a8614-21c4-447c-b976-d9f7d7a29cdf",
"metadata": {},
"outputs": [],
"source": [
"inventory = {}"
]
},
{
"cell_type": "code",
"execution_count": 102,
"id": "ab786eff-e9d6-456f-a4e8-db20b5bee081",
"metadata": {},
"outputs": [],
"source": [
"customer_orders = set({})"
]
},
{
"cell_type": "code",
"execution_count": 103,
"id": "be14d87a-3882-462e-b3fd-9c96038271ba",
"metadata": {},
"outputs": [],
"source": [
"# Define a function named initialize_inventory that takes products as a parameter. \n",
"# Inside the function, implement the code for initializing the inventory dictionary using a loop and user input.\n",
"\n",
"def initialize_inventory(products):\n",
" for product_type in products:\n",
" inventory[product_type] = int(input(f\"Please enter the number of {product_type}\"))\n",
" "
]
},
{
"cell_type": "code",
"execution_count": 104,
"id": "f7afaf48-2398-4420-ae5f-d1f8a51d3a12",
"metadata": {},
"outputs": [
{
"name": "stdin",
"output_type": "stream",
"text": [
"Please enter the number of t-shirt 5\n",
"Please enter the number of mug 10\n",
"Please enter the number of hat 15\n",
"Please enter the number of book 20\n",
"Please enter the number of keychain 25\n"
]
}
],
"source": [
"initialize_inventory(products)"
]
},
{
"cell_type": "code",
"execution_count": 105,
"id": "c5b62665-7686-4cee-ae45-72f4e4e2f6fe",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"{'t-shirt': 5, 'mug': 10, 'hat': 15, 'book': 20, 'keychain': 25}"
]
},
"execution_count": 105,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"inventory"
]
},
{
"cell_type": "code",
"execution_count": 106,
"id": "e903607c-750f-4e42-8d17-ab5210bbd834",
"metadata": {},
"outputs": [],
"source": [
"#Define a function named get_customer_orders that takes no parameters. \n",
"# Inside the function, implement the code for prompting the user to enter the product names using a loop. \n",
"# The function should return the customer_orders set.\n",
"\n",
"def get_customer_orders(products):\n",
" customer_orders_set = set({})\n",
" customer_order_type = input('Please enter the product ordered:')\n",
" if customer_order_type in products:\n",
" customer_orders_set.add(customer_order_type)\n",
" while customer_order_type not in products:\n",
" customer_order_type = input('Please enter the product ordered (among the existing products types):')\n",
" if customer_order_type in products:\n",
" customer_orders_set.add(customer_order_type)\n",
" customer_order_type_question = input('Do you want to add another product? Enter Yes or No')\n",
" if customer_order_type_question != 'Yes' and customer_order_type_question != 'No':\n",
" customer_order_type_question = input('The input is wrong, please try again and answer only with Yes or No the following question: Do you want to add another product?')\n",
" while customer_order_type_question == 'Yes':\n",
" customer_order_type_another = input('Please enter the product ordered:')\n",
" if customer_order_type_another in products:\n",
" customer_orders_set.add(customer_order_type_another)\n",
" while customer_order_type_another not in products:\n",
" customer_order_type_another = input('Please enter the product ordered (among the existing products types):')\n",
" if customer_order_type_another in products:\n",
" customer_orders_set.add(customer_order_type_another)\n",
" customer_order_type_question = input('Do you want to add another product? Enter Yes or No')\n",
" return customer_orders_set"
]
},
{
"cell_type": "code",
"execution_count": 111,
"id": "b14f0fc3-2d1d-4ae4-bab3-059396c7af23",
"metadata": {},
"outputs": [
{
"name": "stdin",
"output_type": "stream",
"text": [
"Please enter the product ordered: mug\n",
"Do you want to add another product? Enter Yes or No Yes\n",
"Please enter the product ordered: mug\n",
"Do you want to add another product? Enter Yes or No No\n"
]
},
{
"data": {
"text/plain": [
"{'mug'}"
]
},
"execution_count": 111,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"another_customer_orders_set = get_customer_orders(products)\n",
"another_customer_orders_set"
]
},
{
"cell_type": "code",
"execution_count": 113,
"id": "e6489795-ba8a-4f38-93fa-37f74b1abead",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"{'mug'}\n",
"{'t-shirt': 5, 'mug': 9, 'hat': 14, 'book': 19, 'keychain': 25}\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",
"print(another_customer_orders_set)\n",
"print(inventory)"
]
},
{
"cell_type": "code",
"execution_count": 114,
"id": "9d7ddccb-5d97-4449-ae3c-11eeadd637e5",
"metadata": {},
"outputs": [],
"source": [
"def update_inventory(customer_orders, inventory):\n",
" for customer_order_element in customer_orders:\n",
" inventory[customer_order_element] -= 1\n",
" return inventory"
]
},
{
"cell_type": "code",
"execution_count": 115,
"id": "916ac323-37f1-4691-b950-15202a90ce49",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"{'t-shirt': 5, 'mug': 8, 'hat': 14, 'book': 19, 'keychain': 25}"
]
},
"execution_count": 115,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"update_inventory(another_customer_orders_set, inventory)"
]
},
{
"cell_type": "code",
"execution_count": 116,
"id": "0017ee77-0e1c-4235-a3ba-9f24f0a31fef",
"metadata": {},
"outputs": [],
"source": [
"# Define a function named calculate_order_statistics that takes customer_orders and products as parameters. \n",
"# Inside the function, implement the code for calculating the order statistics (total products ordered, and percentage of unique products ordered). \n",
"# The function should return these values.\n",
"\n",
"def calculate_order_statistics(customer_orders, products):\n",
" total_products_ordered = len(customer_orders)\n",
" percentage_unique_products_ordered = len(customer_orders)/len(products)\n",
" return total_products_ordered, percentage_unique_products_ordered"
]
},
{
"cell_type": "code",
"execution_count": 120,
"id": "48c23417-1554-4860-ad52-934ad1bc86e9",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"(1, 0.2)\n"
]
}
],
"source": [
"order_statistics = calculate_order_statistics(another_customer_orders_set, products)\n",
"print(order_statistics)"
]
},
{
"cell_type": "code",
"execution_count": 118,
"id": "46c2465d-7c1f-4f6c-a113-ca0ddd2d4321",
"metadata": {},
"outputs": [],
"source": [
"# Define a function named print_order_statistics that takes order_statistics as a parameter. \n",
"# Inside the function, implement the code for printing the order statistics.\n",
"\n",
"def print_order_statistics(order_statistics):\n",
" return print(order_statistics)"
]
},
{
"cell_type": "code",
"execution_count": 121,
"id": "de573b34-a0f1-4cb1-905c-a44b5fe10b01",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"(1, 0.2)\n"
]
}
],
"source": [
"print_order_statistics(order_statistics)"
]
},
{
"cell_type": "code",
"execution_count": 122,
"id": "ed9b1cf3-4b4a-4e50-b053-ba7943d9be44",
"metadata": {},
"outputs": [],
"source": [
"# Define a function named print_updated_inventory that takes inventory as a parameter. \n",
"# Inside the function, implement the code for printing the updated inventory.\n",
"\n",
"def print_updated_inventory(inventory):\n",
" return print(inventory)"
]
},
{
"cell_type": "code",
"execution_count": 123,
"id": "405eb62a-07a0-40ed-8ae9-99808caa193f",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"{'t-shirt': 5, 'mug': 8, 'hat': 14, 'book': 19, 'keychain': 25}\n"
]
}
],
"source": [
"print_updated_inventory(inventory)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "d0d19953-1c6c-4392-b3a3-05766bed0a17",
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
Expand All @@ -61,7 +382,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.9.13"
"version": "3.13.5"
}
},
"nbformat": 4,
Expand Down