31 Years of Python | 48 Hour Sale Extension!!!

Dealing with errors

Continuing the discussion from Multiple list elements:

Screen Link: https://app.dataquest.io/c/115/m/609/python-for-loops/8/dealing-with-errors
My Code:

opened_file = open('AppleStore.csv')
from csv import reader
read_file = reader(opened_file)
apps_data = list(read_file)

apps_data = apps_data[1:]

rating_sum = 0
for row in apps_data[1:]:
    rating = float(row[1:][7])
    rating_sum = rating_sum + rating
    avg_rating = rating_sum / 7197
  avg_rating = rating_sum / len(apps_data[1:])                           

What I expected to happen:

float (<class ‘float’>)
-+ 3.526955675976101

What actually happened:

  • 3.253091565930249
Replace this line with the output/error
avg_rating is less than what we expected.

<!--Dividing the rating_sum and app_data(excluding header row) to find avg_rating.
Here, I thought  dividing using len() function or number of rows(7,197) will give same results. But I am getting the error that the avg_rating is less. where am i going wrong? Guide me pls : -->

I would highly recommend that you go through the content on lists and list indexing/slicing again. Because it doesn’t seem you have reached a level of intuitive understanding of that concept. Which is perfectly ok, you just need to review it a bit more.

There are a couple of issues in your code.

Firstly, you keep slicing apps_data.

apps_data = apps_data[1:]
for row in apps_data[1:]:
avg_rating = rating_sum / len(apps_data[1:])

If I gave you the list what would be the output of the following:

a = [1, 2, 3, 4]
a= a[1:]
a= a[1:]

Think about the above and think more about how (and why) you are using apps_data[1:] in your code.

Secondly, the following

  • Print out a single row, then
  • print out row[1:], and then
  • print out row[1:][7].

Do you get the value you need? Cross-check with the data and see if it matches up or not. Think about what each step above is doing and how you are slicing and indexing the list.


To exclude the header, app_data is stored as app_data[1:].

apps_data = **apps_data[1:]**--> to define it 
for row in **apps_data[1:]**:---> for loop
avg_rating = rating_sum / len**(apps_data[1:]**) for finding avg_rating using len() ==> this is how i understood abt app_data[1:]  at each time it is used.

the example you have mentioned:
a = [1, 2, 3, 4]
a= a[1:]
a= a[1:]

<<<[2, 3, 4]>>>

<<<[3, 4]>>> why??

It's a good suggestion about going through indexing/slicing topic again to know how slicing varies at each step. Really, appreciate your time for replying.

Because you are modifying a twice.

First time -

You modify a and save the result back into a. So, now a contains [2,3,4]

Then, you modify a again with -

So, now a contains [3, 4]. This becomes clearer if you add more print statements -

a = [1, 2, 3, 4]

a= a[1:]

a= a[1:]

You are doing something similar with apps_data.

1 Like

2 posts were split to a new topic: Dividing by length