Creating a Simple Shopping Cart Program in Python with Source Code

Introduction

Welcome to PythonSage!, your go-to resource for mastering Python programming! Today, we'll be creating a simple shopping cart program in Python. This program allows users to add items to a cart, view the cart, remove items, compute the total price, and exit the application. Let's dive into the code and see how it works.

Python shopping cart source code


Code Overview (Shopping Cart Program in Python)

Here's the complete code for our shopping cart program:

import sys

# Initialize lists to store items and their prices

list_s = []

prices = []

 

# Function to add an item to the cart

def add_item():

    item = input("What item would you like to add? ")

    list_s.append(item)

    price = float(input('What is the price of ' + item + '? '))

    prices.append(price)

    print("The item '" + item + "' has been added to the cart.")

 

# Function to print all items in the cart

def print_items():

    for temp in range(0, len(list_s)):

        item = list_s[temp]

        price = prices[temp]

        result = str(temp + 1) + ". " + item + " - $" + str(price)

        print(result)

 

# Function to remove an item from the cart

def remove():

    for temp in range(0, len(list_s)):

        item = list_s[temp]

        price = prices[temp]

        result = str(temp + 1) + ". " + item + " - $" + str(price)

        print(result)

    remo = int(input("Which item would you like to remove? "))

    if remo == 0:

        list_s.pop(0)

        prices.pop(0)

    else:

        list_s.pop(remo - 1)

        prices.pop(remo - 1)

    print("Item removed successfully.")

 

# Function to compute the total price of items in the cart

def total():

    Sum = sum(prices)

    print("The total price of the items in the shopping cart is $", round(Sum, 2))

 

# Main program loop

while True:

    print("\nWelcome to the Shopping Cart Program!\n")

    print("Please select one of the following:")

    print("1. Add item\n2. View cart\n3. Remove item\n4. Compute total\n5. Exit")

    select = input("Please enter an action: ")

    if select == '1':

        print("Welcome to Add Item:")

        add_item()

    elif select == '2':

        print("Here are all the items in the cart:")

        print_items()

    elif select == '3':

        remove()

    elif select == '4':

        total()

    elif select == '5':

        print("Thank you. Goodbye.")

        break

    else:

        print("You entered an incorrect option.")

Step-by-Step Explanation of Code

1. Initial Setup

We start by importing the sys module and initializing two lists: list_s to store item names and prices to store corresponding prices.

2. Adding Items

The add_item function prompts the user to enter an item name and its price. These are then added to the respective lists, and a confirmation message is displayed.

3. Viewing Items

The print_items function goes through all the items in the cart and prints each item with its price in a user-friendly format.

4. Removing Items

The remove function lists all items with their indices and asks the user to choose an item to remove by entering its index. The selected item and its price are then removed from the lists.

5. Computing Total Price

The total function calculates the sum of all prices in the cart and prints the total, rounded to two decimal places.

6. Main Program Loop

The main loop displays a menu with options for adding items, viewing the cart, removing items, computing the total price, and exiting the program. It processes the user's input and calls the appropriate functions based on the selected option.

watch this video to learn more about while loop:



Conclusion

This simple shopping cart program in Python is a great example of how Python can be used to create interactive, menu-driven applications. By understanding and modifying this code, you can enhance your programming skills and build more complex projects in the future. Happy coding!

Feel free to leave comments or questions, and stay tuned for more Python tutorials on PythonSage!

Post a Comment

Previous Post Next Post