Python Program for Computer Science (083) class 11th CBSE

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:

  1. 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]
  2. 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)
  3. 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.
    We can decide the discount with this code:pythonCopy codeif total_amount > 1000: discount_percentage = 10 elif total_amount > 500: discount_percentage = 5 else: discount_percentage = 0
  4. 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.
    The final cost is calculated like this:pythonCopy codediscount = (total_amount * discount_percentage) / 100 final_cost = total_amount - discount
  5. 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

Leave a Reply

Your email address will not be published. Required fields are marked *

Company

Our ebook website brings you the convenience of instant access to a diverse range of titles, spanning genres from fiction and non-fiction to self-help, business.

Features

Most Recent Posts

  • All Post
  • Accounting
  • Blog
  • Blogging Tips
  • Career Advice
  • Career Development
  • Chemistry
  • Chemistry Education
  • Children's Books
  • Coding and Development
  • Digital Marketing Education
  • Education
  • Education Technology
  • Education/Reference
  • Electronics
  • Energy
  • Energy & Environment
  • Environment
  • Environmental Awareness
  • Exam Preparation
  • Gambling Education
  • Health and Science
  • Information Technology
  • Mathematics
  • Mathematics and Geometry
  • Mathematics Education
  • Mental Health
  • Mystery/Thriller
  • Physics
  • Physics and Technology
  • Physics Education
  • Religion/Spirituality
  • Science
  • Science & Mathematics
  • Science and Education
  • Science and Technology
  • Science Education
  • Science/Technology
  • Technology Education
  • Technology Innovations
  • Uncategorized
    •   Back
    • JAVA

eBook App for FREE

Lorem Ipsum is simply dumy text of the printing typesetting industry lorem.

Category

Empowering futures, one student at a time. Explore our diverse range of courses designed to foster academic excellence. Join us on the journey to success with EEPL Classroom – where learning meets limitless possibilities.

© 2023 Created by Emancipation Edutech Private Limited

Important Links

Support

Contacts

Office No. 15C, Abhinandan Complex, Tharpakhna, Near Plaza Chowk, Ranchi, Jharkhand 834001

Call: +91-7488456170

Email: info@eepl.me