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

Error message in the Mission about if and statements

https://app.dataquest.io/m/313/conditional-statements/5/multiple-conditions

Your Code: Enclose your code in 3 backticks like this to format properly: your code

from csv import reader
read_file = reader(opened_file)
apps_data = list(read_file)

free_games_ratings = []
for row in apps_data[1:]:
    rating = float(row[7])
    price = float(row[4])
    genre = row[11]
free_games_ratings = []
for row in apps_data[1:]:
    rating = float(row[7])
    genre = row[11]
    if price ==0.0 and genre == 'Games':
        free_games_ratings.append(rating)
        
avg_rating_free_games = sum(free_games_ratings) / len(free_games_ratings)```

What I expected to happen: I compared it with the answer and I don't see any errors

Error message: ```avg_rating_free_games is greater than what we expected.
free_games_ratings is longer than we expected.```

Hi @rehoyt, welcome to the community!

You have 2 instances of free_games_ratings and the loop. The first loop isn’t really doing anything, just cycling through. The 2nd loop is where you have the if-statement that will append results to free_game_ratings. In the 2nd version of the loop, price = float(row[4]) has been omitted. What this means is that the variable price will retain the last value from the 1st loop, and then this value will be used in the if-statement in your 2nd loop. This means that free_games_ratings will end up including game apps that are not free, because we’re ignoring the actual price.

Once you get one version of the loop that includes rating, price, and genre, it should work.

I hope that helps! Let me know if you need any clarification.

I looked the given answer and it had two loops (see below). When I deleted one of the loops I still got the error message

INITIAL CODE

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

free_games_ratings =
for row in apps_data[1:]:
rating = float(row[7])
price = float(row[4])
genre = row[11]
# Complete code from here

SOLUTION CODE

free_games_ratings =
for row in apps_data[1:]:
rating = float(row[7])
price = float(row[4])
genre = row[11]
if price == 0.0 and genre == ‘Games’:
free_games_ratings.append(rating)

avg_rating_free_games = sum(free_games_ratings) / len(free_games_ratings)

I think the way the solution is presented is a little confusing, but it’s showing more of a before-and-after since we’re completing the code snippet that was already started. I would recommend starting fresh by clicking the button next to Submit Code button to reset the initial code screen (image below), and then copying in your code for the if-statement and the average calculation.
image

Just in case, I tried copying and pasting the solution code as-is (with the extra loop and all), and the answer still passed. This is probably because the 2nd loop in the solution still had the price = float(row[4]) line in it, so it’s just overwriting everything from the 1st loop.

Many thanks for your help…Bob

1 Like

No problem, happy coding!