LIMITED TIME OFFER: 50% OFF OF PREMIUM WITH OUR ANNUAL PLAN (THAT'S $294 IN SAVINGS).
GET OFFER

Different output from what was in the solution code

Screen Link:
https://app.dataquest.io/m/350/guided-project%3A-profitable-app-profiles-for-the-app-store-and-google-play-markets/12/most-popular-apps-by-genre-on-the-app-store

I am asking this question to get a deeper understanding of what I did wrong. The aim of this code is to find out what genres are the most popular (have the most users) by calculating the average number of installs for each app genre.

This was my code:

apple_genres = new_freq_table(apple_final, 11)
total = 0      # This variable will store the sum of user ratings (the number of ratings received, not the actual app ratings) of apps specific to each genre.
len_genre = 0  # This variable will store the number of apps per genre.
    
for key in apple_genres:      # apple_genres is a dictionary containing the frequency table of genre column in apple_final
    for app in apple_final:
        genre = app[11]
    
        if genre == key:
            ratings_ct = float(app[5])
            total += ratings_ct
            len_genre += 1

    average_ratings_ct = total / len_genre
    print(key,':', average_ratings_ct)

What I expected to happen:
I got an answer but it’s different from the output of the solution.
This was the output:

Genres of free English apps on Apple Store:

Social Networking : 71548.34905660378
Book : 67839.53333333334
Games : 25499.85506519559
Navigation : 25681.6265
Sports : 25592.492508458192
Finance : 25692.975296912115
...

I rewrote my code in the format from the solution and got different output:

apple_genres = new_freq_table(apple_final, 11)
    
for key in apple_genres:      # apple_genres is a dictionary containing the frequency table of genre column in apple_final
    total = 0      # This variable will store the sum of user ratings (the number of ratings received, not the actual app ratings) of apps specific to each genre.
    len_genre = 0  # This variable will store the number of apps per genre.

    for app in apple_final:
        genre = app[11]
    
        if genre == key:
            ratings_ct = float(app[5])
            total += ratings_ct
            len_genre += 1

    average_ratings_ct = total / len_genre
    print(key,':', average_ratings_ct)

The output was:

Social Networking : 71548.34905660378
Book : 39758.5
Games : 22788.6696905016
Navigation : 86090.33333333333
Sports : 23008.898550724636
Finance : 31467.944444444445
...

The issue, as you already know, is related to the above two.

Focus on what those variables are storing -

This variable will store the sum of user ratings (the number of ratings received, not the actual app ratings) of apps specific to each genre.

This variable will store the number of apps per genre.

Those variables store values based on each genre.

If you initialize the variables outside the for loop, then their values will accumulate for all genres. That’s what you are doing currently.

However, if you change it such that -

for key in apple_genres:
    total = 0 
    len_genre = 0 

The two are initialized inside the first for loop, then for every iteration of the loop, those values will be reset back to 0. Since the loop iterates over the different genres, that means you are only updating those values for each genre and printing them out accordingly instead of adding all the values across all the genres.

Let me know if this makes sense or not.

3 Likes

Wow, this made perfect sense now. Thank you so much! I would need to improve on my theoretical knowledge of python.

1 Like

Hi,

I run the code exactly like the one in the solution and I still get different numbers, different number of rating events. I got my variables in the first loop and all exactly like the solution book. Yet I’m not getting the same output

(after calculating the averages for each genre, I made tuples and sorted them, that’s the only difference)

I don’t know how to share my code and output here…

my output:

Reference : 67448
Music : 56482
Social Networking : 53078
Weather : 47221
Photo & Video : 27250
Navigation : 25972
Travel : 20216
Food & Drink : 20179
Sports : 20129
Health & Fitness : 19952
Productivity : 19054
Games : 18925
Shopping : 18747
News : 15893
Utilities : 14010
Finance : 13522
Entertainment : 10823
Lifestyle : 8978
Book : 8498
Business : 6368
Education : 6266
Catalogs : 1780
Medical : 460

Please create a separate post instead of adding to existing questions that have been resolved. Your question doesn’t seem directly relevant to the main question of this particular post.

When you create a new post, you can follow these guidelines -