I highly recommend Nomad's chargers, phone cases, and watch bands - and my favorites are on sale now
Back to Tutorials
techTutorialbeginner

I highly recommend Nomad's chargers, phone cases, and watch bands - and my favorites are on sale now

July 15, 20266 views5 min read

Learn to create a Python program that tracks tech accessories like Nomad chargers, phone cases, and watch bands, helping you identify sale items and calculate savings.

Introduction

In this tutorial, you'll learn how to create a simple Python program that helps you track and manage your tech accessories like Nomad chargers, phone cases, and watch bands. This is perfect for anyone who wants to organize their collection of tech accessories or find the best deals, just like the ones mentioned in the Nomad sale article. We'll build a basic inventory management system that can store information about your accessories and help you identify which items are on sale.

Prerequisites

To follow along with this tutorial, you'll need:

  • A computer with Python installed (Python 3.6 or higher recommended)
  • A text editor or IDE (like VS Code, PyCharm, or even Notepad)
  • Basic understanding of Python concepts like variables, lists, and dictionaries

Step-by-step instructions

Step 1: Setting up your Python environment

First, we need to create a new Python file for our project. Open your text editor and create a new file called accessory_tracker.py. This file will contain all our code for managing tech accessories.

Step 2: Creating the basic accessory data structure

We'll start by creating a simple data structure to represent our tech accessories. Each accessory will have properties like name, type, price, and whether it's on sale.

Code for Step 2:

# Create a list to store our accessories
accessories = []

# Define a sample accessory (like a Nomad charger)
charger = {
    "name": "Nomad USB-C Charger",
    "type": "charger",
    "price": 29.99,
    "on_sale": True,
    "discount": 25
}

# Add the charger to our accessories list
accessories.append(charger)

# Print the accessories to verify
print("Current accessories:")
for accessory in accessories:
    print(f"{accessory['name']} - ${accessory['price']}")

Step 3: Adding more accessories to our collection

Let's expand our collection by adding more accessories like phone cases and watch bands, similar to what was mentioned in the Nomad article.

Code for Step 3:

# Add more accessories to our collection
phone_case = {
    "name": "Nomad Phone Case",
    "type": "phone_case",
    "price": 19.99,
    "on_sale": True,
    "discount": 30
}

watch_band = {
    "name": "Nomad Watch Band",
    "type": "watch_band",
    "price": 24.99,
    "on_sale": False,
    "discount": 0
}

# Add these to our accessories list
accessories.append(phone_case)
accessories.append(watch_band)

# Display all accessories
print("\nUpdated accessory collection:")
for accessory in accessories:
    if accessory['on_sale']:
        print(f"{accessory['name']} - ${accessory['price']} (On Sale! {accessory['discount']}% off)")
    else:
        print(f"{accessory['name']} - ${accessory['price']}")

Step 4: Creating a function to find sale items

Now we'll create a function that can automatically identify which accessories are on sale, making it easy to find those great deals like the Nomad items mentioned in the article.

Code for Step 4:

def find_sale_items(accessories_list):
    """Find all accessories that are currently on sale"""
    sale_items = []
    for accessory in accessories_list:
        if accessory['on_sale']:
            sale_items.append(accessory)
    return sale_items

# Use the function to find sale items
sale_accessories = find_sale_items(accessories)

print("\nItems currently on sale:")
for item in sale_accessories:
    print(f"{item['name']} - {item['discount']}% off")

Step 5: Adding functionality to calculate savings

Let's enhance our program by adding a function that calculates how much money you would save on each sale item.

Code for Step 5:

def calculate_savings(accessory):
    """Calculate the savings amount for a sale item"""
    if accessory['on_sale']:
        savings = accessory['price'] * (accessory['discount'] / 100)
        return round(savings, 2)
    else:
        return 0

# Test the savings calculation
print("\nSavings calculation:")
for accessory in accessories:
    if accessory['on_sale']:
        savings = calculate_savings(accessory)
        print(f"{accessory['name']}: You save ${savings}")

Step 6: Creating a user-friendly interface

Let's make our program more interactive by adding a simple menu system that allows users to view items, check sales, and see savings.

Code for Step 6:

def display_menu():
    print("\n=== Nomad Accessory Tracker ===")
    print("1. View all accessories")
    print("2. View only sale items")
    print("3. View savings for sale items")
    print("4. Add new accessory")
    print("5. Exit")

def main():
    while True:
        display_menu()
        choice = input("\nSelect an option (1-5): ")
        
        if choice == '1':
            print("\nAll accessories:")
            for accessory in accessories:
                print(f"- {accessory['name']} (${accessory['price']})")
        
        elif choice == '2':
            print("\nItems on sale:")
            for accessory in accessories:
                if accessory['on_sale']:
                    print(f"- {accessory['name']} ({accessory['discount']}% off)")
        
        elif choice == '3':
            print("\nSavings for sale items:")
            for accessory in accessories:
                if accessory['on_sale']:
                    savings = calculate_savings(accessory)
                    print(f"- {accessory['name']}: Save ${savings}")
        
        elif choice == '4':
            # Add a new accessory
            name = input("Enter accessory name: ")
            type = input("Enter accessory type (charger/phone_case/watch_band): ")
            price = float(input("Enter price: "))
            on_sale = input("Is it on sale? (y/n): ") == 'y'
            discount = 0
            if on_sale:
                discount = int(input("Enter discount percentage: "))
            
            new_accessory = {
                "name": name,
                "type": type,
                "price": price,
                "on_sale": on_sale,
                "discount": discount
            }
            
            accessories.append(new_accessory)
            print(f"\nAdded {name} to your collection!")
        
        elif choice == '5':
            print("\nThank you for using Nomad Accessory Tracker!")
            break
        
        else:
            print("\nInvalid choice. Please try again.")

# Run the main program
if __name__ == "__main__":
    main()

Step 7: Testing your program

Now that you've built your accessory tracker, it's time to test it. Run your Python file and try out the different menu options:

  1. View all accessories to see your complete collection
  2. Check sale items to see which ones are currently discounted
  3. View savings to calculate how much you'd save on each sale item
  4. Add new accessories to expand your collection

Summary

In this tutorial, you've learned how to create a simple but effective accessory tracking program that can help you manage and identify deals on tech accessories like Nomad chargers, phone cases, and watch bands. You've built a system that stores information about different accessories, identifies which ones are on sale, calculates potential savings, and provides a user-friendly interface for managing your collection.

This program demonstrates basic Python concepts including lists, dictionaries, functions, and user input handling. The skills you've learned here can be expanded to create more complex inventory management systems or even integrated into larger applications for tracking personal tech collections or shopping deals.

Source: ZDNet AI

Related Articles