Divide by zero error?

Wondering why I’m getting this error. I’m thinking it must be because the genre_app == genre is False so it never increments the len_genre.
### calculate most popular apps by genre ###

genres_ios = freq_table(ios, -5)

for genre in genres_ios:
    total = 0
    len_genre = 0
    for genre_app in ios:
        genre_app = app[-5]
        if genre_app == genre:
            user_ratings = float(app[5])
            total += user_ratings
            len_genre += 1
    avg_user_ratings = total / len_genre

Basics (3).ipynb (28.0 KB)

Click here to view the jupyter notebook file in a new tab

It’s here:

    for genre_app in ios:
        genre_app = app[-5]

You’re using app[5] but accidentally called your iteration variable genre_app. Changing it to for app in ios: fixes that problem.

Be careful with the print() statements at the end. Since they’re outside the loop, they’ll only print for you the last genre and avg_user_ratings.

1 Like