Blue Week Special Offer | Brighten your week!
days
hours
minutes
seconds

TypeError: 'float' object is not callable: ipython-input-1-e2d57e72c347> in <module>

Screen Link: if, else statements

My Code:
# INITIAL CODE
opened_file = open(‘AppleStore.csv’)
from csv import reader
read_file = reader(opened_file)
apps_data = list(read_file)

free_apps_ratings = []
for row in apps_data[1:]:
    rating = float(row[7])
    price = float(row[4])   
    if price == 0.0:
        free_apps_ratings.append(rating)
    elif price != 0.0:
        non_free_apps_ratings(rating)
    
avg_rating_free = sum(free_apps_ratings) / len(free_apps_ratings)
avg_rating_non_free = sum(non_free_apps_ratings) / len(non_free_apps_ratings)
print(avg_rating_non_free)

What I expected to happen:

At least not an error and the expected result of the average rate from the non free apps

What actually happened:

Throw TypeError: ‘float’ object is not callable

Replace this line with the output/error


I believe it might be a runtime error from the Ipython enviroment and that is why it is bringing up the TypeError, since it stills brings up the error even with the initial code

1 Like

Hi @chaliv203,

Welcome to the Community!

I see 2 things to be fixed in your code:

  1. Don’t forget to initiate the non_free_apps_ratings empty list before the for-loop, just as you did with the free_apps_ratings list.
  2. Don’t forget to use append in the elif statement.

When you fix these 2 issues your code will run smoothly.

1 Like

Hi @Elena_Kosourova ,

Your answer to the thread was right. When I was debugging the program found even without initiating an empty list also the getting expected output for the program. So what is the reason for initiating an empty list?. Kindly help !

Hi @haimillar,

I guess that it happened because initially you ran the code with initiating both empty lists at the beginning, i.e.:

free_apps_ratings = []
non_free_apps_ratings = []
for row in apps_data[1:]:
    rating = float(row[7])
    price = float(row[4])   
    if price == 0.0:
        free_apps_ratings.append(rating)
    elif price != 0.0:
        non_free_apps_ratings.append(rating)
    
avg_rating_free = sum(free_apps_ratings) / len(free_apps_ratings)
avg_rating_non_free = sum(non_free_apps_ratings) / len(non_free_apps_ratings)

Now, if you remove one or even both of the first 2 lines of the code above and re-run the code without initiating an empty list (or both empty lists), the program will still remember your previous variables that you created (free_apps_ratings, non_free_apps_ratings, or both) and will append new values exactly to them at each iteration of the for-loop. To check it, try the following at the end of each cases (with initiating both empty lists or without initiating at least one of them):

print(len(free_apps_ratings))
print(len(non_free_apps_ratings))

You’ll see that the length changed. The program didn’t throw any error but just continued adding new items to the lists that it “remembered” earlier.

All in all, if you don’t create any list (empty or not) before the for-loop, or if the program doesn’t have in its memory a list with such a name from your previous code, then when you try to append something to a non-existing list, it will throw an error.

1 Like

Thanks @Elena_Kosourova your answer make sense. Just come to know by your answer the initialized empty list removed later during the program will have the empty list stored in the memory. Thanks for sharing the knowledge.

1 Like

Just want to clarify again: the program (after removing the line where you initiated an empty list) will remember not an empty list, but the last version of it (in this case - the version after the last run of the for-loop). For example, if you completely close the program and then start it again, it will not remember anymore previously created variables (including the lists). So, if this time you don’t initiate any of the lists again and if you try to append something to them inside the for-loop, the program will throw an error.

1 Like

Thanks @Elena_Kosourova got your point. Now I am completely clear .

1 Like

Thanks, this was helpful.

1 Like