Blue Week Special Offer | Brighten your week!
days
hours
minutes
seconds

313-7 something wrong with source file

Screen Link: https://app.dataquest.io/m/313/conditional-statements/7/combining-logical-operators

Your Code:

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

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)

nonfree_games_social_ratings = []
for rown 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:
        nonfree_games_social_ratings.append(rating)
        
avg_non_free = sum(nonfree_games_social_ratings) / len(nonfree_games_social_ratings)

What I expected to happen: It looks identical to text in the answer field.

What actually happened: it returns an error because the AppStore file doesn’t have fields where the price is not zero, so the nonfree list remains empty.

Other details: here’s what the data in the file looks like:

Hey, Andrew.

Here’s a hint. Modify the last part of your code to the following:

# Non-free apps (average)
debug = []  # <------- Added this line
nonfree_games_social_ratings = []
for rown in apps_data[1:]:
    debug.append(row) # <------- And added this one too
    rating = float(row[7])
    genre = row[11]
    price = float(row[4])
    
    if (genre == 'Social Networking' or genre == 'Games') and price != 0:
        nonfree_games_social_ratings.append(rating)
        
avg_non_free = sum(nonfree_games_social_ratings) / len(nonfree_games_social_ratings)

I added a couple of lines.

Run this code and inspect debug. Try to figure out what is going on and fix it if you find the issue. Let me know if you can’t figure it out. It’s subtle, but instructive.

for some reason it worked after I added your lines.

It really shouldn’t have. Can you take a screenshot of it working?

You didn’t copy and paste :slight_smile:

You had a typo in your first try that I kept in the code in my reply, but that you didn’t keep when you tried to run the code I asked.

The typo was on the equivalent of row 20 above: for rown in apps_data[1.]:. This typo made it fail.

Oh, I thought it was your typo))! Thanks!

1 Like