Purchased Item Tracker using Python

Here's a simple Python script that will allow you to keep track of items purchased while overseas or traveling. The script will prompt you to enter item details such as the name, price, and quantity. It then stores the information in a list and displays a summary of all the items purchased.

Purchased Item Tracker using Python
Photo by Sunguk Kim / Unsplash

Here's a simple Python script that will allow you to keep track of items purchased while overseas or traveling. The script will prompt you to enter item details such as the name, price, and quantity. It then stores the information in a list and displays a summary of all the items purchased.

# Initialize an empty list to store purchased items
purchased_items = []

# Function to add an item to the purchased_items list
def add_item():
    name = input("Enter the item name: ")
    price = float(input("Enter the item price: "))
    quantity = int(input("Enter the quantity purchased: "))
    purchased_items.append({'Name': name, 'Price': price, 'Quantity': quantity})
    print("Item added successfully!")

# Function to display the summary of all purchased items
def display_summary():
    print("\n--- Purchased Items Summary ---")
    total_cost = 0
    for item in purchased_items:
        print("Name:", item['Name'])
        print("Price:", item['Price'])
        print("Quantity:", item['Quantity'])
        print("------------------------")
        total_cost += item['Price'] * item['Quantity']
    print("Total Cost: ", total_cost)
    print("------------------------")

# Main program loop
while True:
    print("1. Add an item")
    print("2. Display purchased items summary")
    print("3. Quit")
    choice = input("Enter your choice (1-3): ")

    if choice == '1':
        add_item()
    elif choice == '2':
        display_summary()
    elif choice == '3':
        print("Thank you for using the travel item tracker. Have a great day!")
        break
    else:
        print("Invalid choice. Please try again.\n")

But wait! Maybe you prefer saving the entered items in a .txt file. If so, you may use the following script, which will prompt you like the first script but instead, will store the items and the total for the items in a .txt file.

# Initialize an empty list to store purchased items
purchased_items = []

# Function to add an item to the purchased_items list
def add_item():
    name = input("Enter the item name: ")
    price = float(input("Enter the item price: "))
    quantity = int(input("Enter the quantity purchased: "))
    purchased_items.append({'Name': name, 'Price': price, 'Quantity': quantity})
    print("Item added successfully!")

# Function to save the purchased items summary to a text file
def save_summary_to_file():
    filename = input("Enter the filename to save the summary (e.g., summary.txt): ")
    with open(filename, 'w') as file:
        file.write("--- Purchased Items Summary ---\n")
        total_cost = 0
        for item in purchased_items:
            file.write("Name: {}\n".format(item['Name']))
            file.write("Price: {}\n".format(item['Price']))
            file.write("Quantity: {}\n".format(item['Quantity']))
            file.write("------------------------\n")
            total_cost += item['Price'] * item['Quantity']
        file.write("Total Cost: {}\n".format(total_cost))
        file.write("------------------------\n")
    print("Summary saved to {} successfully!".format(filename))

# Main program loop
while True:
    print("1. Add an item")
    print("2. Save purchased items summary to a file")
    print("3. Quit")
    choice = input("Enter your choice (1-3): ")

    if choice == '1':
        add_item()
    elif choice == '2':
        save_summary_to_file()
    elif choice == '3':
        print("Thank you for using the travel item tracker. Have a great day!")
        break
    else:
        print("Invalid choice. Please try again.\n")

Hope you found one or both of these scripts useful, and makes going through customs less stressful.