313-7 Combining Logical Operators

I’m not sure why this code is throwing an error. I’m told that I’m dividing by zero. So the length of the non_free_social_games_ratings list that I’ve created is zero? I’m not sure how or why that is.

opened_file = open('AppleStore.csv')
from csv import reader
read_file = reader(opened_file)
apps_data = list(read_file)
opened_file.close()

free_games_social_ratings = []
for row in apps_data[1:]:
    rating = float(row[7])
    genre = row[11]
    price = float(row[4])

    if (genre == 'Social Networking' or genre == 'Games') and price == 0:
        free_games_social_ratings.append(rating)

avg_free = sum(free_games_social_ratings) / len(free_games_social_ratings)

# Non-free apps (average)

non_free_social_games_ratings = []
for item in apps_data[1:]:
    rating = float(item[7])
    genre = item[11]
    price = float(row[4])

    if (genre == 'Social Networking' or genre == 'Games') and price != 0:
        print("Here")
        non_free_social_games_ratings.append(rating)

avg_non_free = sum(non_free_social_games_ratings) / len(non_free_social_games_ratings)

My code is almost identical to the one that the answer shows. I could copy and paste and move on, but I’d really like to know why my code isn’t working.

This error is happening on 7. Combining Logical Operators mission of Conditional Statements in the Python Fundamentals course.

Hey, Charles.

Here’s a hint. What is the value of row in the second loop?

1 Like

ah, I was using item as my iterator instead of row and I was not using row for the price variable.

Thank you, that was driving me up the wall.