Skip to content
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

Lab 4 #242

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open

Lab 4 #242

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
83 changes: 82 additions & 1 deletion lab-python-list-comprehension.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,87 @@
"\n",
"```\n"
]
},
{
"cell_type": "code",
"execution_count": 6,
"id": "a8f8a1ce",
"metadata": {},
"outputs": [],
"source": [
"products = [\"t-shirt\", \"mug\", \"hat\", \"book\", \"keychain\"]\n",
"inventory = {}\n",
"customer_orders = set()\n",
"def initialize_inventory(products_list):\n",
" \n",
" inventory = {item:int(input(f\"How many {item}s are there in the inventory?\")) for item in products_list}\n",
" return inventory"
]
},
{
"cell_type": "code",
"execution_count": 8,
"id": "7a25be3d",
"metadata": {},
"outputs": [],
"source": [
"#2. Modify the function get_customer_orders so it prompts the user to enter the number of customer orders and gathers the product names using a loop and user input. Use comprehension.\n",
"\n",
"\n",
"def get_customer_orders():\n",
" num_orders = int(input(\"Enter the quantity of t-shirts available: \"))\n",
" customer_orders = {input(f\"Ingrese el nombre del producto {i + 1}: \") for i in range(num_orders)}\n",
" return customer_orders\n",
"\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "ed96b826",
"metadata": {},
"outputs": [],
"source": [
"#3. Add a new function to calculate the total price of the customer order. For each product in customer_orders, prompt the user to enter the price of that product. Use comprehension to calculate the total price. Note: assume that the user can only have 1 unit of each product.\n",
"\n",
"def calculate_total_price(customer_orders):\n",
" total_price = sum(float(input(f\"Input the price '{product}': \")) for product in customer_orders)\n",
" return total_price"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "a7737825",
"metadata": {},
"outputs": [],
"source": [
"#4. Modify the update_inventory function to remove the product from the inventory if its quantity becomes zero after fulfilling the customer orders. Use comprehension to filter out the products with a quantity of zero from the inventory.\n",
"\n",
"\n",
"def update_inventory(customer_orders, inventory):\n",
" for product in customer_orders:\n",
" inventory[product] -= 1\n",
" inventory = {product: qty for product, qty in inventory.items() if qty > 0}\n",
" \n",
" return inventory"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "835c3f60",
"metadata": {},
"outputs": [],
"source": [
"#5. Print the total price of the customer order.\n",
"\n",
"\n",
"def calculate_total_price(customer_orders):\n",
" total_price = sum(float(input(f\"Input the price '{product}': \")) for product in customer_orders)\n",
" print(total_price)\n",
" return total_price"
]
}
],
"metadata": {
Expand All @@ -93,7 +174,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.9.13"
"version": "3.9.19"
}
},
"nbformat": 4,
Expand Down