Hi freeCodeCamp folks! I’m new to the forum and just started diving into Python through the freeCodeCamp curriculum. As a car enthusiast who loves crunching numbers, I’m working on a small Python project to track fuel efficiency trends for my road trips, and I’d love your input to make it better!
My goal is to create a script that takes a list of trip data (miles driven, gallons used) and calculates average fuel efficiency (MPG) while flagging trips with unusually low efficiency. I came across a super clean fuel calculator at kalkulatorpaliwa.com.pl that inspired me to aim for a simple but useful tool. I want my script to eventually output stats like total fuel used and maybe even graph trends (thinking matplotlib).
Here’s a basic version I’ve got so far:
def calculate_mpg(trips):
total_miles = 0
total_gallons = 0
for miles, gallons in trips:
if miles <= 0 or gallons <= 0:
return "Invalid input! Miles and gallons must be positive."
total_miles += miles
total_gallons += gallons
avg_mpg = total_miles / total_gallons
return round(avg_mpg, 2)
# Example usage
trips = [(200, 8), (150, 6.5), (300, 12)]
print(calculate_mpg(trips)) # Outputs: 24.62
I’m planning to add features like:
- Detecting outliers (e.g., trips with MPG below a threshold).
- Saving trip data to a CSV file.
- Plotting efficiency trends over time.
As a beginner, I’m not sure how to structure this for scalability or handle edge cases (like missing data). Questions for you all:
- What’s the best way to store and load trip data (CSV, JSON, or something else)?
- Any tips for adding matplotlib charts to visualize trends?
- How would you improve the outlier detection logic?
Thanks for any advice or ideas, and thrilled to be learning with this awesome community!