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
131 changes: 131 additions & 0 deletions Lab_3Functions.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
{
"nbformat": 4,
"nbformat_minor": 0,
"metadata": {
"colab": {
"provenance": []
},
"kernelspec": {
"name": "python3",
"display_name": "Python 3"
},
"language_info": {
"name": "python"
}
},
"cells": [
{
"cell_type": "code",
"execution_count": 14,
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "JFRTYcGshY5v",
"outputId": "bcd53eb0-1bc5-4d47-8eab-e5c9b77d40b3"
},
"outputs": [
{
"output_type": "stream",
"name": "stdout",
"text": [
"What is the quanitity for t-shirt ?3\n",
"What is the quanitity for mug ?3\n",
"What is the quanitity for hat ?3\n",
"What is the quanitity for book ?3\n",
"What is the quanitity for keychain ?3\n",
"What do you want to order? bot\n",
"Do you want to order anything else? yes\n",
"What do you want to order? book\n",
"Do you want to order anything else? yes\n",
"What do you want to order? hat\n",
"Do you want to order anything else? yes\n",
"What do you want to order? hat\n",
"Do you want to order anything else? no\n",
"Okay, that is it \n",
"....Order Stats....\n",
"Total products ordered.... 3\n",
"Percentage of products ordered.... 60.0\n",
"New updated inventory....\n",
"\n",
"t-shirt : 3\n",
"mug : 3\n",
"hat : 2\n",
"book : 2\n",
"keychain : 3\n"
]
}
],
"source": [
"#0 Products list from first two labs\n",
"products = [\"t-shirt\", \"mug\", \"hat\", \"book\", \"keychain\"]\n",
"\n",
"\n",
"#1 Define function\n",
"def initialize_inventory(products):\n",
" inventory = {}\n",
" for i in products:\n",
" quantity = int(input(f\"What is the quanitity for {i} ?\"))\n",
" inventory[i] = quantity\n",
" return inventory\n",
"\n",
"\n",
"#2 Define function\n",
"def get_customer_orders():\n",
" customer_orders = set()\n",
" while True:\n",
" product = input(\"What do you want to order? \")\n",
" customer_orders.add(product)\n",
" another_order = input(\"Do you want to order anything else? \").lower()\n",
" if another_order != 'yes':\n",
" print('Okay, that is it ')\n",
" break\n",
" return customer_orders\n",
"\n",
"#3 Define function\n",
"def update_inventory(customer_orders, inventory):\n",
" for i in customer_orders:\n",
" if i in inventory:\n",
" inventory[i] -= 1\n",
"\n",
"\n",
"#4 Define function\n",
"def calculate_order_statistics(customer_orders, products):\n",
" total_products_ordered = len(customer_orders)\n",
" percentage_ordered = (total_products_ordered / len(products)) * 100\n",
" return total_products_ordered, percentage_ordered\n",
"\n",
"#5 Define function\n",
"def print_order_statistics(order_statistics):\n",
" total, percentage = order_statistics\n",
" print(\"....Order Stats....\")\n",
" print(\"Total products ordered....\", total)\n",
" print(\"Percentage of products ordered....\", percentage)\n",
"\n",
"#6 Define function\n",
"def print_updated_inventory(inventory):\n",
" print(\"New updated inventory....\")\n",
" print()\n",
" for product, quantity in inventory.items():\n",
" print(product, \":\", quantity)\n",
"\n",
"#7 Calling functions\n",
"inventory = initialize_inventory(products)\n",
"customer_orders = get_customer_orders()\n",
"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)"
]
},
{
"cell_type": "code",
"source": [],
"metadata": {
"id": "kYRQrDtQm4Gt"
},
"execution_count": null,
"outputs": []
}
]
}