313-6 Question on "if, or" conditional statement

Hello guys,

Newbie here.

I have a question on the “if, or” conditional statement for exercise 313-6 (Data Analyst, Python).

The instructions that were provided are to:

Complete the code in the editor to compute the average rating of the apps whose genre is either “Social Networking” or “Games.”

Initially, I’ve written my code as:

if genre == ‘Social Networking’ or ‘Games’:
games_social_ratings.append(rating)

This gave me a figure of 3.5 (for example). I realised that this was wrong, and the answer is infact

if genre == ‘Social Networking’ or genre == ‘Games’:
games_social_ratings.append(rating)

which gave me a rating of 3.6. In this case, I missed out the second part of the “genre ==”. However, I’d like to know what is being calculated in my first code where the rating is 3.5? It seems like removing the “or genre == ‘Games’” would give me 2.98 rating (solely for Social Networking). So, what is python calculating when I missed out the “genre ==” for the 2nd part of my 1st code?

hello,
I think I have an explanation for u!
In your first code, as you didn’t mention the variable name after ‘or’,Python considers it as:
if genre == ‘Social Networking’ or ‘Games’: means
if (genre== "Social Networking") or ("Games" evaluates to True)
Here,the second condition is considered always as True.So,even if when the first condition is considered as false,the second condition is always considered as true which provides you the list with all the values and thus both the condition is not checked here as wanted.
But in case of your second code, the conditions are checked properly and you got the list according to that.

1 Like