Skip to content

solved lab #444

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 1 commit 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
136 changes: 134 additions & 2 deletions lab-python-flow-control.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,143 @@
"\n",
"3. Instead of updating the inventory by subtracting 1 from the quantity of each product, only do it for the products that were ordered (those in \"customer_orders\")."
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "d98de8ed",
"metadata": {},
"outputs": [],
"source": [
"## previous lab:\n",
"\n",
"products = [\"t-shirt\",\"mug\",\"hat\",\"book\",\"keychain\"]\n",
"inventory = {}\n",
"\n",
"for product in products:\n",
" quantity = int(input(f\"Enter quantity for {product}:\"))\n",
" inventory[product] = quantity\n",
"\n",
"customer_orders = set()\n",
"\n",
"for i in range(3):\n",
" while True:\n",
" order = input(f\"Please enter the product {i+1}:\").lower()\n",
" if order in products:\n",
" customer_orders.add(order)\n",
" else:\n",
" print(\"Invalid product. Please choose from the list.\")\n",
"\n",
"print(\"\\nCustomer Orders:\", customer_orders)\n",
"\n",
"order_status = (\"total_products_ordered\",\"percentage_ordered\")\n",
"\n",
"total_products_ordered = len(customer_orders)\n",
"percentage_ordered = (total_products_ordered/len(products)) * 100\n",
"\n",
"print(\"\\nOrder Statistics:\")\n",
"print(f\"Total Products Ordered: {order_status[0]}\")\n",
"print(f\"Percentage of Products Ordered: {order_status[1]}%\")\n",
"\n",
"for product in customer_orders:\n",
" if inventory[product] > 0:\n",
" inventory[product] -= 1\n",
"\n",
"print(\"\\nUpdated Inventory:\")\n",
"for product, quantity in inventory.items():\n",
" print(f\"{product}: {quantity}\")"
]
},
{
"cell_type": "code",
"execution_count": 2,
"id": "96b30ecc",
"metadata": {},
"outputs": [],
"source": [
"products = [\"t-shirt\",\"mug\",\"hat\",\"book\",\"keychain\"]\n",
"inventory = {}\n",
"\n",
"\n",
"for product in products:\n",
" while True:\n",
" try:\n",
" quantity = int(input(f\"Please enter quantity for {product}:\"))\n",
" inventory[product] = quantity\n",
" break\n",
" except ValueError:\n",
" print(\"Please enter a valid number.\")\n",
" "
]
},
{
"cell_type": "code",
"execution_count": 4,
"id": "1d31d443",
"metadata": {},
"outputs": [],
"source": [
"customer_orders = set()\n",
"\n",
"while True:\n",
" order = input(f\"Enter a product the customer wants to order:\").lower()\n",
" if order in products:\n",
" customer_orders.add(order)\n",
" else:\n",
" print(\"Invalid product. Please choose from the list:\", products)\n",
" \n",
" continue_order = input(\"Do you want to add another product? (yes/no): \").strip().lower()\n",
" if continue_order != \"yes\":\n",
" break\n",
" "
]
},
{
"cell_type": "code",
"execution_count": 5,
"id": "1cc22f1c",
"metadata": {},
"outputs": [],
"source": [
"for product in customer_orders:\n",
" if inventory.get(product, 0) > 0:\n",
" inventory[product] -= 1"
]
},
{
"cell_type": "code",
"execution_count": 8,
"id": "4b6db0cf",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"\n",
"Order Statistics:\n",
"Total Products Ordered: 1\n",
"Percentage of Products Ordered: 20.00%\n"
]
}
],
"source": [
"\n",
"order_status = (\"total_products_ordered\",\"percentage_ordered\")\n",
"\n",
"total_products_ordered = len(customer_orders)\n",
"percentage_ordered = (total_products_ordered/len(products)) * 100\n",
"\n",
"\n",
"print(\"\\nOrder Statistics:\")\n",
"print(f\"Total Products Ordered: {total_products_ordered}\")\n",
"print(f\"Percentage of Products Ordered: {percentage_ordered:.2f}%\")"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
Expand All @@ -55,7 +187,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.9.13"
"version": "3.13.3"
}
},
"nbformat": 4,
Expand Down