Skip to content
Open
Show file tree
Hide file tree
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
4 changes: 2 additions & 2 deletions lab-python-flow-control.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"display_name": ".venv (3.15.0)",
"language": "python",
"name": "python3"
},
Expand All @@ -55,7 +55,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.9.13"
"version": "3.15.0"
}
},
"nbformat": 4,
Expand Down
65 changes: 65 additions & 0 deletions lab-python-flow-control.py
Original file line number Diff line number Diff line change
@@ -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}")