Float division by zero error in def function(Guided project 1)

In the guided project1, I was going to create a dictionary that
contains the category and average number of installs. But there was an error: float division by zero. Here is my code:

category_android = freq_table(android_final, 1)

unsorted_category_install = {}
for category in category_android:
    total = 0.0
    len_category = 0.0 
    
    for app in android_final:
        category = app[1]
        if category == genre:
            install = app[5]
            install = install.replace(',', '')
            install = install.replace('+', '')
            total += float(install)
            len_category += 1
    average_install = total / len_category
    unsorted_category_install[category] = average_install
    print unsorted_category_install

I was confused because I did transform str to float.

Hi @stellayou1126,
Can you tell us what is the variable gender in

did you mean genre = app[1] ?

Oh, you gave me a hint and I knew what’s the problem. I named two different variables into one same name ‘category’, it should be if category_app == category.

Thank you!

1 Like