Problem: Calculating the Total Cost of Items in a Shopping Cart with Discount
We want to create a program that calculates the total cost of items in a shopping cart. Based on the total amount, we will apply a discount. The discount will vary depending on the total cost:
- If the total cost is above ₹1000, we apply a 10% discount.
- If the total cost is between ₹500 and ₹1000, we apply a 5% discount.
- If the total cost is less than ₹500, no discount is applied.
Python Program Explanation:
- Step 1: Define a list of item prices
We start by creating a list of items in the shopping cart, where each item has a price. For example, the prices might look like this:pythonCopy codecart_items = [200, 450, 150, 300, 100]
- Step 2: Calculate the total amount
We need to find out the total cost of all the items in the cart. This can be done by adding up the prices of each item in the list.
The total cost will be calculated using this command:pythonCopy codetotal_amount = sum(cart_items)
- Step 3: Decide the discount
Depending on the total amount, we will apply a discount:- If the total amount is greater than ₹1000, the discount will be 10%.
- If the total amount is between ₹500 and ₹1000, the discount will be 5%.
- If the total amount is less than ₹500, no discount will be given.
if total_amount > 1000: discount_percentage = 10 elif total_amount > 500: discount_percentage = 5 else: discount_percentage = 0
- Step 4: Apply the discount and calculate the final cost
After determining the discount, we subtract it from the total cost. To do this:- First, we calculate the discount value.
- Then, we subtract this discount from the total amount to get the final cost.
discount = (total_amount * discount_percentage) / 100 final_cost = total_amount - discount
- Step 5: Print the results
Finally, the program will print the following:- The total amount before the discount.
- The discount percentage applied.
- The final cost after the discount.
Complete Program Code:
pythonCopy code# Function to calculate the total cost with a discount
def calculate_total_cost(cart_items, discount_percentage):
total_cost = sum(cart_items) # Sum up all prices in the cart
discount = (total_cost * discount_percentage) / 100 # Calculate the discount amount
final_cost = total_cost - discount # Subtract discount from total cost
return final_cost
# Function to decide the discount percentage based on total amount
def apply_discount_based_on_amount(total_amount):
if total_amount > 1000:
return 10 # 10% discount if total > 1000
elif total_amount > 500:
return 5 # 5% discount if total > 500
else:
return 0 # No discount if total <= 500
# Main program
def main():
# List of item prices in the shopping cart
cart_items = [200, 450, 150, 300, 100] # Prices of items
# Calculate the total cost
total_amount = sum(cart_items)
print(f"Total Amount: ₹{total_amount}")
# Calculate the discount percentage
discount_percentage = apply_discount_based_on_amount(total_amount)
print(f"Applied Discount: {discount_percentage}%")
# Calculate the final cost after applying the discount
final_cost = calculate_total_cost(cart_items, discount_percentage)
print(f"Final Cost after discount: ₹{final_cost}")
# Running the program
if __name__ == "__main__":
main()
Sample Output:
yamlCopy codeTotal Amount: ₹1200
Applied Discount: 10%
Final Cost after discount: ₹1080.0
Explanation of Output:
- Total Amount: ₹1200 is the total of all items.
- Applied Discount: 10% discount is applied because ₹1200 is greater than ₹1000.
- Final Cost: After the discount, the final cost is ₹1080.
This program helps you understand how to calculate the total cost and apply discounts based on conditions. It shows how you can work with lists, perform simple arithmetic, and use conditionals in Python.
EEPL CLASSROOM
Surbhi kumari
7488713635
teamemancipation@gmail.com
eepl.me