Building a Python Fuel Efficiency Tracker – Need Feedback!

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!

A big project like this is a great way to learn. I think you need to work on it more before you ask for feedback though.

Start adding some features and make liberal use of internet searches to answer your own questions

  • “Python CSV vs JSON”
  • “Python how to use matplotlib to visualize trends”

etc.

Thinking about the problems and implementing different solutions is part of the learning process. It seems like you are trying to skip that step.

You should come back when you are really struggling with something that you cannot get past or you are basically finished and want advice on how to improve it.

This is a good start, keep going!

You should also continue to go through the curriculum and many of your questions might be answered as you go.

https://www.freecodecamp.org/learn/data-analysis-with-python/data-analysis-with-python-course/data-cleaning-and-visualizations