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
145 changes: 143 additions & 2 deletions lab-python-functions.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,152 @@
"\n",
"\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "acab0136",
"metadata": {},
"outputs": [],
"source": [
"products = [\"t-shirt\", \"mug\", \"hat\", \"book\", \"keychain\"]\n",
"\n",
"def initialize_inventory(products):\n",
" inventory = {}\n",
" for product in products: \n",
" while True:\n",
" try:\n",
" quantity=int(input(f\"Please enter the number of units available of {product}: \"))\n",
"\n",
" if quantity < 0: \n",
" print(f\"Please enter a valid whole number of units of {product} available.\")\n",
" continue\n",
" inventory[product] = quantity\n",
" break\n",
"\n",
" except ValueError:\n",
" print(f\"Invalid input. Please enter a valid whole number of units of {product} available.\")\n",
" return inventory \n",
"\n",
"def get_customer_orders():\n",
" customer_orders = set()\n",
" shopping = True\n",
" while shopping:\n",
" product_name = input(\"Write the name of the product you would like to order: \").strip().lower()\n",
" if product_name not in products:\n",
" print(\"Item name couldnt be found. Please try again:\")\n",
" continue\n",
" else:\n",
" customer_orders.add(product_name)\n",
" print(f\"{product_name} has been added to the basket.\" )\n",
" while True:\n",
" print(\"Would you like to keep purchasing on our web?\")\n",
" purchase = input(\"Write 'Yes' or 'No': \").title().strip()\n",
" if purchase == \"Yes\":\n",
" break\n",
" elif purchase == \"No\":\n",
" shopping = False\n",
" break\n",
" else:\n",
" print(\"Invalid option.\")\n",
" return customer_orders\n",
"\n",
"def update_inventory(customer_orders, inventory):\n",
" for product in customer_orders:\n",
" if inventory[product] >= 1:\n",
" inventory[product] -= 1\n",
" return inventory\n",
"\n",
"def calculate_order_statistics(customer_orders, products):\n",
" total_products_ordered = len(customer_orders)\n",
" percentage_of_total_products = total_products_ordered/len(products)*100\n",
" return total_products_ordered, percentage_of_total_products\n",
"\n",
"def print_order_statistics(order_statistics):\n",
" (total_products_ordered,percentage_of_total_products) = order_statistics\n",
" print(\"Order Statistics: \")\n",
" print(f\"The total number of products ordered is: {total_products_ordered}\")\n",
" print(f\"The percentage of the total of this order is: {percentage_of_total_products} %\")\n",
"\n",
"def print_updated_inventory(inventory):\n",
" print(inventory)\n",
"\n",
"inventory = initialize_inventory(products)\n",
"customer_orders = get_customer_orders()\n",
"inventory = update_inventory(customer_orders,inventory)\n",
"order_statistics = calculate_order_statistics(customer_orders,products)\n",
"print_order_statistics(order_statistics)\n",
"print_updated_inventory(inventory)\n"
]
},
{
"cell_type": "code",
"execution_count": 7,
"id": "77f97c7a",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"{'t-shirt': 11, 'mug': 12, 'hat': 13, 'book': 14, 'keychain': 15}"
]
},
"execution_count": 7,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"initialize_inventory(products)"
]
},
{
"cell_type": "code",
"execution_count": 14,
"id": "53859bbb",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"t-shirt has been added to the basket.\n",
"Would you like to keep purchasing on our web?\n",
"Invalid option.\n",
"Would you like to keep purchasing on our web?\n",
"Invalid option.\n",
"Would you like to keep purchasing on our web?\n",
"mug has been added to the basket.\n",
"Would you like to keep purchasing on our web?\n"
]
},
{
"data": {
"text/plain": [
"{'mug', 't-shirt'}"
]
},
"execution_count": 14,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"get_customer_orders()"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "30a209b9",
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
Expand All @@ -61,7 +202,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.9.13"
"version": "3.14.0"
}
},
"nbformat": 4,
Expand Down