Skip to content

Third lab done :) #447

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
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
112 changes: 110 additions & 2 deletions lab-python-functions.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,119 @@
"\n",
"\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "37295d16",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"{'t-shirt': 10, 'mug': 10, 'hat': 10, 'book': 10, 'keychain': 10}\n"
]
},
{
"ename": "NameError",
"evalue": "name 'order_statistics' is not defined",
"output_type": "error",
"traceback": [
"\u001b[31m---------------------------------------------------------------------------\u001b[39m",
"\u001b[31mNameError\u001b[39m Traceback (most recent call last)",
"\u001b[36mCell\u001b[39m\u001b[36m \u001b[39m\u001b[32mIn[2]\u001b[39m\u001b[32m, line 77\u001b[39m\n\u001b[32m 75\u001b[39m update_inventory(customer_orders, inventory)\n\u001b[32m 76\u001b[39m calculate_order_statistics(customer_orders, products)\n\u001b[32m---> \u001b[39m\u001b[32m77\u001b[39m print_order_statistics(\u001b[43morder_statistics\u001b[49m)\n\u001b[32m 78\u001b[39m print_updated_inventory(inventory)\n",
"\u001b[31mNameError\u001b[39m: name 'order_statistics' is not defined"
]
}
],
"source": [
"def get_customer_orders():\n",
" \"\"\"this function is designed to get input from the user for customer orders\"\"\"\n",
" quantity_user = \"\"\n",
" status_ordering= True\n",
" customer_orders = set()\n",
"\n",
" while status_ordering == True:\n",
" customer_order = input(\"Enter the name of a product that the customer want's to order:\").replace(\" \", \"\") \n",
" continue_ordering = input(\"Do you want to continue ordering? (yes/no): \")\n",
" customer_order = customer_order.split(\",\") #deleting spaces so it can be split correctly and counted for the inventory\n",
" customer_orders = set(customer_order)\n",
" # customer_orders.append(customer_order) # creating the list dividing the input by commas.\n",
" \n",
" while continue_ordering.lower() not in [\"yes\", \"no\"]: # entrada valida\n",
" print(\"Please enter a valid answer (yes/no)\")\n",
" continue_ordering = input(\"Do you want to continue ordering? (yes/no): \")\n",
" if continue_ordering.lower() == \"no\":\n",
" status_ordering = False\n",
"\n",
" return customer_orders\n",
"\n",
"def initialize_inventory(products):\n",
" \n",
" \"\"\" This is desing to get input for the inventory\"\"\"\n",
" inventory = { product: 0 for product in products}\n",
"\n",
" sum_inventory_final = 0\n",
" \n",
" for product in products: \n",
" \n",
" quantity_user = input(f\"Enter the initial amount for product: {product} in the inventory\")\n",
" \n",
" while quantity_user.isdigit() == False or int(quantity_user) < 0: \n",
" #if it is not a number or less than 0, we break the loop and print a message\n",
" print(\"Quantity must be a number and greater than 0. Start again\") \n",
" quantity_user= input(f\"Enter the amount for products: {product}\")\n",
"\n",
" inventory[product] = int(quantity_user) #adding the product as key and the quantity as value to the dictionary\n",
" sum_inventory_final += int(quantity_user) #adding the quantity to calculate the percentage at the end.\n",
" print(inventory) \n",
" return inventory \n",
" \n",
"def update_inventory(customer_orders, inventory):\n",
" \"\"\"This function updates the inventory based on customer orders\"\"\"\n",
" \n",
" for item in customer_orders:\n",
" if item in inventory:\n",
" inventory[item] = inventory[item] -1 #updating the inventory. \n",
"\n",
" return inventory\n",
" \n",
"def calculate_order_statistics(customer_orders, products):\n",
" \n",
" amount_products=len(products) \n",
" sum_inventory_final=sum([value for value in inventory.values()])\n",
" total_products_percentage=(len(customer_orders)/sum_inventory_final*100) \n",
" order_statistics = (total_products_percentage, amount_products)\n",
" \n",
" return order_statistics\n",
"\n",
"def print_order_statistics(order_statistics):\n",
" \n",
" print(\"Order Statistics:\") #point 8 printing as requested\n",
" print(\"Total products ordered: \", order_statistics[1]) #point 7 statistics 1 done\n",
" print(\"Percentage of products ordered: \", order_statistics[0], \"%\") #point 7 statistics 2 done\n",
"\n",
" return order_statistics\n",
"\n",
"def print_updated_inventory(inventory):\n",
" print(f\"Updated inventory: {inventory}\")\n",
" return inventory\n",
"\n",
"\n",
"products = [\"t-shirt\", \"mug\", \"hat\", \"book\", \"keychain\"]\n",
"inventory = initialize_inventory(products)\n",
"customer_orders = get_customer_orders()\n",
"update_inventory(customer_orders, inventory)\n",
"calculate_order_statistics(customer_orders, products)\n",
"print_order_statistics(order_statistics)\n",
"print_updated_inventory(inventory)"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
Expand All @@ -61,7 +169,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.9.13"
"version": "3.13.3"
}
},
"nbformat": 4,
Expand Down