A Python script for recording and analyzing food prices from different locations. Track your favorite dishes, find the cheapest spots, and get insights like average prices per location and the best deal overall.
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("food_prices.json"):
with open("food_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 food price or (2) look up past food prices? Enter 1 or 2: ").strip()
if action == "1":
# Record a new food price
food_item = input("Enter the food item: ").strip()
location = input("Enter the location (restaurant/store): ").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,
"food_item": food_item,
"location": location,
"price": price
}
entries.append(new_entry)
with open("food_prices.json", "w") as f:
json.dump(entries, f, indent=4)
print(f"Food price recorded: {food_item} at {location} for ${price:.2f}")
elif action == "2":
# Look up past food prices and view statistics
if not entries:
print("No food price entries found. Please record a food price first.")
else:
lookup_type = input("Do you want to look up by (1) food item or (2) location? Enter 1 or 2: ").strip()
if lookup_type == "1":
# Look up by food item
food_item = input("Enter the food item to look up: ").strip()
matching_entries = [entry for entry in entries if entry["food_item"].lower() == food_item.lower()]
if matching_entries:
print(f"\nEntries for {food_item}:")
for entry in matching_entries:
print(f"- {entry['date']} {entry['time']}: {entry['location']} - ${entry['price']:.2f}")
# Find the cheapest spot for this food item
cheapest_entry = min(matching_entries, key=lambda x: x["price"])
print(f"\nCheapest spot for {food_item}: {cheapest_entry['location']} at ${cheapest_entry['price']:.2f}")
else:
print(f"No entries found for {food_item}.")
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['food_item']} - ${entry['price']:.2f}")
# Calculate average price at this location
prices = [entry["price"] for entry in matching_entries]
avg_price = mean(prices)
print(f"\nAverage food price at {location}: ${avg_price:.2f}")
# Find the cheapest food item at this location
cheapest_entry = min(matching_entries, key=lambda x: x["price"])
print(f"Cheapest food item at {location}: {cheapest_entry['food_item']} at ${cheapest_entry['price']:.2f}")
else:
print(f"No entries found for {location}.")
else:
print("Invalid choice. Please enter 1 or 2.")
# Overall best deal (cheapest food item overall)
if entries:
best_deal = min(entries, key=lambda x: x["price"])
print(f"\nBest deal overall: {best_deal['food_item']} at {best_deal['location']} for ${best_deal['price']:.2f}")
else:
print("Invalid choice. Please run the script again and enter 1 or 2.")