Nested loop in "Popular Apps by Genre" is working....... sorta

Screen Link:

My Code:

genreFreqFree=(freq_table(iosFree,-5))
# frequency table of free ios apps by genre, shown as percentages
[Basics.ipynb|attachment](upload://uw5vMoA52tLXFdOQUNug9gWCXr6.ipynb) (24.9 KB)

print(genreFreqFree)
{'Photo & Video': 4.97, 'Games': 58.18, 'Music': 2.05, 'Social Networking': 3.26, 'Reference': 0.56, 'Health & Fitness': 2.02, 'Weather': 0.87, 'Utilities': 2.51, 'Travel': 1.24, 'Shopping': 2.61, 'News': 1.33, 'Navigation': 0.19, 'Lifestyle': 1.58, 'Entertainment': 7.89, 'Food & Drink': 0.81, 'Sports': 2.14, 'Book': 0.43, 'Finance': 1.12, 'Education': 3.66, 'Productivity': 1.74, 'Business': 0.53, 'Catalogs': 0.12, 'Medical': 0.19}
for genre in genreFreqFree:
    total=0
    len_genre=0
    for app in ios[1:]:
        genre_app= app[-5]
        if genre_app == genre:
            n_userRatings= float(app[5])
            total+= n_userRatings
            len_genre+= 1
        
avgRatings_n= total/ len_genre
print(genre,'genre ratings total:', avgRatings_n)

What I expected to happen:
this… but over more than one genre!

What actually happened:

avgRatings_n= total/ len_genre
print(genre,'genre ratings total:', avgRatings_n)
Medical genre ratings total: 592.7826086956521

not sure why i’m only getting medical genre. as noted in freq table output i have 23 genres for ios but for some reason i am only getting one group ouputted from my nested loop functions.
any ideas what i have done here? notebook attached. im stumped.

1 Like

Hi @antlavia ,

Try moving your avgRatings_n = total/len_genre and print( ) statements within the for loop. When your statement is outside the for loop, it looks like it only read the last genre Medical. You can read more about scope here and here.

Does this help?

for genre in genreFreqFree:
    total=0
    len_genre=0
    for app in ios[1:]:
        genre_app= app[-5]
        if genre_app == genre:
            n_userRatings= float(app[5])
            total+= n_userRatings
            len_genre+= 1
        
    avgRatings_n= total/ len_genre
    print(genre,'genre ratings total:', avgRatings_n)

Photo & Video genre ratings total: 14352.280802292264
Games genre ratings total: 13691.996633868463
Music genre ratings total: 28842.021739130436
Social Networking genre ratings total: 45498.89820359281
Reference genre ratings total: 22410.84375
Health & Fitness genre ratings total: 9913.172222222222
Weather genre ratings total: 22181.027777777777
Utilities genre ratings total: 6863.822580645161
Travel genre ratings total: 14129.444444444445
Shopping genre ratings total: 18615.32786885246
News genre ratings total: 13015.066666666668
Navigation genre ratings total: 11853.95652173913
Lifestyle genre ratings total: 6161.763888888889
Entertainment genre ratings total: 7533.678504672897
Food & Drink genre ratings total: 13938.619047619048
Sports genre ratings total: 14026.929824561403
Book genre ratings total: 5125.4375
Finance genre ratings total: 11047.653846153846
Education genre ratings total: 2239.2295805739514
Productivity genre ratings total: 8051.3258426966295
Business genre ratings total: 4788.087719298245
Catalogs genre ratings total: 1732.5
Medical genre ratings total: 592.7826086956521

3 Likes

Ahhhh. Got it. when the instruction area said to put it outside the loop i didnt realize it needed to stay within the first indent.

thanks @Casandra_Hayward !

2 Likes