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

The example for The or Operator lesson is confusing

Screen Link:
https://app.dataquest.io/m/313/conditional-statements/6/the-or-operator

My Code:

Replace this line with your code

What I expected to happen: My question is the example in the screen link. print(games_social_ratings[:5]) how did it get [3.5, 4.5, 4.5, 4.5, 4.5].
I thought it should be [3.5, 4.5, 4.5, 4.0, 4.5] when I look at the user rating I only see 3.5, 4.5, 4.5, 4.5, 4.0

What actually happened:

Replace this line with the output/error

What results do you get when you run the code:

games_social_ratings = []
for row in apps_data[1:]:
    rating = float(row[7])
    genre = row[11]
    if genre == 'Social Networking' or genre == 'Games':
        games_social_ratings.append(rating)
print(games_social_ratings[:5]) 

You should get the same as the Learn section shows (I also get this too):

[3.5, 4.5, 4.5, 4.5, 4.5]

Are you getting something different?

Why do you think it should be:

[3.5, 4.5, 4.5, 4.0, 4.5]

Keep in mind this output comes from the code: print(games_social_ratings[:5]) which will print the first five items in the list games_social_ratings. Those first five items in the list are: [3.5, 4.5, 4.5, 4.5, 4.5]

BONUS: I ran the command print(games_social_ratings.index(4.0)) in order to see where in the list I could find a rating of 4.0 and got a result of 37…which means the first occurrence of a rating of 4.0 in the games_social_ratings list is at index=37…which means you definitely shouldn’t be seeing 4.0 in your result of the first five items in that list!

Hi mathmike I thought those numbers came from user rating column.

You are correct, they do indeed come from the user_rating column…however…the results have been filtered so that we only get back the user ratings rows which have prime_genre (column 11) equal to either "Social Networking" or "Games". If you scroll over a bit in your image, you can see the row that you’ve highlighted has prime_genre == "Music" which is why we aren’t seeing it in our results for games_social_ratings.

You’ll notice that the first, third, and fourth rows belong to Social Networking, Games, and Games (respectively) which is why those user_ratings are the first three results returned in print(games_social_ratings[:5])

Thanks mathmike it makes sense now. I can see the first 3 results 3.5, 4.5 and 4.5. But how about those last 2 results 4.5 and 4.5. It would be nice if they gave a screenshot of a bigger table so I can see the additional rows that had Social Networking and Games so I can see last two results 4.5.

As luck would have it, you can produce such a table yourself! There’s probably a way to do it while still in list format but I prefer to work with pandas objects (you’ll see these soon enough!). Just copy and paste this code in script.py and run the code.

import pandas as pd
df_apps = pd.DataFrame(data=apps_data)
df_apps.columns = ['', *df_apps.iloc[0][1:]]
df_apps.drop(df_apps.index[0], inplace=True)
df_apps.reset_index(inplace=True)
print(df_apps.loc[:10,['track_name', 'user_rating', 'prime_genre']])

Feel free to add/remove column names to the last line and change the :10 to get more/less lines.

If you have any other questions, let me know! If you feel I’ve answered your original question somewhere along the line, please click on the little box that says solution below the message you feel answered your question.

Happy learning!

Thanks mathmike now I can see the complete table. :grinning:

1 Like