0% found this document useful (0 votes)
4 views2 pages

Class ShoppingCart

The document defines a ShoppingCart class that manages a shopping cart with methods to add and remove items, calculate the total price, and display the cart contents. It includes attributes for storing item names and their prices. The class is initialized with an empty cart.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views2 pages

Class ShoppingCart

The document defines a ShoppingCart class that manages a shopping cart with methods to add and remove items, calculate the total price, and display the cart contents. It includes attributes for storing item names and their prices. The class is initialized with an empty cart.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2

class ShoppingCart:

"""
A class to represent a shopping cart.

Attributes:
items (dict): A dictionary where the keys are item names and the values are
their prices.

Methods:
add_item(item, price): Adds an item and its price to the cart.
remove_item(item): Removes an item from the cart.
get_total(): Calculates and returns the total price of all items.
show_cart(): Prints the items in the cart with their prices.
"""

def __init__(self):
"""Initializes the ShoppingCart class with an empty cart."""
self.items = {}

def add_item(self, item, price):


"""
Adds an item and its price to the cart.

Parameters:
item (str): The name of the item.
price (float): The price of the item.
"""
self.items[item] = price
def remove_item(self, item):
"""
Removes an item from the cart.

Parameters:
item (str): The nam

You might also like