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
150 changes: 148 additions & 2 deletions lab-python-functions.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,157 @@
"\n",
"\n"
]
},
{
"cell_type": "code",
"execution_count": 2,
"id": "308032c0",
"metadata": {},
"outputs": [],
"source": [
"#1\n",
"def initialize_inventory (products):\n",
" # dictionary\n",
" inventory = {}\n",
" # quantity\n",
" for product in products:\n",
" quantity = input (f\"Could you, please, let us know what is the quantity available in stock for {product}?\")\n",
" while not quantity.isdigit():\n",
" quantity = input (f\"Wrong format. Please insert a valid quantity for {product}!\")\n",
" print (f\"Available {quantity} {product}!\")\n",
" inventory.update({product: int(quantity)})\n",
" return inventory"
]
},
{
"cell_type": "code",
"execution_count": 3,
"id": "83778fdd",
"metadata": {},
"outputs": [],
"source": [
"#2\n",
"def get_customer_orders ():\n",
" choice = \"yes\"\n",
" customer_orders = set() \n",
" while choice != \"no\":\n",
" preference = input (f\"Could you, please, tell us a product you would like to buy?\")\n",
" if not (preference in products):\n",
" print(f\"Product not available. Please insert a product in {products}!\")\n",
" else:\n",
" customer_orders.add(preference)\n",
" choice = input (f\"Do you want to continue shopping? yes or no?\").lower()\n",
" print (f\"You chose: {preference}.\")\n",
" return customer_orders"
]
},
{
"cell_type": "code",
"execution_count": 4,
"id": "f8c4c388",
"metadata": {},
"outputs": [],
"source": [
"#3\n",
"def update_inventory (customer_orders, inventory):\n",
" for product in customer_orders:\n",
" inventory.update({product: inventory[product]-1})\n",
" print (f\"The quantity of {product} is {inventory[product]}.\\n\")"
]
},
{
"cell_type": "code",
"execution_count": 5,
"id": "7e4a5dfa",
"metadata": {},
"outputs": [],
"source": [
"#4\n",
"def calculate_order_statistics (customer_orders, products):\n",
" total_products_ordered = len(customer_orders)\n",
" print (f\"The total of products ordered is {total_products_ordered}.\")\n",
"\n",
" # Percentage ordered\n",
" total_inventory = sum(inventory.values())\n",
" percentage_ordered = (total_products_ordered / total_inventory) *100\n",
" return total_products_ordered, percentage_ordered"
]
},
{
"cell_type": "code",
"execution_count": 6,
"id": "abbb6ef2",
"metadata": {},
"outputs": [],
"source": [
"#5\n",
"def print_order_statistics (order_statistics):\n",
" print(f\"Order Statistics: \\n Total Products Ordered: {order_statistics[0]}\\n Percentage of Products Ordered: {order_statistics[1]} %.\")"
]
},
{
"cell_type": "code",
"execution_count": 7,
"id": "339ef942",
"metadata": {},
"outputs": [],
"source": [
"#6\n",
"def print_updated_inventory(inventory, customer_orders):\n",
" # update inventory\n",
" update_inventory(customer_orders = customer_orders, inventory = inventory)\n",
" print(f\"The updated inventory is {inventory}\")"
]
},
{
"cell_type": "code",
"execution_count": 8,
"id": "fadc6fba",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Available 98 t-shirt!\n",
"Available 67 mug!\n",
"Available 34 hat!\n",
"Available 78 book!\n",
"Available 54 keychain!\n",
"{'t-shirt': 98, 'mug': 67, 'hat': 34, 'book': 78, 'keychain': 54}\n",
"You chose: book.\n",
"You chose: mug.\n",
"You chose: hat.\n",
"The total of products ordered is 3.\n",
"(3, 0.906344410876133)\n",
"The quantity of book is 77.\n",
"\n",
"The quantity of mug is 66.\n",
"\n",
"The quantity of hat is 33.\n",
"\n",
"The updated inventory is {'t-shirt': 98, 'mug': 66, 'hat': 33, 'book': 77, 'keychain': 54}\n"
]
}
],
"source": [
"#7\n",
"products = [\"t-shirt\", \"mug\", \"hat\", \"book\", \"keychain\"]\n",
"inventory = initialize_inventory(products)\n",
"print (inventory)\n",
"\n",
"customer_orders = get_customer_orders () \n",
"\n",
"order_statistics = calculate_order_statistics(customer_orders, products) \n",
"print(order_statistics)\n",
"\n",
"print_updated_inventory(inventory, customer_orders)"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
Expand All @@ -61,7 +207,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.9.13"
"version": "3.14.0"
}
},
"nbformat": 4,
Expand Down