| n | def calculate_price(quantity, unit_price): | n | def calculate_price(quantity, unit_price, tax_rate=0.0): |
| """Calculate total price for items.""" | | """Calculate total price for items including tax.""" |
| total = quantity * unit_price | | subtotal = quantity * unit_price |
| | | tax_amount = subtotal * tax_rate |
| | | total = subtotal + tax_amount |
| return total | | return total |
| | | |
| def apply_discount(price, discount_rate): | | def apply_discount(price, discount_rate): |
| """Apply percentage discount to price.""" | | """Apply percentage discount to price.""" |
| n | | n | if discount_rate < 0 or discount_rate > 1: |
| | | raise ValueError("Discount rate must be between 0 and 1") |
| discount_amount = price * discount_rate | | discount_amount = price * discount_rate |
| final_price = price - discount_amount | | final_price = price - discount_amount |
| | | |
| | | |
| n | def process_order(items): | n | def process_order(items, tax_rate=0.1): |
| """Process an order with multiple items.""" | | """Process an order with multiple items and apply tax.""" |
| total = 0 | | total = 0 |
| for item in items: | | for item in items: |
| n | price = calculate_price(item['qty'], item['price']) | n | price = calculate_price(item['qty'], item['price'], tax_rate) |
| total += price | | total += price |
| t | | t | |
| | | print(f"Order total: ${total:.2f}") |
| return total | | return total |