diff --git a/lab-python-flow-control.ipynb b/lab-python-flow-control.ipynb index f4c7391..65ccecd 100644 --- a/lab-python-flow-control.ipynb +++ b/lab-python-flow-control.ipynb @@ -41,7 +41,7 @@ ], "metadata": { "kernelspec": { - "display_name": "Python 3 (ipykernel)", + "display_name": ".venv (3.15.0)", "language": "python", "name": "python3" }, @@ -55,7 +55,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.9.13" + "version": "3.15.0" } }, "nbformat": 4, diff --git a/lab-python-flow-control.py b/lab-python-flow-control.py new file mode 100644 index 0000000..afa2c95 --- /dev/null +++ b/lab-python-flow-control.py @@ -0,0 +1,65 @@ +# 1. Define products and initialize inventory using a loop +products = ["t-shirt", "mug", "hat", "book", "keychain"] +inventory = {} + +print("=== INVENTORY SETUP ===") +for product in products: + while True: + user_input = input(f"Enter the quantity for '{product}': ").strip() + if user_input.isdigit(): + inventory[product] = int(user_input) + break + else: + print(f" ⚠ Invalid input. Please enter a whole number (e.g. 5).") + +print("\n--- Initial Inventory ---") +for product, qty in inventory.items(): + print(f" {product}: {qty}") + + +# 2. Take customer orders using a while loop +customer_orders = set() +add_another = "yes" + +print("\n=== PLACE YOUR ORDER ===") +print(f"Available products: {products}") + +while add_another == "yes": + product_name = input("Enter a product name: ").strip().lower() + + if product_name in products: + customer_orders.add(product_name) + print(f" '{product_name}' added to your order.") + else: + print(f" '{product_name}' is not available. Choose from: {products}") + + while True: + add_another = input("Add another product? (yes/no): ").strip().lower() + if add_another in ("yes", "no"): + break + else: + print(" ⚠ Please type 'yes' or 'no'.") + +print(f"\nCustomer Orders: {customer_orders}") + + +# 3. Calculate and print order statistics +total_products_ordered = len(customer_orders) +percentage_ordered = (total_products_ordered / len(products)) * 100 +order_status = (total_products_ordered, percentage_ordered) + +print("\n=== ORDER STATISTICS ===") +print(f" Total Products Ordered: {order_status[0]}") +print(f" Percentage of Products Ordered: {order_status[1]:.1f}%") + + +# 4. Update inventory only for ordered products +for product in customer_orders: + if inventory[product] > 0: + inventory[product] -= 1 + else: + print(f" Warning: '{product}' is out of stock!") + +print("\n=== UPDATED INVENTORY ===") +for product, qty in inventory.items(): + print(f" {product}: {qty}")