-1
 if genre == 'Social Networking' or 'Games':

The above results in a different output than the below:

 if genre == 'Social Networking' or  genre == 'Games':

Does anyone know why this would be the case? Full code below if needed, it is just from a python beginner course I am doing on dataquest.

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

games_social_ratings = []
for row in apps_data[1:]:
    rating = float(row[7])
    genre = row[11]
    # Complete code from here
    if genre == 'Social Networking' or == 'Games':
        games_social_ratings.append(rating)
avg_games_social = sum(games_social_ratings)/len(games_social_ratings)

print (avg_games_social)
Maestro
  • 1
  • 1
  • 2
    The first one is invalid syntax. The second one is valid. – Unmitigated Feb 20 '23 at 03:09
  • Apologies, I messed up on the first one. The actual line was not …. or ‘Games’, not or == ‘Games’. The script still runs, but the averages are different. – Maestro Feb 20 '23 at 03:09
  • doing `or 'Games'` is not comparing `'Games'` to `genre`. `'Games'` is a string with a truthy value. Since it is not a blank string, `'Games'` will evaluate as `True`. therefore, doing `or 'Games'` will cause that if statement to always evaluate as `True`. – Shorn Feb 20 '23 at 03:13
  • `==` has higher precedence than `or`, so your first statement is parsed as `if (genre == 'Social Networking') or ('Games'):`. The string 'Games' is always True, so the result of the `or` will always be True. – Tim Roberts Feb 20 '23 at 03:13
  • @TomKarzes I changed my post. It was indeed invalid when I put it there initially, I typed it wrong and typed as “… == ‘Games’ “ – Maestro Feb 20 '23 at 03:21
  • I appreciate all the help here gents, I think I understand a little, but emphasis on the little. Is anyone able to explain why the first (incorrect) statement included some values that shouldn’t have been included and did not include some values that did need to be included in the averages? What is it using in that case to calculate the average rating? – Maestro Feb 20 '23 at 03:23
  • @Maestro Um - you really shouldn't go making significant changes like that to the posted code. Adding something that's missing is fine. Fixing bugs isn't. Post the actual code the first time. Don't make changes to it. – Tom Karzes Feb 20 '23 at 03:24

1 Answers1

-1

The only way for this line to make sense:

if genre == 'Social Networking' or 'Games':

You probably expected this to mean something like this (but it doesn't actually evaluate to that):

if genre == ('Social Networking' or 'Games'):

Here, 'Social Networking' or 'Games' would be or operating on two string values as if they were boolean values, and an empty string '' is not truthy (i.e. evaluates to False), while a non-empty string is truthy (i.e. evaluates to True).

So, 'Social Networking' or 'Games' is True and the code would be the same as:

if genre == (True):

That's not what you want. or is a logical operator that takes two boolean values and evaluates to True if either or both of the operands is True. It doesn't mean everything 'or' means in English.

As pointed out in the comments however, what really happens here is:

if (genre == 'Social Networking') or 'Games':

Because == binds more strongly than or, so genre == 'Social Networking' gets evaluated first. If that evaluates to True, the result of the whole expression would be True (the second operand of or doesn't even get evaluated). If that evaluates to False (i.e. genre is not 'Social Networking'), the whole expression evaluates to 'Games', which is truthy, and thus the whole expression is True.

In either case, the outcome is True, no matter the value of genre.

Grismar
  • 27,561
  • 4
  • 31
  • 54
  • This is simply not true. `==` binds higher than `or`, so it's `(genre == 'Social Networking') or ('Games')`. You might want to delete this. – Tim Roberts Feb 20 '23 at 03:14
  • Ah you're right of course, this would evaluate to `'Games'` - I'll update the answer – Grismar Feb 20 '23 at 03:15
  • @Grismar This question gets asked almost daily. It's a common beginner's mistake. In fact this post has been closed as a duplicate. There's no need for yet another answer. – Tom Karzes Feb 20 '23 at 03:18
  • @TomKarzes I agree that the type of question comes up very often - however I feel there's not a really good canonical answer. To people who struggle with the underlying problem, I don't think the question that was linked seems like it answers theirs (even though it clearly does, once you understand the issue). But of course I agree that it would be better to have all the information about the broader issue in one place. – Grismar Feb 20 '23 at 03:21