Home > database >  For Loop - Failing to Iterate Over Elements
For Loop - Failing to Iterate Over Elements

Time:01-17

Issue: The for loop for this function is not iterating the over all elements. Its stopping at 1. I used some diagnostic print statements to count the number of loops and its stopping at 1. I have reviewed the indentiation and the loop but cannot seem to find the issue.

def process_data(data):
    """Analyzes the data, looking for maximums.

    Returns a list of lines that summarize the information.
    """
    loop_count = 0
    year_by_sales = dict()
    max_revenue = {"revenue": 0}
    # ----------->This is where the Loop Issue Exists <-----
    for item in data:
        item_price = locale.atof(item["price"].strip("$"))
        item_revenue = item["total_sales"] * item_price
        if item["car"]["car_year"] not in year_by_sales.keys():
            year_by_sales[item["car"]["car_year"]] = item["total_sales"]
            loop_count  = 1
            if item_revenue > max_revenue["revenue"]:
                item["revenue"] = item_revenue
                max_revenue = item
                most_sold_model = item['car']['car_model']
                highest_total_sales = item["total_sales"]
        else:
            year_by_sales[item["car"]["car_year"]]  = item["total_sales"]
            loop_count  =1 
        most_popular_year = max(year_by_sales, key=year_by_sales.get)
        summary = [
            "The {} generated the most revenue: ${}".format(
                format_car(max_revenue["car"]), max_revenue["revenue"]
            ),
            f"The {most_sold_model} had the most sales: {highest_total_sales}",
            f"The most popular year was {most_popular_year} with {highest_total_sales} sales.",
        ]
        print(loop_count)
        print(year_by_sales)
        return summary

Input Data

[{
        "id": 1,
        "car": {
            "car_make": "Ford",
            "car_model": "Club Wagon",
            "car_year": 1997
        },
        "price": "$5179.39",
        "total_sales": 446
    },
    {
        "id": 2,
        "car": {
            "car_make": "Acura",
            "car_model": "TL",
            "car_year": 2005
        },
        "price": "$14558.19",
        "total_sales": 589
    },
    {
        "id": 3,
        "car": {
            "car_make": "Volkswagen",
            "car_model": "Jetta",
            "car_year": 2009
        },
        "price": "$14879.11",
        "total_sales": 825
    }]

The entire codebase for this script is https://replit.com/join/dkuzpdujne-terry-brooksjr

CodePudding user response:

Actually problem is that your return statement is inside the for loop so you return after the first iteration itself, it should run just fine if you move it outside something like below:

def process_data(data):
    """Analyzes the data, looking for maximums.

    Returns a list of lines that summarize the information.
    """
    loop_count = 0
    year_by_sales = dict()
    max_revenue = {"revenue": 0}
    # ----------->This is where the Loop Issue Exists <-----
    for item in data:
        item_price = locale.atof(item["price"].strip("$"))
        item_revenue = item["total_sales"] * item_price
        if item["car"]["car_year"] not in year_by_sales.keys():
            year_by_sales[item["car"]["car_year"]] = item["total_sales"]
            loop_count  = 1
            if item_revenue > max_revenue["revenue"]:
                item["revenue"] = item_revenue
                max_revenue = item
                most_sold_model = item['car']['car_model']
                highest_total_sales = item["total_sales"]
        else:
            year_by_sales[item["car"]["car_year"]]  = item["total_sales"]
            loop_count  =1 
        most_popular_year = max(year_by_sales, key=year_by_sales.get)
        summary = "1"

        print(loop_count)
        print(year_by_sales)
    return summary # move this out of for loop

  • Related