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

Error in conditional statement code, most probably a bug

Screen Link:
https://app.dataquest.io/m/313/conditional-statements/4/the-average-rating-of-gaming-apps

My Code:

opened_file = open(‘AppleStore.csv’)
from csv import reader
read_file = reader(opened_file)
apps_data = list(read_file)
non_games_ratings =
for data in apps_data[1:]:
rating = float(row[7])
genre = row[11]

if genre != 'Games':
    non_games_ratings.append(rating)

avg_rating_non_games = sum(non_games_ratings) / len(non_games_ratings)

print(avg_rating_non_games)
What I expected to happen:

**What actually happened: **
This was the error message

  • avg_rating_non_games is less than what we expected.
  • non_games_ratings is longer than we expected.

The same code typed above was working perfectly fine in the previous screen (exercise). And even in the solution same code is given. This entire section (Conditional Statements) have this issue. Due to that, I was not able to finish the section. Here’s the link. I believe its some kind of bug, because the code appears to be fine. Here’s the link to the previous screen
https://app.dataquest.io/m/313/conditional-statements/3/the-average-rating-of-non-free-apps.

Please help.

1 Like

You are looping through the data using for data in apps_data[1:], but you are using row to assign the variabes rating and genre. You have to do:

for data in apps_data[1:]:
rating = float(data[7])
genre = data[11]

or:

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

It is not a bug.

1 Like

Hi,

Thanks for the reply. I changed the code as you have suggested, but still the same error message. Kindly check the error message carefully, because I’m clueless, where I’m going wrong. I’ve included my comments in bold in line with error messages. Please

opened_file = open(‘AppleStore.csv’)
from csv import reader
read_file = reader(opened_file)
apps_data = list(read_file)
non_games_ratings =
for row apps_data[1:]:
rating = float(row[7])
genre = row[11]

if genre != 'Games':
    non_games_ratings.append(rating)

avg_rating_non_games = sum(non_games_ratings) / len(non_games_ratings)

print(avg_rating_non_games)

Error Message

  • Running your code caused an error.
  • non_games_ratings isn’t defined in your code, but we expected it to be list type - It’s defined
  • avg_rating_non_games isn’t defined in your code, but we expected it to be float type - Even if not defined, it should work

Notice that is not the same error again.

There’s an error in the for, it is missing the in.

for row apps_data[1:]:

It should be:

for row in apps_data[1:]:

You probably deleted the in while replacing data for row.

2 Likes

Hi Otavios

It’s resolved. I don’t believe, how silly error I was making.

Thanks
Ashish

1 Like

Hello Otavios.s,

I think I have the same issue. Mine says the value of avg rating non game is greater than what is expected. My output value is 3.526…

opened_file = open('AppleStore.csv')
from csv import reader
read_file = reader(opened_file)
apps_data = list(read_file)
non_games_ratings = []
for data in apps_data[1:]:
    rating = float(data[7])
    genre = data[11]
    
    if genre != 'Game':
        non_games_ratings.append(rating)
avg_rating_non_games = sum(non_games_ratings) / len(non_games_ratings)
print(avg_rating_non_games)

Hello @kaytwo_why, welcome to the community!

There’s a typo in your code. You typed "Game" instead of "Games in the following line:

if genre != 'Game':

Hello @otavios.s

It worked. Thanks for the correction.
Little mistake, disturbing headache. I will be more observant…thanks again

1 Like

Hi @otavios.s, I think I have the same issue.
My code is

opened_file = open('AppleStore.csv')
from csv import reader
read_file = reader(opened_file)
apps_data = list(read_file)
non_games_ratings = []
for row in apps_data[1:]:
    rating = float(row[7])
    genre = row[11]
        
    if genre != 'Games':
        non_games_ratings.append(rating)
        
avg_rating_non_games = sum(non_games_ratings) / len(non_games_ratings) 

I think it should be correct, but there’s error message :slight_smile:

  • non_games_ratings isn’t defined in your code, but we expected it to be list type
  • avg_rating_non_games isn’t defined in your code, but we expected it to be float type

Please help, Thanks!

Hello @x.he27, welcome to he community!

Your code is working fine on my end and I get the “Nice Work!” message. Try to refresh the page or to restart the browser and run it again. It should work.