Drink Price Tracker Python Script

A Python script for recording and analyzing drink prices. Track your favorite cocktails, find the cheapest spots, and get insights like average prices per location and the best deal of the night.

Click to view script…
import json
import os
from datetime import datetime
from statistics import mean

# Load existing entries from the JSON file
if os.path.exists("drink_prices.json"):
    with open("drink_prices.json", "r") as f:
        entries = json.load(f)
else:
    entries = []

# Ask the user what they want to do
action = input("Do you want to (1) record a new drink price or (2) look up past drink prices? Enter 1 or 2: ").strip()

if action == "1":
    # Record a new drink price
    drink_name = input("Enter the drink name: ").strip()
    location = input("Enter the location (casino/bar): ").strip()
    while True:
        try:
            price = float(input("Enter the price in USD: "))
            break
        except ValueError:
            print("Invalid input. Please enter a number.")
    
    now = datetime.now()
    date_str = now.strftime("%Y-%m-%d")
    time_str = now.strftime("%H:%M:%S")
    
    new_entry = {
        "date": date_str,
        "time": time_str,
        "drink": drink_name,
        "location": location,
        "price": price
    }
    entries.append(new_entry)
    
    with open("drink_prices.json", "w") as f:
        json.dump(entries, f, indent=4)
    
    print(f"Drink price recorded: {drink_name} at {location} for ${price:.2f}")

elif action == "2":
    # Look up past drink prices and view statistics
    if not entries:
        print("No drink price entries found. Please record a drink price first.")
    else:
        lookup_type = input("Do you want to look up by (1) drink name or (2) location? Enter 1 or 2: ").strip()
        
        if lookup_type == "1":
            # Look up by drink name
            drink_name = input("Enter the drink name to look up: ").strip()
            matching_entries = [entry for entry in entries if entry["drink"].lower() == drink_name.lower()]
            
            if matching_entries:
                print(f"\nEntries for {drink_name}:")
                for entry in matching_entries:
                    print(f"- {entry['date']} {entry['time']}: {entry['location']} - ${entry['price']:.2f}")
                
                # Find the cheapest spot for this drink
                cheapest_entry = min(matching_entries, key=lambda x: x["price"])
                print(f"\nCheapest spot for {drink_name}: {cheapest_entry['location']} at ${cheapest_entry['price']:.2f}")
            else:
                print(f"No entries found for {drink_name}.")
        
        elif lookup_type == "2":
            # Look up by location
            location = input("Enter the location to look up: ").strip()
            matching_entries = [entry for entry in entries if entry["location"].lower() == location.lower()]
            
            if matching_entries:
                print(f"\nEntries for {location}:")
                for entry in matching_entries:
                    print(f"- {entry['date']} {entry['time']}: {entry['drink']} - ${entry['price']:.2f}")
                
                # Calculate average price at this location
                prices = [entry["price"] for entry in matching_entries]
                avg_price = mean(prices)
                print(f"\nAverage drink price at {location}: ${avg_price:.2f}")
                
                # Find the cheapest drink at this location
                cheapest_entry = min(matching_entries, key=lambda x: x["price"])
                print(f"Cheapest drink at {location}: {cheapest_entry['drink']} at ${cheapest_entry['price']:.2f}")
            else:
                print(f"No entries found for {location}.")
        
        else:
            print("Invalid choice. Please enter 1 or 2.")
        
        # Overall deal of the night (cheapest drink overall)
        if entries:
            deal_of_the_night = min(entries, key=lambda x: x["price"])
            print(f"\nDeal of the night: {deal_of_the_night['drink']} at {deal_of_the_night['location']} for ${deal_of_the_night['price']:.2f}")

else:
    print("Invalid choice. Please run the script again and enter 1 or 2.")