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
70 changes: 68 additions & 2 deletions lab-python-flow-control.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,77 @@
"\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": 2,
"id": "4b8a8be8",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Your orders: {'mug', 'book', 'hat'}\n",
"Total inventory {'t-shirt': 200, 'mug': 20, 'hat': 330, 'book': 1000, 'keychain': 33}\n",
"{'mug', 'book', 'hat'}\n",
"Total orders 3\n",
"Percetage Ordered 0.18951358180669614 %\n",
"Order Summary (3, 0.18951358180669614)\n",
"Remained inventory {'t-shirt': 200, 'mug': 19, 'hat': 329, 'book': 999, 'keychain': 33}\n"
]
}
],
"source": [
"products = [\"t-shirt\", \"mug\", \"hat\", \"book\", \"keychain\"] # Define a list of product names\n",
"inventory = {} # Create an empty dictionary to store product quantities\n",
"\n",
"for product in products: # Loop through each product name in the list\n",
" qty = int(input(f\"Enter {product} quantity: \")) # Ask the user to enter a quantity for the current product.\n",
" # f-string → {product} is replaced with the variable's value.\n",
" # Example: if product = \"mug\", text shown is \"Enter mug quantity: \"\n",
" inventory[product] = qty # Save the quantity in the dictionary with product name as the key\n",
" # Example: if product = \"mug\" and qty = 5 → inventory[\"mug\"] = 5\n",
"\n",
"\n",
"customer_orders = set() # Create an empty set to store customer orders (no duplicates allowed)\n",
"\n",
"while True: # Infinite loop, will stop only when we use \"break\"\n",
" product = input(\"Enter one of these: t-shirt, mug, hat, book, keychain: \") # Ask the user for a product\n",
" customer_orders.add(product) # Add the chosen product to the set\n",
"\n",
" another = input(\"Do you want to add another product? (yes/no): \").lower() # Ask if they want to continue, convert to lowercase\n",
" if another == \"no\": # If the answer is \"no\", exit the loop\n",
" break\n",
"\n",
"print(\"Your orders:\", customer_orders) # Show the final set of orders\n",
"\n",
"print(\"Total inventory\",inventory)\n",
"print(customer_orders) #Print the products in the `customer_orders` set.\n",
"\n",
"totalorders= len(customer_orders)\n",
"totalavailable=sum(inventory.values())\n",
"percentageordered=(totalorders/totalavailable)*100\n",
"order_status = (totalorders, percentageordered)#Calculate the order statistics\n",
"\n",
"\n",
"print(\"Total orders\",totalorders)\n",
"print(\"Percetage Ordered\", percentageordered, \"%\")\n",
"print(\"Order Summary\",order_status) # Print the order statistics \n",
"\n",
"\n",
"for product in customer_orders: # Loop only over the ordered products\n",
" if product in inventory and inventory[product] > 0: # Check that the product exists and has stock\n",
" inventory[product] -= 1 # Subtract 1 from that product\n",
"\n",
"\n",
"print(\"Remained inventory\",inventory) #10. Print the updated inventory\n"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
Expand All @@ -55,7 +121,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.9.13"
"version": "3.13.7"
}
},
"nbformat": 4,
Expand Down